md: fix use-after-free in md_attr_store()
PATCH 1/1 md: fix use-after-free in md_attr_store()
md_attr_store() calls sysfs_break_active_protection() for a "clear" write to array_state before it calls mddev_get(). sysfs_break_active_protection() does kobject_get() on the mddev kobject. If the array is concurrently being deleted, the kobject refcount can already be zero, which triggers "refcount_t: addition on 0; use-after-free".
Fix by calling mddev_get() first. If the array is being deleted it fails with -EBUSY before the kobject is touched, and if it succeeds the array cannot be freed until mddev_put(). The -EBUSY path no longer needs to call sysfs_unbreak_active_protection().
--- drivers/md/md.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/md/md.c +4 -5
@@ -6163,17 +6163,16 @@ md_attr_store(struct kobject *kobj, struct attribute *attr,if (!capable (CAP_SYS_ADMIN ))return -EACCES ; -if (entry->store == array_state_store &&cmd_match (page,"clear" )) - kn =sysfs_break_active_protection (kobj, attr); -spin_lock (&all_mddevs_lock);if (!mddev_get (mddev)) {spin_unlock (&all_mddevs_lock); -if (kn) -sysfs_unbreak_active_protection (kn);return -EBUSY ; }spin_unlock (&all_mddevs_lock); + +if (entry->store == array_state_store &&cmd_match (page,"clear" )) + kn =sysfs_break_active_protection (kobj, attr); + rv = entry->store (mddev, page, length);/*
Signature · 1 line
2.56.0
Quoted 12 lines · Sajal Gupta
md_attr_store()callssysfs_break_active_protection()for a "clear" write toarray_statebefore it callsmddev_get().sysfs_break_active_protection()doeskobject_get()on the mddev kobject. If the array is concurrently being deleted, the kobject refcount can already be zero, which triggers "refcount_t: addition on 0; use-after-free".Fix by calling
mddev_get()first. If the array is being deleted it fails with-EBUSYbefore the kobject is touched, and if it succeeds the array cannot be freed untilmddev_put(). The-EBUSYpath no longer needs to callsysfs_unbreak_active_protection().
Quoted 8 lines
Fixes 9e59d609763f ("md: call del_gendisk in control path")Reported-by [email protected]Tested-by [email protected]Signed-off-by Sajal Gupta<[email protected]>--- drivers/md/md.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
Applied
Signature · 2 lines
Thanks, Kuai