1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2017-2018 HUAWEI, Inc. 4 * https://www.huawei.com/ 5 * Copyright (C) 2021-2022, Alibaba Cloud 6 */ 7 #include <linux/security.h> 8 #include "xattr.h" 9 10 struct xattr_iter { 11 struct super_block *sb; 12 struct erofs_buf buf; 13 void *kaddr; 14 15 erofs_blk_t blkaddr; 16 unsigned int ofs; 17 }; 18 19 static int init_inode_xattrs(struct inode *inode) 20 { 21 struct erofs_inode *const vi = EROFS_I(inode); 22 struct xattr_iter it; 23 unsigned int i; 24 struct erofs_xattr_ibody_header *ih; 25 struct super_block *sb = inode->i_sb; 26 int ret = 0; 27 28 /* the most case is that xattrs of this inode are initialized. */ 29 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) { 30 /* 31 * paired with smp_mb() at the end of the function to ensure 32 * fields will only be observed after the bit is set. 33 */ 34 smp_mb(); 35 return 0; 36 } 37 38 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE)) 39 return -ERESTARTSYS; 40 41 /* someone has initialized xattrs for us? */ 42 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) 43 goto out_unlock; 44 45 /* 46 * bypass all xattr operations if ->xattr_isize is not greater than 47 * sizeof(struct erofs_xattr_ibody_header), in detail: 48 * 1) it is not enough to contain erofs_xattr_ibody_header then 49 * ->xattr_isize should be 0 (it means no xattr); 50 * 2) it is just to contain erofs_xattr_ibody_header, which is on-disk 51 * undefined right now (maybe use later with some new sb feature). 52 */ 53 if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) { 54 erofs_err(sb, 55 "xattr_isize %d of nid %llu is not supported yet", 56 vi->xattr_isize, vi->nid); 57 ret = -EOPNOTSUPP; 58 goto out_unlock; 59 } else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) { 60 if (vi->xattr_isize) { 61 erofs_err(sb, "bogus xattr ibody @ nid %llu", vi->nid); 62 DBG_BUGON(1); 63 ret = -EFSCORRUPTED; 64 goto out_unlock; /* xattr ondisk layout error */ 65 } 66 ret = -ENOATTR; 67 goto out_unlock; 68 } 69 70 it.buf = __EROFS_BUF_INITIALIZER; 71 it.blkaddr = erofs_blknr(erofs_iloc(inode) + vi->inode_isize); 72 it.ofs = erofs_blkoff(erofs_iloc(inode) + vi->inode_isize); 73 74 /* read in shared xattr array (non-atomic, see kmalloc below) */ 75 it.kaddr = erofs_read_metabuf(&it.buf, sb, it.blkaddr, EROFS_KMAP); 76 if (IS_ERR(it.kaddr)) { 77 ret = PTR_ERR(it.kaddr); 78 goto out_unlock; 79 } 80 81 ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs); 82 vi->xattr_shared_count = ih->h_shared_count; 83 vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count, 84 sizeof(uint), GFP_KERNEL); 85 if (!vi->xattr_shared_xattrs) { 86 erofs_put_metabuf(&it.buf); 87 ret = -ENOMEM; 88 goto out_unlock; 89 } 90 91 /* let's skip ibody header */ 92 it.ofs += sizeof(struct erofs_xattr_ibody_header); 93 94 for (i = 0; i < vi->xattr_shared_count; ++i) { 95 if (it.ofs >= EROFS_BLKSIZ) { 96 /* cannot be unaligned */ 97 DBG_BUGON(it.ofs != EROFS_BLKSIZ); 98 99 it.kaddr = erofs_read_metabuf(&it.buf, sb, ++it.blkaddr, 100 EROFS_KMAP); 101 if (IS_ERR(it.kaddr)) { 102 kfree(vi->xattr_shared_xattrs); 103 vi->xattr_shared_xattrs = NULL; 104 ret = PTR_ERR(it.kaddr); 105 goto out_unlock; 106 } 107 it.ofs = 0; 108 } 109 vi->xattr_shared_xattrs[i] = 110 le32_to_cpu(*(__le32 *)(it.kaddr + it.ofs)); 111 it.ofs += sizeof(__le32); 112 } 113 erofs_put_metabuf(&it.buf); 114 115 /* paired with smp_mb() at the beginning of the function. */ 116 smp_mb(); 117 set_bit(EROFS_I_EA_INITED_BIT, &vi->flags); 118 119 out_unlock: 120 clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags); 121 return ret; 122 } 123 124 /* 125 * the general idea for these return values is 126 * if 0 is returned, go on processing the current xattr; 127 * 1 (> 0) is returned, skip this round to process the next xattr; 128 * -err (< 0) is returned, an error (maybe ENOXATTR) occurred 129 * and need to be handled 130 */ 131 struct xattr_iter_handlers { 132 int (*entry)(struct xattr_iter *_it, struct erofs_xattr_entry *entry); 133 int (*name)(struct xattr_iter *_it, unsigned int processed, char *buf, 134 unsigned int len); 135 int (*alloc_buffer)(struct xattr_iter *_it, unsigned int value_sz); 136 void (*value)(struct xattr_iter *_it, unsigned int processed, char *buf, 137 unsigned int len); 138 }; 139 140 static inline int xattr_iter_fixup(struct xattr_iter *it) 141 { 142 if (it->ofs < EROFS_BLKSIZ) 143 return 0; 144 145 it->blkaddr += erofs_blknr(it->ofs); 146 it->kaddr = erofs_read_metabuf(&it->buf, it->sb, it->blkaddr, 147 EROFS_KMAP); 148 if (IS_ERR(it->kaddr)) 149 return PTR_ERR(it->kaddr); 150 it->ofs = erofs_blkoff(it->ofs); 151 return 0; 152 } 153 154 static int inline_xattr_iter_begin(struct xattr_iter *it, 155 struct inode *inode) 156 { 157 struct erofs_inode *const vi = EROFS_I(inode); 158 unsigned int xattr_header_sz, inline_xattr_ofs; 159 160 xattr_header_sz = inlinexattr_header_size(inode); 161 if (xattr_header_sz >= vi->xattr_isize) { 162 DBG_BUGON(xattr_header_sz > vi->xattr_isize); 163 return -ENOATTR; 164 } 165 166 inline_xattr_ofs = vi->inode_isize + xattr_header_sz; 167 168 it->blkaddr = erofs_blknr(erofs_iloc(inode) + inline_xattr_ofs); 169 it->ofs = erofs_blkoff(erofs_iloc(inode) + inline_xattr_ofs); 170 it->kaddr = erofs_read_metabuf(&it->buf, inode->i_sb, it->blkaddr, 171 EROFS_KMAP); 172 if (IS_ERR(it->kaddr)) 173 return PTR_ERR(it->kaddr); 174 return vi->xattr_isize - xattr_header_sz; 175 } 176 177 /* 178 * Regardless of success or failure, `xattr_foreach' will end up with 179 * `ofs' pointing to the next xattr item rather than an arbitrary position. 180 */ 181 static int xattr_foreach(struct xattr_iter *it, 182 const struct xattr_iter_handlers *op, 183 unsigned int *tlimit) 184 { 185 struct erofs_xattr_entry entry; 186 unsigned int value_sz, processed, slice; 187 int err; 188 189 /* 0. fixup blkaddr, ofs, ipage */ 190 err = xattr_iter_fixup(it); 191 if (err) 192 return err; 193 194 /* 195 * 1. read xattr entry to the memory, 196 * since we do EROFS_XATTR_ALIGN 197 * therefore entry should be in the page 198 */ 199 entry = *(struct erofs_xattr_entry *)(it->kaddr + it->ofs); 200 if (tlimit) { 201 unsigned int entry_sz = erofs_xattr_entry_size(&entry); 202 203 /* xattr on-disk corruption: xattr entry beyond xattr_isize */ 204 if (*tlimit < entry_sz) { 205 DBG_BUGON(1); 206 return -EFSCORRUPTED; 207 } 208 *tlimit -= entry_sz; 209 } 210 211 it->ofs += sizeof(struct erofs_xattr_entry); 212 value_sz = le16_to_cpu(entry.e_value_size); 213 214 /* handle entry */ 215 err = op->entry(it, &entry); 216 if (err) { 217 it->ofs += entry.e_name_len + value_sz; 218 goto out; 219 } 220 221 /* 2. handle xattr name (ofs will finally be at the end of name) */ 222 processed = 0; 223 224 while (processed < entry.e_name_len) { 225 if (it->ofs >= EROFS_BLKSIZ) { 226 DBG_BUGON(it->ofs > EROFS_BLKSIZ); 227 228 err = xattr_iter_fixup(it); 229 if (err) 230 goto out; 231 it->ofs = 0; 232 } 233 234 slice = min_t(unsigned int, EROFS_BLKSIZ - it->ofs, 235 entry.e_name_len - processed); 236 237 /* handle name */ 238 err = op->name(it, processed, it->kaddr + it->ofs, slice); 239 if (err) { 240 it->ofs += entry.e_name_len - processed + value_sz; 241 goto out; 242 } 243 244 it->ofs += slice; 245 processed += slice; 246 } 247 248 /* 3. handle xattr value */ 249 processed = 0; 250 251 if (op->alloc_buffer) { 252 err = op->alloc_buffer(it, value_sz); 253 if (err) { 254 it->ofs += value_sz; 255 goto out; 256 } 257 } 258 259 while (processed < value_sz) { 260 if (it->ofs >= EROFS_BLKSIZ) { 261 DBG_BUGON(it->ofs > EROFS_BLKSIZ); 262 263 err = xattr_iter_fixup(it); 264 if (err) 265 goto out; 266 it->ofs = 0; 267 } 268 269 slice = min_t(unsigned int, EROFS_BLKSIZ - it->ofs, 270 value_sz - processed); 271 op->value(it, processed, it->kaddr + it->ofs, slice); 272 it->ofs += slice; 273 processed += slice; 274 } 275 276 out: 277 /* xattrs should be 4-byte aligned (on-disk constraint) */ 278 it->ofs = EROFS_XATTR_ALIGN(it->ofs); 279 return err < 0 ? err : 0; 280 } 281 282 struct getxattr_iter { 283 struct xattr_iter it; 284 285 char *buffer; 286 int buffer_size, index; 287 struct qstr name; 288 }; 289 290 static int xattr_entrymatch(struct xattr_iter *_it, 291 struct erofs_xattr_entry *entry) 292 { 293 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it); 294 295 return (it->index != entry->e_name_index || 296 it->name.len != entry->e_name_len) ? -ENOATTR : 0; 297 } 298 299 static int xattr_namematch(struct xattr_iter *_it, 300 unsigned int processed, char *buf, unsigned int len) 301 { 302 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it); 303 304 return memcmp(buf, it->name.name + processed, len) ? -ENOATTR : 0; 305 } 306 307 static int xattr_checkbuffer(struct xattr_iter *_it, 308 unsigned int value_sz) 309 { 310 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it); 311 int err = it->buffer_size < value_sz ? -ERANGE : 0; 312 313 it->buffer_size = value_sz; 314 return !it->buffer ? 1 : err; 315 } 316 317 static void xattr_copyvalue(struct xattr_iter *_it, 318 unsigned int processed, 319 char *buf, unsigned int len) 320 { 321 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it); 322 323 memcpy(it->buffer + processed, buf, len); 324 } 325 326 static const struct xattr_iter_handlers find_xattr_handlers = { 327 .entry = xattr_entrymatch, 328 .name = xattr_namematch, 329 .alloc_buffer = xattr_checkbuffer, 330 .value = xattr_copyvalue 331 }; 332 333 static int inline_getxattr(struct inode *inode, struct getxattr_iter *it) 334 { 335 int ret; 336 unsigned int remaining; 337 338 ret = inline_xattr_iter_begin(&it->it, inode); 339 if (ret < 0) 340 return ret; 341 342 remaining = ret; 343 while (remaining) { 344 ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining); 345 if (ret != -ENOATTR) 346 break; 347 } 348 return ret ? ret : it->buffer_size; 349 } 350 351 static int shared_getxattr(struct inode *inode, struct getxattr_iter *it) 352 { 353 struct erofs_inode *const vi = EROFS_I(inode); 354 struct super_block *const sb = inode->i_sb; 355 struct erofs_sb_info *const sbi = EROFS_SB(sb); 356 unsigned int i; 357 int ret = -ENOATTR; 358 359 for (i = 0; i < vi->xattr_shared_count; ++i) { 360 erofs_blk_t blkaddr = 361 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]); 362 363 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]); 364 it->it.kaddr = erofs_read_metabuf(&it->it.buf, sb, blkaddr, 365 EROFS_KMAP); 366 if (IS_ERR(it->it.kaddr)) 367 return PTR_ERR(it->it.kaddr); 368 it->it.blkaddr = blkaddr; 369 370 ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL); 371 if (ret != -ENOATTR) 372 break; 373 } 374 return ret ? ret : it->buffer_size; 375 } 376 377 static bool erofs_xattr_user_list(struct dentry *dentry) 378 { 379 return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER); 380 } 381 382 static bool erofs_xattr_trusted_list(struct dentry *dentry) 383 { 384 return capable(CAP_SYS_ADMIN); 385 } 386 387 int erofs_getxattr(struct inode *inode, int index, 388 const char *name, 389 void *buffer, size_t buffer_size) 390 { 391 int ret; 392 struct getxattr_iter it; 393 394 if (!name) 395 return -EINVAL; 396 397 ret = init_inode_xattrs(inode); 398 if (ret) 399 return ret; 400 401 it.index = index; 402 it.name.len = strlen(name); 403 if (it.name.len > EROFS_NAME_LEN) 404 return -ERANGE; 405 406 it.it.buf = __EROFS_BUF_INITIALIZER; 407 it.name.name = name; 408 409 it.buffer = buffer; 410 it.buffer_size = buffer_size; 411 412 it.it.sb = inode->i_sb; 413 ret = inline_getxattr(inode, &it); 414 if (ret == -ENOATTR) 415 ret = shared_getxattr(inode, &it); 416 erofs_put_metabuf(&it.it.buf); 417 return ret; 418 } 419 420 static int erofs_xattr_generic_get(const struct xattr_handler *handler, 421 struct dentry *unused, struct inode *inode, 422 const char *name, void *buffer, size_t size) 423 { 424 struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 425 426 switch (handler->flags) { 427 case EROFS_XATTR_INDEX_USER: 428 if (!test_opt(&sbi->opt, XATTR_USER)) 429 return -EOPNOTSUPP; 430 break; 431 case EROFS_XATTR_INDEX_TRUSTED: 432 break; 433 case EROFS_XATTR_INDEX_SECURITY: 434 break; 435 default: 436 return -EINVAL; 437 } 438 439 return erofs_getxattr(inode, handler->flags, name, buffer, size); 440 } 441 442 const struct xattr_handler erofs_xattr_user_handler = { 443 .prefix = XATTR_USER_PREFIX, 444 .flags = EROFS_XATTR_INDEX_USER, 445 .list = erofs_xattr_user_list, 446 .get = erofs_xattr_generic_get, 447 }; 448 449 const struct xattr_handler erofs_xattr_trusted_handler = { 450 .prefix = XATTR_TRUSTED_PREFIX, 451 .flags = EROFS_XATTR_INDEX_TRUSTED, 452 .list = erofs_xattr_trusted_list, 453 .get = erofs_xattr_generic_get, 454 }; 455 456 #ifdef CONFIG_EROFS_FS_SECURITY 457 const struct xattr_handler __maybe_unused erofs_xattr_security_handler = { 458 .prefix = XATTR_SECURITY_PREFIX, 459 .flags = EROFS_XATTR_INDEX_SECURITY, 460 .get = erofs_xattr_generic_get, 461 }; 462 #endif 463 464 const struct xattr_handler *erofs_xattr_handlers[] = { 465 &erofs_xattr_user_handler, 466 &erofs_xattr_trusted_handler, 467 #ifdef CONFIG_EROFS_FS_SECURITY 468 &erofs_xattr_security_handler, 469 #endif 470 NULL, 471 }; 472 473 struct listxattr_iter { 474 struct xattr_iter it; 475 476 struct dentry *dentry; 477 char *buffer; 478 int buffer_size, buffer_ofs; 479 }; 480 481 static int xattr_entrylist(struct xattr_iter *_it, 482 struct erofs_xattr_entry *entry) 483 { 484 struct listxattr_iter *it = 485 container_of(_it, struct listxattr_iter, it); 486 unsigned int prefix_len; 487 const char *prefix; 488 489 prefix = erofs_xattr_prefix(entry->e_name_index, it->dentry); 490 if (!prefix) 491 return 1; 492 prefix_len = strlen(prefix); 493 494 if (!it->buffer) { 495 it->buffer_ofs += prefix_len + entry->e_name_len + 1; 496 return 1; 497 } 498 499 if (it->buffer_ofs + prefix_len 500 + entry->e_name_len + 1 > it->buffer_size) 501 return -ERANGE; 502 503 memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len); 504 it->buffer_ofs += prefix_len; 505 return 0; 506 } 507 508 static int xattr_namelist(struct xattr_iter *_it, 509 unsigned int processed, char *buf, unsigned int len) 510 { 511 struct listxattr_iter *it = 512 container_of(_it, struct listxattr_iter, it); 513 514 memcpy(it->buffer + it->buffer_ofs, buf, len); 515 it->buffer_ofs += len; 516 return 0; 517 } 518 519 static int xattr_skipvalue(struct xattr_iter *_it, 520 unsigned int value_sz) 521 { 522 struct listxattr_iter *it = 523 container_of(_it, struct listxattr_iter, it); 524 525 it->buffer[it->buffer_ofs++] = '\0'; 526 return 1; 527 } 528 529 static const struct xattr_iter_handlers list_xattr_handlers = { 530 .entry = xattr_entrylist, 531 .name = xattr_namelist, 532 .alloc_buffer = xattr_skipvalue, 533 .value = NULL 534 }; 535 536 static int inline_listxattr(struct listxattr_iter *it) 537 { 538 int ret; 539 unsigned int remaining; 540 541 ret = inline_xattr_iter_begin(&it->it, d_inode(it->dentry)); 542 if (ret < 0) 543 return ret; 544 545 remaining = ret; 546 while (remaining) { 547 ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining); 548 if (ret) 549 break; 550 } 551 return ret ? ret : it->buffer_ofs; 552 } 553 554 static int shared_listxattr(struct listxattr_iter *it) 555 { 556 struct inode *const inode = d_inode(it->dentry); 557 struct erofs_inode *const vi = EROFS_I(inode); 558 struct super_block *const sb = inode->i_sb; 559 struct erofs_sb_info *const sbi = EROFS_SB(sb); 560 unsigned int i; 561 int ret = 0; 562 563 for (i = 0; i < vi->xattr_shared_count; ++i) { 564 erofs_blk_t blkaddr = 565 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]); 566 567 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]); 568 it->it.kaddr = erofs_read_metabuf(&it->it.buf, sb, blkaddr, 569 EROFS_KMAP); 570 if (IS_ERR(it->it.kaddr)) 571 return PTR_ERR(it->it.kaddr); 572 it->it.blkaddr = blkaddr; 573 574 ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL); 575 if (ret) 576 break; 577 } 578 return ret ? ret : it->buffer_ofs; 579 } 580 581 ssize_t erofs_listxattr(struct dentry *dentry, 582 char *buffer, size_t buffer_size) 583 { 584 int ret; 585 struct listxattr_iter it; 586 587 ret = init_inode_xattrs(d_inode(dentry)); 588 if (ret == -ENOATTR) 589 return 0; 590 if (ret) 591 return ret; 592 593 it.it.buf = __EROFS_BUF_INITIALIZER; 594 it.dentry = dentry; 595 it.buffer = buffer; 596 it.buffer_size = buffer_size; 597 it.buffer_ofs = 0; 598 599 it.it.sb = dentry->d_sb; 600 601 ret = inline_listxattr(&it); 602 if (ret >= 0 || ret == -ENOATTR) 603 ret = shared_listxattr(&it); 604 erofs_put_metabuf(&it.it.buf); 605 return ret; 606 } 607 608 #ifdef CONFIG_EROFS_FS_POSIX_ACL 609 struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu) 610 { 611 struct posix_acl *acl; 612 int prefix, rc; 613 char *value = NULL; 614 615 if (rcu) 616 return ERR_PTR(-ECHILD); 617 618 switch (type) { 619 case ACL_TYPE_ACCESS: 620 prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS; 621 break; 622 case ACL_TYPE_DEFAULT: 623 prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT; 624 break; 625 default: 626 return ERR_PTR(-EINVAL); 627 } 628 629 rc = erofs_getxattr(inode, prefix, "", NULL, 0); 630 if (rc > 0) { 631 value = kmalloc(rc, GFP_KERNEL); 632 if (!value) 633 return ERR_PTR(-ENOMEM); 634 rc = erofs_getxattr(inode, prefix, "", value, rc); 635 } 636 637 if (rc == -ENOATTR) 638 acl = NULL; 639 else if (rc < 0) 640 acl = ERR_PTR(rc); 641 else 642 acl = posix_acl_from_xattr(&init_user_ns, value, rc); 643 kfree(value); 644 return acl; 645 } 646 #endif 647