mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] w1: Fix the lifetime of slaves and their master
@ 2026-11-19 20:12 Suyuan Wang via B4 Relay
  2026-11-19 20:12 ` [PATCH 1/2] w1: Free the slave from its device release callback Suyuan Wang via B4 Relay
  2026-11-19 20:12 ` [PATCH 2/2] w1: Keep the master alive until the slave is released Suyuan Wang via B4 Relay
  0 siblings, 2 replies; 3+ messages in thread
From: Suyuan Wang via B4 Relay @ 2026-11-19 20:12 UTC (permalink / raw)
  To: krzk; +Cc: josef, linux-kernel

w1_unref_slave() frees a slave right after unregistering its device,
while the device's release callback still uses the slave and its
master. Since commit 9fcbbac5ded4 removing a slave no longer waits for
that callback, so a delayed release (or any late reference to the
slave device) runs on freed memory.

Patch 1 frees the slave from its release callback. Patch 2 makes the
slave hold a reference to the master until then, since the master can
otherwise be freed before the delayed release of its last slave.

Found with a static analysis tool and checked by hand. Both problems
reproduce under KASAN with CONFIG_DEBUG_KOBJECT_RELEASE and a test w1
master in QEMU; details below the --- line of each patch.

---
Suyuan Wang (2):
      w1: Free the slave from its device release callback
      w1: Keep the master alive until the slave is released

 drivers/w1/w1.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)



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

* [PATCH 1/2] w1: Free the slave from its device release callback
  2026-11-19 20:12 [PATCH 0/2] w1: Fix the lifetime of slaves and their master Suyuan Wang via B4 Relay
@ 2026-11-19 20:12 ` Suyuan Wang via B4 Relay
  2026-11-19 20:12 ` [PATCH 2/2] w1: Keep the master alive until the slave is released Suyuan Wang via B4 Relay
  1 sibling, 0 replies; 3+ messages in thread
From: Suyuan Wang via B4 Relay @ 2026-11-19 20:12 UTC (permalink / raw)
  To: krzk; +Cc: josef, linux-kernel

From: Suyuan Wang <stevewong@hust.edu.cn>

w1_unref_slave() frees the slave right after unregistering its device:

	device_unregister(&sl->dev);
	...
	kfree(sl);

but sl embeds that struct device, and w1_slave_release() still uses sl
(sl->family, sl->master) when the last reference to the device is
dropped. If anything else holds a reference to sl->dev at that point,
the release callback and the kobject code run on freed memory.

Before commit 9fcbbac5ded4 ("w1: process w1 netlink commands in
w1_process thread"), w1_slave_detach() waited for the release callback
to complete before freeing the slave; that commit dropped the wait.
The attach error path has had the same problem since commit
0ec4eb71b495 ("w1: use put_device() if device_register() fail"): it
frees sl after __w1_attach_slave_device() has dropped the device with
put_device().

This is easy to see with CONFIG_DEBUG_KOBJECT_RELEASE, which delays the
release: adding a slave through the master's w1_master_add file and
removing it through w1_master_remove gives a series of KASAN
use-after-free reports (in __run_timers, __queue_work and
w1_slave_release) on the object freed by:

  kfree+0x176/0x530
  w1_unref_slave+0x38b/0x510
  w1_slave_detach+0xbf/0xf0
  w1_master_attribute_store_remove+0x137/0x240

Move the kfree() into w1_slave_release(). The attach error path now
relies on the release callback as well, and no longer reads sl->name
or sl->master after put_device().

Found with a static analysis tool and checked by hand.

Fixes: 9fcbbac5ded4 ("w1: process w1 netlink commands in w1_process thread")
Fixes: 0ec4eb71b495 ("w1: use put_device() if device_register() fail")
Cc: stable@vger.kernel.org
Co-developed-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Suyuan Wang <stevewong@hust.edu.cn>
---
Tested in QEMU (x86_64, KASAN, CONFIG_DEBUG_KOBJECT_RELEASE) with a test
master that has no devices on the bus: write a slave id to w1_master_add,
then to w1_master_remove. Without the patch KASAN reports the
use-after-free above; with it there are no reports. The attach error
path is not exercised by this test.
---
 drivers/w1/w1.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/drivers/w1/w1.c b/drivers/w1/w1.c
index 8b33d933a582..bead2fa08c8b 100644
--- a/drivers/w1/w1.c
+++ b/drivers/w1/w1.c
@@ -80,6 +80,10 @@ static void w1_slave_release(struct device *dev)
 
 	w1_family_put(sl->family);
 	sl->master->slave_count--;
+#ifdef DEBUG
+	memset(sl, 0, sizeof(*sl));
+#endif
+	kfree(sl);
 }
 
 static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf)
