1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * 4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved. 5 * 6 */ 7 8 #include <linux/fs.h> 9 #include <linux/posix_acl.h> 10 #include <linux/posix_acl_xattr.h> 11 #include <linux/xattr.h> 12 13 #include "debug.h" 14 #include "ntfs.h" 15 #include "ntfs_fs.h" 16 17 // clang-format off 18 #define SYSTEM_DOS_ATTRIB "system.dos_attrib" 19 #define SYSTEM_NTFS_ATTRIB "system.ntfs_attrib" 20 #define SYSTEM_NTFS_ATTRIB_BE "system.ntfs_attrib_be" 21 #define SYSTEM_NTFS_SECURITY "system.ntfs_security" 22 // clang-format on 23 24 static inline size_t unpacked_ea_size(const struct EA_FULL *ea) 25 { 26 return ea->size ? le32_to_cpu(ea->size) : 27 ALIGN(struct_size(ea, name, 28 1 + ea->name_len + 29 le16_to_cpu(ea->elength)), 30 4); 31 } 32 33 static inline size_t packed_ea_size(const struct EA_FULL *ea) 34 { 35 return struct_size(ea, name, 36 1 + ea->name_len + le16_to_cpu(ea->elength)) - 37 offsetof(struct EA_FULL, flags); 38 } 39 40 /* 41 * find_ea 42 * 43 * Assume there is at least one xattr in the list. 44 */ 45 static inline bool find_ea(const struct EA_FULL *ea_all, u32 bytes, 46 const char *name, u8 name_len, u32 *off, u32 *ea_sz) 47 { 48 u32 ea_size; 49 50 *off = 0; 51 if (!ea_all) 52 return false; 53 54 for (; *off < bytes; *off += ea_size) { 55 const struct EA_FULL *ea = Add2Ptr(ea_all, *off); 56 ea_size = unpacked_ea_size(ea); 57 if (ea->name_len == name_len && 58 !memcmp(ea->name, name, name_len)) { 59 if (ea_sz) 60 *ea_sz = ea_size; 61 return true; 62 } 63 } 64 65 return false; 66 } 67 68 /* 69 * ntfs_read_ea - Read all extended attributes. 70 * @ea: New allocated memory. 71 * @info: Pointer into resident data. 72 */ 73 static int ntfs_read_ea(struct ntfs_inode *ni, struct EA_FULL **ea, 74 size_t add_bytes, const struct EA_INFO **info) 75 { 76 int err = -EINVAL; 77 struct ntfs_sb_info *sbi = ni->mi.sbi; 78 struct ATTR_LIST_ENTRY *le = NULL; 79 struct ATTRIB *attr_info, *attr_ea; 80 void *ea_p; 81 u32 size, off, ea_size; 82 83 static_assert(le32_to_cpu(ATTR_EA_INFO) < le32_to_cpu(ATTR_EA)); 84 85 *ea = NULL; 86 *info = NULL; 87 88 attr_info = 89 ni_find_attr(ni, NULL, &le, ATTR_EA_INFO, NULL, 0, NULL, NULL); 90 attr_ea = 91 ni_find_attr(ni, attr_info, &le, ATTR_EA, NULL, 0, NULL, NULL); 92 93 if (!attr_ea || !attr_info) 94 return 0; 95 96 *info = resident_data_ex(attr_info, sizeof(struct EA_INFO)); 97 if (!*info) 98 goto out; 99 100 /* Check Ea limit. */ 101 size = le32_to_cpu((*info)->size); 102 if (size > sbi->ea_max_size) { 103 err = -EFBIG; 104 goto out; 105 } 106 107 if (attr_size(attr_ea) > sbi->ea_max_size) { 108 err = -EFBIG; 109 goto out; 110 } 111 112 if (!size) { 113 /* EA info persists, but xattr is empty. Looks like EA problem. */ 114 goto out; 115 } 116 117 /* Allocate memory for packed Ea. */ 118 ea_p = kmalloc(size_add(size, add_bytes), GFP_NOFS); 119 if (!ea_p) 120 return -ENOMEM; 121 122 if (attr_ea->non_res) { 123 struct runs_tree run; 124 125 run_init(&run); 126 127 err = attr_load_runs_range(ni, ATTR_EA, NULL, 0, &run, 0, size); 128 if (!err) 129 err = ntfs_read_run_nb(sbi, &run, 0, ea_p, size, NULL); 130 run_close(&run); 131 132 if (err) 133 goto out1; 134 } else { 135 void *p = resident_data_ex(attr_ea, size); 136 137 if (!p) 138 goto out1; 139 memcpy(ea_p, p, size); 140 } 141 142 memset(Add2Ptr(ea_p, size), 0, add_bytes); 143 144 err = -EINVAL; 145 /* Check all attributes for consistency. */ 146 for (off = 0; off < size; off += ea_size) { 147 const struct EA_FULL *ef = Add2Ptr(ea_p, off); 148 u32 bytes = size - off; 149 size_t need; 150 151 /* Check if we can use field ea->size. */ 152 if (bytes < sizeof(ef->size)) 153 goto out1; 154 155 /* Check if we can use fields ef->name_len and ef->elength. */ 156 if (bytes < offsetof(struct EA_FULL, name)) 157 goto out1; 158 159 /* Size needed to hold this record's name and value. */ 160 need = struct_size(ef, name, 161 1 + ef->name_len + le16_to_cpu(ef->elength)); 162 163 if (ef->size) { 164 ea_size = le32_to_cpu(ef->size); 165 /* ef->size must fit the list and cover the record. */ 166 if (ea_size > bytes || ea_size < need) 167 goto out1; 168 continue; 169 } 170 171 ea_size = ALIGN(need, 4); 172 if (ea_size > bytes) 173 goto out1; 174 } 175 176 *ea = ea_p; 177 return 0; 178 179 out1: 180 kfree(ea_p); 181 out: 182 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); 183 return err; 184 } 185 186 /* 187 * ntfs_list_ea 188 * 189 * Copy a list of xattrs names into the buffer 190 * provided, or compute the buffer size required. 191 * 192 * Return: 193 * * Number of bytes used / required on 194 * * -ERRNO - on failure 195 */ 196 static ssize_t ntfs_list_ea(struct ntfs_inode *ni, char *buffer, 197 size_t bytes_per_buffer) 198 { 199 const struct EA_INFO *info; 200 struct EA_FULL *ea_all = NULL; 201 u32 off, size; 202 int err; 203 size_t ret; 204 205 err = ntfs_read_ea(ni, &ea_all, 0, &info); 206 if (err) 207 return err; 208 209 if (!info || !ea_all) 210 return 0; 211 212 size = le32_to_cpu(info->size); 213 214 /* Enumerate all xattrs. */ 215 ret = 0; 216 off = 0; 217 while (off + sizeof(struct EA_FULL) < size) { 218 const struct EA_FULL *ea = Add2Ptr(ea_all, off); 219 int ea_size = unpacked_ea_size(ea); 220 u8 name_len = ea->name_len; 221 222 if (!name_len) 223 break; 224 225 if (name_len > ea_size) { 226 ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_ERROR); 227 err = -EINVAL; /* corrupted fs. */ 228 break; 229 } 230 231 if (buffer) { 232 /* Check if we can use field ea->name */ 233 if (off + ea_size > size) 234 break; 235 236 if (ret + name_len + 1 > bytes_per_buffer) { 237 err = -ERANGE; 238 goto out; 239 } 240 241 memcpy(buffer + ret, ea->name, name_len); 242 buffer[ret + name_len] = 0; 243 } 244 245 ret += name_len + 1; 246 off += ea_size; 247 } 248 249 out: 250 kfree(ea_all); 251 return err ? err : ret; 252 } 253 254 static int ntfs_get_ea(struct inode *inode, const char *name, size_t name_len, 255 void *buffer, size_t size, size_t *required) 256 { 257 struct ntfs_inode *ni = ntfs_i(inode); 258 const struct EA_INFO *info; 259 struct EA_FULL *ea_all = NULL; 260 const struct EA_FULL *ea; 261 u32 off, len; 262 int err; 263 264 if (!(ni->ni_flags & NI_FLAG_EA)) 265 return -ENODATA; 266 267 if (!required) 268 ni_lock(ni); 269 270 len = 0; 271 272 if (name_len > 255) { 273 err = -ENAMETOOLONG; 274 goto out; 275 } 276 277 err = ntfs_read_ea(ni, &ea_all, 0, &info); 278 if (err) 279 goto out; 280 281 if (!info) 282 goto out; 283 284 /* Enumerate all xattrs. */ 285 if (!find_ea(ea_all, le32_to_cpu(info->size), name, name_len, &off, 286 NULL)) { 287 err = -ENODATA; 288 goto out; 289 } 290 ea = Add2Ptr(ea_all, off); 291 292 len = le16_to_cpu(ea->elength); 293 if (!buffer) { 294 err = 0; 295 goto out; 296 } 297 298 if (len > size) { 299 err = -ERANGE; 300 if (required) 301 *required = len; 302 goto out; 303 } 304 305 memcpy(buffer, ea->name + ea->name_len + 1, len); 306 err = 0; 307 308 out: 309 kfree(ea_all); 310 if (!required) 311 ni_unlock(ni); 312 313 return err ? err : len; 314 } 315 316 static noinline int ntfs_set_ea(struct inode *inode, const char *name, 317 size_t name_len, const void *value, 318 size_t val_size, int flags, bool locked, 319 __le32 *ea_size) 320 { 321 struct ntfs_inode *ni = ntfs_i(inode); 322 struct ntfs_sb_info *sbi = ni->mi.sbi; 323 int err; 324 struct EA_INFO ea_info; 325 const struct EA_INFO *info; 326 struct EA_FULL *new_ea; 327 struct EA_FULL *ea_all = NULL; 328 size_t add, new_pack; 329 u32 off, size, ea_sz; 330 __le16 size_pack; 331 struct ATTRIB *attr; 332 struct ATTR_LIST_ENTRY *le; 333 struct mft_inode *mi; 334 struct runs_tree ea_run; 335 u64 new_sz; 336 void *p; 337 338 if (!locked) 339 ni_lock(ni); 340 341 run_init(&ea_run); 342 343 if (name_len > 255) { 344 err = -ENAMETOOLONG; 345 goto out; 346 } 347 348 add = ALIGN(struct_size(ea_all, name, 1 + name_len + val_size), 4); 349 350 err = ntfs_read_ea(ni, &ea_all, add, &info); 351 if (err) 352 goto out; 353 354 if (!info) { 355 memset(&ea_info, 0, sizeof(ea_info)); 356 size = 0; 357 size_pack = 0; 358 } else { 359 memcpy(&ea_info, info, sizeof(ea_info)); 360 size = le32_to_cpu(ea_info.size); 361 size_pack = ea_info.size_pack; 362 } 363 364 if (info && find_ea(ea_all, size, name, name_len, &off, &ea_sz)) { 365 struct EA_FULL *ea; 366 367 if (flags & XATTR_CREATE) { 368 err = -EEXIST; 369 goto out; 370 } 371 372 ea = Add2Ptr(ea_all, off); 373 374 /* 375 * Check simple case when we try to insert xattr with the same value 376 * e.g. ntfs_save_wsl_perm 377 */ 378 if (val_size && le16_to_cpu(ea->elength) == val_size && 379 !memcmp(ea->name + ea->name_len + 1, value, val_size)) { 380 /* xattr already contains the required value. */ 381 goto out; 382 } 383 384 /* Remove current xattr. */ 385 if (ea->flags & FILE_NEED_EA) 386 le16_add_cpu(&ea_info.count, -1); 387 388 le16_add_cpu(&ea_info.size_pack, 0 - packed_ea_size(ea)); 389 390 memmove(ea, Add2Ptr(ea, ea_sz), size - off - ea_sz); 391 392 size -= ea_sz; 393 memset(Add2Ptr(ea_all, size), 0, ea_sz); 394 395 ea_info.size = cpu_to_le32(size); 396 397 if ((flags & XATTR_REPLACE) && !val_size) { 398 /* Remove xattr. */ 399 goto update_ea; 400 } 401 } else { 402 if (flags & XATTR_REPLACE) { 403 err = -ENODATA; 404 goto out; 405 } 406 407 if (!ea_all) { 408 ea_all = kzalloc(add, GFP_NOFS); 409 if (!ea_all) { 410 err = -ENOMEM; 411 goto out; 412 } 413 } 414 } 415 416 /* Append new xattr. */ 417 new_ea = Add2Ptr(ea_all, size); 418 new_ea->size = cpu_to_le32(add); 419 new_ea->flags = 0; 420 new_ea->name_len = name_len; 421 new_ea->elength = cpu_to_le16(val_size); 422 memcpy(new_ea->name, name, name_len); 423 new_ea->name[name_len] = 0; 424 memcpy(new_ea->name + name_len + 1, value, val_size); 425 new_pack = le16_to_cpu(ea_info.size_pack) + packed_ea_size(new_ea); 426 ea_info.size_pack = cpu_to_le16(new_pack); 427 /* New size of ATTR_EA. */ 428 size += add; 429 ea_info.size = cpu_to_le32(size); 430 431 /* 432 * 1. Check ea_info.size_pack for overflow. 433 * 2. New attribute size must fit value from $AttrDef 434 */ 435 if (new_pack > 0xffff || size > sbi->ea_max_size) { 436 ntfs_inode_warn( 437 inode, 438 "The size of extended attributes must not exceed 64KiB"); 439 err = -EFBIG; // -EINVAL? 440 goto out; 441 } 442 443 update_ea: 444 445 if (!info) { 446 /* Create xattr. */ 447 if (!size) { 448 err = 0; 449 goto out; 450 } 451 452 err = ni_insert_resident(ni, sizeof(struct EA_INFO), 453 ATTR_EA_INFO, NULL, 0, NULL, NULL, 454 NULL); 455 if (err) 456 goto out; 457 458 err = ni_insert_resident(ni, 0, ATTR_EA, NULL, 0, NULL, NULL, 459 NULL); 460 if (err) 461 goto out; 462 } 463 464 new_sz = size; 465 err = attr_set_size(ni, ATTR_EA, NULL, 0, &ea_run, new_sz, &new_sz, 466 false); 467 if (err) 468 goto out; 469 470 le = NULL; 471 attr = ni_find_attr(ni, NULL, &le, ATTR_EA_INFO, NULL, 0, NULL, &mi); 472 if (!attr) { 473 err = -EINVAL; 474 goto out; 475 } 476 477 if (!size) { 478 /* Delete xattr, ATTR_EA_INFO */ 479 ni_remove_attr_le(ni, attr, mi, le); 480 } else { 481 p = resident_data_ex(attr, sizeof(struct EA_INFO)); 482 if (!p) { 483 err = -EINVAL; 484 goto out; 485 } 486 memcpy(p, &ea_info, sizeof(struct EA_INFO)); 487 mi->dirty = true; 488 } 489 490 le = NULL; 491 attr = ni_find_attr(ni, NULL, &le, ATTR_EA, NULL, 0, NULL, &mi); 492 if (!attr) { 493 err = -EINVAL; 494 goto out; 495 } 496 497 if (!size) { 498 /* Delete xattr, ATTR_EA */ 499 ni_remove_attr_le(ni, attr, mi, le); 500 } else if (attr->non_res) { 501 err = attr_load_runs_range(ni, ATTR_EA, NULL, 0, &ea_run, 0, 502 size); 503 if (err) 504 goto out; 505 506 err = ntfs_sb_write_run(sbi, &ea_run, 0, ea_all, size, 0); 507 if (err) 508 goto out; 509 } else { 510 p = resident_data_ex(attr, size); 511 if (!p) { 512 err = -EINVAL; 513 goto out; 514 } 515 memcpy(p, ea_all, size); 516 mi->dirty = true; 517 } 518 519 /* Check if we delete the last xattr. */ 520 if (size) 521 ni->ni_flags |= NI_FLAG_EA; 522 else 523 ni->ni_flags &= ~NI_FLAG_EA; 524 525 if (ea_info.size_pack != size_pack) 526 ni->ni_flags |= NI_FLAG_UPDATE_PARENT; 527 if (ea_size) 528 *ea_size = ea_info.size; 529 mark_inode_dirty(&ni->vfs_inode); 530 531 out: 532 if (!locked) 533 ni_unlock(ni); 534 535 run_close(&ea_run); 536 kfree(ea_all); 537 538 return err; 539 } 540 541 #ifdef CONFIG_NTFS3_FS_POSIX_ACL 542 543 /* 544 * ntfs_get_acl - inode_operations::get_acl 545 */ 546 struct posix_acl *ntfs_get_acl(const struct mnt_idmap *idmap, struct dentry *dentry, 547 int type) 548 { 549 struct inode *inode = d_inode(dentry); 550 struct ntfs_inode *ni = ntfs_i(inode); 551 const char *name; 552 size_t name_len; 553 struct posix_acl *acl; 554 size_t req; 555 int err; 556 void *buf; 557 558 /* Avoid any operation if inode is bad. */ 559 if (unlikely(is_bad_ni(ni))) 560 return ERR_PTR(-EINVAL); 561 562 buf = kmalloc(PATH_MAX, GFP_KERNEL); 563 if (!buf) 564 return ERR_PTR(-ENOMEM); 565 566 /* Possible values of 'type' was already checked above. */ 567 if (type == ACL_TYPE_ACCESS) { 568 name = XATTR_NAME_POSIX_ACL_ACCESS; 569 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1; 570 } else { 571 name = XATTR_NAME_POSIX_ACL_DEFAULT; 572 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1; 573 } 574 575 ni_lock(ni); 576 577 err = ntfs_get_ea(inode, name, name_len, buf, PATH_MAX, &req); 578 579 ni_unlock(ni); 580 581 /* Translate extended attribute to acl. */ 582 if (err >= 0) { 583 acl = posix_acl_from_xattr(&init_user_ns, buf, err); 584 } else if (err == -ENODATA) { 585 acl = NULL; 586 } else { 587 acl = ERR_PTR(err); 588 } 589 590 if (!IS_ERR(acl)) 591 set_cached_acl(inode, type, acl); 592 593 kfree(buf); 594 595 return acl; 596 } 597 598 static noinline int ntfs_set_acl_ex(const struct mnt_idmap *idmap, 599 struct inode *inode, struct posix_acl *acl, 600 int type, bool init_acl) 601 { 602 const char *name; 603 size_t size, name_len; 604 void *value; 605 int err; 606 int flags; 607 umode_t mode; 608 609 /* Avoid any operation if inode is bad. */ 610 if (unlikely(is_bad_ni(ntfs_i(inode)))) 611 return -EINVAL; 612 613 if (S_ISLNK(inode->i_mode)) 614 return -EOPNOTSUPP; 615 616 mode = inode->i_mode; 617 switch (type) { 618 case ACL_TYPE_ACCESS: 619 /* Do not change i_mode if we are in init_acl */ 620 if (acl && !init_acl) { 621 err = posix_acl_update_mode(idmap, inode, &mode, &acl); 622 if (err) 623 return err; 624 } 625 name = XATTR_NAME_POSIX_ACL_ACCESS; 626 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1; 627 break; 628 629 case ACL_TYPE_DEFAULT: 630 if (!S_ISDIR(inode->i_mode)) 631 return acl ? -EACCES : 0; 632 name = XATTR_NAME_POSIX_ACL_DEFAULT; 633 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1; 634 break; 635 636 default: 637 return -EINVAL; 638 } 639 640 if (!acl) { 641 /* Remove xattr if it can be presented via mode. */ 642 size = 0; 643 value = NULL; 644 flags = XATTR_REPLACE; 645 } else { 646 value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_NOFS); 647 if (!value) 648 return -ENOMEM; 649 flags = 0; 650 } 651 652 err = ntfs_set_ea(inode, name, name_len, value, size, flags, 0, NULL); 653 if (err == -ENODATA && !size) 654 err = 0; /* Removing non existed xattr. */ 655 if (err) 656 goto out; 657 658 if (inode->i_mode != mode) { 659 umode_t old_mode = inode->i_mode; 660 inode->i_mode = mode; 661 err = ntfs_save_wsl_perm(inode, NULL); 662 if (err) { 663 inode->i_mode = old_mode; 664 goto out; 665 } 666 } 667 set_cached_acl(inode, type, acl); 668 inode_set_ctime_current(inode); 669 mark_inode_dirty(inode); 670 671 out: 672 kfree(value); 673 674 return err; 675 } 676 677 /* 678 * ntfs_set_acl - inode_operations::set_acl 679 */ 680 int ntfs_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry, 681 struct posix_acl *acl, int type) 682 { 683 return ntfs_set_acl_ex(idmap, d_inode(dentry), acl, type, false); 684 } 685 686 /* 687 * ntfs_init_acl - Initialize the ACLs of a new inode. 688 * 689 * Called from ntfs_create_inode(). 690 */ 691 int ntfs_init_acl(const struct mnt_idmap *idmap, struct inode *inode, 692 struct inode *dir) 693 { 694 struct posix_acl *default_acl, *acl; 695 int err; 696 697 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); 698 if (err) 699 return err; 700 701 if (default_acl) { 702 err = ntfs_set_acl_ex(idmap, inode, default_acl, 703 ACL_TYPE_DEFAULT, true); 704 posix_acl_release(default_acl); 705 } else { 706 inode->i_default_acl = NULL; 707 } 708 709 if (acl) { 710 if (!err) 711 err = ntfs_set_acl_ex(idmap, inode, acl, 712 ACL_TYPE_ACCESS, true); 713 posix_acl_release(acl); 714 } else { 715 inode->i_acl = NULL; 716 } 717 718 return err; 719 } 720 #endif 721 722 /* 723 * ntfs_acl_chmod - Helper for ntfs_setattr(). 724 */ 725 int ntfs_acl_chmod(const struct mnt_idmap *idmap, struct dentry *dentry) 726 { 727 struct inode *inode = d_inode(dentry); 728 struct super_block *sb = inode->i_sb; 729 730 if (!(sb->s_flags & SB_POSIXACL)) 731 return 0; 732 733 if (S_ISLNK(inode->i_mode)) 734 return -EOPNOTSUPP; 735 736 return posix_acl_chmod(idmap, dentry, inode->i_mode); 737 } 738 739 /* 740 * ntfs_listxattr - inode_operations::listxattr 741 */ 742 ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size) 743 { 744 struct inode *inode = d_inode(dentry); 745 struct ntfs_inode *ni = ntfs_i(inode); 746 ssize_t ret; 747 748 /* Avoid any operation if inode is bad. */ 749 if (unlikely(is_bad_ni(ni))) 750 return -EINVAL; 751 752 if (!(ni->ni_flags & NI_FLAG_EA)) { 753 /* no xattr in file */ 754 return 0; 755 } 756 757 ni_lock(ni); 758 759 ret = ntfs_list_ea(ni, buffer, size); 760 761 ni_unlock(ni); 762 763 return ret; 764 } 765 766 static int ntfs_getxattr(const struct xattr_handler *handler, struct dentry *de, 767 struct inode *inode, const char *name, void *buffer, 768 size_t size) 769 { 770 int err; 771 struct ntfs_inode *ni = ntfs_i(inode); 772 773 /* Avoid any operation if inode is bad. */ 774 if (unlikely(is_bad_ni(ni))) 775 return -EINVAL; 776 777 if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 778 return -EIO; 779 780 /* Dispatch request. */ 781 if (!strcmp(name, SYSTEM_DOS_ATTRIB)) { 782 /* system.dos_attrib */ 783 if (!buffer) { 784 err = sizeof(u8); 785 } else if (size < sizeof(u8)) { 786 err = -ENODATA; 787 } else { 788 err = sizeof(u8); 789 *(u8 *)buffer = le32_to_cpu(ni->std_fa); 790 } 791 goto out; 792 } 793 794 if (!strcmp(name, SYSTEM_NTFS_ATTRIB) || 795 !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) { 796 /* system.ntfs_attrib */ 797 if (!buffer) { 798 err = sizeof(u32); 799 } else if (size < sizeof(u32)) { 800 err = -ENODATA; 801 } else { 802 err = sizeof(u32); 803 *(u32 *)buffer = le32_to_cpu(ni->std_fa); 804 if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) 805 *(__be32 *)buffer = cpu_to_be32(*(u32 *)buffer); 806 } 807 goto out; 808 } 809 810 if (!strcmp(name, SYSTEM_NTFS_SECURITY)) { 811 /* system.ntfs_security*/ 812 struct SECURITY_DESCRIPTOR_RELATIVE *sd = NULL; 813 size_t sd_size = 0; 814 815 if (!is_ntfs3(ni->mi.sbi)) { 816 /* We should get nt4 security. */ 817 err = -EINVAL; 818 goto out; 819 } else if (le32_to_cpu(ni->std_security_id) < 820 SECURITY_ID_FIRST) { 821 err = -ENOENT; 822 goto out; 823 } 824 825 err = ntfs_get_security_by_id(ni->mi.sbi, ni->std_security_id, 826 &sd, &sd_size); 827 if (err) 828 goto out; 829 830 if (!is_sd_valid(sd, sd_size)) { 831 ntfs_inode_warn( 832 inode, 833 "looks like you get incorrect security descriptor id=%u", 834 ni->std_security_id); 835 } 836 837 if (!buffer) { 838 err = sd_size; 839 } else if (size < sd_size) { 840 err = -ENODATA; 841 } else { 842 err = sd_size; 843 memcpy(buffer, sd, sd_size); 844 } 845 kfree(sd); 846 goto out; 847 } 848 849 /* Deal with NTFS extended attribute. */ 850 err = ntfs_get_ea(inode, name, strlen(name), buffer, size, NULL); 851 852 out: 853 return err; 854 } 855 856 static bool ntfs_is_reserved_lxattr(const char *name) 857 { 858 return !strcmp(name, "$LXUID") || !strcmp(name, "$LXGID") || 859 !strcmp(name, "$LXMOD") || !strcmp(name, "$LXDEV"); 860 } 861 862 /* 863 * ntfs_setxattr - inode_operations::setxattr 864 */ 865 static noinline int ntfs_setxattr(const struct xattr_handler *handler, 866 const struct mnt_idmap *idmap, struct dentry *de, 867 struct inode *inode, const char *name, 868 const void *value, size_t size, int flags) 869 { 870 int err = -EINVAL; 871 struct ntfs_inode *ni = ntfs_i(inode); 872 enum FILE_ATTRIBUTE new_fa; 873 874 /* Dispatch request. */ 875 if (!strcmp(name, SYSTEM_DOS_ATTRIB)) { 876 if (sizeof(u8) != size) 877 goto out; 878 /* system.dos_attrib only covers the low DOS attribute byte. */ 879 new_fa = (ni->std_fa & ~cpu_to_le32(0xff)) | 880 cpu_to_le32(*(u8 *)value); 881 goto set_new_fa; 882 } 883 884 if (!strcmp(name, SYSTEM_NTFS_ATTRIB) || 885 !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) { 886 if (size != sizeof(u32)) 887 goto out; 888 if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) 889 new_fa = cpu_to_le32(be32_to_cpu(*(__be32 *)value)); 890 else 891 new_fa = cpu_to_le32(*(u32 *)value); 892 893 if (S_ISREG(inode->i_mode)) { 894 /* Process compressed/sparsed in special way. */ 895 ni_lock(ni); 896 err = ni_new_attr_flags(ni, new_fa); 897 ni_unlock(ni); 898 if (err) 899 goto out; 900 } 901 set_new_fa: 902 /* 903 * Thanks Mark Harmstone: 904 * Keep directory bit consistency. 905 */ 906 if (S_ISDIR(inode->i_mode)) 907 new_fa |= FILE_ATTRIBUTE_DIRECTORY; 908 else 909 new_fa &= ~FILE_ATTRIBUTE_DIRECTORY; 910 911 if (ni->std_fa != new_fa) { 912 ni->std_fa = new_fa; 913 if (new_fa & FILE_ATTRIBUTE_READONLY) 914 inode->i_mode &= ~0222; 915 else 916 inode->i_mode |= 0222; 917 /* Std attribute always in primary record. */ 918 ni->mi.dirty = true; 919 mark_inode_dirty(inode); 920 } 921 err = 0; 922 923 goto out; 924 } 925 926 if (!strcmp(name, SYSTEM_NTFS_SECURITY)) { 927 /* system.ntfs_security*/ 928 __le32 security_id; 929 bool inserted; 930 struct ATTR_STD_INFO5 *std; 931 932 if (!is_ntfs3(ni->mi.sbi)) { 933 /* 934 * We should replace ATTR_SECURE. 935 * Skip this way cause it is nt4 feature. 936 */ 937 err = -EINVAL; 938 goto out; 939 } 940 941 if (!is_sd_valid(value, size)) { 942 err = -EINVAL; 943 ntfs_inode_warn( 944 inode, 945 "you try to set invalid security descriptor"); 946 goto out; 947 } 948 949 err = ntfs_insert_security(ni->mi.sbi, value, size, 950 &security_id, &inserted); 951 if (err) 952 goto out; 953 954 ni_lock(ni); 955 std = ni_std5(ni); 956 if (!std) { 957 err = -EINVAL; 958 } else if (std->security_id != security_id) { 959 std->security_id = ni->std_security_id = security_id; 960 /* Std attribute always in primary record. */ 961 ni->mi.dirty = true; 962 mark_inode_dirty(&ni->vfs_inode); 963 } 964 ni_unlock(ni); 965 goto out; 966 } 967 968 /* Do not allow non privileged users to change $LXUID/$LXGID... */ 969 if (ntfs_is_reserved_lxattr(name) && !capable(CAP_SYS_ADMIN)) { 970 err = -EPERM; 971 goto out; 972 } 973 974 /* Deal with NTFS extended attribute. */ 975 err = ntfs_set_ea(inode, name, strlen(name), value, size, flags, 0, 976 NULL); 977 978 out: 979 inode_set_ctime_current(inode); 980 mark_inode_dirty(inode); 981 982 return err; 983 } 984 985 /* 986 * ntfs_save_wsl_perm 987 * 988 * save uid/gid/mode in xattr 989 */ 990 int ntfs_save_wsl_perm(struct inode *inode, __le32 *ea_size) 991 { 992 int err; 993 __le32 value; 994 struct ntfs_inode *ni = ntfs_i(inode); 995 996 ni_lock(ni); 997 value = cpu_to_le32(i_uid_read(inode)); 998 err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value, 999 sizeof(value), 0, true, ea_size); 1000 if (err) 1001 goto out; 1002 1003 value = cpu_to_le32(i_gid_read(inode)); 1004 err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value, 1005 sizeof(value), 0, true, ea_size); 1006 if (err) 1007 goto out; 1008 1009 value = cpu_to_le32(inode->i_mode); 1010 err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value, 1011 sizeof(value), 0, true, ea_size); 1012 if (err) 1013 goto out; 1014 1015 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 1016 value = cpu_to_le32(inode->i_rdev); 1017 err = ntfs_set_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &value, 1018 sizeof(value), 0, true, ea_size); 1019 if (err) 1020 goto out; 1021 } 1022 1023 out: 1024 ni_unlock(ni); 1025 /* In case of error should we delete all WSL xattr? */ 1026 return err; 1027 } 1028 1029 /* 1030 * ntfs_get_wsl_perm 1031 * 1032 * get uid/gid/mode from xattr 1033 * it is called from ntfs_iget5->ntfs_read_mft 1034 */ 1035 void ntfs_get_wsl_perm(struct inode *inode) 1036 { 1037 size_t sz; 1038 __le32 value[3]; 1039 1040 if (ntfs_get_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value[0], 1041 sizeof(value[0]), &sz) == sizeof(value[0]) && 1042 ntfs_get_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value[1], 1043 sizeof(value[1]), &sz) == sizeof(value[1]) && 1044 ntfs_get_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value[2], 1045 sizeof(value[2]), &sz) == sizeof(value[2])) { 1046 i_uid_write(inode, (uid_t)le32_to_cpu(value[0])); 1047 i_gid_write(inode, (gid_t)le32_to_cpu(value[1])); 1048 inode->i_mode = le32_to_cpu(value[2]); 1049 1050 if (ntfs_get_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, 1051 &value[0], sizeof(value), 1052 &sz) == sizeof(value[0])) { 1053 inode->i_rdev = le32_to_cpu(value[0]); 1054 } 1055 } 1056 } 1057 1058 static bool ntfs_xattr_user_list(struct dentry *dentry) 1059 { 1060 return true; 1061 } 1062 1063 // clang-format off 1064 static const struct xattr_handler ntfs_other_xattr_handler = { 1065 .prefix = "", 1066 .get = ntfs_getxattr, 1067 .set = ntfs_setxattr, 1068 .list = ntfs_xattr_user_list, 1069 }; 1070 1071 const struct xattr_handler * const ntfs_xattr_handlers[] = { 1072 &ntfs_other_xattr_handler, 1073 NULL, 1074 }; 1075 // clang-format on 1076