check: check so offset is not bigger then the leaf
PATCH 1/1 check: check so offset is not bigger then the leaf
This could crash before because of dangerous dangling offset of pointer.
--- cmds-check.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
cmds-check.c +10 -0
@@ -8906,6 +8906,16 @@ static int build_roots_info_cache(struct btrfs_fs_info *info) goto next; ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); + +if ((long long )ei > info->extent_root->leafsize) { +fprintf (stderr,"Bad leaf = %p, slot = %d\n" , leaf, slot); +fprintf (stderr,"item ptr = %p\n" , ei); +fprintf (stderr,"objectid = %llx\n" , found_key.objectid); +fprintf (stderr,"type = %x\n" , found_key.type); +fprintf (stderr,"offset = %llx\n" , found_key.offset); + goto next; + } + flags =btrfs_extent_flags (leaf, ei); if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
Signature · 1 line
2.1.0
Quoted 2 lines · Robert Marklund
This could crash before because of dangerous dangling offset of pointer.
That's right, this can happen. There are more btrfs_item_ptr that would be good to validate that way, namely in the checker as it's most likely to see corrupted data.
I think it's worth to add a wrapper macro for that, that would be like
(int) btrfs_item_ptr_validate(ei, leaf, slot, struct ..., *optional_key)
and return 0 if it's ok, 1 if there's a problem and prints the details.
Quoted 10 lines
Signed-off-by Robert Marklund <[email protected]>--- cmds-check.c | 10 ++++++++++ 1 file changed, 10 insertions(+)cmds-check.c +0
-0
cmds-check.c static int build_roots_info_cache(struct btrfs_fs_info *info) · line 8915
Quoted 10 lines
cmds-check.c +7
-0@@ -8906,6 +8906,16 @@ static int build_roots_info_cache(struct btrfs_fs_info *info) goto next; ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); + +if ((long long )ei > info->extent_root->leafsize) { +fprintf (stderr,"Bad leaf = %p, slot = %d\n" , leaf, slot); +fprintf (stderr,"item ptr = %p\n" , ei); +fprintf (stderr,"objectid = %llx\n" , found_key.objectid); +fprintf (stderr,"type = %x\n" , found_key.type); +fprintf (stderr,"offset = %llx\n" , found_key.offset);
Hm, I'm not sure whether to continue or fail at this point.
Do you have a crafted filesystem image that can reproduce that or was that found by code inspection?
Quoted 6 lines
cmds-check.c +3
-0+ goto next; + } + flags =btrfs_extent_flags (leaf, ei); if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
Quoted 8 lines · David Sterba
Quoted 2 lines · Robert Marklund
This could crash before because of dangerous dangling offset of pointer.
That's right, this can happen. There are more
btrfs_item_ptrthat would be good to validate that way, namely in the checker as it's most likely to see corrupted data.
The check_block stuff should be doing this, if it isn't that's where we need to fix it. Thanks,
Josef
Quoted 5 lines · Josef Bacik
Quoted 4 lines · David Sterba
Quoted 2 lines · Robert Marklund
This could crash before because of dangerous dangling offset of pointer.
Quoted 7 lines
Quoted 4 lines
That's right, this can happen. There are more
btrfs_item_ptrthat would be good to validate that way, namely in the checker as it's most likely to see corrupted data.The
check_blockstuff should be doing this, if it isn't that's where we need to fix it. Thanks,
Something like that?
ctree.c +13 -0
@@ -521,6 +521,19 @@ btrfs_check_leaf(struct btrfs_root *root, struct btrfs_disk_key *parent_key, goto fail; } } + +for (i =0 ; i < nritems; i++) { +void *tmp; + + tmp =btrfs_item_ptr (buf, i, void); +if ((long )tmp >=BTRFS_LEAF_DATA_SIZE (root)) { + ret =BTRFS_TREE_BLOCK_INVALID_OFFSETS ; +fprintf (stderr,"bad item pointer %lu\n" , + (long )tmp); + goto fail; + } + } +return BTRFS_TREE_BLOCK_CLEAN ; fail:if (btrfs_header_owner (buf) ==BTRFS_EXTENT_TREE_OBJECTID ) {
---
Compile-tested only.
Quoted 24 lines · David Sterba
Quoted 5 lines · Josef Bacik
Quoted 4 lines · David Sterba
Quoted 2 lines · Robert Marklund
This could crash before because of dangerous dangling offset of pointer.
Quoted 7 lines
Quoted 4 lines
That's right, this can happen. There are more
btrfs_item_ptrthat would be good to validate that way, namely in the checker as it's most likely to see corrupted data.The
check_blockstuff should be doing this, if it isn't that's where we need to fix it. Thanks,Something like that?
ctree.c +2
-0@@ -521,6 +521,19 @@ btrfs_check_leaf(struct btrfs_root *root, struct btrfs_disk_key *parent_key, goto fail; } } + +for (i =0 ; i < nritems; i++) {
ctree.c btrfs_check_leaf(struct btrfs_root *root, struct btrfs_disk_key *parent_key, · line 535
Quoted 10 lines
ctree.c +10
-0+void *tmp; + + tmp =btrfs_item_ptr (buf, i, void); +if ((long )tmp >=BTRFS_LEAF_DATA_SIZE (root)) { + ret =BTRFS_TREE_BLOCK_INVALID_OFFSETS ; +fprintf (stderr,"bad item pointer %lu\n" , + (long )tmp); + goto fail; + } + }
I'd just do
if (btrfs_item_end_nr(buf, i) >= BTRFS_LEAF_DATA_SIZE(root))
that way you catch problems with offset and size. Thanks,
Josef
Quoted 15 lines · Josef Bacik
ctree.c btrfs_check_leaf(struct btrfs_root *root, struct btrfs_disk_key *parent_key, · line 535
Quoted 12 lines
ctree.c +12
-0+ +for (i =0 ; i < nritems; i++) { +void *tmp; + + tmp =btrfs_item_ptr (buf, i, void); +if ((long )tmp >=BTRFS_LEAF_DATA_SIZE (root)) { + ret =BTRFS_TREE_BLOCK_INVALID_OFFSETS ; +fprintf (stderr,"bad item pointer %lu\n" , + (long )tmp); + goto fail; + } + }I'd just do
Quoted 3 lines
if (
btrfs_item_end_nr(buf, i)>=BTRFS_LEAF_DATA_SIZE(root))that way you catch problems with offset and size. Thanks,
Ah right, my check would not catch 'offset + size >= leaf data size' if 'offset < leaf data size'. Patch welcome.
Quoted 29 lines · David Sterba
Quoted 2 lines · Robert Marklund
This could crash before because of dangerous dangling offset of pointer.
That's right, this can happen. There are more
btrfs_item_ptrthat would be good to validate that way, namely in the checker as it's most likely to see corrupted data.I think it's worth to add a wrapper macro for that, that would be like
(int)
btrfs_item_ptr_validate(ei, leaf, slot, struct ..., *optional_key)and return 0 if it's ok, 1 if there's a problem and prints the details.
Quoted 12 lines
Signed-off-by Robert Marklund <[email protected]>---
cmds-check.c| 10 ++++++++++1 file changed, 10 insertions(+)cmds-check.c +0
-0@@ -8906,6 +8906,16 @@ static int build_roots_info_cache(struct btrfs_fs_info *info) goto next;
Quoted 11 lines
cmds-check.c static int build_roots_info_cache(struct btrfs_fs_info *info) · line 8915
Quoted 8 lines
cmds-check.c +7
-0ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); + +if ((long long )ei > info->extent_root->leafsize) { +fprintf (stderr,"Bad leaf = %p, slot = %d\n" , leaf, slot); +fprintf (stderr,"item ptr = %p\n" , ei); +fprintf (stderr,"objectid = %llx\n" , found_key.objectid); +fprintf (stderr,"type = %x\n" , found_key.type); +fprintf (stderr,"offset = %llx\n" , found_key.offset);Hm, I'm not sure whether to continue or fail at this point.
Im not either :)
But for me its better to keep trying until you hot the wall for real.
Quoted 2 lines
Do you have a crafted filesystem image that can reproduce that or was that found by code inspection?
I have a failed filesystem caused by a failing disk that I tried to fix/recover. Then i stumbled on this, and later on on some more places other then this. Ill submit that also and in a nicer way when my filesystem is rescued.
Quoted 7 lines
Quoted 7 lines
cmds-check.c +3
-0+ goto next; + } + flags =btrfs_extent_flags (leaf, ei); if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
Quoted 6 lines · Trollkarlen Marklund
Quoted 2 lines
Do you have a crafted filesystem image that can reproduce that or was that found by code inspection?
I have a failed filesystem caused by a failing disk that I tried to fix/recover. Then i stumbled on this, and later on on some more places other then this. Ill submit that also and in a nicer way when my filesystem is rescued.
FYI, I've committed a patch based on the discussion in the thread.