1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2007 Red Hat. All rights reserved.
4 */
5
6 #include <linux/init.h>
7 #include <linux/fs.h>
8 #include <linux/slab.h>
9 #include <linux/rwsem.h>
10 #include <linux/xattr.h>
11 #include <linux/security.h>
12 #include <linux/posix_acl_xattr.h>
13 #include <linux/iversion.h>
14 #include <linux/sched/mm.h>
15 #include "ctree.h"
16 #include "fs.h"
17 #include "messages.h"
18 #include "btrfs_inode.h"
19 #include "transaction.h"
20 #include "xattr.h"
21 #include "disk-io.h"
22 #include "props.h"
23 #include "locking.h"
24 #include "accessors.h"
25 #include "dir-item.h"
26
btrfs_getxattr(const struct inode * inode,const char * name,void * buffer,size_t size)27 int btrfs_getxattr(const struct inode *inode, const char *name,
28 void *buffer, size_t size)
29 {
30 struct btrfs_dir_item *di;
31 struct btrfs_root *root = BTRFS_I(inode)->root;
32 BTRFS_PATH_AUTO_FREE(path);
33 struct extent_buffer *leaf;
34 unsigned long data_ptr;
35
36 path = btrfs_alloc_path();
37 if (!path)
38 return -ENOMEM;
39
40 /* lookup the xattr by name */
41 di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(BTRFS_I(inode)),
42 name, strlen(name), 0);
43 if (!di)
44 return -ENODATA;
45 if (IS_ERR(di))
46 return PTR_ERR(di);
47
48 leaf = path->nodes[0];
49 /* if size is 0, that means we want the size of the attr */
50 if (!size)
51 return btrfs_dir_data_len(leaf, di);
52
53 /* now get the data out of our dir_item */
54 if (btrfs_dir_data_len(leaf, di) > size)
55 return -ERANGE;
56
57 /*
58 * The way things are packed into the leaf is like this
59 * |struct btrfs_dir_item|name|data|
60 * where name is the xattr name, so security.foo, and data is the
61 * content of the xattr. data_ptr points to the location in memory
62 * where the data starts in the in memory leaf
63 */
64 data_ptr = (unsigned long)((char *)(di + 1) +
65 btrfs_dir_name_len(leaf, di));
66 read_extent_buffer(leaf, buffer, data_ptr,
67 btrfs_dir_data_len(leaf, di));
68 return btrfs_dir_data_len(leaf, di);
69 }
70
btrfs_setxattr(struct btrfs_trans_handle * trans,struct inode * inode,const char * name,const void * value,size_t size,int flags)71 int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode,
72 const char *name, const void *value, size_t size, int flags)
73 {
74 struct btrfs_dir_item *di = NULL;
75 struct btrfs_root *root = BTRFS_I(inode)->root;
76 BTRFS_PATH_AUTO_FREE(path);
77 size_t name_len = strlen(name);
78 int ret = 0;
79
80 ASSERT(trans);
81
82 if (name_len + size > BTRFS_MAX_XATTR_SIZE(root->fs_info))
83 return -ENOSPC;
84
85 path = btrfs_alloc_path();
86 if (!path)
87 return -ENOMEM;
88 path->skip_release_on_error = true;
89
90 if (!value) {
91 di = btrfs_lookup_xattr(trans, root, path,
92 btrfs_ino(BTRFS_I(inode)), name, name_len, -1);
93 if (!di && (flags & XATTR_REPLACE))
94 ret = -ENODATA;
95 else if (IS_ERR(di))
96 ret = PTR_ERR(di);
97 else if (di)
98 ret = btrfs_delete_one_dir_name(trans, root, path, di);
99 goto out;
100 }
101
102 /*
103 * For a replace we can't just do the insert blindly.
104 * Do a lookup first (read-only btrfs_search_slot), and return if xattr
105 * doesn't exist. If it exists, fall down below to the insert/replace
106 * path - we can't race with a concurrent xattr delete, because the VFS
107 * locks the inode's i_mutex before calling setxattr or removexattr.
108 */
109 if (flags & XATTR_REPLACE) {
110 btrfs_assert_inode_locked(BTRFS_I(inode));
111 di = btrfs_lookup_xattr(NULL, root, path,
112 btrfs_ino(BTRFS_I(inode)), name, name_len, 0);
113 if (!di)
114 ret = -ENODATA;
115 else if (IS_ERR(di))
116 ret = PTR_ERR(di);
117 if (ret)
118 goto out;
119 btrfs_release_path(path);
120 di = NULL;
121 }
122
123 ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(BTRFS_I(inode)),
124 name, name_len, value, size);
125 if (ret == -EOVERFLOW) {
126 /*
127 * We have an existing item in a leaf, split_leaf couldn't
128 * expand it. That item might have or not a dir_item that
129 * matches our target xattr, so lets check.
130 */
131 ret = 0;
132 btrfs_assert_tree_write_locked(path->nodes[0]);
133 di = btrfs_match_dir_item_name(path, name, name_len);
134 if (!di && !(flags & XATTR_REPLACE)) {
135 ret = -ENOSPC;
136 goto out;
137 }
138 } else if (ret == -EEXIST) {
139 ret = 0;
140 di = btrfs_match_dir_item_name(path, name, name_len);
141 ASSERT(di); /* logic error */
142 } else if (ret) {
143 goto out;
144 }
145
146 if (di && (flags & XATTR_CREATE)) {
147 ret = -EEXIST;
148 goto out;
149 }
150
151 if (di) {
152 /*
153 * We're doing a replace, and it must be atomic, that is, at
154 * any point in time we have either the old or the new xattr
155 * value in the tree. We don't want readers (getxattr and
156 * listxattrs) to miss a value, this is specially important
157 * for ACLs.
158 */
159 const int slot = path->slots[0];
160 struct extent_buffer *leaf = path->nodes[0];
161 const u16 old_data_len = btrfs_dir_data_len(leaf, di);
162 const u32 item_size = btrfs_item_size(leaf, slot);
163 const u32 data_size = sizeof(*di) + name_len + size;
164 unsigned long name_ptr;
165 unsigned long data_ptr;
166 char *ptr;
167
168 if (size > old_data_len) {
169 if (btrfs_leaf_free_space(leaf) <
170 (size - old_data_len)) {
171 ret = -ENOSPC;
172 goto out;
173 }
174 }
175
176 if (old_data_len + name_len + sizeof(*di) == item_size) {
177 /* No other xattrs packed in the same leaf item. */
178 if (size > old_data_len)
179 btrfs_extend_item(trans, path, size - old_data_len);
180 else if (size < old_data_len)
181 btrfs_truncate_item(trans, path, data_size, 1);
182 } else {
183 /* There are other xattrs packed in the same item. */
184 ret = btrfs_delete_one_dir_name(trans, root, path, di);
185 if (ret)
186 goto out;
187 btrfs_extend_item(trans, path, data_size);
188 }
189
190 ptr = btrfs_item_ptr(leaf, slot, char);
191 ptr += btrfs_item_size(leaf, slot) - data_size;
192 di = (struct btrfs_dir_item *)ptr;
193 memzero_extent_buffer(leaf, (unsigned long)ptr +
194 offsetof(struct btrfs_dir_item, location),
195 sizeof(struct btrfs_disk_key));
196 btrfs_set_dir_flags(leaf, di, BTRFS_FT_XATTR);
197 btrfs_set_dir_transid(leaf, di, trans->transid);
198 btrfs_set_dir_name_len(leaf, di, name_len);
199 btrfs_set_dir_data_len(leaf, di, size);
200 name_ptr = (unsigned long)(di + 1);
201 data_ptr = ((unsigned long)(di + 1)) + name_len;
202 write_extent_buffer(leaf, name, name_ptr, name_len);
203 write_extent_buffer(leaf, value, data_ptr, size);
204 } else {
205 /*
206 * Insert, and we had space for the xattr, so path->slots[0] is
207 * where our xattr dir_item is and btrfs_insert_xattr_item()
208 * filled it.
209 */
210 }
211 out:
212 if (!ret) {
213 set_bit(BTRFS_INODE_COPY_EVERYTHING,
214 &BTRFS_I(inode)->runtime_flags);
215 clear_bit(BTRFS_INODE_NO_XATTRS, &BTRFS_I(inode)->runtime_flags);
216 }
217 return ret;
218 }
219
220 /*
221 * @value: "" makes the attribute to empty, NULL removes it
222 */
btrfs_setxattr_trans(struct inode * inode,const char * name,const void * value,size_t size,int flags)223 int btrfs_setxattr_trans(struct inode *inode, const char *name,
224 const void *value, size_t size, int flags)
225 {
226 struct btrfs_root *root = BTRFS_I(inode)->root;
227 struct btrfs_trans_handle *trans;
228 const bool start_trans = (current->journal_info == NULL);
229 int ret;
230
231 if (start_trans) {
232 /*
233 * 1 unit for inserting/updating/deleting the xattr
234 * 1 unit for the inode item update
235 */
236 trans = btrfs_start_transaction(root, 2);
237 if (IS_ERR(trans))
238 return PTR_ERR(trans);
239 } else {
240 /*
241 * This can happen when smack is enabled and a directory is being
242 * created. It happens through d_instantiate_new(), which calls
243 * smack_d_instantiate(), which in turn calls __vfs_setxattr() to
244 * set the transmute xattr (XATTR_NAME_SMACKTRANSMUTE) on the
245 * inode. We have already reserved space for the xattr and inode
246 * update at btrfs_mkdir(), so just use the transaction handle.
247 * We don't join or start a transaction, as that will reset the
248 * block_rsv of the handle and trigger a warning for the start
249 * case.
250 */
251 ASSERT(strncmp(name, XATTR_SECURITY_PREFIX,
252 XATTR_SECURITY_PREFIX_LEN) == 0);
253 trans = current->journal_info;
254 }
255
256 ret = btrfs_setxattr(trans, inode, name, value, size, flags);
257 if (ret)
258 goto out;
259
260 inode_inc_iversion(inode);
261 inode_set_ctime_current(inode);
262 ret = btrfs_update_inode(trans, BTRFS_I(inode));
263 if (ret)
264 btrfs_abort_transaction(trans, ret);
265 out:
266 if (start_trans)
267 btrfs_end_transaction(trans);
268 return ret;
269 }
270
btrfs_listxattr(struct dentry * dentry,char * buffer,size_t size)271 ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
272 {
273 struct btrfs_key found_key;
274 struct btrfs_key key;
275 struct inode *inode = d_inode(dentry);
276 struct btrfs_root *root = BTRFS_I(inode)->root;
277 BTRFS_PATH_AUTO_FREE(path);
278 int iter_ret = 0;
279 int ret = 0;
280 size_t total_size = 0, size_left = size;
281
282 /*
283 * ok we want all objects associated with this id.
284 * NOTE: we set key.offset = 0; because we want to start with the
285 * first xattr that we find and walk forward
286 */
287 key.objectid = btrfs_ino(BTRFS_I(inode));
288 key.type = BTRFS_XATTR_ITEM_KEY;
289 key.offset = 0;
290
291 path = btrfs_alloc_path();
292 if (!path)
293 return -ENOMEM;
294 path->reada = READA_FORWARD;
295
296 /* search for our xattrs */
297 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
298 struct extent_buffer *leaf;
299 int slot;
300 struct btrfs_dir_item *di;
301 u32 item_size;
302 u32 cur;
303
304 leaf = path->nodes[0];
305 slot = path->slots[0];
306
307 /* check to make sure this item is what we want */
308 if (found_key.objectid != key.objectid)
309 break;
310 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
311 break;
312 if (found_key.type < BTRFS_XATTR_ITEM_KEY)
313 continue;
314
315 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
316 item_size = btrfs_item_size(leaf, slot);
317 cur = 0;
318 while (cur < item_size) {
319 u16 name_len = btrfs_dir_name_len(leaf, di);
320 u16 data_len = btrfs_dir_data_len(leaf, di);
321 u32 this_len = sizeof(*di) + name_len + data_len;
322 unsigned long name_ptr = (unsigned long)(di + 1);
323
324 total_size += name_len + 1;
325 /*
326 * We are just looking for how big our buffer needs to
327 * be.
328 */
329 if (!size)
330 goto next;
331
332 if (!buffer || (name_len + 1) > size_left)
333 return -ERANGE;
334
335 read_extent_buffer(leaf, buffer, name_ptr, name_len);
336 buffer[name_len] = '\0';
337
338 size_left -= name_len + 1;
339 buffer += name_len + 1;
340 next:
341 cur += this_len;
342 di = (struct btrfs_dir_item *)((char *)di + this_len);
343 }
344 }
345
346 if (iter_ret < 0)
347 ret = iter_ret;
348 else
349 ret = total_size;
350
351 return ret;
352 }
353
btrfs_xattr_handler_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size)354 static int btrfs_xattr_handler_get(const struct xattr_handler *handler,
355 struct dentry *unused, struct inode *inode,
356 const char *name, void *buffer, size_t size)
357 {
358 name = xattr_full_name(handler, name);
359 return btrfs_getxattr(inode, name, buffer, size);
360 }
361
btrfs_xattr_handler_set(const struct xattr_handler * handler,const struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * name,const void * buffer,size_t size,int flags)362 static int btrfs_xattr_handler_set(const struct xattr_handler *handler,
363 const struct mnt_idmap *idmap,
364 struct dentry *unused, struct inode *inode,
365 const char *name, const void *buffer,
366 size_t size, int flags)
367 {
368 if (btrfs_root_readonly(BTRFS_I(inode)->root))
369 return -EROFS;
370
371 name = xattr_full_name(handler, name);
372 return btrfs_setxattr_trans(inode, name, buffer, size, flags);
373 }
374
btrfs_xattr_handler_get_security(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size)375 static int btrfs_xattr_handler_get_security(const struct xattr_handler *handler,
376 struct dentry *unused,
377 struct inode *inode,
378 const char *name, void *buffer,
379 size_t size)
380 {
381 int ret;
382 bool is_cap = false;
383
384 name = xattr_full_name(handler, name);
385
386 /*
387 * security.capability doesn't cache the results, so calls into us
388 * constantly to see if there's a capability xattr. Cache the result
389 * here in order to avoid wasting time doing lookups for xattrs we know
390 * don't exist.
391 */
392 if (strcmp(name, XATTR_NAME_CAPS) == 0) {
393 is_cap = true;
394 if (test_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags))
395 return -ENODATA;
396 }
397
398 ret = btrfs_getxattr(inode, name, buffer, size);
399 if (ret == -ENODATA && is_cap)
400 set_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags);
401 return ret;
402 }
403
btrfs_xattr_handler_set_security(const struct xattr_handler * handler,const struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * name,const void * buffer,size_t size,int flags)404 static int btrfs_xattr_handler_set_security(const struct xattr_handler *handler,
405 const struct mnt_idmap *idmap,
406 struct dentry *unused,
407 struct inode *inode,
408 const char *name,
409 const void *buffer,
410 size_t size, int flags)
411 {
412 if (btrfs_root_readonly(BTRFS_I(inode)->root))
413 return -EROFS;
414
415 name = xattr_full_name(handler, name);
416 if (strcmp(name, XATTR_NAME_CAPS) == 0)
417 clear_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags);
418
419 return btrfs_setxattr_trans(inode, name, buffer, size, flags);
420 }
421
btrfs_xattr_handler_set_prop(const struct xattr_handler * handler,const struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * name,const void * value,size_t size,int flags)422 static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,
423 const struct mnt_idmap *idmap,
424 struct dentry *unused, struct inode *inode,
425 const char *name, const void *value,
426 size_t size, int flags)
427 {
428 int ret;
429 struct btrfs_trans_handle *trans;
430 struct btrfs_root *root = BTRFS_I(inode)->root;
431
432 name = xattr_full_name(handler, name);
433 ret = btrfs_validate_prop(BTRFS_I(inode), name, value, size);
434 if (ret)
435 return ret;
436
437 if (btrfs_ignore_prop(BTRFS_I(inode), name))
438 return 0;
439
440 trans = btrfs_start_transaction(root, 2);
441 if (IS_ERR(trans))
442 return PTR_ERR(trans);
443
444 ret = btrfs_set_prop(trans, BTRFS_I(inode), name, value, size, flags);
445 if (!ret) {
446 inode_inc_iversion(inode);
447 inode_set_ctime_current(inode);
448 ret = btrfs_update_inode(trans, BTRFS_I(inode));
449 if (ret)
450 btrfs_abort_transaction(trans, ret);
451 }
452
453 btrfs_end_transaction(trans);
454
455 return ret;
456 }
457
458 static const struct xattr_handler btrfs_security_xattr_handler = {
459 .prefix = XATTR_SECURITY_PREFIX,
460 .get = btrfs_xattr_handler_get_security,
461 .set = btrfs_xattr_handler_set_security,
462 };
463
464 static const struct xattr_handler btrfs_trusted_xattr_handler = {
465 .prefix = XATTR_TRUSTED_PREFIX,
466 .get = btrfs_xattr_handler_get,
467 .set = btrfs_xattr_handler_set,
468 };
469
470 static const struct xattr_handler btrfs_user_xattr_handler = {
471 .prefix = XATTR_USER_PREFIX,
472 .get = btrfs_xattr_handler_get,
473 .set = btrfs_xattr_handler_set,
474 };
475
476 static const struct xattr_handler btrfs_btrfs_xattr_handler = {
477 .prefix = XATTR_BTRFS_PREFIX,
478 .get = btrfs_xattr_handler_get,
479 .set = btrfs_xattr_handler_set_prop,
480 };
481
482 const struct xattr_handler * const btrfs_xattr_handlers[] = {
483 &btrfs_security_xattr_handler,
484 &btrfs_trusted_xattr_handler,
485 &btrfs_user_xattr_handler,
486 &btrfs_btrfs_xattr_handler,
487 NULL,
488 };
489
btrfs_initxattrs(struct inode * inode,const struct xattr * xattr_array,void * fs_private)490 static int btrfs_initxattrs(struct inode *inode,
491 const struct xattr *xattr_array, void *fs_private)
492 {
493 struct btrfs_trans_handle *trans = fs_private;
494 const struct xattr *xattr;
495 unsigned int nofs_flag;
496 char *name;
497 int ret = 0;
498
499 /*
500 * We're holding a transaction handle, so use a NOFS memory allocation
501 * context to avoid deadlock if reclaim happens.
502 */
503 nofs_flag = memalloc_nofs_save();
504 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
505 const size_t name_len = XATTR_SECURITY_PREFIX_LEN +
506 strlen(xattr->name) + 1;
507
508 name = kmalloc(name_len, GFP_KERNEL);
509 if (!name) {
510 ret = -ENOMEM;
511 break;
512 }
513 scnprintf(name, name_len, "%s%s", XATTR_SECURITY_PREFIX, xattr->name);
514
515 if (strcmp(name, XATTR_NAME_CAPS) == 0)
516 clear_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags);
517
518 ret = btrfs_setxattr(trans, inode, name, xattr->value,
519 xattr->value_len, 0);
520 kfree(name);
521 if (ret < 0)
522 break;
523 }
524 memalloc_nofs_restore(nofs_flag);
525 return ret;
526 }
527
btrfs_xattr_security_init(struct btrfs_trans_handle * trans,struct inode * inode,struct inode * dir,const struct qstr * qstr)528 int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
529 struct inode *inode, struct inode *dir,
530 const struct qstr *qstr)
531 {
532 return security_inode_init_security(inode, dir, qstr,
533 &btrfs_initxattrs, trans);
534 }
535