Skip to content

Commit 0621809

Browse files
krzkJiri Kosina
authored andcommitted
HID: hid-input: Fix accessing freed memory during device disconnect
During unbinding the driver was dereferencing a pointer to memory already freed by power_supply_unregister(). Driver was freeing its internal description of battery through pointers stored in power_supply structure. However, because the core owns the power supply instance, after calling power_supply_unregister() this memory is freed and the driver cannot access these members. Fix this by storing the pointer to internal description of battery in a local variable before calling power_supply_unregister(), so the pointer remains valid. Signed-off-by: Krzysztof Kozlowski <[email protected]> Reported-by: H.J. Lu <[email protected]> Fixes: 297d716 ("power_supply: Change ownership from driver to core") Cc: <[email protected]> Reviewed-by: Dmitry Torokhov <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 4a8e70f commit 0621809

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

drivers/hid/hid-input.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,15 @@ static bool hidinput_setup_battery(struct hid_device *dev, unsigned report_type,
462462

463463
static void hidinput_cleanup_battery(struct hid_device *dev)
464464
{
465+
const struct power_supply_desc *psy_desc;
466+
465467
if (!dev->battery)
466468
return;
467469

470+
psy_desc = dev->battery->desc;
468471
power_supply_unregister(dev->battery);
469-
kfree(dev->battery->desc->name);
470-
kfree(dev->battery->desc);
472+
kfree(psy_desc->name);
473+
kfree(psy_desc);
471474
dev->battery = NULL;
472475
}
473476
#else /* !CONFIG_HID_BATTERY_STRENGTH */

0 commit comments

Comments
 (0)