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 ((new_aflags & ATTR_IS_COMPRESSED) && ni->vol->cluster_size > 4096) { 725 err = -EOPNOTSUPP; 726 goto err_out; 727 } 728 729 if (!a->non_resident) { 730 if (!(new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED))) { 731 err = 0; 732 goto err_out; 733 } 734 735 if (le32_to_cpu(a->data.resident.value_length)) { 736 pr_err("Can't change sparse/compressed for non-empty file"); 737 err = -EOPNOTSUPP; 738 goto err_out; 739 } 740 741 err = ntfs_attr_make_non_resident(ni, 0); 742 if (err) 743 goto err_out; 744 745 ntfs_attr_reinit_search_ctx(ctx); 746 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 747 CASE_SENSITIVE, 748 0, NULL, 0, ctx); 749 if (err) { 750 err = -EINVAL; 751 goto err_out; 752 } 753 a = ctx->attr; 754 } else { 755 if (a->data.non_resident.data_size) { 756 pr_err("Can't change sparsed/compressed for non-empty file"); 757 err = -EOPNOTSUPP; 758 goto err_out; 759 } 760 } 761 762 old_name_ofs = le16_to_cpu(a->name_offset); 763 old_mp_ofs = le16_to_cpu(a->data.non_resident.mapping_pairs_offset); 764 765 if (new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) 766 name_ofs = (offsetof(struct attr_record, 767 data.non_resident.compressed_size) + 768 sizeof(a->data.non_resident.compressed_size) + 7) & ~7; 769 else 770 name_ofs = (offsetof(struct attr_record, 771 data.non_resident.compressed_size) + 7) & ~7; 772 773 mp_size = ntfs_get_size_for_mapping_pairs(ni->vol, ni->runlist.rl, 0, -1, -1); 774 if (unlikely(mp_size < 0)) { 775 err = mp_size; 776 ntfs_debug("Failed to get size for mapping pairs array, error code %i.\n", err); 777 goto err_out; 778 } 779 780 mp_ofs = (name_ofs + a->name_length * sizeof(__le16) + 7) & ~7; 781 arec_size = (mp_ofs + mp_size + 7) & ~7; 782 old_arec_size = le32_to_cpu(a->length); 783 784 /* 785 * Move payloads before shrinking the record. Otherwise resizing moves 786 * the following attribute over the old payload before it can be copied. 787 * 788 * When offsets increase, move mapping_pairs first to avoid name 789 * overwriting the start of mapping_pairs. 790 */ 791 if (arec_size < old_arec_size) { 792 if (name_ofs > old_name_ofs) { 793 /* Payload offsets increased: move mapping pairs first. */ 794 if (mp_ofs != old_mp_ofs) 795 memmove((u8 *)a + mp_ofs, 796 (u8 *)a + old_mp_ofs, 797 mp_size); 798 if (a->name_length && name_ofs != old_name_ofs) 799 memmove((u8 *)a + name_ofs, 800 (u8 *)a + old_name_ofs, 801 a->name_length * 802 sizeof(__le16)); 803 } else { 804 /* Payload offsets decreased or unchanged: move name first. */ 805 if (a->name_length && name_ofs != old_name_ofs) 806 memmove((u8 *)a + name_ofs, 807 (u8 *)a + old_name_ofs, 808 a->name_length * 809 sizeof(__le16)); 810 if (mp_ofs != old_mp_ofs) 811 memmove((u8 *)a + mp_ofs, 812 (u8 *)a + old_mp_ofs, 813 mp_size); 814 } 815 } 816 817 err = ntfs_attr_record_resize(ctx->mrec, a, arec_size); 818 if (unlikely(err)) 819 goto err_out; 820 821 /* 822 * When compressed/sparse state changes, the non-resident header grows or 823 * shrinks by the compressed_size field. Update the in-record payload layout 824 * to match the new offsets before exposing the new mapping_pairs_offset. 825 */ 826 if (arec_size > old_arec_size) { 827 if (mp_ofs != old_mp_ofs) 828 memmove((u8 *)a + mp_ofs, (u8 *)a + old_mp_ofs, mp_size); 829 if (a->name_length) 830 memmove((u8 *)a + name_ofs, (u8 *)a + old_name_ofs, 831 a->name_length * sizeof(__le16)); 832 } 833 834 if (new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) { 835 a->data.non_resident.compression_unit = 0; 836 if (new_aflags & ATTR_IS_COMPRESSED || ni->vol->major_ver < 3) 837 a->data.non_resident.compression_unit = 4; 838 a->data.non_resident.compressed_size = 0; 839 ni->itype.compressed.size = 0; 840 if (a->data.non_resident.compression_unit) { 841 ni->itype.compressed.block_size = 1U << 842 (a->data.non_resident.compression_unit + 843 ni->vol->cluster_size_bits); 844 ni->itype.compressed.block_size_bits = 845 ffs(ni->itype.compressed.block_size) - 846 1; 847 ni->itype.compressed.block_clusters = 1U << 848 a->data.non_resident.compression_unit; 849 } else { 850 ni->itype.compressed.block_size = 0; 851 ni->itype.compressed.block_size_bits = 0; 852 ni->itype.compressed.block_clusters = 0; 853 } 854 } else { 855 a->data.non_resident.compression_unit = 0; 856 } 857 858 a->name_offset = cpu_to_le16(name_ofs); 859 a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs); 860 861 a->flags = new_aflags; 862 863 if (new_aflags & ATTR_IS_SPARSE) { 864 NInoSetSparse(ni); 865 ni->flags |= FILE_ATTR_SPARSE_FILE; 866 } else { 867 NInoClearSparse(ni); 868 ni->flags &= ~FILE_ATTR_SPARSE_FILE; 869 } 870 871 if (new_aflags & ATTR_IS_COMPRESSED) { 872 NInoSetCompressed(ni); 873 ni->flags |= FILE_ATTR_COMPRESSED; 874 } else { 875 NInoClearCompressed(ni); 876 ni->flags &= ~FILE_ATTR_COMPRESSED; 877 } 878 879 mark_mft_record_dirty(ctx->ntfs_ino); 880 err_out: 881 if (ctx) 882 ntfs_attr_put_search_ctx(ctx); 883 unmap_mft_record(ni); 884 return err; 885 } 886 887 static bool ntfs_is_reserved_lxattr(const char *name) 888 { 889 return !strcmp(name, "$LXUID") || !strcmp(name, "$LXGID") || 890 !strcmp(name, "$LXMOD") || !strcmp(name, "$LXDEV"); 891 } 892 893 static int ntfs_validate_fattr(struct ntfs_inode *ni, __le32 fattr) 894 { 895 const __le32 wof_flags = FILE_ATTR_SPARSE_FILE | 896 FILE_ATTR_REPARSE_POINT; 897 898 if (NInoWofCompressed(ni) && ((ni->flags ^ fattr) & wof_flags)) 899 return -EPERM; 900 901 return 0; 902 } 903 904 static int ntfs_setxattr(const struct xattr_handler *handler, 905 const struct mnt_idmap *idmap, struct dentry *unused, 906 struct inode *inode, const char *name, const void *value, 907 size_t size, int flags) 908 { 909 struct ntfs_inode *ni = NTFS_I(inode); 910 int err; 911 __le32 fattr; 912 913 if (ntfs_inode_is_named_stream(ni)) 914 return -EOPNOTSUPP; 915 if (NVolShutdown(ni->vol)) 916 return -EIO; 917 918 /* Only fileattr_set may change the flags and their cached state. */ 919 if (!strcmp(name, "$LINUXFLAGS")) 920 return -EPERM; 921 922 if (ntfs_is_reserved_lxattr(name) && !capable(CAP_SYS_ADMIN)) 923 return -EPERM; 924 925 if (!strcmp(name, SYSTEM_DOS_ATTRIB)) { 926 if (sizeof(u8) != size) { 927 err = -EINVAL; 928 goto out; 929 } 930 fattr = cpu_to_le32((le32_to_cpu(ni->flags) & ~0xffU) | 931 *(u8 *)value); 932 goto set_fattr; 933 } 934 935 if (!strcmp(name, SYSTEM_NTFS_ATTRIB) || 936 !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) { 937 if (size != sizeof(u32)) { 938 err = -EINVAL; 939 goto out; 940 } 941 if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) 942 fattr = cpu_to_le32(be32_to_cpu(*(__be32 *)value)); 943 else 944 fattr = cpu_to_le32(*(u32 *)value); 945 946 err = ntfs_validate_fattr(ni, fattr); 947 if (err) 948 goto out; 949 950 if (S_ISREG(inode->i_mode)) { 951 mutex_lock(&ni->mrec_lock); 952 err = ntfs_new_attr_flags(ni, fattr); 953 mutex_unlock(&ni->mrec_lock); 954 if (err) 955 goto out; 956 } 957 958 set_fattr: 959 if (S_ISDIR(inode->i_mode)) 960 fattr |= FILE_ATTR_DIRECTORY; 961 else 962 fattr &= ~FILE_ATTR_DIRECTORY; 963 964 err = ntfs_validate_fattr(ni, fattr); 965 if (err) 966 goto out; 967 968 if (ni->flags != fattr) { 969 ni->flags = fattr; 970 if (fattr & FILE_ATTR_READONLY) 971 inode->i_mode &= ~0222; 972 else 973 inode->i_mode |= 0222; 974 NInoSetFileNameDirty(ni); 975 mark_inode_dirty(inode); 976 } 977 err = 0; 978 goto out; 979 } 980 981 mutex_lock(&ni->mrec_lock); 982 err = ntfs_set_ea(inode, name, strlen(name), value, size, flags, NULL); 983 mutex_unlock(&ni->mrec_lock); 984 985 out: 986 if (!err) { 987 inode_set_ctime_current(inode); 988 mark_inode_dirty(inode); 989 } 990 return err; 991 } 992 993 static bool ntfs_xattr_user_list(struct dentry *dentry) 994 { 995 return true; 996 } 997 998 // clang-format off 999 static const struct xattr_handler ntfs_other_xattr_handler = { 1000 .prefix = "", 1001 .get = ntfs_getxattr, 1002 .set = ntfs_setxattr, 1003 .list = ntfs_xattr_user_list, 1004 }; 1005 1006 const struct xattr_handler * const ntfs_xattr_handlers[] = { 1007 &ntfs_other_xattr_handler, 1008 NULL, 1009 }; 1010 // clang-format on 1011 1012 #ifdef CONFIG_NTFS_FS_POSIX_ACL 1013 struct posix_acl *ntfs_get_acl(const struct mnt_idmap *idmap, struct dentry *dentry, 1014 int type) 1015 { 1016 struct inode *inode = d_inode(dentry); 1017 struct ntfs_inode *ni = NTFS_I(inode); 1018 const char *name; 1019 size_t name_len; 1020 struct posix_acl *acl; 1021 int err; 1022 void *buf; 1023 1024 buf = kmalloc(PATH_MAX, GFP_KERNEL); 1025 if (!buf) 1026 return ERR_PTR(-ENOMEM); 1027 1028 /* Possible values of 'type' was already checked above. */ 1029 if (type == ACL_TYPE_ACCESS) { 1030 name = XATTR_NAME_POSIX_ACL_ACCESS; 1031 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1; 1032 } else { 1033 name = XATTR_NAME_POSIX_ACL_DEFAULT; 1034 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1; 1035 } 1036 1037 mutex_lock(&ni->mrec_lock); 1038 err = ntfs_get_ea(inode, name, name_len, buf, PATH_MAX); 1039 mutex_unlock(&ni->mrec_lock); 1040 1041 /* Translate extended attribute to acl. */ 1042 if (err >= 0) 1043 acl = posix_acl_from_xattr(&init_user_ns, buf, err); 1044 else if (err == -ENODATA) 1045 acl = NULL; 1046 else 1047 acl = ERR_PTR(err); 1048 1049 if (!IS_ERR(acl)) 1050 set_cached_acl(inode, type, acl); 1051 1052 kfree(buf); 1053 1054 return acl; 1055 } 1056 1057 static noinline int ntfs_set_acl_ex(const struct mnt_idmap *idmap, 1058 struct inode *inode, struct posix_acl *acl, 1059 int type, bool init_acl) 1060 { 1061 const char *name; 1062 size_t size, name_len; 1063 void *value, *old_value = NULL; 1064 int err, old_len = -ENODATA; 1065 bool acl_committed = false; 1066 int flags; 1067 umode_t mode; 1068 1069 if (S_ISLNK(inode->i_mode)) 1070 return -EOPNOTSUPP; 1071 1072 mode = inode->i_mode; 1073 switch (type) { 1074 case ACL_TYPE_ACCESS: 1075 /* Do not change i_mode if we are in init_acl */ 1076 if (acl && !init_acl) { 1077 err = posix_acl_update_mode(idmap, inode, &mode, &acl); 1078 if (err) 1079 return err; 1080 } 1081 name = XATTR_NAME_POSIX_ACL_ACCESS; 1082 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1; 1083 break; 1084 1085 case ACL_TYPE_DEFAULT: 1086 if (!S_ISDIR(inode->i_mode)) 1087 return acl ? -EACCES : 0; 1088 name = XATTR_NAME_POSIX_ACL_DEFAULT; 1089 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1; 1090 break; 1091 1092 default: 1093 return -EINVAL; 1094 } 1095 1096 if (!acl) { 1097 /* Remove xattr if it can be presented via mode. */ 1098 size = 0; 1099 value = NULL; 1100 flags = XATTR_REPLACE; 1101 } else { 1102 value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_NOFS); 1103 if (!value) 1104 return -ENOMEM; 1105 flags = 0; 1106 } 1107 1108 mutex_lock(&NTFS_I(inode)->mrec_lock); 1109 if (!init_acl) { 1110 /* Save the old xattr for rollback. */ 1111 old_len = ntfs_get_ea(inode, name, name_len, NULL, 0); 1112 if (old_len >= 0) { 1113 /* kmalloc(0) returns the non-NULL ZERO_SIZE_PTR. */ 1114 old_value = kmalloc(old_len, GFP_NOFS); 1115 if (!old_value) { 1116 err = -ENOMEM; 1117 goto out_unlock; 1118 } 1119 err = ntfs_get_ea(inode, name, name_len, 1120 old_value, old_len); 1121 if (err != old_len) { 1122 kfree(old_value); 1123 old_value = NULL; 1124 if (err >= 0) 1125 err = -EIO; 1126 goto out_unlock; 1127 } 1128 } else if (old_len != -ENODATA) { 1129 err = old_len; 1130 goto out_unlock; 1131 } 1132 } 1133 err = ntfs_set_ea(inode, name, name_len, value, size, flags, NULL); 1134 if (!err) 1135 acl_committed = true; 1136 out_unlock: 1137 mutex_unlock(&NTFS_I(inode)->mrec_lock); 1138 if (err == -ENODATA && !size) 1139 err = 0; /* Removing non existed xattr. */ 1140 if (!err) { 1141 __le16 ea_size = 0; 1142 umode_t old_mode = inode->i_mode; 1143 1144 inode->i_mode = mode; 1145 mutex_lock(&NTFS_I(inode)->mrec_lock); 1146 err = ntfs_ea_set_wsl_inode(inode, 0, &ea_size, NTFS_EA_MODE); 1147 if (err) { 1148 if (acl_committed) { 1149 int undo_err; 1150 1151 if (old_len >= 0) { 1152 /* 1153 * A removed or zero-length xattr must 1154 * be recreated without XATTR_REPLACE. 1155 */ 1156 int undo_flags = acl && old_len ? 1157 XATTR_REPLACE : 0; 1158 1159 undo_err = ntfs_set_ea(inode, name, 1160 name_len, old_value, 1161 old_len, undo_flags, 1162 NULL); 1163 } else { 1164 undo_err = ntfs_set_ea(inode, name, 1165 name_len, NULL, 0, 1166 XATTR_REPLACE, NULL); 1167 } 1168 if (undo_err) { 1169 /* 1170 * Record a failed rollback so the next 1171 * persistence point marks the volume 1172 * dirty. Run chkdsk. 1173 */ 1174 NVolSetErrors(NTFS_I(inode)->vol); 1175 ntfs_error(inode->i_sb, 1176 "Failed to undo the ACL xattr: %d", 1177 undo_err); 1178 } 1179 } 1180 mutex_unlock(&NTFS_I(inode)->mrec_lock); 1181 inode->i_mode = old_mode; 1182 goto out; 1183 } 1184 mutex_unlock(&NTFS_I(inode)->mrec_lock); 1185 1186 set_cached_acl(inode, type, acl); 1187 inode_set_ctime_current(inode); 1188 mark_inode_dirty(inode); 1189 } 1190 1191 out: 1192 kfree(old_value); 1193 kfree(value); 1194 1195 return err; 1196 } 1197 1198 int ntfs_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry, 1199 struct posix_acl *acl, int type) 1200 { 1201 return ntfs_set_acl_ex(idmap, d_inode(dentry), acl, type, false); 1202 } 1203 1204 int ntfs_init_acl(const struct mnt_idmap *idmap, struct inode *inode, 1205 struct inode *dir) 1206 { 1207 struct posix_acl *default_acl, *acl; 1208 int err; 1209 1210 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); 1211 if (err) 1212 return err; 1213 1214 if (default_acl) { 1215 err = ntfs_set_acl_ex(idmap, inode, default_acl, 1216 ACL_TYPE_DEFAULT, true); 1217 posix_acl_release(default_acl); 1218 } else { 1219 inode->i_default_acl = NULL; 1220 } 1221 1222 if (acl) { 1223 if (!err) 1224 err = ntfs_set_acl_ex(idmap, inode, acl, 1225 ACL_TYPE_ACCESS, true); 1226 posix_acl_release(acl); 1227 } else { 1228 inode->i_acl = NULL; 1229 } 1230 1231 return err; 1232 } 1233 #endif 1234