0001
0002
0003
0004
0005
0006
0007 #include <linux/security.h>
0008 #include "xattr.h"
0009
0010 struct xattr_iter {
0011 struct super_block *sb;
0012 struct erofs_buf buf;
0013 void *kaddr;
0014
0015 erofs_blk_t blkaddr;
0016 unsigned int ofs;
0017 };
0018
0019 static int init_inode_xattrs(struct inode *inode)
0020 {
0021 struct erofs_inode *const vi = EROFS_I(inode);
0022 struct xattr_iter it;
0023 unsigned int i;
0024 struct erofs_xattr_ibody_header *ih;
0025 struct super_block *sb;
0026 struct erofs_sb_info *sbi;
0027 int ret = 0;
0028
0029
0030 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) {
0031
0032
0033
0034
0035 smp_mb();
0036 return 0;
0037 }
0038
0039 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE))
0040 return -ERESTARTSYS;
0041
0042
0043 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags))
0044 goto out_unlock;
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054 if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) {
0055 erofs_err(inode->i_sb,
0056 "xattr_isize %d of nid %llu is not supported yet",
0057 vi->xattr_isize, vi->nid);
0058 ret = -EOPNOTSUPP;
0059 goto out_unlock;
0060 } else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) {
0061 if (vi->xattr_isize) {
0062 erofs_err(inode->i_sb,
0063 "bogus xattr ibody @ nid %llu", vi->nid);
0064 DBG_BUGON(1);
0065 ret = -EFSCORRUPTED;
0066 goto out_unlock;
0067 }
0068 ret = -ENOATTR;
0069 goto out_unlock;
0070 }
0071
0072 sb = inode->i_sb;
0073 sbi = EROFS_SB(sb);
0074 it.buf = __EROFS_BUF_INITIALIZER;
0075 it.blkaddr = erofs_blknr(iloc(sbi, vi->nid) + vi->inode_isize);
0076 it.ofs = erofs_blkoff(iloc(sbi, vi->nid) + vi->inode_isize);
0077
0078
0079 it.kaddr = erofs_read_metabuf(&it.buf, sb, it.blkaddr, EROFS_KMAP);
0080 if (IS_ERR(it.kaddr)) {
0081 ret = PTR_ERR(it.kaddr);
0082 goto out_unlock;
0083 }
0084
0085 ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
0086 vi->xattr_shared_count = ih->h_shared_count;
0087 vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
0088 sizeof(uint), GFP_KERNEL);
0089 if (!vi->xattr_shared_xattrs) {
0090 erofs_put_metabuf(&it.buf);
0091 ret = -ENOMEM;
0092 goto out_unlock;
0093 }
0094
0095
0096 it.ofs += sizeof(struct erofs_xattr_ibody_header);
0097
0098 for (i = 0; i < vi->xattr_shared_count; ++i) {
0099 if (it.ofs >= EROFS_BLKSIZ) {
0100
0101 DBG_BUGON(it.ofs != EROFS_BLKSIZ);
0102
0103 it.kaddr = erofs_read_metabuf(&it.buf, sb, ++it.blkaddr,
0104 EROFS_KMAP);
0105 if (IS_ERR(it.kaddr)) {
0106 kfree(vi->xattr_shared_xattrs);
0107 vi->xattr_shared_xattrs = NULL;
0108 ret = PTR_ERR(it.kaddr);
0109 goto out_unlock;
0110 }
0111 it.ofs = 0;
0112 }
0113 vi->xattr_shared_xattrs[i] =
0114 le32_to_cpu(*(__le32 *)(it.kaddr + it.ofs));
0115 it.ofs += sizeof(__le32);
0116 }
0117 erofs_put_metabuf(&it.buf);
0118
0119
0120 smp_mb();
0121 set_bit(EROFS_I_EA_INITED_BIT, &vi->flags);
0122
0123 out_unlock:
0124 clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags);
0125 return ret;
0126 }
0127
0128
0129
0130
0131
0132
0133
0134
0135 struct xattr_iter_handlers {
0136 int (*entry)(struct xattr_iter *_it, struct erofs_xattr_entry *entry);
0137 int (*name)(struct xattr_iter *_it, unsigned int processed, char *buf,
0138 unsigned int len);
0139 int (*alloc_buffer)(struct xattr_iter *_it, unsigned int value_sz);
0140 void (*value)(struct xattr_iter *_it, unsigned int processed, char *buf,
0141 unsigned int len);
0142 };
0143
0144 static inline int xattr_iter_fixup(struct xattr_iter *it)
0145 {
0146 if (it->ofs < EROFS_BLKSIZ)
0147 return 0;
0148
0149 it->blkaddr += erofs_blknr(it->ofs);
0150 it->kaddr = erofs_read_metabuf(&it->buf, it->sb, it->blkaddr,
0151 EROFS_KMAP_ATOMIC);
0152 if (IS_ERR(it->kaddr))
0153 return PTR_ERR(it->kaddr);
0154 it->ofs = erofs_blkoff(it->ofs);
0155 return 0;
0156 }
0157
0158 static int inline_xattr_iter_begin(struct xattr_iter *it,
0159 struct inode *inode)
0160 {
0161 struct erofs_inode *const vi = EROFS_I(inode);
0162 struct erofs_sb_info *const sbi = EROFS_SB(inode->i_sb);
0163 unsigned int xattr_header_sz, inline_xattr_ofs;
0164
0165 xattr_header_sz = inlinexattr_header_size(inode);
0166 if (xattr_header_sz >= vi->xattr_isize) {
0167 DBG_BUGON(xattr_header_sz > vi->xattr_isize);
0168 return -ENOATTR;
0169 }
0170
0171 inline_xattr_ofs = vi->inode_isize + xattr_header_sz;
0172
0173 it->blkaddr = erofs_blknr(iloc(sbi, vi->nid) + inline_xattr_ofs);
0174 it->ofs = erofs_blkoff(iloc(sbi, vi->nid) + inline_xattr_ofs);
0175
0176 it->kaddr = erofs_read_metabuf(&it->buf, inode->i_sb, it->blkaddr,
0177 EROFS_KMAP_ATOMIC);
0178 if (IS_ERR(it->kaddr))
0179 return PTR_ERR(it->kaddr);
0180 return vi->xattr_isize - xattr_header_sz;
0181 }
0182
0183
0184
0185
0186
0187 static int xattr_foreach(struct xattr_iter *it,
0188 const struct xattr_iter_handlers *op,
0189 unsigned int *tlimit)
0190 {
0191 struct erofs_xattr_entry entry;
0192 unsigned int value_sz, processed, slice;
0193 int err;
0194
0195
0196 err = xattr_iter_fixup(it);
0197 if (err)
0198 return err;
0199
0200
0201
0202
0203
0204
0205 entry = *(struct erofs_xattr_entry *)(it->kaddr + it->ofs);
0206 if (tlimit) {
0207 unsigned int entry_sz = erofs_xattr_entry_size(&entry);
0208
0209
0210 if (*tlimit < entry_sz) {
0211 DBG_BUGON(1);
0212 return -EFSCORRUPTED;
0213 }
0214 *tlimit -= entry_sz;
0215 }
0216
0217 it->ofs += sizeof(struct erofs_xattr_entry);
0218 value_sz = le16_to_cpu(entry.e_value_size);
0219
0220
0221 err = op->entry(it, &entry);
0222 if (err) {
0223 it->ofs += entry.e_name_len + value_sz;
0224 goto out;
0225 }
0226
0227
0228 processed = 0;
0229
0230 while (processed < entry.e_name_len) {
0231 if (it->ofs >= EROFS_BLKSIZ) {
0232 DBG_BUGON(it->ofs > EROFS_BLKSIZ);
0233
0234 err = xattr_iter_fixup(it);
0235 if (err)
0236 goto out;
0237 it->ofs = 0;
0238 }
0239
0240 slice = min_t(unsigned int, EROFS_BLKSIZ - it->ofs,
0241 entry.e_name_len - processed);
0242
0243
0244 err = op->name(it, processed, it->kaddr + it->ofs, slice);
0245 if (err) {
0246 it->ofs += entry.e_name_len - processed + value_sz;
0247 goto out;
0248 }
0249
0250 it->ofs += slice;
0251 processed += slice;
0252 }
0253
0254
0255 processed = 0;
0256
0257 if (op->alloc_buffer) {
0258 err = op->alloc_buffer(it, value_sz);
0259 if (err) {
0260 it->ofs += value_sz;
0261 goto out;
0262 }
0263 }
0264
0265 while (processed < value_sz) {
0266 if (it->ofs >= EROFS_BLKSIZ) {
0267 DBG_BUGON(it->ofs > EROFS_BLKSIZ);
0268
0269 err = xattr_iter_fixup(it);
0270 if (err)
0271 goto out;
0272 it->ofs = 0;
0273 }
0274
0275 slice = min_t(unsigned int, EROFS_BLKSIZ - it->ofs,
0276 value_sz - processed);
0277 op->value(it, processed, it->kaddr + it->ofs, slice);
0278 it->ofs += slice;
0279 processed += slice;
0280 }
0281
0282 out:
0283
0284 it->ofs = EROFS_XATTR_ALIGN(it->ofs);
0285 return err < 0 ? err : 0;
0286 }
0287
0288 struct getxattr_iter {
0289 struct xattr_iter it;
0290
0291 char *buffer;
0292 int buffer_size, index;
0293 struct qstr name;
0294 };
0295
0296 static int xattr_entrymatch(struct xattr_iter *_it,
0297 struct erofs_xattr_entry *entry)
0298 {
0299 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
0300
0301 return (it->index != entry->e_name_index ||
0302 it->name.len != entry->e_name_len) ? -ENOATTR : 0;
0303 }
0304
0305 static int xattr_namematch(struct xattr_iter *_it,
0306 unsigned int processed, char *buf, unsigned int len)
0307 {
0308 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
0309
0310 return memcmp(buf, it->name.name + processed, len) ? -ENOATTR : 0;
0311 }
0312
0313 static int xattr_checkbuffer(struct xattr_iter *_it,
0314 unsigned int value_sz)
0315 {
0316 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
0317 int err = it->buffer_size < value_sz ? -ERANGE : 0;
0318
0319 it->buffer_size = value_sz;
0320 return !it->buffer ? 1 : err;
0321 }
0322
0323 static void xattr_copyvalue(struct xattr_iter *_it,
0324 unsigned int processed,
0325 char *buf, unsigned int len)
0326 {
0327 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
0328
0329 memcpy(it->buffer + processed, buf, len);
0330 }
0331
0332 static const struct xattr_iter_handlers find_xattr_handlers = {
0333 .entry = xattr_entrymatch,
0334 .name = xattr_namematch,
0335 .alloc_buffer = xattr_checkbuffer,
0336 .value = xattr_copyvalue
0337 };
0338
0339 static int inline_getxattr(struct inode *inode, struct getxattr_iter *it)
0340 {
0341 int ret;
0342 unsigned int remaining;
0343
0344 ret = inline_xattr_iter_begin(&it->it, inode);
0345 if (ret < 0)
0346 return ret;
0347
0348 remaining = ret;
0349 while (remaining) {
0350 ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining);
0351 if (ret != -ENOATTR)
0352 break;
0353 }
0354 return ret ? ret : it->buffer_size;
0355 }
0356
0357 static int shared_getxattr(struct inode *inode, struct getxattr_iter *it)
0358 {
0359 struct erofs_inode *const vi = EROFS_I(inode);
0360 struct super_block *const sb = inode->i_sb;
0361 struct erofs_sb_info *const sbi = EROFS_SB(sb);
0362 unsigned int i;
0363 int ret = -ENOATTR;
0364
0365 for (i = 0; i < vi->xattr_shared_count; ++i) {
0366 erofs_blk_t blkaddr =
0367 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
0368
0369 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
0370 it->it.kaddr = erofs_read_metabuf(&it->it.buf, sb, blkaddr,
0371 EROFS_KMAP_ATOMIC);
0372 if (IS_ERR(it->it.kaddr))
0373 return PTR_ERR(it->it.kaddr);
0374 it->it.blkaddr = blkaddr;
0375
0376 ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL);
0377 if (ret != -ENOATTR)
0378 break;
0379 }
0380 return ret ? ret : it->buffer_size;
0381 }
0382
0383 static bool erofs_xattr_user_list(struct dentry *dentry)
0384 {
0385 return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER);
0386 }
0387
0388 static bool erofs_xattr_trusted_list(struct dentry *dentry)
0389 {
0390 return capable(CAP_SYS_ADMIN);
0391 }
0392
0393 int erofs_getxattr(struct inode *inode, int index,
0394 const char *name,
0395 void *buffer, size_t buffer_size)
0396 {
0397 int ret;
0398 struct getxattr_iter it;
0399
0400 if (!name)
0401 return -EINVAL;
0402
0403 ret = init_inode_xattrs(inode);
0404 if (ret)
0405 return ret;
0406
0407 it.index = index;
0408 it.name.len = strlen(name);
0409 if (it.name.len > EROFS_NAME_LEN)
0410 return -ERANGE;
0411
0412 it.it.buf = __EROFS_BUF_INITIALIZER;
0413 it.name.name = name;
0414
0415 it.buffer = buffer;
0416 it.buffer_size = buffer_size;
0417
0418 it.it.sb = inode->i_sb;
0419 ret = inline_getxattr(inode, &it);
0420 if (ret == -ENOATTR)
0421 ret = shared_getxattr(inode, &it);
0422 erofs_put_metabuf(&it.it.buf);
0423 return ret;
0424 }
0425
0426 static int erofs_xattr_generic_get(const struct xattr_handler *handler,
0427 struct dentry *unused, struct inode *inode,
0428 const char *name, void *buffer, size_t size)
0429 {
0430 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
0431
0432 switch (handler->flags) {
0433 case EROFS_XATTR_INDEX_USER:
0434 if (!test_opt(&sbi->opt, XATTR_USER))
0435 return -EOPNOTSUPP;
0436 break;
0437 case EROFS_XATTR_INDEX_TRUSTED:
0438 break;
0439 case EROFS_XATTR_INDEX_SECURITY:
0440 break;
0441 default:
0442 return -EINVAL;
0443 }
0444
0445 return erofs_getxattr(inode, handler->flags, name, buffer, size);
0446 }
0447
0448 const struct xattr_handler erofs_xattr_user_handler = {
0449 .prefix = XATTR_USER_PREFIX,
0450 .flags = EROFS_XATTR_INDEX_USER,
0451 .list = erofs_xattr_user_list,
0452 .get = erofs_xattr_generic_get,
0453 };
0454
0455 const struct xattr_handler erofs_xattr_trusted_handler = {
0456 .prefix = XATTR_TRUSTED_PREFIX,
0457 .flags = EROFS_XATTR_INDEX_TRUSTED,
0458 .list = erofs_xattr_trusted_list,
0459 .get = erofs_xattr_generic_get,
0460 };
0461
0462 #ifdef CONFIG_EROFS_FS_SECURITY
0463 const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
0464 .prefix = XATTR_SECURITY_PREFIX,
0465 .flags = EROFS_XATTR_INDEX_SECURITY,
0466 .get = erofs_xattr_generic_get,
0467 };
0468 #endif
0469
0470 const struct xattr_handler *erofs_xattr_handlers[] = {
0471 &erofs_xattr_user_handler,
0472 #ifdef CONFIG_EROFS_FS_POSIX_ACL
0473 &posix_acl_access_xattr_handler,
0474 &posix_acl_default_xattr_handler,
0475 #endif
0476 &erofs_xattr_trusted_handler,
0477 #ifdef CONFIG_EROFS_FS_SECURITY
0478 &erofs_xattr_security_handler,
0479 #endif
0480 NULL,
0481 };
0482
0483 struct listxattr_iter {
0484 struct xattr_iter it;
0485
0486 struct dentry *dentry;
0487 char *buffer;
0488 int buffer_size, buffer_ofs;
0489 };
0490
0491 static int xattr_entrylist(struct xattr_iter *_it,
0492 struct erofs_xattr_entry *entry)
0493 {
0494 struct listxattr_iter *it =
0495 container_of(_it, struct listxattr_iter, it);
0496 unsigned int prefix_len;
0497 const char *prefix;
0498
0499 const struct xattr_handler *h =
0500 erofs_xattr_handler(entry->e_name_index);
0501
0502 if (!h || (h->list && !h->list(it->dentry)))
0503 return 1;
0504
0505 prefix = xattr_prefix(h);
0506 prefix_len = strlen(prefix);
0507
0508 if (!it->buffer) {
0509 it->buffer_ofs += prefix_len + entry->e_name_len + 1;
0510 return 1;
0511 }
0512
0513 if (it->buffer_ofs + prefix_len
0514 + entry->e_name_len + 1 > it->buffer_size)
0515 return -ERANGE;
0516
0517 memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
0518 it->buffer_ofs += prefix_len;
0519 return 0;
0520 }
0521
0522 static int xattr_namelist(struct xattr_iter *_it,
0523 unsigned int processed, char *buf, unsigned int len)
0524 {
0525 struct listxattr_iter *it =
0526 container_of(_it, struct listxattr_iter, it);
0527
0528 memcpy(it->buffer + it->buffer_ofs, buf, len);
0529 it->buffer_ofs += len;
0530 return 0;
0531 }
0532
0533 static int xattr_skipvalue(struct xattr_iter *_it,
0534 unsigned int value_sz)
0535 {
0536 struct listxattr_iter *it =
0537 container_of(_it, struct listxattr_iter, it);
0538
0539 it->buffer[it->buffer_ofs++] = '\0';
0540 return 1;
0541 }
0542
0543 static const struct xattr_iter_handlers list_xattr_handlers = {
0544 .entry = xattr_entrylist,
0545 .name = xattr_namelist,
0546 .alloc_buffer = xattr_skipvalue,
0547 .value = NULL
0548 };
0549
0550 static int inline_listxattr(struct listxattr_iter *it)
0551 {
0552 int ret;
0553 unsigned int remaining;
0554
0555 ret = inline_xattr_iter_begin(&it->it, d_inode(it->dentry));
0556 if (ret < 0)
0557 return ret;
0558
0559 remaining = ret;
0560 while (remaining) {
0561 ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining);
0562 if (ret)
0563 break;
0564 }
0565 return ret ? ret : it->buffer_ofs;
0566 }
0567
0568 static int shared_listxattr(struct listxattr_iter *it)
0569 {
0570 struct inode *const inode = d_inode(it->dentry);
0571 struct erofs_inode *const vi = EROFS_I(inode);
0572 struct super_block *const sb = inode->i_sb;
0573 struct erofs_sb_info *const sbi = EROFS_SB(sb);
0574 unsigned int i;
0575 int ret = 0;
0576
0577 for (i = 0; i < vi->xattr_shared_count; ++i) {
0578 erofs_blk_t blkaddr =
0579 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
0580
0581 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
0582 it->it.kaddr = erofs_read_metabuf(&it->it.buf, sb, blkaddr,
0583 EROFS_KMAP_ATOMIC);
0584 if (IS_ERR(it->it.kaddr))
0585 return PTR_ERR(it->it.kaddr);
0586 it->it.blkaddr = blkaddr;
0587
0588 ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL);
0589 if (ret)
0590 break;
0591 }
0592 return ret ? ret : it->buffer_ofs;
0593 }
0594
0595 ssize_t erofs_listxattr(struct dentry *dentry,
0596 char *buffer, size_t buffer_size)
0597 {
0598 int ret;
0599 struct listxattr_iter it;
0600
0601 ret = init_inode_xattrs(d_inode(dentry));
0602 if (ret == -ENOATTR)
0603 return 0;
0604 if (ret)
0605 return ret;
0606
0607 it.it.buf = __EROFS_BUF_INITIALIZER;
0608 it.dentry = dentry;
0609 it.buffer = buffer;
0610 it.buffer_size = buffer_size;
0611 it.buffer_ofs = 0;
0612
0613 it.it.sb = dentry->d_sb;
0614
0615 ret = inline_listxattr(&it);
0616 if (ret >= 0 || ret == -ENOATTR)
0617 ret = shared_listxattr(&it);
0618 erofs_put_metabuf(&it.it.buf);
0619 return ret;
0620 }
0621
0622 #ifdef CONFIG_EROFS_FS_POSIX_ACL
0623 struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
0624 {
0625 struct posix_acl *acl;
0626 int prefix, rc;
0627 char *value = NULL;
0628
0629 if (rcu)
0630 return ERR_PTR(-ECHILD);
0631
0632 switch (type) {
0633 case ACL_TYPE_ACCESS:
0634 prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
0635 break;
0636 case ACL_TYPE_DEFAULT:
0637 prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
0638 break;
0639 default:
0640 return ERR_PTR(-EINVAL);
0641 }
0642
0643 rc = erofs_getxattr(inode, prefix, "", NULL, 0);
0644 if (rc > 0) {
0645 value = kmalloc(rc, GFP_KERNEL);
0646 if (!value)
0647 return ERR_PTR(-ENOMEM);
0648 rc = erofs_getxattr(inode, prefix, "", value, rc);
0649 }
0650
0651 if (rc == -ENOATTR)
0652 acl = NULL;
0653 else if (rc < 0)
0654 acl = ERR_PTR(rc);
0655 else
0656 acl = posix_acl_from_xattr(&init_user_ns, value, rc);
0657 kfree(value);
0658 return acl;
0659 }
0660 #endif