diff mbox

[v2,2/2] mtd: nand: atmel_nand: fix a possible NULL dereference

Message ID 1448005517-26004-3-git-send-email-clabbe.montjoie@gmail.com
State Changes Requested
Headers show

Commit Message

Corentin Labbe Nov. 20, 2015, 7:45 a.m. UTC
of_match_device could return NULL, and so cause a NULL pointer
dereference later.

Signed-off-by: LABBE Corentin <clabbe.montjoie@gmail.com>
---
 drivers/mtd/nand/atmel_nand.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Josh Wu Nov. 20, 2015, 8:33 a.m. UTC | #1
Hi, Corentin

Thanks for the patch. It looks fine to me, just one nitpick in below:

On 11/20/2015 3:45 PM, LABBE Corentin wrote:
> of_match_device could return NULL, and so cause a NULL pointer
> dereference later.
>
> Signed-off-by: LABBE Corentin <clabbe.montjoie@gmail.com>
> ---
>   drivers/mtd/nand/atmel_nand.c | 5 +++--
>   1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/mtd/nand/atmel_nand.c b/drivers/mtd/nand/atmel_nand.c
> index 475c938..7902967 100644
> --- a/drivers/mtd/nand/atmel_nand.c
> +++ b/drivers/mtd/nand/atmel_nand.c
> @@ -1496,8 +1496,9 @@ static int atmel_of_init_port(struct atmel_nand_host *host,
>   	struct atmel_nand_data *board = &host->board;
>   	enum of_gpio_flags flags = 0;
>   
> -	host->caps = (struct atmel_nand_caps *)
> -		of_match_device(atmel_nand_dt_ids, host->dev)->data;
> +	host->caps = of_device_get_match_data(host->dev);
> +	if (!host->caps)
> +		return 1;

it's better to use -EINVAL here.

Best Regards,
Josh Wu

>   
>   	if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
>   		if (val >= 32) {
Corentin Labbe Nov. 24, 2015, 1:12 p.m. UTC | #2
On Fri, Nov 20, 2015 at 04:33:14PM +0800, Josh Wu wrote:
> Hi, Corentin
> 
> Thanks for the patch. It looks fine to me, just one nitpick in below:
> 
> On 11/20/2015 3:45 PM, LABBE Corentin wrote:
> > of_match_device could return NULL, and so cause a NULL pointer
> > dereference later.
> >
> > Signed-off-by: LABBE Corentin <clabbe.montjoie@gmail.com>
> > ---
> >   drivers/mtd/nand/atmel_nand.c | 5 +++--
> >   1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/mtd/nand/atmel_nand.c b/drivers/mtd/nand/atmel_nand.c
> > index 475c938..7902967 100644
> > --- a/drivers/mtd/nand/atmel_nand.c
> > +++ b/drivers/mtd/nand/atmel_nand.c
> > @@ -1496,8 +1496,9 @@ static int atmel_of_init_port(struct atmel_nand_host *host,
> >   	struct atmel_nand_data *board = &host->board;
> >   	enum of_gpio_flags flags = 0;
> >   
> > -	host->caps = (struct atmel_nand_caps *)
> > -		of_match_device(atmel_nand_dt_ids, host->dev)->data;
> > +	host->caps = of_device_get_match_data(host->dev);
> > +	if (!host->caps)
> > +		return 1;
> 
> it's better to use -EINVAL here.
> 

Hello

I do that Uwe Kleine-König said to me to do in others thread:
https://lkml.org/lkml/2015/11/12/70 and https://lkml.org/lkml/2015/11/16/211

Regards
Josh Wu Nov. 25, 2015, 2:20 a.m. UTC | #3
Hi, Corentin

On 11/24/2015 9:12 PM, LABBE Corentin wrote:
> On Fri, Nov 20, 2015 at 04:33:14PM +0800, Josh Wu wrote:
>> Hi, Corentin
>>
>> Thanks for the patch. It looks fine to me, just one nitpick in below:
>>
>> On 11/20/2015 3:45 PM, LABBE Corentin wrote:
>>> of_match_device could return NULL, and so cause a NULL pointer
>>> dereference later.
>>>
>>> Signed-off-by: LABBE Corentin <clabbe.montjoie@gmail.com>
>>> ---
>>>    drivers/mtd/nand/atmel_nand.c | 5 +++--
>>>    1 file changed, 3 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/drivers/mtd/nand/atmel_nand.c b/drivers/mtd/nand/atmel_nand.c
>>> index 475c938..7902967 100644
>>> --- a/drivers/mtd/nand/atmel_nand.c
>>> +++ b/drivers/mtd/nand/atmel_nand.c
>>> @@ -1496,8 +1496,9 @@ static int atmel_of_init_port(struct atmel_nand_host *host,
>>>    	struct atmel_nand_data *board = &host->board;
>>>    	enum of_gpio_flags flags = 0;
>>>    
>>> -	host->caps = (struct atmel_nand_caps *)
>>> -		of_match_device(atmel_nand_dt_ids, host->dev)->data;
>>> +	host->caps = of_device_get_match_data(host->dev);
>>> +	if (!host->caps)
>>> +		return 1;
>> it's better to use -EINVAL here.
>>
> Hello
>
> I do that Uwe Kleine-König said to me to do in others thread:
> https://lkml.org/lkml/2015/11/12/70 and https://lkml.org/lkml/2015/11/16/211

Thank you for the information.
I checked the imx serial driver(drivers/tty/serial/imx.c) in 
serial_imx_probe():

     ...
     ret = serial_imx_probe_dt(sport, pdev);
     if (ret > 0)     --->  So here is why you need to return 1 in 
serial_imx_probe_dt().
         serial_imx_probe_pdata(sport, pdev);
     else if (ret < 0)
         return ret;

There has a branch to check whether ret is > 0 or < 0. So that's why you 
need to return 1 in imx serial driver.

But in atmel_nand driver, we don't have such code for that. The 
atmel_of_init_port() is only used for dt probe. So if dt is not matched, 
just return an error.

>
> Regards
>
Best Regards,
Josh Wu
diff mbox

Patch

diff --git a/drivers/mtd/nand/atmel_nand.c b/drivers/mtd/nand/atmel_nand.c
index 475c938..7902967 100644
--- a/drivers/mtd/nand/atmel_nand.c
+++ b/drivers/mtd/nand/atmel_nand.c
@@ -1496,8 +1496,9 @@  static int atmel_of_init_port(struct atmel_nand_host *host,
 	struct atmel_nand_data *board = &host->board;
 	enum of_gpio_flags flags = 0;
 
-	host->caps = (struct atmel_nand_caps *)
-		of_match_device(atmel_nand_dt_ids, host->dev)->data;
+	host->caps = of_device_get_match_data(host->dev);
+	if (!host->caps)
+		return 1;
 
 	if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
 		if (val >= 32) {