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>
11 struct super_block *sb;
19 static int init_inode_xattrs(struct inode *inode)
21 struct erofs_inode *const vi = EROFS_I(inode);
24 struct erofs_xattr_ibody_header *ih;
25 struct super_block *sb;
26 struct erofs_sb_info *sbi;
29 /* the most case is that xattrs of this inode are initialized. */
30 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) {
32 * paired with smp_mb() at the end of the function to ensure
33 * fields will only be observed after the bit is set.
39 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE))
42 /* someone has initialized xattrs for us? */
43 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags))
47 * bypass all xattr operations if ->xattr_isize is not greater than
48 * sizeof(struct erofs_xattr_ibody_header), in detail:
49 * 1) it is not enough to contain erofs_xattr_ibody_header then
50 * ->xattr_isize should be 0 (it means no xattr);
51 * 2) it is just to contain erofs_xattr_ibody_header, which is on-disk
52 * undefined right now (maybe use later with some new sb feature).
54 if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) {
55 erofs_err(inode->i_sb,
56 "xattr_isize %d of nid %llu is not supported yet",
57 vi->xattr_isize, vi->nid);
60 } else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) {
61 if (vi->xattr_isize) {
62 erofs_err(inode->i_sb,
63 "bogus xattr ibody @ nid %llu", vi->nid);
66 goto out_unlock; /* xattr ondisk layout error */
74 it.buf = __EROFS_BUF_INITIALIZER;
75 it.blkaddr = erofs_blknr(iloc(sbi, vi->nid) + vi->inode_isize);
76 it.ofs = erofs_blkoff(iloc(sbi, vi->nid) + vi->inode_isize);
78 /* read in shared xattr array (non-atomic, see kmalloc below) */
79 it.kaddr = erofs_read_metabuf(&it.buf, sb, it.blkaddr, EROFS_KMAP);
80 if (IS_ERR(it.kaddr)) {
81 ret = PTR_ERR(it.kaddr);
85 ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
86 vi->xattr_shared_count = ih->h_shared_count;
87 vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
88 sizeof(uint), GFP_KERNEL);
89 if (!vi->xattr_shared_xattrs) {
90 erofs_put_metabuf(&it.buf);
95 /* let's skip ibody header */
96 it.ofs += sizeof(struct erofs_xattr_ibody_header);
98 for (i = 0; i < vi->xattr_shared_count; ++i) {
99 if (it.ofs >= EROFS_BLKSIZ) {
100 /* cannot be unaligned */
101 DBG_BUGON(it.ofs != EROFS_BLKSIZ);
103 it.kaddr = erofs_read_metabuf(&it.buf, sb, ++it.blkaddr,
105 if (IS_ERR(it.kaddr)) {
106 kfree(vi->xattr_shared_xattrs);
107 vi->xattr_shared_xattrs = NULL;
108 ret = PTR_ERR(it.kaddr);
113 vi->xattr_shared_xattrs[i] =
114 le32_to_cpu(*(__le32 *)(it.kaddr + it.ofs));
115 it.ofs += sizeof(__le32);
117 erofs_put_metabuf(&it.buf);
119 /* paired with smp_mb() at the beginning of the function. */
121 set_bit(EROFS_I_EA_INITED_BIT, &vi->flags);
124 clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags);
129 * the general idea for these return values is
130 * if 0 is returned, go on processing the current xattr;
131 * 1 (> 0) is returned, skip this round to process the next xattr;
132 * -err (< 0) is returned, an error (maybe ENOXATTR) occurred
133 * and need to be handled
135 struct xattr_iter_handlers {
136 int (*entry)(struct xattr_iter *_it, struct erofs_xattr_entry *entry);
137 int (*name)(struct xattr_iter *_it, unsigned int processed, char *buf,
139 int (*alloc_buffer)(struct xattr_iter *_it, unsigned int value_sz);
140 void (*value)(struct xattr_iter *_it, unsigned int processed, char *buf,
144 static inline int xattr_iter_fixup(struct xattr_iter *it)
146 if (it->ofs < EROFS_BLKSIZ)
149 it->blkaddr += erofs_blknr(it->ofs);
150 it->kaddr = erofs_read_metabuf(&it->buf, it->sb, it->blkaddr,
152 if (IS_ERR(it->kaddr))
153 return PTR_ERR(it->kaddr);
154 it->ofs = erofs_blkoff(it->ofs);
158 static int inline_xattr_iter_begin(struct xattr_iter *it,
161 struct erofs_inode *const vi = EROFS_I(inode);
162 struct erofs_sb_info *const sbi = EROFS_SB(inode->i_sb);
163 unsigned int xattr_header_sz, inline_xattr_ofs;
165 xattr_header_sz = inlinexattr_header_size(inode);
166 if (xattr_header_sz >= vi->xattr_isize) {
167 DBG_BUGON(xattr_header_sz > vi->xattr_isize);
171 inline_xattr_ofs = vi->inode_isize + xattr_header_sz;
173 it->blkaddr = erofs_blknr(iloc(sbi, vi->nid) + inline_xattr_ofs);
174 it->ofs = erofs_blkoff(iloc(sbi, vi->nid) + inline_xattr_ofs);
176 it->kaddr = erofs_read_metabuf(&it->buf, inode->i_sb, it->blkaddr,
178 if (IS_ERR(it->kaddr))
179 return PTR_ERR(it->kaddr);
180 return vi->xattr_isize - xattr_header_sz;
184 * Regardless of success or failure, `xattr_foreach' will end up with
185 * `ofs' pointing to the next xattr item rather than an arbitrary position.
187 static int xattr_foreach(struct xattr_iter *it,
188 const struct xattr_iter_handlers *op,
189 unsigned int *tlimit)
191 struct erofs_xattr_entry entry;
192 unsigned int value_sz, processed, slice;
195 /* 0. fixup blkaddr, ofs, ipage */
196 err = xattr_iter_fixup(it);
201 * 1. read xattr entry to the memory,
202 * since we do EROFS_XATTR_ALIGN
203 * therefore entry should be in the page
205 entry = *(struct erofs_xattr_entry *)(it->kaddr + it->ofs);
207 unsigned int entry_sz = erofs_xattr_entry_size(&entry);
209 /* xattr on-disk corruption: xattr entry beyond xattr_isize */
210 if (*tlimit < entry_sz) {
212 return -EFSCORRUPTED;
217 it->ofs += sizeof(struct erofs_xattr_entry);
218 value_sz = le16_to_cpu(entry.e_value_size);
221 err = op->entry(it, &entry);
223 it->ofs += entry.e_name_len + value_sz;
227 /* 2. handle xattr name (ofs will finally be at the end of name) */
230 while (processed < entry.e_name_len) {
231 if (it->ofs >= EROFS_BLKSIZ) {
232 DBG_BUGON(it->ofs > EROFS_BLKSIZ);
234 err = xattr_iter_fixup(it);
240 slice = min_t(unsigned int, EROFS_BLKSIZ - it->ofs,
241 entry.e_name_len - processed);
244 err = op->name(it, processed, it->kaddr + it->ofs, slice);
246 it->ofs += entry.e_name_len - processed + value_sz;
254 /* 3. handle xattr value */
257 if (op->alloc_buffer) {
258 err = op->alloc_buffer(it, value_sz);
265 while (processed < value_sz) {
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, EROFS_BLKSIZ - it->ofs,
276 value_sz - processed);
277 op->value(it, processed, it->kaddr + it->ofs, slice);
283 /* xattrs should be 4-byte aligned (on-disk constraint) */
284 it->ofs = EROFS_XATTR_ALIGN(it->ofs);
285 return err < 0 ? err : 0;
288 struct getxattr_iter {
289 struct xattr_iter it;
292 int buffer_size, index;
296 static int xattr_entrymatch(struct xattr_iter *_it,
297 struct erofs_xattr_entry *entry)
299 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
301 return (it->index != entry->e_name_index ||
302 it->name.len != entry->e_name_len) ? -ENOATTR : 0;
305 static int xattr_namematch(struct xattr_iter *_it,
306 unsigned int processed, char *buf, unsigned int len)
308 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
310 return memcmp(buf, it->name.name + processed, len) ? -ENOATTR : 0;
313 static int xattr_checkbuffer(struct xattr_iter *_it,
314 unsigned int value_sz)
316 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
317 int err = it->buffer_size < value_sz ? -ERANGE : 0;
319 it->buffer_size = value_sz;
320 return !it->buffer ? 1 : err;
323 static void xattr_copyvalue(struct xattr_iter *_it,
324 unsigned int processed,
325 char *buf, unsigned int len)
327 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
329 memcpy(it->buffer + processed, buf, len);
332 static const struct xattr_iter_handlers find_xattr_handlers = {
333 .entry = xattr_entrymatch,
334 .name = xattr_namematch,
335 .alloc_buffer = xattr_checkbuffer,
336 .value = xattr_copyvalue
339 static int inline_getxattr(struct inode *inode, struct getxattr_iter *it)
342 unsigned int remaining;
344 ret = inline_xattr_iter_begin(&it->it, inode);
350 ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining);
354 return ret ? ret : it->buffer_size;
357 static int shared_getxattr(struct inode *inode, struct getxattr_iter *it)
359 struct erofs_inode *const vi = EROFS_I(inode);
360 struct super_block *const sb = inode->i_sb;
361 struct erofs_sb_info *const sbi = EROFS_SB(sb);
365 for (i = 0; i < vi->xattr_shared_count; ++i) {
366 erofs_blk_t blkaddr =
367 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
369 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
370 it->it.kaddr = erofs_read_metabuf(&it->it.buf, sb, blkaddr,
372 if (IS_ERR(it->it.kaddr))
373 return PTR_ERR(it->it.kaddr);
374 it->it.blkaddr = blkaddr;
376 ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL);
380 return ret ? ret : it->buffer_size;
383 static bool erofs_xattr_user_list(struct dentry *dentry)
385 return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER);
388 static bool erofs_xattr_trusted_list(struct dentry *dentry)
390 return capable(CAP_SYS_ADMIN);
393 int erofs_getxattr(struct inode *inode, int index,
395 void *buffer, size_t buffer_size)
398 struct getxattr_iter it;
403 ret = init_inode_xattrs(inode);
408 it.name.len = strlen(name);
409 if (it.name.len > EROFS_NAME_LEN)
412 it.it.buf = __EROFS_BUF_INITIALIZER;
416 it.buffer_size = buffer_size;
418 it.it.sb = inode->i_sb;
419 ret = inline_getxattr(inode, &it);
421 ret = shared_getxattr(inode, &it);
422 erofs_put_metabuf(&it.it.buf);
426 static int erofs_xattr_generic_get(const struct xattr_handler *handler,
427 struct dentry *unused, struct inode *inode,
428 const char *name, void *buffer, size_t size)
430 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
432 switch (handler->flags) {
433 case EROFS_XATTR_INDEX_USER:
434 if (!test_opt(&sbi->opt, XATTR_USER))
437 case EROFS_XATTR_INDEX_TRUSTED:
439 case EROFS_XATTR_INDEX_SECURITY:
445 return erofs_getxattr(inode, handler->flags, name, buffer, size);
448 const struct xattr_handler erofs_xattr_user_handler = {
449 .prefix = XATTR_USER_PREFIX,
450 .flags = EROFS_XATTR_INDEX_USER,
451 .list = erofs_xattr_user_list,
452 .get = erofs_xattr_generic_get,
455 const struct xattr_handler erofs_xattr_trusted_handler = {
456 .prefix = XATTR_TRUSTED_PREFIX,
457 .flags = EROFS_XATTR_INDEX_TRUSTED,
458 .list = erofs_xattr_trusted_list,
459 .get = erofs_xattr_generic_get,
462 #ifdef CONFIG_EROFS_FS_SECURITY
463 const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
464 .prefix = XATTR_SECURITY_PREFIX,
465 .flags = EROFS_XATTR_INDEX_SECURITY,
466 .get = erofs_xattr_generic_get,
470 const struct xattr_handler *erofs_xattr_handlers[] = {
471 &erofs_xattr_user_handler,
472 #ifdef CONFIG_EROFS_FS_POSIX_ACL
473 &posix_acl_access_xattr_handler,
474 &posix_acl_default_xattr_handler,
476 &erofs_xattr_trusted_handler,
477 #ifdef CONFIG_EROFS_FS_SECURITY
478 &erofs_xattr_security_handler,
483 struct listxattr_iter {
484 struct xattr_iter it;
486 struct dentry *dentry;
488 int buffer_size, buffer_ofs;
491 static int xattr_entrylist(struct xattr_iter *_it,
492 struct erofs_xattr_entry *entry)
494 struct listxattr_iter *it =
495 container_of(_it, struct listxattr_iter, it);
496 unsigned int prefix_len;
499 const struct xattr_handler *h =
500 erofs_xattr_handler(entry->e_name_index);
502 if (!h || (h->list && !h->list(it->dentry)))
505 prefix = xattr_prefix(h);
506 prefix_len = strlen(prefix);
509 it->buffer_ofs += prefix_len + entry->e_name_len + 1;
513 if (it->buffer_ofs + prefix_len
514 + entry->e_name_len + 1 > it->buffer_size)
517 memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
518 it->buffer_ofs += prefix_len;
522 static int xattr_namelist(struct xattr_iter *_it,
523 unsigned int processed, char *buf, unsigned int len)
525 struct listxattr_iter *it =
526 container_of(_it, struct listxattr_iter, it);
528 memcpy(it->buffer + it->buffer_ofs, buf, len);
529 it->buffer_ofs += len;
533 static int xattr_skipvalue(struct xattr_iter *_it,
534 unsigned int value_sz)
536 struct listxattr_iter *it =
537 container_of(_it, struct listxattr_iter, it);
539 it->buffer[it->buffer_ofs++] = '\0';
543 static const struct xattr_iter_handlers list_xattr_handlers = {
544 .entry = xattr_entrylist,
545 .name = xattr_namelist,
546 .alloc_buffer = xattr_skipvalue,
550 static int inline_listxattr(struct listxattr_iter *it)
553 unsigned int remaining;
555 ret = inline_xattr_iter_begin(&it->it, d_inode(it->dentry));
561 ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining);
565 return ret ? ret : it->buffer_ofs;
568 static int shared_listxattr(struct listxattr_iter *it)
570 struct inode *const inode = d_inode(it->dentry);
571 struct erofs_inode *const vi = EROFS_I(inode);
572 struct super_block *const sb = inode->i_sb;
573 struct erofs_sb_info *const sbi = EROFS_SB(sb);
577 for (i = 0; i < vi->xattr_shared_count; ++i) {
578 erofs_blk_t blkaddr =
579 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
581 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
582 it->it.kaddr = erofs_read_metabuf(&it->it.buf, sb, blkaddr,
584 if (IS_ERR(it->it.kaddr))
585 return PTR_ERR(it->it.kaddr);
586 it->it.blkaddr = blkaddr;
588 ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL);
592 return ret ? ret : it->buffer_ofs;
595 ssize_t erofs_listxattr(struct dentry *dentry,
596 char *buffer, size_t buffer_size)
599 struct listxattr_iter it;
601 ret = init_inode_xattrs(d_inode(dentry));
607 it.it.buf = __EROFS_BUF_INITIALIZER;
610 it.buffer_size = buffer_size;
613 it.it.sb = dentry->d_sb;
615 ret = inline_listxattr(&it);
616 if (ret >= 0 || ret == -ENOATTR)
617 ret = shared_listxattr(&it);
618 erofs_put_metabuf(&it.it.buf);
622 #ifdef CONFIG_EROFS_FS_POSIX_ACL
623 struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
625 struct posix_acl *acl;
630 return ERR_PTR(-ECHILD);
633 case ACL_TYPE_ACCESS:
634 prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
636 case ACL_TYPE_DEFAULT:
637 prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
640 return ERR_PTR(-EINVAL);
643 rc = erofs_getxattr(inode, prefix, "", NULL, 0);
645 value = kmalloc(rc, GFP_KERNEL);
647 return ERR_PTR(-ENOMEM);
648 rc = erofs_getxattr(inode, prefix, "", value, rc);
656 acl = posix_acl_from_xattr(&init_user_ns, value, rc);