1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Copyright (C) 2021-2022, Alibaba Cloud
7 #include <linux/security.h>
10 static inline erofs_blk_t erofs_xattr_blkaddr(struct super_block *sb,
11 unsigned int xattr_id)
13 return EROFS_SB(sb)->xattr_blkaddr +
14 erofs_blknr(sb, xattr_id * sizeof(__u32));
17 static inline unsigned int erofs_xattr_blkoff(struct super_block *sb,
18 unsigned int xattr_id)
20 return erofs_blkoff(sb, xattr_id * sizeof(__u32));
24 struct super_block *sb;
32 static int erofs_init_inode_xattrs(struct inode *inode)
34 struct erofs_inode *const vi = EROFS_I(inode);
37 struct erofs_xattr_ibody_header *ih;
38 struct super_block *sb = inode->i_sb;
41 /* the most case is that xattrs of this inode are initialized. */
42 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) {
44 * paired with smp_mb() at the end of the function to ensure
45 * fields will only be observed after the bit is set.
51 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE))
54 /* someone has initialized xattrs for us? */
55 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags))
59 * bypass all xattr operations if ->xattr_isize is not greater than
60 * sizeof(struct erofs_xattr_ibody_header), in detail:
61 * 1) it is not enough to contain erofs_xattr_ibody_header then
62 * ->xattr_isize should be 0 (it means no xattr);
63 * 2) it is just to contain erofs_xattr_ibody_header, which is on-disk
64 * undefined right now (maybe use later with some new sb feature).
66 if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) {
68 "xattr_isize %d of nid %llu is not supported yet",
69 vi->xattr_isize, vi->nid);
72 } else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) {
73 if (vi->xattr_isize) {
74 erofs_err(sb, "bogus xattr ibody @ nid %llu", vi->nid);
77 goto out_unlock; /* xattr ondisk layout error */
83 it.buf = __EROFS_BUF_INITIALIZER;
84 it.blkaddr = erofs_blknr(sb, erofs_iloc(inode) + vi->inode_isize);
85 it.ofs = erofs_blkoff(sb, erofs_iloc(inode) + vi->inode_isize);
87 /* read in shared xattr array (non-atomic, see kmalloc below) */
88 it.kaddr = erofs_read_metabuf(&it.buf, sb, it.blkaddr, EROFS_KMAP);
89 if (IS_ERR(it.kaddr)) {
90 ret = PTR_ERR(it.kaddr);
94 ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
95 vi->xattr_shared_count = ih->h_shared_count;
96 vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
97 sizeof(uint), GFP_KERNEL);
98 if (!vi->xattr_shared_xattrs) {
99 erofs_put_metabuf(&it.buf);
104 /* let's skip ibody header */
105 it.ofs += sizeof(struct erofs_xattr_ibody_header);
107 for (i = 0; i < vi->xattr_shared_count; ++i) {
108 if (it.ofs >= sb->s_blocksize) {
109 /* cannot be unaligned */
110 DBG_BUGON(it.ofs != sb->s_blocksize);
112 it.kaddr = erofs_read_metabuf(&it.buf, sb, ++it.blkaddr,
114 if (IS_ERR(it.kaddr)) {
115 kfree(vi->xattr_shared_xattrs);
116 vi->xattr_shared_xattrs = NULL;
117 ret = PTR_ERR(it.kaddr);
122 vi->xattr_shared_xattrs[i] =
123 le32_to_cpu(*(__le32 *)(it.kaddr + it.ofs));
124 it.ofs += sizeof(__le32);
126 erofs_put_metabuf(&it.buf);
128 /* paired with smp_mb() at the beginning of the function. */
130 set_bit(EROFS_I_EA_INITED_BIT, &vi->flags);
133 clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags);
138 * the general idea for these return values is
139 * if 0 is returned, go on processing the current xattr;
140 * 1 (> 0) is returned, skip this round to process the next xattr;
141 * -err (< 0) is returned, an error (maybe ENOXATTR) occurred
142 * and need to be handled
144 struct xattr_iter_handlers {
145 int (*entry)(struct xattr_iter *_it, struct erofs_xattr_entry *entry);
146 int (*name)(struct xattr_iter *_it, unsigned int processed, char *buf,
148 int (*alloc_buffer)(struct xattr_iter *_it, unsigned int value_sz);
149 void (*value)(struct xattr_iter *_it, unsigned int processed, char *buf,
153 static inline int xattr_iter_fixup(struct xattr_iter *it)
155 if (it->ofs < it->sb->s_blocksize)
158 it->blkaddr += erofs_blknr(it->sb, it->ofs);
159 it->kaddr = erofs_read_metabuf(&it->buf, it->sb, it->blkaddr,
161 if (IS_ERR(it->kaddr))
162 return PTR_ERR(it->kaddr);
163 it->ofs = erofs_blkoff(it->sb, it->ofs);
167 static int inline_xattr_iter_begin(struct xattr_iter *it,
170 struct erofs_inode *const vi = EROFS_I(inode);
171 unsigned int xattr_header_sz, inline_xattr_ofs;
173 xattr_header_sz = sizeof(struct erofs_xattr_ibody_header) +
174 sizeof(u32) * vi->xattr_shared_count;
175 if (xattr_header_sz >= vi->xattr_isize) {
176 DBG_BUGON(xattr_header_sz > vi->xattr_isize);
180 inline_xattr_ofs = vi->inode_isize + xattr_header_sz;
182 it->blkaddr = erofs_blknr(it->sb, erofs_iloc(inode) + inline_xattr_ofs);
183 it->ofs = erofs_blkoff(it->sb, erofs_iloc(inode) + inline_xattr_ofs);
184 it->kaddr = erofs_read_metabuf(&it->buf, inode->i_sb, it->blkaddr,
186 if (IS_ERR(it->kaddr))
187 return PTR_ERR(it->kaddr);
188 return vi->xattr_isize - xattr_header_sz;
192 * Regardless of success or failure, `xattr_foreach' will end up with
193 * `ofs' pointing to the next xattr item rather than an arbitrary position.
195 static int xattr_foreach(struct xattr_iter *it,
196 const struct xattr_iter_handlers *op,
197 unsigned int *tlimit)
199 struct erofs_xattr_entry entry;
200 unsigned int value_sz, processed, slice;
203 /* 0. fixup blkaddr, ofs, ipage */
204 err = xattr_iter_fixup(it);
209 * 1. read xattr entry to the memory,
210 * since we do EROFS_XATTR_ALIGN
211 * therefore entry should be in the page
213 entry = *(struct erofs_xattr_entry *)(it->kaddr + it->ofs);
215 unsigned int entry_sz = erofs_xattr_entry_size(&entry);
217 /* xattr on-disk corruption: xattr entry beyond xattr_isize */
218 if (*tlimit < entry_sz) {
220 return -EFSCORRUPTED;
225 it->ofs += sizeof(struct erofs_xattr_entry);
226 value_sz = le16_to_cpu(entry.e_value_size);
229 err = op->entry(it, &entry);
231 it->ofs += entry.e_name_len + value_sz;
235 /* 2. handle xattr name (ofs will finally be at the end of name) */
238 while (processed < entry.e_name_len) {
239 if (it->ofs >= it->sb->s_blocksize) {
240 DBG_BUGON(it->ofs > it->sb->s_blocksize);
242 err = xattr_iter_fixup(it);
248 slice = min_t(unsigned int, it->sb->s_blocksize - it->ofs,
249 entry.e_name_len - processed);
252 err = op->name(it, processed, it->kaddr + it->ofs, slice);
254 it->ofs += entry.e_name_len - processed + value_sz;
262 /* 3. handle xattr value */
265 if (op->alloc_buffer) {
266 err = op->alloc_buffer(it, value_sz);
273 while (processed < value_sz) {
274 if (it->ofs >= it->sb->s_blocksize) {
275 DBG_BUGON(it->ofs > it->sb->s_blocksize);
277 err = xattr_iter_fixup(it);
283 slice = min_t(unsigned int, it->sb->s_blocksize - it->ofs,
284 value_sz - processed);
285 op->value(it, processed, it->kaddr + it->ofs, slice);
291 /* xattrs should be 4-byte aligned (on-disk constraint) */
292 it->ofs = EROFS_XATTR_ALIGN(it->ofs);
293 return err < 0 ? err : 0;
296 struct getxattr_iter {
297 struct xattr_iter it;
300 int buffer_size, index, infix_len;
304 static int erofs_xattr_long_entrymatch(struct getxattr_iter *it,
305 struct erofs_xattr_entry *entry)
307 struct erofs_sb_info *sbi = EROFS_SB(it->it.sb);
308 struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
309 (entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
311 if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
314 if (it->index != pf->prefix->base_index ||
315 it->name.len != entry->e_name_len + pf->infix_len)
318 if (memcmp(it->name.name, pf->prefix->infix, pf->infix_len))
321 it->infix_len = pf->infix_len;
325 static int xattr_entrymatch(struct xattr_iter *_it,
326 struct erofs_xattr_entry *entry)
328 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
330 /* should also match the infix for long name prefixes */
331 if (entry->e_name_index & EROFS_XATTR_LONG_PREFIX)
332 return erofs_xattr_long_entrymatch(it, entry);
334 if (it->index != entry->e_name_index ||
335 it->name.len != entry->e_name_len)
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 if (memcmp(buf, it->name.name + it->infix_len + processed, len))
351 static int xattr_checkbuffer(struct xattr_iter *_it,
352 unsigned int value_sz)
354 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
355 int err = it->buffer_size < value_sz ? -ERANGE : 0;
357 it->buffer_size = value_sz;
358 return !it->buffer ? 1 : err;
361 static void xattr_copyvalue(struct xattr_iter *_it,
362 unsigned int processed,
363 char *buf, unsigned int len)
365 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
367 memcpy(it->buffer + processed, buf, len);
370 static const struct xattr_iter_handlers find_xattr_handlers = {
371 .entry = xattr_entrymatch,
372 .name = xattr_namematch,
373 .alloc_buffer = xattr_checkbuffer,
374 .value = xattr_copyvalue
377 static int inline_getxattr(struct inode *inode, struct getxattr_iter *it)
380 unsigned int remaining;
382 ret = inline_xattr_iter_begin(&it->it, inode);
388 ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining);
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 = it->it.sb;
399 unsigned int i, xsid;
402 for (i = 0; i < vi->xattr_shared_count; ++i) {
403 xsid = vi->xattr_shared_xattrs[i];
404 it->it.blkaddr = erofs_xattr_blkaddr(sb, xsid);
405 it->it.ofs = erofs_xattr_blkoff(sb, xsid);
406 it->it.kaddr = erofs_read_metabuf(&it->it.buf, sb,
407 it->it.blkaddr, EROFS_KMAP);
408 if (IS_ERR(it->it.kaddr))
409 return PTR_ERR(it->it.kaddr);
411 ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL);
415 return ret ? ret : it->buffer_size;
418 static bool erofs_xattr_user_list(struct dentry *dentry)
420 return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER);
423 static bool erofs_xattr_trusted_list(struct dentry *dentry)
425 return capable(CAP_SYS_ADMIN);
428 int erofs_getxattr(struct inode *inode, int index,
430 void *buffer, size_t buffer_size)
433 struct getxattr_iter it;
438 ret = erofs_init_inode_xattrs(inode);
443 it.name.len = strlen(name);
444 if (it.name.len > EROFS_NAME_LEN)
447 it.it.buf = __EROFS_BUF_INITIALIZER;
451 it.buffer_size = buffer_size;
453 it.it.sb = inode->i_sb;
454 ret = inline_getxattr(inode, &it);
456 ret = shared_getxattr(inode, &it);
457 erofs_put_metabuf(&it.it.buf);
461 static int erofs_xattr_generic_get(const struct xattr_handler *handler,
462 struct dentry *unused, struct inode *inode,
463 const char *name, void *buffer, size_t size)
465 if (handler->flags == EROFS_XATTR_INDEX_USER &&
466 !test_opt(&EROFS_I_SB(inode)->opt, XATTR_USER))
469 return erofs_getxattr(inode, handler->flags, name, buffer, size);
472 const struct xattr_handler erofs_xattr_user_handler = {
473 .prefix = XATTR_USER_PREFIX,
474 .flags = EROFS_XATTR_INDEX_USER,
475 .list = erofs_xattr_user_list,
476 .get = erofs_xattr_generic_get,
479 const struct xattr_handler erofs_xattr_trusted_handler = {
480 .prefix = XATTR_TRUSTED_PREFIX,
481 .flags = EROFS_XATTR_INDEX_TRUSTED,
482 .list = erofs_xattr_trusted_list,
483 .get = erofs_xattr_generic_get,
486 #ifdef CONFIG_EROFS_FS_SECURITY
487 const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
488 .prefix = XATTR_SECURITY_PREFIX,
489 .flags = EROFS_XATTR_INDEX_SECURITY,
490 .get = erofs_xattr_generic_get,
494 const struct xattr_handler *erofs_xattr_handlers[] = {
495 &erofs_xattr_user_handler,
496 &erofs_xattr_trusted_handler,
497 #ifdef CONFIG_EROFS_FS_SECURITY
498 &erofs_xattr_security_handler,
503 struct listxattr_iter {
504 struct xattr_iter it;
506 struct dentry *dentry;
508 int buffer_size, buffer_ofs;
511 static int xattr_entrylist(struct xattr_iter *_it,
512 struct erofs_xattr_entry *entry)
514 struct listxattr_iter *it =
515 container_of(_it, struct listxattr_iter, it);
516 unsigned int base_index = entry->e_name_index;
517 unsigned int prefix_len, infix_len = 0;
518 const char *prefix, *infix = NULL;
520 if (entry->e_name_index & EROFS_XATTR_LONG_PREFIX) {
521 struct erofs_sb_info *sbi = EROFS_SB(_it->sb);
522 struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
523 (entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
525 if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
527 infix = pf->prefix->infix;
528 infix_len = pf->infix_len;
529 base_index = pf->prefix->base_index;
532 prefix = erofs_xattr_prefix(base_index, it->dentry);
535 prefix_len = strlen(prefix);
538 it->buffer_ofs += prefix_len + infix_len +
539 entry->e_name_len + 1;
543 if (it->buffer_ofs + prefix_len + infix_len +
544 + entry->e_name_len + 1 > it->buffer_size)
547 memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
548 memcpy(it->buffer + it->buffer_ofs + prefix_len, infix, infix_len);
549 it->buffer_ofs += prefix_len + infix_len;
553 static int xattr_namelist(struct xattr_iter *_it,
554 unsigned int processed, char *buf, unsigned int len)
556 struct listxattr_iter *it =
557 container_of(_it, struct listxattr_iter, it);
559 memcpy(it->buffer + it->buffer_ofs, buf, len);
560 it->buffer_ofs += len;
564 static int xattr_skipvalue(struct xattr_iter *_it,
565 unsigned int value_sz)
567 struct listxattr_iter *it =
568 container_of(_it, struct listxattr_iter, it);
570 it->buffer[it->buffer_ofs++] = '\0';
574 static const struct xattr_iter_handlers list_xattr_handlers = {
575 .entry = xattr_entrylist,
576 .name = xattr_namelist,
577 .alloc_buffer = xattr_skipvalue,
581 static int inline_listxattr(struct listxattr_iter *it)
584 unsigned int remaining;
586 ret = inline_xattr_iter_begin(&it->it, d_inode(it->dentry));
592 ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining);
596 return ret ? ret : it->buffer_ofs;
599 static int shared_listxattr(struct listxattr_iter *it)
601 struct inode *const inode = d_inode(it->dentry);
602 struct erofs_inode *const vi = EROFS_I(inode);
603 struct super_block *const sb = it->it.sb;
604 unsigned int i, xsid;
607 for (i = 0; i < vi->xattr_shared_count; ++i) {
608 xsid = vi->xattr_shared_xattrs[i];
609 it->it.blkaddr = erofs_xattr_blkaddr(sb, xsid);
610 it->it.ofs = erofs_xattr_blkoff(sb, xsid);
611 it->it.kaddr = erofs_read_metabuf(&it->it.buf, sb,
612 it->it.blkaddr, EROFS_KMAP);
613 if (IS_ERR(it->it.kaddr))
614 return PTR_ERR(it->it.kaddr);
616 ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL);
620 return ret ? ret : it->buffer_ofs;
623 ssize_t erofs_listxattr(struct dentry *dentry,
624 char *buffer, size_t buffer_size)
627 struct listxattr_iter it;
629 ret = erofs_init_inode_xattrs(d_inode(dentry));
635 it.it.buf = __EROFS_BUF_INITIALIZER;
638 it.buffer_size = buffer_size;
641 it.it.sb = dentry->d_sb;
643 ret = inline_listxattr(&it);
644 if (ret >= 0 || ret == -ENOATTR)
645 ret = shared_listxattr(&it);
646 erofs_put_metabuf(&it.it.buf);
650 void erofs_xattr_prefixes_cleanup(struct super_block *sb)
652 struct erofs_sb_info *sbi = EROFS_SB(sb);
655 if (sbi->xattr_prefixes) {
656 for (i = 0; i < sbi->xattr_prefix_count; i++)
657 kfree(sbi->xattr_prefixes[i].prefix);
658 kfree(sbi->xattr_prefixes);
659 sbi->xattr_prefixes = NULL;
663 int erofs_xattr_prefixes_init(struct super_block *sb)
665 struct erofs_sb_info *sbi = EROFS_SB(sb);
666 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
667 erofs_off_t pos = (erofs_off_t)sbi->xattr_prefix_start << 2;
668 struct erofs_xattr_prefix_item *pfs;
671 if (!sbi->xattr_prefix_count)
674 pfs = kzalloc(sbi->xattr_prefix_count * sizeof(*pfs), GFP_KERNEL);
678 if (sbi->packed_inode)
679 buf.inode = sbi->packed_inode;
681 erofs_init_metabuf(&buf, sb);
683 for (i = 0; i < sbi->xattr_prefix_count; i++) {
684 void *ptr = erofs_read_metadata(sb, &buf, &pos, &len);
689 } else if (len < sizeof(*pfs->prefix) ||
690 len > EROFS_NAME_LEN + sizeof(*pfs->prefix)) {
696 pfs[i].infix_len = len - sizeof(struct erofs_xattr_long_prefix);
699 erofs_put_metabuf(&buf);
700 sbi->xattr_prefixes = pfs;
702 erofs_xattr_prefixes_cleanup(sb);
706 #ifdef CONFIG_EROFS_FS_POSIX_ACL
707 struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
709 struct posix_acl *acl;
714 return ERR_PTR(-ECHILD);
717 case ACL_TYPE_ACCESS:
718 prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
720 case ACL_TYPE_DEFAULT:
721 prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
724 return ERR_PTR(-EINVAL);
727 rc = erofs_getxattr(inode, prefix, "", NULL, 0);
729 value = kmalloc(rc, GFP_KERNEL);
731 return ERR_PTR(-ENOMEM);
732 rc = erofs_getxattr(inode, prefix, "", value, rc);
740 acl = posix_acl_from_xattr(&init_user_ns, value, rc);