All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, richard.henderson@linaro.org, qemu-devel@nongnu.org
Subject: [PULL 1/8] block/bochs: validate bitmap size and compute extent_blocks after validation
Date: Tue, 17 Nov 2026 18:17:51 +0100	[thread overview]
Message-ID: <20261117171758.237462-2-kwolf@redhat.com> (raw)
In-Reply-To: <20261117171758.237462-1-kwolf@redhat.com>

From: malike <malike@kylinos.cn>

bochs_open() computes bitmap_blocks using the formula
"1 + (le32_to_cpu(bochs.bitmap) - 1) / 512".  le32_to_cpu() returns
uint32_t, so when bochs.bitmap is zero the subtraction wraps to
UINT32_MAX (0xFFFFFFFF) in unsigned arithmetic, yielding
bitmap_blocks = 8388608 instead of 1.

The bitmap field is read directly from the on-disk image header and was
never validated.  The overflowed bitmap_blocks propagates into
seek_to_sector()'s bitmap_offset calculation, causing reads to target
wrong offsets and return I/O errors or incorrect data.

Add an explicit zero check for bochs.bitmap before the formula.

Also move the extent_blocks calculation to after the existing
extent_size validation.  The same "1 + (x - 1) / 512" pattern applies
to extent, but extent==0 is already caught by the "extent_size <
BDRV_SECTOR_SIZE" check; the calculation is moved after that check for
clarity and robustness, and uses the already-validated s->extent_size
instead of re-reading the header field.

Cc: qemu-stable@nongnu.org
Fixes: a8753c3466dc ("Bochs disk image support")
Signed-off-by: malike <malike@kylinos.cn>
Message-ID: <20261014033419.236057-1-malike@kylinos.cn>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/bochs.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/block/bochs.c b/block/bochs.c
index bfda88017d9..7a8091d47d9 100644
--- a/block/bochs.c
+++ b/block/bochs.c
@@ -167,8 +167,13 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
 
     s->data_offset = le32_to_cpu(bochs.header) + (s->catalog_size * 4);
 
+    if (le32_to_cpu(bochs.bitmap) == 0) {
+        error_setg(errp, "Bitmap size must not be zero");
+        ret = -EINVAL;
+        goto fail;
+    }
+
     s->bitmap_blocks = 1 + (le32_to_cpu(bochs.bitmap) - 1) / 512;
-    s->extent_blocks = 1 + (le32_to_cpu(bochs.extent) - 1) / 512;
 
     s->extent_size = le32_to_cpu(bochs.extent);
     if (s->extent_size < BDRV_SECTOR_SIZE) {
@@ -188,6 +193,8 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail;
     }
 
+    s->extent_blocks = 1 + (s->extent_size - 1) / 512;
+
     if (s->catalog_size < DIV_ROUND_UP(bs->total_sectors,
                                        s->extent_size / BDRV_SECTOR_SIZE))
     {
-- 
2.56.1



  reply	other threads:[~2026-11-17 17:19 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 17:17 [PULL 0/8] Block layer patches Kevin Wolf
2026-11-17 17:17 ` Kevin Wolf [this message]
2026-11-17 17:17 ` [PULL 2/8] iotests: better heuristics to find I/O tests build/source dirs Kevin Wolf
2026-11-17 20:06   ` Richard Henderson
2026-11-17 20:50     ` Kevin Wolf
2026-11-17 17:17 ` [PULL 3/8] block/vhdx-log: Improve sanity checks to avoid OOB accesses with bad images Kevin Wolf
2026-11-17 17:17 ` [PULL 4/8] iotests: fix false positive on running-outside-venv warning Kevin Wolf
2026-11-17 17:17 ` [PULL 5/8] block/vhdx: Add alignment checks for region tables Kevin Wolf
2026-11-17 17:17 ` [PULL 6/8] block: read the old child length after draining in bdrv_replace_child_bs() Kevin Wolf
2026-11-17 17:17 ` [PULL 7/8] block: add bdrv_child_notify_resize() to notify a single parent Kevin Wolf
2026-11-17 17:17 ` [PULL 8/8] block: notify only the replaced child in bdrv_replace_child_bs() Kevin Wolf

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=20261117171758.237462-2-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.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 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.