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

Hi Hyeonjun,

On Thu, Nov 26, 2026 at 06:36:55PM +0800, Hyeonjun Hong wrote:
> 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.
> 

The fix makes sense. Thanks for the patch up. I take this for 7.5 submission.


Coly Li

> 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 16:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  9:36 Hyeonjun Hong
2026-11-26 16:12 ` Coly Li [this message]

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=awhLVBrBR51A6KmN@studio.local \
    --to=colyli@fygo.io \
    --cc=axboe@kernel.dk \
    --cc=gunwoo9643@uos.ac.kr \
    --cc=hyeonjunhong330@gmail.com \
    --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®