Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
27int 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
71int 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 data_ptr;
165 char *ptr;
166
167 if (size > old_data_len) {
168 if (btrfs_leaf_free_space(leaf) <
169 (size - old_data_len)) {
170 ret = -ENOSPC;
171 goto out;
172 }
173 }
174
175 if (old_data_len + name_len + sizeof(*di) == item_size) {
176 /* No other xattrs packed in the same leaf item. */
177 if (size > old_data_len)
178 btrfs_extend_item(trans, path, size - old_data_len);
179 else if (size < old_data_len)
180 btrfs_truncate_item(trans, path, data_size, 1);
181 } else {
182 /* There are other xattrs packed in the same item. */
183 ret = btrfs_delete_one_dir_name(trans, root, path, di);
184 if (ret)
185 goto out;
186 btrfs_extend_item(trans, path, data_size);
187 }
188
189 ptr = btrfs_item_ptr(leaf, slot, char);
190 ptr += btrfs_item_size(leaf, slot) - data_size;
191 di = (struct btrfs_dir_item *)ptr;
192 btrfs_set_dir_data_len(leaf, di, size);
193 data_ptr = ((unsigned long)(di + 1)) + name_len;
194 write_extent_buffer(leaf, value, data_ptr, size);
195 } else {
196 /*
197 * Insert, and we had space for the xattr, so path->slots[0] is
198 * where our xattr dir_item is and btrfs_insert_xattr_item()
199 * filled it.
200 */
201 }
202out:
203 if (!ret) {
204 set_bit(BTRFS_INODE_COPY_EVERYTHING,
205 &BTRFS_I(inode)->runtime_flags);
206 clear_bit(BTRFS_INODE_NO_XATTRS, &BTRFS_I(inode)->runtime_flags);
207 }
208 return ret;
209}
210
211/*
212 * @value: "" makes the attribute to empty, NULL removes it
213 */
214int btrfs_setxattr_trans(struct inode *inode, const char *name,
215 const void *value, size_t size, int flags)
216{
217 struct btrfs_root *root = BTRFS_I(inode)->root;
218 struct btrfs_trans_handle *trans;
219 const bool start_trans = (current->journal_info == NULL);
220 int ret;
221
222 if (start_trans) {
223 /*
224 * 1 unit for inserting/updating/deleting the xattr
225 * 1 unit for the inode item update
226 */
227 trans = btrfs_start_transaction(root, 2);
228 if (IS_ERR(trans))
229 return PTR_ERR(trans);
230 } else {
231 /*
232 * This can happen when smack is enabled and a directory is being
233 * created. It happens through d_instantiate_new(), which calls
234 * smack_d_instantiate(), which in turn calls __vfs_setxattr() to
235 * set the transmute xattr (XATTR_NAME_SMACKTRANSMUTE) on the
236 * inode. We have already reserved space for the xattr and inode
237 * update at btrfs_mkdir(), so just use the transaction handle.
238 * We don't join or start a transaction, as that will reset the
239 * block_rsv of the handle and trigger a warning for the start
240 * case.
241 */
242 ASSERT(strncmp(name, XATTR_SECURITY_PREFIX,
243 XATTR_SECURITY_PREFIX_LEN) == 0);
244 trans = current->journal_info;
245 }
246
247 ret = btrfs_setxattr(trans, inode, name, value, size, flags);
248 if (ret)
249 goto out;
250
251 inode_inc_iversion(inode);
252 inode_set_ctime_current(inode);
253 ret = btrfs_update_inode(trans, BTRFS_I(inode));
254 if (ret)
255 btrfs_abort_transaction(trans, ret);
256out:
257 if (start_trans)
258 btrfs_end_transaction(trans);
259 return ret;
260}
261
262ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
263{
264 struct btrfs_key found_key;
265 struct btrfs_key key;
266 struct inode *inode = d_inode(dentry);
267 struct btrfs_root *root = BTRFS_I(inode)->root;
268 BTRFS_PATH_AUTO_FREE(path);
269 int iter_ret = 0;
270 int ret = 0;
271 size_t total_size = 0, size_left = size;
272
273 /*
274 * ok we want all objects associated with this id.
275 * NOTE: we set key.offset = 0; because we want to start with the
276 * first xattr that we find and walk forward
277 */
278 key.objectid = btrfs_ino(BTRFS_I(inode));
279 key.type = BTRFS_XATTR_ITEM_KEY;
280 key.offset = 0;
281
282 path = btrfs_alloc_path();
283 if (!path)
284 return -ENOMEM;
285 path->reada = READA_FORWARD;
286
287 /* search for our xattrs */
288 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
289 struct extent_buffer *leaf;
290 int slot;
291 struct btrfs_dir_item *di;
292 u32 item_size;
293 u32 cur;
294
295 leaf = path->nodes[0];
296 slot = path->slots[0];
297
298 /* check to make sure this item is what we want */
299 if (found_key.objectid != key.objectid)
300 break;
301 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
302 break;
303 if (found_key.type < BTRFS_XATTR_ITEM_KEY)
304 continue;
305
306 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
307 item_size = btrfs_item_size(leaf, slot);
308 cur = 0;
309 while (cur < item_size) {
310 u16 name_len = btrfs_dir_name_len(leaf, di);
311 u16 data_len = btrfs_dir_data_len(leaf, di);
312 u32 this_len = sizeof(*di) + name_len + data_len;
313 unsigned long name_ptr = (unsigned long)(di + 1);
314
315 total_size += name_len + 1;
316 /*
317 * We are just looking for how big our buffer needs to
318 * be.
319 */
320 if (!size)
321 goto next;
322
323 if (!buffer || (name_len + 1) > size_left) {
324 iter_ret = -ERANGE;
325 break;
326 }
327
328 read_extent_buffer(leaf, buffer, name_ptr, name_len);
329 buffer[name_len] = '\0';
330
331 size_left -= name_len + 1;
332 buffer += name_len + 1;
333next:
334 cur += this_len;
335 di = (struct btrfs_dir_item *)((char *)di + this_len);
336 }
337 }
338
339 if (iter_ret < 0)
340 ret = iter_ret;
341 else
342 ret = total_size;
343
344 return ret;
345}
346
347static int btrfs_xattr_handler_get(const struct xattr_handler *handler,
348 struct dentry *unused, struct inode *inode,
349 const char *name, void *buffer, size_t size)
350{
351 name = xattr_full_name(handler, name);
352 return btrfs_getxattr(inode, name, buffer, size);
353}
354
355static int btrfs_xattr_handler_set(const struct xattr_handler *handler,
356 struct mnt_idmap *idmap,
357 struct dentry *unused, struct inode *inode,
358 const char *name, const void *buffer,
359 size_t size, int flags)
360{
361 if (btrfs_root_readonly(BTRFS_I(inode)->root))
362 return -EROFS;
363
364 name = xattr_full_name(handler, name);
365 return btrfs_setxattr_trans(inode, name, buffer, size, flags);
366}
367
368static int btrfs_xattr_handler_get_security(const struct xattr_handler *handler,
369 struct dentry *unused,
370 struct inode *inode,
371 const char *name, void *buffer,
372 size_t size)
373{
374 int ret;
375 bool is_cap = false;
376
377 name = xattr_full_name(handler, name);
378
379 /*
380 * security.capability doesn't cache the results, so calls into us
381 * constantly to see if there's a capability xattr. Cache the result
382 * here in order to avoid wasting time doing lookups for xattrs we know
383 * don't exist.
384 */
385 if (strcmp(name, XATTR_NAME_CAPS) == 0) {
386 is_cap = true;
387 if (test_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags))
388 return -ENODATA;
389 }
390
391 ret = btrfs_getxattr(inode, name, buffer, size);
392 if (ret == -ENODATA && is_cap)
393 set_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags);
394 return ret;
395}
396
397static int btrfs_xattr_handler_set_security(const struct xattr_handler *handler,
398 struct mnt_idmap *idmap,
399 struct dentry *unused,
400 struct inode *inode,
401 const char *name,
402 const void *buffer,
403 size_t size, int flags)
404{
405 if (btrfs_root_readonly(BTRFS_I(inode)->root))
406 return -EROFS;
407
408 name = xattr_full_name(handler, name);
409 if (strcmp(name, XATTR_NAME_CAPS) == 0)
410 clear_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags);
411
412 return btrfs_setxattr_trans(inode, name, buffer, size, flags);
413}
414
415static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,
416 struct mnt_idmap *idmap,
417 struct dentry *unused, struct inode *inode,
418 const char *name, const void *value,
419 size_t size, int flags)
420{
421 int ret;
422 struct btrfs_trans_handle *trans;
423 struct btrfs_root *root = BTRFS_I(inode)->root;
424
425 name = xattr_full_name(handler, name);
426 ret = btrfs_validate_prop(BTRFS_I(inode), name, value, size);
427 if (ret)
428 return ret;
429
430 if (btrfs_ignore_prop(BTRFS_I(inode), name))
431 return 0;
432
433 trans = btrfs_start_transaction(root, 2);
434 if (IS_ERR(trans))
435 return PTR_ERR(trans);
436
437 ret = btrfs_set_prop(trans, BTRFS_I(inode), name, value, size, flags);
438 if (!ret) {
439 inode_inc_iversion(inode);
440 inode_set_ctime_current(inode);
441 ret = btrfs_update_inode(trans, BTRFS_I(inode));
442 if (ret)
443 btrfs_abort_transaction(trans, ret);
444 }
445
446 btrfs_end_transaction(trans);
447
448 return ret;
449}
450
451static const struct xattr_handler btrfs_security_xattr_handler = {
452 .prefix = XATTR_SECURITY_PREFIX,
453 .get = btrfs_xattr_handler_get_security,
454 .set = btrfs_xattr_handler_set_security,
455};
456
457static const struct xattr_handler btrfs_trusted_xattr_handler = {
458 .prefix = XATTR_TRUSTED_PREFIX,
459 .get = btrfs_xattr_handler_get,
460 .set = btrfs_xattr_handler_set,
461};
462
463static const struct xattr_handler btrfs_user_xattr_handler = {
464 .prefix = XATTR_USER_PREFIX,
465 .get = btrfs_xattr_handler_get,
466 .set = btrfs_xattr_handler_set,
467};
468
469static const struct xattr_handler btrfs_btrfs_xattr_handler = {
470 .prefix = XATTR_BTRFS_PREFIX,
471 .get = btrfs_xattr_handler_get,
472 .set = btrfs_xattr_handler_set_prop,
473};
474
475const struct xattr_handler * const btrfs_xattr_handlers[] = {
476 &btrfs_security_xattr_handler,
477 &btrfs_trusted_xattr_handler,
478 &btrfs_user_xattr_handler,
479 &btrfs_btrfs_xattr_handler,
480 NULL,
481};
482
483static int btrfs_initxattrs(struct inode *inode,
484 const struct xattr *xattr_array, void *fs_private)
485{
486 struct btrfs_trans_handle *trans = fs_private;
487 const struct xattr *xattr;
488 unsigned int nofs_flag;
489 char *name;
490 int ret = 0;
491
492 /*
493 * We're holding a transaction handle, so use a NOFS memory allocation
494 * context to avoid deadlock if reclaim happens.
495 */
496 nofs_flag = memalloc_nofs_save();
497 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
498 const size_t name_len = XATTR_SECURITY_PREFIX_LEN +
499 strlen(xattr->name) + 1;
500
501 name = kmalloc(name_len, GFP_KERNEL);
502 if (!name) {
503 ret = -ENOMEM;
504 break;
505 }
506 scnprintf(name, name_len, "%s%s", XATTR_SECURITY_PREFIX, xattr->name);
507
508 if (strcmp(name, XATTR_NAME_CAPS) == 0)
509 clear_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags);
510
511 ret = btrfs_setxattr(trans, inode, name, xattr->value,
512 xattr->value_len, 0);
513 kfree(name);
514 if (ret < 0)
515 break;
516 }
517 memalloc_nofs_restore(nofs_flag);
518 return ret;
519}
520
521int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
522 struct inode *inode, struct inode *dir,
523 const struct qstr *qstr)
524{
525 return security_inode_init_security(inode, dir, qstr,
526 &btrfs_initxattrs, trans);
527}