From: "Coly Li" <colyli@fygo.io>
To: "Ramesh Adhikari" <adhikari.resume@gmail.com>
Cc: <axboe@kernel.dk>, <gregkh@linuxfoundation.org>,
<linux-block@vger.kernel.org>, <stable@vger.kernel.org>
Subject: Re: [PATCH v7 2/2] badblocks: validate sector range and shift before rounding
Date: Wed, 2 Sep 2026 23:17:44 +0800 [thread overview]
Message-ID: <4B2B2780-254F-4702-8A78-2CD3F57C1D1F@fygo.io> (raw)
In-Reply-To: <anQdWeHd2EDZtkzp@studio.local>
> 2026年8月6日 15:22,Coly Li <colyli@fygo.io> 写道:
>
> On Tue, Jul 21, 2026 at 10:10:24PM +0800, Ramesh Adhikari wrote:
>> _badblocks_set(), _badblocks_clear() and badblocks_check() round
>> the caller-supplied [s, s+sectors) range to the current bb->shift
>> block size before touching the bad block table. That rounding
>> was not defensive against a few cases:
>>
>> - s + sectors can overflow sector_t (u64), wrapping the range
>> end before it is ever compared against s.
>>
>> - bb->shift is a plain 'int' field, populated in one case
>> (drivers/md/md.c, from the on-disk superblock's bblog_shift)
>> straight from an unvalidated byte with no upper bound. Shifting
>> by an amount >= the width of the shifted type is undefined
>> behaviour in C; "1 << bb->shift" was shifting an int literal,
>> so this was already undefined for bb->shift >= 32, let alone
>> the full 0-255 range bblog_shift allows.
>>
>> - round_up()/round_down() rounding a value near ULLONG_MAX can
>> itself wrap back to a small value, so even with a valid shift
>> the rounded end of the range could end up smaller than the
>> rounded start, silently turning a small range into a huge one
>> (in _badblocks_clear()/badblocks_check(), which round the end
>> up) or losing the range entirely.
>>
>> Add an explicit s+sectors overflow check and cast the shift
>> operand to sector_t so the shift itself is never performed on a
>> 32-bit int, and detect post-rounding wrap by comparing the
>> rounded result back against the pre-rounding value.
>>
>> badblocks.c does not itself bound bb->shift: every caller except
>> drivers/md/md.c always leaves it at 0, so the one caller that
>> populates it from untrusted on-disk data is responsible for
>> bounding it before assigning it, per struct badblocks's shift
>> field documentation in include/linux/badblocks.h. That md.c-side
>> bound is being sent as a separate patch.
>>
>> badblocks_check() returns 0 rather than -EINVAL on the wrap case,
>> matching its existing "0: no known bad blocks in the range"
>> return convention instead of introducing a new error path callers
>> don't expect.
>>
>> Suggested-by: Coly Li <colyli@fygo.io>
>> Fixes: aa511ff8218b ("badblocks: switch to the improved badblock handling code")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Ramesh Adhikari <adhikari.resume@gmail.com>
>
> The patch looks good to me, thanks.
>
> Reviewed-by: Coly Li <colyli@fygo.io>
>
> BTW, the shift overflow checking in md raid code is accepted by
> md maintainer, so you may submit these 2 patches with my Reviewed-by
> to Jens. That's enough.
Hi Ramesh,
If these patches are not merged yet, I will submit them with my other bcache patches to Jens
later. It might take some time, but won’t be forgot.
Thanks.
Coly Li
next prev parent reply other threads:[~2026-09-02 15:18 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-27 15:10 [PATCH] badblocks: fix infinite loop due to incorrect rounding and overflow Ramesh Adhikari
2026-04-27 15:12 ` Greg KH
2026-04-29 23:06 ` kernel test robot
2026-04-30 5:09 ` kernel test robot
2026-07-04 17:13 ` [PATCH v5] " Ramesh Adhikari
2026-07-09 10:25 ` Coly Li
2026-07-09 13:19 ` [PATCH v6 0/2] badblocks: fix infinite loop and validate sector range/shift Ramesh Adhikari
2026-07-09 13:19 ` [PATCH v6 1/2] badblocks: fix in-place round_up/round_down usage bug Ramesh Adhikari
2026-07-09 15:16 ` Coly Li
2026-07-09 13:19 ` [PATCH v6 2/2] badblocks: validate sector range and shift before rounding Ramesh Adhikari
2026-07-20 7:40 ` Coly Li
2026-07-21 16:40 ` [PATCH v7 0/2] badblocks: fix rounding bug and validate input range Ramesh Adhikari
2026-07-21 16:40 ` [PATCH v7 1/2] badblocks: fix in-place round_up/round_down usage bug Ramesh Adhikari
2026-07-21 16:40 ` [PATCH v7 2/2] badblocks: validate sector range and shift before rounding Ramesh Adhikari
2026-08-06 7:22 ` Coly Li
2026-09-02 15:17 ` Coly Li [this message]
2026-08-04 5:41 ` [PATCH v7 0/2] badblocks: fix rounding bug and validate input range Ramesh Adhikari
2026-08-04 5:49 ` Coly Li
-- strict thread matches above, loose matches on Subject: below --
2026-09-02 16:02 Ramesh Adhikari
2026-09-02 16:02 ` [PATCH v7 2/2] badblocks: validate sector range and shift before rounding Ramesh Adhikari
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=4B2B2780-254F-4702-8A78-2CD3F57C1D1F@fygo.io \
--to=colyli@fygo.io \
--cc=adhikari.resume@gmail.com \
--cc=axboe@kernel.dk \
--cc=gregkh@linuxfoundation.org \
--cc=linux-block@vger.kernel.org \
--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