Skip to contentSkip to conversation

bcachefs:fix use-after-free in __bch2_bkey_fsck_err

PATCH In review [email protected] 3 participants 4 messages Oct 5, 2025 – Oct 15, 2025 2 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bcachefs:fix use-after-free in __bch2_bkey_fsck_err

From: Shaurya Rane <[email protected]>

When mounting a corrupted bcachefs filesystem, KASAN detects a use-after-free in the error reporting path. This occurs due to a race between error reporting and key deletion during fsck.

The sequence of events:

1. extent_ptr_validate() detects an invalid extent pointer with a corrupt disk offset
2. It calls __bch2_bkey_fsck_err() to report the error
3. The fsck logic decides to delete the corrupt key
4. Meanwhile, __bch2_bkey_fsck_err() calls bch2_bkey_val_to_text()
5. This traverses the extent entries via bch2_extent_ptr_to_text()
6. sector_to_bucket_and_offset() is called with the corrupt offset
7. Memory access occurs on data being concurrently freed

The crash happens in bch2_extent_ptr_to_text() when it attempts to validate bucket boundaries using already-freed extent pointer data.

Replace bch2_bkey_val_to_text() with bch2_bpos_to_text() to print only the key position (inode, offset, snapshot) instead of traversing potentially corrupt or freed extent metadata. The position is part of the key header and remains safe to read even as the extent data is being freed. The specific validation error is still printed in the subsequent lines, providing sufficient context for debugging.

Tested successfully locally using syzbot provided reproducer

Reported-by [email protected]
Fixes d97de0d017cd ("bcachefs: Make bkey_fsck_err() a wrapper around fsck_err()")
---
 fs/bcachefs/error.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/bcachefs/error.c +1 -1
index 267e73d9d7e6..b0bf08915aa2 100644
@@ -688,7 +688,7 @@ int __bch2_bkey_fsck_err(struct bch_fs *c,
 	bch2_btree_id_to_text(&buf, from.btree);
 	prt_printf(&buf, " level=%u: ", from.level);
 
-	bch2_bkey_val_to_text(&buf, c, k);
+	bch2_bpos_to_text(&buf, k.k->p);
 	prt_newline(&buf);
 
 	va_list args;
Signature · 1 line
2.34.1

A message that is not in the archive (sent off-list or not yet received): CANNWa05YhQ9Ycr13e3SOYgD=cms5iskSENUwXUgk0T8zkMy97Q@mail.gmail.com

syzbot Tested-by
#2 lore raw

Re: [syzbot] [bcachefs?] kernel BUG in bch2_btree_path_level_init (4)

syzbot has tested the proposed patch and the reproducer did not trigger any issue:

Hello,

syzbot has tested the proposed patch and the reproducer did not trigger any issue:

Tested on:

commit:         6093a688 Merge tag 'char-misc-6.18-rc1' of git://git.k..
git tree:       https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
console output: https://syzkaller.appspot.com/x/log.txt?x=16ea2458580000
kernel config:  https://syzkaller.appspot.com/x/.config?x=e2b03b8b7809165e
dashboard link: https://syzkaller.appspot.com/bug?extid=55c84106264e92ea9ada
compiler:       Debian clang version 20.1.8 (++20250708063551+0c9f909b7976-1~exp1~20250708183702.136), Debian LLD 20.1.8

Note: no patches were applied.
Note: testing is done by a robot and is best-effort only.

#3 lore raw
In reply to a message not in the archive
Quoted 2 lines · SHAURYA RANE

#syz test:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master

Instead of having syzbot test your code, please try the ktest syzbot reproducer test:

https://evilpiepirate.org/git/ktest.git/

build-test-kernel run -I ~/ktest/tests/syzbot-repro.ktest <bug-id>

I get very nervous when people send me patches they've never run themselves. Always try to have eyes on what your code is doing, try to look with your eyes and see that it's doing what you think it's doing :)

A message that is not in the archive (sent off-list or not yet received): CANNWa04Mei+hLsZ5JkCfejgiByJgu8e1Euy--ypT_ZmWuvvHeQ@mail.gmail.com

#5 lore raw

Thanks for the feedback. I’ve tested the patch locally as well using kselftest, and it passed successfully.

Quoted 22 lines · SHAURYA RANE

Thanks for the feedback. I’ve tested the patch locally as well using kselftest, and it passed successfully.

Best regards,
Shaurya Rane

Quoted 15 lines · On Sun, 5 Oct 2025, 23:35 Kent Overstreet, <[email protected]> wrote:
Quoted 2 lines · SHAURYA RANE

#syz test:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master

Instead of having syzbot test your code, please try the ktest syzbot reproducer test:

https://evilpiepirate.org/git/ktest.git/

build-test-kernel run -I ~/ktest/tests/syzbot-repro.ktest <bug-id>

I get very nervous when people send me patches they've never run themselves. Always try to have eyes on what your code is doing, try to look with your eyes and see that it's doing what you think it's doing :)