* [PATCH] iio: cm36651: Fix i2c client leak and possible NULL pointer dereference
@ 2014-03-06 9:33 Krzysztof Kozlowski
2014-03-07 0:34 ` Beomho Seo
2014-03-15 16:24 ` Jonathan Cameron
0 siblings, 2 replies; 6+ messages in thread
From: Krzysztof Kozlowski @ 2014-03-06 9:33 UTC (permalink / raw)
To: Jonathan Cameron, linux-iio, linux-kernel
Cc: Beomho Seo, Lars-Peter Clausen, Krzysztof Kozlowski
During probe the driver allocates dummy I2C devices (i2c_new_dummy())
but they aren't unregistered during driver remove or probe failure.
Additionally driver does not check the return value of i2c_new_dummy().
In case of error (i2c_new_device(): memory allocation failure or I2C
address cannot be used) this function returns NULL which is later
dereferenced by i2c_smbus_{read,write}_data() functions.
Fix issues by properly checking for i2c_new_dummy() return value and
unregistering I2C devices on driver remove or probe failure.
Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
---
drivers/iio/light/cm36651.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/drivers/iio/light/cm36651.c b/drivers/iio/light/cm36651.c
index a45e07492db3..e7e9a597159f 100644
--- a/drivers/iio/light/cm36651.c
+++ b/drivers/iio/light/cm36651.c
@@ -653,6 +653,11 @@ static int cm36651_probe(struct i2c_client *client,
cm36651->ps_client = i2c_new_dummy(client->adapter,
CM36651_I2C_ADDR_PS);
cm36651->ara_client = i2c_new_dummy(client->adapter, CM36651_ARA);
+ if (!cm36651->ps_client || !cm36651->ara_client) {
+ dev_err(&client->dev, "%s: new i2c device failed\n", __func__);
+ ret = -ENODEV;
+ goto error_i2c_unregister;
+ }
mutex_init(&cm36651->lock);
indio_dev->dev.parent = &client->dev;
indio_dev->channels = cm36651_channels;
@@ -687,6 +692,11 @@ error_free_irq:
free_irq(client->irq, indio_dev);
error_disable_reg:
regulator_disable(cm36651->vled_reg);
+error_i2c_unregister:
+ if (cm36651->ps_client)
+ i2c_unregister_device(cm36651->ps_client);
+ if (cm36651->ara_client)
+ i2c_unregister_device(cm36651->ara_client);
return ret;
}
@@ -698,6 +708,8 @@ static int cm36651_remove(struct i2c_client *client)
iio_device_unregister(indio_dev);
regulator_disable(cm36651->vled_reg);
free_irq(client->irq, indio_dev);
+ i2c_unregister_device(cm36651->ps_client);
+ i2c_unregister_device(cm36651->ara_client);
return 0;
}
--
1.7.9.5
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] iio: cm36651: Fix i2c client leak and possible NULL pointer dereference
2014-03-06 9:33 [PATCH] iio: cm36651: Fix i2c client leak and possible NULL pointer dereference Krzysztof Kozlowski
@ 2014-03-07 0:34 ` Beomho Seo
2014-03-15 16:24 ` Jonathan Cameron
1 sibling, 0 replies; 6+ messages in thread
From: Beomho Seo @ 2014-03-07 0:34 UTC (permalink / raw)
To: Krzysztof Kozlowski, Jonathan Cameron, linux-iio, linux-kernel
Cc: Lars-Peter Clausen
On 03/06/2014 06:33 PM, Krzysztof Kozlowski wrote:
> During probe the driver allocates dummy I2C devices (i2c_new_dummy())
> but they aren't unregistered during driver remove or probe failure.
>
> Additionally driver does not check the return value of i2c_new_dummy().
> In case of error (i2c_new_device(): memory allocation failure or I2C
> address cannot be used) this function returns NULL which is later
> dereferenced by i2c_smbus_{read,write}_data() functions.
>
> Fix issues by properly checking for i2c_new_dummy() return value and
> unregistering I2C devices on driver remove or probe failure.
>
> Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
Thanks,
Acked-by: Beomho Seo <beomho.seo@samsung.com>
> ---
> drivers/iio/light/cm36651.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/drivers/iio/light/cm36651.c b/drivers/iio/light/cm36651.c
> index a45e07492db3..e7e9a597159f 100644
> --- a/drivers/iio/light/cm36651.c
> +++ b/drivers/iio/light/cm36651.c
> @@ -653,6 +653,11 @@ static int cm36651_probe(struct i2c_client *client,
> cm36651->ps_client = i2c_new_dummy(client->adapter,
> CM36651_I2C_ADDR_PS);
> cm36651->ara_client = i2c_new_dummy(client->adapter, CM36651_ARA);
> + if (!cm36651->ps_client || !cm36651->ara_client) {
> + dev_err(&client->dev, "%s: new i2c device failed\n", __func__);
> + ret = -ENODEV;
> + goto error_i2c_unregister;
> + }
> mutex_init(&cm36651->lock);
> indio_dev->dev.parent = &client->dev;
> indio_dev->channels = cm36651_channels;
> @@ -687,6 +692,11 @@ error_free_irq:
> free_irq(client->irq, indio_dev);
> error_disable_reg:
> regulator_disable(cm36651->vled_reg);
> +error_i2c_unregister:
> + if (cm36651->ps_client)
> + i2c_unregister_device(cm36651->ps_client);
> + if (cm36651->ara_client)
> + i2c_unregister_device(cm36651->ara_client);
> return ret;
> }
>
> @@ -698,6 +708,8 @@ static int cm36651_remove(struct i2c_client *client)
> iio_device_unregister(indio_dev);
> regulator_disable(cm36651->vled_reg);
> free_irq(client->irq, indio_dev);
> + i2c_unregister_device(cm36651->ps_client);
> + i2c_unregister_device(cm36651->ara_client);
>
> return 0;
> }
>
--
Best Regards,
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] iio: cm36651: Fix i2c client leak and possible NULL pointer dereference
2014-03-06 9:33 [PATCH] iio: cm36651: Fix i2c client leak and possible NULL pointer dereference Krzysztof Kozlowski
2014-03-07 0:34 ` Beomho Seo
@ 2014-03-15 16:24 ` Jonathan Cameron
2014-03-17 8:01 ` Krzysztof Kozlowski
1 sibling, 1 reply; 6+ messages in thread
From: Jonathan Cameron @ 2014-03-15 16:24 UTC (permalink / raw)
To: Krzysztof Kozlowski, linux-iio, linux-kernel
Cc: Beomho Seo, Lars-Peter Clausen
On 06/03/14 09:33, Krzysztof Kozlowski wrote:
> During probe the driver allocates dummy I2C devices (i2c_new_dummy())
> but they aren't unregistered during driver remove or probe failure.
>
> Additionally driver does not check the return value of i2c_new_dummy().
> In case of error (i2c_new_device(): memory allocation failure or I2C
> address cannot be used) this function returns NULL which is later
> dereferenced by i2c_smbus_{read,write}_data() functions.
>
> Fix issues by properly checking for i2c_new_dummy() return value and
> unregistering I2C devices on driver remove or probe failure.
>
> Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
Good catch, but the error path needs more care.
> ---
> drivers/iio/light/cm36651.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/drivers/iio/light/cm36651.c b/drivers/iio/light/cm36651.c
> index a45e07492db3..e7e9a597159f 100644
> --- a/drivers/iio/light/cm36651.c
> +++ b/drivers/iio/light/cm36651.c
> @@ -653,6 +653,11 @@ static int cm36651_probe(struct i2c_client *client,
> cm36651->ps_client = i2c_new_dummy(client->adapter,
> CM36651_I2C_ADDR_PS);
> cm36651->ara_client = i2c_new_dummy(client->adapter, CM36651_ARA);
> + if (!cm36651->ps_client || !cm36651->ara_client) {
> + dev_err(&client->dev, "%s: new i2c device failed\n", __func__);
> + ret = -ENODEV;
> + goto error_i2c_unregister;
> + }
The two failures need to be handled independently as we only want to unregister
those that succeeded. i2c_new_dummy will not return an error and leave a device
registered. This is particularly true given the first thing that i2c_unregister_device
does is to derefence the client pointer. That will cause a segfault if you do it
for NULL as here.
> mutex_init(&cm36651->lock);
> indio_dev->dev.parent = &client->dev;
> indio_dev->channels = cm36651_channels;
> @@ -687,6 +692,11 @@ error_free_irq:
> free_irq(client->irq, indio_dev);
> error_disable_reg:
> regulator_disable(cm36651->vled_reg);
> +error_i2c_unregister:
> + if (cm36651->ps_client)
> + i2c_unregister_device(cm36651->ps_client);
> + if (cm36651->ara_client)
> + i2c_unregister_device(cm36651->ara_client);
> return ret;
> }
>
> @@ -698,6 +708,8 @@ static int cm36651_remove(struct i2c_client *client)
> iio_device_unregister(indio_dev);
> regulator_disable(cm36651->vled_reg);
> free_irq(client->irq, indio_dev);
> + i2c_unregister_device(cm36651->ps_client);
> + i2c_unregister_device(cm36651->ara_client);
Good catch.
>
> return 0;
> }
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] iio: cm36651: Fix i2c client leak and possible NULL pointer dereference
2014-03-15 16:24 ` Jonathan Cameron
@ 2014-03-17 8:01 ` Krzysztof Kozlowski
2014-03-17 19:24 ` Jonathan Cameron
0 siblings, 1 reply; 6+ messages in thread
From: Krzysztof Kozlowski @ 2014-03-17 8:01 UTC (permalink / raw)
To: Jonathan Cameron; +Cc: linux-iio, linux-kernel, Beomho Seo, Lars-Peter Clausen
Hi,
On Sat, 2014-03-15 at 16:24 +0000, Jonathan Cameron wrote:
> On 06/03/14 09:33, Krzysztof Kozlowski wrote:
> > During probe the driver allocates dummy I2C devices (i2c_new_dummy())
> > but they aren't unregistered during driver remove or probe failure.
> >
> > Additionally driver does not check the return value of i2c_new_dummy().
> > In case of error (i2c_new_device(): memory allocation failure or I2C
> > address cannot be used) this function returns NULL which is later
> > dereferenced by i2c_smbus_{read,write}_data() functions.
> >
> > Fix issues by properly checking for i2c_new_dummy() return value and
> > unregistering I2C devices on driver remove or probe failure.
> >
> > Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
> Good catch, but the error path needs more care.
> > ---
> > drivers/iio/light/cm36651.c | 12 ++++++++++++
> > 1 file changed, 12 insertions(+)
> >
> > diff --git a/drivers/iio/light/cm36651.c b/drivers/iio/light/cm36651.c
> > index a45e07492db3..e7e9a597159f 100644
> > --- a/drivers/iio/light/cm36651.c
> > +++ b/drivers/iio/light/cm36651.c
> > @@ -653,6 +653,11 @@ static int cm36651_probe(struct i2c_client *client,
> > cm36651->ps_client = i2c_new_dummy(client->adapter,
> > CM36651_I2C_ADDR_PS);
> > cm36651->ara_client = i2c_new_dummy(client->adapter, CM36651_ARA);
> > + if (!cm36651->ps_client || !cm36651->ara_client) {
> > + dev_err(&client->dev, "%s: new i2c device failed\n", __func__);
> > + ret = -ENODEV;
> > + goto error_i2c_unregister;
> > + }
> The two failures need to be handled independently as we only want to unregister
> those that succeeded. i2c_new_dummy will not return an error and leave a device
> registered. This is particularly true given the first thing that i2c_unregister_device
> does is to derefence the client pointer. That will cause a segfault if you do it
> for NULL as here.
>
Where the segfault would occur? If i2c_new_dummy fails then
i2c_unregister_device() will be called only on NON-null values:
+error_i2c_unregister:
+ if (cm36651->ps_client)
+ i2c_unregister_device(cm36651->ps_client);
+ if (cm36651->ara_client)
+ i2c_unregister_device(cm36651->ara_client);
If probe() succeeds (both i2c_new_dummy return proper pointer) then
remove() will unregister two i2c devices.
> > mutex_init(&cm36651->lock);
> > indio_dev->dev.parent = &client->dev;
> > indio_dev->channels = cm36651_channels;
> > @@ -687,6 +692,11 @@ error_free_irq:
> > free_irq(client->irq, indio_dev);
> > error_disable_reg:
> > regulator_disable(cm36651->vled_reg);
> > +error_i2c_unregister:
> > + if (cm36651->ps_client)
> > + i2c_unregister_device(cm36651->ps_client);
> > + if (cm36651->ara_client)
> > + i2c_unregister_device(cm36651->ara_client);
> > return ret;
> > }
> >
> > @@ -698,6 +708,8 @@ static int cm36651_remove(struct i2c_client *client)
> > iio_device_unregister(indio_dev);
> > regulator_disable(cm36651->vled_reg);
> > free_irq(client->irq, indio_dev);
> > + i2c_unregister_device(cm36651->ps_client);
> > + i2c_unregister_device(cm36651->ara_client);
> Good catch.
> >
> > return 0;
> > }
> >
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] iio: cm36651: Fix i2c client leak and possible NULL pointer dereference
2014-03-17 8:01 ` Krzysztof Kozlowski
@ 2014-03-17 19:24 ` Jonathan Cameron
2014-03-18 8:08 ` Krzysztof Kozlowski
0 siblings, 1 reply; 6+ messages in thread
From: Jonathan Cameron @ 2014-03-17 19:24 UTC (permalink / raw)
To: Krzysztof Kozlowski
Cc: linux-iio, linux-kernel, Beomho Seo, Lars-Peter Clausen
On 17/03/14 08:01, Krzysztof Kozlowski wrote:
> Hi,
>
> On Sat, 2014-03-15 at 16:24 +0000, Jonathan Cameron wrote:
>> On 06/03/14 09:33, Krzysztof Kozlowski wrote:
>>> During probe the driver allocates dummy I2C devices (i2c_new_dummy())
>>> but they aren't unregistered during driver remove or probe failure.
>>>
>>> Additionally driver does not check the return value of i2c_new_dummy().
>>> In case of error (i2c_new_device(): memory allocation failure or I2C
>>> address cannot be used) this function returns NULL which is later
>>> dereferenced by i2c_smbus_{read,write}_data() functions.
>>>
>>> Fix issues by properly checking for i2c_new_dummy() return value and
>>> unregistering I2C devices on driver remove or probe failure.
>>>
>>> Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
>> Good catch, but the error path needs more care.
>>> ---
>>> drivers/iio/light/cm36651.c | 12 ++++++++++++
>>> 1 file changed, 12 insertions(+)
>>>
>>> diff --git a/drivers/iio/light/cm36651.c b/drivers/iio/light/cm36651.c
>>> index a45e07492db3..e7e9a597159f 100644
>>> --- a/drivers/iio/light/cm36651.c
>>> +++ b/drivers/iio/light/cm36651.c
>>> @@ -653,6 +653,11 @@ static int cm36651_probe(struct i2c_client *client,
>>> cm36651->ps_client = i2c_new_dummy(client->adapter,
>>> CM36651_I2C_ADDR_PS);
>>> cm36651->ara_client = i2c_new_dummy(client->adapter, CM36651_ARA);
>>> + if (!cm36651->ps_client || !cm36651->ara_client) {
>>> + dev_err(&client->dev, "%s: new i2c device failed\n", __func__);
>>> + ret = -ENODEV;
>>> + goto error_i2c_unregister;
>>> + }
>> The two failures need to be handled independently as we only want to unregister
>> those that succeeded. i2c_new_dummy will not return an error and leave a device
>> registered. This is particularly true given the first thing that i2c_unregister_device
>> does is to derefence the client pointer. That will cause a segfault if you do it
>> for NULL as here.
>>
>
> Where the segfault would occur? If i2c_new_dummy fails then
> i2c_unregister_device() will be called only on NON-null values:
> +error_i2c_unregister:
> + if (cm36651->ps_client)
> + i2c_unregister_device(cm36651->ps_client);
> + if (cm36651->ara_client)
> + i2c_unregister_device(cm36651->ara_client);
>
> If probe() succeeds (both i2c_new_dummy return proper pointer) then
> remove() will unregister two i2c devices.
>
Oops, I missed that. Still, the form of this is unusual so please
change it to the more conventional option of a goto per error rather
than grouping them. That will also allow you to drop the null checks
below leading to a more obviously correct error path.
>
>>> mutex_init(&cm36651->lock);
>>> indio_dev->dev.parent = &client->dev;
>>> indio_dev->channels = cm36651_channels;
>>> @@ -687,6 +692,11 @@ error_free_irq:
>>> free_irq(client->irq, indio_dev);
>>> error_disable_reg:
>>> regulator_disable(cm36651->vled_reg);
>>> +error_i2c_unregister:
>>> + if (cm36651->ps_client)
>>> + i2c_unregister_device(cm36651->ps_client);
>>> + if (cm36651->ara_client)
>>> + i2c_unregister_device(cm36651->ara_client);
>>> return ret;
>>> }
>>>
>>> @@ -698,6 +708,8 @@ static int cm36651_remove(struct i2c_client *client)
>>> iio_device_unregister(indio_dev);
>>> regulator_disable(cm36651->vled_reg);
>>> free_irq(client->irq, indio_dev);
>>> + i2c_unregister_device(cm36651->ps_client);
>>> + i2c_unregister_device(cm36651->ara_client);
>> Good catch.
>>>
>>> return 0;
>>> }
>>>
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-iio" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] iio: cm36651: Fix i2c client leak and possible NULL pointer dereference
2014-03-17 19:24 ` Jonathan Cameron
@ 2014-03-18 8:08 ` Krzysztof Kozlowski
0 siblings, 0 replies; 6+ messages in thread
From: Krzysztof Kozlowski @ 2014-03-18 8:08 UTC (permalink / raw)
To: Jonathan Cameron; +Cc: linux-iio, linux-kernel, Beomho Seo, Lars-Peter Clausen
On Mon, 2014-03-17 at 19:24 +0000, Jonathan Cameron wrote:
> On 17/03/14 08:01, Krzysztof Kozlowski wrote:
> > Hi,
> >
> > On Sat, 2014-03-15 at 16:24 +0000, Jonathan Cameron wrote:
> >> On 06/03/14 09:33, Krzysztof Kozlowski wrote:
> >>> During probe the driver allocates dummy I2C devices (i2c_new_dummy())
> >>> but they aren't unregistered during driver remove or probe failure.
> >>>
> >>> Additionally driver does not check the return value of i2c_new_dummy().
> >>> In case of error (i2c_new_device(): memory allocation failure or I2C
> >>> address cannot be used) this function returns NULL which is later
> >>> dereferenced by i2c_smbus_{read,write}_data() functions.
> >>>
> >>> Fix issues by properly checking for i2c_new_dummy() return value and
> >>> unregistering I2C devices on driver remove or probe failure.
> >>>
> >>> Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
> >> Good catch, but the error path needs more care.
> >>> ---
> >>> drivers/iio/light/cm36651.c | 12 ++++++++++++
> >>> 1 file changed, 12 insertions(+)
> >>>
> >>> diff --git a/drivers/iio/light/cm36651.c b/drivers/iio/light/cm36651.c
> >>> index a45e07492db3..e7e9a597159f 100644
> >>> --- a/drivers/iio/light/cm36651.c
> >>> +++ b/drivers/iio/light/cm36651.c
> >>> @@ -653,6 +653,11 @@ static int cm36651_probe(struct i2c_client *client,
> >>> cm36651->ps_client = i2c_new_dummy(client->adapter,
> >>> CM36651_I2C_ADDR_PS);
> >>> cm36651->ara_client = i2c_new_dummy(client->adapter, CM36651_ARA);
> >>> + if (!cm36651->ps_client || !cm36651->ara_client) {
> >>> + dev_err(&client->dev, "%s: new i2c device failed\n", __func__);
> >>> + ret = -ENODEV;
> >>> + goto error_i2c_unregister;
> >>> + }
> >> The two failures need to be handled independently as we only want to unregister
> >> those that succeeded. i2c_new_dummy will not return an error and leave a device
> >> registered. This is particularly true given the first thing that i2c_unregister_device
> >> does is to derefence the client pointer. That will cause a segfault if you do it
> >> for NULL as here.
> >>
> >
> > Where the segfault would occur? If i2c_new_dummy fails then
> > i2c_unregister_device() will be called only on NON-null values:
> > +error_i2c_unregister:
> > + if (cm36651->ps_client)
> > + i2c_unregister_device(cm36651->ps_client);
> > + if (cm36651->ara_client)
> > + i2c_unregister_device(cm36651->ara_client);
> >
> > If probe() succeeds (both i2c_new_dummy return proper pointer) then
> > remove() will unregister two i2c devices.
> >
> Oops, I missed that. Still, the form of this is unusual so please
> change it to the more conventional option of a goto per error rather
> than grouping them. That will also allow you to drop the null checks
> below leading to a more obviously correct error path.
Sure, I'll send a fixed version.
Best regards,
Krzysztof
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2014-03-18 8:08 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-06 9:33 [PATCH] iio: cm36651: Fix i2c client leak and possible NULL pointer dereference Krzysztof Kozlowski
2014-03-07 0:34 ` Beomho Seo
2014-03-15 16:24 ` Jonathan Cameron
2014-03-17 8:01 ` Krzysztof Kozlowski
2014-03-17 19:24 ` Jonathan Cameron
2014-03-18 8:08 ` Krzysztof Kozlowski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).