1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * https://www.huawei.com/
7 #include <linux/security.h>
11 struct super_block *sb;
19 static inline void xattr_iter_end(struct xattr_iter *it, bool atomic)
21 /* the only user of kunmap() is 'init_inode_xattrs' */
25 kunmap_atomic(it->kaddr);
27 unlock_page(it->page);
31 static inline void xattr_iter_end_final(struct xattr_iter *it)
36 xattr_iter_end(it, true);
39 static int init_inode_xattrs(struct inode *inode)
41 struct erofs_inode *const vi = EROFS_I(inode);
44 struct erofs_xattr_ibody_header *ih;
45 struct super_block *sb;
46 struct erofs_sb_info *sbi;
50 /* the most case is that xattrs of this inode are initialized. */
51 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) {
53 * paired with smp_mb() at the end of the function to ensure
54 * fields will only be observed after the bit is set.
60 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE))
63 /* someone has initialized xattrs for us? */
64 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags))
68 * bypass all xattr operations if ->xattr_isize is not greater than
69 * sizeof(struct erofs_xattr_ibody_header), in detail:
70 * 1) it is not enough to contain erofs_xattr_ibody_header then
71 * ->xattr_isize should be 0 (it means no xattr);
72 * 2) it is just to contain erofs_xattr_ibody_header, which is on-disk
73 * undefined right now (maybe use later with some new sb feature).
75 if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) {
76 erofs_err(inode->i_sb,
77 "xattr_isize %d of nid %llu is not supported yet",
78 vi->xattr_isize, vi->nid);
81 } else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) {
82 if (vi->xattr_isize) {
83 erofs_err(inode->i_sb,
84 "bogus xattr ibody @ nid %llu", vi->nid);
87 goto out_unlock; /* xattr ondisk layout error */
95 it.blkaddr = erofs_blknr(iloc(sbi, vi->nid) + vi->inode_isize);
96 it.ofs = erofs_blkoff(iloc(sbi, vi->nid) + vi->inode_isize);
98 it.page = erofs_get_meta_page(sb, it.blkaddr);
99 if (IS_ERR(it.page)) {
100 ret = PTR_ERR(it.page);
104 /* read in shared xattr array (non-atomic, see kmalloc below) */
105 it.kaddr = kmap(it.page);
108 ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
110 vi->xattr_shared_count = ih->h_shared_count;
111 vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
112 sizeof(uint), GFP_KERNEL);
113 if (!vi->xattr_shared_xattrs) {
114 xattr_iter_end(&it, atomic_map);
119 /* let's skip ibody header */
120 it.ofs += sizeof(struct erofs_xattr_ibody_header);
122 for (i = 0; i < vi->xattr_shared_count; ++i) {
123 if (it.ofs >= EROFS_BLKSIZ) {
124 /* cannot be unaligned */
125 DBG_BUGON(it.ofs != EROFS_BLKSIZ);
126 xattr_iter_end(&it, atomic_map);
128 it.page = erofs_get_meta_page(sb, ++it.blkaddr);
129 if (IS_ERR(it.page)) {
130 kfree(vi->xattr_shared_xattrs);
131 vi->xattr_shared_xattrs = NULL;
132 ret = PTR_ERR(it.page);
136 it.kaddr = kmap_atomic(it.page);
140 vi->xattr_shared_xattrs[i] =
141 le32_to_cpu(*(__le32 *)(it.kaddr + it.ofs));
142 it.ofs += sizeof(__le32);
144 xattr_iter_end(&it, atomic_map);
146 /* paired with smp_mb() at the beginning of the function. */
148 set_bit(EROFS_I_EA_INITED_BIT, &vi->flags);
151 clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags);
156 * the general idea for these return values is
157 * if 0 is returned, go on processing the current xattr;
158 * 1 (> 0) is returned, skip this round to process the next xattr;
159 * -err (< 0) is returned, an error (maybe ENOXATTR) occurred
160 * and need to be handled
162 struct xattr_iter_handlers {
163 int (*entry)(struct xattr_iter *_it, struct erofs_xattr_entry *entry);
164 int (*name)(struct xattr_iter *_it, unsigned int processed, char *buf,
166 int (*alloc_buffer)(struct xattr_iter *_it, unsigned int value_sz);
167 void (*value)(struct xattr_iter *_it, unsigned int processed, char *buf,
171 static inline int xattr_iter_fixup(struct xattr_iter *it)
173 if (it->ofs < EROFS_BLKSIZ)
176 xattr_iter_end(it, true);
178 it->blkaddr += erofs_blknr(it->ofs);
180 it->page = erofs_get_meta_page(it->sb, it->blkaddr);
181 if (IS_ERR(it->page)) {
182 int err = PTR_ERR(it->page);
188 it->kaddr = kmap_atomic(it->page);
189 it->ofs = erofs_blkoff(it->ofs);
193 static int inline_xattr_iter_begin(struct xattr_iter *it,
196 struct erofs_inode *const vi = EROFS_I(inode);
197 struct erofs_sb_info *const sbi = EROFS_SB(inode->i_sb);
198 unsigned int xattr_header_sz, inline_xattr_ofs;
200 xattr_header_sz = inlinexattr_header_size(inode);
201 if (xattr_header_sz >= vi->xattr_isize) {
202 DBG_BUGON(xattr_header_sz > vi->xattr_isize);
206 inline_xattr_ofs = vi->inode_isize + xattr_header_sz;
208 it->blkaddr = erofs_blknr(iloc(sbi, vi->nid) + inline_xattr_ofs);
209 it->ofs = erofs_blkoff(iloc(sbi, vi->nid) + inline_xattr_ofs);
211 it->page = erofs_get_meta_page(inode->i_sb, it->blkaddr);
212 if (IS_ERR(it->page))
213 return PTR_ERR(it->page);
215 it->kaddr = kmap_atomic(it->page);
216 return vi->xattr_isize - xattr_header_sz;
220 * Regardless of success or failure, `xattr_foreach' will end up with
221 * `ofs' pointing to the next xattr item rather than an arbitrary position.
223 static int xattr_foreach(struct xattr_iter *it,
224 const struct xattr_iter_handlers *op,
225 unsigned int *tlimit)
227 struct erofs_xattr_entry entry;
228 unsigned int value_sz, processed, slice;
231 /* 0. fixup blkaddr, ofs, ipage */
232 err = xattr_iter_fixup(it);
237 * 1. read xattr entry to the memory,
238 * since we do EROFS_XATTR_ALIGN
239 * therefore entry should be in the page
241 entry = *(struct erofs_xattr_entry *)(it->kaddr + it->ofs);
243 unsigned int entry_sz = erofs_xattr_entry_size(&entry);
245 /* xattr on-disk corruption: xattr entry beyond xattr_isize */
246 if (*tlimit < entry_sz) {
248 return -EFSCORRUPTED;
253 it->ofs += sizeof(struct erofs_xattr_entry);
254 value_sz = le16_to_cpu(entry.e_value_size);
257 err = op->entry(it, &entry);
259 it->ofs += entry.e_name_len + value_sz;
263 /* 2. handle xattr name (ofs will finally be at the end of name) */
266 while (processed < entry.e_name_len) {
267 if (it->ofs >= EROFS_BLKSIZ) {
268 DBG_BUGON(it->ofs > EROFS_BLKSIZ);
270 err = xattr_iter_fixup(it);
276 slice = min_t(unsigned int, PAGE_SIZE - it->ofs,
277 entry.e_name_len - processed);
280 err = op->name(it, processed, it->kaddr + it->ofs, slice);
282 it->ofs += entry.e_name_len - processed + value_sz;
290 /* 3. handle xattr value */
293 if (op->alloc_buffer) {
294 err = op->alloc_buffer(it, value_sz);
301 while (processed < value_sz) {
302 if (it->ofs >= EROFS_BLKSIZ) {
303 DBG_BUGON(it->ofs > EROFS_BLKSIZ);
305 err = xattr_iter_fixup(it);
311 slice = min_t(unsigned int, PAGE_SIZE - it->ofs,
312 value_sz - processed);
313 op->value(it, processed, it->kaddr + it->ofs, slice);
319 /* xattrs should be 4-byte aligned (on-disk constraint) */
320 it->ofs = EROFS_XATTR_ALIGN(it->ofs);
321 return err < 0 ? err : 0;
324 struct getxattr_iter {
325 struct xattr_iter it;
328 int buffer_size, index;
332 static int xattr_entrymatch(struct xattr_iter *_it,
333 struct erofs_xattr_entry *entry)
335 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
337 return (it->index != entry->e_name_index ||
338 it->name.len != entry->e_name_len) ? -ENOATTR : 0;
341 static int xattr_namematch(struct xattr_iter *_it,
342 unsigned int processed, char *buf, unsigned int len)
344 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
346 return memcmp(buf, it->name.name + processed, len) ? -ENOATTR : 0;
349 static int xattr_checkbuffer(struct xattr_iter *_it,
350 unsigned int value_sz)
352 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
353 int err = it->buffer_size < value_sz ? -ERANGE : 0;
355 it->buffer_size = value_sz;
356 return !it->buffer ? 1 : err;
359 static void xattr_copyvalue(struct xattr_iter *_it,
360 unsigned int processed,
361 char *buf, unsigned int len)
363 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
365 memcpy(it->buffer + processed, buf, len);
368 static const struct xattr_iter_handlers find_xattr_handlers = {
369 .entry = xattr_entrymatch,
370 .name = xattr_namematch,
371 .alloc_buffer = xattr_checkbuffer,
372 .value = xattr_copyvalue
375 static int inline_getxattr(struct inode *inode, struct getxattr_iter *it)
378 unsigned int remaining;
380 ret = inline_xattr_iter_begin(&it->it, inode);
386 ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining);
390 xattr_iter_end_final(&it->it);
392 return ret ? ret : it->buffer_size;
395 static int shared_getxattr(struct inode *inode, struct getxattr_iter *it)
397 struct erofs_inode *const vi = EROFS_I(inode);
398 struct super_block *const sb = inode->i_sb;
399 struct erofs_sb_info *const sbi = EROFS_SB(sb);
403 for (i = 0; i < vi->xattr_shared_count; ++i) {
404 erofs_blk_t blkaddr =
405 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
407 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
409 if (!i || blkaddr != it->it.blkaddr) {
411 xattr_iter_end(&it->it, true);
413 it->it.page = erofs_get_meta_page(sb, blkaddr);
414 if (IS_ERR(it->it.page))
415 return PTR_ERR(it->it.page);
417 it->it.kaddr = kmap_atomic(it->it.page);
418 it->it.blkaddr = blkaddr;
421 ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL);
425 if (vi->xattr_shared_count)
426 xattr_iter_end_final(&it->it);
428 return ret ? ret : it->buffer_size;
431 static bool erofs_xattr_user_list(struct dentry *dentry)
433 return test_opt(&EROFS_SB(dentry->d_sb)->ctx, XATTR_USER);
436 static bool erofs_xattr_trusted_list(struct dentry *dentry)
438 return capable(CAP_SYS_ADMIN);
441 int erofs_getxattr(struct inode *inode, int index,
443 void *buffer, size_t buffer_size)
446 struct getxattr_iter it;
451 ret = init_inode_xattrs(inode);
457 it.name.len = strlen(name);
458 if (it.name.len > EROFS_NAME_LEN)
463 it.buffer_size = buffer_size;
465 it.it.sb = inode->i_sb;
466 ret = inline_getxattr(inode, &it);
468 ret = shared_getxattr(inode, &it);
472 static int erofs_xattr_generic_get(const struct xattr_handler *handler,
473 struct dentry *unused, struct inode *inode,
474 const char *name, void *buffer, size_t size)
476 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
478 switch (handler->flags) {
479 case EROFS_XATTR_INDEX_USER:
480 if (!test_opt(&sbi->ctx, XATTR_USER))
483 case EROFS_XATTR_INDEX_TRUSTED:
485 case EROFS_XATTR_INDEX_SECURITY:
491 return erofs_getxattr(inode, handler->flags, name, buffer, size);
494 const struct xattr_handler erofs_xattr_user_handler = {
495 .prefix = XATTR_USER_PREFIX,
496 .flags = EROFS_XATTR_INDEX_USER,
497 .list = erofs_xattr_user_list,
498 .get = erofs_xattr_generic_get,
501 const struct xattr_handler erofs_xattr_trusted_handler = {
502 .prefix = XATTR_TRUSTED_PREFIX,
503 .flags = EROFS_XATTR_INDEX_TRUSTED,
504 .list = erofs_xattr_trusted_list,
505 .get = erofs_xattr_generic_get,
508 #ifdef CONFIG_EROFS_FS_SECURITY
509 const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
510 .prefix = XATTR_SECURITY_PREFIX,
511 .flags = EROFS_XATTR_INDEX_SECURITY,
512 .get = erofs_xattr_generic_get,
516 const struct xattr_handler *erofs_xattr_handlers[] = {
517 &erofs_xattr_user_handler,
518 #ifdef CONFIG_EROFS_FS_POSIX_ACL
519 &posix_acl_access_xattr_handler,
520 &posix_acl_default_xattr_handler,
522 &erofs_xattr_trusted_handler,
523 #ifdef CONFIG_EROFS_FS_SECURITY
524 &erofs_xattr_security_handler,
529 struct listxattr_iter {
530 struct xattr_iter it;
532 struct dentry *dentry;
534 int buffer_size, buffer_ofs;
537 static int xattr_entrylist(struct xattr_iter *_it,
538 struct erofs_xattr_entry *entry)
540 struct listxattr_iter *it =
541 container_of(_it, struct listxattr_iter, it);
542 unsigned int prefix_len;
545 const struct xattr_handler *h =
546 erofs_xattr_handler(entry->e_name_index);
548 if (!h || (h->list && !h->list(it->dentry)))
551 prefix = xattr_prefix(h);
552 prefix_len = strlen(prefix);
555 it->buffer_ofs += prefix_len + entry->e_name_len + 1;
559 if (it->buffer_ofs + prefix_len
560 + entry->e_name_len + 1 > it->buffer_size)
563 memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
564 it->buffer_ofs += prefix_len;
568 static int xattr_namelist(struct xattr_iter *_it,
569 unsigned int processed, char *buf, unsigned int len)
571 struct listxattr_iter *it =
572 container_of(_it, struct listxattr_iter, it);
574 memcpy(it->buffer + it->buffer_ofs, buf, len);
575 it->buffer_ofs += len;
579 static int xattr_skipvalue(struct xattr_iter *_it,
580 unsigned int value_sz)
582 struct listxattr_iter *it =
583 container_of(_it, struct listxattr_iter, it);
585 it->buffer[it->buffer_ofs++] = '\0';
589 static const struct xattr_iter_handlers list_xattr_handlers = {
590 .entry = xattr_entrylist,
591 .name = xattr_namelist,
592 .alloc_buffer = xattr_skipvalue,
596 static int inline_listxattr(struct listxattr_iter *it)
599 unsigned int remaining;
601 ret = inline_xattr_iter_begin(&it->it, d_inode(it->dentry));
607 ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining);
611 xattr_iter_end_final(&it->it);
612 return ret ? ret : it->buffer_ofs;
615 static int shared_listxattr(struct listxattr_iter *it)
617 struct inode *const inode = d_inode(it->dentry);
618 struct erofs_inode *const vi = EROFS_I(inode);
619 struct super_block *const sb = inode->i_sb;
620 struct erofs_sb_info *const sbi = EROFS_SB(sb);
624 for (i = 0; i < vi->xattr_shared_count; ++i) {
625 erofs_blk_t blkaddr =
626 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
628 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
629 if (!i || blkaddr != it->it.blkaddr) {
631 xattr_iter_end(&it->it, true);
633 it->it.page = erofs_get_meta_page(sb, blkaddr);
634 if (IS_ERR(it->it.page))
635 return PTR_ERR(it->it.page);
637 it->it.kaddr = kmap_atomic(it->it.page);
638 it->it.blkaddr = blkaddr;
641 ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL);
645 if (vi->xattr_shared_count)
646 xattr_iter_end_final(&it->it);
648 return ret ? ret : it->buffer_ofs;
651 ssize_t erofs_listxattr(struct dentry *dentry,
652 char *buffer, size_t buffer_size)
655 struct listxattr_iter it;
657 ret = init_inode_xattrs(d_inode(dentry));
665 it.buffer_size = buffer_size;
668 it.it.sb = dentry->d_sb;
670 ret = inline_listxattr(&it);
671 if (ret < 0 && ret != -ENOATTR)
673 return shared_listxattr(&it);
676 #ifdef CONFIG_EROFS_FS_POSIX_ACL
677 struct posix_acl *erofs_get_acl(struct inode *inode, int type)
679 struct posix_acl *acl;
684 case ACL_TYPE_ACCESS:
685 prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
687 case ACL_TYPE_DEFAULT:
688 prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
691 return ERR_PTR(-EINVAL);
694 rc = erofs_getxattr(inode, prefix, "", NULL, 0);
696 value = kmalloc(rc, GFP_KERNEL);
698 return ERR_PTR(-ENOMEM);
699 rc = erofs_getxattr(inode, prefix, "", value, rc);
707 acl = posix_acl_from_xattr(&init_user_ns, value, rc);