Linux I2C development
 help / color / mirror / Atom feed
* [PATCH i2c-fixes v1] i2c: core: Fix use-after-free during i2c device removal
@ 2026-08-22  6:26 Rafael Alejandro Diaz Cruz
  2026-08-22  8:51 ` Hillf Danton
  0 siblings, 1 reply; 10+ messages in thread
From: Rafael Alejandro Diaz Cruz @ 2026-08-22  6:26 UTC (permalink / raw)
  To: Andi Shyti, linux-i2c, linux-kernel
  Cc: Rafael Alejandro Diaz Cruz, syzbot+227dbc9afd022922d624

A race condition between a process calling i2c_device_probe()
and removal of a USB device by another process leading to both
processes calling debugfs_remove() opens up the possibility of
UAF.

Fix it by adding atomic xchg() and replacing &client->debugfs
with null before calling debugfs_remove() such that the
following statement: "if (IS_ERR_OR_NULL(dentry)) return;"
inside of debugfs_remove() executes properly.

Fixes: d06905d68610 ("i2c: add core-managed per-client directory in debugfs")
Reported-by: syzbot+227dbc9afd022922d624@syzkaller.appspotmail.com
Link: https://syzkaller.appspot.com/bug?extid=227dbc9afd022922d624
Signed-off-by: Rafael Alejandro Diaz Cruz <rafad900@gmail.com>
---
 drivers/i2c/i2c-core-base.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index fb25704219c7..6fe11232f5ee 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -586,8 +586,14 @@ static int i2c_device_probe(struct device *dev)
 		goto err_clear_wakeup_irq;
 	}
 
-	client->debugfs = debugfs_create_dir(dev_name(&client->dev),
-					     client->adapter->debugfs);
+	struct dentry *parent = READ_ONCE(client->adapter->debugfs);
+
+	if (!parent) {
+		status = -ENODEV;
+		goto err_clear_wakeup_irq;
+	}
+
+	client->debugfs = debugfs_create_dir(dev_name(&client->dev), parent);
 
 	if (driver->probe)
 		status = driver->probe(client);
@@ -608,7 +614,10 @@ static int i2c_device_probe(struct device *dev)
 	return 0;
 
 err_release_driver_resources:
-	debugfs_remove_recursive(client->debugfs);
+	// debugfs_remove_recursive(client->debugfs);
+	struct dentry *dir = xchg(&client->debugfs, NULL);
+
+	debugfs_remove_recursive(dir);
 	devres_release_group(&client->dev, client->devres_group_id);
 err_clear_wakeup_irq:
 	dev_pm_clear_wake_irq(&client->dev);
@@ -632,7 +641,9 @@ static void i2c_device_remove(struct device *dev)
 		driver->remove(client);
 	}
 
-	debugfs_remove_recursive(client->debugfs);
+	struct dentry *dir = xchg(&client->debugfs, NULL);
+
+	debugfs_remove_recursive(dir);
 
 	devres_release_group(&client->dev, client->devres_group_id);
 
@@ -1818,6 +1829,8 @@ void i2c_del_adapter(struct i2c_adapter *adap)
 
 	i2c_acpi_remove_space_handler(adap);
 
+	struct dentry *dir = xchg(&adap->debugfs, NULL);
+
 	i2c_deregister_clients(adap);
 
 	/* device name is gone after device_unregister */
@@ -1827,7 +1840,7 @@ void i2c_del_adapter(struct i2c_adapter *adap)
 
 	i2c_host_notify_irq_teardown(adap);
 
-	debugfs_remove_recursive(adap->debugfs);
+	debugfs_remove_recursive(dir);
 
 	/* wait until all references to the device are gone
 	 *
-- 
2.43.0


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

end of thread, other threads:[~2026-08-23  3:48 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-22  6:26 [PATCH i2c-fixes v1] i2c: core: Fix use-after-free during i2c device removal Rafael Alejandro Diaz Cruz
2026-08-22  8:51 ` Hillf Danton
2026-08-22  9:25   ` [syzbot] [fs?] [usb?] KASAN: slab-use-after-free Read in lockref_get (2) syzbot
     [not found]   ` <CALp66yH7QzvGmo+N7BWykYjoaGT-yAezwRv0Pc_ir6qDRTsKvw@mail.gmail.com>
     [not found]     ` <CALp66yFrutQ2h8SNyBZn04+LMPMnSEtWxW_Dr0vzfoHvEahSsQ@mail.gmail.com>
2026-08-22 19:11       ` syzbot
2026-08-22 19:37       ` [PATCH i2c-fixes v1] i2c: core: Fix use-after-free during i2c device removal Rafael Alejandro Díaz Cruz
2026-08-22 21:03         ` [syzbot] [fs?] [usb?] KASAN: slab-use-after-free Read in lockref_get (2) syzbot
2026-08-22 23:16         ` [PATCH i2c-fixes v1] i2c: core: Fix use-after-free during i2c device removal Rafael Alejandro Díaz Cruz
2026-08-22 23:57           ` [syzbot] [fs?] [usb?] KASAN: slab-use-after-free Read in lockref_get (2) syzbot
2026-08-23  1:12           ` [PATCH i2c-fixes v1] i2c: core: Fix use-after-free during i2c device removal Rafael Alejandro Díaz Cruz
2026-08-23  3:48             ` [syzbot] [fs?] [usb?] KASAN: slab-use-after-free Read in lockref_get (2) syzbot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox