2017-07-07 06:39:50

by Gustavo A. R. Silva

[permalink] [raw]
Subject: [PATCH] pwm: sun4i: add NULL check on of_match_device() return value

Check return value from call to of_match_device()
in order to prevent a NULL pointer dereference.

In case of NULL print error message and return -ENODEV

Signed-off-by: Gustavo A. R. Silva <[email protected]>
---
drivers/pwm/pwm-sun4i.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/drivers/pwm/pwm-sun4i.c b/drivers/pwm/pwm-sun4i.c
index 1284ffa..295ca19 100644
--- a/drivers/pwm/pwm-sun4i.c
+++ b/drivers/pwm/pwm-sun4i.c
@@ -321,6 +321,10 @@ static int sun4i_pwm_probe(struct platform_device *pdev)
const struct of_device_id *match;

match = of_match_device(sun4i_pwm_dt_ids, &pdev->dev);
+ if (!match) {
+ dev_err(&pdev->dev, "failed to match device\n");
+ return -ENODEV;
+ }

pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
if (!pwm)
--
2.5.0


2017-07-07 07:16:50

by Chen-Yu Tsai

[permalink] [raw]
Subject: Re: [PATCH] pwm: sun4i: add NULL check on of_match_device() return value

On Fri, Jul 7, 2017 at 2:39 PM, Gustavo A. R. Silva
<[email protected]> wrote:
> Check return value from call to of_match_device()
> in order to prevent a NULL pointer dereference.
>
> In case of NULL print error message and return -ENODEV
>
> Signed-off-by: Gustavo A. R. Silva <[email protected]>
> ---
> drivers/pwm/pwm-sun4i.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/drivers/pwm/pwm-sun4i.c b/drivers/pwm/pwm-sun4i.c
> index 1284ffa..295ca19 100644
> --- a/drivers/pwm/pwm-sun4i.c
> +++ b/drivers/pwm/pwm-sun4i.c
> @@ -321,6 +321,10 @@ static int sun4i_pwm_probe(struct platform_device *pdev)
> const struct of_device_id *match;
>
> match = of_match_device(sun4i_pwm_dt_ids, &pdev->dev);

AFAIK this simply can not and should not fail with a device tree
only driver with properly populated of_device_id tables.

ChenYu

> + if (!match) {
> + dev_err(&pdev->dev, "failed to match device\n");
> + return -ENODEV;
> + }
>
> pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
> if (!pwm)
> --
> 2.5.0
>
>
> _______________________________________________
> linux-arm-kernel mailing list
> [email protected]
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

2017-07-07 09:33:08

by Thierry Reding

[permalink] [raw]
Subject: Re: [PATCH] pwm: sun4i: add NULL check on of_match_device() return value

On Fri, Jul 07, 2017 at 03:16:23PM +0800, Chen-Yu Tsai wrote:
> On Fri, Jul 7, 2017 at 2:39 PM, Gustavo A. R. Silva
> <[email protected]> wrote:
> > Check return value from call to of_match_device()
> > in order to prevent a NULL pointer dereference.
> >
> > In case of NULL print error message and return -ENODEV
> >
> > Signed-off-by: Gustavo A. R. Silva <[email protected]>
> > ---
> > drivers/pwm/pwm-sun4i.c | 4 ++++
> > 1 file changed, 4 insertions(+)
> >
> > diff --git a/drivers/pwm/pwm-sun4i.c b/drivers/pwm/pwm-sun4i.c
> > index 1284ffa..295ca19 100644
> > --- a/drivers/pwm/pwm-sun4i.c
> > +++ b/drivers/pwm/pwm-sun4i.c
> > @@ -321,6 +321,10 @@ static int sun4i_pwm_probe(struct platform_device *pdev)
> > const struct of_device_id *match;
> >
> > match = of_match_device(sun4i_pwm_dt_ids, &pdev->dev);
>
> AFAIK this simply can not and should not fail with a device tree
> only driver with properly populated of_device_id tables.

Technically this could happen if someone manually created the platform
device. In that case the core will try to match by name if there is no
device node to use for a match against the list of of_device_ids.

In my opinion it's best to let things crash in this situation to make
it really obvious that people have done something wrong.

Thierry


Attachments:
(No filename) (1.29 kB)
signature.asc (833.00 B)
Download all attachments

2017-07-10 19:26:57

by Gustavo A. R. Silva

[permalink] [raw]
Subject: Re: [PATCH] pwm: sun4i: add NULL check on of_match_device() return value

Hi all,

Quoting Thierry Reding <[email protected]>:

> On Fri, Jul 07, 2017 at 03:16:23PM +0800, Chen-Yu Tsai wrote:
>> On Fri, Jul 7, 2017 at 2:39 PM, Gustavo A. R. Silva
>> <[email protected]> wrote:
>> > Check return value from call to of_match_device()
>> > in order to prevent a NULL pointer dereference.
>> >
>> > In case of NULL print error message and return -ENODEV
>> >
>> > Signed-off-by: Gustavo A. R. Silva <[email protected]>
>> > ---
>> > drivers/pwm/pwm-sun4i.c | 4 ++++
>> > 1 file changed, 4 insertions(+)
>> >
>> > diff --git a/drivers/pwm/pwm-sun4i.c b/drivers/pwm/pwm-sun4i.c
>> > index 1284ffa..295ca19 100644
>> > --- a/drivers/pwm/pwm-sun4i.c
>> > +++ b/drivers/pwm/pwm-sun4i.c
>> > @@ -321,6 +321,10 @@ static int sun4i_pwm_probe(struct
>> platform_device *pdev)
>> > const struct of_device_id *match;
>> >
>> > match = of_match_device(sun4i_pwm_dt_ids, &pdev->dev);
>>
>> AFAIK this simply can not and should not fail with a device tree
>> only driver with properly populated of_device_id tables.
>
> Technically this could happen if someone manually created the platform
> device. In that case the core will try to match by name if there is no
> device node to use for a match against the list of of_device_ids.
>
> In my opinion it's best to let things crash in this situation to make
> it really obvious that people have done something wrong.
>

I get it.

Thanks for clarifying.
--
Gustavo A. R. Silva