mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] md: reject out-of-bounds desc_nr in super_90_validate()
       [not found] <sashiko-outbox-216570@kernel.org>
@ 2026-11-16  8:59 ` Shuai Liu
  0 siblings, 0 replies; only message in thread
From: Shuai Liu @ 2026-11-16  8:59 UTC (permalink / raw)
  To: linux-raid
  Cc: song, yukuai, magiclinan, xiao, linux-kernel,
	syzbot+c5969d7e0a934b384fd6, sashiko-bot, 847807867

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.

Both callers of validate_super() in the hot-add path previously
ignored its return value, so a disk with an out-of-bounds desc_nr
would still be accepted onto the array with the invalid state
intact, and a later md_update_sb() would then write
sb->disks[rdev->desc_nr] out of bounds in super_90_sync() as well.
Make add_bound_rdev() and md_add_new_disk() honor the -EINVAL and
reject the disk instead, which closes that write path too.

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@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=c5969d7e0a934b384fd6
Signed-off-by: Shuai Liu <847807867@qq.com>
---

Changes in v2:
  - make add_bound_rdev() and md_add_new_disk() honor the
    validate_super() return value so a disk with an out-of-bounds
    desc_nr is rejected instead of being hot-added with the invalid
    state intact; this also closes the super_90_sync() OOB write path
    mentioned in v1 (Sashiko)
  - raid1_add_disk() lacking an upper bound on rdev->raid_disk is a
    related pre-existing issue (Sashiko); it is outside the scope of
    this patch and flagged for separate discussion
 drivers/md/md.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/drivers/md/md.c b/drivers/md/md.c
index 03e35386a8028..78ee8d932e71a 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);
@@ -3009,9 +3012,10 @@ static int add_bound_rdev(struct md_rdev *rdev)
 		 * then added disks for geometry changes,
 		 * and should be added immediately.
 		 */
-		super_types[mddev->major_version].
+		err = super_types[mddev->major_version].
 			validate_super(mddev, NULL/*freshest*/, rdev);
-		err = mddev->pers->hot_add_disk(mddev, rdev);
+		if (!err)
+			err = mddev->pers->hot_add_disk(mddev, rdev);
 		if (err) {
 			md_kick_rdev_from_array(rdev);
 			return err;
@@ -7590,9 +7594,11 @@ int md_add_new_disk(struct mddev *mddev, struct mdu_disk_info_s *info)
 			} else
 				rdev->raid_disk = -1;
 			rdev->saved_raid_disk = rdev->raid_disk;
-		} else
-			super_types[mddev->major_version].
-				validate_super(mddev, NULL/*freshest*/, rdev);
+		} else if (super_types[mddev->major_version].
+			   validate_super(mddev, NULL/*freshest*/, rdev)) {
+			export_rdev(rdev);
+			return -EINVAL;
+		}
 		if ((info->state & (1<<MD_DISK_SYNC)) &&
 		     rdev->raid_disk != info->raid_disk) {
 			/* This was a hot-add request, but events doesn't
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-16  8:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <sashiko-outbox-216570@kernel.org>
2026-11-16  8:59 ` [PATCH v2] md: reject out-of-bounds desc_nr in super_90_validate() Shuai Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®