Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, Krzysztof Kozlowski <krzk@kernel.org>,
	Evgeniy Polyakov <zbr@ioremap.net>, David Fries <David@Fries.net>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH 2/6] w1: Synchronise thread teardown with netlink callbacks
Date: Wed,  2 Dec 2026 09:36:44 +0000	[thread overview]
Message-ID: <40c82de262ee4eaf8609b99520fc6861206f37d4.1796204137.git.lee@kernel.org> (raw)
In-Reply-To: <cover.1796204137.git.lee@kernel.org>

When __w1_remove_master_device() stops dev->thread via kthread_stop(),
a concurrent w1_cn_callback() that already holds a reference on dev can
still call wake_up_process(dev->thread) after the kthread task_struct
has been freed, causing a use-after-free.  In addition, draining queued
netlink commands via w1_process_cb() during teardown can execute
W1_CMD_SLAVE_ADD or W1_CMD_SEARCH after the master has already detached
all slaves, re-attaching a slave and leaving dev->refcnt permanently
non-zero.

Clear dev->thread under dev->list_mutex before calling kthread_stop(),
only call wake_up_process(dev->thread) in w1_cn_callback() when
dev->thread is non-NULL, and fail queued commands with -ENODEV in
w1_process_cb() once W1_ABORT_SEARCH is set.

Fixes: 9fcbbac5ded4 ("w1: process w1 netlink commands in w1_process thread")
Closes: https://sashiko.dev/#/bug/linux-9bfa25b6-f273-41d6-9b56-ad4779ba6dd2
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 drivers/w1/w1_int.c     | 8 +++++++-
 drivers/w1/w1_netlink.c | 9 +++++++--
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/drivers/w1/w1_int.c b/drivers/w1/w1_int.c
index 3fcc9ee56727..c5bc7e662294 100644
--- a/drivers/w1/w1_int.c
+++ b/drivers/w1/w1_int.c
@@ -168,6 +168,7 @@ EXPORT_SYMBOL(w1_add_master_device);
 
 void __w1_remove_master_device(struct w1_master *dev)
 {
+	struct task_struct *thread;
 	struct w1_netlink_msg msg;
 	struct w1_slave *sl, *sln;
 
@@ -183,7 +184,12 @@ void __w1_remove_master_device(struct w1_master *dev)
 	 */
 	w1_destroy_master_attributes(dev);
 
-	kthread_stop(dev->thread);
+	mutex_lock(&dev->list_mutex);
+	thread = dev->thread;
+	dev->thread = NULL;
+	mutex_unlock(&dev->list_mutex);
+
+	kthread_stop(thread);
 
 	mutex_lock(&dev->mutex);
 	mutex_lock(&dev->list_mutex);
diff --git a/drivers/w1/w1_netlink.c b/drivers/w1/w1_netlink.c
index 40fbb96a793e..c1104886d707 100644
--- a/drivers/w1/w1_netlink.c
+++ b/drivers/w1/w1_netlink.c
@@ -451,8 +451,12 @@ static void w1_process_cb(struct w1_master *dev, struct w1_async_cmd *async_cmd)
 
 	mutex_lock(&dev->bus_mutex);
 	dev->priv = node->block;
-	if (sl && w1_reset_select_slave(sl))
+	if (test_bit(W1_ABORT_SEARCH, &dev->flags)) {
 		err = -ENODEV;
+		cmd = NULL;
+	} else if (sl && w1_reset_select_slave(sl)) {
+		err = -ENODEV;
+	}
 	node->block->cur_msg = node->msg;
 
 	while (mlen && !err) {
@@ -688,7 +692,8 @@ static void w1_cn_callback(struct cn_msg *cn, struct netlink_skb_parms *nsp)
 
 		mutex_lock(&dev->list_mutex);
 		list_add_tail(&node->async.async_entry, &dev->async_list);
-		wake_up_process(dev->thread);
+		if (dev->thread)
+			wake_up_process(dev->thread);
 		mutex_unlock(&dev->list_mutex);
 		++node;
 
-- 
2.56.1.911.g883d8bcb20-goog


  parent reply	other threads:[~2026-12-02  9:37 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1796204137.git.lee@kernel.org>
2026-12-02  9:36 ` [PATCH 1/6] w1: Free slave structure in w1_slave_release() Lee Jones
2026-12-02 10:38   ` Krzysztof Kozlowski
2026-12-02 10:43     ` Krzysztof Kozlowski
2026-12-02  9:36 ` Lee Jones [this message]
2026-12-02 10:38   ` [PATCH 2/6] w1: Synchronise thread teardown with netlink callbacks Krzysztof Kozlowski
2026-12-02  9:36 ` [PATCH 3/6] w1: Return actual bytes read from w1_read_block() and rw_read() Lee Jones
2026-12-02 10:54   ` Krzysztof Kozlowski
2026-12-02  9:36 ` [PATCH 4/6] w1: Protect w1_masters traversal with w1_mlock during removal Lee Jones
2026-12-02 10:44   ` Krzysztof Kozlowski
2026-12-02  9:36 ` [PATCH 5/6] w1: ds28e04: Always allocate family data to avoid NULL dereference Lee Jones
2026-12-02 10:45   ` Krzysztof Kozlowski

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=40c82de262ee4eaf8609b99520fc6861206f37d4.1796204137.git.lee@kernel.org \
    --to=lee@kernel.org \
    --cc=David@Fries.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=krzk@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=zbr@ioremap.net \
    /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