UBSAN: shift-out-of-bounds in verity_ctr

Status: upstream: reported C repro on 2026/11/23 19:05
Subsystems: dm
Labels: prio:low
[Documentation on labels]
Reported-by: syzbot+3f8f5b65af8de862dcec@syzkaller.appspotmail.com
First crash: 1h25m, last: 38m
✨ AI cause bisection: introduced by (AI job) :
commit b1bf2de07271932326af847a3c6a01fdfd29d4be
Author: Mikulas Patocka <mpatocka@redhat.com>
Date: Wed Jul 10 22:41:16 2013 +0000

  dm verity: fix inability to use a few specific devices sizes

✨ AI-generated root cause summary:

In verity_ctr() in drivers/md/dm-verity-target.c, the loop computing v->levels checks v->hash_per_block_bits * v->levels < 64 before incrementing v->levels, allowing v->levels * v->hash_per_block_bits to reach 64 or more while still passing the DM_VERITY_MAX_LEVELS (63) check. When computing the block count s for the top Merkle tree level (i = v->levels - 1), verity_ctr() shifts 64-bit sector_t values by (i + 1) * v->hash_per_block_bits (>= 64), triggering UBSAN shift-out-of-bounds. Additionally, the numerator v->data_blocks + ((sector_t)1 << shift) - 1 is not protected against 64-bit sector_t overflow.

▶ ▼ ✨ Full AI root cause analysis
Based on kernel commit a041bdbe1f3a:

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.

  1. 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.

  2. 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).
  3. 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).
▶ ▼ ✨ AI Jobs (2)
ID Workflow Result Correct Bug Created Started Finished Revision
8e0ed690 bisect-cause IsRelease: ❌ ❓ UBSAN: shift-out-of-bounds in verity_ctr 2026/11/23 18:18 2026/11/23 18:18 2026/11/23 18:20 fab64d89
b1b89bb7 assessment-security DenialOfService: ✅ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ❌ UserNamespace: ❌ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ UBSAN: shift-out-of-bounds in verity_ctr 2026/11/23 18:17 2026/11/23 18:18 2026/11/23 18:23 fab64d89
▶ ▼ Discussions (1)
Title Replies (including bot) Last reply
[syzbot] [dm?] UBSAN: shift-out-of-bounds in verity_ctr 0 (1) 2026/11/23 19:05
▶ ▼ Similar bugs (1)
Kernel Title Rank 🛈 Repro Cause bisect Fix bisect Count Last Reported Patched Status
android-6-12 UBSAN: shift-out-of-bounds in verity_ctr origin:upstream -1 syz, C 2 now 14d 0/1 premoderation: reported C repro on 2026/11/09 00:19

Sample crash report:
------------[ cut here ]------------
UBSAN: shift-out-of-bounds in drivers/md/dm-verity-target.c:1590:38
shift exponent 64 is too large for 64-bit type 'sector_t' (aka 'unsigned long long')
CPU: 1 UID: 0 PID: 4949 Comm: syz.0.17 Not tainted syzkaller #0 PREEMPT 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
Call trace:
 show_stack+0x2c/0x3c arch/arm64/kernel/stacktrace.c:499 (C)
 __dump_stack+0x30/0x40 lib/dump_stack.c:94
 dump_stack_lvl+0xd8/0x12c lib/dump_stack.c:120
 dump_stack+0x1c/0x28 lib/dump_stack.c:129
 ubsan_epilogue+0x14/0x48 lib/ubsan.c:233
 __ubsan_handle_shift_out_of_bounds+0x224/0x264 lib/ubsan.c:494
 verity_ctr+0xfc8/0x1660 drivers/md/dm-verity-target.c:1590
 dm_table_add_target+0x44c/0xb18 drivers/md/dm-table.c:749
 populate_table drivers/md/dm-ioctl.c:1602 [inline]
 table_load+0x27c/0x764 drivers/md/dm-ioctl.c:1645
 ctl_ioctl drivers/md/dm-ioctl.c:2221 [inline]
 dm_ctl_ioctl+0x884/0xbbc drivers/md/dm-ioctl.c:2243
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:597 [inline]
 __se_sys_ioctl fs/ioctl.c:583 [inline]
 __arm64_sys_ioctl+0x154/0x1cc fs/ioctl.c:583
 __invoke_syscall arch/arm64/kernel/syscall.c:35 [inline]
 invoke_syscall+0x98/0x244 arch/arm64/kernel/syscall.c:49
 el0_svc_common+0xec/0x23c arch/arm64/kernel/syscall.c:121
 do_el0_svc+0x4c/0x5c arch/arm64/kernel/syscall.c:140
 el0_svc+0x60/0x1d8 arch/arm64/kernel/entry-common.c:797
 el0t_64_sync_handler+0x44/0x104 arch/arm64/kernel/entry-common.c:816
 el0t_64_sync+0x198/0x19c arch/arm64/kernel/entry.S:606
