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 struct erofs_xattr_iter {
11 struct super_block *sb;
17 int buffer_size, buffer_ofs;
24 struct dentry *dentry;
27 static int erofs_init_inode_xattrs(struct inode *inode)
29 struct erofs_inode *const vi = EROFS_I(inode);
30 struct erofs_xattr_iter it;
32 struct erofs_xattr_ibody_header *ih;
33 struct super_block *sb = inode->i_sb;
36 /* the most case is that xattrs of this inode are initialized. */
37 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) {
39 * paired with smp_mb() at the end of the function to ensure
40 * fields will only be observed after the bit is set.
46 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE))
49 /* someone has initialized xattrs for us? */
50 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags))
54 * bypass all xattr operations if ->xattr_isize is not greater than
55 * sizeof(struct erofs_xattr_ibody_header), in detail:
56 * 1) it is not enough to contain erofs_xattr_ibody_header then
57 * ->xattr_isize should be 0 (it means no xattr);
58 * 2) it is just to contain erofs_xattr_ibody_header, which is on-disk
59 * undefined right now (maybe use later with some new sb feature).
61 if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) {
63 "xattr_isize %d of nid %llu is not supported yet",
64 vi->xattr_isize, vi->nid);
67 } else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) {
68 if (vi->xattr_isize) {
69 erofs_err(sb, "bogus xattr ibody @ nid %llu", vi->nid);
72 goto out_unlock; /* xattr ondisk layout error */
78 it.buf = __EROFS_BUF_INITIALIZER;
79 erofs_init_metabuf(&it.buf, sb);
80 it.pos = erofs_iloc(inode) + vi->inode_isize;
82 /* read in shared xattr array (non-atomic, see kmalloc below) */
83 it.kaddr = erofs_bread(&it.buf, erofs_blknr(sb, it.pos), EROFS_KMAP);
84 if (IS_ERR(it.kaddr)) {
85 ret = PTR_ERR(it.kaddr);
89 ih = it.kaddr + erofs_blkoff(sb, it.pos);
90 vi->xattr_shared_count = ih->h_shared_count;
91 vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
92 sizeof(uint), GFP_KERNEL);
93 if (!vi->xattr_shared_xattrs) {
94 erofs_put_metabuf(&it.buf);
99 /* let's skip ibody header */
100 it.pos += sizeof(struct erofs_xattr_ibody_header);
102 for (i = 0; i < vi->xattr_shared_count; ++i) {
103 it.kaddr = erofs_bread(&it.buf, erofs_blknr(sb, it.pos),
105 if (IS_ERR(it.kaddr)) {
106 kfree(vi->xattr_shared_xattrs);
107 vi->xattr_shared_xattrs = NULL;
108 ret = PTR_ERR(it.kaddr);
111 vi->xattr_shared_xattrs[i] = le32_to_cpu(*(__le32 *)
112 (it.kaddr + erofs_blkoff(sb, it.pos)));
113 it.pos += sizeof(__le32);
115 erofs_put_metabuf(&it.buf);
117 /* paired with smp_mb() at the beginning of the function. */
119 set_bit(EROFS_I_EA_INITED_BIT, &vi->flags);
122 clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags);
126 static bool erofs_xattr_user_list(struct dentry *dentry)
128 return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER);
131 static bool erofs_xattr_trusted_list(struct dentry *dentry)
133 return capable(CAP_SYS_ADMIN);
136 static int erofs_xattr_generic_get(const struct xattr_handler *handler,
137 struct dentry *unused, struct inode *inode,
138 const char *name, void *buffer, size_t size)
140 if (handler->flags == EROFS_XATTR_INDEX_USER &&
141 !test_opt(&EROFS_I_SB(inode)->opt, XATTR_USER))
144 return erofs_getxattr(inode, handler->flags, name, buffer, size);
147 const struct xattr_handler erofs_xattr_user_handler = {
148 .prefix = XATTR_USER_PREFIX,
149 .flags = EROFS_XATTR_INDEX_USER,
150 .list = erofs_xattr_user_list,
151 .get = erofs_xattr_generic_get,
154 const struct xattr_handler erofs_xattr_trusted_handler = {
155 .prefix = XATTR_TRUSTED_PREFIX,
156 .flags = EROFS_XATTR_INDEX_TRUSTED,
157 .list = erofs_xattr_trusted_list,
158 .get = erofs_xattr_generic_get,
161 #ifdef CONFIG_EROFS_FS_SECURITY
162 const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
163 .prefix = XATTR_SECURITY_PREFIX,
164 .flags = EROFS_XATTR_INDEX_SECURITY,
165 .get = erofs_xattr_generic_get,
169 const struct xattr_handler *erofs_xattr_handlers[] = {
170 &erofs_xattr_user_handler,
171 &erofs_xattr_trusted_handler,
172 #ifdef CONFIG_EROFS_FS_SECURITY
173 &erofs_xattr_security_handler,
178 static int erofs_xattr_copy_to_buffer(struct erofs_xattr_iter *it,
181 unsigned int slice, processed;
182 struct super_block *sb = it->sb;
185 for (processed = 0; processed < len; processed += slice) {
186 it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
188 if (IS_ERR(it->kaddr))
189 return PTR_ERR(it->kaddr);
191 src = it->kaddr + erofs_blkoff(sb, it->pos);
192 slice = min_t(unsigned int, sb->s_blocksize -
193 erofs_blkoff(sb, it->pos), len - processed);
194 memcpy(it->buffer + it->buffer_ofs, src, slice);
195 it->buffer_ofs += slice;
201 static int erofs_listxattr_foreach(struct erofs_xattr_iter *it)
203 struct erofs_xattr_entry entry;
204 unsigned int base_index, name_total, prefix_len, infix_len = 0;
205 const char *prefix, *infix = NULL;
208 /* 1. handle xattr entry */
209 entry = *(struct erofs_xattr_entry *)
210 (it->kaddr + erofs_blkoff(it->sb, it->pos));
211 it->pos += sizeof(struct erofs_xattr_entry);
213 base_index = entry.e_name_index;
214 if (entry.e_name_index & EROFS_XATTR_LONG_PREFIX) {
215 struct erofs_sb_info *sbi = EROFS_SB(it->sb);
216 struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
217 (entry.e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
219 if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
221 infix = pf->prefix->infix;
222 infix_len = pf->infix_len;
223 base_index = pf->prefix->base_index;
226 prefix = erofs_xattr_prefix(base_index, it->dentry);
229 prefix_len = strlen(prefix);
230 name_total = prefix_len + infix_len + entry.e_name_len + 1;
233 it->buffer_ofs += name_total;
237 if (it->buffer_ofs + name_total > it->buffer_size)
240 memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
241 memcpy(it->buffer + it->buffer_ofs + prefix_len, infix, infix_len);
242 it->buffer_ofs += prefix_len + infix_len;
244 /* 2. handle xattr name */
245 err = erofs_xattr_copy_to_buffer(it, entry.e_name_len);
249 it->buffer[it->buffer_ofs++] = '\0';
253 static int erofs_getxattr_foreach(struct erofs_xattr_iter *it)
255 struct super_block *sb = it->sb;
256 struct erofs_xattr_entry entry;
257 unsigned int slice, processed, value_sz;
259 /* 1. handle xattr entry */
260 entry = *(struct erofs_xattr_entry *)
261 (it->kaddr + erofs_blkoff(sb, it->pos));
262 it->pos += sizeof(struct erofs_xattr_entry);
263 value_sz = le16_to_cpu(entry.e_value_size);
265 /* should also match the infix for long name prefixes */
266 if (entry.e_name_index & EROFS_XATTR_LONG_PREFIX) {
267 struct erofs_sb_info *sbi = EROFS_SB(sb);
268 struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
269 (entry.e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
271 if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
274 if (it->index != pf->prefix->base_index ||
275 it->name.len != entry.e_name_len + pf->infix_len)
278 if (memcmp(it->name.name, pf->prefix->infix, pf->infix_len))
281 it->infix_len = pf->infix_len;
283 if (it->index != entry.e_name_index ||
284 it->name.len != entry.e_name_len)
290 /* 2. handle xattr name */
291 for (processed = 0; processed < entry.e_name_len; processed += slice) {
292 it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
294 if (IS_ERR(it->kaddr))
295 return PTR_ERR(it->kaddr);
297 slice = min_t(unsigned int,
298 sb->s_blocksize - erofs_blkoff(sb, it->pos),
299 entry.e_name_len - processed);
300 if (memcmp(it->name.name + it->infix_len + processed,
301 it->kaddr + erofs_blkoff(sb, it->pos), slice))
306 /* 3. handle xattr value */
308 it->buffer_ofs = value_sz;
312 if (it->buffer_size < value_sz)
315 return erofs_xattr_copy_to_buffer(it, value_sz);
318 static int erofs_xattr_iter_inline(struct erofs_xattr_iter *it,
319 struct inode *inode, bool getxattr)
321 struct erofs_inode *const vi = EROFS_I(inode);
322 unsigned int xattr_header_sz, remaining, entry_sz;
323 erofs_off_t next_pos;
326 xattr_header_sz = sizeof(struct erofs_xattr_ibody_header) +
327 sizeof(u32) * vi->xattr_shared_count;
328 if (xattr_header_sz >= vi->xattr_isize) {
329 DBG_BUGON(xattr_header_sz > vi->xattr_isize);
333 remaining = vi->xattr_isize - xattr_header_sz;
334 it->pos = erofs_iloc(inode) + vi->inode_isize + xattr_header_sz;
337 it->kaddr = erofs_bread(&it->buf, erofs_blknr(it->sb, it->pos),
339 if (IS_ERR(it->kaddr))
340 return PTR_ERR(it->kaddr);
342 entry_sz = erofs_xattr_entry_size(it->kaddr +
343 erofs_blkoff(it->sb, it->pos));
344 /* xattr on-disk corruption: xattr entry beyond xattr_isize */
345 if (remaining < entry_sz) {
347 return -EFSCORRUPTED;
349 remaining -= entry_sz;
350 next_pos = it->pos + entry_sz;
353 ret = erofs_getxattr_foreach(it);
355 ret = erofs_listxattr_foreach(it);
356 if ((getxattr && ret != -ENOATTR) || (!getxattr && ret))
364 static int erofs_xattr_iter_shared(struct erofs_xattr_iter *it,
365 struct inode *inode, bool getxattr)
367 struct erofs_inode *const vi = EROFS_I(inode);
368 struct super_block *const sb = it->sb;
369 struct erofs_sb_info *sbi = EROFS_SB(sb);
373 for (i = 0; i < vi->xattr_shared_count; ++i) {
374 it->pos = erofs_pos(sb, sbi->xattr_blkaddr) +
375 vi->xattr_shared_xattrs[i] * sizeof(__le32);
376 it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
378 if (IS_ERR(it->kaddr))
379 return PTR_ERR(it->kaddr);
382 ret = erofs_getxattr_foreach(it);
384 ret = erofs_listxattr_foreach(it);
385 if ((getxattr && ret != -ENOATTR) || (!getxattr && ret))
391 int erofs_getxattr(struct inode *inode, int index, const char *name,
392 void *buffer, size_t buffer_size)
395 struct erofs_xattr_iter it;
400 ret = erofs_init_inode_xattrs(inode);
405 it.name = (struct qstr)QSTR_INIT(name, strlen(name));
406 if (it.name.len > EROFS_NAME_LEN)
410 it.buf = __EROFS_BUF_INITIALIZER;
411 erofs_init_metabuf(&it.buf, it.sb);
413 it.buffer_size = buffer_size;
416 ret = erofs_xattr_iter_inline(&it, inode, true);
418 ret = erofs_xattr_iter_shared(&it, inode, true);
419 erofs_put_metabuf(&it.buf);
420 return ret ? ret : it.buffer_ofs;
423 ssize_t erofs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
426 struct erofs_xattr_iter it;
427 struct inode *inode = d_inode(dentry);
429 ret = erofs_init_inode_xattrs(inode);
435 it.sb = dentry->d_sb;
436 it.buf = __EROFS_BUF_INITIALIZER;
437 erofs_init_metabuf(&it.buf, it.sb);
440 it.buffer_size = buffer_size;
443 ret = erofs_xattr_iter_inline(&it, inode, false);
444 if (!ret || ret == -ENOATTR)
445 ret = erofs_xattr_iter_shared(&it, inode, false);
448 erofs_put_metabuf(&it.buf);
449 return ret ? ret : it.buffer_ofs;
452 void erofs_xattr_prefixes_cleanup(struct super_block *sb)
454 struct erofs_sb_info *sbi = EROFS_SB(sb);
457 if (sbi->xattr_prefixes) {
458 for (i = 0; i < sbi->xattr_prefix_count; i++)
459 kfree(sbi->xattr_prefixes[i].prefix);
460 kfree(sbi->xattr_prefixes);
461 sbi->xattr_prefixes = NULL;
465 int erofs_xattr_prefixes_init(struct super_block *sb)
467 struct erofs_sb_info *sbi = EROFS_SB(sb);
468 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
469 erofs_off_t pos = (erofs_off_t)sbi->xattr_prefix_start << 2;
470 struct erofs_xattr_prefix_item *pfs;
473 if (!sbi->xattr_prefix_count)
476 pfs = kzalloc(sbi->xattr_prefix_count * sizeof(*pfs), GFP_KERNEL);
480 if (sbi->packed_inode)
481 buf.inode = sbi->packed_inode;
483 erofs_init_metabuf(&buf, sb);
485 for (i = 0; i < sbi->xattr_prefix_count; i++) {
486 void *ptr = erofs_read_metadata(sb, &buf, &pos, &len);
491 } else if (len < sizeof(*pfs->prefix) ||
492 len > EROFS_NAME_LEN + sizeof(*pfs->prefix)) {
498 pfs[i].infix_len = len - sizeof(struct erofs_xattr_long_prefix);
501 erofs_put_metabuf(&buf);
502 sbi->xattr_prefixes = pfs;
504 erofs_xattr_prefixes_cleanup(sb);
508 #ifdef CONFIG_EROFS_FS_POSIX_ACL
509 struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
511 struct posix_acl *acl;
516 return ERR_PTR(-ECHILD);
519 case ACL_TYPE_ACCESS:
520 prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
522 case ACL_TYPE_DEFAULT:
523 prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
526 return ERR_PTR(-EINVAL);
529 rc = erofs_getxattr(inode, prefix, "", NULL, 0);
531 value = kmalloc(rc, GFP_KERNEL);
533 return ERR_PTR(-ENOMEM);
534 rc = erofs_getxattr(inode, prefix, "", value, rc);
542 acl = posix_acl_from_xattr(&init_user_ns, value, rc);