dm-verity: fix shift-out-of-bounds when computing hash level sizes
PATCH 1/1 dm-verity: fix shift-out-of-bounds when computing hash level sizes
verity_ctr() computes the number of hash blocks at each level of the hash tree as
s = (data_blocks + (1 << n) - 1) >> n
where n = (i + 1) * hash_per_block_bits, i.e. data_blocks / 2^n rounded up.
The loop that counts the levels only stops once hash_per_block_bits * levels is 64 or more, so n can be 64 or larger for the top level. For example, with 1024-byte hash blocks and sha512, hash_per_block_bits is 4, and a large enough number of data blocks gives 16 levels and n = 64. Shifting a 64-bit value by 64 or more is undefined behaviour, and UBSAN reports:
UBSAN: shift-out-of-bounds in drivers/md/dm-verity-target.c:1598:38 shift exponent 64 is too large for 64-bit type 'long long unsigned int'
data_blocks is non-zero here (levels is 0 otherwise) and smaller than 2^64, so data_blocks / 2^n rounded up is 1 whenever n >= 64. Use that value directly instead of shifting.
With this change the syzbot reproducer no longer triggers the report on v7.4-rc4 in QEMU, and the table is still rejected with "Hash device is too small".
---
The code change is mine. An AI assistant (Claude) helped me reproduce the syzbot report, reviewed the change and helped word this description.
drivers/md/dm-verity-target.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/md/dm-verity-target.c +5 -2
@@ -1593,10 +1593,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;if (hash_position + s < hash_position) { ti->error ="Hash device offset overflow" ; r = -E2BIG ;
Signature · 1 line
2.56.0
[email protected]
AI reviewThank 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 --
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 <[email protected]>
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.
Quoted 7 lines
drivers/md/dm-verity-target.c +0
-0@@ -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--) {
drivers/md/dm-verity-target.c static int verity_ctr(struct dm_target *ti, unsigned int argc, char **argv) · line 1592
Quoted 10 lines
drivers/md/dm-verity-target.c +5
-2sector_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).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1