1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * 4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved. 5 * 6 * Directory handling functions for NTFS-based filesystems. 7 * 8 */ 9 10 #include <linux/fs.h> 11 #include <linux/nls.h> 12 13 #include "debug.h" 14 #include "ntfs.h" 15 #include "ntfs_fs.h" 16 17 /* Convert little endian UTF-16 to NLS string. */ 18 int ntfs_utf16_to_nls(struct ntfs_sb_info *sbi, const __le16 *name, u32 len, 19 u8 *buf, int buf_len) 20 { 21 int ret, warn; 22 u8 *op; 23 struct nls_table *nls = sbi->options->nls; 24 25 static_assert(sizeof(wchar_t) == sizeof(__le16)); 26 27 if (!nls) { 28 /* UTF-16 -> UTF-8 */ 29 ret = utf16s_to_utf8s((wchar_t *)name, len, UTF16_LITTLE_ENDIAN, 30 buf, buf_len); 31 buf[ret] = '\0'; 32 return ret; 33 } 34 35 op = buf; 36 warn = 0; 37 38 while (len--) { 39 u16 ec; 40 int charlen; 41 char dump[5]; 42 43 if (buf_len < NLS_MAX_CHARSET_SIZE) { 44 ntfs_warn(sbi->sb, 45 "filename was truncated while converting."); 46 break; 47 } 48 49 ec = le16_to_cpu(*name++); 50 charlen = nls->uni2char(ec, op, buf_len); 51 52 if (charlen > 0) { 53 op += charlen; 54 buf_len -= charlen; 55 continue; 56 } 57 58 *op++ = '_'; 59 buf_len -= 1; 60 if (warn) 61 continue; 62 63 warn = 1; 64 hex_byte_pack(&dump[0], ec >> 8); 65 hex_byte_pack(&dump[2], ec); 66 dump[4] = 0; 67 68 ntfs_err(sbi->sb, "failed to convert \"%s\" to %s", dump, 69 nls->charset); 70 } 71 72 *op = '\0'; 73 return op - buf; 74 } 75 76 // clang-format off 77 #define PLANE_SIZE 0x00010000 78 79 #define SURROGATE_PAIR 0x0000d800 80 #define SURROGATE_LOW 0x00000400 81 #define SURROGATE_BITS 0x000003ff 82 // clang-format on 83 84 /* 85 * put_utf16 - Modified version of put_utf16 from fs/nls/nls_base.c 86 * 87 * Function is sparse warnings free. 88 */ 89 static inline void put_utf16(wchar_t *s, unsigned int c, 90 enum utf16_endian endian) 91 { 92 static_assert(sizeof(wchar_t) == sizeof(__le16)); 93 static_assert(sizeof(wchar_t) == sizeof(__be16)); 94 95 switch (endian) { 96 default: 97 *s = (wchar_t)c; 98 break; 99 case UTF16_LITTLE_ENDIAN: 100 *(__le16 *)s = __cpu_to_le16(c); 101 break; 102 case UTF16_BIG_ENDIAN: 103 *(__be16 *)s = __cpu_to_be16(c); 104 break; 105 } 106 } 107 108 /* 109 * _utf8s_to_utf16s 110 * 111 * Modified version of 'utf8s_to_utf16s' allows to 112 * detect -ENAMETOOLONG without writing out of expected maximum. 113 */ 114 static int _utf8s_to_utf16s(const u8 *s, int inlen, enum utf16_endian endian, 115 wchar_t *pwcs, int maxout) 116 { 117 u16 *op; 118 int size; 119 unicode_t u; 120 121 op = pwcs; 122 while (inlen > 0 && *s) { 123 if (*s & 0x80) { 124 size = utf8_to_utf32(s, inlen, &u); 125 if (size < 0) 126 return -EINVAL; 127 s += size; 128 inlen -= size; 129 130 if (u >= PLANE_SIZE) { 131 if (maxout < 2) 132 return -ENAMETOOLONG; 133 134 u -= PLANE_SIZE; 135 put_utf16(op++, 136 SURROGATE_PAIR | 137 ((u >> 10) & SURROGATE_BITS), 138 endian); 139 put_utf16(op++, 140 SURROGATE_PAIR | SURROGATE_LOW | 141 (u & SURROGATE_BITS), 142 endian); 143 maxout -= 2; 144 } else { 145 if (maxout < 1) 146 return -ENAMETOOLONG; 147 148 put_utf16(op++, u, endian); 149 maxout--; 150 } 151 } else { 152 if (maxout < 1) 153 return -ENAMETOOLONG; 154 155 put_utf16(op++, *s++, endian); 156 inlen--; 157 maxout--; 158 } 159 } 160 return op - pwcs; 161 } 162 163 /* 164 * ntfs_nls_to_utf16 - Convert input string to UTF-16. 165 * @name: Input name. 166 * @name_len: Input name length. 167 * @uni: Destination memory. 168 * @max_ulen: Destination memory. 169 * @endian: Endian of target UTF-16 string. 170 * 171 * This function is called: 172 * - to create NTFS name 173 * - to create symlink 174 * 175 * Return: UTF-16 string length or error (if negative). 176 */ 177 int ntfs_nls_to_utf16(struct ntfs_sb_info *sbi, const u8 *name, u32 name_len, 178 struct cpu_str *uni, u32 max_ulen, 179 enum utf16_endian endian) 180 { 181 int ret, slen; 182 const u8 *end; 183 struct nls_table *nls = sbi->options->nls; 184 u16 *uname = uni->name; 185 186 static_assert(sizeof(wchar_t) == sizeof(u16)); 187 188 if (!nls) { 189 /* utf8 -> utf16 */ 190 ret = _utf8s_to_utf16s(name, name_len, endian, uname, max_ulen); 191 uni->len = ret; 192 return ret; 193 } 194 195 for (ret = 0, end = name + name_len; name < end; ret++, name += slen) { 196 if (ret >= max_ulen) 197 return -ENAMETOOLONG; 198 199 slen = nls->char2uni(name, end - name, uname + ret); 200 if (!slen) 201 return -EINVAL; 202 if (slen < 0) 203 return slen; 204 } 205 206 #ifdef __BIG_ENDIAN 207 if (endian == UTF16_LITTLE_ENDIAN) { 208 int i = ret; 209 210 while (i--) { 211 __cpu_to_le16s(uname); 212 uname++; 213 } 214 } 215 #else 216 if (endian == UTF16_BIG_ENDIAN) { 217 int i = ret; 218 219 while (i--) { 220 __cpu_to_be16s(uname); 221 uname++; 222 } 223 } 224 #endif 225 226 uni->len = ret; 227 return ret; 228 } 229 230 /* 231 * dir_search_u - Helper function. 232 */ 233 struct inode *dir_search_u(struct inode *dir, const struct cpu_str *uni, 234 struct ntfs_fnd *fnd) 235 { 236 int err = 0; 237 struct super_block *sb = dir->i_sb; 238 struct ntfs_sb_info *sbi = sb->s_fs_info; 239 struct ntfs_inode *ni = ntfs_i(dir); 240 struct NTFS_DE *e; 241 int diff; 242 struct inode *inode = NULL; 243 struct ntfs_fnd *fnd_a = NULL; 244 245 if (!fnd) { 246 fnd_a = fnd_get(); 247 if (!fnd_a) { 248 err = -ENOMEM; 249 goto out; 250 } 251 fnd = fnd_a; 252 } 253 254 err = indx_find(&ni->dir, ni, NULL, uni, 0, sbi, &diff, &e, fnd); 255 256 if (err) 257 goto out; 258 259 if (diff) { 260 err = -ENOENT; 261 goto out; 262 } 263 264 inode = ntfs_iget5(sb, &e->ref, uni); 265 if (!IS_ERR(inode) && is_bad_inode(inode)) { 266 iput(inode); 267 err = -EINVAL; 268 } 269 out: 270 fnd_put(fnd_a); 271 272 return err == -ENOENT ? NULL : err ? ERR_PTR(err) : inode; 273 } 274 275 /* 276 * returns false if 'ctx' if full 277 */ 278 static inline bool ntfs_dir_emit(struct ntfs_sb_info *sbi, 279 struct ntfs_inode *ni, const struct NTFS_DE *e, 280 u8 *name, struct dir_context *ctx) 281 { 282 const struct ATTR_FILE_NAME *fname; 283 unsigned long ino; 284 int name_len; 285 u32 dt_type; 286 287 fname = Add2Ptr(e, sizeof(struct NTFS_DE)); 288 289 if (fname->type == FILE_NAME_DOS) 290 return true; 291 292 if (!mi_is_ref(&ni->mi, &fname->home)) 293 return true; 294 295 ino = ino_get(&e->ref); 296 297 if (ino == MFT_REC_ROOT) 298 return true; 299 300 /* Skip meta files. Unless option to show metafiles is set. */ 301 if (!sbi->options->showmeta && ntfs_is_meta_file(sbi, ino)) 302 return true; 303 304 if (sbi->options->nohidden && (fname->dup.fa & FILE_ATTRIBUTE_HIDDEN)) 305 return true; 306 307 if (fname->name_len + sizeof(struct NTFS_DE) > le16_to_cpu(e->size)) 308 return true; 309 310 name_len = ntfs_utf16_to_nls(sbi, fname->name, fname->name_len, name, 311 PATH_MAX); 312 if (name_len <= 0) { 313 ntfs_warn(sbi->sb, "failed to convert name for inode %lx.", 314 ino); 315 return true; 316 } 317 318 /* 319 * NTFS: symlinks are "dir + reparse" or "file + reparse" 320 * Unfortunately reparse attribute is used for many purposes (several dozens). 321 * It is not possible here to know is this name symlink or not. 322 * To get exactly the type of name we should to open inode (read mft). 323 * getattr for opened file (fstat) correctly returns symlink. 324 */ 325 dt_type = (fname->dup.fa & FILE_ATTRIBUTE_DIRECTORY) ? DT_DIR : DT_REG; 326 327 /* 328 * It is not reliable to detect the type of name using duplicated information 329 * stored in parent directory. 330 * The only correct way to get the type of name - read MFT record and find ATTR_STD. 331 * The code below is not good idea. 332 * It does additional locks/reads just to get the type of name. 333 * Should we use additional mount option to enable branch below? 334 */ 335 if (fname->dup.extend_data && ino != ni->mi.rno) { 336 struct inode *inode = ntfs_iget5(sbi->sb, &e->ref, NULL); 337 if (!IS_ERR_OR_NULL(inode)) { 338 dt_type = fs_umode_to_dtype(inode->i_mode); 339 iput(inode); 340 } 341 } 342 343 return dir_emit(ctx, (s8 *)name, name_len, ino, dt_type); 344 } 345 346 /* 347 * ntfs_read_hdr - Helper function for ntfs_readdir(). 348 * 349 * returns 0 if ok. 350 * returns -EINVAL if directory is corrupted. 351 * returns +1 if 'ctx' is full. 352 */ 353 static int ntfs_read_hdr(struct ntfs_sb_info *sbi, struct ntfs_inode *ni, 354 const struct INDEX_HDR *hdr, u64 vbo, u64 pos, 355 u8 *name, struct dir_context *ctx) 356 { 357 const struct NTFS_DE *e; 358 u32 e_size; 359 u32 end = le32_to_cpu(hdr->used); 360 u32 off = le32_to_cpu(hdr->de_off); 361 362 for (;; off += e_size) { 363 if (off + sizeof(struct NTFS_DE) > end) 364 return -EINVAL; 365 366 e = Add2Ptr(hdr, off); 367 e_size = le16_to_cpu(e->size); 368 if (e_size < sizeof(struct NTFS_DE) || off + e_size > end) 369 return -EINVAL; 370 371 if (de_is_last(e)) 372 return 0; 373 374 /* Skip already enumerated. */ 375 if (vbo + off < pos) 376 continue; 377 378 if (le16_to_cpu(e->key_size) < SIZEOF_ATTRIBUTE_FILENAME) 379 return -EINVAL; 380 381 ctx->pos = vbo + off; 382 383 /* Submit the name to the filldir callback. */ 384 if (!ntfs_dir_emit(sbi, ni, e, name, ctx)) { 385 /* ctx is full. */ 386 return +1; 387 } 388 } 389 } 390 391 /* 392 * ntfs_readdir - file_operations::iterate_shared 393 * 394 * Use non sorted enumeration. 395 * We have an example of broken volume where sorted enumeration 396 * counts each name twice. 397 */ 398 static int ntfs_readdir(struct file *file, struct dir_context *ctx) 399 { 400 const struct INDEX_ROOT *root; 401 u64 vbo; 402 size_t bit; 403 loff_t eod; 404 int err = 0; 405 struct inode *dir = file_inode(file); 406 struct ntfs_inode *ni = ntfs_i(dir); 407 struct super_block *sb = dir->i_sb; 408 struct ntfs_sb_info *sbi = sb->s_fs_info; 409 loff_t i_size = i_size_read(dir); 410 u32 pos = ctx->pos; 411 u8 *name = NULL; 412 struct indx_node *node = NULL; 413 u8 index_bits = ni->dir.index_bits; 414 415 /* Name is a buffer of PATH_MAX length. */ 416 static_assert(NTFS_NAME_LEN * 4 < PATH_MAX); 417 418 eod = i_size + sbi->record_size; 419 420 if (pos >= eod) 421 return 0; 422 423 if (!dir_emit_dots(file, ctx)) 424 return 0; 425 426 /* Allocate PATH_MAX bytes. */ 427 name = __getname(); 428 if (!name) 429 return -ENOMEM; 430 431 if (!ni->mi_loaded && ni->attr_list.size) { 432 /* 433 * Directory inode is locked for read. 434 * Load all subrecords to avoid 'write' access to 'ni' during 435 * directory reading. 436 */ 437 ni_lock(ni); 438 if (!ni->mi_loaded && ni->attr_list.size) { 439 err = ni_load_all_mi(ni); 440 if (!err) 441 ni->mi_loaded = true; 442 } 443 ni_unlock(ni); 444 if (err) 445 goto out; 446 } 447 448 root = indx_get_root(&ni->dir, ni, NULL, NULL); 449 if (!root) { 450 err = -EINVAL; 451 goto out; 452 } 453 454 if (pos >= sbi->record_size) { 455 bit = (pos - sbi->record_size) >> index_bits; 456 } else { 457 err = ntfs_read_hdr(sbi, ni, &root->ihdr, 0, pos, name, ctx); 458 if (err) 459 goto out; 460 bit = 0; 461 } 462 463 if (!i_size) { 464 ctx->pos = eod; 465 goto out; 466 } 467 468 for (;;) { 469 vbo = (u64)bit << index_bits; 470 if (vbo >= i_size) { 471 ctx->pos = eod; 472 goto out; 473 } 474 475 err = indx_used_bit(&ni->dir, ni, &bit); 476 if (err) 477 goto out; 478 479 if (bit == MINUS_ONE_T) { 480 ctx->pos = eod; 481 goto out; 482 } 483 484 vbo = (u64)bit << index_bits; 485 if (vbo >= i_size) { 486 err = -EINVAL; 487 goto out; 488 } 489 490 err = indx_read(&ni->dir, ni, bit << ni->dir.idx2vbn_bits, 491 &node); 492 if (err) 493 goto out; 494 495 err = ntfs_read_hdr(sbi, ni, &node->index->ihdr, 496 vbo + sbi->record_size, pos, name, ctx); 497 if (err) 498 goto out; 499 500 bit += 1; 501 } 502 503 out: 504 505 __putname(name); 506 put_indx_node(node); 507 508 if (err == 1) { 509 /* 'ctx' is full. */ 510 err = 0; 511 } else if (err == -ENOENT) { 512 err = 0; 513 ctx->pos = pos; 514 } else if (err < 0) { 515 if (err == -EINVAL) 516 _ntfs_bad_inode(dir); 517 ctx->pos = eod; 518 } 519 520 return err; 521 } 522 523 static int ntfs_dir_count(struct inode *dir, bool *is_empty, size_t *dirs, 524 size_t *files) 525 { 526 int err = 0; 527 struct ntfs_inode *ni = ntfs_i(dir); 528 struct NTFS_DE *e = NULL; 529 struct INDEX_ROOT *root; 530 struct INDEX_HDR *hdr; 531 const struct ATTR_FILE_NAME *fname; 532 u32 e_size, off, end; 533 size_t drs = 0, fles = 0, bit = 0; 534 struct indx_node *node = NULL; 535 size_t max_indx = i_size_read(&ni->vfs_inode) >> ni->dir.index_bits; 536 537 if (is_empty) 538 *is_empty = true; 539 540 root = indx_get_root(&ni->dir, ni, NULL, NULL); 541 if (!root) 542 return -EINVAL; 543 544 hdr = &root->ihdr; 545 546 for (;;) { 547 end = le32_to_cpu(hdr->used); 548 off = le32_to_cpu(hdr->de_off); 549 550 for (; off + sizeof(struct NTFS_DE) <= end; off += e_size) { 551 e = Add2Ptr(hdr, off); 552 e_size = le16_to_cpu(e->size); 553 if (e_size < sizeof(struct NTFS_DE) || 554 off + e_size > end) { 555 /* Looks like corruption. */ 556 break; 557 } 558 559 if (de_is_last(e)) 560 break; 561 562 fname = de_get_fname(e); 563 if (!fname) 564 continue; 565 566 if (fname->type == FILE_NAME_DOS) 567 continue; 568 569 if (is_empty) { 570 *is_empty = false; 571 if (!dirs && !files) 572 goto out; 573 } 574 575 if (fname->dup.fa & FILE_ATTRIBUTE_DIRECTORY) 576 drs += 1; 577 else 578 fles += 1; 579 } 580 581 if (bit >= max_indx) 582 goto out; 583 584 err = indx_used_bit(&ni->dir, ni, &bit); 585 if (err) 586 goto out; 587 588 if (bit == MINUS_ONE_T) 589 goto out; 590 591 if (bit >= max_indx) 592 goto out; 593 594 err = indx_read(&ni->dir, ni, bit << ni->dir.idx2vbn_bits, 595 &node); 596 if (err) 597 goto out; 598 599 hdr = &node->index->ihdr; 600 bit += 1; 601 } 602 603 out: 604 put_indx_node(node); 605 if (dirs) 606 *dirs = drs; 607 if (files) 608 *files = fles; 609 610 return err; 611 } 612 613 bool dir_is_empty(struct inode *dir) 614 { 615 bool is_empty = false; 616 617 ntfs_dir_count(dir, &is_empty, NULL, NULL); 618 619 return is_empty; 620 } 621 622 // clang-format off 623 const struct file_operations ntfs_dir_operations = { 624 .llseek = generic_file_llseek, 625 .read = generic_read_dir, 626 .iterate_shared = ntfs_readdir, 627 .fsync = generic_file_fsync, 628 .open = ntfs_file_open, 629 .unlocked_ioctl = ntfs_ioctl, 630 #ifdef CONFIG_COMPAT 631 .compat_ioctl = ntfs_compat_ioctl, 632 #endif 633 }; 634 635 #if IS_ENABLED(CONFIG_NTFS_FS) 636 const struct file_operations ntfs_legacy_dir_operations = { 637 .llseek = generic_file_llseek, 638 .read = generic_read_dir, 639 .iterate_shared = ntfs_readdir, 640 .open = ntfs_file_open, 641 }; 642 #endif 643 // clang-format on 644