All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] bcache: don't read more than the buffer holds for a btree node or the uuids
@ 2026-11-28  3:36 Ozgun Kultekin
  2026-11-30 15:27 ` Coly Li
  2026-12-01  9:43 ` [PATCH v2 0/2] bcache: bound the btree node and uuid reads by their buffers Ozgun Kultekin
  0 siblings, 2 replies; 6+ messages in thread
From: Ozgun Kultekin @ 2026-11-28  3:36 UTC (permalink / raw)
  To: colyli, kent.overstreet; +Cc: linux-bcache, Ozgun Kultekin, stable

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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-01 10:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28  3:36 [PATCH] bcache: don't read more than the buffer holds for a btree node or the uuids Ozgun Kultekin
2026-11-30 15:27 ` Coly Li
2026-12-01  9:43 ` [PATCH v2 0/2] bcache: bound the btree node and uuid reads by their buffers Ozgun Kultekin
2026-12-01  9:43   ` [PATCH v2 1/2] bcache: check the btree node size before reading it Ozgun Kultekin
2026-12-01  9:43   ` [PATCH v2 2/2] bcache: check the uuid bucket " Ozgun Kultekin
2026-12-01 10:07   ` [PATCH v2 0/2] bcache: bound the btree node and uuid reads by their buffers Coly Li

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.