mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Suyuan Wang via B4 Relay <devnull+shyckless.gmail.com@kernel.org>
To: krzk@kernel.org
Cc: josef@toxicpanda.com, linux-kernel@vger.kernel.org
Subject: [PATCH 2/2] w1: Keep the master alive until the slave is released
Date: Thu, 19 Nov 2026 20:12:14 +0000	[thread overview]
Message-ID: <20261119-w1-slave-release-v1-2-c2948c825d08@gmail.com> (raw)
In-Reply-To: <20261119-w1-slave-release-v1-0-c2948c825d08@gmail.com>

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



      parent reply	other threads:[~2026-11-19 20:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

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=20261119-w1-slave-release-v1-2-c2948c825d08@gmail.com \
    --to=devnull+shyckless.gmail.com@kernel.org \
    --cc=josef@toxicpanda.com \
    --cc=krzk@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=shyckless@gmail.com \
    /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®