mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hyeonjun Hong <hyeonjunhong330@gmail.com>
To: Coly Li <colyli@fygo.io>, Kent Overstreet <kent.overstreet@linux.dev>
Cc: Jens Axboe <axboe@kernel.dk>,
	linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org,
	Hyeonjun Hong <hyeonjunhong330@gmail.com>,
	Seojin An <love09010224@uos.ac.kr>,
	GEONWOO HAN <gunwoo9643@uos.ac.kr>,
	stable@vger.kernel.org
Subject: [PATCH] bcache: fix out-of-bounds write in bch_data_verify()
Date: Thu, 26 Nov 2026 18:36:55 +0900	[thread overview]
Message-ID: <20261126093655.393608-1-hyeonjunhong330@gmail.com> (raw)

bch_data_verify() sizes its check bio from bio_segments() of the original
read bio, which is an unsigned int.  bio_kmalloc() takes an unsigned short,
so the count is truncated before bio_kmalloc() compares it against
BIO_MAX_INLINE_VECS.  A read bio with 65536 segments is truncated to zero,
passes the limit check and gets a bio with no bio_vec space.  bch_bio_map()
then fills in bio_vecs from bi_size without looking at bi_max_vecs and
writes far beyond the allocation.

A single read bio of this size reaches bcache through an io_uring
registered buffer, which the block layer attaches to the bio without
splitting.  Reading 256 MiB from a cached device with verify enabled on a
KASAN/KFENCE kernel gives:

  BUG: KFENCE: out-of-bounds write in bch_bio_map+0x132/0x420
  Out-of-bounds write at 0x00000000cb5c6057 (132B right of kfence-#252):
   bch_bio_map+0x132/0x420
   bch_data_verify+0x3d2/0xc40
   cached_dev_read_done+0x6d4/0x890
   process_one_work+0xafd/0x1ee0
  kfence-#252: 0x000000008ee01c90-0x000000001f8e8fd6, size=120, cache=kmalloc-128
  allocated by task 12 on cpu 0 at 63.371786s (0.000577s ago):
   bio_kmalloc+0x45/0x80
   bch_data_verify+0x2be/0xc40

followed by a general protection fault and panic.  A 255 MiB read is not
affected because its truncated count still exceeds BIO_MAX_INLINE_VECS and
bio_kmalloc() returns NULL.

The verify path needs CONFIG_BCACHE_DEBUG and the verify attribute to be
set through sysfs.

Check the segment count before it is narrowed and skip verification of
bios that are too large, which is what already happens for every other
count above BIO_MAX_INLINE_VECS.

AI tools assisted the source audit, reproducer construction and patch
drafting.

Fixes: 0f2e6ab851ae ("block: turn the nr_iovecs argument to bio_alloc* into an unsigned short")
Reported-by: Seojin An <love09010224@uos.ac.kr>
Reported-by: GEONWOO HAN <gunwoo9643@uos.ac.kr>
Cc: stable@vger.kernel.org
Signed-off-by: Hyeonjun Hong <hyeonjunhong330@gmail.com>
---
 drivers/md/bcache/debug.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/md/bcache/debug.c b/drivers/md/bcache/debug.c
index 2691a5da3368..fa665b4628b1 100644
--- a/drivers/md/bcache/debug.c
+++ b/drivers/md/bcache/debug.c
@@ -112,6 +112,14 @@ void bch_data_verify(struct cached_dev *dc, struct bio *bio)
 	struct bio_vec bv, cbv;
 	struct bvec_iter iter, citer = { 0 };
 
+	/*
+	 * bio_kmalloc() takes an unsigned short, so a larger segment count
+	 * would be truncated before its own limit check.  Skip verification
+	 * of bios which cannot be represented.
+	 */
+	if (nr_segs > BIO_MAX_INLINE_VECS)
+		return;
+
 	check = bio_kmalloc(nr_segs, GFP_NOIO);
 	if (!check)
 		return;

                 reply	other threads:[~2026-11-26  9:38 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=20261126093655.393608-1-hyeonjunhong330@gmail.com \
    --to=hyeonjunhong330@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=colyli@fygo.io \
    --cc=gunwoo9643@uos.ac.kr \
    --cc=kent.overstreet@linux.dev \
    --cc=linux-bcache@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=love09010224@uos.ac.kr \
    --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

all inboxes | Powered by JetHome®