tree-checker.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) Qu Wenruo 2017. All rights reserved.
  4. */
  5. /*
  6. * The module is used to catch unexpected/corrupted tree block data.
  7. * Such behavior can be caused either by a fuzzed image or bugs.
  8. *
  9. * The objective is to do leaf/node validation checks when tree block is read
  10. * from disk, and check *every* possible member, so other code won't
  11. * need to checking them again.
  12. *
  13. * Due to the potential and unwanted damage, every checker needs to be
  14. * carefully reviewed otherwise so it does not prevent mount of valid images.
  15. */
  16. #include "ctree.h"
  17. #include "tree-checker.h"
  18. #include "disk-io.h"
  19. #include "compression.h"
  20. /*
  21. * Error message should follow the following format:
  22. * corrupt <type>: <identifier>, <reason>[, <bad_value>]
  23. *
  24. * @type: leaf or node
  25. * @identifier: the necessary info to locate the leaf/node.
  26. * It's recommened to decode key.objecitd/offset if it's
  27. * meaningful.
  28. * @reason: describe the error
  29. * @bad_value: optional, it's recommened to output bad value and its
  30. * expected value (range).
  31. *
  32. * Since comma is used to separate the components, only space is allowed
  33. * inside each component.
  34. */
  35. /*
  36. * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
  37. * Allows callers to customize the output.
  38. */
  39. __printf(4, 5)
  40. __cold
  41. static void generic_err(const struct btrfs_fs_info *fs_info,
  42. const struct extent_buffer *eb, int slot,
  43. const char *fmt, ...)
  44. {
  45. struct va_format vaf;
  46. va_list args;
  47. va_start(args, fmt);
  48. vaf.fmt = fmt;
  49. vaf.va = &args;
  50. btrfs_crit(fs_info,
  51. "corrupt %s: root=%llu block=%llu slot=%d, %pV",
  52. btrfs_header_level(eb) == 0 ? "leaf" : "node",
  53. btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf);
  54. va_end(args);
  55. }
  56. /*
  57. * Customized reporter for extent data item, since its key objectid and
  58. * offset has its own meaning.
  59. */
  60. __printf(4, 5)
  61. __cold
  62. static void file_extent_err(const struct btrfs_fs_info *fs_info,
  63. const struct extent_buffer *eb, int slot,
  64. const char *fmt, ...)
  65. {
  66. struct btrfs_key key;
  67. struct va_format vaf;
  68. va_list args;
  69. btrfs_item_key_to_cpu(eb, &key, slot);
  70. va_start(args, fmt);
  71. vaf.fmt = fmt;
  72. vaf.va = &args;
  73. btrfs_crit(fs_info,
  74. "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
  75. btrfs_header_level(eb) == 0 ? "leaf" : "node",
  76. btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
  77. key.objectid, key.offset, &vaf);
  78. va_end(args);
  79. }
  80. /*
  81. * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
  82. * Else return 1
  83. */
  84. #define CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, name, alignment) \
  85. ({ \
  86. if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \
  87. file_extent_err((fs_info), (leaf), (slot), \
  88. "invalid %s for file extent, have %llu, should be aligned to %u", \
  89. (#name), btrfs_file_extent_##name((leaf), (fi)), \
  90. (alignment)); \
  91. (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \
  92. })
  93. static int check_extent_data_item(struct btrfs_fs_info *fs_info,
  94. struct extent_buffer *leaf,
  95. struct btrfs_key *key, int slot)
  96. {
  97. struct btrfs_file_extent_item *fi;
  98. u32 sectorsize = fs_info->sectorsize;
  99. u32 item_size = btrfs_item_size_nr(leaf, slot);
  100. if (!IS_ALIGNED(key->offset, sectorsize)) {
  101. file_extent_err(fs_info, leaf, slot,
  102. "unaligned file_offset for file extent, have %llu should be aligned to %u",
  103. key->offset, sectorsize);
  104. return -EUCLEAN;
  105. }
  106. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  107. if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) {
  108. file_extent_err(fs_info, leaf, slot,
  109. "invalid type for file extent, have %u expect range [0, %u]",
  110. btrfs_file_extent_type(leaf, fi),
  111. BTRFS_FILE_EXTENT_TYPES);
  112. return -EUCLEAN;
  113. }
  114. /*
  115. * Support for new compression/encrption must introduce incompat flag,
  116. * and must be caught in open_ctree().
  117. */
  118. if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) {
  119. file_extent_err(fs_info, leaf, slot,
  120. "invalid compression for file extent, have %u expect range [0, %u]",
  121. btrfs_file_extent_compression(leaf, fi),
  122. BTRFS_COMPRESS_TYPES);
  123. return -EUCLEAN;
  124. }
  125. if (btrfs_file_extent_encryption(leaf, fi)) {
  126. file_extent_err(fs_info, leaf, slot,
  127. "invalid encryption for file extent, have %u expect 0",
  128. btrfs_file_extent_encryption(leaf, fi));
  129. return -EUCLEAN;
  130. }
  131. if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
  132. /* Inline extent must have 0 as key offset */
  133. if (key->offset) {
  134. file_extent_err(fs_info, leaf, slot,
  135. "invalid file_offset for inline file extent, have %llu expect 0",
  136. key->offset);
  137. return -EUCLEAN;
  138. }
  139. /* Compressed inline extent has no on-disk size, skip it */
  140. if (btrfs_file_extent_compression(leaf, fi) !=
  141. BTRFS_COMPRESS_NONE)
  142. return 0;
  143. /* Uncompressed inline extent size must match item size */
  144. if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
  145. btrfs_file_extent_ram_bytes(leaf, fi)) {
  146. file_extent_err(fs_info, leaf, slot,
  147. "invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
  148. item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START +
  149. btrfs_file_extent_ram_bytes(leaf, fi));
  150. return -EUCLEAN;
  151. }
  152. return 0;
  153. }
  154. /* Regular or preallocated extent has fixed item size */
  155. if (item_size != sizeof(*fi)) {
  156. file_extent_err(fs_info, leaf, slot,
  157. "invalid item size for reg/prealloc file extent, have %u expect %zu",
  158. item_size, sizeof(*fi));
  159. return -EUCLEAN;
  160. }
  161. if (CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, ram_bytes, sectorsize) ||
  162. CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_bytenr, sectorsize) ||
  163. CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_num_bytes, sectorsize) ||
  164. CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, offset, sectorsize) ||
  165. CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, num_bytes, sectorsize))
  166. return -EUCLEAN;
  167. return 0;
  168. }
  169. static int check_csum_item(struct btrfs_fs_info *fs_info,
  170. struct extent_buffer *leaf, struct btrfs_key *key,
  171. int slot)
  172. {
  173. u32 sectorsize = fs_info->sectorsize;
  174. u32 csumsize = btrfs_super_csum_size(fs_info->super_copy);
  175. if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) {
  176. generic_err(fs_info, leaf, slot,
  177. "invalid key objectid for csum item, have %llu expect %llu",
  178. key->objectid, BTRFS_EXTENT_CSUM_OBJECTID);
  179. return -EUCLEAN;
  180. }
  181. if (!IS_ALIGNED(key->offset, sectorsize)) {
  182. generic_err(fs_info, leaf, slot,
  183. "unaligned key offset for csum item, have %llu should be aligned to %u",
  184. key->offset, sectorsize);
  185. return -EUCLEAN;
  186. }
  187. if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) {
  188. generic_err(fs_info, leaf, slot,
  189. "unaligned item size for csum item, have %u should be aligned to %u",
  190. btrfs_item_size_nr(leaf, slot), csumsize);
  191. return -EUCLEAN;
  192. }
  193. return 0;
  194. }
  195. /*
  196. * Customized reported for dir_item, only important new info is key->objectid,
  197. * which represents inode number
  198. */
  199. __printf(4, 5)
  200. __cold
  201. static void dir_item_err(const struct btrfs_fs_info *fs_info,
  202. const struct extent_buffer *eb, int slot,
  203. const char *fmt, ...)
  204. {
  205. struct btrfs_key key;
  206. struct va_format vaf;
  207. va_list args;
  208. btrfs_item_key_to_cpu(eb, &key, slot);
  209. va_start(args, fmt);
  210. vaf.fmt = fmt;
  211. vaf.va = &args;
  212. btrfs_crit(fs_info,
  213. "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
  214. btrfs_header_level(eb) == 0 ? "leaf" : "node",
  215. btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
  216. key.objectid, &vaf);
  217. va_end(args);
  218. }
  219. static int check_dir_item(struct btrfs_fs_info *fs_info,
  220. struct extent_buffer *leaf,
  221. struct btrfs_key *key, int slot)
  222. {
  223. struct btrfs_dir_item *di;
  224. u32 item_size = btrfs_item_size_nr(leaf, slot);
  225. u32 cur = 0;
  226. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  227. while (cur < item_size) {
  228. u32 name_len;
  229. u32 data_len;
  230. u32 max_name_len;
  231. u32 total_size;
  232. u32 name_hash;
  233. u8 dir_type;
  234. /* header itself should not cross item boundary */
  235. if (cur + sizeof(*di) > item_size) {
  236. dir_item_err(fs_info, leaf, slot,
  237. "dir item header crosses item boundary, have %zu boundary %u",
  238. cur + sizeof(*di), item_size);
  239. return -EUCLEAN;
  240. }
  241. /* dir type check */
  242. dir_type = btrfs_dir_type(leaf, di);
  243. if (dir_type >= BTRFS_FT_MAX) {
  244. dir_item_err(fs_info, leaf, slot,
  245. "invalid dir item type, have %u expect [0, %u)",
  246. dir_type, BTRFS_FT_MAX);
  247. return -EUCLEAN;
  248. }
  249. if (key->type == BTRFS_XATTR_ITEM_KEY &&
  250. dir_type != BTRFS_FT_XATTR) {
  251. dir_item_err(fs_info, leaf, slot,
  252. "invalid dir item type for XATTR key, have %u expect %u",
  253. dir_type, BTRFS_FT_XATTR);
  254. return -EUCLEAN;
  255. }
  256. if (dir_type == BTRFS_FT_XATTR &&
  257. key->type != BTRFS_XATTR_ITEM_KEY) {
  258. dir_item_err(fs_info, leaf, slot,
  259. "xattr dir type found for non-XATTR key");
  260. return -EUCLEAN;
  261. }
  262. if (dir_type == BTRFS_FT_XATTR)
  263. max_name_len = XATTR_NAME_MAX;
  264. else
  265. max_name_len = BTRFS_NAME_LEN;
  266. /* Name/data length check */
  267. name_len = btrfs_dir_name_len(leaf, di);
  268. data_len = btrfs_dir_data_len(leaf, di);
  269. if (name_len > max_name_len) {
  270. dir_item_err(fs_info, leaf, slot,
  271. "dir item name len too long, have %u max %u",
  272. name_len, max_name_len);
  273. return -EUCLEAN;
  274. }
  275. if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info)) {
  276. dir_item_err(fs_info, leaf, slot,
  277. "dir item name and data len too long, have %u max %u",
  278. name_len + data_len,
  279. BTRFS_MAX_XATTR_SIZE(fs_info));
  280. return -EUCLEAN;
  281. }
  282. if (data_len && dir_type != BTRFS_FT_XATTR) {
  283. dir_item_err(fs_info, leaf, slot,
  284. "dir item with invalid data len, have %u expect 0",
  285. data_len);
  286. return -EUCLEAN;
  287. }
  288. total_size = sizeof(*di) + name_len + data_len;
  289. /* header and name/data should not cross item boundary */
  290. if (cur + total_size > item_size) {
  291. dir_item_err(fs_info, leaf, slot,
  292. "dir item data crosses item boundary, have %u boundary %u",
  293. cur + total_size, item_size);
  294. return -EUCLEAN;
  295. }
  296. /*
  297. * Special check for XATTR/DIR_ITEM, as key->offset is name
  298. * hash, should match its name
  299. */
  300. if (key->type == BTRFS_DIR_ITEM_KEY ||
  301. key->type == BTRFS_XATTR_ITEM_KEY) {
  302. char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
  303. read_extent_buffer(leaf, namebuf,
  304. (unsigned long)(di + 1), name_len);
  305. name_hash = btrfs_name_hash(namebuf, name_len);
  306. if (key->offset != name_hash) {
  307. dir_item_err(fs_info, leaf, slot,
  308. "name hash mismatch with key, have 0x%016x expect 0x%016llx",
  309. name_hash, key->offset);
  310. return -EUCLEAN;
  311. }
  312. }
  313. cur += total_size;
  314. di = (struct btrfs_dir_item *)((void *)di + total_size);
  315. }
  316. return 0;
  317. }
  318. /*
  319. * Common point to switch the item-specific validation.
  320. */
  321. static int check_leaf_item(struct btrfs_fs_info *fs_info,
  322. struct extent_buffer *leaf,
  323. struct btrfs_key *key, int slot)
  324. {
  325. int ret = 0;
  326. switch (key->type) {
  327. case BTRFS_EXTENT_DATA_KEY:
  328. ret = check_extent_data_item(fs_info, leaf, key, slot);
  329. break;
  330. case BTRFS_EXTENT_CSUM_KEY:
  331. ret = check_csum_item(fs_info, leaf, key, slot);
  332. break;
  333. case BTRFS_DIR_ITEM_KEY:
  334. case BTRFS_DIR_INDEX_KEY:
  335. case BTRFS_XATTR_ITEM_KEY:
  336. ret = check_dir_item(fs_info, leaf, key, slot);
  337. break;
  338. }
  339. return ret;
  340. }
  341. static int check_leaf(struct btrfs_fs_info *fs_info, struct extent_buffer *leaf,
  342. bool check_item_data)
  343. {
  344. /* No valid key type is 0, so all key should be larger than this key */
  345. struct btrfs_key prev_key = {0, 0, 0};
  346. struct btrfs_key key;
  347. u32 nritems = btrfs_header_nritems(leaf);
  348. int slot;
  349. /*
  350. * Extent buffers from a relocation tree have a owner field that
  351. * corresponds to the subvolume tree they are based on. So just from an
  352. * extent buffer alone we can not find out what is the id of the
  353. * corresponding subvolume tree, so we can not figure out if the extent
  354. * buffer corresponds to the root of the relocation tree or not. So
  355. * skip this check for relocation trees.
  356. */
  357. if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
  358. struct btrfs_root *check_root;
  359. key.objectid = btrfs_header_owner(leaf);
  360. key.type = BTRFS_ROOT_ITEM_KEY;
  361. key.offset = (u64)-1;
  362. check_root = btrfs_get_fs_root(fs_info, &key, false);
  363. /*
  364. * The only reason we also check NULL here is that during
  365. * open_ctree() some roots has not yet been set up.
  366. */
  367. if (!IS_ERR_OR_NULL(check_root)) {
  368. struct extent_buffer *eb;
  369. eb = btrfs_root_node(check_root);
  370. /* if leaf is the root, then it's fine */
  371. if (leaf != eb) {
  372. generic_err(fs_info, leaf, 0,
  373. "invalid nritems, have %u should not be 0 for non-root leaf",
  374. nritems);
  375. free_extent_buffer(eb);
  376. return -EUCLEAN;
  377. }
  378. free_extent_buffer(eb);
  379. }
  380. return 0;
  381. }
  382. if (nritems == 0)
  383. return 0;
  384. /*
  385. * Check the following things to make sure this is a good leaf, and
  386. * leaf users won't need to bother with similar sanity checks:
  387. *
  388. * 1) key ordering
  389. * 2) item offset and size
  390. * No overlap, no hole, all inside the leaf.
  391. * 3) item content
  392. * If possible, do comprehensive sanity check.
  393. * NOTE: All checks must only rely on the item data itself.
  394. */
  395. for (slot = 0; slot < nritems; slot++) {
  396. u32 item_end_expected;
  397. int ret;
  398. btrfs_item_key_to_cpu(leaf, &key, slot);
  399. /* Make sure the keys are in the right order */
  400. if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) {
  401. generic_err(fs_info, leaf, slot,
  402. "bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
  403. prev_key.objectid, prev_key.type,
  404. prev_key.offset, key.objectid, key.type,
  405. key.offset);
  406. return -EUCLEAN;
  407. }
  408. /*
  409. * Make sure the offset and ends are right, remember that the
  410. * item data starts at the end of the leaf and grows towards the
  411. * front.
  412. */
  413. if (slot == 0)
  414. item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
  415. else
  416. item_end_expected = btrfs_item_offset_nr(leaf,
  417. slot - 1);
  418. if (btrfs_item_end_nr(leaf, slot) != item_end_expected) {
  419. generic_err(fs_info, leaf, slot,
  420. "unexpected item end, have %u expect %u",
  421. btrfs_item_end_nr(leaf, slot),
  422. item_end_expected);
  423. return -EUCLEAN;
  424. }
  425. /*
  426. * Check to make sure that we don't point outside of the leaf,
  427. * just in case all the items are consistent to each other, but
  428. * all point outside of the leaf.
  429. */
  430. if (btrfs_item_end_nr(leaf, slot) >
  431. BTRFS_LEAF_DATA_SIZE(fs_info)) {
  432. generic_err(fs_info, leaf, slot,
  433. "slot end outside of leaf, have %u expect range [0, %u]",
  434. btrfs_item_end_nr(leaf, slot),
  435. BTRFS_LEAF_DATA_SIZE(fs_info));
  436. return -EUCLEAN;
  437. }
  438. /* Also check if the item pointer overlaps with btrfs item. */
  439. if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) >
  440. btrfs_item_ptr_offset(leaf, slot)) {
  441. generic_err(fs_info, leaf, slot,
  442. "slot overlaps with its data, item end %lu data start %lu",
  443. btrfs_item_nr_offset(slot) +
  444. sizeof(struct btrfs_item),
  445. btrfs_item_ptr_offset(leaf, slot));
  446. return -EUCLEAN;
  447. }
  448. if (check_item_data) {
  449. /*
  450. * Check if the item size and content meet other
  451. * criteria
  452. */
  453. ret = check_leaf_item(fs_info, leaf, &key, slot);
  454. if (ret < 0)
  455. return ret;
  456. }
  457. prev_key.objectid = key.objectid;
  458. prev_key.type = key.type;
  459. prev_key.offset = key.offset;
  460. }
  461. return 0;
  462. }
  463. int btrfs_check_leaf_full(struct btrfs_fs_info *fs_info,
  464. struct extent_buffer *leaf)
  465. {
  466. return check_leaf(fs_info, leaf, true);
  467. }
  468. int btrfs_check_leaf_relaxed(struct btrfs_fs_info *fs_info,
  469. struct extent_buffer *leaf)
  470. {
  471. return check_leaf(fs_info, leaf, false);
  472. }
  473. int btrfs_check_node(struct btrfs_fs_info *fs_info, struct extent_buffer *node)
  474. {
  475. unsigned long nr = btrfs_header_nritems(node);
  476. struct btrfs_key key, next_key;
  477. int slot;
  478. u64 bytenr;
  479. int ret = 0;
  480. if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info)) {
  481. btrfs_crit(fs_info,
  482. "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
  483. btrfs_header_owner(node), node->start,
  484. nr == 0 ? "small" : "large", nr,
  485. BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  486. return -EUCLEAN;
  487. }
  488. for (slot = 0; slot < nr - 1; slot++) {
  489. bytenr = btrfs_node_blockptr(node, slot);
  490. btrfs_node_key_to_cpu(node, &key, slot);
  491. btrfs_node_key_to_cpu(node, &next_key, slot + 1);
  492. if (!bytenr) {
  493. generic_err(fs_info, node, slot,
  494. "invalid NULL node pointer");
  495. ret = -EUCLEAN;
  496. goto out;
  497. }
  498. if (!IS_ALIGNED(bytenr, fs_info->sectorsize)) {
  499. generic_err(fs_info, node, slot,
  500. "unaligned pointer, have %llu should be aligned to %u",
  501. bytenr, fs_info->sectorsize);
  502. ret = -EUCLEAN;
  503. goto out;
  504. }
  505. if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) {
  506. generic_err(fs_info, node, slot,
  507. "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
  508. key.objectid, key.type, key.offset,
  509. next_key.objectid, next_key.type,
  510. next_key.offset);
  511. ret = -EUCLEAN;
  512. goto out;
  513. }
  514. }
  515. out:
  516. return ret;
  517. }