1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
|
/*
* Copyright (C) 2019 Ernesto A. Fernández <ernesto.mnd.fernandez@gmail.com>
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <apfs/raw.h>
#include <apfs/types.h>
#include "apfsck.h"
#include "compress.h"
#include "dir.h"
#include "extents.h"
#include "htable.h"
#include "inode.h"
#include "key.h"
#include "super.h"
/**
* xbmap_set - Set an xfield type in the xfield bitmap
* @bmap: the xfield bitmap
* @type: the extended field type
*/
static inline void xbmap_set(u16 *bmap, u8 type)
{
*bmap |= 1 << type;
}
/**
* xbmap_test - Test if an xfield type is present in the xfield bitmap
* @bmap: the xfield bitmap
* @type: the extended field type
*/
static inline bool xbmap_test(u16 bmap, u8 type)
{
return bmap & (1 << type);
}
/**
* check_finder_info - Check the inode flag that reports finder info
* @inode: the inode to check
*
* I've encountered some old images that put the finder info in a xattr instead
* of an xfield, but they still set the inode flag, so this check has to be done
* after all xattrs have been parsed.
*/
static void check_finder_info(struct inode *inode)
{
bool xfield = xbmap_test(inode->i_xfield_bmap, APFS_INO_EXT_TYPE_FINDER_INFO);
bool xattr = inode->i_xattr_bmap & XATTR_BMAP_FINDER_INFO;
bool flag = inode->i_flags & APFS_INODE_HAS_FINDER_INFO;
if (xfield && xattr)
report("Inode record", "duplicated finder info.");
if (flag != (xfield || xattr))
report("Inode record", "wrong setting for finder info flag.");
}
/**
* check_inode_stats - Verify the stats gathered by the fsck vs the metadata
* @inode: inode structure to check
*/
static void check_inode_stats(struct inode *inode)
{
struct dstream *dstream;
/* The inodes must be freed before the dstreams */
assert(vsb->v_dstream_table);
if ((inode->i_mode & S_IFMT) == S_IFDIR) {
if (inode->i_link_count != 1)
report("Inode record", "directory has hard links.");
if (inode->i_nchildren != inode->i_child_count)
report("Inode record", "wrong directory child count.");
if (inode->i_first_parent == APFS_PRIV_DIR_INO_NUM && inode->i_nchildren != 0)
report("Orphan directory", "has children of its own.");
} else if (inode->i_first_parent == APFS_PRIV_DIR_INO_NUM) {
if (inode->i_link_count != 1)
report("Orphan inode", "not really orphaned.");
if (inode->i_nlink != 0)
report("Orphan inode", "has a link count.");
} else {
if (inode->i_nlink != inode->i_link_count)
report("Inode record", "wrong link count.");
}
dstream = inode->i_dstream;
if (dstream) {
if (dstream->d_sparse_bytes != inode->i_sparse_bytes)
report("Inode record", "wrong count of sparse bytes.");
if (dstream->d_refcnt > 1 && !(inode->i_flags & (APFS_INODE_WAS_CLONED | APFS_INODE_WAS_EVER_CLONED)))
report("Inode record", "wrong flags for cloned inode.");
if (inode->i_first_parent == APFS_PRIV_DIR_INO_NUM)
dstream->d_orphan = true;
} else {
if (inode->i_sparse_bytes)
report("Inode record", "sparse bytes without dstream.");
}
if ((bool)(inode->i_xattr_bmap & XATTR_BMAP_SYMLINK) !=
(bool)((inode->i_mode & S_IFMT) == S_IFLNK))
report("Inode record",
"symlink inode should come with target xattr.");
if ((bool)(inode->i_xattr_bmap & XATTR_BMAP_RSRC_FORK) !=
(bool)(inode->i_flags & APFS_INODE_HAS_RSRC_FORK))
report("Inode record", "wrong flag for resource fork.");
if ((bool)(inode->i_xattr_bmap & XATTR_BMAP_SECURITY) !=
(bool)(inode->i_flags & APFS_INODE_HAS_SECURITY_EA))
report("Inode record", "wrong flag for access control list.");
if ((bool)(inode->i_purg_name) != (bool)(inode->i_flags & APFS_INODE_IS_PURGEABLE))
report("Inode record", "wrong purgeability flag.");
check_finder_info(inode);
}
static void check_purgeable_name(struct inode *inode)
{
const char *format = "0x%llx-0x%llx:%u";
char *buf;
int buflen;
struct dstream *dstream = NULL;
unsigned long long filesize;
dstream = inode->i_dstream;
filesize = dstream ? dstream->d_bytes : 0;
buflen = snprintf(NULL, 0, format, filesize, (unsigned long long)inode->i_ino, (unsigned int)inode->i_owner);
if (buflen < 0)
system_error();
buf = calloc(1, ++buflen);
if (!buf)
system_error();
buflen = snprintf(buf, buflen, format, filesize, (unsigned long long)inode->i_ino, (unsigned int)inode->i_owner);
if (buflen < 0)
system_error();
if (strcmp(inode->i_purg_name, buf) != 0)
report("Purgeable inode", "wrong name for purgeable dentry.");
free(buf);
}
/**
* free_inode_names - Free all data on an inode's names
* @inode: inode to free
*
* Frees the primary name and all sibling links, but not before running a few
* remaining consistency checks.
*/
static void free_inode_names(struct inode *inode)
{
struct sibling *current = inode->i_siblings;
struct sibling *next;
u32 count = 0;
if (!inode->i_name) /* Oddly, this seems to be always required */
report("Inode record", "no name for primary link.");
if (!inode->i_first_name)
report("Catalog", "inode with no dentries.");
if (inode->i_flags & APFS_INODE_ACTIVE_FILE_TRIMMED) {
/* No idea if any of this is actually required */
if (strcmp(inode->i_name, ".overprovisioning_file"))
report("Overprovisioning file", "wrong name.");
if (inode->i_link_count != 1)
report("Overprovisioning file", "has hard links.");
}
if (inode->i_purg_name) {
check_purgeable_name(inode);
free(inode->i_purg_name);
inode->i_purg_name = NULL;
}
if (current) {
/* Primary link has lowest id, so it comes first in the list */
if (strcmp(inode->i_name, (char *)current->s_name))
report("Inode record", "wrong name for primary link.");
if (inode->i_parent_id != current->s_parent_ino)
report("Inode record", "bad parent for primary link.");
} else if (inode->i_first_parent != APFS_PRIV_DIR_INO_NUM) {
/*
* No siblings, so the primary link is the first and only.
* Files moved to the private directory preserve their original
* name and parent_id, so there's nothing to check.
*/
if (strcmp(inode->i_name, inode->i_first_name))
report("Inode record", "wrong name for only link.");
if (inode->i_parent_id != inode->i_first_parent)
report("Inode record", "bad parent for only link.");
}
free(inode->i_name);
inode->i_name = NULL;
free(inode->i_first_name);
inode->i_first_name = NULL;
while (current) {
struct listed_cnid *cnid;
/* Put all filesystem object ids in a list to check for reuse */
cnid = get_listed_cnid(current->s_id);
cnid_set_state_flag(cnid, CNID_IN_SIBLING_LINK);
if (!current->s_checked)
report("Catalog", "orphaned or missing sibling link.");
if (!current->s_mapped)
report("Catalog", "no sibling map for link.");
next = current->s_next;
free(current->s_name);
free(current);
current = next;
++count;
}
if (inode->i_first_parent == APFS_PRIV_DIR_INO_NUM) {
if (count != 0)
report("Orphan inode", "has sibling links.");
return;
}
/* Inodes with one link can have a sibling record, but don't need it */
if (inode->i_link_count == 1 && count == 0)
return;
if (count != inode->i_link_count)
report("Inode record",
"link count inconsistent with sibling records.");
}
/**
* free_inode - Free an inode structure after performing some final checks
* @entry: the entry to free
*/
static void free_inode(struct htable_entry *entry)
{
struct inode *inode = (struct inode *)entry;
struct compress *compress = inode->i_compress;
struct listed_cnid *cnid;
/* All of these must still be around for the inodes to access */
assert(vsb->v_cnid_table);
assert(vsb->v_dirstat_table);
assert(vsb->v_dstream_table);
/* To check for reuse, put all filesystem object ids in a list */
cnid = get_listed_cnid(inode->i_ino);
cnid_set_state_flag(cnid, CNID_IN_INODE);
if (compress) {
apfs_compress_open(compress);
apfs_compress_check(compress);
verify_dstream_hashes(compress->rsrc_dstream, compress);
apfs_compress_close(compress);
free(compress->decmpfs);
free(compress);
inode->i_compress = NULL;
}
check_inode_stats(inode);
free_inode_names(inode);
free(entry);
}
static void collect_dirstats(struct htable_entry *entry)
{
struct inode *inode = (struct inode *)entry;
u16 filetype = inode->i_mode & S_IFMT;
struct dirstat *stat = inode->i_dirstat;
struct dirstat *parent_stat = NULL;
if (inode->i_parent_id >= APFS_MIN_USER_INO_NUM) {
parent_stat = get_inode(inode->i_parent_id)->i_dirstat;
if (stat && parent_stat && stat != parent_stat)
report("Inode record", "dirstat id differs from parent.");
}
if (inode->i_flags & APFS_INODE_MAINTAIN_DIR_STATS) {
assert(stat);
if (!stat->ds_seen)
report("Inode record", "missing a dirstats record.");
if (!(inode->i_flags & APFS_INODE_DIR_STATS_ORIGIN) && !parent_stat)
report("Directory statistics", "origin missing.");
stat->ds_child_count += inode->i_nchildren;
} else if (parent_stat) {
if (inode->i_flags & APFS_INODE_DIR_STATS_ORIGIN)
report_unknown("Nested dirstat origins.");
if (filetype == S_IFDIR)
report("Inode record", "should gather dir stats for ancestor.");
else if (inode->i_dstream)
parent_stat->ds_total_size += inode->i_dstream->d_size;
}
}
/**
* free_inode_table - Free the inode hash table and all its entries
* @table: table to free
*
* Also performs some consistency checks that can only be done after the whole
* catalog has been parsed.
*/
void free_inode_table(struct htable_entry **table)
{
/*
* Collect directory statistics, and check that descendant directories
* inherit the APFS_INODE_MAINTAIN_DIR_STATS flag.
*/
apply_on_htable(table, collect_dirstats);
free_htable(table, free_inode);
}
/**
* get_inode - Find or create an inode structure in the inode hash table
* @ino: inode number
*
* Returns the inode structure, after creating it if necessary.
*/
struct inode *get_inode(u64 ino)
{
struct htable_entry *entry;
entry = get_htable_entry(ino, sizeof(struct inode), vsb->v_inode_table);
return (struct inode *)entry;
}
/**
* read_sparse_bytes_xfield - Parse and check an xfield that counts sparse bytes
* @xval: pointer to the xfield value
* @len: remaining length of the inode value
* @inode: struct to receive the results
*
* Returns the length of the xfield value.
*/
static int read_sparse_bytes_xfield(char *xval, int len, struct inode *inode)
{
__le64 *sbytes;
if (len < 8)
report("Sparse bytes xfield", "doesn't fit in inode record.");
sbytes = (__le64 *)xval;
inode->i_sparse_bytes = le64_to_cpu(*sbytes);
return sizeof(*sbytes);
}
/**
* read_document_id_xfield - Parse and check a document id xfield
* @xval: pointer to the xfield value
* @len: remaining length of the inode value
* @inode: inode structure
*
* Returns the length of the xfield value.
*/
static int read_document_id_xfield(char *xval, int len, struct inode *inode)
{
__le32 *id_raw;
u32 id;
if (len < 4)
report("Document id xfield", "doesn't fit in inode record.");
id_raw = (__le32 *)xval;
id = le32_to_cpu(*id_raw);
if (id < APFS_MIN_DOC_ID)
report("Document id xfield", "invalid id in use.");
if (id >= vsb->v_next_doc_id)
report("Document id xfield", "free id in use.");
return sizeof(*id_raw);
}
/**
* read_rdev_xfield - Parse and check a device identifier xfield
* @xval: pointer to the xfield value
* @len: remaining length of the inode value
* @inode: struct to receive the results
*
* Returns the length of the xfield value.
*/
static int read_rdev_xfield(char *xval, int len, struct inode *inode)
{
u16 filetype = inode->i_mode & S_IFMT;
__le32 *rdev;
assert(filetype); /* Mode must be set before parsing xfields */
if (filetype != S_IFCHR && filetype != S_IFBLK)
report("Inode record", "not device but has device identifier.");
if (len < 4)
report("Device ID xfield", "doesn't fit in inode record.");
rdev = (__le32 *)xval;
inode->i_rdev = le32_to_cpu(*rdev);
return sizeof(*rdev);
}
/**
* read_name_xfield - Parse a name xfield and check its consistency
* @xval: pointer to the xfield value
* @len: remaining length of the inode value
* @inode: struct to receive the results
*
* Returns the length of the xfield value.
*/
static int read_name_xfield(char *xval, int len, struct inode *inode)
{
int xlen;
xlen = strnlen(xval, len - 1) + 1;
if (xval[xlen - 1] != 0)
report("Name xfield", "name with no null termination");
inode->i_name = malloc(xlen);
if (!inode->i_name)
system_error();
strcpy(inode->i_name, xval);
return xlen;
}
/**
* read_dstream_xfield - Parse a dstream xfield and check its consistency
* @xval: pointer to the xfield value
* @len: remaining length of the inode value
* @inode: struct to receive the results
*
* Returns the length of the xfield value.
*/
static int read_dstream_xfield(char *xval, int len, struct inode *inode)
{
struct apfs_dstream *dstream_raw;
struct dstream *dstream;
u64 size, alloced_size;
u64 crypid;
if ((inode->i_mode & S_IFMT) != S_IFREG)
report("Inode record", "has dstream but isn't a regular file.");
if (len < sizeof(*dstream_raw))
report("Dstream xfield", "doesn't fit in inode record.");
dstream_raw = (struct apfs_dstream *)xval;
size = le64_to_cpu(dstream_raw->size);
alloced_size = le64_to_cpu(dstream_raw->alloced_size);
crypid = le64_to_cpu(dstream_raw->default_crypto_id);
if (crypid && crypid != APFS_CRYPTO_SW_ID) {
/*
* I'm not yet sure how this crypto cloning thing is supposed
* to work, but it's very common (TODO).
*/
if (crypid == APFS_UNASSIGNED_CRYPTO_ID) {
if (!(inode->i_flags & APFS_INODE_WAS_CLONED))
report("Dstream xfield", "not a clone but has unassigned crypto.");
} else {
struct crypto_state *crypto = get_crypto_state(crypid);
++crypto->c_references;
if (inode->i_flags & APFS_INODE_ACTIVE_FILE_TRIMMED)
crypto->c_overprov = true;
}
}
dstream = get_dstream(inode->i_private_id);
if (dstream->d_references) {
/* A dstream structure for this id has already been seen */
if (dstream->d_obj_type != APFS_TYPE_INODE)
report("Dstream xfield", "shared by inode and xattr.");
if (dstream->d_size != size)
report("Dstream xfield",
"inconsistent size for stream.");
if (dstream->d_alloced_size != alloced_size)
report("Dstream xfield",
"inconsistent allocated size for stream.");
} else {
dstream->d_obj_type = APFS_TYPE_INODE;
dstream->d_size = size;
dstream->d_alloced_size = alloced_size;
}
dstream->d_xattr = false;
dstream->d_references++;
dstream->d_owner = dstream->d_id;
inode->i_dstream = dstream;
return sizeof(*dstream_raw);
}
/**
* read_dir_stats_xfield - Parse a dir stats xfield and check its consistency
* @xval: pointer to the xfield value
* @len: remaining length of the inode value
* @inode: struct to receive the results
*
* Returns the length of the xfield value.
*/
static int read_dir_stats_xfield(char *xval, int len, struct inode *inode)
{
u16 filetype = inode->i_mode & S_IFMT;
__le64 *oid = NULL;
struct dirstat *stats = NULL;
/* TODO: I'm yet to see a file with stats, this is probably wrong */
if (filetype != S_IFDIR)
report("Dir stats xfield", "inode is not a directory.");
if (len < sizeof(*oid))
report("Dir stats xfield", "doesn't fit in inode record.");
oid = (__le64 *)xval;
stats = inode->i_dirstat = get_dirstat(le64_to_cpu(*oid));
if (inode->i_flags & APFS_INODE_DIR_STATS_ORIGIN) {
/* TODO: this will be a problem for nested origins */
if (stats->ds_origin_seen)
report("Dir stats", "has two origins.");
stats->ds_origin_seen = true;
stats->ds_origin = inode->i_ino;
}
return sizeof(*oid);
}
static int read_purgeable_flags_xfield(char *xval, int len, struct inode *inode)
{
__le64 *flags;
if (len < sizeof(*flags))
report("Purgeable flags xfield", "doesn't fit in inode record.");
flags = (__le64 *)xval;
/*
* TODO: Figure out these flags. So far I have only seen them set to
* 0x10005, in a directory that was not purgeable itself, but had mostly
* purgeable entries.
*/
inode->i_purg_flags = le64_to_cpu(*flags);
return sizeof(*flags);
}
/**
* check_xfield_flags - Run common flag checks for all xfield types
* @flags: flags to check
*/
void check_xfield_flags(u8 flags)
{
if (flags & APFS_XF_RESERVED_4 || flags & APFS_XF_RESERVED_40 ||
flags & APFS_XF_RESERVED_80)
report("Inode xfield", "reserved flag in use.");
if (flags & APFS_XF_USER_FIELD && flags & APFS_XF_SYSTEM_FIELD)
report("Inode xfield", "created by both user and kernel.");
}
/**
* check_xfield_inode_flags - Check that xfields are consistent with inode flags
* @bmap: bitmap of xfield types seen in the inode
* @flags: inode flags
*
* This doesn't check the finder info flag, that happens when we free the inode.
*/
static void check_xfield_inode_flags(u16 bmap, u64 flags)
{
if (xbmap_test(bmap, APFS_INO_EXT_TYPE_DIR_STATS_KEY) !=
(bool)(flags & APFS_INODE_MAINTAIN_DIR_STATS))
report("Inode record", "wrong setting for dir stats flag.");
if (xbmap_test(bmap, APFS_INO_EXT_TYPE_SPARSE_BYTES) !=
(bool)(flags & APFS_INODE_IS_SPARSE))
report("Inode record", "wrong setting for sparse flag.");
if (xbmap_test(bmap, APFS_INO_EXT_TYPE_PURGEABLE_FLAGS) != (bool)(flags & APFS_INODE_HAS_PURGEABLE_FLAGS))
report("Inode record", "wrong setting for purgeable flags option.");
}
/**
* parse_inode_xfields - Parse and check an inode extended fields
* @xblob: pointer to the beginning of the xfields in the inode value
* @len: length of the xfields
* @inode: struct to receive the results
*
* Internal consistency of @key must be checked before calling this function.
*/
static void parse_inode_xfields(struct apfs_xf_blob *xblob, int len,
struct inode *inode)
{
u16 filetype = inode->i_mode & S_IFMT;
struct apfs_x_field *xfield;
u16 type_bitmap = 0;
char *xval;
int xcount;
int i;
if (len == 0) { /* No extended fields */
check_xfield_inode_flags(type_bitmap, inode->i_flags);
return;
}
len -= sizeof(*xblob);
if (len < 0)
report("Inode records", "no room for extended fields.");
xcount = le16_to_cpu(xblob->xf_num_exts);
if (!xcount)
report("Inode record", "xfield blob has no xfields.");
xfield = (struct apfs_x_field *)xblob->xf_data;
xval = (char *)xfield + xcount * sizeof(xfield[0]);
len -= xcount * sizeof(xfield[0]);
if (len < 0)
report("Inode record", "number of xfields cannot fit.");
/* The official reference seems to be wrong here */
if (le16_to_cpu(xblob->xf_used_data) != len)
report("Inode record", "value size incompatible with xfields.");
for (i = 0; i < le16_to_cpu(xblob->xf_num_exts); ++i) {
int xlen, xpad_len;
u8 xflags = xfield[i].x_flags;
check_xfield_flags(xflags);
switch (xfield[i].x_type) {
case APFS_INO_EXT_TYPE_FS_UUID:
xlen = 16;
report_unknown("UUID xfield");
break;
case APFS_INO_EXT_TYPE_PREV_FSIZE:
xlen = 8;
report_crash("Inode xfield");
if (xflags != 0)
report("Previous size xfield", "wrong flags.");
break;
case APFS_INO_EXT_TYPE_SNAP_XID:
xlen = 8;
report_unknown("Snapshot id xfield");
break;
case APFS_INO_EXT_TYPE_DELTA_TREE_OID:
xlen = 8;
report_unknown("Snapshot's extent delta list xfield");
break;
case APFS_INO_EXT_TYPE_SPARSE_BYTES:
xlen = read_sparse_bytes_xfield(xval, len, inode);
if (xflags != (APFS_XF_SYSTEM_FIELD |
APFS_XF_CHILDREN_INHERIT))
report("Sparse bytes xfield", "wrong flags.");
break;
case APFS_INO_EXT_TYPE_DOCUMENT_ID:
xlen = read_document_id_xfield(xval, len, inode);
report_unknown("Document id xfield");
break;
case APFS_INO_EXT_TYPE_FINDER_INFO:
xlen = 32;
report_unknown("Finder info xfield");
break;
case APFS_INO_EXT_TYPE_RDEV:
xlen = read_rdev_xfield(xval, len, inode);
break;
case APFS_INO_EXT_TYPE_NAME:
xlen = read_name_xfield(xval, len, inode);
if (xflags != APFS_XF_DO_NOT_COPY)
report("Name xfield", "wrong flags.");
break;
case APFS_INO_EXT_TYPE_DSTREAM:
xlen = read_dstream_xfield(xval, len, inode);
if (xflags != APFS_XF_SYSTEM_FIELD)
report("Data stream xfield", "wrong flags.");
break;
case APFS_INO_EXT_TYPE_DIR_STATS_KEY:
xlen = read_dir_stats_xfield(xval, len, inode);
if (xflags != (APFS_XF_SYSTEM_FIELD | APFS_XF_DO_NOT_COPY))
report("Dir stats xfield", "wrong flags.");
break;
case APFS_INO_EXT_TYPE_PURGEABLE_FLAGS:
xlen = read_purgeable_flags_xfield(xval, len, inode);
break;
case APFS_INO_EXT_TYPE_ORIG_SYNC_ROOT_ID:
xlen = 8;
report_unknown("Sync root id xfield");
break;
case APFS_INO_EXT_TYPE_RESERVED_6:
case APFS_INO_EXT_TYPE_RESERVED_9:
case APFS_INO_EXT_TYPE_RESERVED_12:
report("Inode xfield", "reserved type in use.");
break;
default:
report("Inode xfield", "invalid type.");
}
if (xbmap_test(type_bitmap, xfield[i].x_type))
report("Inode record", "two xfields of the same type.");
xbmap_set(&type_bitmap, xfield[i].x_type);
if (xlen != le16_to_cpu(xfield[i].x_size))
report("Inode xfield", "wrong size");
len -= xlen;
xval += xlen;
/* Attribute length is padded with zeroes to a multiple of 8 */
xpad_len = ROUND_UP(xlen, 8) - xlen;
len -= xpad_len;
if (len < 0)
report("Inode xfield", "does not fit in record value.");
for (; xpad_len; ++xval, --xpad_len)
if (*xval)
report("Inode xfield", "non-zero padding.");
}
if (len)
report("Inode record", "length of xfields does not add up.");
check_xfield_inode_flags(type_bitmap, inode->i_flags);
inode->i_xfield_bmap = type_bitmap;
if ((filetype == S_IFCHR || filetype == S_IFBLK) && !xbmap_test(type_bitmap, APFS_INO_EXT_TYPE_RDEV))
report("Inode record", "device file with no device ID.");
}
/**
* check_inode_internal_flags - Check basic consistency of inode flags
* @flags: the flags
*/
static void check_inode_internal_flags(u64 flags)
{
if ((flags & APFS_VALID_INTERNAL_INODE_FLAGS) != flags)
report("Inode record", "invalid flags in use.");
if ((flags & APFS_INODE_DIR_STATS_ORIGIN) &&
!(flags & APFS_INODE_MAINTAIN_DIR_STATS))
report("Inode record", "incompatible directory stats flags.");
if (flags & APFS_INODE_HAS_RSRC_FORK && flags & APFS_INODE_NO_RSRC_FORK)
report("Inode record", "incompatible resource fork flags.");
if (flags & APFS_INODE_BEING_TRUNCATED)
report_crash("Inode internal flags");
if (flags & APFS_INODE_PINNED_MASK) {
/*
* Preboot volume seems to be mostly pinned to main, even in
* non-fusion drives.
*/
if ((flags & APFS_INODE_PINNED_TO_TIER2) || apfs_volume_role() != APFS_VOL_ROLE_PREBOOT)
report_unknown("Fusion drive");
if ((flags & APFS_INODE_PINNED_TO_TIER2) && (flags & APFS_INODE_PINNED_TO_MAIN))
report("Inode record", "pinned to both tiers.");
}
if (flags & APFS_INODE_ALLOCATION_SPILLEDOVER)
report_unknown("Fusion drive");
if (flags & APFS_INODE_IS_APFS_PRIVATE)
report_unknown("Private implementation inode");
}
/**
* inos_valid_for_sysvol_in_group - Can a system vol in a group use these inos?
* @ino: inode number
* @parent_ino: parent inode number
*/
static bool inos_valid_for_sysvol_in_group(u64 ino, u64 parent_ino)
{
/*
* Reserved inode numbers in volume groups always seem to come
* from the data range, even if this contradicts the reference.
*/
if (ino < APFS_MIN_USER_INO_NUM && parent_ino < APFS_MIN_USER_INO_NUM)
return true;
if (ino < APFS_UNIFIED_ID_SPACE_MARK + APFS_MIN_USER_INO_NUM)
return false;
if (parent_ino < APFS_MIN_USER_INO_NUM)
return true;
if (parent_ino < APFS_UNIFIED_ID_SPACE_MARK + APFS_MIN_USER_INO_NUM)
return false;
return true;
}
/**
* check_inode_ids - Check that an inode id is consistent with its parent id
* @ino: inode number
* @parent_ino: parent inode number
*/
void check_inode_ids(u64 ino, u64 parent_ino)
{
if (ino >= vsb->v_next_obj_id || parent_ino >= vsb->v_next_obj_id)
report("Inode record", "free inode number in use.");
if (ino < APFS_MIN_USER_INO_NUM) {
switch (ino) {
case APFS_INVALID_INO_NUM:
case APFS_ROOT_DIR_PARENT:
case APFS_PURGEABLE_DIR_INO_NUM:
report("Inode record", "invalid inode number.");
case APFS_ROOT_DIR_INO_NUM:
case APFS_PRIV_DIR_INO_NUM:
case APFS_SNAP_DIR_INO_NUM:
/* All children of this fake parent? TODO: check this */
if (parent_ino != APFS_ROOT_DIR_PARENT)
report("Root inode record", "bad parent id");
break;
default:
report("Inode record", "reserved inode number.");
}
return;
}
if (parent_ino < APFS_MIN_USER_INO_NUM) {
switch (parent_ino) {
case APFS_INVALID_INO_NUM:
report("Inode record", "invalid parent inode number.");
case APFS_ROOT_DIR_PARENT:
report("Inode record", "root parent id for nonroot.");
case APFS_ROOT_DIR_INO_NUM:
case APFS_PRIV_DIR_INO_NUM:
case APFS_SNAP_DIR_INO_NUM:
case APFS_PURGEABLE_DIR_INO_NUM:
/* These are fine */
break;
default:
report("Inode record", "reserved parent inode number.");
}
}
if (apfs_is_data_volume_in_group()) {
if (ino >= APFS_UNIFIED_ID_SPACE_MARK || parent_ino >= APFS_UNIFIED_ID_SPACE_MARK)
report("Inode record", "bad number for data volume inode.");
} else if (apfs_is_system_volume_in_group()) {
if (!inos_valid_for_sysvol_in_group(ino, parent_ino))
report("Inode record", "bad number for system volume inode.");
}
}
/**
* parse_inode_record - Parse an inode record value and check for corruption
* @key: pointer to the raw key
* @val: pointer to the raw value
* @len: length of the raw value
*
* Internal consistency of @key must be checked before calling this function.
*/
void parse_inode_record(struct apfs_inode_key *key,
struct apfs_inode_val *val, int len)
{
struct inode *inode;
u16 mode, filetype;
u32 def_prot_class, bsd_flags;
if (len < sizeof(*val))
report("Inode record", "value is too small.");
inode = get_inode(cat_cnid(&key->hdr));
if (inode->i_seen)
report("Catalog", "inode numbers are repeated.");
inode->i_seen = true;
inode->i_private_id = le64_to_cpu(val->private_id);
inode->i_parent_id = le64_to_cpu(val->parent_id);
check_inode_ids(inode->i_ino, inode->i_parent_id);
if (inode->i_parent_id == APFS_PRIV_DIR_INO_NUM) {
/* The reported parent id is not updated when orphaned */
report("Inode record", "parent is private directory.");
}
if (inode->i_ino == APFS_ROOT_DIR_INO_NUM)
vsb->v_has_root = true;
if (inode->i_ino == APFS_PRIV_DIR_INO_NUM)
vsb->v_has_priv = true;
inode->i_flags = le64_to_cpu(val->internal_flags);
check_inode_internal_flags(inode->i_flags);
if (inode->i_ino != inode->i_private_id && !(inode->i_flags & (APFS_INODE_WAS_CLONED | APFS_INODE_WAS_EVER_CLONED)))
report("Inode record", "not a clone but changed private id.");
def_prot_class = le32_to_cpu(val->default_protection_class);
if (def_prot_class > APFS_PROTECTION_CLASS_F || def_prot_class == 5)
report("Inode record", "invalid default protection class");
bsd_flags = le32_to_cpu(val->bsd_flags);
if (bsd_flags & APFS_INOBSD_COMPRESSED) {
inode->i_compress = calloc(1, sizeof(*inode->i_compress));
if (!inode->i_compress)
system_error();
}
mode = le16_to_cpu(val->mode);
filetype = mode & S_IFMT;
/* A dentry may have already set the mode, but only the type bits */
if (inode->i_mode && inode->i_mode != filetype)
report("Inode record", "file mode doesn't match dentry type.");
inode->i_mode = mode;
switch (filetype) {
case S_IFREG:
vsb->v_file_count++;
break;
case S_IFDIR:
if (inode->i_ino >= APFS_MIN_USER_INO_NUM)
vsb->v_dir_count++;
break;
case S_IFLNK:
vsb->v_symlink_count++;
break;
case S_IFSOCK:
case S_IFBLK:
case S_IFCHR:
case S_IFIFO:
vsb->v_special_count++;
break;
default:
report("Inode record", "invalid file mode.");
}
inode->i_nlink = le32_to_cpu(val->nlink);
inode->i_owner = le32_to_cpu(val->owner);
if (le16_to_cpu(val->pad1))
report("Inode record", "padding should be zeroes.");
if (!(inode->i_flags & APFS_INODE_HAS_UNCOMPRESSED_SIZE) && le64_to_cpu(val->uncompressed_size))
report("Inode record", "should not report uncompressed size.");
parse_inode_xfields((struct apfs_xf_blob *)val->xfields,
len - sizeof(*val), inode);
}
/**
* get_sibling - Find or create a sibling link structure for an inode
* @id: sibling id
* @inode: the inode
*
* Returns the sibling structure, after creating it if necessary.
*/
struct sibling *get_sibling(u64 id, struct inode *inode)
{
struct sibling **entry_p = &inode->i_siblings;
struct sibling *entry = *entry_p;
struct sibling *new;
/* Siblings are ordered by id in the inode's linked list */
while (entry) {
if (id == entry->s_id)
return entry;
if (id < entry->s_id)
break;
entry_p = &entry->s_next;
entry = *entry_p;
}
new = calloc(1, sizeof(*new));
if (!new)
system_error();
new->s_checked = false;
new->s_id = id;
new->s_next = entry;
*entry_p = new;
return new;
}
/**
* set_or_check_sibling - Set or check the fields of a sibling structure
* @parent_id: parent id
* @namelen: length of the name
* @name: name of the sibling
* @sibling: the sibling structure
*
* When first called for @sibling, sets the three given fields. On the second
* call, checks that they are set to the correct values.
*/
void set_or_check_sibling(u64 parent_id, int namelen, u8 *name,
struct sibling *sibling)
{
/* Whichever was read first, dentry or sibling, sets the fields */
if (!sibling->s_name) {
sibling->s_parent_ino = parent_id;
sibling->s_name_len = namelen;
sibling->s_name = malloc(namelen);
if (!sibling->s_name)
system_error();
strcpy((char *)sibling->s_name, (char *)name);
return;
}
/* Fields already set, check them */
if (sibling->s_name_len != namelen)
report("Sibling record", "name length doesn't match dentry's.");
if (strcmp((char *)sibling->s_name, (char *)name))
report("Sibling record", "name doesn't match dentry's.");
if (sibling->s_parent_ino != parent_id)
report("Sibling record", "parent id doesn't match dentry's.");
sibling->s_checked = true;
}
/**
* parse_sibling_record - Parse and check a sibling link record value
* @key: pointer to the raw key
* @val: pointer to the raw value
* @len: length of the raw value
*
* Internal consistency of @key must be checked before calling this function.
*/
void parse_sibling_record(struct apfs_sibling_link_key *key,
struct apfs_sibling_val *val, int len)
{
struct inode *inode;
struct sibling *sibling;
int namelen;
if (len < sizeof(*val))
report("Sibling link record", "value is too small.");
namelen = le16_to_cpu(val->name_len);
if (len != sizeof(*val) + namelen)
report("Sibling link record", "wrong size of value.");
if (val->name[namelen - 1] != 0)
report("Sibling link record", "name lacks NULL-termination.");
/* Name length doesn't need checking: it's the same for the dentry */
inode = get_inode(cat_cnid(&key->hdr));
if (!inode->i_seen) /* The b-tree keys are in order */
report("Sibling link record", "inode is missing");
sibling = get_sibling(le64_to_cpu(key->sibling_id), inode);
/* It seems that sibling ids come from the same pool as inode numbers */
if (sibling->s_id < APFS_MIN_USER_INO_NUM)
report("Sibling record", "invalid sibling id.");
if (sibling->s_id >= vsb->v_next_obj_id)
report("Sibling record", "free id in use.");
set_or_check_sibling(le64_to_cpu(val->parent_id), namelen, val->name,
sibling);
}
/**
* parse_sibling_record - Parse and check a sibling map record value
* @key: pointer to the raw key
* @val: pointer to the raw value
* @len: length of the raw value
*
* Internal consistency of @key must be checked before calling this function.
*/
void parse_sibling_map_record(struct apfs_sibling_map_key *key,
struct apfs_sibling_map_val *val, int len)
{
struct inode *inode;
struct sibling *sibling;
if (len != sizeof(*val))
report("Sibling map record", "wrong size of value.");
inode = get_inode(le64_to_cpu(val->file_id));
sibling = get_sibling(cat_cnid(&key->hdr), inode);
sibling->s_mapped = true;
}
|