[PATCH] md: reject out-of-bounds desc_nr in super_90_validate()

From: Shuai Liu

Date: Mon Nov 16 2026 - 03:32:55 EST


super_90_validate() indexes the on-disk disk descriptor array with
rdev->desc_nr without any bounds check:

desc = sb->disks + rdev->desc_nr;
if (desc->state & (1<<MD_DISK_FAULTY))
...

mdp_super_t carries only MD_SB_DISKS (27) descriptors of 128 bytes
each starting at offset 512, so any desc_nr outside [0, MD_SB_DISKS)
reads past the end of the 4KB superblock page.

Such an out-of-range desc_nr is reachable in several ways:

1. On a running non-persistent array, ADD_NEW_DISK with the
MD_DISK_JOURNAL flag reaches add_bound_rdev(), which calls
validate_super() unconditionally for journal disks.
bind_rdev_to_array() auto-assigns desc_nr = mddev->raid_disks to
a new rdev, and its max_disks check is skipped both for journal
disks and when mddev->max_disks == 0 (the case for non-persistent
arrays, which never set it). With raid_disks > MD_SB_DISKS the
assigned desc_nr points past the descriptor array.

2. During assembly, rdev->desc_nr comes straight from the on-disk
sb->this_disk.number, which is fully attacker-controlled fuzz
input.

syzbot hit path 1: a RAID1 array with raid_disks=35 and
not_persistent=1 plus a hot-added journal disk made
super_90_validate() read sb->disks[35].state at byte offset
512 + 35 * 128 + 16 = 5008, i.e. 912 bytes into the page following
the 4096-byte superblock page:

BUG: KASAN: use-after-free in super_90_validate+0x178b/0x18c0 drivers/md/md.c:1592
Read of size 4 at addr ffff888073d36390 by task syz-executor291/6009
...
super_90_validate+0x178b/0x18c0 drivers/md/md.c:1592
add_bound_rdev+0x274/0x390 drivers/md/md.c:3012
md_add_new_disk+0xca1/0x1820 drivers/md/md.c:7666
md_ioctl+0x2c1d/0x3700 drivers/md/md.c:8525
blkdev_ioctl+0x5ad/0x6f0 block/ioctl.c:798

(The KASAN report shows use-after-free or slab-out-of-bounds
depending on what occupies the neighbouring memory.)

super_1_validate() already bounds desc_nr against sb->max_dev; the
0.90 metadata path has no equivalent check. Reject desc_nr outside
[0, MD_SB_DISKS) with -EINVAL, which covers both the desc->state
reads in super_90_validate() itself and keeps fuzzed on-disk
sb->this_disk.number values from being used as an array index.

Note that super_90_sync() also writes sb->disks[] indexed by
desc_nr; on a persistent array a journal disk can still bypass the
bind_rdev_to_array() max_disks check and reach that write. Reaching
it additionally requires a crafted on-disk superblock, so it is left
for follow-up discussion.

Verified with the syzbot reproducer under QEMU with KASAN (7.4-rc3
based config): baseline reproduces the use-after-free on every run,
with this patch the reproducer completes with zero KASAN reports
across repeated runs.

Reported-by: syzbot+c5969d7e0a934b384fd6@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=c5969d7e0a934b384fd6
Signed-off-by: Shuai Liu <847807867@xxxxxx>
---
drivers/md/md.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/md/md.c b/drivers/md/md.c
index 03e35386a8028..83009b0d84092 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -1495,6 +1495,9 @@ static int super_90_validate(struct mddev *mddev, struct md_rdev *freshest, stru
mdp_super_t *sb = page_address(rdev->sb_page);
__u64 ev1 = md_event(sb);

+ if (rdev->desc_nr < 0 || rdev->desc_nr >= MD_SB_DISKS)
+ return -EINVAL;
+
rdev->raid_disk = -1;
clear_bit(Faulty, &rdev->flags);
clear_bit(In_sync, &rdev->flags);
--
2.43.0