xref: /openbmc/linux/fs/erofs/xattr.c (revision d47a97bd)
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(erofs_iloc(inode) + vi->inode_isize);
72 	it.ofs = erofs_blkoff(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 >= EROFS_BLKSIZ) {
96 			/* cannot be unaligned */
97 			DBG_BUGON(it.ofs != EROFS_BLKSIZ);
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 < EROFS_BLKSIZ)
143 		return 0;
144 
145 	it->blkaddr += erofs_blknr(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->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(erofs_iloc(inode) + inline_xattr_ofs);
169 	it->ofs = erofs_blkoff(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 >= EROFS_BLKSIZ) {
226 			DBG_BUGON(it->ofs > EROFS_BLKSIZ);
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, EROFS_BLKSIZ - 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 >= EROFS_BLKSIZ) {
261 			DBG_BUGON(it->ofs > EROFS_BLKSIZ);
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, EROFS_BLKSIZ - 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 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
356 	unsigned int i;
357 	int ret = -ENOATTR;
358 
359 	for (i = 0; i < vi->xattr_shared_count; ++i) {
360 		erofs_blk_t blkaddr =
361 			xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
362 
363 		it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
364 		it->it.kaddr = erofs_read_metabuf(&it->it.buf, sb, blkaddr,
365 						  EROFS_KMAP);
366 		if (IS_ERR(it->it.kaddr))
367 			return PTR_ERR(it->it.kaddr);
368 		it->it.blkaddr = blkaddr;
369 
370 		ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL);
371 		if (ret != -ENOATTR)
372 			break;
373 	}
374 	return ret ? ret : it->buffer_size;
375 }
376 
377 static bool erofs_xattr_user_list(struct dentry *dentry)
378 {
379 	return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER);
380 }
381 
382 static bool erofs_xattr_trusted_list(struct dentry *dentry)
383 {
384 	return capable(CAP_SYS_ADMIN);
385 }
386 
387 int erofs_getxattr(struct inode *inode, int index,
388 		   const char *name,
389 		   void *buffer, size_t buffer_size)
390 {
391 	int ret;
392 	struct getxattr_iter it;
393 
394 	if (!name)
395 		return -EINVAL;
396 
397 	ret = init_inode_xattrs(inode);
398 	if (ret)
399 		return ret;
400 
401 	it.index = index;
402 	it.name.len = strlen(name);
403 	if (it.name.len > EROFS_NAME_LEN)
404 		return -ERANGE;
405 
406 	it.it.buf = __EROFS_BUF_INITIALIZER;
407 	it.name.name = name;
408 
409 	it.buffer = buffer;
410 	it.buffer_size = buffer_size;
411 
412 	it.it.sb = inode->i_sb;
413 	ret = inline_getxattr(inode, &it);
414 	if (ret == -ENOATTR)
415 		ret = shared_getxattr(inode, &it);
416 	erofs_put_metabuf(&it.it.buf);
417 	return ret;
418 }
419 
420 static int erofs_xattr_generic_get(const struct xattr_handler *handler,
421 				   struct dentry *unused, struct inode *inode,
422 				   const char *name, void *buffer, size_t size)
423 {
424 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
425 
426 	switch (handler->flags) {
427 	case EROFS_XATTR_INDEX_USER:
428 		if (!test_opt(&sbi->opt, XATTR_USER))
429 			return -EOPNOTSUPP;
430 		break;
431 	case EROFS_XATTR_INDEX_TRUSTED:
432 		break;
433 	case EROFS_XATTR_INDEX_SECURITY:
434 		break;
435 	default:
436 		return -EINVAL;
437 	}
438 
439 	return erofs_getxattr(inode, handler->flags, name, buffer, size);
440 }
441 
442 const struct xattr_handler erofs_xattr_user_handler = {
443 	.prefix	= XATTR_USER_PREFIX,
444 	.flags	= EROFS_XATTR_INDEX_USER,
445 	.list	= erofs_xattr_user_list,
446 	.get	= erofs_xattr_generic_get,
447 };
448 
449 const struct xattr_handler erofs_xattr_trusted_handler = {
450 	.prefix	= XATTR_TRUSTED_PREFIX,
451 	.flags	= EROFS_XATTR_INDEX_TRUSTED,
452 	.list	= erofs_xattr_trusted_list,
453 	.get	= erofs_xattr_generic_get,
454 };
455 
456 #ifdef CONFIG_EROFS_FS_SECURITY
457 const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
458 	.prefix	= XATTR_SECURITY_PREFIX,
459 	.flags	= EROFS_XATTR_INDEX_SECURITY,
460 	.get	= erofs_xattr_generic_get,
461 };
462 #endif
463 
464 const struct xattr_handler *erofs_xattr_handlers[] = {
465 	&erofs_xattr_user_handler,
466 #ifdef CONFIG_EROFS_FS_POSIX_ACL
467 	&posix_acl_access_xattr_handler,
468 	&posix_acl_default_xattr_handler,
469 #endif
470 	&erofs_xattr_trusted_handler,
471 #ifdef CONFIG_EROFS_FS_SECURITY
472 	&erofs_xattr_security_handler,
473 #endif
474 	NULL,
475 };
476 
477 struct listxattr_iter {
478 	struct xattr_iter it;
479 
480 	struct dentry *dentry;
481 	char *buffer;
482 	int buffer_size, buffer_ofs;
483 };
484 
485 static int xattr_entrylist(struct xattr_iter *_it,
486 			   struct erofs_xattr_entry *entry)
487 {
488 	struct listxattr_iter *it =
489 		container_of(_it, struct listxattr_iter, it);
490 	unsigned int prefix_len;
491 	const char *prefix;
492 
493 	const struct xattr_handler *h =
494 		erofs_xattr_handler(entry->e_name_index);
495 
496 	if (!h || (h->list && !h->list(it->dentry)))
497 		return 1;
498 
499 	prefix = xattr_prefix(h);
500 	prefix_len = strlen(prefix);
501 
502 	if (!it->buffer) {
503 		it->buffer_ofs += prefix_len + entry->e_name_len + 1;
504 		return 1;
505 	}
506 
507 	if (it->buffer_ofs + prefix_len
508 		+ entry->e_name_len + 1 > it->buffer_size)
509 		return -ERANGE;
510 
511 	memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
512 	it->buffer_ofs += prefix_len;
513 	return 0;
514 }
515 
516 static int xattr_namelist(struct xattr_iter *_it,
517 			  unsigned int processed, char *buf, unsigned int len)
518 {
519 	struct listxattr_iter *it =
520 		container_of(_it, struct listxattr_iter, it);
521 
522 	memcpy(it->buffer + it->buffer_ofs, buf, len);
523 	it->buffer_ofs += len;
524 	return 0;
525 }
526 
527 static int xattr_skipvalue(struct xattr_iter *_it,
528 			   unsigned int value_sz)
529 {
530 	struct listxattr_iter *it =
531 		container_of(_it, struct listxattr_iter, it);
532 
533 	it->buffer[it->buffer_ofs++] = '\0';
534 	return 1;
535 }
536 
537 static const struct xattr_iter_handlers list_xattr_handlers = {
538 	.entry = xattr_entrylist,
539 	.name = xattr_namelist,
540 	.alloc_buffer = xattr_skipvalue,
541 	.value = NULL
542 };
543 
544 static int inline_listxattr(struct listxattr_iter *it)
545 {
546 	int ret;
547 	unsigned int remaining;
548 
549 	ret = inline_xattr_iter_begin(&it->it, d_inode(it->dentry));
550 	if (ret < 0)
551 		return ret;
552 
553 	remaining = ret;
554 	while (remaining) {
555 		ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining);
556 		if (ret)
557 			break;
558 	}
559 	return ret ? ret : it->buffer_ofs;
560 }
561 
562 static int shared_listxattr(struct listxattr_iter *it)
563 {
564 	struct inode *const inode = d_inode(it->dentry);
565 	struct erofs_inode *const vi = EROFS_I(inode);
566 	struct super_block *const sb = inode->i_sb;
567 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
568 	unsigned int i;
569 	int ret = 0;
570 
571 	for (i = 0; i < vi->xattr_shared_count; ++i) {
572 		erofs_blk_t blkaddr =
573 			xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
574 
575 		it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
576 		it->it.kaddr = erofs_read_metabuf(&it->it.buf, sb, blkaddr,
577 						  EROFS_KMAP);
578 		if (IS_ERR(it->it.kaddr))
579 			return PTR_ERR(it->it.kaddr);
580 		it->it.blkaddr = blkaddr;
581 
582 		ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL);
583 		if (ret)
584 			break;
585 	}
586 	return ret ? ret : it->buffer_ofs;
587 }
588 
589 ssize_t erofs_listxattr(struct dentry *dentry,
590 			char *buffer, size_t buffer_size)
591 {
592 	int ret;
593 	struct listxattr_iter it;
594 
595 	ret = init_inode_xattrs(d_inode(dentry));
596 	if (ret == -ENOATTR)
597 		return 0;
598 	if (ret)
599 		return ret;
600 
601 	it.it.buf = __EROFS_BUF_INITIALIZER;
602 	it.dentry = dentry;
603 	it.buffer = buffer;
604 	it.buffer_size = buffer_size;
605 	it.buffer_ofs = 0;
606 
607 	it.it.sb = dentry->d_sb;
608 
609 	ret = inline_listxattr(&it);
610 	if (ret >= 0 || ret == -ENOATTR)
611 		ret = shared_listxattr(&it);
612 	erofs_put_metabuf(&it.it.buf);
613 	return ret;
614 }
615 
616 #ifdef CONFIG_EROFS_FS_POSIX_ACL
617 struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
618 {
619 	struct posix_acl *acl;
620 	int prefix, rc;
621 	char *value = NULL;
622 
623 	if (rcu)
624 		return ERR_PTR(-ECHILD);
625 
626 	switch (type) {
627 	case ACL_TYPE_ACCESS:
628 		prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
629 		break;
630 	case ACL_TYPE_DEFAULT:
631 		prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
632 		break;
633 	default:
634 		return ERR_PTR(-EINVAL);
635 	}
636 
637 	rc = erofs_getxattr(inode, prefix, "", NULL, 0);
638 	if (rc > 0) {
639 		value = kmalloc(rc, GFP_KERNEL);
640 		if (!value)
641 			return ERR_PTR(-ENOMEM);
642 		rc = erofs_getxattr(inode, prefix, "", value, rc);
643 	}
644 
645 	if (rc == -ENOATTR)
646 		acl = NULL;
647 	else if (rc < 0)
648 		acl = ERR_PTR(rc);
649 	else
650 		acl = posix_acl_from_xattr(&init_user_ns, value, rc);
651 	kfree(value);
652 	return acl;
653 }
654 #endif
655