[PATCH v2 2/8] i2c: core: fix swnode use-after-free on client deregistration

From: Johan Hovold

Date: Tue Nov 10 2026 - 03:53:53 EST


If a client has both a primary and a secondary firmware node, the
secondary node is accessed via the ->secondary pointer of the primary
node.

Use a temporary variable to track whether the firmware node is a
software node and remove any software node before dropping its reference
to avoid a use-after-free in device_remove_software_node() when
accessing the secondary firmware node.

Fixes: df6d7277e552 ("i2c: core: Do not dereference fwnode in struct device")
Fixes: 1c24e5fc0c70 ("i2c: core: Fix double-free of fwnode in i2c_unregister_device()")
Cc: stable@xxxxxxxxxxxxxxx # 6.16
Cc: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
Cc: Hans de Goede <hansg@xxxxxxxxxx>
Signed-off-by: Johan Hovold <johan@xxxxxxxxxx>
---
drivers/i2c/i2c-core-base.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index 81603317b64d..c55ca14eaad9 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -1056,6 +1056,7 @@ EXPORT_SYMBOL_GPL(i2c_new_client_device);
void i2c_unregister_device(struct i2c_client *client)
{
struct fwnode_handle *fwnode;
+ bool fwnode_is_software_node;

if (IS_ERR_OR_NULL(client))
return;
@@ -1066,14 +1067,14 @@ void i2c_unregister_device(struct i2c_client *client)
else if (is_acpi_device_node(fwnode))
acpi_device_clear_enumerated(to_acpi_device_node(fwnode));

+ fwnode_is_software_node = is_software_node(fwnode);
+ device_remove_software_node(&client->dev);
/*
- * If the primary fwnode is a software node it is free-ed by
- * device_remove_software_node() below, avoid double-free.
+ * If the firmware node is a software node, its reference count
+ * was decremented by device_remove_software_node().
*/
- if (!is_software_node(fwnode))
+ if (!fwnode_is_software_node)
fwnode_handle_put(fwnode);
-
- device_remove_software_node(&client->dev);
device_unregister(&client->dev);
}
EXPORT_SYMBOL_GPL(i2c_unregister_device);
--
2.56.0