linux-stable/drivers/md
Yu Kuai e2a9f73ee4 md: fix mddev uaf while iterating all_mddevs list
commit 8542870237 upstream.

While iterating all_mddevs list from md_notify_reboot() and md_exit(),
list_for_each_entry_safe is used, and this can race with deletint the
next mddev, causing UAF:

t1:
spin_lock
//list_for_each_entry_safe(mddev, n, ...)
 mddev_get(mddev1)
 // assume mddev2 is the next entry
 spin_unlock
            t2:
            //remove mddev2
            ...
            mddev_free
            spin_lock
            list_del
            spin_unlock
            kfree(mddev2)
 mddev_put(mddev1)
 spin_lock
 //continue dereference mddev2->all_mddevs

The old helper for_each_mddev() actually grab the reference of mddev2
while holding the lock, to prevent from being freed. This problem can be
fixed the same way, however, the code will be complex.

Hence switch to use list_for_each_entry, in this case mddev_put() can free
the mddev1 and it's not safe as well. Refer to md_seq_show(), also factor
out a helper mddev_put_locked() to fix this problem.

Cc: Christoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/linux-raid/20250220124348.845222-1-yukuai1@huaweicloud.com
Fixes: f265143422 ("md: stop using for_each_mddev in md_notify_reboot")
Fixes: 16648bac86 ("md: stop using for_each_mddev in md_exit")
Reported-and-tested-by: Guillaume Morin <guillaume@morinfr.org>
Closes: https://lore.kernel.org/all/Z7Y0SURoA8xwg7vn@bender.morinfr.org/
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Cc: Salvatore Bonaccorso <carnil@debian.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2025-04-25 10:48:04 +02:00
..
bcache
dm-vdo dm vdo: add missing spin_lock_init 2025-03-07 18:25:44 +01:00
persistent-data
dm-audit.c
dm-audit.h
dm-bio-prison-v1.c
dm-bio-prison-v1.h
dm-bio-prison-v2.c
dm-bio-prison-v2.h
dm-bio-record.h
dm-bufio.c
dm-builtin.c
dm-cache-background-tracker.c
dm-cache-background-tracker.h
dm-cache-block-types.h
dm-cache-metadata.c
dm-cache-metadata.h
dm-cache-policy-internal.h
dm-cache-policy-smq.c
dm-cache-policy.c
dm-cache-policy.h
dm-cache-target.c
dm-clone-metadata.c
dm-clone-metadata.h
dm-clone-target.c
dm-core.h
dm-crypt.c dm-crypt: track tag_offset in convert_context 2025-02-17 10:05:19 +01:00
dm-delay.c
dm-dust.c
dm-ebs-target.c dm-ebs: fix prefetch-vs-suspend race 2025-04-20 10:15:53 +02:00
dm-era-target.c
dm-exception-store.c
dm-exception-store.h
dm-flakey.c dm-flakey: Fix memory corruption in optional corrupt_bio_byte feature 2025-03-22 12:54:25 -07:00
dm-ima.c
dm-ima.h
dm-init.c
dm-integrity.c dm-integrity: fix non-constant-time tag verification 2025-04-20 10:15:53 +02:00
dm-io-rewind.c
dm-io-tracker.h
dm-io.c
dm-ioctl.c
dm-kcopyd.c
dm-linear.c
dm-log-userspace-base.c
dm-log-userspace-transfer.c
dm-log-userspace-transfer.h
dm-log-writes.c
dm-log.c
dm-mpath.c
dm-mpath.h
dm-path-selector.c
dm-path-selector.h
dm-ps-historical-service-time.c
dm-ps-io-affinity.c
dm-ps-queue-length.c
dm-ps-round-robin.c
dm-ps-service-time.c
dm-raid1.c
dm-raid.c
dm-region-hash.c
dm-rq.c
dm-rq.h
dm-snap-persistent.c
dm-snap-transient.c
dm-snap.c
dm-stats.c
dm-stats.h
dm-stripe.c
dm-switch.c
dm-sysfs.c
dm-table.c
dm-target.c
dm-thin-metadata.c
dm-thin-metadata.h
dm-thin.c
dm-uevent.c
dm-uevent.h
dm-unstripe.c
dm-verity-fec.c
dm-verity-fec.h
dm-verity-loadpin.c
dm-verity-target.c dm-verity: fix prefetch-vs-suspend race 2025-04-20 10:15:53 +02:00
dm-verity-verify-sig.c
dm-verity-verify-sig.h
dm-verity.h
dm-writecache.c
dm-zero.c
dm-zone.c
dm-zoned-metadata.c
dm-zoned-reclaim.c
dm-zoned-target.c
dm-zoned.h
dm.c
dm.h
Kconfig md: reintroduce md-linear 2025-02-17 10:05:04 +01:00
Makefile md: reintroduce md-linear 2025-02-17 10:05:04 +01:00
md-autodetect.c md: reintroduce md-linear 2025-02-17 10:05:04 +01:00
md-bitmap.c md/md-bitmap: fix stats collection for external bitmaps 2025-04-25 10:47:38 +02:00
md-bitmap.h
md-cluster.c
md-cluster.h
md-linear.c md: Fix linear_set_limits() 2025-02-17 10:05:47 +01:00
md.c md: fix mddev uaf while iterating all_mddevs list 2025-04-25 10:48:04 +02:00
md.h md/md-bitmap: move bitmap_{start, end}write to md upper layer 2025-02-08 09:58:12 +01:00
raid0.c md/raid*: Fix the set_queue_limits implementations 2025-02-27 04:30:19 -08:00
raid0.h
raid1-10.c
raid1.c md/raid*: Fix the set_queue_limits implementations 2025-02-27 04:30:19 -08:00
raid1.h
raid5-cache.c md/md-bitmap: move bitmap_{start, end}write to md upper layer 2025-02-08 09:58:12 +01:00
raid5-log.h
raid5-ppl.c
raid5.c md/md-bitmap: move bitmap_{start, end}write to md upper layer 2025-02-08 09:58:12 +01:00
raid5.h md/md-bitmap: move bitmap_{start, end}write to md upper layer 2025-02-08 09:58:12 +01:00
raid10.c md/raid10: fix missing discard IO accounting 2025-04-25 10:47:38 +02:00
raid10.h