All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] bcache: hold invalidate_lock while reading the superblock folio
@ 2026-12-05  6:39 Praneeth Balasubramanian
  0 siblings, 0 replies; only message in thread
From: Praneeth Balasubramanian @ 2026-12-05  6:39 UTC (permalink / raw)
  To: Coly Li, Kent Overstreet
  Cc: Praneeth Balasubramanian, stable, linux-bcache, linux-kernel

read_super() reads the superblock folio with mapping_read_folio_gfp()
without holding the mapping's invalidate_lock. That function documents
the lock as a caller requirement. The read path allocates the folio using
the mapping's minimum folio order and the block layer then fills it with
buffers sized from the device block size. Both values must stay stable
while the folio is allocated, inserted into the page cache and read.

A concurrent BLKBSZSET on the same block device reaches set_blocksize(),
which under filemap_invalidate_lock() discards the page cache and installs
a larger block size together with a larger mapping minimum folio order.
If that runs between the folio allocation and the read on the bcache side,
the reader keeps an order-0 folio while the block size has grown, buffer
allocation for the folio fails, and create_empty_buffers() dereferences
the missing buffer head. Both racing paths require CAP_SYS_ADMIN, so the
impact is a local privileged kernel crash.

Take the shared variant of filemap_invalidate_lock() around the superblock
folio read in read_super(). The page-cache read paths
(filemap_create_folio, readahead) take it shared as well, so this matches
them. The lock is dropped right after the read and the folio contents are
parsed without it. Nothing else is locked on this path, and the lock is
released long before register_bcache() takes any bcache lock.

This was found by an LLM-based audit that checks code against the rules
stated in kernel comments.

Fixes: 39107ccbc696 ("bcache: switch from pages to folios in read_super()")
Cc: stable@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---

Notes:
    This was found by an LLM-based audit, part of research at UCSB SecLab.
    An LLM turned a kernel comment into a rule, and three LLM votes checked
    the code against it. The fix and changelog were drafted with GLM-5.3,
    then checked against the source and partly rewritten with Claude Opus
    5.5. I reviewed the patch and take responsibility for it.
    
    Testing: builds without new warnings with W=1 (x86_64 allmodconfig),
    clang 23. checkpatch.pl is clean. Not runtime-tested, and there is no
    reproducer. I did not write a reproducer.
    
    Other in-kernel readers of block device page cache, for example
    read_part_sector() in block/partitions, also call read_mapping_folio()
    without invalidate_lock. This patch only changes bcache.

 drivers/md/bcache/super.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 97d9adb0bf96..50b36067d639 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -171,8 +171,10 @@ static const char *read_super(struct cache_sb *sb, struct block_device *bdev,
 	struct folio *folio;
 	unsigned int i;
 
+	filemap_invalidate_lock_shared(bdev->bd_mapping);
 	folio = mapping_read_folio_gfp(bdev->bd_mapping,
 			SB_OFFSET >> PAGE_SHIFT, GFP_KERNEL);
+	filemap_invalidate_unlock_shared(bdev->bd_mapping);
 	if (IS_ERR(folio))
 		return "IO error";
 	s = folio_address(folio) + offset_in_folio(folio, SB_OFFSET);

base-commit: 29beb21c21ada7207692b2d69fc58dcd15dab6e9
-- 
2.25.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-05  6:40 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  6:39 [PATCH] bcache: hold invalidate_lock while reading the superblock folio Praneeth Balasubramanian

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.