Linux bcache driver list
 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
  0 siblings, 1 reply; 2+ 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] 2+ messages in thread

* Re: [PATCH] bcache: don't read more than the buffer holds for a btree node or the uuids
  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
  0 siblings, 0 replies; 2+ messages in thread
From: Coly Li @ 2026-11-30 15:27 UTC (permalink / raw)
  To: Ozgun Kultekin; +Cc: kent.overstreet, linux-bcache, stable

Hi Ozgun,

On Fri, Nov 27, 2026 at 07:36:09PM +0800, Ozgun Kultekin wrote:
> 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.
> 

My question is, how can the above condition happens? I mean "a 1023
sector btree pointer in a 1024 sector bucket". Firstly you need to
provide the detail code location where such circomstances can happen
in bcache code. Otherwide I don't take this as a valid defact.


> __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.
> 

Same here, before solve a problem, I want to see how the problem may
take place by the current code or reasonable configuration.

Some situations will be treated as invalid by me, e.g. deliberatedly
compose a hash collision in journal checksum with a valid KEY value.
We cannot avoid such deliberated on-media hash colission, and won't
fix the addressed condition of this patch in kernel code like bcache.


> 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.
> 

So before you fix this 'problem', I want to see how such issue may
take place in real live.

Thanks.

Coly Li

> 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	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30 15:27 UTC | newest]

Thread overview: 2+ 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox