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>
8 #include <linux/xxhash.h>
11 struct erofs_xattr_iter {
12 struct super_block *sb;
18 int buffer_size, buffer_ofs;
25 struct dentry *dentry;
28 static int erofs_init_inode_xattrs(struct inode *inode)
30 struct erofs_inode *const vi = EROFS_I(inode);
31 struct erofs_xattr_iter it;
33 struct erofs_xattr_ibody_header *ih;
34 struct super_block *sb = inode->i_sb;
37 /* the most case is that xattrs of this inode are initialized. */
38 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) {
40 * paired with smp_mb() at the end of the function to ensure
41 * fields will only be observed after the bit is set.
47 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE))
50 /* someone has initialized xattrs for us? */
51 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags))
55 * bypass all xattr operations if ->xattr_isize is not greater than
56 * sizeof(struct erofs_xattr_ibody_header), in detail:
57 * 1) it is not enough to contain erofs_xattr_ibody_header then
58 * ->xattr_isize should be 0 (it means no xattr);
59 * 2) it is just to contain erofs_xattr_ibody_header, which is on-disk
60 * undefined right now (maybe use later with some new sb feature).
62 if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) {
64 "xattr_isize %d of nid %llu is not supported yet",
65 vi->xattr_isize, vi->nid);
68 } else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) {
69 if (vi->xattr_isize) {
70 erofs_err(sb, "bogus xattr ibody @ nid %llu", vi->nid);
73 goto out_unlock; /* xattr ondisk layout error */
79 it.buf = __EROFS_BUF_INITIALIZER;
80 erofs_init_metabuf(&it.buf, sb);
81 it.pos = erofs_iloc(inode) + vi->inode_isize;
83 /* read in shared xattr array (non-atomic, see kmalloc below) */
84 it.kaddr = erofs_bread(&it.buf, erofs_blknr(sb, it.pos), EROFS_KMAP);
85 if (IS_ERR(it.kaddr)) {
86 ret = PTR_ERR(it.kaddr);
90 ih = it.kaddr + erofs_blkoff(sb, it.pos);
91 vi->xattr_name_filter = le32_to_cpu(ih->h_name_filter);
92 vi->xattr_shared_count = ih->h_shared_count;
93 vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
94 sizeof(uint), GFP_KERNEL);
95 if (!vi->xattr_shared_xattrs) {
96 erofs_put_metabuf(&it.buf);
101 /* let's skip ibody header */
102 it.pos += sizeof(struct erofs_xattr_ibody_header);
104 for (i = 0; i < vi->xattr_shared_count; ++i) {
105 it.kaddr = erofs_bread(&it.buf, erofs_blknr(sb, it.pos),
107 if (IS_ERR(it.kaddr)) {
108 kfree(vi->xattr_shared_xattrs);
109 vi->xattr_shared_xattrs = NULL;
110 ret = PTR_ERR(it.kaddr);
113 vi->xattr_shared_xattrs[i] = le32_to_cpu(*(__le32 *)
114 (it.kaddr + erofs_blkoff(sb, it.pos)));
115 it.pos += 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);
128 static bool erofs_xattr_user_list(struct dentry *dentry)
130 return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER);
133 static bool erofs_xattr_trusted_list(struct dentry *dentry)
135 return capable(CAP_SYS_ADMIN);
138 static int erofs_xattr_generic_get(const struct xattr_handler *handler,
139 struct dentry *unused, struct inode *inode,
140 const char *name, void *buffer, size_t size)
142 if (handler->flags == EROFS_XATTR_INDEX_USER &&
143 !test_opt(&EROFS_I_SB(inode)->opt, XATTR_USER))
146 return erofs_getxattr(inode, handler->flags, name, buffer, size);
149 const struct xattr_handler erofs_xattr_user_handler = {
150 .prefix = XATTR_USER_PREFIX,
151 .flags = EROFS_XATTR_INDEX_USER,
152 .list = erofs_xattr_user_list,
153 .get = erofs_xattr_generic_get,
156 const struct xattr_handler erofs_xattr_trusted_handler = {
157 .prefix = XATTR_TRUSTED_PREFIX,
158 .flags = EROFS_XATTR_INDEX_TRUSTED,
159 .list = erofs_xattr_trusted_list,
160 .get = erofs_xattr_generic_get,
163 #ifdef CONFIG_EROFS_FS_SECURITY
164 const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
165 .prefix = XATTR_SECURITY_PREFIX,
166 .flags = EROFS_XATTR_INDEX_SECURITY,
167 .get = erofs_xattr_generic_get,
171 const struct xattr_handler *erofs_xattr_handlers[] = {
172 &erofs_xattr_user_handler,
173 &erofs_xattr_trusted_handler,
174 #ifdef CONFIG_EROFS_FS_SECURITY
175 &erofs_xattr_security_handler,
180 static int erofs_xattr_copy_to_buffer(struct erofs_xattr_iter *it,
183 unsigned int slice, processed;
184 struct super_block *sb = it->sb;
187 for (processed = 0; processed < len; processed += slice) {
188 it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
190 if (IS_ERR(it->kaddr))
191 return PTR_ERR(it->kaddr);
193 src = it->kaddr + erofs_blkoff(sb, it->pos);
194 slice = min_t(unsigned int, sb->s_blocksize -
195 erofs_blkoff(sb, it->pos), len - processed);
196 memcpy(it->buffer + it->buffer_ofs, src, slice);
197 it->buffer_ofs += slice;
203 static int erofs_listxattr_foreach(struct erofs_xattr_iter *it)
205 struct erofs_xattr_entry entry;
206 unsigned int base_index, name_total, prefix_len, infix_len = 0;
207 const char *prefix, *infix = NULL;
210 /* 1. handle xattr entry */
211 entry = *(struct erofs_xattr_entry *)
212 (it->kaddr + erofs_blkoff(it->sb, it->pos));
213 it->pos += sizeof(struct erofs_xattr_entry);
215 base_index = entry.e_name_index;
216 if (entry.e_name_index & EROFS_XATTR_LONG_PREFIX) {
217 struct erofs_sb_info *sbi = EROFS_SB(it->sb);
218 struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
219 (entry.e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
221 if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
223 infix = pf->prefix->infix;
224 infix_len = pf->infix_len;
225 base_index = pf->prefix->base_index;
228 prefix = erofs_xattr_prefix(base_index, it->dentry);
231 prefix_len = strlen(prefix);
232 name_total = prefix_len + infix_len + entry.e_name_len + 1;
235 it->buffer_ofs += name_total;
239 if (it->buffer_ofs + name_total > it->buffer_size)
242 memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
243 memcpy(it->buffer + it->buffer_ofs + prefix_len, infix, infix_len);
244 it->buffer_ofs += prefix_len + infix_len;
246 /* 2. handle xattr name */
247 err = erofs_xattr_copy_to_buffer(it, entry.e_name_len);
251 it->buffer[it->buffer_ofs++] = '\0';
255 static int erofs_getxattr_foreach(struct erofs_xattr_iter *it)
257 struct super_block *sb = it->sb;
258 struct erofs_xattr_entry entry;
259 unsigned int slice, processed, value_sz;
261 /* 1. handle xattr entry */
262 entry = *(struct erofs_xattr_entry *)
263 (it->kaddr + erofs_blkoff(sb, it->pos));
264 it->pos += sizeof(struct erofs_xattr_entry);
265 value_sz = le16_to_cpu(entry.e_value_size);
267 /* should also match the infix for long name prefixes */
268 if (entry.e_name_index & EROFS_XATTR_LONG_PREFIX) {
269 struct erofs_sb_info *sbi = EROFS_SB(sb);
270 struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
271 (entry.e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
273 if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
276 if (it->index != pf->prefix->base_index ||
277 it->name.len != entry.e_name_len + pf->infix_len)
280 if (memcmp(it->name.name, pf->prefix->infix, pf->infix_len))
283 it->infix_len = pf->infix_len;
285 if (it->index != entry.e_name_index ||
286 it->name.len != entry.e_name_len)
292 /* 2. handle xattr name */
293 for (processed = 0; processed < entry.e_name_len; processed += slice) {
294 it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
296 if (IS_ERR(it->kaddr))
297 return PTR_ERR(it->kaddr);
299 slice = min_t(unsigned int,
300 sb->s_blocksize - erofs_blkoff(sb, it->pos),
301 entry.e_name_len - processed);
302 if (memcmp(it->name.name + it->infix_len + processed,
303 it->kaddr + erofs_blkoff(sb, it->pos), slice))
308 /* 3. handle xattr value */
310 it->buffer_ofs = value_sz;
314 if (it->buffer_size < value_sz)
317 return erofs_xattr_copy_to_buffer(it, value_sz);
320 static int erofs_xattr_iter_inline(struct erofs_xattr_iter *it,
321 struct inode *inode, bool getxattr)
323 struct erofs_inode *const vi = EROFS_I(inode);
324 unsigned int xattr_header_sz, remaining, entry_sz;
325 erofs_off_t next_pos;
328 xattr_header_sz = sizeof(struct erofs_xattr_ibody_header) +
329 sizeof(u32) * vi->xattr_shared_count;
330 if (xattr_header_sz >= vi->xattr_isize) {
331 DBG_BUGON(xattr_header_sz > vi->xattr_isize);
335 remaining = vi->xattr_isize - xattr_header_sz;
336 it->pos = erofs_iloc(inode) + vi->inode_isize + xattr_header_sz;
339 it->kaddr = erofs_bread(&it->buf, erofs_blknr(it->sb, it->pos),
341 if (IS_ERR(it->kaddr))
342 return PTR_ERR(it->kaddr);
344 entry_sz = erofs_xattr_entry_size(it->kaddr +
345 erofs_blkoff(it->sb, it->pos));
346 /* xattr on-disk corruption: xattr entry beyond xattr_isize */
347 if (remaining < entry_sz) {
349 return -EFSCORRUPTED;
351 remaining -= entry_sz;
352 next_pos = it->pos + entry_sz;
355 ret = erofs_getxattr_foreach(it);
357 ret = erofs_listxattr_foreach(it);
358 if ((getxattr && ret != -ENOATTR) || (!getxattr && ret))
366 static int erofs_xattr_iter_shared(struct erofs_xattr_iter *it,
367 struct inode *inode, bool getxattr)
369 struct erofs_inode *const vi = EROFS_I(inode);
370 struct super_block *const sb = it->sb;
371 struct erofs_sb_info *sbi = EROFS_SB(sb);
375 for (i = 0; i < vi->xattr_shared_count; ++i) {
376 it->pos = erofs_pos(sb, sbi->xattr_blkaddr) +
377 vi->xattr_shared_xattrs[i] * sizeof(__le32);
378 it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
380 if (IS_ERR(it->kaddr))
381 return PTR_ERR(it->kaddr);
384 ret = erofs_getxattr_foreach(it);
386 ret = erofs_listxattr_foreach(it);
387 if ((getxattr && ret != -ENOATTR) || (!getxattr && ret))
393 int erofs_getxattr(struct inode *inode, int index, const char *name,
394 void *buffer, size_t buffer_size)
397 unsigned int hashbit;
398 struct erofs_xattr_iter it;
399 struct erofs_inode *vi = EROFS_I(inode);
400 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
405 ret = erofs_init_inode_xattrs(inode);
409 /* reserved flag is non-zero if there's any change of on-disk format */
410 if (erofs_sb_has_xattr_filter(sbi) && !sbi->xattr_filter_reserved) {
411 hashbit = xxh32(name, strlen(name),
412 EROFS_XATTR_FILTER_SEED + index);
413 hashbit &= EROFS_XATTR_FILTER_BITS - 1;
414 if (vi->xattr_name_filter & (1U << hashbit))
419 it.name = (struct qstr)QSTR_INIT(name, strlen(name));
420 if (it.name.len > EROFS_NAME_LEN)
424 it.buf = __EROFS_BUF_INITIALIZER;
425 erofs_init_metabuf(&it.buf, it.sb);
427 it.buffer_size = buffer_size;
430 ret = erofs_xattr_iter_inline(&it, inode, true);
432 ret = erofs_xattr_iter_shared(&it, inode, true);
433 erofs_put_metabuf(&it.buf);
434 return ret ? ret : it.buffer_ofs;
437 ssize_t erofs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
440 struct erofs_xattr_iter it;
441 struct inode *inode = d_inode(dentry);
443 ret = erofs_init_inode_xattrs(inode);
449 it.sb = dentry->d_sb;
450 it.buf = __EROFS_BUF_INITIALIZER;
451 erofs_init_metabuf(&it.buf, it.sb);
454 it.buffer_size = buffer_size;
457 ret = erofs_xattr_iter_inline(&it, inode, false);
458 if (!ret || ret == -ENOATTR)
459 ret = erofs_xattr_iter_shared(&it, inode, false);
462 erofs_put_metabuf(&it.buf);
463 return ret ? ret : it.buffer_ofs;
466 void erofs_xattr_prefixes_cleanup(struct super_block *sb)
468 struct erofs_sb_info *sbi = EROFS_SB(sb);
471 if (sbi->xattr_prefixes) {
472 for (i = 0; i < sbi->xattr_prefix_count; i++)
473 kfree(sbi->xattr_prefixes[i].prefix);
474 kfree(sbi->xattr_prefixes);
475 sbi->xattr_prefixes = NULL;
479 int erofs_xattr_prefixes_init(struct super_block *sb)
481 struct erofs_sb_info *sbi = EROFS_SB(sb);
482 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
483 erofs_off_t pos = (erofs_off_t)sbi->xattr_prefix_start << 2;
484 struct erofs_xattr_prefix_item *pfs;
487 if (!sbi->xattr_prefix_count)
490 pfs = kzalloc(sbi->xattr_prefix_count * sizeof(*pfs), GFP_KERNEL);
494 if (sbi->packed_inode)
495 buf.inode = sbi->packed_inode;
497 erofs_init_metabuf(&buf, sb);
499 for (i = 0; i < sbi->xattr_prefix_count; i++) {
500 void *ptr = erofs_read_metadata(sb, &buf, &pos, &len);
505 } else if (len < sizeof(*pfs->prefix) ||
506 len > EROFS_NAME_LEN + sizeof(*pfs->prefix)) {
512 pfs[i].infix_len = len - sizeof(struct erofs_xattr_long_prefix);
515 erofs_put_metabuf(&buf);
516 sbi->xattr_prefixes = pfs;
518 erofs_xattr_prefixes_cleanup(sb);
522 #ifdef CONFIG_EROFS_FS_POSIX_ACL
523 struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
525 struct posix_acl *acl;
530 return ERR_PTR(-ECHILD);
533 case ACL_TYPE_ACCESS:
534 prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
536 case ACL_TYPE_DEFAULT:
537 prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
540 return ERR_PTR(-EINVAL);
543 rc = erofs_getxattr(inode, prefix, "", NULL, 0);
545 value = kmalloc(rc, GFP_KERNEL);
547 return ERR_PTR(-ENOMEM);
548 rc = erofs_getxattr(inode, prefix, "", value, rc);
556 acl = posix_acl_from_xattr(&init_user_ns, value, rc);