From: Ozgun Kultekin <ozgunkultekin@gmail.com>
To: colyli@fygo.io, kent.overstreet@linux.dev
Cc: linux-bcache@vger.kernel.org,
Ozgun Kultekin <ozgunkultekin@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH] bcache: don't read more than the buffer holds for a btree node or the uuids
Date: Fri, 27 Nov 2026 19:36:09 -0800 [thread overview]
Message-ID: <20261128033609.42213-1-ozgunkultekin@gmail.com> (raw)
bch_btree_node_read() sets the bio size to KEY_SIZE(&b->key) << 9 and
maps it onto the node buffer, which is PAGE_SIZE << b->keys.page_order
bytes. page_order is ilog2(KEY_SIZE / PAGE_SECTORS), so it rounds
down, and nothing checks that the key fits in the buffer. For example
a 1023 sector btree pointer in a 1024 sector bucket passes
__bch_btree_ptr_invalid(), gets a 256 KiB buffer, and the read runs
past the end of it.
__ptr_invalid() also skips the bucket_size check for pointers whose
device index is >= MAX_CACHES_PER_SET, while bch_submit_bbio() always
reads from c->cache. Such a key can be up to 65535 sectors, which is
larger than both the node buffer and the bvecs of the bbio.
uuid_io() has the same shape: it reads KEY_SIZE(k) << 9 bytes of
jset->uuid_bucket into c->uuids, which is one meta bucket.
The btree root and uuid keys are read from the journal, so a corrupted
on-disk key makes these reads overrun their buffers before any node
checksum is checked.
Reject a btree pointer larger than a bucket regardless of its device
index, fail a node read whose key is larger than the node buffer or
the bbio, and reject a uuid pointer larger than a meta bucket. Keys
that bcache writes itself are btree_pages * PAGE_SECTORS and
meta_bucket_pages() * PAGE_SECTORS sectors on device 0, so they are
not affected.
Fixes: cafe56359144 ("bcache: A block layer cache")
Cc: stable@vger.kernel.org
Signed-off-by: Ozgun Kultekin <ozgunkultekin@gmail.com>
---
drivers/md/bcache/btree.c | 13 +++++++++++++
drivers/md/bcache/extents.c | 7 +++++++
drivers/md/bcache/super.c | 4 +++-
3 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index 27a129d47d0a..bbff98661494 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -248,6 +248,19 @@ static void bch_btree_node_read(struct btree *b)
trace_bcache_btree_read(b);
+ /*
+ * KEY_SIZE() comes from disk. Don't read more than the node buffer
+ * holds, or than a bbio has bvecs for.
+ */
+ if (KEY_SIZE(&b->key) > PAGE_SECTORS << b->keys.page_order ||
+ KEY_SIZE(&b->key) > meta_bucket_pages(&b->c->cache->sb) * PAGE_SECTORS) {
+ set_btree_node_io_error(b);
+ bch_cache_set_error(b->c, "btree node too big at bucket %zu: %llu sectors",
+ PTR_BUCKET_NR(b->c, &b->key, 0),
+ KEY_SIZE(&b->key));
+ return;
+ }
+
closure_init_stack(&cl);
bio = bch_bbio_alloc(b->c);
diff --git a/drivers/md/bcache/extents.c b/drivers/md/bcache/extents.c
index d626ffcbecb9..9ddc90049623 100644
--- a/drivers/md/bcache/extents.c
+++ b/drivers/md/bcache/extents.c
@@ -153,6 +153,13 @@ bool __bch_btree_ptr_invalid(struct cache_set *c, const struct bkey *k)
if (!KEY_PTRS(k) || !KEY_SIZE(k) || KEY_DIRTY(k))
goto bad;
+ /*
+ * __ptr_invalid() skips pointers to a device index >= MAX_CACHES_PER_SET,
+ * but btree nodes and uuids are always read from c->cache.
+ */
+ if (KEY_SIZE(k) > c->cache->sb.bucket_size)
+ goto bad;
+
if (__ptr_invalid(c, k))
goto bad;
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 97d9adb0bf96..fd35e4b76fca 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -458,7 +458,9 @@ static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl)
{
struct bkey *k = &j->uuid_bucket;
- if (__bch_btree_ptr_invalid(c, k))
+ /* c->uuids and the bbio hold one meta bucket */
+ if (__bch_btree_ptr_invalid(c, k) ||
+ KEY_SIZE(k) > meta_bucket_pages(&c->cache->sb) * PAGE_SECTORS)
return "bad uuid pointer";
bkey_copy(&c->uuid_bucket, k);
--
2.39.5
next reply other threads:[~2026-11-28 3:36 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-28 3:36 Ozgun Kultekin [this message]
2026-11-30 15:27 ` [PATCH] bcache: don't read more than the buffer holds for a btree node or the uuids Coly Li
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=20261128033609.42213-1-ozgunkultekin@gmail.com \
--to=ozgunkultekin@gmail.com \
--cc=colyli@fygo.io \
--cc=kent.overstreet@linux.dev \
--cc=linux-bcache@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