1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * https://www.huawei.com/
6 #include <linux/security.h>
10 struct super_block *sb;
18 static inline void xattr_iter_end(struct xattr_iter *it, bool atomic)
20 /* the only user of kunmap() is 'init_inode_xattrs' */
24 kunmap_atomic(it->kaddr);
26 unlock_page(it->page);
30 static inline void xattr_iter_end_final(struct xattr_iter *it)
35 xattr_iter_end(it, true);
38 static int init_inode_xattrs(struct inode *inode)
40 struct erofs_inode *const vi = EROFS_I(inode);
43 struct erofs_xattr_ibody_header *ih;
44 struct super_block *sb;
45 struct erofs_sb_info *sbi;
49 /* the most case is that xattrs of this inode are initialized. */
50 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) {
52 * paired with smp_mb() at the end of the function to ensure
53 * fields will only be observed after the bit is set.
59 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE))
62 /* someone has initialized xattrs for us? */
63 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags))
67 * bypass all xattr operations if ->xattr_isize is not greater than
68 * sizeof(struct erofs_xattr_ibody_header), in detail:
69 * 1) it is not enough to contain erofs_xattr_ibody_header then
70 * ->xattr_isize should be 0 (it means no xattr);
71 * 2) it is just to contain erofs_xattr_ibody_header, which is on-disk
72 * undefined right now (maybe use later with some new sb feature).
74 if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) {
75 erofs_err(inode->i_sb,
76 "xattr_isize %d of nid %llu is not supported yet",
77 vi->xattr_isize, vi->nid);
80 } else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) {
81 if (vi->xattr_isize) {
82 erofs_err(inode->i_sb,
83 "bogus xattr ibody @ nid %llu", vi->nid);
86 goto out_unlock; /* xattr ondisk layout error */
94 it.blkaddr = erofs_blknr(iloc(sbi, vi->nid) + vi->inode_isize);
95 it.ofs = erofs_blkoff(iloc(sbi, vi->nid) + vi->inode_isize);
97 it.page = erofs_get_meta_page(sb, it.blkaddr);
98 if (IS_ERR(it.page)) {
99 ret = PTR_ERR(it.page);
103 /* read in shared xattr array (non-atomic, see kmalloc below) */
104 it.kaddr = kmap(it.page);
107 ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
109 vi->xattr_shared_count = ih->h_shared_count;
110 vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
111 sizeof(uint), GFP_KERNEL);
112 if (!vi->xattr_shared_xattrs) {
113 xattr_iter_end(&it, atomic_map);
118 /* let's skip ibody header */
119 it.ofs += sizeof(struct erofs_xattr_ibody_header);
121 for (i = 0; i < vi->xattr_shared_count; ++i) {
122 if (it.ofs >= EROFS_BLKSIZ) {
123 /* cannot be unaligned */
124 DBG_BUGON(it.ofs != EROFS_BLKSIZ);
125 xattr_iter_end(&it, atomic_map);
127 it.page = erofs_get_meta_page(sb, ++it.blkaddr);
128 if (IS_ERR(it.page)) {
129 kfree(vi->xattr_shared_xattrs);
130 vi->xattr_shared_xattrs = NULL;
131 ret = PTR_ERR(it.page);
135 it.kaddr = kmap_atomic(it.page);
139 vi->xattr_shared_xattrs[i] =
140 le32_to_cpu(*(__le32 *)(it.kaddr + it.ofs));
141 it.ofs += sizeof(__le32);
143 xattr_iter_end(&it, atomic_map);
145 /* paired with smp_mb() at the beginning of the function. */
147 set_bit(EROFS_I_EA_INITED_BIT, &vi->flags);
150 clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags);
155 * the general idea for these return values is
156 * if 0 is returned, go on processing the current xattr;
157 * 1 (> 0) is returned, skip this round to process the next xattr;
158 * -err (< 0) is returned, an error (maybe ENOXATTR) occurred
159 * and need to be handled
161 struct xattr_iter_handlers {
162 int (*entry)(struct xattr_iter *_it, struct erofs_xattr_entry *entry);
163 int (*name)(struct xattr_iter *_it, unsigned int processed, char *buf,
165 int (*alloc_buffer)(struct xattr_iter *_it, unsigned int value_sz);
166 void (*value)(struct xattr_iter *_it, unsigned int processed, char *buf,
170 static inline int xattr_iter_fixup(struct xattr_iter *it)
172 if (it->ofs < EROFS_BLKSIZ)
175 xattr_iter_end(it, true);
177 it->blkaddr += erofs_blknr(it->ofs);
179 it->page = erofs_get_meta_page(it->sb, it->blkaddr);
180 if (IS_ERR(it->page)) {
181 int err = PTR_ERR(it->page);
187 it->kaddr = kmap_atomic(it->page);
188 it->ofs = erofs_blkoff(it->ofs);
192 static int inline_xattr_iter_begin(struct xattr_iter *it,
195 struct erofs_inode *const vi = EROFS_I(inode);
196 struct erofs_sb_info *const sbi = EROFS_SB(inode->i_sb);
197 unsigned int xattr_header_sz, inline_xattr_ofs;
199 xattr_header_sz = inlinexattr_header_size(inode);
200 if (xattr_header_sz >= vi->xattr_isize) {
201 DBG_BUGON(xattr_header_sz > vi->xattr_isize);
205 inline_xattr_ofs = vi->inode_isize + xattr_header_sz;
207 it->blkaddr = erofs_blknr(iloc(sbi, vi->nid) + inline_xattr_ofs);
208 it->ofs = erofs_blkoff(iloc(sbi, vi->nid) + inline_xattr_ofs);
210 it->page = erofs_get_meta_page(inode->i_sb, it->blkaddr);
211 if (IS_ERR(it->page))
212 return PTR_ERR(it->page);
214 it->kaddr = kmap_atomic(it->page);
215 return vi->xattr_isize - xattr_header_sz;
219 * Regardless of success or failure, `xattr_foreach' will end up with
220 * `ofs' pointing to the next xattr item rather than an arbitrary position.
222 static int xattr_foreach(struct xattr_iter *it,
223 const struct xattr_iter_handlers *op,
224 unsigned int *tlimit)
226 struct erofs_xattr_entry entry;
227 unsigned int value_sz, processed, slice;
230 /* 0. fixup blkaddr, ofs, ipage */
231 err = xattr_iter_fixup(it);
236 * 1. read xattr entry to the memory,
237 * since we do EROFS_XATTR_ALIGN
238 * therefore entry should be in the page
240 entry = *(struct erofs_xattr_entry *)(it->kaddr + it->ofs);
242 unsigned int entry_sz = erofs_xattr_entry_size(&entry);
244 /* xattr on-disk corruption: xattr entry beyond xattr_isize */
245 if (*tlimit < entry_sz) {
247 return -EFSCORRUPTED;
252 it->ofs += sizeof(struct erofs_xattr_entry);
253 value_sz = le16_to_cpu(entry.e_value_size);
256 err = op->entry(it, &entry);
258 it->ofs += entry.e_name_len + value_sz;
262 /* 2. handle xattr name (ofs will finally be at the end of name) */
265 while (processed < entry.e_name_len) {
266 if (it->ofs >= EROFS_BLKSIZ) {
267 DBG_BUGON(it->ofs > EROFS_BLKSIZ);
269 err = xattr_iter_fixup(it);
275 slice = min_t(unsigned int, PAGE_SIZE - it->ofs,
276 entry.e_name_len - processed);
279 err = op->name(it, processed, it->kaddr + it->ofs, slice);
281 it->ofs += entry.e_name_len - processed + value_sz;
289 /* 3. handle xattr value */
292 if (op->alloc_buffer) {
293 err = op->alloc_buffer(it, value_sz);
300 while (processed < value_sz) {
301 if (it->ofs >= EROFS_BLKSIZ) {
302 DBG_BUGON(it->ofs > EROFS_BLKSIZ);
304 err = xattr_iter_fixup(it);
310 slice = min_t(unsigned int, PAGE_SIZE - it->ofs,
311 value_sz - processed);
312 op->value(it, processed, it->kaddr + it->ofs, slice);
318 /* xattrs should be 4-byte aligned (on-disk constraint) */
319 it->ofs = EROFS_XATTR_ALIGN(it->ofs);
320 return err < 0 ? err : 0;
323 struct getxattr_iter {
324 struct xattr_iter it;
327 int buffer_size, index;
331 static int xattr_entrymatch(struct xattr_iter *_it,
332 struct erofs_xattr_entry *entry)
334 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
336 return (it->index != entry->e_name_index ||
337 it->name.len != entry->e_name_len) ? -ENOATTR : 0;
340 static int xattr_namematch(struct xattr_iter *_it,
341 unsigned int processed, char *buf, unsigned int len)
343 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
345 return memcmp(buf, it->name.name + processed, len) ? -ENOATTR : 0;
348 static int xattr_checkbuffer(struct xattr_iter *_it,
349 unsigned int value_sz)
351 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
352 int err = it->buffer_size < value_sz ? -ERANGE : 0;
354 it->buffer_size = value_sz;
355 return !it->buffer ? 1 : err;
358 static void xattr_copyvalue(struct xattr_iter *_it,
359 unsigned int processed,
360 char *buf, unsigned int len)
362 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
364 memcpy(it->buffer + processed, buf, len);
367 static const struct xattr_iter_handlers find_xattr_handlers = {
368 .entry = xattr_entrymatch,
369 .name = xattr_namematch,
370 .alloc_buffer = xattr_checkbuffer,
371 .value = xattr_copyvalue
374 static int inline_getxattr(struct inode *inode, struct getxattr_iter *it)
377 unsigned int remaining;
379 ret = inline_xattr_iter_begin(&it->it, inode);
385 ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining);
389 xattr_iter_end_final(&it->it);
391 return ret ? ret : it->buffer_size;
394 static int shared_getxattr(struct inode *inode, struct getxattr_iter *it)
396 struct erofs_inode *const vi = EROFS_I(inode);
397 struct super_block *const sb = inode->i_sb;
398 struct erofs_sb_info *const sbi = EROFS_SB(sb);
402 for (i = 0; i < vi->xattr_shared_count; ++i) {
403 erofs_blk_t blkaddr =
404 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
406 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
408 if (!i || blkaddr != it->it.blkaddr) {
410 xattr_iter_end(&it->it, true);
412 it->it.page = erofs_get_meta_page(sb, blkaddr);
413 if (IS_ERR(it->it.page))
414 return PTR_ERR(it->it.page);
416 it->it.kaddr = kmap_atomic(it->it.page);
417 it->it.blkaddr = blkaddr;
420 ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL);
424 if (vi->xattr_shared_count)
425 xattr_iter_end_final(&it->it);
427 return ret ? ret : it->buffer_size;
430 static bool erofs_xattr_user_list(struct dentry *dentry)
432 return test_opt(&EROFS_SB(dentry->d_sb)->ctx, XATTR_USER);
435 static bool erofs_xattr_trusted_list(struct dentry *dentry)
437 return capable(CAP_SYS_ADMIN);
440 int erofs_getxattr(struct inode *inode, int index,
442 void *buffer, size_t buffer_size)
445 struct getxattr_iter it;
450 ret = init_inode_xattrs(inode);
456 it.name.len = strlen(name);
457 if (it.name.len > EROFS_NAME_LEN)
462 it.buffer_size = buffer_size;
464 it.it.sb = inode->i_sb;
465 ret = inline_getxattr(inode, &it);
467 ret = shared_getxattr(inode, &it);
471 static int erofs_xattr_generic_get(const struct xattr_handler *handler,
472 struct dentry *unused, struct inode *inode,
473 const char *name, void *buffer, size_t size)
475 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
477 switch (handler->flags) {
478 case EROFS_XATTR_INDEX_USER:
479 if (!test_opt(&sbi->ctx, XATTR_USER))
482 case EROFS_XATTR_INDEX_TRUSTED:
484 case EROFS_XATTR_INDEX_SECURITY:
490 return erofs_getxattr(inode, handler->flags, name, buffer, size);
493 const struct xattr_handler erofs_xattr_user_handler = {
494 .prefix = XATTR_USER_PREFIX,
495 .flags = EROFS_XATTR_INDEX_USER,
496 .list = erofs_xattr_user_list,
497 .get = erofs_xattr_generic_get,
500 const struct xattr_handler erofs_xattr_trusted_handler = {
501 .prefix = XATTR_TRUSTED_PREFIX,
502 .flags = EROFS_XATTR_INDEX_TRUSTED,
503 .list = erofs_xattr_trusted_list,
504 .get = erofs_xattr_generic_get,
507 #ifdef CONFIG_EROFS_FS_SECURITY
508 const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
509 .prefix = XATTR_SECURITY_PREFIX,
510 .flags = EROFS_XATTR_INDEX_SECURITY,
511 .get = erofs_xattr_generic_get,
515 const struct xattr_handler *erofs_xattr_handlers[] = {
516 &erofs_xattr_user_handler,
517 #ifdef CONFIG_EROFS_FS_POSIX_ACL
518 &posix_acl_access_xattr_handler,
519 &posix_acl_default_xattr_handler,
521 &erofs_xattr_trusted_handler,
522 #ifdef CONFIG_EROFS_FS_SECURITY
523 &erofs_xattr_security_handler,
528 struct listxattr_iter {
529 struct xattr_iter it;
531 struct dentry *dentry;
533 int buffer_size, buffer_ofs;
536 static int xattr_entrylist(struct xattr_iter *_it,
537 struct erofs_xattr_entry *entry)
539 struct listxattr_iter *it =
540 container_of(_it, struct listxattr_iter, it);
541 unsigned int prefix_len;
544 const struct xattr_handler *h =
545 erofs_xattr_handler(entry->e_name_index);
547 if (!h || (h->list && !h->list(it->dentry)))
550 prefix = xattr_prefix(h);
551 prefix_len = strlen(prefix);
554 it->buffer_ofs += prefix_len + entry->e_name_len + 1;
558 if (it->buffer_ofs + prefix_len
559 + entry->e_name_len + 1 > it->buffer_size)
562 memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
563 it->buffer_ofs += prefix_len;
567 static int xattr_namelist(struct xattr_iter *_it,
568 unsigned int processed, char *buf, unsigned int len)
570 struct listxattr_iter *it =
571 container_of(_it, struct listxattr_iter, it);
573 memcpy(it->buffer + it->buffer_ofs, buf, len);
574 it->buffer_ofs += len;
578 static int xattr_skipvalue(struct xattr_iter *_it,
579 unsigned int value_sz)
581 struct listxattr_iter *it =
582 container_of(_it, struct listxattr_iter, it);
584 it->buffer[it->buffer_ofs++] = '\0';
588 static const struct xattr_iter_handlers list_xattr_handlers = {
589 .entry = xattr_entrylist,
590 .name = xattr_namelist,
591 .alloc_buffer = xattr_skipvalue,
595 static int inline_listxattr(struct listxattr_iter *it)
598 unsigned int remaining;
600 ret = inline_xattr_iter_begin(&it->it, d_inode(it->dentry));
606 ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining);
610 xattr_iter_end_final(&it->it);
611 return ret ? ret : it->buffer_ofs;
614 static int shared_listxattr(struct listxattr_iter *it)
616 struct inode *const inode = d_inode(it->dentry);
617 struct erofs_inode *const vi = EROFS_I(inode);
618 struct super_block *const sb = inode->i_sb;
619 struct erofs_sb_info *const sbi = EROFS_SB(sb);
623 for (i = 0; i < vi->xattr_shared_count; ++i) {
624 erofs_blk_t blkaddr =
625 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
627 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
628 if (!i || blkaddr != it->it.blkaddr) {
630 xattr_iter_end(&it->it, true);
632 it->it.page = erofs_get_meta_page(sb, blkaddr);
633 if (IS_ERR(it->it.page))
634 return PTR_ERR(it->it.page);
636 it->it.kaddr = kmap_atomic(it->it.page);
637 it->it.blkaddr = blkaddr;
640 ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL);
644 if (vi->xattr_shared_count)
645 xattr_iter_end_final(&it->it);
647 return ret ? ret : it->buffer_ofs;
650 ssize_t erofs_listxattr(struct dentry *dentry,
651 char *buffer, size_t buffer_size)
654 struct listxattr_iter it;
656 ret = init_inode_xattrs(d_inode(dentry));
664 it.buffer_size = buffer_size;
667 it.it.sb = dentry->d_sb;
669 ret = inline_listxattr(&it);
670 if (ret < 0 && ret != -ENOATTR)
672 return shared_listxattr(&it);
675 #ifdef CONFIG_EROFS_FS_POSIX_ACL
676 struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
678 struct posix_acl *acl;
683 return ERR_PTR(-ECHILD);
686 case ACL_TYPE_ACCESS:
687 prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
689 case ACL_TYPE_DEFAULT:
690 prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
693 return ERR_PTR(-EINVAL);
696 rc = erofs_getxattr(inode, prefix, "", NULL, 0);
698 value = kmalloc(rc, GFP_KERNEL);
700 return ERR_PTR(-ENOMEM);
701 rc = erofs_getxattr(inode, prefix, "", value, rc);
709 acl = posix_acl_from_xattr(&init_user_ns, value, rc);