buffer: guard folio_test_dropbehind() against a NULL b_folio in __bh_submit()
PATCH 1/1 buffer: guard folio_test_dropbehind() against a NULL b_folio in __bh_submit()
From: Ridong Chen <[email protected]>
Commit 8deae2284976 ("buffer: allow a buffer_head to point at memory outside the page cache") made bh->b_folio optional: jbd2 submits a shadow buffer_head whose data lives in slab (the frozen/escaped copy of a metadata block), and such a buffer carries no folio at all - b_folio stays NULL from alloc_buffer_head() onwards (see the comment in jbd2_journal_write_metadata_buffer()). That commit audited __bh_submit() and taught bio_add_folio_nofail() and wbc_account_cgroup_owner() to check for a NULL folio, but it missed an earlier dereference:
if (folio_test_dropbehind(bh->b_folio) && op_is_write(opf)) bio_set_flag(bio, BIO_COMPLETE_IN_TASK);
That test was added by commit a2c924c240e7 ("buffer: set BIO_COMPLETE_IN_TASK for dropbehind writeback"), which predates the NULL-b_folio semantics and so dereferenced b_folio unconditionally. Once b_folio may be NULL, folio_test_dropbehind() reads NULL->flags and the kernel takes a NULL pointer dereference.
This is reached from the jbd2 commit path (bh_submit() -> __bh_submit()) whenever a shadow buffer is submitted, i.e. when a metadata block has to be escaped or was copied out, so it is data/timing dependent rather than seen on every commit. When it does hit, it is deterministic and independent of the dropbehind bit, since the flag test dereferences the folio before examining it.
Reproduced on x86_64 with KASAN under filesystem write pressure:
KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] RIP: 0010:__bh_submit+0x21b/0x9d0 Call Trace: bh_submit+0x15/0x30 jbd2_journal_commit_transaction+0x1c19/0x5ac0 kjournald2+0x1cf/0x760
Guard the flag test with a b_folio check, matching the other folio accesses in the same function.
--- fs/buffer.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
fs/buffer.c +2 -1
@@ -1106,7 +1106,8 @@ static void __bh_submit(struct buffer_head *bh, blk_opf_t opf, bio =bio_alloc (bh->b_bdev,1 , opf,GFP_NOIO ); -if (folio_test_dropbehind (bh->b_folio) &&op_is_write (opf)) +if (bh->b_folio &&folio_test_dropbehind (bh->b_folio) && +op_is_write (opf))bio_set_flag (bio,BIO_COMPLETE_IN_TASK );if (IS_ENABLED (CONFIG_FS_ENCRYPTION ))
Signature · 1 line
2.34.1
Jan Kara
reviewerQuoted 36 lines · Ridong Chen
From: Ridong Chen <[email protected]>
Commit
8deae2284976("buffer: allow abuffer_headto point at memory outside the page cache") madebh->b_foliooptional: jbd2 submits a shadowbuffer_headwhose data lives in slab (the frozen/escaped copy of a metadata block), and such a buffer carries no folio at all -b_foliostays NULL fromalloc_buffer_head()onwards (see the comment injbd2_journal_write_metadata_buffer()). That commit audited__bh_submit()and taughtbio_add_folio_nofail()andwbc_account_cgroup_owner()to check for a NULL folio, but it missed an earlier dereference:if (folio_test_dropbehind(bh->b_folio) && op_is_write(opf)) bio_set_flag(bio, BIO_COMPLETE_IN_TASK);That test was added by commit
a2c924c240e7("buffer: setBIO_COMPLETE_IN_TASKfor dropbehind writeback"), which predates the NULL-b_folio semantics and so dereferencedb_foliounconditionally. Onceb_foliomay be NULL,folio_test_dropbehind()readsNULL->flagsand the kernel takes a NULL pointer dereference.This is reached from the jbd2 commit path (
bh_submit()->__bh_submit()) whenever a shadow buffer is submitted, i.e. when a metadata block has to be escaped or was copied out, so it is data/timing dependent rather than seen on every commit. When it does hit, it is deterministic and independent of the dropbehind bit, since the flag test dereferences the folio before examining it.Reproduced on
x86_64with KASAN under filesystem write pressure:KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] RIP: 0010:__bh_submit+0x21b/0x9d0 Call Trace: bh_submit+0x15/0x30 jbd2_journal_commit_transaction+0x1c19/0x5ac0 kjournald2+0x1cf/0x760
Quoted 6 lines
Guard the flag test with a
b_foliocheck, matching the other folio accesses in the same function.Fixes 8deae2284976 ("buffer: allow a buffer_head to point at memory outside the page cache")Assisted-by Claude:claude-opus-4-8Signed-off-by Ridong Chen <[email protected]>
A similar fix was already posted here [1] and is on its way to upstream.
Honza
[1] https://lore.kernel.org/all/[email protected]/
Quoted 21 lines
--- fs/buffer.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)fs/buffer.c +2
-1@@ -1106,7 +1106,8 @@ static void __bh_submit(struct buffer_head *bh, blk_opf_t opf, bio =bio_alloc (bh->b_bdev,1 , opf,GFP_NOIO ); -if (folio_test_dropbehind (bh->b_folio) &&op_is_write (opf)) +if (bh->b_folio &&folio_test_dropbehind (bh->b_folio) && +op_is_write (opf))bio_set_flag (bio,BIO_COMPLETE_IN_TASK );if (IS_ENABLED (CONFIG_FS_ENCRYPTION ))Signature · 1 line
2.34.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 44 lines · Jan Kara
Quoted 36 lines · Ridong Chen
From: Ridong Chen <[email protected]>
Commit
8deae2284976("buffer: allow abuffer_headto point at memory outside the page cache") madebh->b_foliooptional: jbd2 submits a shadowbuffer_headwhose data lives in slab (the frozen/escaped copy of a metadata block), and such a buffer carries no folio at all -b_foliostays NULL fromalloc_buffer_head()onwards (see the comment injbd2_journal_write_metadata_buffer()). That commit audited__bh_submit()and taughtbio_add_folio_nofail()andwbc_account_cgroup_owner()to check for a NULL folio, but it missed an earlier dereference:if (folio_test_dropbehind(bh->b_folio) && op_is_write(opf)) bio_set_flag(bio, BIO_COMPLETE_IN_TASK);That test was added by commit
a2c924c240e7("buffer: setBIO_COMPLETE_IN_TASKfor dropbehind writeback"), which predates the NULL-b_folio semantics and so dereferencedb_foliounconditionally. Onceb_foliomay be NULL,folio_test_dropbehind()readsNULL->flagsand the kernel takes a NULL pointer dereference.This is reached from the jbd2 commit path (
bh_submit()->__bh_submit()) whenever a shadow buffer is submitted, i.e. when a metadata block has to be escaped or was copied out, so it is data/timing dependent rather than seen on every commit. When it does hit, it is deterministic and independent of the dropbehind bit, since the flag test dereferences the folio before examining it.Reproduced on
x86_64with KASAN under filesystem write pressure:KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] RIP: 0010:__bh_submit+0x21b/0x9d0 Call Trace: bh_submit+0x15/0x30 jbd2_journal_commit_transaction+0x1c19/0x5ac0 kjournald2+0x1cf/0x760Quoted 6 lines
Guard the flag test with a
b_foliocheck, matching the other folio accesses in the same function.Fixes 8deae2284976 ("buffer: allow a buffer_head to point at memory outside the page cache")Assisted-by Claude:claude-opus-4-8Signed-off-by Ridong Chen <[email protected]>
Quoted 6 lines
A similar fix was already posted here [1] and is on its way to upstream.
Honza
Apologies, I missed that. Thank you for letting me know.
Quoted 21 lines
Quoted 21 lines
--- fs/buffer.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)fs/buffer.c +2
-1@@ -1106,7 +1106,8 @@ static void __bh_submit(struct buffer_head *bh, blk_opf_t opf, bio =bio_alloc (bh->b_bdev,1 , opf,GFP_NOIO ); -if (folio_test_dropbehind (bh->b_folio) &&op_is_write (opf)) +if (bh->b_folio &&folio_test_dropbehind (bh->b_folio) && +op_is_write (opf))bio_set_flag (bio,BIO_COMPLETE_IN_TASK );if (IS_ENABLED (CONFIG_FS_ENCRYPTION ))Signature · 1 line
2.34.1
Signature · 2 lines
Best regards Ridong