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