Skip to content

Commit 9e59d60

Browse files
XiaoNi87YuKuai-huawei
authored andcommitted
md: call del_gendisk in control path
Now del_gendisk and put_disk are called asynchronously in workqueue work. The asynchronous way has a problem that the device node can still exist after mdadm --stop command returns in a short window. So udev rule can open this device node and create the struct mddev in kernel again. So put del_gendisk in control path and still leave put_disk in md_kobj_release to avoid uaf of gendisk. Function del_gendisk can't be called with reconfig_mutex. If it's called with reconfig mutex, a deadlock can happen. del_gendisk waits all sysfs files access to finish and sysfs file access waits reconfig mutex. So put del_gendisk after releasing reconfig mutex. But there is still a window that sysfs can be accessed between mddev_unlock and del_gendisk. So some actions (add disk, change level, .e.g) can happen which lead unexpected results. MD_DELETED is used to resolve this problem. MD_DELETED is set before releasing reconfig mutex and it should be checked for these sysfs access which need reconfig mutex. For sysfs access which don't need reconfig mutex, del_gendisk will wait them to finish. But it doesn't need to do this in function mddev_lock_nointr. There are ten places that call it. * Five of them are in dm raid which we don't need to care. MD_DELETED is only used for md raid. * stop_sync_thread, md_do_sync and md_start_sync are related sync request, and it needs to wait sync thread to finish before stopping an array. * md_ioctl: md_open is called before md_ioctl, so ->openers is added. It will fail to stop the array. So it doesn't need to check MD_DELETED here * md_set_readonly: It needs to call mddev_set_closing_and_sync_blockdev when setting readonly or read_auto. So it will fail to stop the array too because MD_CLOSING is already set. Reviewed-by: Yu Kuai <[email protected]> Signed-off-by: Xiao Ni <[email protected]> Link: https://lore.kernel.org/linux-raid/[email protected] Signed-off-by: Yu Kuai <[email protected]>
1 parent 7e49538 commit 9e59d60

2 files changed

Lines changed: 47 additions & 12 deletions

File tree

drivers/md/md.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -636,9 +636,6 @@ static void __mddev_put(struct mddev *mddev)
636636
mddev->ctime || mddev->hold_active)
637637
return;
638638

639-
/* Array is not configured at all, and not held active, so destroy it */
640-
set_bit(MD_DELETED, &mddev->flags);
641-
642639
/*
643640
* Call queue_work inside the spinlock so that flush_workqueue() after
644641
* mddev_find will succeed in waiting for the work to be done.
@@ -873,6 +870,16 @@ void mddev_unlock(struct mddev *mddev)
873870
kobject_del(&rdev->kobj);
874871
export_rdev(rdev, mddev);
875872
}
873+
874+
/* Call del_gendisk after release reconfig_mutex to avoid
875+
* deadlock (e.g. call del_gendisk under the lock and an
876+
* access to sysfs files waits the lock)
877+
* And MD_DELETED is only used for md raid which is set in
878+
* do_md_stop. dm raid only uses md_stop to stop. So dm raid
879+
* doesn't need to check MD_DELETED when getting reconfig lock
880+
*/
881+
if (test_bit(MD_DELETED, &mddev->flags))
882+
del_gendisk(mddev->gendisk);
876883
}
877884
EXPORT_SYMBOL_GPL(mddev_unlock);
878885

@@ -5774,32 +5781,37 @@ md_attr_store(struct kobject *kobj, struct attribute *attr,
57745781
struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
57755782
struct mddev *mddev = container_of(kobj, struct mddev, kobj);
57765783
ssize_t rv;
5784+
struct kernfs_node *kn = NULL;
57775785

57785786
if (!entry->store)
57795787
return -EIO;
57805788
if (!capable(CAP_SYS_ADMIN))
57815789
return -EACCES;
5790+
5791+
if (entry->store == array_state_store && cmd_match(page, "clear"))
5792+
kn = sysfs_break_active_protection(kobj, attr);
5793+
57825794
spin_lock(&all_mddevs_lock);
57835795
if (!mddev_get(mddev)) {
57845796
spin_unlock(&all_mddevs_lock);
5797+
if (kn)
5798+
sysfs_unbreak_active_protection(kn);
57855799
return -EBUSY;
57865800
}
57875801
spin_unlock(&all_mddevs_lock);
57885802
rv = entry->store(mddev, page, length);
57895803
mddev_put(mddev);
5804+
5805+
if (kn)
5806+
sysfs_unbreak_active_protection(kn);
5807+
57905808
return rv;
57915809
}
57925810

57935811
static void md_kobj_release(struct kobject *ko)
57945812
{
57955813
struct mddev *mddev = container_of(ko, struct mddev, kobj);
57965814

5797-
if (mddev->sysfs_state)
5798-
sysfs_put(mddev->sysfs_state);
5799-
if (mddev->sysfs_level)
5800-
sysfs_put(mddev->sysfs_level);
5801-
5802-
del_gendisk(mddev->gendisk);
58035815
put_disk(mddev->gendisk);
58045816
}
58055817

@@ -6646,8 +6658,9 @@ static int do_md_stop(struct mddev *mddev, int mode)
66466658
mddev->bitmap_info.offset = 0;
66476659

66486660
export_array(mddev);
6649-
66506661
md_clean(mddev);
6662+
set_bit(MD_DELETED, &mddev->flags);
6663+
66516664
if (mddev->hold_active == UNTIL_STOP)
66526665
mddev->hold_active = 0;
66536666
}

drivers/md/md.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,11 +700,26 @@ static inline bool reshape_interrupted(struct mddev *mddev)
700700

701701
static inline int __must_check mddev_lock(struct mddev *mddev)
702702
{
703-
return mutex_lock_interruptible(&mddev->reconfig_mutex);
703+
int ret;
704+
705+
ret = mutex_lock_interruptible(&mddev->reconfig_mutex);
706+
707+
/* MD_DELETED is set in do_md_stop with reconfig_mutex.
708+
* So check it here.
709+
*/
710+
if (!ret && test_bit(MD_DELETED, &mddev->flags)) {
711+
ret = -ENODEV;
712+
mutex_unlock(&mddev->reconfig_mutex);
713+
}
714+
715+
return ret;
704716
}
705717

706718
/* Sometimes we need to take the lock in a situation where
707719
* failure due to interrupts is not acceptable.
720+
* It doesn't need to check MD_DELETED here, the owner which
721+
* holds the lock here can't be stopped. And all paths can't
722+
* call this function after do_md_stop.
708723
*/
709724
static inline void mddev_lock_nointr(struct mddev *mddev)
710725
{
@@ -713,7 +728,14 @@ static inline void mddev_lock_nointr(struct mddev *mddev)
713728

714729
static inline int mddev_trylock(struct mddev *mddev)
715730
{
716-
return mutex_trylock(&mddev->reconfig_mutex);
731+
int ret;
732+
733+
ret = mutex_trylock(&mddev->reconfig_mutex);
734+
if (!ret && test_bit(MD_DELETED, &mddev->flags)) {
735+
ret = -ENODEV;
736+
mutex_unlock(&mddev->reconfig_mutex);
737+
}
738+
return ret;
717739
}
718740
extern void mddev_unlock(struct mddev *mddev);
719741

0 commit comments

Comments
 (0)