mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] md: fix use-after-free in md_attr_store()
@ 2026-11-02 12:23 Sajal Gupta
  2026-11-11  1:17 ` yu kuai
  0 siblings, 1 reply; 2+ messages in thread
From: Sajal Gupta @ 2026-11-02 12:23 UTC (permalink / raw)
  To: Song Liu, Yu Kuai
  Cc: Li Nan, Xiao Ni, linux-raid, linux-kernel, Sajal Gupta,
	syzbot+dcad997ed2b5bd95cc5e

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().

Fixes: 9e59d609763f ("md: call del_gendisk in control path")
Reported-by: syzbot+dcad997ed2b5bd95cc5e@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=dcad997ed2b5bd95cc5e
Tested-by: syzbot+dcad997ed2b5bd95cc5e@syzkaller.appspotmail.com
Signed-off-by: Sajal Gupta <sajal2005gupta@gmail.com>
---
 drivers/md/md.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/drivers/md/md.c b/drivers/md/md.c
index 03e35386a802..26f17dd24bdf 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -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);
 
 	/*
-- 
2.56.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] md: fix use-after-free in md_attr_store()
  2026-11-02 12:23 [PATCH] md: fix use-after-free in md_attr_store() Sajal Gupta
@ 2026-11-11  1:17 ` yu kuai
  0 siblings, 0 replies; 2+ messages in thread
From: yu kuai @ 2026-11-11  1:17 UTC (permalink / raw)
  To: Sajal Gupta, Song Liu, yu kuai
  Cc: Li Nan, Xiao Ni, linux-raid, linux-kernel, syzbot+dcad997ed2b5bd95cc5e

在 2026/11/2 20:23, Sajal Gupta 写道:

> 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().
>
> Fixes: 9e59d609763f ("md: call del_gendisk in control path")
> Reported-by:syzbot+dcad997ed2b5bd95cc5e@syzkaller.appspotmail.com
> Closes:https://syzkaller.appspot.com/bug?extid=dcad997ed2b5bd95cc5e
> Tested-by:syzbot+dcad997ed2b5bd95cc5e@syzkaller.appspotmail.com
> Signed-off-by: Sajal Gupta<sajal2005gupta@gmail.com>
> ---
>   drivers/md/md.c | 9 ++++-----
>   1 file changed, 4 insertions(+), 5 deletions(-)
Applied

-- 
Thanks,
Kuai

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-11  1:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-02 12:23 [PATCH] md: fix use-after-free in md_attr_store() Sajal Gupta
2026-11-11  1:17 ` yu kuai

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®