Linux Device Mapper development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jake Joyner" <jake@jakejoyner.com>,
	"Alasdair Kergon" <agk@redhat.com>,
	"Mike Snitzer" <snitzer@kernel.org>,
	"Mikulas Patocka" <mpatocka@redhat.com>,
	"Benjamin Marzinski" <bmarzins@redhat.com>
Cc: dm-devel@lists.linux.dev,
	syzbot+3f8f5b65af8de862dcec@syzkaller.appspotmail.com
Subject: Re: [PATCH] dm-verity: fix shift-out-of-bounds when computing hash level sizes
Date: Mon, 23 Nov 2026 20:42:09 +0000	[thread overview]
Message-ID: <sashiko-outbox-230016@kernel.org> (raw)
In-Reply-To: <20261123203413.145298-1-jake@jakejoyner.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] dm-verity: 64-bit addition overflow in verity_ctr() hash
  level calculation
--

commit cd7aad3c8d1954be46e65f7c6faabc1543f1ca63
Author: Jake Joyner <jake@jakejoyner.com>

dm-verity: fix shift-out-of-bounds when computing hash level sizes

This commit avoids shifting by 64 or more in verity_ctr() when computing
the number of hash blocks at the top level of the hash tree, setting the
level block count directly to 1 when the shift count reaches 64.

> diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c
> index 1c0ce746adf67..553e85aa0a457 100644
> --- a/drivers/md/dm-verity-target.c
> +++ b/drivers/md/dm-verity-target.c
> @@ -1583,10 +1583,13 @@ static int verity_ctr(struct dm_target *ti, unsigned int argc, char **argv)
>  	hash_position = v->hash_start;
>  	for (i = v->levels - 1; i >= 0; i--) {
>  		sector_t s;
> +		unsigned int shift = (i + 1) * v->hash_per_block_bits;
>  
>  		v->hash_level_block[i] = hash_position;
> -		s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1)
> -					>> ((i + 1) * v->hash_per_block_bits);
> +		if (shift >= 64)
> +			s = 1;
> +		else
> +			s = (v->data_blocks + ((sector_t)1 << shift) - 1) >> shift;

[Severity: Medium]
Can the addition in the else branch still overflow sector_t and wrap s to 0
for the lower levels when v->data_blocks is near U64_MAX?

With 512-byte data blocks (v->data_dev_block_bits == SECTOR_SHIFT),
verity_ctr() accepts v->data_blocks up to U64_MAX.  When v->data_blocks is
U64_MAX and the top level has shift >= 64, this sets s = 1 for the top
level while every lower level with 1 <= shift < 64 overflows the 64-bit
addition and computes s = 0.  That leaves v->hash_end at v->hash_start + 1,
allowing the table to pass the dm_bufio_get_device_size() check on a small
hash device and later underflowing the block count in verity_prefetch_io()
when hash_block_end is clamped to v->hash_end - 1:

drivers/md/dm-verity-target.c:verity_prefetch_io() {
    ...
			if (unlikely(hash_block_end >= v->hash_end))
				hash_block_end = v->hash_end - 1;
		}
no_prefetch_cluster:
		dm_bufio_prefetch_with_ioprio(v->bufio, hash_block_start,
					hash_block_end - hash_block_start + 1,
					pw->ioprio);
    ...
}

Since v->data_blocks is always non-zero when v->levels > 0, computing s as
((v->data_blocks - 1) >> shift) + 1 would avoid both the 64-bit addition
overflow and the shift-out-of-bounds when shift == 64 (or for all shift < 64
 alongside the shift >= 64 check).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123203413.145298-1-jake@jakejoyner.com?part=1

      reply	other threads:[~2026-11-23 20:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 20:34 [PATCH] dm-verity: fix shift-out-of-bounds when computing hash level sizes Jake Joyner
2026-11-23 20:42 ` sashiko-bot [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=sashiko-outbox-230016@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agk@redhat.com \
    --cc=bmarzins@redhat.com \
    --cc=dm-devel@lists.linux.dev \
    --cc=jake@jakejoyner.com \
    --cc=mpatocka@redhat.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=snitzer@kernel.org \
    --cc=syzbot+3f8f5b65af8de862dcec@syzkaller.appspotmail.com \
    /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