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