Re: [RFT PATCH 1/4] power_supply: twl4030_madc: Check return value of power_supply_register

From: Krzysztof Kozlowski
Date: Wed Jan 28 2015 - 21:09:18 EST


On wto, 2015-01-27 at 22:06 +0100, Belisko Marek wrote:
> Hi Krzystof,
>
> On Tue, Jan 27, 2015 at 12:30 PM, Krzysztof Kozlowski
> <k.kozlowski@xxxxxxxxxxx> wrote:
> > The return value of power_supply_register() call was not checked and
> > even on error probe() function returned 0. If registering failed then
> > during unbind the driver tried to unregister power supply which was not
> > actually registered.
> >
> > This could lead to memory corruption because power_supply_unregister()
> > unconditionally cleans up given power supply.
> >
> > Signed-off-by: Krzysztof Kozlowski <k.kozlowski@xxxxxxxxxxx>
> > Fixes: da0a00ebc239 ("power: Add twl4030_madc battery driver.")
> > Cc: <stable@xxxxxxxxxxxxxxx>
> > ---
> > drivers/power/twl4030_madc_battery.c | 7 +++++--
> > 1 file changed, 5 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/power/twl4030_madc_battery.c b/drivers/power/twl4030_madc_battery.c
> > index 7ef445a6cfa6..cf907609ec49 100644
> > --- a/drivers/power/twl4030_madc_battery.c
> > +++ b/drivers/power/twl4030_madc_battery.c
> > @@ -192,6 +192,7 @@ static int twl4030_madc_battery_probe(struct platform_device *pdev)
> > {
> > struct twl4030_madc_battery *twl4030_madc_bat;
> > struct twl4030_madc_bat_platform_data *pdata = pdev->dev.platform_data;
> > + int ret = 0;
> >
> > twl4030_madc_bat = kzalloc(sizeof(*twl4030_madc_bat), GFP_KERNEL);
> > if (!twl4030_madc_bat)
> > @@ -216,9 +217,11 @@ static int twl4030_madc_battery_probe(struct platform_device *pdev)
> >
> > twl4030_madc_bat->pdata = pdata;
> > platform_set_drvdata(pdev, twl4030_madc_bat);
> > - power_supply_register(&pdev->dev, &twl4030_madc_bat->psy);
> > + ret = power_supply_register(&pdev->dev, &twl4030_madc_bat->psy);
> > + if (ret < 0)
> > + kfree(twl4030_madc_bat);
> I post update twl4030_madc to iio some time ago [1] where this change
> was incorporated.
> I plan to respin in next week so we can drop this one (or take and
> I'll rebase on top).

Hi,

I am fine with both solutions (especially that I am not able to test my
change). However there is one benefit of my fix: after applying it could
be backported to stable kernels (3.11+?).

Best regards,
Krzysztof

> >
> > - return 0;
> > + return ret;
> > }
> >
> > static int twl4030_madc_battery_remove(struct platform_device *pdev)
> > --
> > 1.9.1
> >
>
> [1] - https://lkml.org/lkml/2014/3/5/284
>
> BR,
>
> marek
>

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/