1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Pocessing of EA's 4 * 5 * Part of this file is based on code from the NTFS-3G. 6 * 7 * Copyright (c) 2014-2021 Jean-Pierre Andre 8 * Copyright (c) 2025 LG Electronics Co., Ltd. 9 */ 10 11 #include <linux/fs.h> 12 #include <linux/posix_acl.h> 13 #include <linux/posix_acl_xattr.h> 14 #include <linux/xattr.h> 15 16 #include "layout.h" 17 #include "attrib.h" 18 #include "index.h" 19 #include "dir.h" 20 #include "ea.h" 21 22 static int ntfs_write_ea(struct ntfs_inode *ni, __le32 type, char *value, s64 ea_off, 23 s64 ea_size, bool need_truncate) 24 { 25 struct inode *ea_vi; 26 int err = 0; 27 s64 written; 28 29 ea_vi = ntfs_attr_iget(VFS_I(ni), type, AT_UNNAMED, 0); 30 if (IS_ERR(ea_vi)) 31 return PTR_ERR(ea_vi); 32 33 written = ntfs_inode_attr_pwrite(ea_vi, ea_off, ea_size, value, false); 34 if (written != ea_size) 35 err = written < 0 ? written : -EIO; 36 else { 37 struct ntfs_inode *ea_ni = NTFS_I(ea_vi); 38 39 if (need_truncate && ea_ni->data_size > ea_off + ea_size) 40 err = ntfs_attr_truncate(ea_ni, ea_off + ea_size); 41 mark_mft_record_dirty(ni); 42 } 43 44 iput(ea_vi); 45 return err; 46 } 47 48 static int ntfs_ea_lookup(char *ea_buf, s64 ea_buf_size, const char *name, 49 int name_len, s64 *ea_offset, s64 *ea_size) 50 { 51 const struct ea_attr *p_ea; 52 size_t actual_size; 53 loff_t offset, p_ea_size; 54 unsigned int next; 55 56 if (!ea_buf_size) 57 return -ENOENT; 58 59 offset = 0; 60 do { 61 if (ea_buf_size - offset < sizeof(struct ea_attr)) 62 return -EUCLEAN; 63 64 p_ea = (const struct ea_attr *)&ea_buf[offset]; 65 next = le32_to_cpu(p_ea->next_entry_offset); 66 p_ea_size = next ? next : (ea_buf_size - offset); 67 68 if (p_ea_size < sizeof(struct ea_attr) || 69 offset + p_ea_size > ea_buf_size) 70 return -EUCLEAN; 71 72 if ((s64)p_ea->ea_name_length + 1 > 73 p_ea_size - offsetof(struct ea_attr, ea_name)) 74 return -EUCLEAN; 75 76 actual_size = ALIGN(struct_size(p_ea, ea_name, 1 + p_ea->ea_name_length + 77 le16_to_cpu(p_ea->ea_value_length)), 4); 78 if (actual_size > p_ea_size || 79 p_ea->ea_name[p_ea->ea_name_length]) 80 return -EUCLEAN; 81 82 if (name && p_ea->ea_name_length == name_len && 83 !memcmp(p_ea->ea_name, name, name_len)) { 84 *ea_offset = offset; 85 *ea_size = p_ea_size; 86 87 if (ea_buf_size < *ea_offset + *ea_size) 88 return -EUCLEAN; 89 90 return 0; 91 } 92 offset += next; 93 } while (next > 0 && offset < ea_buf_size); 94 95 return -ENOENT; 96 } 97 98 /* 99 * Return the existing EA 100 * 101 * The EA_INFORMATION is not examined and the consistency of the 102 * existing EA is not checked. 103 * 104 * If successful, the full attribute is returned unchanged 105 * and its size is returned. 106 * If the designated buffer is too small, the needed size is 107 * returned, and the buffer is left unchanged. 108 * If there is an error, a negative value is returned and errno 109 * is set according to the error. 110 */ 111 static int ntfs_get_ea(struct inode *inode, const char *name, size_t name_len, 112 void *buffer, size_t size) 113 { 114 struct ntfs_inode *ni = NTFS_I(inode); 115 const struct ea_attr *p_ea; 116 char *ea_buf; 117 s64 ea_off, ea_size, all_ea_size, ea_info_size; 118 int err; 119 u32 ea_info_qlen; 120 u16 ea_value_len; 121 struct ea_information *p_ea_info; 122 123 if (!NInoHasEA(ni)) 124 return -ENODATA; 125 126 p_ea_info = ntfs_attr_readall(ni, AT_EA_INFORMATION, NULL, 0, 127 &ea_info_size); 128 if (IS_ERR(p_ea_info)) 129 return PTR_ERR(p_ea_info); 130 if (ea_info_size != sizeof(struct ea_information)) { 131 kvfree(p_ea_info); 132 return -EUCLEAN; 133 } 134 135 ea_info_qlen = le32_to_cpu(p_ea_info->ea_query_length); 136 kvfree(p_ea_info); 137 138 ea_buf = ntfs_attr_readall(ni, AT_EA, NULL, 0, &all_ea_size); 139 if (IS_ERR(ea_buf)) 140 return PTR_ERR(ea_buf); 141 142 if (ea_info_qlen > all_ea_size) { 143 err = -EUCLEAN; 144 goto free_ea_buf; 145 } 146 147 err = ntfs_ea_lookup(ea_buf, ea_info_qlen, name, name_len, &ea_off, 148 &ea_size); 149 if (!err) { 150 p_ea = (struct ea_attr *)&ea_buf[ea_off]; 151 ea_value_len = le16_to_cpu(p_ea->ea_value_length); 152 if (!buffer) { 153 kvfree(ea_buf); 154 return ea_value_len; 155 } 156 157 if (ea_value_len > size) { 158 err = -ERANGE; 159 goto free_ea_buf; 160 } 161 162 memcpy(buffer, &p_ea->ea_name[p_ea->ea_name_length + 1], 163 ea_value_len); 164 kvfree(ea_buf); 165 return ea_value_len; 166 } 167 168 if (err == -ENOENT) 169 err = -ENODATA; 170 free_ea_buf: 171 kvfree(ea_buf); 172 return err; 173 } 174 175 static inline int ea_packed_size(const struct ea_attr *p_ea) 176 { 177 /* 178 * 4 bytes for header (flags and lengths) + name length + 1 + 179 * value length. 180 */ 181 return 5 + p_ea->ea_name_length + le16_to_cpu(p_ea->ea_value_length); 182 } 183 184 /* Unlike ntfs_attr_exist(), preserve lookup errors. */ 185 static int ntfs_ea_attr_lookup(struct ntfs_inode *ni, __le32 type) 186 { 187 struct ntfs_attr_search_ctx *ctx; 188 struct mft_record *mrec; 189 int err; 190 191 mrec = map_mft_record(ni); 192 if (IS_ERR(mrec)) 193 return PTR_ERR(mrec); 194 ctx = ntfs_attr_get_search_ctx(ni, mrec); 195 if (!ctx) { 196 err = -ENOMEM; 197 goto out; 198 } 199 err = ntfs_attr_lookup(type, AT_UNNAMED, 0, CASE_SENSITIVE, 200 0, NULL, 0, ctx); 201 ntfs_attr_put_search_ctx(ctx); 202 out: 203 unmap_mft_record(ni); 204 return err; 205 } 206 207 /* A NULL value means that the attribute should not exist. */ 208 static int ntfs_ea_update_attr(struct ntfs_inode *ni, __le32 type, 209 char *value, s64 size) 210 { 211 int err = ntfs_ea_attr_lookup(ni, type); 212 213 if (err && err != -ENOENT) 214 return err; 215 if (!value) 216 return err == -ENOENT ? 0 : 217 ntfs_attr_remove(ni, type, AT_UNNAMED, 0, NULL); 218 if (err == -ENOENT) 219 return ntfs_attr_add(ni, type, AT_UNNAMED, 0, value, size); 220 return ntfs_write_ea(ni, type, value, 0, size, true); 221 } 222 223 /* 224 * Prepare the new EA list before changing either NTFS attribute. Keep the 225 * original values until both updates succeed, so a failed update can be 226 * rolled back. This does not provide atomicity across a crash or power loss. 227 */ 228 static int ntfs_set_ea(struct inode *inode, const char *name, size_t name_len, 229 const void *value, size_t val_size, int flags, 230 __le16 *packed_ea_size) 231 { 232 struct ntfs_inode *ni = NTFS_I(inode); 233 struct ea_information *old_info = NULL, info = {}; 234 char *old_ea = NULL, *ea = NULL; 235 struct ea_attr *entry; 236 s64 old_size = 0, info_size, offset = 0, entry_size = 0; 237 u32 size = 0, new_size, next; 238 size_t add_size; 239 int err, info_err, ea_err, packed = 0, needed = 0; 240 bool exists, remove = !value; 241 242 if (name_len > U8_MAX) 243 return -ENAMETOOLONG; 244 if (val_size > U16_MAX) 245 return -E2BIG; 246 247 err = ntfs_ea_attr_lookup(ni, AT_EA_INFORMATION); 248 if (err && err != -ENOENT) 249 return err; 250 exists = !err; 251 err = ntfs_ea_attr_lookup(ni, AT_EA); 252 if (err && err != -ENOENT) 253 return err; 254 /* Do not silently discard an orphaned attribute. */ 255 if (exists != !err) 256 return -EUCLEAN; 257 258 if (exists) { 259 old_info = ntfs_attr_readall(ni, AT_EA_INFORMATION, NULL, 0, 260 &info_size); 261 if (IS_ERR(old_info)) 262 return PTR_ERR(old_info); 263 if (info_size != sizeof(*old_info)) { 264 err = -EUCLEAN; 265 goto out; 266 } 267 old_ea = ntfs_attr_readall(ni, AT_EA, NULL, 0, &old_size); 268 if (IS_ERR(old_ea)) { 269 err = PTR_ERR(old_ea); 270 old_ea = NULL; 271 goto out; 272 } 273 size = le32_to_cpu(old_info->ea_query_length); 274 if (size > old_size) { 275 err = -EUCLEAN; 276 goto out; 277 } 278 } 279 280 /* Validate even the entries following the one being replaced. */ 281 err = ntfs_ea_lookup(old_ea, size, NULL, 0, &offset, &entry_size); 282 if (err != -ENOENT) 283 goto out; 284 err = ntfs_ea_lookup(old_ea, size, name, name_len, &offset, &entry_size); 285 if (err && err != -ENOENT) 286 goto out; 287 if (!err) { 288 if (flags & XATTR_CREATE) { 289 err = -EEXIST; 290 goto out; 291 } 292 entry = (struct ea_attr *)(old_ea + offset); 293 if (!remove && le16_to_cpu(entry->ea_value_length) == val_size && 294 !memcmp(entry->ea_name + name_len + 1, value, val_size)) { 295 info = *old_info; 296 goto done; 297 } 298 } else { 299 if (remove || (flags & XATTR_REPLACE)) { 300 err = -ENODATA; 301 goto out; 302 } 303 offset = size; 304 entry_size = 0; 305 } 306 307 add_size = remove ? 0 : ALIGN(sizeof(*entry) + name_len + 1 + val_size, 4); 308 new_size = size - entry_size + add_size; 309 if (new_size && ntfs_attr_size_bounds_check(ni->vol, AT_EA, new_size)) { 310 err = -EFBIG; 311 goto out; 312 } 313 if (new_size) { 314 ea = kvzalloc(new_size, GFP_NOFS); 315 if (!ea) { 316 err = -ENOMEM; 317 goto out; 318 } 319 if (offset) 320 memcpy(ea, old_ea, offset); 321 if (size > offset + entry_size) 322 memcpy(ea + offset, old_ea + offset + entry_size, 323 size - offset - entry_size); 324 } 325 size -= entry_size; 326 327 /* Link a zero-terminated old tail before appending the new entry. */ 328 for (offset = 0; offset < size; offset += next) { 329 entry = (struct ea_attr *)(ea + offset); 330 next = le32_to_cpu(entry->next_entry_offset); 331 if (!next) { 332 next = size - offset; 333 entry->next_entry_offset = cpu_to_le32(next); 334 } 335 packed += ea_packed_size(entry); 336 if (entry->flags & NEED_EA) 337 needed++; 338 } 339 if (!remove) { 340 entry = (struct ea_attr *)(ea + size); 341 entry->next_entry_offset = cpu_to_le32(add_size); 342 entry->ea_name_length = name_len; 343 entry->ea_value_length = cpu_to_le16(val_size); 344 memcpy(entry->ea_name, name, name_len); 345 memcpy(entry->ea_name + name_len + 1, value, val_size); 346 packed += ea_packed_size(entry); 347 } 348 if (packed > U16_MAX) { 349 err = -EFBIG; 350 goto out; 351 } 352 info.ea_length = cpu_to_le16(packed); 353 info.need_ea_count = cpu_to_le16(needed); 354 info.ea_query_length = cpu_to_le32(new_size); 355 356 /* All preparation is complete; errors from here require rollback. */ 357 err = ntfs_ea_update_attr(ni, AT_EA, ea, new_size); 358 if (!err) 359 err = ntfs_ea_update_attr(ni, AT_EA_INFORMATION, 360 new_size ? (char *)&info : NULL, 361 sizeof(info)); 362 if (err) { 363 /* Try both restorations even if the first one fails. */ 364 ea_err = ntfs_ea_update_attr(ni, AT_EA, old_ea, old_size); 365 info_err = ntfs_ea_update_attr(ni, AT_EA_INFORMATION, 366 (char *)old_info, sizeof(info)); 367 if (ea_err || info_err) { 368 NVolSetErrors(ni->vol); 369 NVolSetShutdown(ni->vol); 370 ntfs_error(inode->i_sb, 371 "Failed to restore EAs of inode 0x%llx (%d, %d); shutting down.", 372 ni->mft_no, ea_err, info_err); 373 err = -EIO; 374 } 375 goto out; 376 } 377 size = new_size; 378 mark_mft_record_dirty(ni); 379 done: 380 if (packed_ea_size) 381 *packed_ea_size = info.ea_length; 382 if (size) 383 NInoSetHasEA(ni); 384 else 385 NInoClearHasEA(ni); 386 out: 387 kvfree(ea); 388 kvfree(old_ea); 389 kvfree(old_info); 390 return err; 391 } 392 /* 393 * Check for the presence of an EA "$LXDEV" (used by WSL) 394 * and return its value as a device address 395 */ 396 int ntfs_ea_get_wsl_inode(struct inode *inode, dev_t *rdevp, unsigned int flags, 397 bool *has_lxmod) 398 { 399 int err; 400 __le32 v; 401 402 *has_lxmod = false; 403 404 if (!(flags & NTFS_VOL_UID)) { 405 /* Load uid to lxuid EA */ 406 err = ntfs_get_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &v, 407 sizeof(v)); 408 if (err == sizeof(v)) 409 i_uid_write(inode, le32_to_cpu(v)); 410 } 411 412 if (!(flags & NTFS_VOL_GID)) { 413 /* Load gid to lxgid EA */ 414 err = ntfs_get_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &v, 415 sizeof(v)); 416 if (err == sizeof(v)) 417 i_gid_write(inode, le32_to_cpu(v)); 418 } 419 420 /* Load mode to lxmod EA */ 421 err = ntfs_get_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &v, sizeof(v)); 422 if (err == sizeof(v)) { 423 inode->i_mode = le32_to_cpu(v); 424 *has_lxmod = true; 425 } else { 426 /* Everyone gets all permissions. */ 427 inode->i_mode |= 0777; 428 } 429 430 /* Load mode to lxdev EA */ 431 err = ntfs_get_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &v, sizeof(v)); 432 if (err == sizeof(v)) 433 *rdevp = le32_to_cpu(v); 434 err = 0; 435 436 return err; 437 } 438 439 int ntfs_ea_set_wsl_inode(struct inode *inode, dev_t rdev, __le16 *ea_size, 440 unsigned int flags) 441 { 442 __le32 v; 443 int err = 0; 444 445 if (ea_size) 446 *ea_size = 0; 447 448 if (flags & NTFS_EA_UID) { 449 /* Store uid to lxuid EA */ 450 v = cpu_to_le32(i_uid_read(inode)); 451 err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &v, 452 sizeof(v), 0, ea_size); 453 if (err) 454 return err; 455 } 456 457 if (flags & NTFS_EA_GID) { 458 /* Store gid to lxgid EA */ 459 v = cpu_to_le32(i_gid_read(inode)); 460 err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &v, 461 sizeof(v), 0, ea_size); 462 if (err) 463 return err; 464 } 465 466 if (flags & NTFS_EA_MODE) { 467 /* Store mode to lxmod EA */ 468 v = cpu_to_le32(inode->i_mode); 469 err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &v, 470 sizeof(v), 0, ea_size); 471 if (err) 472 return err; 473 } 474 475 if (rdev) { 476 v = cpu_to_le32(rdev); 477 err = ntfs_set_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &v, sizeof(v), 478 0, ea_size); 479 } 480 481 return err; 482 } 483 484 int ntfs_ea_get_linuxflags(struct inode *inode) 485 { 486 struct ntfs_inode *ni = NTFS_I(inode); 487 __le32 value; 488 int err; 489 490 err = ntfs_get_ea(inode, "$LINUXFLAGS", sizeof("$LINUXFLAGS") - 1, 491 &value, sizeof(value)); 492 if (err == -ENODATA) 493 return 0; 494 if (err < 0) 495 return err == -ERANGE ? -EUCLEAN : err; 496 if (err != sizeof(value)) 497 return -EUCLEAN; 498 499 ni->linuxflags = le32_to_cpu(value); 500 if (ni->linuxflags & NTFS_LINUXFLAGS_IMMUTABLE) 501 inode->i_flags |= S_IMMUTABLE; 502 if (ni->linuxflags & NTFS_LINUXFLAGS_APPEND) 503 inode->i_flags |= S_APPEND; 504 if (ni->linuxflags & NTFS_LINUXFLAGS_NOATIME) 505 inode->i_flags |= S_NOATIME; 506 return 0; 507 } 508 509 int ntfs_ea_set_linuxflags(struct inode *inode, u32 linuxflags) 510 { 511 struct ntfs_inode *ni = NTFS_I(inode); 512 __le32 value; 513 int err; 514 515 if (linuxflags) { 516 value = cpu_to_le32(linuxflags); 517 err = ntfs_set_ea(inode, "$LINUXFLAGS", sizeof("$LINUXFLAGS") - 1, 518 &value, sizeof(value), 0, NULL); 519 } else if (NInoHasEA(ni)) { 520 err = ntfs_set_ea(inode, "$LINUXFLAGS", sizeof("$LINUXFLAGS") - 1, 521 NULL, 0, XATTR_REPLACE, NULL); 522 if (err == -ENODATA) 523 err = 0; 524 } else { 525 err = 0; 526 } 527 528 if (!err) 529 ni->linuxflags = linuxflags; 530 return err; 531 } 532 533 ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size) 534 { 535 struct inode *inode = d_inode(dentry); 536 struct ntfs_inode *ni = NTFS_I(inode); 537 const struct ea_attr *p_ea; 538 s64 offset, ea_buf_size, ea_info_size; 539 s64 ea_size; 540 u32 next; 541 int err = 0; 542 u32 ea_info_qsize; 543 char *ea_buf = NULL; 544 ssize_t ret = 0; 545 struct ea_information *ea_info; 546 547 if (!NInoHasEA(ni)) 548 return 0; 549 550 mutex_lock(&NTFS_I(inode)->mrec_lock); 551 ea_info = ntfs_attr_readall(ni, AT_EA_INFORMATION, NULL, 0, 552 &ea_info_size); 553 if (IS_ERR(ea_info)) { 554 err = PTR_ERR(ea_info); 555 ea_info = NULL; 556 goto out; 557 } 558 if (ea_info_size != sizeof(struct ea_information)) { 559 err = -EUCLEAN; 560 goto out; 561 } 562 563 ea_info_qsize = le32_to_cpu(ea_info->ea_query_length); 564 565 ea_buf = ntfs_attr_readall(ni, AT_EA, NULL, 0, &ea_buf_size); 566 if (IS_ERR(ea_buf)) { 567 err = PTR_ERR(ea_buf); 568 ea_buf = NULL; 569 goto out; 570 } 571 572 if (ea_info_qsize > ea_buf_size) { 573 err = -EUCLEAN; 574 goto out; 575 } 576 577 /* Validate the entire chain, including size-only queries. */ 578 err = ntfs_ea_lookup(ea_buf, ea_info_qsize, NULL, 0, &offset, 579 &ea_size); 580 if (err != -ENOENT) 581 goto out; 582 err = 0; 583 if (!ea_info_qsize) 584 goto out; 585 586 offset = 0; 587 do { 588 p_ea = (const struct ea_attr *)&ea_buf[offset]; 589 next = le32_to_cpu(p_ea->next_entry_offset); 590 ea_size = next ? next : (ea_info_qsize - offset); 591 592 if (buffer) { 593 if (ret + p_ea->ea_name_length + 1 > size) { 594 err = -ERANGE; 595 goto out; 596 } 597 598 memcpy(buffer + ret, p_ea->ea_name, p_ea->ea_name_length); 599 buffer[ret + p_ea->ea_name_length] = 0; 600 } 601 602 ret += p_ea->ea_name_length + 1; 603 offset += ea_size; 604 } while (next > 0 && offset < ea_info_qsize); 605 606 out: 607 mutex_unlock(&NTFS_I(inode)->mrec_lock); 608 kvfree(ea_info); 609 kvfree(ea_buf); 610 611 return err ? err : ret; 612 } 613 614 // clang-format off 615 #define SYSTEM_DOS_ATTRIB "system.dos_attrib" 616 #define SYSTEM_NTFS_ATTRIB "system.ntfs_attrib" 617 #define SYSTEM_NTFS_ATTRIB_BE "system.ntfs_attrib_be" 618 // clang-format on 619 620 static int ntfs_getxattr(const struct xattr_handler *handler, 621 struct dentry *unused, struct inode *inode, const char *name, 622 void *buffer, size_t size) 623 { 624 struct ntfs_inode *ni = NTFS_I(inode); 625 int err; 626 627 /* 628 * Stream permissions and privileges belong to the base inode. This 629 * also lets VFS killpriv helpers find the base security attributes. 630 */ 631 if (ntfs_inode_is_named_stream(ni)) { 632 ni = ni->ext.base_ntfs_ino; 633 inode = VFS_I(ni); 634 } 635 if (NVolShutdown(ni->vol)) 636 return -EIO; 637 638 if (!strcmp(name, SYSTEM_DOS_ATTRIB)) { 639 if (!buffer) { 640 err = sizeof(u8); 641 } else if (size < sizeof(u8)) { 642 err = -ERANGE; 643 } else { 644 err = sizeof(u8); 645 *(u8 *)buffer = (u8)(le32_to_cpu(ni->flags) & 0x3F); 646 } 647 goto out; 648 } 649 650 if (!strcmp(name, SYSTEM_NTFS_ATTRIB) || 651 !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) { 652 if (!buffer) { 653 err = sizeof(u32); 654 } else if (size < sizeof(u32)) { 655 err = -ERANGE; 656 } else { 657 err = sizeof(u32); 658 *(u32 *)buffer = le32_to_cpu(ni->flags); 659 if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) 660 *(__be32 *)buffer = cpu_to_be32(*(u32 *)buffer); 661 } 662 goto out; 663 } 664 665 mutex_lock(&ni->mrec_lock); 666 err = ntfs_get_ea(inode, name, strlen(name), buffer, size); 667 mutex_unlock(&ni->mrec_lock); 668 669 out: 670 return err; 671 } 672 673 static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr) 674 { 675 struct ntfs_attr_search_ctx *ctx; 676 struct mft_record *m; 677 struct attr_record *a; 678 __le16 new_aflags; 679 u16 old_name_ofs, old_mp_ofs; 680 int mp_size, mp_ofs, name_ofs, old_arec_size, arec_size, err; 681 682 m = map_mft_record(ni); 683 if (IS_ERR(m)) 684 return PTR_ERR(m); 685 686 ctx = ntfs_attr_get_search_ctx(ni, m); 687 if (!ctx) { 688 err = -ENOMEM; 689 goto err_out; 690 } 691 692 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 693 CASE_SENSITIVE, 0, NULL, 0, ctx); 694 if (err) { 695 err = -EINVAL; 696 goto err_out; 697 } 698 699 a = ctx->attr; 700 new_aflags = ctx->attr->flags; 701 702 if (fattr & FILE_ATTR_SPARSE_FILE) 703 new_aflags |= ATTR_IS_SPARSE; 704 else 705 new_aflags &= ~ATTR_IS_SPARSE; 706 707 if (fattr & FILE_ATTR_COMPRESSED) 708 new_aflags |= ATTR_IS_COMPRESSED; 709 else 710 new_aflags &= ~ATTR_IS_COMPRESSED; 711 712 if (new_aflags == a->flags) { 713 err = 0; 714 goto err_out; 715 } 716 717 if ((new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) == 718 (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) { 719 pr_err("file can't be sparsed and compressed\n"); 720 err = -EOPNOTSUPP; 721 goto err_out; 722 } 723 724 if (!a->non_resident) { 725 if (!(new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED))) { 726 err = 0; 727 goto err_out; 728 } 729 730 if (le32_to_cpu(a->data.resident.value_length)) { 731 pr_err("Can't change sparse/compressed for non-empty file"); 732 err = -EOPNOTSUPP; 733 goto err_out; 734 } 735 736 err = ntfs_attr_make_non_resident(ni, 0); 737 if (err) 738 goto err_out; 739 740 ntfs_attr_reinit_search_ctx(ctx); 741 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 742 CASE_SENSITIVE, 743 0, NULL, 0, ctx); 744 if (err) { 745 err = -EINVAL; 746 goto err_out; 747 } 748 a = ctx->attr; 749 } else { 750 if (a->data.non_resident.data_size) { 751 pr_err("Can't change sparsed/compressed for non-empty file"); 752 err = -EOPNOTSUPP; 753 goto err_out; 754 } 755 } 756 757 old_name_ofs = le16_to_cpu(a->name_offset); 758 old_mp_ofs = le16_to_cpu(a->data.non_resident.mapping_pairs_offset); 759 760 if (new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) 761 name_ofs = (offsetof(struct attr_record, 762 data.non_resident.compressed_size) + 763 sizeof(a->data.non_resident.compressed_size) + 7) & ~7; 764 else 765 name_ofs = (offsetof(struct attr_record, 766 data.non_resident.compressed_size) + 7) & ~7; 767 768 mp_size = ntfs_get_size_for_mapping_pairs(ni->vol, ni->runlist.rl, 0, -1, -1); 769 if (unlikely(mp_size < 0)) { 770 err = mp_size; 771 ntfs_debug("Failed to get size for mapping pairs array, error code %i.\n", err); 772 goto err_out; 773 } 774 775 mp_ofs = (name_ofs + a->name_length * sizeof(__le16) + 7) & ~7; 776 arec_size = (mp_ofs + mp_size + 7) & ~7; 777 old_arec_size = le32_to_cpu(a->length); 778 779 /* 780 * Move payloads before shrinking the record. Otherwise resizing moves 781 * the following attribute over the old payload before it can be copied. 782 * 783 * When offsets increase, move mapping_pairs first to avoid name 784 * overwriting the start of mapping_pairs. 785 */ 786 if (arec_size < old_arec_size) { 787 if (name_ofs > old_name_ofs) { 788 /* Payload offsets increased: move mapping pairs first. */ 789 if (mp_ofs != old_mp_ofs) 790 memmove((u8 *)a + mp_ofs, 791 (u8 *)a + old_mp_ofs, 792 mp_size); 793 if (a->name_length && name_ofs != old_name_ofs) 794 memmove((u8 *)a + name_ofs, 795 (u8 *)a + old_name_ofs, 796 a->name_length * 797 sizeof(__le16)); 798 } else { 799 /* Payload offsets decreased or unchanged: move name first. */ 800 if (a->name_length && name_ofs != old_name_ofs) 801 memmove((u8 *)a + name_ofs, 802 (u8 *)a + old_name_ofs, 803 a->name_length * 804 sizeof(__le16)); 805 if (mp_ofs != old_mp_ofs) 806 memmove((u8 *)a + mp_ofs, 807 (u8 *)a + old_mp_ofs, 808 mp_size); 809 } 810 } 811 812 err = ntfs_attr_record_resize(ctx->mrec, a, arec_size); 813 if (unlikely(err)) 814 goto err_out; 815 816 /* 817 * When compressed/sparse state changes, the non-resident header grows or 818 * shrinks by the compressed_size field. Update the in-record payload layout 819 * to match the new offsets before exposing the new mapping_pairs_offset. 820 */ 821 if (arec_size > old_arec_size) { 822 if (mp_ofs != old_mp_ofs) 823 memmove((u8 *)a + mp_ofs, (u8 *)a + old_mp_ofs, mp_size); 824 if (a->name_length) 825 memmove((u8 *)a + name_ofs, (u8 *)a + old_name_ofs, 826 a->name_length * sizeof(__le16)); 827 } 828 829 if (new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) { 830 a->data.non_resident.compression_unit = 0; 831 if (new_aflags & ATTR_IS_COMPRESSED || ni->vol->major_ver < 3) 832 a->data.non_resident.compression_unit = 4; 833 a->data.non_resident.compressed_size = 0; 834 ni->itype.compressed.size = 0; 835 if (a->data.non_resident.compression_unit) { 836 ni->itype.compressed.block_size = 1U << 837 (a->data.non_resident.compression_unit + 838 ni->vol->cluster_size_bits); 839 ni->itype.compressed.block_size_bits = 840 ffs(ni->itype.compressed.block_size) - 841 1; 842 ni->itype.compressed.block_clusters = 1U << 843 a->data.non_resident.compression_unit; 844 } else { 845 ni->itype.compressed.block_size = 0; 846 ni->itype.compressed.block_size_bits = 0; 847 ni->itype.compressed.block_clusters = 0; 848 } 849 } else { 850 a->data.non_resident.compression_unit = 0; 851 } 852 853 a->name_offset = cpu_to_le16(name_ofs); 854 a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs); 855 856 a->flags = new_aflags; 857 858 if (new_aflags & ATTR_IS_SPARSE) { 859 NInoSetSparse(ni); 860 ni->flags |= FILE_ATTR_SPARSE_FILE; 861 } else { 862 NInoClearSparse(ni); 863 ni->flags &= ~FILE_ATTR_SPARSE_FILE; 864 } 865 866 if (new_aflags & ATTR_IS_COMPRESSED) { 867 NInoSetCompressed(ni); 868 ni->flags |= FILE_ATTR_COMPRESSED; 869 } else { 870 NInoClearCompressed(ni); 871 ni->flags &= ~FILE_ATTR_COMPRESSED; 872 } 873 874 mark_mft_record_dirty(ctx->ntfs_ino); 875 err_out: 876 if (ctx) 877 ntfs_attr_put_search_ctx(ctx); 878 unmap_mft_record(ni); 879 return err; 880 } 881 882 static bool ntfs_is_reserved_lxattr(const char *name) 883 { 884 return !strcmp(name, "$LXUID") || !strcmp(name, "$LXGID") || 885 !strcmp(name, "$LXMOD") || !strcmp(name, "$LXDEV"); 886 } 887 888 static int ntfs_validate_fattr(struct ntfs_inode *ni, __le32 fattr) 889 { 890 const __le32 wof_flags = FILE_ATTR_SPARSE_FILE | 891 FILE_ATTR_REPARSE_POINT; 892 893 if (NInoWofCompressed(ni) && ((ni->flags ^ fattr) & wof_flags)) 894 return -EPERM; 895 896 return 0; 897 } 898 899 static int ntfs_setxattr(const struct xattr_handler *handler, 900 const struct mnt_idmap *idmap, struct dentry *unused, 901 struct inode *inode, const char *name, const void *value, 902 size_t size, int flags) 903 { 904 struct ntfs_inode *ni = NTFS_I(inode); 905 int err; 906 __le32 fattr; 907 908 if (ntfs_inode_is_named_stream(ni)) 909 return -EOPNOTSUPP; 910 if (NVolShutdown(ni->vol)) 911 return -EIO; 912 913 /* Only fileattr_set may change the flags and their cached state. */ 914 if (!strcmp(name, "$LINUXFLAGS")) 915 return -EPERM; 916 917 if (ntfs_is_reserved_lxattr(name) && !capable(CAP_SYS_ADMIN)) 918 return -EPERM; 919 920 if (!strcmp(name, SYSTEM_DOS_ATTRIB)) { 921 if (sizeof(u8) != size) { 922 err = -EINVAL; 923 goto out; 924 } 925 fattr = cpu_to_le32((le32_to_cpu(ni->flags) & ~0xffU) | 926 *(u8 *)value); 927 goto set_fattr; 928 } 929 930 if (!strcmp(name, SYSTEM_NTFS_ATTRIB) || 931 !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) { 932 if (size != sizeof(u32)) { 933 err = -EINVAL; 934 goto out; 935 } 936 if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) 937 fattr = cpu_to_le32(be32_to_cpu(*(__be32 *)value)); 938 else 939 fattr = cpu_to_le32(*(u32 *)value); 940 941 err = ntfs_validate_fattr(ni, fattr); 942 if (err) 943 goto out; 944 945 if (S_ISREG(inode->i_mode)) { 946 mutex_lock(&ni->mrec_lock); 947 err = ntfs_new_attr_flags(ni, fattr); 948 mutex_unlock(&ni->mrec_lock); 949 if (err) 950 goto out; 951 } 952 953 set_fattr: 954 if (S_ISDIR(inode->i_mode)) 955 fattr |= FILE_ATTR_DIRECTORY; 956 else 957 fattr &= ~FILE_ATTR_DIRECTORY; 958 959 err = ntfs_validate_fattr(ni, fattr); 960 if (err) 961 goto out; 962 963 if (ni->flags != fattr) { 964 ni->flags = fattr; 965 if (fattr & FILE_ATTR_READONLY) 966 inode->i_mode &= ~0222; 967 else 968 inode->i_mode |= 0222; 969 NInoSetFileNameDirty(ni); 970 mark_inode_dirty(inode); 971 } 972 err = 0; 973 goto out; 974 } 975 976 mutex_lock(&ni->mrec_lock); 977 err = ntfs_set_ea(inode, name, strlen(name), value, size, flags, NULL); 978 mutex_unlock(&ni->mrec_lock); 979 980 out: 981 if (!err) { 982 inode_set_ctime_current(inode); 983 mark_inode_dirty(inode); 984 } 985 return err; 986 } 987 988 static bool ntfs_xattr_user_list(struct dentry *dentry) 989 { 990 return true; 991 } 992 993 // clang-format off 994 static const struct xattr_handler ntfs_other_xattr_handler = { 995 .prefix = "", 996 .get = ntfs_getxattr, 997 .set = ntfs_setxattr, 998 .list = ntfs_xattr_user_list, 999 }; 1000 1001 const struct xattr_handler * const ntfs_xattr_handlers[] = { 1002 &ntfs_other_xattr_handler, 1003 NULL, 1004 }; 1005 // clang-format on 1006 1007 #ifdef CONFIG_NTFS_FS_POSIX_ACL 1008 struct posix_acl *ntfs_get_acl(const struct mnt_idmap *idmap, struct dentry *dentry, 1009 int type) 1010 { 1011 struct inode *inode = d_inode(dentry); 1012 struct ntfs_inode *ni = NTFS_I(inode); 1013 const char *name; 1014 size_t name_len; 1015 struct posix_acl *acl; 1016 int err; 1017 void *buf; 1018 1019 buf = kmalloc(PATH_MAX, GFP_KERNEL); 1020 if (!buf) 1021 return ERR_PTR(-ENOMEM); 1022 1023 /* Possible values of 'type' was already checked above. */ 1024 if (type == ACL_TYPE_ACCESS) { 1025 name = XATTR_NAME_POSIX_ACL_ACCESS; 1026 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1; 1027 } else { 1028 name = XATTR_NAME_POSIX_ACL_DEFAULT; 1029 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1; 1030 } 1031 1032 mutex_lock(&ni->mrec_lock); 1033 err = ntfs_get_ea(inode, name, name_len, buf, PATH_MAX); 1034 mutex_unlock(&ni->mrec_lock); 1035 1036 /* Translate extended attribute to acl. */ 1037 if (err >= 0) 1038 acl = posix_acl_from_xattr(&init_user_ns, buf, err); 1039 else if (err == -ENODATA) 1040 acl = NULL; 1041 else 1042 acl = ERR_PTR(err); 1043 1044 if (!IS_ERR(acl)) 1045 set_cached_acl(inode, type, acl); 1046 1047 kfree(buf); 1048 1049 return acl; 1050 } 1051 1052 static noinline int ntfs_set_acl_ex(const struct mnt_idmap *idmap, 1053 struct inode *inode, struct posix_acl *acl, 1054 int type, bool init_acl) 1055 { 1056 const char *name; 1057 size_t size, name_len; 1058 void *value, *old_value = NULL; 1059 int err, old_len = -ENODATA; 1060 bool acl_committed = false; 1061 int flags; 1062 umode_t mode; 1063 1064 if (S_ISLNK(inode->i_mode)) 1065 return -EOPNOTSUPP; 1066 1067 mode = inode->i_mode; 1068 switch (type) { 1069 case ACL_TYPE_ACCESS: 1070 /* Do not change i_mode if we are in init_acl */ 1071 if (acl && !init_acl) { 1072 err = posix_acl_update_mode(idmap, inode, &mode, &acl); 1073 if (err) 1074 return err; 1075 } 1076 name = XATTR_NAME_POSIX_ACL_ACCESS; 1077 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1; 1078 break; 1079 1080 case ACL_TYPE_DEFAULT: 1081 if (!S_ISDIR(inode->i_mode)) 1082 return acl ? -EACCES : 0; 1083 name = XATTR_NAME_POSIX_ACL_DEFAULT; 1084 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1; 1085 break; 1086 1087 default: 1088 return -EINVAL; 1089 } 1090 1091 if (!acl) { 1092 /* Remove xattr if it can be presented via mode. */ 1093 size = 0; 1094 value = NULL; 1095 flags = XATTR_REPLACE; 1096 } else { 1097 value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_NOFS); 1098 if (!value) 1099 return -ENOMEM; 1100 flags = 0; 1101 } 1102 1103 mutex_lock(&NTFS_I(inode)->mrec_lock); 1104 if (!init_acl) { 1105 /* Save the old xattr for rollback. */ 1106 old_len = ntfs_get_ea(inode, name, name_len, NULL, 0); 1107 if (old_len >= 0) { 1108 /* kmalloc(0) returns the non-NULL ZERO_SIZE_PTR. */ 1109 old_value = kmalloc(old_len, GFP_NOFS); 1110 if (!old_value) { 1111 err = -ENOMEM; 1112 goto out_unlock; 1113 } 1114 err = ntfs_get_ea(inode, name, name_len, 1115 old_value, old_len); 1116 if (err != old_len) { 1117 kfree(old_value); 1118 old_value = NULL; 1119 if (err >= 0) 1120 err = -EIO; 1121 goto out_unlock; 1122 } 1123 } else if (old_len != -ENODATA) { 1124 err = old_len; 1125 goto out_unlock; 1126 } 1127 } 1128 err = ntfs_set_ea(inode, name, name_len, value, size, flags, NULL); 1129 if (!err) 1130 acl_committed = true; 1131 out_unlock: 1132 mutex_unlock(&NTFS_I(inode)->mrec_lock); 1133 if (err == -ENODATA && !size) 1134 err = 0; /* Removing non existed xattr. */ 1135 if (!err) { 1136 __le16 ea_size = 0; 1137 umode_t old_mode = inode->i_mode; 1138 1139 inode->i_mode = mode; 1140 mutex_lock(&NTFS_I(inode)->mrec_lock); 1141 err = ntfs_ea_set_wsl_inode(inode, 0, &ea_size, NTFS_EA_MODE); 1142 if (err) { 1143 if (acl_committed) { 1144 int undo_err; 1145 1146 if (old_len >= 0) { 1147 /* 1148 * A removed or zero-length xattr must 1149 * be recreated without XATTR_REPLACE. 1150 */ 1151 int undo_flags = acl && old_len ? 1152 XATTR_REPLACE : 0; 1153 1154 undo_err = ntfs_set_ea(inode, name, 1155 name_len, old_value, 1156 old_len, undo_flags, 1157 NULL); 1158 } else { 1159 undo_err = ntfs_set_ea(inode, name, 1160 name_len, NULL, 0, 1161 XATTR_REPLACE, NULL); 1162 } 1163 if (undo_err) { 1164 /* 1165 * Record a failed rollback so the next 1166 * persistence point marks the volume 1167 * dirty. Run chkdsk. 1168 */ 1169 NVolSetErrors(NTFS_I(inode)->vol); 1170 ntfs_error(inode->i_sb, 1171 "Failed to undo the ACL xattr: %d", 1172 undo_err); 1173 } 1174 } 1175 mutex_unlock(&NTFS_I(inode)->mrec_lock); 1176 inode->i_mode = old_mode; 1177 goto out; 1178 } 1179 mutex_unlock(&NTFS_I(inode)->mrec_lock); 1180 1181 set_cached_acl(inode, type, acl); 1182 inode_set_ctime_current(inode); 1183 mark_inode_dirty(inode); 1184 } 1185 1186 out: 1187 kfree(old_value); 1188 kfree(value); 1189 1190 return err; 1191 } 1192 1193 int ntfs_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry, 1194 struct posix_acl *acl, int type) 1195 { 1196 return ntfs_set_acl_ex(idmap, d_inode(dentry), acl, type, false); 1197 } 1198 1199 int ntfs_init_acl(const struct mnt_idmap *idmap, struct inode *inode, 1200 struct inode *dir) 1201 { 1202 struct posix_acl *default_acl, *acl; 1203 int err; 1204 1205 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); 1206 if (err) 1207 return err; 1208 1209 if (default_acl) { 1210 err = ntfs_set_acl_ex(idmap, inode, default_acl, 1211 ACL_TYPE_DEFAULT, true); 1212 posix_acl_release(default_acl); 1213 } else { 1214 inode->i_default_acl = NULL; 1215 } 1216 1217 if (acl) { 1218 if (!err) 1219 err = ntfs_set_acl_ex(idmap, inode, acl, 1220 ACL_TYPE_ACCESS, true); 1221 posix_acl_release(acl); 1222 } else { 1223 inode->i_acl = NULL; 1224 } 1225 1226 return err; 1227 } 1228 #endif 1229