From: Sergey Shtylyov <s.shtylyov@auroraos.dev>
To: Jens Axboe <axboe@kernel.dk>, <linux-block@vger.kernel.org>
Cc: Sergey Shtylyov <s.shtylyov@auroraos.dev>,
<linux-kernel@vger.kernel.org>,
Karina Yankevich <k.yankevich@auroraos.dev>
Subject: [PATCH] block: bio: check offset/length sanity in {__,}bio_add_page()
Date: Wed, 24 Jun 2026 23:33:26 +0300 [thread overview]
Message-ID: <20260624203327.25553-1-s.shtylyov@auroraos.dev> (raw)
Sum of the *struct* bio_vec's fields bv_offset and bv_len is calculated in
some functions in block/{blk-merge.c,blk.h> (and that sum is often compared
to PAGE_SIZE) -- that sum may overflow (and so the comparison yield a wrong
result) if some bad arguments were previusly passed to {__,}bio_add_page().
Add a check that the sum of the offset and length parameters won't overflow
to {__,}bio_add_page()...
Found by Linux Verification Center (linuxtesting.org) with the Svace static
analysis tool.
Signed-off-by: Sergey Shtylyov <s.shtylyov@auroraos.dev>
---
The patch is against the for-next branch of Jens Axboeu's linux.git repo...
block/bio.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/block/bio.c b/block/bio.c
index f2a5f4d0a967..daca63b94fae 100644
--- a/block/bio.c
+++ b/block/bio.c
@@ -1000,6 +1000,7 @@ void __bio_add_page(struct bio *bio, struct page *page,
{
WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
WARN_ON_ONCE(bio_full(bio, len));
+ WARN_ON_ONCE(off + len < off); /* does the sum overflow? */
if (is_pci_p2pdma_page(page))
bio->bi_opf |= REQ_NOMERGE;
@@ -1045,6 +1046,9 @@ int bio_add_page(struct bio *bio, struct page *page,
return 0;
if (bio->bi_iter.bi_size > BIO_MAX_SIZE - len)
return 0;
+ /* Are offset and len sane, i.e. their sum doesn't overflow? */
+ if (offset + len < offset)
+ return 0;
if (bio->bi_vcnt > 0) {
struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
--
2.54.0
next reply other threads:[~2026-06-24 20:49 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-24 20:33 Sergey Shtylyov [this message]
2026-06-26 6:12 ` Christoph Hellwig
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=20260624203327.25553-1-s.shtylyov@auroraos.dev \
--to=s.shtylyov@auroraos.dev \
--cc=axboe@kernel.dk \
--cc=k.yankevich@auroraos.dev \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@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®