Root Cause
The UBSAN shift-out-of-bounds occurs in verity_ctr() (drivers/md/dm-verity-target.c:1590-1591) due to an off-by-one bound on the Merkle tree shift exponent when struct dm_verity::data_blocks is very large.
-
Unconstrained v->data_blocks:
verity_ctr() parses v->data_blocks (sector_t, i.e., u64) from argv[5] at lines 1495–1502, checking only that num_ll << (v->data_dev_block_bits - SECTOR_SHIFT) does not overflow sector_t and that ti->len fits within it (line 1504). When v->data_dev_block_bits == SECTOR_SHIFT (512-byte data blocks), v->data_blocks can be any 64-bit value up to ULLONG_MAX; even with 4096-byte data blocks (data_dev_block_bits == 12), it can be up to (1ULL << 61) - 1.
-
Tree depth calculation allows v->levels * v->hash_per_block_bits >= 64:
v->hash_per_block_bits is computed at lines 1569–1570 (>= 1 because line 1318 requires (1 << v->hash_dev_block_bits) >= v->digest_size * 2). Then lines 1572–1577 calculate v->levels:
v->levels = 0;
if (v->data_blocks)
while (v->hash_per_block_bits * v->levels < 64 &&
(unsigned long long)(v->data_blocks - 1) >>
(v->hash_per_block_bits * v->levels))
v->levels++;
Because the loop checks v->hash_per_block_bits * v->levels < 64 before incrementing v->levels, whenever v->hash_per_block_bits >= 2 and (v->data_blocks - 1) still has nonzero bits after shifting by k * v->hash_per_block_bits (for the largest k with k * v->hash_per_block_bits < 64), v->levels is incremented to k + 1.
v->levels remains <= DM_VERITY_MAX_LEVELS (63, drivers/md/dm-verity.h:21), so the check at line 1579 passes.
- However,
v->levels * v->hash_per_block_bits >= 64 (for example, hash_per_block_bits = 4 and data_blocks > 2^60 yields levels = 16, so 16 * 4 = 64; hash_per_block_bits = 8 and data_blocks > 2^56 yields levels = 8, so 8 * 8 = 64).
-
Shift-out-of-bounds (and 64-bit addition overflow) in level block calculation:
Lines 1586–1598 compute v->hash_level_block[i] and v->hash_end:
for (i = v->levels - 1; i >= 0; i--) {
sector_t s;
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);
...
Unlike verity_position_at_level() (line 115, which shifts by level * v->hash_per_block_bits < 64), this loop shifts by (i + 1) * v->hash_per_block_bits. On the first iteration (i = v->levels - 1), the shift exponent is v->levels * v->hash_per_block_bits >= 64, causing undefined shift-out-of-bounds on both ((sector_t)1 << ...) (line 1590) and >> (...) (line 1591).
In addition, the ceiling-division numerator v->data_blocks + ((sector_t)1 << shift) - 1 can overflow 64-bit sector_t when v->data_blocks is near ULLONG_MAX, wrapping s to 0 across all levels and leaving v->hash_end == v->hash_start (which can later underflow v->hash_end - 1 in verity_prefetch_io() at line 737).
Fix Considerations
- For the top level (
i = v->levels - 1) when v->levels * v->hash_per_block_bits >= 64, the number of root-level hash blocks is simply 1 (or more generally, DIV_ROUND_UP_ULL / ((v->data_blocks - 1) >> shift) + 1 avoids the addition overflow on v->data_blocks, provided shift < 64 is enforced or the top level is handled without shifting by >= 64).