---[ end trace ]---
------------[ cut here ]------------
UBSAN: shift-out-of-bounds in drivers/md/dm-verity-target.c:1591:6
shift exponent 64 is too large for 64-bit type 'sector_t' (aka 'unsigned long long')
CPU: 1 UID: 0 PID: 4949 Comm: syz.0.17 Not tainted syzkaller #0 PREEMPT 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
Call trace:
 show_stack+0x2c/0x3c arch/arm64/kernel/stacktrace.c:499 (C)
 __dump_stack+0x30/0x40 lib/dump_stack.c:94
 dump_stack_lvl+0xd8/0x12c lib/dump_stack.c:120
 dump_stack+0x1c/0x28 lib/dump_stack.c:129
 ubsan_epilogue+0x14/0x48 lib/ubsan.c:233
 __ubsan_handle_shift_out_of_bounds+0x224/0x264 lib/ubsan.c:494
 verity_ctr+0x1000/0x1660 drivers/md/dm-verity-target.c:1591
 dm_table_add_target+0x44c/0xb18 drivers/md/dm-table.c:749
 populate_table drivers/md/dm-ioctl.c:1602 [inline]
 table_load+0x27c/0x764 drivers/md/dm-ioctl.c:1645
 ctl_ioctl drivers/md/dm-ioctl.c:2221 [inline]
 dm_ctl_ioctl+0x884/0xbbc drivers/md/dm-ioctl.c:2243
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:597 [inline]
 __se_sys_ioctl fs/ioctl.c:583 [inline]
 __arm64_sys_ioctl+0x154/0x1cc fs/ioctl.c:583
 __invoke_syscall arch/arm64/kernel/syscall.c:35 [inline]
 invoke_syscall+0x98/0x244 arch/arm64/kernel/syscall.c:49
 el0_svc_common+0xec/0x23c arch/arm64/kernel/syscall.c:121
 do_el0_svc+0x4c/0x5c arch/arm64/kernel/syscall.c:140
 el0_svc+0x60/0x1d8 arch/arm64/kernel/entry-common.c:797
 el0t_64_sync_handler+0x44/0x104 arch/arm64/kernel/entry-common.c:816
 el0t_64_sync+0x198/0x19c arch/arm64/kernel/entry.S:606
---[ end trace ]---
device-mapper: table: 248:0: verity: Hash device is too small (-E2BIG)
device-mapper: ioctl: error adding target to table

Crashes (2):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/23 19:04 git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-kernelci a041bdbe1f3a e9bfbe3a .config console log report syz / log C [disk image] [vmlinux] [kernel image] ci-upstream-gce-arm64 UBSAN: shift-out-of-bounds in verity_ctr
2026/11/23 18:17 git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-kernelci a041bdbe1f3a e9bfbe3a .config console log report info [disk image] [vmlinux] [kernel image] ci-upstream-gce-arm64 UBSAN: shift-out-of-bounds in verity_ctr
* Struck through repros no longer work on HEAD.