]> git.baikalelectronics.ru Git - kernel.git/commitdiff
dm raid: fix address sanitizer warning in raid_status
authorMikulas Patocka <mpatocka@redhat.com>
Sun, 24 Jul 2022 18:31:35 +0000 (14:31 -0400)
committerMike Snitzer <snitzer@kernel.org>
Thu, 28 Jul 2022 21:29:56 +0000 (17:29 -0400)
There is this warning when using a kernel with the address sanitizer
and running this testsuite:
https://gitlab.com/cki-project/kernel-tests/-/tree/main/storage/swraid/scsi_raid

==================================================================
BUG: KASAN: slab-out-of-bounds in raid_status+0x1747/0x2820 [dm_raid]
Read of size 4 at addr ffff888079d2c7e8 by task lvcreate/13319
CPU: 0 PID: 13319 Comm: lvcreate Not tainted 5.18.0-0.rc3.<snip> #1
Hardware name: Red Hat KVM, BIOS 0.5.1 01/01/2011
Call Trace:
 <TASK>
 dump_stack_lvl+0x6a/0x9c
 print_address_description.constprop.0+0x1f/0x1e0
 print_report.cold+0x55/0x244
 kasan_report+0xc9/0x100
 raid_status+0x1747/0x2820 [dm_raid]
 dm_ima_measure_on_table_load+0x4b8/0xca0 [dm_mod]
 table_load+0x35c/0x630 [dm_mod]
 ctl_ioctl+0x411/0x630 [dm_mod]
 dm_ctl_ioctl+0xa/0x10 [dm_mod]
 __x64_sys_ioctl+0x12a/0x1a0
 do_syscall_64+0x5b/0x80

The warning is caused by reading conf->max_nr_stripes in raid_status. The
code in raid_status reads mddev->private, casts it to struct r5conf and
reads the entry max_nr_stripes.

However, if we have different raid type than 4/5/6, mddev->private
doesn't point to struct r5conf; it may point to struct r0conf, struct
r1conf, struct r10conf or struct mpconf. If we cast a pointer to one
of these structs to struct r5conf, we will be reading invalid memory
and KASAN warns about it.

Fix this bug by reading struct r5conf only if raid type is 4, 5 or 6.

Cc: stable@vger.kernel.org
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Signed-off-by: Mike Snitzer <snitzer@kernel.org>
drivers/md/dm-raid.c

index a438efc70e8772e1894bbb5ef05e37261ebc9eab..f41fd3cf5c7d0233bd037134c74bd4a32a48af75 100644 (file)
@@ -3507,7 +3507,7 @@ static void raid_status(struct dm_target *ti, status_type_t type,
 {
        struct raid_set *rs = ti->private;
        struct mddev *mddev = &rs->md;
-       struct r5conf *conf = mddev->private;
+       struct r5conf *conf = rs_is_raid456(rs) ? mddev->private : NULL;
        int i, max_nr_stripes = conf ? conf->max_nr_stripes : 0;
        unsigned long recovery;
        unsigned int raid_param_cnt = 1; /* at least 1 for chunksize */