diff mbox series

power: supply: fix null pointer dereferencing in power_supply_get_battery_info

Message ID 20221204023857.1621757-1-ruanjinjie@huawei.com (mailing list archive)
State Handled Elsewhere, archived
Headers show
Series power: supply: fix null pointer dereferencing in power_supply_get_battery_info | expand

Commit Message

Jinjie Ruan Dec. 4, 2022, 2:38 a.m. UTC
when kmalloc() fail to allocate memory in kasprintf(), propname
will be NULL, strcmp() called by of_get_property() will cause
null pointer dereference.

So directly return ENOMEM, if kasprintf() return NULL pointer.

Fixes: 3afb50d7125b ("power: supply: core: Add some helpers to use the battery OCV capacity table")
Signed-off-by: ruanjinjie <ruanjinjie@huawei.com>
---
 drivers/power/supply/power_supply_core.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Baolin Wang Dec. 5, 2022, 7:12 a.m. UTC | #1
Hi,

On 12/4/2022 10:38 AM, ruanjinjie wrote:
> when kmalloc() fail to allocate memory in kasprintf(), propname
> will be NULL, strcmp() called by of_get_property() will cause
> null pointer dereference.
> 
> So directly return ENOMEM, if kasprintf() return NULL pointer.
> 
> Fixes: 3afb50d7125b ("power: supply: core: Add some helpers to use the battery OCV capacity table")
> Signed-off-by: ruanjinjie <ruanjinjie@huawei.com>
> ---
>   drivers/power/supply/power_supply_core.c | 2 ++
>   1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
> index 4b5fb172fa99..2fd6ab7a4471 100644
> --- a/drivers/power/supply/power_supply_core.c
> +++ b/drivers/power/supply/power_supply_core.c
> @@ -750,6 +750,8 @@ int power_supply_get_battery_info(struct power_supply *psy,
>   		int i, tab_len, size;
>   
>   		propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index);
> +		if (!propname)
> +			return -ENOMEM;

Please read the code carefully, you can not just return error number 
without releasing resource.

>   		list = of_get_property(battery_np, propname, &size);
>   		if (!list || !size) {
>   			dev_err(&psy->dev, "failed to get %s\n", propname);
diff mbox series

Patch

diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index 4b5fb172fa99..2fd6ab7a4471 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -750,6 +750,8 @@  int power_supply_get_battery_info(struct power_supply *psy,
 		int i, tab_len, size;
 
 		propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index);
+		if (!propname)
+			return -ENOMEM;
 		list = of_get_property(battery_np, propname, &size);
 		if (!list || !size) {
 			dev_err(&psy->dev, "failed to get %s\n", propname);