mirror of https://lore.kernel.org/lkml/
 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>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	David Fries <David@Fries.net>,
	linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH 1/6] w1: Free slave structure in w1_slave_release()
Date: Wed,  2 Dec 2026 09:36:43 +0000	[thread overview]
Message-ID: <04fc999ac2352c9b97aaa284983a2a44bd0c12e4.1796204137.git.lee@kernel.org> (raw)
In-Reply-To: <cover.1796204137.git.lee@kernel.org>

w1_unref_slave() calls kfree(sl) immediately after device_unregister(),
violating the driver model lifetime rules.  If another thread holds a
reference on sl->dev (such as via sysfs or device iteration), the
embedded struct device is freed while still in use, causing a
use-after-free when w1_slave_release() later runs.  Similarly,
__w1_attach_slave_device() calls put_device(&sl->dev) on registration
failure, so freeing sl in w1_slave_release() means
w1_attach_slave_device() must not touch or free sl afterwards.

Move kfree(sl) and the final atomic_dec(&master->refcnt) into
w1_slave_release() so both the slave and its master remain valid until
the final device reference is released.  Adjust w1_unref_slave() to
decrement dev->refcnt only when refcnt remains non-zero, and update the
error path in w1_attach_slave_device() accordingly.

Fixes: 9fcbbac5ded4 ("w1: process w1 netlink commands in w1_process thread")
Closes: https://sashiko.dev/#/bug/linux-8d066669-dde3-4c3f-a842-12383edebc99
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 drivers/w1/w1.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/drivers/w1/w1.c b/drivers/w1/w1.c
index 4eb785c643c9..048cf8cb7f6b 100644
--- a/drivers/w1/w1.c
+++ b/drivers/w1/w1.c
@@ -75,11 +75,17 @@ static void w1_master_release(struct device *dev)
 static void w1_slave_release(struct device *dev)
 {
 	struct w1_slave *sl = dev_to_w1_slave(dev);
+	struct w1_master *master = sl->master;
 
 	dev_dbg(dev, "%s: Releasing %s [%p]\n", __func__, sl->name, sl);
 
 	w1_family_put(sl->family);
-	sl->master->slave_count--;
+	master->slave_count--;
+#ifdef DEBUG
+	memset(sl, 0, sizeof(*sl));
+#endif
+	kfree(sl);
+	atomic_dec(&master->refcnt);
 }
 
 static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf)
@@ -761,10 +767,8 @@ int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
 
 	err = __w1_attach_slave_device(sl);
 	if (err < 0) {
-		dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
-			 sl->name);
-		atomic_dec(&sl->master->refcnt);
-		kfree(sl);
+		dev_err(&dev->dev, "%s: Attaching %02x-%012llx failed.\n",
+			__func__, rn->family, (unsigned long long)rn->id);
 		return err;
 	}
 
@@ -806,12 +810,9 @@ int w1_unref_slave(struct w1_slave *sl)
 
 		w1_family_notify(BUS_NOTIFY_DEL_DEVICE, sl);
 		device_unregister(&sl->dev);
-		#ifdef DEBUG
-		memset(sl, 0, sizeof(*sl));
-		#endif
-		kfree(sl);
+	} else {
+		atomic_dec(&dev->refcnt);
 	}
-	atomic_dec(&dev->refcnt);
 	return refcnt;
 }
 
-- 
2.56.1.911.g883d8bcb20-goog


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

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  9:36 [PATCH 0/6] w1: Fix Critical and High issues reported by Sashiko Lee Jones
2026-12-02  9:36 ` Lee Jones [this message]
2026-12-02 10:38   ` [PATCH 1/6] w1: Free slave structure in w1_slave_release() 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 ` [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
2026-12-02  9:36 ` [PATCH 6/6] w1: sgi: Add timeout to sgi_w1_wait() polling loop Lee Jones
2026-12-02 10:46   ` Krzysztof Kozlowski
2026-12-02  9:45 ` [PATCH 0/6] w1: Fix Critical and High issues reported by Sashiko Greg Kroah-Hartman
2026-12-02 10:23   ` Lee Jones
2026-12-02 10:30     ` Greg Kroah-Hartman

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=04fc999ac2352c9b97aaa284983a2a44bd0c12e4.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

all inboxes | Powered by JetHome®