A consumer's psy->supplied_from[] array keeps pointing at the supplier's desc->name after the supplier is unregistered. That pointer dangles once the name is freed or when the name lives in the supplier's module .rodata and the module is unloaded. Every subsequent power_supply_changed() passes that freed pointer to strcmp() in __power_supply_is_supplied_by(), triggering a use-after-free. If the freed memory is poisoned or reused, consumers also fail to match the supplier when it is bound again: BUG: KASAN: slab-use-after-free in strcmp+0x40/0x80 Read of size 1 at addr ffff0008039e8840 by task kworker/4:2/173 Workqueue: events power_supply_changed_work Call trace: kasan_report+0xb0/0x108 __asan_load1+0x98/0xa0 strcmp+0x40/0x80 __power_supply_is_supplied_by+0x98/0x160 __power_supply_changed_work+0x2c/0x68 psy_for_each_psy_cb+0x44/0x60 class_for_each_device+0x178/0x1c0 power_supply_changed_work+0x100/0x238 process_one_work+0x4a0/0x960 Allocated by task 60: devm_kmalloc+0x6c/0x180 tcpm_register_port+0x840/0xd30 [tcpm] tcpci_register_port+0x2a8/0x400 [tcpci] max_tcpci_probe+0x224/0x430 [tcpci_maxim] i2c_device_probe+0x254/0x4c0 Freed by task 238: kfree+0x228/0x5f0 dr_node_free+0x18/0x38 release_nodes+0xb4/0xe8 devres_release_group+0x1ec/0x248 i2c_device_remove+0x70/0x138 device_remove+0x74/0xc0 device_release_driver_internal+0x2b0/0x330 driver_detach+0x78/0x100 bus_remove_driver+0xa4/0x160 driver_unregister+0x4c/0x88 i2c_del_driver+0x5c/0xb0 max_tcpci_i2c_driver_exit+0x18/0x5a8 [tcpci_maxim] __power_supply_populate_supplied_from() stores epsy->desc->name directly in psy->supplied_from[] without duplicating the string, so the consumer borrows memory tied to the supplier's device or module lifetime. Make psy->supplied_from const char ** (removing the now-redundant cast in power_supply_check_supplies_by_name()), duplicate the supplier name with devm_kstrdup_const() on the consumer device, and return -ENOMEM (releasing the fwnode reference) on allocation failure. Fixes: f6e0b081fb30 ("power_supply: Populate supplied_from hierarchy from the device tree") Cc: stable@vger.kernel.org # 6.18+ Signed-off-by: André Draszik --- Tested on Pixel 6 (Oriole) with CONFIG_KASAN_GENERIC=y: rmmod tcpci_maxim && modprobe tcpci_maxim Without this patch, the first reload triggers the KASAN slab-use-after-free in strcmp() above. With this patch, repeated unload/reload cycles don't trigger it anymore. --- drivers/power/supply/power_supply_core.c | 12 +++++++++--- include/linux/power_supply.h | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index 1cc203f2780b..4dfcdb720a31 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -208,8 +208,7 @@ static int power_supply_check_supplies_by_name(struct power_supply *psy) return -ENOMEM; ret = device_property_read_string_array(parent, "supplied-from", - (const char **)psy->supplied_from, - nval); + psy->supplied_from, nval); if (ret < 0) return ret; @@ -223,6 +222,7 @@ static int __power_supply_populate_supplied_from(struct power_supply *epsy, { struct power_supply *psy = data; struct fwnode_handle *np; + const char *name; int i = 0; do { @@ -231,9 +231,15 @@ static int __power_supply_populate_supplied_from(struct power_supply *epsy, break; if (np == epsy->dev.fwnode) { + name = devm_kstrdup_const(&psy->dev, epsy->desc->name, + GFP_KERNEL); + if (!name) { + fwnode_handle_put(np); + return -ENOMEM; + } dev_dbg(&psy->dev, "%s: Found supply : %s\n", psy->desc->name, epsy->desc->name); - psy->supplied_from[i-1] = (char *)epsy->desc->name; + psy->supplied_from[i - 1] = name; psy->num_supplies++; fwnode_handle_put(np); break; diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index 7fbc0b80bcbb..7d8044d91be6 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -355,7 +355,7 @@ struct power_supply { char **supplied_to; size_t num_supplicants; - char **supplied_from; + const char **supplied_from; size_t num_supplies; /* Driver private data */ --- base-commit: 3191063649dbf7c0d0b768e1f3655e4c5fc6edb5 change-id: 20261125-psy-core-supplier-fixes-120527078972 Best regards, -- André Draszik