@@ -761,10 +765,10 @@ 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);
+		/* the put_device() in __w1_attach_slave_device() freed sl */
+		dev_err(&dev->dev, "%s: Attaching %02x-%012llx failed.\n",
+			__func__, rn->family, (unsigned long long)rn->id);
+		atomic_dec(&dev->refcnt);
 		return err;
 	}
 
@@ -806,10 +810,6 @@ 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);
 	}
 	atomic_dec(&dev->refcnt);
 	return refcnt;

-- 
2.34.1



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

* [PATCH 2/2] w1: Keep the master alive until the slave is released
  2026-11-19 20:12 [PATCH 0/2] w1: Fix the lifetime of slaves and their master Suyuan Wang via B4 Relay
  2026-11-19 20:12 ` [PATCH 1/2] w1: Free the slave from its device release callback Suyuan Wang via B4 Relay
@ 2026-11-19 20:12 ` Suyuan Wang via B4 Relay
  1 sibling, 0 replies; 3+ messages in thread
From: Suyuan Wang via B4 Relay @ 2026-11-19 20:12 UTC (permalink / raw)
  To: krzk; +Cc: josef, linux-kernel

From: Suyuan Wang <stevewong@hust.edu.cn>

w1_slave_release() updates the master's slave count:

	sl->master->slave_count--;

but the slave holds no reference to the master. The reference the
slave device took on its parent in device_add() is dropped again in
device_del(), and w1_unref_slave() drops the master's refcnt right
after unregistering the slave, so the master can be removed and freed
before a delayed release of the slave runs.

With CONFIG_DEBUG_KOBJECT_RELEASE and the previous patch applied,
removing a slave through w1_master_remove and then removing the master
right away gives a KASAN report in w1_slave_release(), reading the
4096-byte w1_master allocation freed by the master's release.

Take a reference to the master device when the slave is attached and
drop it at the end of w1_slave_release(). Before commit 9fcbbac5ded4
("w1: process w1 netlink commands in w1_process thread") this could
not happen, because removing a slave waited for its release.

Found with a static analysis tool and checked by hand.

Fixes: 9fcbbac5ded4 ("w1: process w1 netlink commands in w1_process thread")
Cc: stable@vger.kernel.org
Co-developed-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Suyuan Wang <stevewong@hust.edu.cn>
---
Tested in the same setup: six rounds of adding a slave, removing it and
removing the master right after. With only 1/2 applied KASAN reported
the use-after-free in w1_slave_release() in four rounds; with both
patches there were no reports.
---
 drivers/w1/w1.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/w1/w1.c b/drivers/w1/w1.c
index bead2fa08c8b..348edc5bf455 100644
--- a/drivers/w1/w1.c
+++ b/drivers/w1/w1.c
@@ -75,15 +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);
+	put_device(&master->dev);
 }
 
 static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf)
@@ -735,6 +737,8 @@ int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
 
 	sl->owner = THIS_MODULE;
 	sl->master = dev;
+	/* w1_slave_release() uses the master, keep it until then */
+	get_device(&dev->dev);
 	set_bit(W1_SLAVE_ACTIVE, &sl->flags);
 
 	memset(&msg, 0, sizeof(msg));

-- 
2.34.1



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

end of thread, other threads:[~2026-11-19 20:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 20:12 [PATCH 0/2] w1: Fix the lifetime of slaves and their master Suyuan Wang via B4 Relay
2026-11-19 20:12 ` [PATCH 1/2] w1: Free the slave from its device release callback Suyuan Wang via B4 Relay
2026-11-19 20:12 ` [PATCH 2/2] w1: Keep the master alive until the slave is released Suyuan Wang via B4 Relay

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®