]> git.baikalelectronics.ru Git - kernel.git/commit
power_supply: Fix NULL pointer dereference during bq27x00_battery probe
authorKrzysztof Kozlowski <k.kozlowski@samsung.com>
Tue, 19 May 2015 07:13:01 +0000 (16:13 +0900)
committerSebastian Reichel <sre@kernel.org>
Thu, 21 May 2015 13:40:54 +0000 (15:40 +0200)
commitab59c7a70e12b1b6d1629087b0f2f00b28010014
tree3e26099e1e90a2112c467a9df7b1e1e4224ee398
parent459ce5742371391c62dbab6c7c9603c8f569a30a
power_supply: Fix NULL pointer dereference during bq27x00_battery probe

Power supply is often registered during probe of a driver. The
power_supply_register() returns pointer to newly allocated structure as
return value. However before returning the power_supply_register()
calls back the get_property() method provided by the driver through
uevent.

In that time the driver probe is still in progress and driver did not
assigned pointer to power supply to its local variables. This leads to
NULL pointer dereference from get_property() function.
Starting from bq27x00_battery_probe():
  di->bat = power_supply_register()
    device_add()
      kobject_uevent()
        power_supply_uevent()
          power_supply_show_property()
            power_supply_get_property()
              bq27x00_battery_get_property()
                dereference of (di->bat) which is NULL here

The first uevent of power supply (the one coming from device creation)
should not call back to the driver. To prevent that from happening,
increment the atomic use counter at the end of power_supply_register().
This means that power_supply_get_property() will return -ENODEV.

IMPORTANT:
The patch has impact on this first uevent sent from power supply because
it will not contain properties from power supply.

The uevent with properties will be sent later after indicating that
power supply has changed. This also has a race now, but will be fixed in
other patches.

Reported-by: H. Nikolaus Schaller <hns@goldelico.com>
Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
Fixes: af564da181b0 ("power_supply: Change ownership from driver to core")
Tested-By: Dr. H. Nikolaus Schaller <hns@goldelico.com>
Signed-off-by: Sebastian Reichel <sre@kernel.org>
drivers/power/power_supply_core.c