Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Praneeth Balasubramanian <praneethb2002@gmail.com>
To: Coly Li <colyli@fygo.io>, Kent Overstreet <kent.overstreet@linux.dev>
Cc: Praneeth Balasubramanian <praneethb2002@gmail.com>,
	stable@vger.kernel.org, linux-bcache@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH] bcache: hold invalidate_lock while reading the superblock folio
Date: Fri,  4 Dec 2026 22:39:50 -0800	[thread overview]
Message-ID: <20261205063950.2525443-1-praneethb2002@gmail.com> (raw)

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


                 reply	other threads:[~2026-12-05  6:40 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205063950.2525443-1-praneethb2002@gmail.com \
    --to=praneethb2002@gmail.com \
    --cc=colyli@fygo.io \
    --cc=kent.overstreet@linux.dev \
    --cc=linux-bcache@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox