Linux RAID subsystem development
 help / color / mirror / Atom feed
From: Edward Adam Davis <eadavis@sina.com>
To: syzbot+08318698e045b4b85f51@syzkaller.appspotmail.com
Cc: linux-kernel@vger.kernel.org, peterz@infradead.org,
	syzkaller-bugs@googlegroups.com, tglx@kernel.org,
	bigeasy@linutronix.de, song@kernel.org, yukuai@fygo.io,
	magiclinan@didiglobal.com, xiao@kernel.org,
	linux-raid@vger.kernel.org
Subject: [PATCH] md/raid5: Cancel remove instance if adding it fails
Date: Fri, 30 Oct 2026 18:56:14 +0800	[thread overview]
Message-ID: <20261030105614.378971-1-eadavis@sina.com> (raw)
In-Reply-To: <6ae34c61.bc88765f.313a3f.00b3.GAE@google.com>

The failure of raid456_cpu_up_prepare() to allocate memory causes
cpuhp_state_add_instance() to fail; consequently, the corresponding
instance is not initialized, potentially hitting [1].

Additionally, there is another null-ptr-deref issue: conf->mddev is
assigned too late, meaning that any error-handling exit occurring before
this assignment, which jumps to the abort label, potentially hitting [2].

[1]
KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007]
Call Trace:
 cpuhp_state_remove_instance include/linux/cpuhotplug.h:480 [inline]
 raid5_free_percpu drivers/md/raid5.c:7541 [inline]
 free_conf+0x238/0x710 drivers/md/raid5.c:7554
 setup_conf+0xd80/0x2a20 drivers/md/raid5.c:7885
 raid5_run+0x1260/0x1670 drivers/md/raid5.c:8129

[2]
BUG: KASAN: null-ptr-deref in free_conf+0xfd/0x7e0
Read of size 8 at addr 0000000000000028 by task rp0/9459
Call Trace:
 free_conf+0xfd/0x7e0
 setup_conf.cold+0x6e/0x4a1
 raid5_run+0x1260/0x1670

Add a check before removing an instance, if the instance was not successfully
added, skip the removal process. Move the assignment of conf->mddev after the
memory allocation for conf.

Fixes: 29c6d1bbd7a2 ("md/raid5: Convert to hotplug state machine")
Reported-by: syzbot+08318698e045b4b85f51@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=08318698e045b4b85f51
Signed-off-by: Edward Adam Davis <eadavis@sina.com>
---
 drivers/md/raid5.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c
index b91545ce090d..e1f9ec8f25be 100644
--- a/drivers/md/raid5.c
+++ b/drivers/md/raid5.c
@@ -7538,7 +7538,8 @@ static void raid5_free_percpu(struct r5conf *conf)
 	if (!conf->percpu)
 		return;
 
-	cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
+	if (!hlist_unhashed(&conf->node))
+		cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
 	free_percpu(conf->percpu);
 }
 
@@ -7675,6 +7676,7 @@ static struct r5conf *setup_conf(struct mddev *mddev)
 	if (conf == NULL)
 		goto abort;
 
+	conf->mddev = mddev;
 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
 	conf->stripe_size = DEFAULT_STRIPE_SIZE;
 	conf->stripe_shift = ilog2(DEFAULT_STRIPE_SIZE) - 9;
@@ -7743,7 +7745,6 @@ static struct r5conf *setup_conf(struct mddev *mddev)
 	ret = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
 	if (ret)
 		goto abort;
-	conf->mddev = mddev;
 
 	ret = -ENOMEM;
 	conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL);
-- 
2.43.0


           reply	other threads:[~2026-10-30 10:56 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <6ae34c61.bc88765f.313a3f.00b3.GAE@google.com>]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261030105614.378971-1-eadavis@sina.com \
    --to=eadavis@sina.com \
    --cc=bigeasy@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=magiclinan@didiglobal.com \
    --cc=peterz@infradead.org \
    --cc=song@kernel.org \
    --cc=syzbot+08318698e045b4b85f51@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    --cc=tglx@kernel.org \
    --cc=xiao@kernel.org \
    --cc=yukuai@fygo.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox