Skip to content

Commit

Permalink
ACPI / Battery: avoid acpi_battery_add() use-after-free
Browse files Browse the repository at this point in the history
When acpi_battery_add_fs() fails the error handling code does not clean
up completely.  Moreover, it does not return resulting in a
use-after-free.

Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Len Brown <len.brown@intel.com>
  • Loading branch information
Stefan Hajnoczi authored and Len Brown committed Jul 16, 2011
1 parent 9c921c2 commit e80bba4
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions drivers/acpi/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -986,21 +986,27 @@ static int acpi_battery_add(struct acpi_device *device)
#ifdef CONFIG_ACPI_PROCFS_POWER
result = acpi_battery_add_fs(device);
#endif
if (!result) {
printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
device->status.battery_present ? "present" : "absent");
} else {
if (result) {
#ifdef CONFIG_ACPI_PROCFS_POWER
acpi_battery_remove_fs(device);
#endif
kfree(battery);
goto fail;
}

printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
device->status.battery_present ? "present" : "absent");

battery->pm_nb.notifier_call = battery_notify;
register_pm_notifier(&battery->pm_nb);

return result;

fail:
sysfs_remove_battery(battery);
mutex_destroy(&battery->lock);
kfree(battery);
return result;
}

static int acpi_battery_remove(struct acpi_device *device, int type)
Expand Down

0 comments on commit e80bba4

Please sign in to comment.