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>,
	linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH 4/6] w1: Protect w1_masters traversal with w1_mlock during removal
Date: Wed,  2 Dec 2026 09:36:46 +0000	[thread overview]
Message-ID: <ee419fefcfc27e4a0bd4dc28793fea0003b72192.1796204137.git.lee@kernel.org> (raw)
In-Reply-To: <cover.1796204137.git.lee@kernel.org>

w1_remove_master_device() and w1_fini() iterate over the global
w1_masters list without holding w1_mlock, racing with concurrent
w1_add_master_device() and w1_remove_master_device() calls that mutate
the list.  In addition, leaving list_del() to
__w1_remove_master_device() creates a window in which two concurrent
removals can find and tear down the same master device.

Hold w1_mlock across lookup and list_del() in both
w1_remove_master_device() and w1_fini(), and remove the now-redundant
locking and list_del() from __w1_remove_master_device().

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Closes: https://sashiko.dev/#/bug/linux-568be2be-e67b-4b92-8416-512e4968f535
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 drivers/w1/w1.c     | 12 ++++++++++--
 drivers/w1/w1_int.c |  8 ++++----
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/drivers/w1/w1.c b/drivers/w1/w1.c
index 7ed2a01c8571..73505cf53572 100644
--- a/drivers/w1/w1.c
+++ b/drivers/w1/w1.c
@@ -1263,11 +1263,19 @@ static int __init w1_init(void)
 
 static void __exit w1_fini(void)
 {
-	struct w1_master *dev, *n;
+	struct w1_master *dev;
 
 	/* Set netlink removal messages and some cleanup */
-	list_for_each_entry_safe(dev, n, &w1_masters, w1_master_entry)
+	mutex_lock(&w1_mlock);
+	while (!list_empty(&w1_masters)) {
+		dev = list_first_entry(&w1_masters, struct w1_master,
+				       w1_master_entry);
+		list_del(&dev->w1_master_entry);
+		mutex_unlock(&w1_mlock);
 		__w1_remove_master_device(dev);
+		mutex_lock(&w1_mlock);
+	}
+	mutex_unlock(&w1_mlock);
 
 	w1_fini_netlink();
 
diff --git a/drivers/w1/w1_int.c b/drivers/w1/w1_int.c
index c5bc7e662294..c76f22bfd394 100644
--- a/drivers/w1/w1_int.c
+++ b/drivers/w1/w1_int.c
@@ -166,16 +166,13 @@ int w1_add_master_device(struct w1_bus_master *master)
 }
 EXPORT_SYMBOL(w1_add_master_device);
 
+/* Caller must have removed @dev from w1_masters under w1_mlock. */
 void __w1_remove_master_device(struct w1_master *dev)
 {
 	struct task_struct *thread;
 	struct w1_netlink_msg msg;
 	struct w1_slave *sl, *sln;
 
-	mutex_lock(&w1_mlock);
-	list_del(&dev->w1_master_entry);
-	mutex_unlock(&w1_mlock);
-
 	set_bit(W1_ABORT_SEARCH, &dev->flags);
 
 	/*
@@ -232,15 +229,18 @@ void w1_remove_master_device(struct w1_bus_master *bm)
 {
 	struct w1_master *dev, *found = NULL;
 
+	mutex_lock(&w1_mlock);
 	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
 		if (!dev->initialized)
 			continue;
 
 		if (dev->bus_master->data == bm->data) {
 			found = dev;
+			list_del(&dev->w1_master_entry);
 			break;
 		}
 	}
+	mutex_unlock(&w1_mlock);
 
 	if (!found) {
 		pr_err("Device doesn't exist.\n");
-- 
2.56.1.911.g883d8bcb20-goog


  parent reply	other threads:[~2026-12-02  9:38 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 ` [PATCH 2/6] w1: Synchronise thread teardown with netlink callbacks Lee Jones
2026-12-02 10:38   ` 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 ` Lee Jones [this message]
2026-12-02 10:44   ` [PATCH 4/6] w1: Protect w1_masters traversal with w1_mlock during removal 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=ee419fefcfc27e4a0bd4dc28793fea0003b72192.1796204137.git.lee@kernel.org \
    --to=lee@kernel.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