* [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* 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
2026-12-01 9:43 ` [PATCH v2 0/2] bcache: bound the btree node and uuid reads by their buffers Ozgun Kultekin
1 sibling, 0 replies; 6+ 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] 6+ messages in thread* [PATCH v2 0/2] bcache: bound the btree node and uuid reads by their buffers
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 ` Ozgun Kultekin
2026-12-01 9:43 ` [PATCH v2 1/2] bcache: check the btree node size before reading it Ozgun Kultekin
` (2 more replies)
1 sibling, 3 replies; 6+ messages in thread
From: Ozgun Kultekin @ 2026-12-01 9:43 UTC (permalink / raw)
To: Coly Li, Kent Overstreet; +Cc: linux-bcache, Ozgun Kultekin
bch_btree_node_read() and uuid_io() read KEY_SIZE() sectors of a key
that comes from the journal into buffers of a fixed size, and nothing
checks that the key fits. These two patches add that check on the read
side, one patch per read path.
Coly, about your question on v1: bcache never writes such a key itself.
Btree nodes are always written with c->btree_pages pages and the uuid
bucket with meta_bucket_pages() pages, so a cache device that only this
code has written is fine. The bad size has to come from the disk, with
a jset checksum that matches, so it is a device whose metadata was not
written by bcache or was changed later. The jset checksum is a crc64
that catches media errors, but it does not mean the fields are sane,
and the kernel already validates these keys with
__bch_btree_ptr_invalid() before it uses them. That check just does
not compare the size with the buffer the read goes into.
With such a device, registration today reads past the node buffer
(KASAN: slab-out-of-bounds in bch_bio_map() for the PTR_DEV >= 8 case).
With these patches it fails with "btree node too big" or "bad uuid
pointer" and nothing else changes. Tested on v7.4-rc5 with KASAN in
QEMU, with a normal cache device and with modified ones. I can send
the small script that makes the test images if that helps.
Changes since v1:
- Split into two patches, one for the btree node read and one for the
uuid read.
- Dropped the __bch_btree_ptr_invalid() change in extents.c. That
function also validates every btree pointer through
bch_btree_ptr_invalid(), so the change reached further than needed.
The checks in the read paths bound each read by its actual buffer and
bio, which also covers pointers with a device index >=
MAX_CACHES_PER_SET.
v1: https://lore.kernel.org/linux-bcache/20261128033609.42213-1-ozgunkultekin@gmail.com/
Ozgun Kultekin (2):
bcache: check the btree node size before reading it
bcache: check the uuid bucket size before reading it
drivers/md/bcache/btree.c | 13 +++++++++++++
drivers/md/bcache/super.c | 4 +++-
2 files changed, 16 insertions(+), 1 deletion(-)
--
2.39.5
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v2 1/2] bcache: check the btree node size before reading it
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 ` 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
2 siblings, 0 replies; 6+ messages in thread
From: Ozgun Kultekin @ 2026-12-01 9:43 UTC (permalink / raw)
To: Coly Li, 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 holds PAGE_SECTORS <<
b->keys.page_order sectors. The bio itself has meta_bucket_pages()
bvecs. Nothing checks that the key fits in either of them.
page_order comes from btree_order(), which is ilog2() of the key size in
pages and so rounds down. A key whose size is not a power of two pages
is up to twice the size of the buffer. __bch_btree_ptr_invalid() only
limits KEY_SIZE() to the bucket size for pointers with a device index
below MAX_CACHES_PER_SET, while the read always goes to c->cache. So a
bad size in the btree root key in the journal, or in a pointer in a
parent node, makes the read run past the node buffer, and
bch_bio_map() can run past the bvecs of the bbio. This happens before
the node checksum is checked.
Fail the read with a cache set error if the key is larger than the node
buffer or the bio. Nodes that bcache writes are c->btree_pages pages,
which always fits both, 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 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index 27a129d47d0a..86bf08b3e6de 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
+ * or the bio can hold.
+ */
+ 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);
--
2.39.5
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v2 2/2] bcache: check the uuid bucket size before reading it
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 ` Ozgun Kultekin
2026-12-01 10:07 ` [PATCH v2 0/2] bcache: bound the btree node and uuid reads by their buffers Coly Li
2 siblings, 0 replies; 6+ messages in thread
From: Ozgun Kultekin @ 2026-12-01 9:43 UTC (permalink / raw)
To: Coly Li, Kent Overstreet; +Cc: linux-bcache, Ozgun Kultekin, stable
uuid_io() sets the bio size to KEY_SIZE(k) << 9 and maps it onto
c->uuids, which is meta_bucket_pages() pages. The bio has the same
number of bvecs.
On read, the key is jset->uuid_bucket from the journal, and uuid_read()
only checks it with __bch_btree_ptr_invalid(). That limits KEY_SIZE()
to the bucket size, which can be larger than a meta bucket, and only
for pointers with a device index below MAX_CACHES_PER_SET. A larger
key makes the read run past c->uuids, and bch_bio_map() past the bvecs
of the bbio.
Reject a uuid pointer larger than a meta bucket in uuid_read(). The
uuid key that __uuid_write() writes is exactly meta_bucket_pages() *
PAGE_SECTORS sectors, so it is 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/super.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 97d9adb0bf96..166e4f8fa7b8 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 bio 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* Re: [PATCH v2 0/2] bcache: bound the btree node and uuid reads by their buffers
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 ` Coly Li
2 siblings, 0 replies; 6+ messages in thread
From: Coly Li @ 2026-12-01 10:07 UTC (permalink / raw)
To: Ozgun Kultekin; +Cc: Kent Overstreet, linux-bcache
Hi Ozgun,
> 2026年12月1日 17:43,Ozgun Kultekin <ozgunkultekin@gmail.com> 写道:
>
> bch_btree_node_read() and uuid_io() read KEY_SIZE() sectors of a key
> that comes from the journal into buffers of a fixed size, and nothing
> checks that the key fits. These two patches add that check on the read
> side, one patch per read path.
>
> Coly, about your question on v1: bcache never writes such a key itself.
> Btree nodes are always written with c->btree_pages pages and the uuid
> bucket with meta_bucket_pages() pages, so a cache device that only this
> code has written is fine. The bad size has to come from the disk, with
> a jset checksum that matches, so it is a device whose metadata was not
How can a corrupted jset with a match checksum?
> written by bcache or was changed later. The jset checksum is a crc64
> that catches media errors, but it does not mean the fields are sane,
> and the kernel already validates these keys with
> __bch_btree_ptr_invalid() before it uses them. That check just does
> not compare the size with the buffer the read goes into.
>
You don’t explain how can such condition happen in real life without
deliberately composed checksum with broken key size.
Thanks.
Coly Li
> With such a device, registration today reads past the node buffer
> (KASAN: slab-out-of-bounds in bch_bio_map() for the PTR_DEV >= 8 case).
> With these patches it fails with "btree node too big" or "bad uuid
> pointer" and nothing else changes. Tested on v7.4-rc5 with KASAN in
> QEMU, with a normal cache device and with modified ones. I can send
> the small script that makes the test images if that helps.
>
> Changes since v1:
> - Split into two patches, one for the btree node read and one for the
> uuid read.
> - Dropped the __bch_btree_ptr_invalid() change in extents.c. That
> function also validates every btree pointer through
> bch_btree_ptr_invalid(), so the change reached further than needed.
> The checks in the read paths bound each read by its actual buffer and
> bio, which also covers pointers with a device index >=
> MAX_CACHES_PER_SET.
>
> v1: https://lore.kernel.org/linux-bcache/20261128033609.42213-1-ozgunkultekin@gmail.com/
>
> Ozgun Kultekin (2):
> bcache: check the btree node size before reading it
> bcache: check the uuid bucket size before reading it
>
> drivers/md/bcache/btree.c | 13 +++++++++++++
> drivers/md/bcache/super.c | 4 +++-
> 2 files changed, 16 insertions(+), 1 deletion(-)
>
> --
> 2.39.5
>
^ permalink raw reply [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.