Skip to content

Commit ad7b036

Browse files
adam900710kdave
authored andcommitted
btrfs: tree-checker: Add checker for dir item
Add checker for dir item, for key types DIR_ITEM, DIR_INDEX and XATTR_ITEM. This checker does comprehensive checks for: 1) dir_item header and its data size Against item boundary and maximum name/xattr length. This part is mostly the same as old verify_dir_item(). 2) dir_type Against maximum file types, and against key type. Since XATTR key should only have FT_XATTR dir item, and normal dir item type should not have XATTR key. The check between key->type and dir_type is newly introduced by this patch. 3) name hash For XATTR and DIR_ITEM key, key->offset is name hash (crc32c). Check the hash of the name against the key to ensure it's correct. The name hash check is only found in btrfs-progs before this patch. Signed-off-by: Qu Wenruo <[email protected]> Reviewed-by: Nikolay Borisov <[email protected]> Reviewed-by: Su Yue <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 712e36c commit ad7b036

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

fs/btrfs/tree-checker.c

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "tree-checker.h"
3131
#include "disk-io.h"
3232
#include "compression.h"
33+
#include "hash.h"
3334

3435
/*
3536
* Error message should follow the following format:
@@ -222,6 +223,141 @@ static int check_csum_item(struct btrfs_root *root, struct extent_buffer *leaf,
222223
return 0;
223224
}
224225

226+
/*
227+
* Customized reported for dir_item, only important new info is key->objectid,
228+
* which represents inode number
229+
*/
230+
__printf(4, 5)
231+
static void dir_item_err(const struct btrfs_root *root,
232+
const struct extent_buffer *eb, int slot,
233+
const char *fmt, ...)
234+
{
235+
struct btrfs_key key;
236+
struct va_format vaf;
237+
va_list args;
238+
239+
btrfs_item_key_to_cpu(eb, &key, slot);
240+
va_start(args, fmt);
241+
242+
vaf.fmt = fmt;
243+
vaf.va = &args;
244+
245+
btrfs_crit(root->fs_info,
246+
"corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
247+
btrfs_header_level(eb) == 0 ? "leaf" : "node", root->objectid,
248+
btrfs_header_bytenr(eb), slot, key.objectid, &vaf);
249+
va_end(args);
250+
}
251+
252+
static int check_dir_item(struct btrfs_root *root,
253+
struct extent_buffer *leaf,
254+
struct btrfs_key *key, int slot)
255+
{
256+
struct btrfs_dir_item *di;
257+
u32 item_size = btrfs_item_size_nr(leaf, slot);
258+
u32 cur = 0;
259+
260+
di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
261+
while (cur < item_size) {
262+
char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
263+
u32 name_len;
264+
u32 data_len;
265+
u32 max_name_len;
266+
u32 total_size;
267+
u32 name_hash;
268+
u8 dir_type;
269+
270+
/* header itself should not cross item boundary */
271+
if (cur + sizeof(*di) > item_size) {
272+
dir_item_err(root, leaf, slot,
273+
"dir item header crosses item boundary, have %lu boundary %u",
274+
cur + sizeof(*di), item_size);
275+
return -EUCLEAN;
276+
}
277+
278+
/* dir type check */
279+
dir_type = btrfs_dir_type(leaf, di);
280+
if (dir_type >= BTRFS_FT_MAX) {
281+
dir_item_err(root, leaf, slot,
282+
"invalid dir item type, have %u expect [0, %u)",
283+
dir_type, BTRFS_FT_MAX);
284+
return -EUCLEAN;
285+
}
286+
287+
if (key->type == BTRFS_XATTR_ITEM_KEY &&
288+
dir_type != BTRFS_FT_XATTR) {
289+
dir_item_err(root, leaf, slot,
290+
"invalid dir item type for XATTR key, have %u expect %u",
291+
dir_type, BTRFS_FT_XATTR);
292+
return -EUCLEAN;
293+
}
294+
if (dir_type == BTRFS_FT_XATTR &&
295+
key->type != BTRFS_XATTR_ITEM_KEY) {
296+
dir_item_err(root, leaf, slot,
297+
"xattr dir type found for non-XATTR key");
298+
return -EUCLEAN;
299+
}
300+
if (dir_type == BTRFS_FT_XATTR)
301+
max_name_len = XATTR_NAME_MAX;
302+
else
303+
max_name_len = BTRFS_NAME_LEN;
304+
305+
/* Name/data length check */
306+
name_len = btrfs_dir_name_len(leaf, di);
307+
data_len = btrfs_dir_data_len(leaf, di);
308+
if (name_len > max_name_len) {
309+
dir_item_err(root, leaf, slot,
310+
"dir item name len too long, have %u max %u",
311+
name_len, max_name_len);
312+
return -EUCLEAN;
313+
}
314+
if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
315+
dir_item_err(root, leaf, slot,
316+
"dir item name and data len too long, have %u max %u",
317+
name_len + data_len,
318+
BTRFS_MAX_XATTR_SIZE(root->fs_info));
319+
return -EUCLEAN;
320+
}
321+
322+
if (data_len && dir_type != BTRFS_FT_XATTR) {
323+
dir_item_err(root, leaf, slot,
324+
"dir item with invalid data len, have %u expect 0",
325+
data_len);
326+
return -EUCLEAN;
327+
}
328+
329+
total_size = sizeof(*di) + name_len + data_len;
330+
331+
/* header and name/data should not cross item boundary */
332+
if (cur + total_size > item_size) {
333+
dir_item_err(root, leaf, slot,
334+
"dir item data crosses item boundary, have %u boundary %u",
335+
cur + total_size, item_size);
336+
return -EUCLEAN;
337+
}
338+
339+
/*
340+
* Special check for XATTR/DIR_ITEM, as key->offset is name
341+
* hash, should match its name
342+
*/
343+
if (key->type == BTRFS_DIR_ITEM_KEY ||
344+
key->type == BTRFS_XATTR_ITEM_KEY) {
345+
read_extent_buffer(leaf, namebuf,
346+
(unsigned long)(di + 1), name_len);
347+
name_hash = btrfs_name_hash(namebuf, name_len);
348+
if (key->offset != name_hash) {
349+
dir_item_err(root, leaf, slot,
350+
"name hash mismatch with key, have 0x%016x expect 0x%016llx",
351+
name_hash, key->offset);
352+
return -EUCLEAN;
353+
}
354+
}
355+
cur += total_size;
356+
di = (struct btrfs_dir_item *)((void *)di + total_size);
357+
}
358+
return 0;
359+
}
360+
225361
/*
226362
* Common point to switch the item-specific validation.
227363
*/
@@ -238,6 +374,11 @@ static int check_leaf_item(struct btrfs_root *root,
238374
case BTRFS_EXTENT_CSUM_KEY:
239375
ret = check_csum_item(root, leaf, key, slot);
240376
break;
377+
case BTRFS_DIR_ITEM_KEY:
378+
case BTRFS_DIR_INDEX_KEY:
379+
case BTRFS_XATTR_ITEM_KEY:
380+
ret = check_dir_item(root, leaf, key, slot);
381+
break;
241382
}
242383
return ret;
243384
}

0 commit comments

Comments
 (0)