w1: Fix the lifetime of slaves and their master
cover w1: Fix the lifetime of slaves and their master
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(-)
PATCH 1/2 w1: Free the slave from its device release callback
From: Suyuan Wang <[email protected]>
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.
---
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(-)
drivers/w1/w1.c +8 -8
@@ -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;
Signature · 1 line
2.34.1
PATCH 2/2 w1: Keep the master alive until the slave is released
In reply to Suyuan WangFrom: Suyuan Wang <[email protected]>
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.
---
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(-)
drivers/w1/w1.c +5 -1
@@ -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));
Signature · 1 line
2.34.1