blob: 5acdf3355a3ffe8b28047fa374aae2f952842df3 [file] [log] [blame]
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001/*
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 Wenruobba4f292017-10-09 01:51:03 +000040/*
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)
61static 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 Wenruo557ea5d2017-10-09 01:51:02 +000080static 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
157static 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 */
181static 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
198int 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) {
233 CORRUPT("non-root leaf's nritems is 0",
234 leaf, check_root, 0);
235 free_extent_buffer(eb);
236 return -EUCLEAN;
237 }
238 free_extent_buffer(eb);
239 }
240 return 0;
241 }
242
243 if (nritems == 0)
244 return 0;
245
246 /*
247 * Check the following things to make sure this is a good leaf, and
248 * leaf users won't need to bother with similar sanity checks:
249 *
250 * 1) key ordering
251 * 2) item offset and size
252 * No overlap, no hole, all inside the leaf.
253 * 3) item content
254 * If possible, do comprehensive sanity check.
255 * NOTE: All checks must only rely on the item data itself.
256 */
257 for (slot = 0; slot < nritems; slot++) {
258 u32 item_end_expected;
259 int ret;
260
261 btrfs_item_key_to_cpu(leaf, &key, slot);
262
263 /* Make sure the keys are in the right order */
264 if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) {
265 CORRUPT("bad key order", leaf, root, slot);
266 return -EUCLEAN;
267 }
268
269 /*
270 * Make sure the offset and ends are right, remember that the
271 * item data starts at the end of the leaf and grows towards the
272 * front.
273 */
274 if (slot == 0)
275 item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
276 else
277 item_end_expected = btrfs_item_offset_nr(leaf,
278 slot - 1);
279 if (btrfs_item_end_nr(leaf, slot) != item_end_expected) {
280 CORRUPT("slot offset bad", leaf, root, slot);
281 return -EUCLEAN;
282 }
283
284 /*
285 * Check to make sure that we don't point outside of the leaf,
286 * just in case all the items are consistent to each other, but
287 * all point outside of the leaf.
288 */
289 if (btrfs_item_end_nr(leaf, slot) >
290 BTRFS_LEAF_DATA_SIZE(fs_info)) {
291 CORRUPT("slot end outside of leaf", leaf, root, slot);
292 return -EUCLEAN;
293 }
294
295 /* Also check if the item pointer overlaps with btrfs item. */
296 if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) >
297 btrfs_item_ptr_offset(leaf, slot)) {
298 CORRUPT("slot overlap with its data", leaf, root, slot);
299 return -EUCLEAN;
300 }
301
302 /* Check if the item size and content meet other criteria */
303 ret = check_leaf_item(root, leaf, &key, slot);
304 if (ret < 0)
305 return ret;
306
307 prev_key.objectid = key.objectid;
308 prev_key.type = key.type;
309 prev_key.offset = key.offset;
310 }
311
312 return 0;
313}
314
315int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node)
316{
317 unsigned long nr = btrfs_header_nritems(node);
318 struct btrfs_key key, next_key;
319 int slot;
320 u64 bytenr;
321 int ret = 0;
322
323 if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info)) {
324 btrfs_crit(root->fs_info,
Qu Wenruobba4f292017-10-09 01:51:03 +0000325"corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
326 root->objectid, node->start,
327 nr == 0 ? "small" : "large", nr,
328 BTRFS_NODEPTRS_PER_BLOCK(root->fs_info));
329 return -EUCLEAN;
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000330 }
331
332 for (slot = 0; slot < nr - 1; slot++) {
333 bytenr = btrfs_node_blockptr(node, slot);
334 btrfs_node_key_to_cpu(node, &key, slot);
335 btrfs_node_key_to_cpu(node, &next_key, slot + 1);
336
337 if (!bytenr) {
Qu Wenruobba4f292017-10-09 01:51:03 +0000338 generic_err(root, node, slot,
339 "invalid NULL node pointer");
340 ret = -EUCLEAN;
341 goto out;
342 }
343 if (!IS_ALIGNED(bytenr, root->fs_info->sectorsize)) {
344 generic_err(root, node, slot,
345 "unaligned pointer, have %llu should be aligned to %u",
346 bytenr, root->fs_info->sectorsize);
347 ret = -EUCLEAN;
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000348 goto out;
349 }
350
351 if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) {
Qu Wenruobba4f292017-10-09 01:51:03 +0000352 generic_err(root, node, slot,
353 "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
354 key.objectid, key.type, key.offset,
355 next_key.objectid, next_key.type,
356 next_key.offset);
357 ret = -EUCLEAN;
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000358 goto out;
359 }
360 }
361out:
362 return ret;
363}