xref: /openbmc/linux/fs/erofs/xattr.c (revision 3acea5fc)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017-2018 HUAWEI, Inc.
4  *             https://www.huawei.com/
5  * Copyright (C) 2021-2022, Alibaba Cloud
6  */
7 #include <linux/security.h>
8 #include "xattr.h"
9 
10 struct xattr_iter {
11 	struct super_block *sb;
12 	struct erofs_buf buf;
13 	void *kaddr;
14 
15 	erofs_blk_t blkaddr;
16 	unsigned int ofs;
17 };
18 
19 static int init_inode_xattrs(struct inode *inode)
20 {
21 	struct erofs_inode *const vi = EROFS_I(inode);
22 	struct xattr_iter it;
23 	unsigned int i;
24 	struct erofs_xattr_ibody_header *ih;
25 	struct super_block *sb = inode->i_sb;
26 	int ret = 0;
27 
28 	/* the most case is that xattrs of this inode are initialized. */
29 	if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) {
30 		/*
31 		 * paired with smp_mb() at the end of the function to ensure
32 		 * fields will only be observed after the bit is set.
33 		 */
34 		smp_mb();
35 		return 0;
36 	}
37 
38 	if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE))
39 		return -ERESTARTSYS;
40 
41 	/* someone has initialized xattrs for us? */
42 	if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags))
43 		goto out_unlock;
44 
45 	/*
46 	 * bypass all xattr operations if ->xattr_isize is not greater than
47 	 * sizeof(struct erofs_xattr_ibody_header), in detail:
48 	 * 1) it is not enough to contain erofs_xattr_ibody_header then
49 	 *    ->xattr_isize should be 0 (it means no xattr);
50 	 * 2) it is just to contain erofs_xattr_ibody_header, which is on-disk
51 	 *    undefined right now (maybe use later with some new sb feature).
52 	 */
53 	if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) {
54 		erofs_err(sb,
55 			  "xattr_isize %d of nid %llu is not supported yet",
56 			  vi->xattr_isize, vi->nid);
57 		ret = -EOPNOTSUPP;
58 		goto out_unlock;
59 	} else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) {
60 		if (vi->xattr_isize) {
61 			erofs_err(sb, "bogus xattr ibody @ nid %llu", vi->nid);
62 			DBG_BUGON(1);
63 			ret = -EFSCORRUPTED;
64 			goto out_unlock;	/* xattr ondisk layout error */
65 		}
66 		ret = -ENOATTR;
67 		goto out_unlock;
68 	}
69 
70 	it.buf = __EROFS_BUF_INITIALIZER;
71 	it.blkaddr = erofs_blknr(sb, erofs_iloc(inode) + vi->inode_isize);
72 	it.ofs = erofs_blkoff(sb, erofs_iloc(inode) + vi->inode_isize);
73 
74 	/* read in shared xattr array (non-atomic, see kmalloc below) */
75 	it.kaddr = erofs_read_metabuf(&it.buf, sb, it.blkaddr, EROFS_KMAP);
76 	if (IS_ERR(it.kaddr)) {
77 		ret = PTR_ERR(it.kaddr);
78 		goto out_unlock;
79 	}
80 
81 	ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
82 	vi->xattr_shared_count = ih->h_shared_count;
83 	vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
84 						sizeof(uint), GFP_KERNEL);
85 	if (!vi->xattr_shared_xattrs) {
86 		erofs_put_metabuf(&it.buf);
87 		ret = -ENOMEM;
88 		goto out_unlock;
89 	}
90 
91 	/* let's skip ibody header */
92 	it.ofs += sizeof(struct erofs_xattr_ibody_header);
93 
94 	for (i = 0; i < vi->xattr_shared_count; ++i) {
95 		if (it.ofs >= sb->s_blocksize) {
96 			/* cannot be unaligned */
97 			DBG_BUGON(it.ofs != sb->s_blocksize);
98 
99 			it.kaddr = erofs_read_metabuf(&it.buf, sb, ++it.blkaddr,
100 						      EROFS_KMAP);
101 			if (IS_ERR(it.kaddr)) {
102 				kfree(vi->xattr_shared_xattrs);
103 				vi->xattr_shared_xattrs = NULL;
104 				ret = PTR_ERR(it.kaddr);
105 				goto out_unlock;
106 			}
107 			it.ofs = 0;
108 		}
109 		vi->xattr_shared_xattrs[i] =
110 			le32_to_cpu(*(__le32 *)(it.kaddr + it.ofs));
111 		it.ofs += sizeof(__le32);
112 	}
113 	erofs_put_metabuf(&it.buf);
114 
115 	/* paired with smp_mb() at the beginning of the function. */
116 	smp_mb();
117 	set_bit(EROFS_I_EA_INITED_BIT, &vi->flags);
118 
119 out_unlock:
120 	clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags);
121 	return ret;
122 }
123 
124 /*
125  * the general idea for these return values is
126  * if    0 is returned, go on processing the current xattr;
127  *       1 (> 0) is returned, skip this round to process the next xattr;
128  *    -err (< 0) is returned, an error (maybe ENOXATTR) occurred
129  *                            and need to be handled
130  */
131 struct xattr_iter_handlers {
132 	int (*entry)(struct xattr_iter *_it, struct erofs_xattr_entry *entry);
133 	int (*name)(struct xattr_iter *_it, unsigned int processed, char *buf,
134 		    unsigned int len);
135 	int (*alloc_buffer)(struct xattr_iter *_it, unsigned int value_sz);
136 	void (*value)(struct xattr_iter *_it, unsigned int processed, char *buf,
137 		      unsigned int len);
138 };
139 
140 static inline int xattr_iter_fixup(struct xattr_iter *it)
141 {
142 	if (it->ofs < it->sb->s_blocksize)
143 		return 0;
144 
145 	it->blkaddr += erofs_blknr(it->sb, it->ofs);
146 	it->kaddr = erofs_read_metabuf(&it->buf, it->sb, it->blkaddr,
147 				       EROFS_KMAP);
148 	if (IS_ERR(it->kaddr))
149 		return PTR_ERR(it->kaddr);
150 	it->ofs = erofs_blkoff(it->sb, it->ofs);
151 	return 0;
152 }
153 
154 static int inline_xattr_iter_begin(struct xattr_iter *it,
155 				   struct inode *inode)
156 {
157 	struct erofs_inode *const vi = EROFS_I(inode);
158 	unsigned int xattr_header_sz, inline_xattr_ofs;
159 
160 	xattr_header_sz = inlinexattr_header_size(inode);
161 	if (xattr_header_sz >= vi->xattr_isize) {
162 		DBG_BUGON(xattr_header_sz > vi->xattr_isize);
163 		return -ENOATTR;
164 	}
165 
166 	inline_xattr_ofs = vi->inode_isize + xattr_header_sz;
167 
168 	it->blkaddr = erofs_blknr(it->sb, erofs_iloc(inode) + inline_xattr_ofs);
169 	it->ofs = erofs_blkoff(it->sb, erofs_iloc(inode) + inline_xattr_ofs);
170 	it->kaddr = erofs_read_metabuf(&it->buf, inode->i_sb, it->blkaddr,
171 				       EROFS_KMAP);
172 	if (IS_ERR(it->kaddr))
173 		return PTR_ERR(it->kaddr);
174 	return vi->xattr_isize - xattr_header_sz;
175 }
176 
177 /*
178  * Regardless of success or failure, `xattr_foreach' will end up with
179  * `ofs' pointing to the next xattr item rather than an arbitrary position.
180  */
181 static int xattr_foreach(struct xattr_iter *it,
182 			 const struct xattr_iter_handlers *op,
183 			 unsigned int *tlimit)
184 {
185 	struct erofs_xattr_entry entry;
186 	unsigned int value_sz, processed, slice;
187 	int err;
188 
189 	/* 0. fixup blkaddr, ofs, ipage */
190 	err = xattr_iter_fixup(it);
191 	if (err)
192 		return err;
193 
194 	/*
195 	 * 1. read xattr entry to the memory,
196 	 *    since we do EROFS_XATTR_ALIGN
197 	 *    therefore entry should be in the page
198 	 */
199 	entry = *(struct erofs_xattr_entry *)(it->kaddr + it->ofs);
200 	if (tlimit) {
201 		unsigned int entry_sz = erofs_xattr_entry_size(&entry);
202 
203 		/* xattr on-disk corruption: xattr entry beyond xattr_isize */
204 		if (*tlimit < entry_sz) {
205 			DBG_BUGON(1);
206 			return -EFSCORRUPTED;
207 		}
208 		*tlimit -= entry_sz;
209 	}
210 
211 	it->ofs += sizeof(struct erofs_xattr_entry);
212 	value_sz = le16_to_cpu(entry.e_value_size);
213 
214 	/* handle entry */
215 	err = op->entry(it, &entry);
216 	if (err) {
217 		it->ofs += entry.e_name_len + value_sz;
218 		goto out;
219 	}
220 
221 	/* 2. handle xattr name (ofs will finally be at the end of name) */
222 	processed = 0;
223 
224 	while (processed < entry.e_name_len) {
225 		if (it->ofs >= it->sb->s_blocksize) {
226 			DBG_BUGON(it->ofs > it->sb->s_blocksize);
227 
228 			err = xattr_iter_fixup(it);
229 			if (err)
230 				goto out;
231 			it->ofs = 0;
232 		}
233 
234 		slice = min_t(unsigned int, it->sb->s_blocksize - it->ofs,
235 			      entry.e_name_len - processed);
236 
237 		/* handle name */
238 		err = op->name(it, processed, it->kaddr + it->ofs, slice);
239 		if (err) {
240 			it->ofs += entry.e_name_len - processed + value_sz;
241 			goto out;
242 		}
243 
244 		it->ofs += slice;
245 		processed += slice;
246 	}
247 
248 	/* 3. handle xattr value */
249 	processed = 0;
250 
251 	if (op->alloc_buffer) {
252 		err = op->alloc_buffer(it, value_sz);
253 		if (err) {
254 			it->ofs += value_sz;
255 			goto out;
256 		}
257 	}
258 
259 	while (processed < value_sz) {
260 		if (it->ofs >= it->sb->s_blocksize) {
261 			DBG_BUGON(it->ofs > it->sb->s_blocksize);
262 
263 			err = xattr_iter_fixup(it);
264 			if (err)
265 				goto out;
266 			it->ofs = 0;
267 		}
268 
269 		slice = min_t(unsigned int, it->sb->s_blocksize - it->ofs,
270 			      value_sz - processed);
271 		op->value(it, processed, it->kaddr + it->ofs, slice);
272 		it->ofs += slice;
273 		processed += slice;
274 	}
275 
276 out:
277 	/* xattrs should be 4-byte aligned (on-disk constraint) */
278 	it->ofs = EROFS_XATTR_ALIGN(it->ofs);
279 	return err < 0 ? err : 0;
280 }
281 
282 struct getxattr_iter {
283 	struct xattr_iter it;
284 
285 	char *buffer;
286 	int buffer_size, index;
287 	struct qstr name;
288 };
289 
290 static int xattr_entrymatch(struct xattr_iter *_it,
291 			    struct erofs_xattr_entry *entry)
292 {
293 	struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
294 
295 	return (it->index != entry->e_name_index ||
296 		it->name.len != entry->e_name_len) ? -ENOATTR : 0;
297 }
298 
299 static int xattr_namematch(struct xattr_iter *_it,
300 			   unsigned int processed, char *buf, unsigned int len)
301 {
302 	struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
303 
304 	return memcmp(buf, it->name.name + processed, len) ? -ENOATTR : 0;
305 }
306 
307 static int xattr_checkbuffer(struct xattr_iter *_it,
308 			     unsigned int value_sz)
309 {
310 	struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
311 	int err = it->buffer_size < value_sz ? -ERANGE : 0;
312 
313 	it->buffer_size = value_sz;
314 	return !it->buffer ? 1 : err;
315 }
316 
317 static void xattr_copyvalue(struct xattr_iter *_it,
318 			    unsigned int processed,
319 			    char *buf, unsigned int len)
320 {
321 	struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
322 
323 	memcpy(it->buffer + processed, buf, len);
324 }
325 
326 static const struct xattr_iter_handlers find_xattr_handlers = {
327 	.entry = xattr_entrymatch,
328 	.name = xattr_namematch,
329 	.alloc_buffer = xattr_checkbuffer,
330 	.value = xattr_copyvalue
331 };
332 
333 static int inline_getxattr(struct inode *inode, struct getxattr_iter *it)
334 {
335 	int ret;
336 	unsigned int remaining;
337 
338 	ret = inline_xattr_iter_begin(&it->it, inode);
339 	if (ret < 0)
340 		return ret;
341 
342 	remaining = ret;
343 	while (remaining) {
344 		ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining);
345 		if (ret != -ENOATTR)
346 			break;
347 	}
348 	return ret ? ret : it->buffer_size;
349 }
350 
351 static int shared_getxattr(struct inode *inode, struct getxattr_iter *it)
352 {
353 	struct erofs_inode *const vi = EROFS_I(inode);
354 	struct super_block *const sb = inode->i_sb;
355 	unsigned int i;
356 	int ret = -ENOATTR;
357 
358 	for (i = 0; i < vi->xattr_shared_count; ++i) {
359 		erofs_blk_t blkaddr =
360 			xattrblock_addr(sb, vi->xattr_shared_xattrs[i]);
361 
362 		it->it.ofs = xattrblock_offset(sb, vi->xattr_shared_xattrs[i]);
363 		it->it.kaddr = erofs_read_metabuf(&it->it.buf, sb, blkaddr,
364 						  EROFS_KMAP);
365 		if (IS_ERR(it->it.kaddr))
366 			return PTR_ERR(it->it.kaddr);
367 		it->it.blkaddr = blkaddr;
368 
369 		ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL);
370 		if (ret != -ENOATTR)
371 			break;
372 	}
373 	return ret ? ret : it->buffer_size;
374 }
375 
376 static bool erofs_xattr_user_list(struct dentry *dentry)
377 {
378 	return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER);
379 }
380 
381 static bool erofs_xattr_trusted_list(struct dentry *dentry)
382 {
383 	return capable(CAP_SYS_ADMIN);
384 }
385 
386 int erofs_getxattr(struct inode *inode, int index,
387 		   const char *name,
388 		   void *buffer, size_t buffer_size)
389 {
390 	int ret;
391 	struct getxattr_iter it;
392 
393 	if (!name)
394 		return -EINVAL;
395 
396 	ret = init_inode_xattrs(inode);
397 	if (ret)
398 		return ret;
399 
400 	it.index = index;
401 	it.name.len = strlen(name);
402 	if (it.name.len > EROFS_NAME_LEN)
403 		return -ERANGE;
404 
405 	it.it.buf = __EROFS_BUF_INITIALIZER;
406 	it.name.name = name;
407 
408 	it.buffer = buffer;
409 	it.buffer_size = buffer_size;
410 
411 	it.it.sb = inode->i_sb;
412 	ret = inline_getxattr(inode, &it);
413 	if (ret == -ENOATTR)
414 		ret = shared_getxattr(inode, &it);
415 	erofs_put_metabuf(&it.it.buf);
416 	return ret;
417 }
418 
419 static int erofs_xattr_generic_get(const struct xattr_handler *handler,
420 				   struct dentry *unused, struct inode *inode,
421 				   const char *name, void *buffer, size_t size)
422 {
423 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
424 
425 	switch (handler->flags) {
426 	case EROFS_XATTR_INDEX_USER:
427 		if (!test_opt(&sbi->opt, XATTR_USER))
428 			return -EOPNOTSUPP;
429 		break;
430 	case EROFS_XATTR_INDEX_TRUSTED:
431 		break;
432 	case EROFS_XATTR_INDEX_SECURITY:
433 		break;
434 	default:
435 		return -EINVAL;
436 	}
437 
438 	return erofs_getxattr(inode, handler->flags, name, buffer, size);
439 }
440 
441 const struct xattr_handler erofs_xattr_user_handler = {
442 	.prefix	= XATTR_USER_PREFIX,
443 	.flags	= EROFS_XATTR_INDEX_USER,
444 	.list	= erofs_xattr_user_list,
445 	.get	= erofs_xattr_generic_get,
446 };
447 
448 const struct xattr_handler erofs_xattr_trusted_handler = {
449 	.prefix	= XATTR_TRUSTED_PREFIX,
450 	.flags	= EROFS_XATTR_INDEX_TRUSTED,
451 	.list	= erofs_xattr_trusted_list,
452 	.get	= erofs_xattr_generic_get,
453 };
454 
455 #ifdef CONFIG_EROFS_FS_SECURITY
456 const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
457 	.prefix	= XATTR_SECURITY_PREFIX,
458 	.flags	= EROFS_XATTR_INDEX_SECURITY,
459 	.get	= erofs_xattr_generic_get,
460 };
461 #endif
462 
463 const struct xattr_handler *erofs_xattr_handlers[] = {
464 	&erofs_xattr_user_handler,
465 #ifdef CONFIG_EROFS_FS_POSIX_ACL
466 	&posix_acl_access_xattr_handler,
467 	&posix_acl_default_xattr_handler,
468 #endif
469 	&erofs_xattr_trusted_handler,
470 #ifdef CONFIG_EROFS_FS_SECURITY
471 	&erofs_xattr_security_handler,
472 #endif
473 	NULL,
474 };
475 
476 struct listxattr_iter {
477 	struct xattr_iter it;
478 
479 	struct dentry *dentry;
480 	char *buffer;
481 	int buffer_size, buffer_ofs;
482 };
483 
484 static int xattr_entrylist(struct xattr_iter *_it,
485 			   struct erofs_xattr_entry *entry)
486 {
487 	struct listxattr_iter *it =
488 		container_of(_it, struct listxattr_iter, it);
489 	unsigned int prefix_len;
490 	const char *prefix;
491 
492 	const struct xattr_handler *h =
493 		erofs_xattr_handler(entry->e_name_index);
494 
495 	if (!h || (h->list && !h->list(it->dentry)))
496 		return 1;
497 
498 	prefix = xattr_prefix(h);
499 	prefix_len = strlen(prefix);
500 
501 	if (!it->buffer) {
502 		it->buffer_ofs += prefix_len + entry->e_name_len + 1;
503 		return 1;
504 	}
505 
506 	if (it->buffer_ofs + prefix_len
507 		+ entry->e_name_len + 1 > it->buffer_size)
508 		return -ERANGE;
509 
510 	memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
511 	it->buffer_ofs += prefix_len;
512 	return 0;
513 }
514 
515 static int xattr_namelist(struct xattr_iter *_it,
516 			  unsigned int processed, char *buf, unsigned int len)
517 {
518 	struct listxattr_iter *it =
519 		container_of(_it, struct listxattr_iter, it);
520 
521 	memcpy(it->buffer + it->buffer_ofs, buf, len);
522 	it->buffer_ofs += len;
523 	return 0;
524 }
525 
526 static int xattr_skipvalue(struct xattr_iter *_it,
527 			   unsigned int value_sz)
528 {
529 	struct listxattr_iter *it =
530 		container_of(_it, struct listxattr_iter, it);
531 
532 	it->buffer[it->buffer_ofs++] = '\0';
533 	return 1;
534 }
535 
536 static const struct xattr_iter_handlers list_xattr_handlers = {
537 	.entry = xattr_entrylist,
538 	.name = xattr_namelist,
539 	.alloc_buffer = xattr_skipvalue,
540 	.value = NULL
541 };
542 
543 static int inline_listxattr(struct listxattr_iter *it)
544 {
545 	int ret;
546 	unsigned int remaining;
547 
548 	ret = inline_xattr_iter_begin(&it->it, d_inode(it->dentry));
549 	if (ret < 0)
550 		return ret;
551 
552 	remaining = ret;
553 	while (remaining) {
554 		ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining);
555 		if (ret)
556 			break;
557 	}
558 	return ret ? ret : it->buffer_ofs;
559 }
560 
561 static int shared_listxattr(struct listxattr_iter *it)
562 {
563 	struct inode *const inode = d_inode(it->dentry);
564 	struct erofs_inode *const vi = EROFS_I(inode);
565 	struct super_block *const sb = inode->i_sb;
566 	unsigned int i;
567 	int ret = 0;
568 
569 	for (i = 0; i < vi->xattr_shared_count; ++i) {
570 		erofs_blk_t blkaddr =
571 			xattrblock_addr(sb, vi->xattr_shared_xattrs[i]);
572 
573 		it->it.ofs = xattrblock_offset(sb, vi->xattr_shared_xattrs[i]);
574 		it->it.kaddr = erofs_read_metabuf(&it->it.buf, sb, blkaddr,
575 						  EROFS_KMAP);
576 		if (IS_ERR(it->it.kaddr))
577 			return PTR_ERR(it->it.kaddr);
578 		it->it.blkaddr = blkaddr;
579 
580 		ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL);
581 		if (ret)
582 			break;
583 	}
584 	return ret ? ret : it->buffer_ofs;
585 }
586 
587 ssize_t erofs_listxattr(struct dentry *dentry,
588 			char *buffer, size_t buffer_size)
589 {
590 	int ret;
591 	struct listxattr_iter it;
592 
593 	ret = init_inode_xattrs(d_inode(dentry));
594 	if (ret == -ENOATTR)
595 		return 0;
596 	if (ret)
597 		return ret;
598 
599 	it.it.buf = __EROFS_BUF_INITIALIZER;
600 	it.dentry = dentry;
601 	it.buffer = buffer;
602 	it.buffer_size = buffer_size;
603 	it.buffer_ofs = 0;
604 
605 	it.it.sb = dentry->d_sb;
606 
607 	ret = inline_listxattr(&it);
608 	if (ret >= 0 || ret == -ENOATTR)
609 		ret = shared_listxattr(&it);
610 	erofs_put_metabuf(&it.it.buf);
611 	return ret;
612 }
613 
614 #ifdef CONFIG_EROFS_FS_POSIX_ACL
615 struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
616 {
617 	struct posix_acl *acl;
618 	int prefix, rc;
619 	char *value = NULL;
620 
621 	if (rcu)
622 		return ERR_PTR(-ECHILD);
623 
624 	switch (type) {
625 	case ACL_TYPE_ACCESS:
626 		prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
627 		break;
628 	case ACL_TYPE_DEFAULT:
629 		prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
630 		break;
631 	default:
632 		return ERR_PTR(-EINVAL);
633 	}
634 
635 	rc = erofs_getxattr(inode, prefix, "", NULL, 0);
636 	if (rc > 0) {
637 		value = kmalloc(rc, GFP_KERNEL);
638 		if (!value)
639 			return ERR_PTR(-ENOMEM);
640 		rc = erofs_getxattr(inode, prefix, "", value, rc);
641 	}
642 
643 	if (rc == -ENOATTR)
644 		acl = NULL;
645 	else if (rc < 0)
646 		acl = ERR_PTR(rc);
647 	else
648 		acl = posix_acl_from_xattr(&init_user_ns, value, rc);
649 	kfree(value);
650 	return acl;
651 }
652 #endif
653