Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Qu Wenruo 2017. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * The module is used to catch unexpected/corrupted tree block data. |
| 19 | * Such behavior can be caused either by a fuzzed image or bugs. |
| 20 | * |
| 21 | * The objective is to do leaf/node validation checks when tree block is read |
| 22 | * from disk, and check *every* possible member, so other code won't |
| 23 | * need to checking them again. |
| 24 | * |
| 25 | * Due to the potential and unwanted damage, every checker needs to be |
| 26 | * carefully reviewed otherwise so it does not prevent mount of valid images. |
| 27 | */ |
| 28 | |
| 29 | #include "ctree.h" |
| 30 | #include "tree-checker.h" |
| 31 | #include "disk-io.h" |
| 32 | #include "compression.h" |
| 33 | |
| 34 | #define CORRUPT(reason, eb, root, slot) \ |
| 35 | btrfs_crit(root->fs_info, \ |
| 36 | "corrupt %s, %s: block=%llu, root=%llu, slot=%d", \ |
| 37 | btrfs_header_level(eb) == 0 ? "leaf" : "node", \ |
| 38 | reason, btrfs_header_bytenr(eb), root->objectid, slot) |
| 39 | |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 40 | /* |
| 41 | * Error message should follow the following format: |
| 42 | * corrupt <type>: <identifier>, <reason>[, <bad_value>] |
| 43 | * |
| 44 | * @type: leaf or node |
| 45 | * @identifier: the necessary info to locate the leaf/node. |
| 46 | * It's recommened to decode key.objecitd/offset if it's |
| 47 | * meaningful. |
| 48 | * @reason: describe the error |
| 49 | * @bad_value: optional, it's recommened to output bad value and its |
| 50 | * expected value (range). |
| 51 | * |
| 52 | * Since comma is used to separate the components, only space is allowed |
| 53 | * inside each component. |
| 54 | */ |
| 55 | |
| 56 | /* |
| 57 | * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt. |
| 58 | * Allows callers to customize the output. |
| 59 | */ |
| 60 | __printf(4, 5) |
| 61 | static void generic_err(const struct btrfs_root *root, |
| 62 | const struct extent_buffer *eb, int slot, |
| 63 | const char *fmt, ...) |
| 64 | { |
| 65 | struct va_format vaf; |
| 66 | va_list args; |
| 67 | |
| 68 | va_start(args, fmt); |
| 69 | |
| 70 | vaf.fmt = fmt; |
| 71 | vaf.va = &args; |
| 72 | |
| 73 | btrfs_crit(root->fs_info, |
| 74 | "corrupt %s: root=%llu block=%llu slot=%d, %pV", |
| 75 | btrfs_header_level(eb) == 0 ? "leaf" : "node", |
| 76 | root->objectid, btrfs_header_bytenr(eb), slot, &vaf); |
| 77 | va_end(args); |
| 78 | } |
| 79 | |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 80 | static int check_extent_data_item(struct btrfs_root *root, |
| 81 | struct extent_buffer *leaf, |
| 82 | struct btrfs_key *key, int slot) |
| 83 | { |
| 84 | struct btrfs_file_extent_item *fi; |
| 85 | u32 sectorsize = root->fs_info->sectorsize; |
| 86 | u32 item_size = btrfs_item_size_nr(leaf, slot); |
| 87 | |
| 88 | if (!IS_ALIGNED(key->offset, sectorsize)) { |
| 89 | CORRUPT("unaligned key offset for file extent", |
| 90 | leaf, root, slot); |
| 91 | return -EUCLEAN; |
| 92 | } |
| 93 | |
| 94 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 95 | |
| 96 | if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) { |
| 97 | CORRUPT("invalid file extent type", leaf, root, slot); |
| 98 | return -EUCLEAN; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Support for new compression/encrption must introduce incompat flag, |
| 103 | * and must be caught in open_ctree(). |
| 104 | */ |
| 105 | if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) { |
| 106 | CORRUPT("invalid file extent compression", leaf, root, slot); |
| 107 | return -EUCLEAN; |
| 108 | } |
| 109 | if (btrfs_file_extent_encryption(leaf, fi)) { |
| 110 | CORRUPT("invalid file extent encryption", leaf, root, slot); |
| 111 | return -EUCLEAN; |
| 112 | } |
| 113 | if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { |
| 114 | /* Inline extent must have 0 as key offset */ |
| 115 | if (key->offset) { |
| 116 | CORRUPT("inline extent has non-zero key offset", |
| 117 | leaf, root, slot); |
| 118 | return -EUCLEAN; |
| 119 | } |
| 120 | |
| 121 | /* Compressed inline extent has no on-disk size, skip it */ |
| 122 | if (btrfs_file_extent_compression(leaf, fi) != |
| 123 | BTRFS_COMPRESS_NONE) |
| 124 | return 0; |
| 125 | |
| 126 | /* Uncompressed inline extent size must match item size */ |
| 127 | if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START + |
| 128 | btrfs_file_extent_ram_bytes(leaf, fi)) { |
| 129 | CORRUPT("plaintext inline extent has invalid size", |
| 130 | leaf, root, slot); |
| 131 | return -EUCLEAN; |
| 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | /* Regular or preallocated extent has fixed item size */ |
| 137 | if (item_size != sizeof(*fi)) { |
| 138 | CORRUPT( |
| 139 | "regluar or preallocated extent data item size is invalid", |
| 140 | leaf, root, slot); |
| 141 | return -EUCLEAN; |
| 142 | } |
| 143 | if (!IS_ALIGNED(btrfs_file_extent_ram_bytes(leaf, fi), sectorsize) || |
| 144 | !IS_ALIGNED(btrfs_file_extent_disk_bytenr(leaf, fi), sectorsize) || |
| 145 | !IS_ALIGNED(btrfs_file_extent_disk_num_bytes(leaf, fi), sectorsize) || |
| 146 | !IS_ALIGNED(btrfs_file_extent_offset(leaf, fi), sectorsize) || |
| 147 | !IS_ALIGNED(btrfs_file_extent_num_bytes(leaf, fi), sectorsize)) { |
| 148 | CORRUPT( |
| 149 | "regular or preallocated extent data item has unaligned value", |
| 150 | leaf, root, slot); |
| 151 | return -EUCLEAN; |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static int check_csum_item(struct btrfs_root *root, struct extent_buffer *leaf, |
| 158 | struct btrfs_key *key, int slot) |
| 159 | { |
| 160 | u32 sectorsize = root->fs_info->sectorsize; |
| 161 | u32 csumsize = btrfs_super_csum_size(root->fs_info->super_copy); |
| 162 | |
| 163 | if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) { |
| 164 | CORRUPT("invalid objectid for csum item", leaf, root, slot); |
| 165 | return -EUCLEAN; |
| 166 | } |
| 167 | if (!IS_ALIGNED(key->offset, sectorsize)) { |
| 168 | CORRUPT("unaligned key offset for csum item", leaf, root, slot); |
| 169 | return -EUCLEAN; |
| 170 | } |
| 171 | if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) { |
| 172 | CORRUPT("unaligned csum item size", leaf, root, slot); |
| 173 | return -EUCLEAN; |
| 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Common point to switch the item-specific validation. |
| 180 | */ |
| 181 | static int check_leaf_item(struct btrfs_root *root, |
| 182 | struct extent_buffer *leaf, |
| 183 | struct btrfs_key *key, int slot) |
| 184 | { |
| 185 | int ret = 0; |
| 186 | |
| 187 | switch (key->type) { |
| 188 | case BTRFS_EXTENT_DATA_KEY: |
| 189 | ret = check_extent_data_item(root, leaf, key, slot); |
| 190 | break; |
| 191 | case BTRFS_EXTENT_CSUM_KEY: |
| 192 | ret = check_csum_item(root, leaf, key, slot); |
| 193 | break; |
| 194 | } |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | int btrfs_check_leaf(struct btrfs_root *root, struct extent_buffer *leaf) |
| 199 | { |
| 200 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 201 | /* No valid key type is 0, so all key should be larger than this key */ |
| 202 | struct btrfs_key prev_key = {0, 0, 0}; |
| 203 | struct btrfs_key key; |
| 204 | u32 nritems = btrfs_header_nritems(leaf); |
| 205 | int slot; |
| 206 | |
| 207 | /* |
| 208 | * Extent buffers from a relocation tree have a owner field that |
| 209 | * corresponds to the subvolume tree they are based on. So just from an |
| 210 | * extent buffer alone we can not find out what is the id of the |
| 211 | * corresponding subvolume tree, so we can not figure out if the extent |
| 212 | * buffer corresponds to the root of the relocation tree or not. So |
| 213 | * skip this check for relocation trees. |
| 214 | */ |
| 215 | if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) { |
| 216 | struct btrfs_root *check_root; |
| 217 | |
| 218 | key.objectid = btrfs_header_owner(leaf); |
| 219 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 220 | key.offset = (u64)-1; |
| 221 | |
| 222 | check_root = btrfs_get_fs_root(fs_info, &key, false); |
| 223 | /* |
| 224 | * The only reason we also check NULL here is that during |
| 225 | * open_ctree() some roots has not yet been set up. |
| 226 | */ |
| 227 | if (!IS_ERR_OR_NULL(check_root)) { |
| 228 | struct extent_buffer *eb; |
| 229 | |
| 230 | eb = btrfs_root_node(check_root); |
| 231 | /* if leaf is the root, then it's fine */ |
| 232 | if (leaf != eb) { |
Qu Wenruo | 478d01b | 2017-10-09 01:51:04 +0000 | [diff] [blame^] | 233 | generic_err(check_root, leaf, 0, |
| 234 | "invalid nritems, have %u should not be 0 for non-root leaf", |
| 235 | nritems); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 236 | free_extent_buffer(eb); |
| 237 | return -EUCLEAN; |
| 238 | } |
| 239 | free_extent_buffer(eb); |
| 240 | } |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | if (nritems == 0) |
| 245 | return 0; |
| 246 | |
| 247 | /* |
| 248 | * Check the following things to make sure this is a good leaf, and |
| 249 | * leaf users won't need to bother with similar sanity checks: |
| 250 | * |
| 251 | * 1) key ordering |
| 252 | * 2) item offset and size |
| 253 | * No overlap, no hole, all inside the leaf. |
| 254 | * 3) item content |
| 255 | * If possible, do comprehensive sanity check. |
| 256 | * NOTE: All checks must only rely on the item data itself. |
| 257 | */ |
| 258 | for (slot = 0; slot < nritems; slot++) { |
| 259 | u32 item_end_expected; |
| 260 | int ret; |
| 261 | |
| 262 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 263 | |
| 264 | /* Make sure the keys are in the right order */ |
| 265 | if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) { |
Qu Wenruo | 478d01b | 2017-10-09 01:51:04 +0000 | [diff] [blame^] | 266 | generic_err(root, leaf, slot, |
| 267 | "bad key order, prev (%llu %u %llu) current (%llu %u %llu)", |
| 268 | prev_key.objectid, prev_key.type, |
| 269 | prev_key.offset, key.objectid, key.type, |
| 270 | key.offset); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 271 | return -EUCLEAN; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Make sure the offset and ends are right, remember that the |
| 276 | * item data starts at the end of the leaf and grows towards the |
| 277 | * front. |
| 278 | */ |
| 279 | if (slot == 0) |
| 280 | item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info); |
| 281 | else |
| 282 | item_end_expected = btrfs_item_offset_nr(leaf, |
| 283 | slot - 1); |
| 284 | if (btrfs_item_end_nr(leaf, slot) != item_end_expected) { |
Qu Wenruo | 478d01b | 2017-10-09 01:51:04 +0000 | [diff] [blame^] | 285 | generic_err(root, leaf, slot, |
| 286 | "unexpected item end, have %u expect %u", |
| 287 | btrfs_item_end_nr(leaf, slot), |
| 288 | item_end_expected); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 289 | return -EUCLEAN; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Check to make sure that we don't point outside of the leaf, |
| 294 | * just in case all the items are consistent to each other, but |
| 295 | * all point outside of the leaf. |
| 296 | */ |
| 297 | if (btrfs_item_end_nr(leaf, slot) > |
| 298 | BTRFS_LEAF_DATA_SIZE(fs_info)) { |
Qu Wenruo | 478d01b | 2017-10-09 01:51:04 +0000 | [diff] [blame^] | 299 | generic_err(root, leaf, slot, |
| 300 | "slot end outside of leaf, have %u expect range [0, %u]", |
| 301 | btrfs_item_end_nr(leaf, slot), |
| 302 | BTRFS_LEAF_DATA_SIZE(fs_info)); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 303 | return -EUCLEAN; |
| 304 | } |
| 305 | |
| 306 | /* Also check if the item pointer overlaps with btrfs item. */ |
| 307 | if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) > |
| 308 | btrfs_item_ptr_offset(leaf, slot)) { |
Qu Wenruo | 478d01b | 2017-10-09 01:51:04 +0000 | [diff] [blame^] | 309 | generic_err(root, leaf, slot, |
| 310 | "slot overlaps with its data, item end %lu data start %lu", |
| 311 | btrfs_item_nr_offset(slot) + |
| 312 | sizeof(struct btrfs_item), |
| 313 | btrfs_item_ptr_offset(leaf, slot)); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 314 | return -EUCLEAN; |
| 315 | } |
| 316 | |
| 317 | /* Check if the item size and content meet other criteria */ |
| 318 | ret = check_leaf_item(root, leaf, &key, slot); |
| 319 | if (ret < 0) |
| 320 | return ret; |
| 321 | |
| 322 | prev_key.objectid = key.objectid; |
| 323 | prev_key.type = key.type; |
| 324 | prev_key.offset = key.offset; |
| 325 | } |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node) |
| 331 | { |
| 332 | unsigned long nr = btrfs_header_nritems(node); |
| 333 | struct btrfs_key key, next_key; |
| 334 | int slot; |
| 335 | u64 bytenr; |
| 336 | int ret = 0; |
| 337 | |
| 338 | if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info)) { |
| 339 | btrfs_crit(root->fs_info, |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 340 | "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]", |
| 341 | root->objectid, node->start, |
| 342 | nr == 0 ? "small" : "large", nr, |
| 343 | BTRFS_NODEPTRS_PER_BLOCK(root->fs_info)); |
| 344 | return -EUCLEAN; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | for (slot = 0; slot < nr - 1; slot++) { |
| 348 | bytenr = btrfs_node_blockptr(node, slot); |
| 349 | btrfs_node_key_to_cpu(node, &key, slot); |
| 350 | btrfs_node_key_to_cpu(node, &next_key, slot + 1); |
| 351 | |
| 352 | if (!bytenr) { |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 353 | generic_err(root, node, slot, |
| 354 | "invalid NULL node pointer"); |
| 355 | ret = -EUCLEAN; |
| 356 | goto out; |
| 357 | } |
| 358 | if (!IS_ALIGNED(bytenr, root->fs_info->sectorsize)) { |
| 359 | generic_err(root, node, slot, |
| 360 | "unaligned pointer, have %llu should be aligned to %u", |
| 361 | bytenr, root->fs_info->sectorsize); |
| 362 | ret = -EUCLEAN; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 363 | goto out; |
| 364 | } |
| 365 | |
| 366 | if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) { |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 367 | generic_err(root, node, slot, |
| 368 | "bad key order, current (%llu %u %llu) next (%llu %u %llu)", |
| 369 | key.objectid, key.type, key.offset, |
| 370 | next_key.objectid, next_key.type, |
| 371 | next_key.offset); |
| 372 | ret = -EUCLEAN; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 373 | goto out; |
| 374 | } |
| 375 | } |
| 376 | out: |
| 377 | return ret; |
| 378 | } |