linux-mediatek.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mtd: rawnand: mtk: avoid underflow in mtk_nfc_nand_chip_init()
@ 2020-09-16 19:40 Evgeny Novikov
  2020-09-17 16:30 ` Miquel Raynal
  0 siblings, 1 reply; 3+ messages in thread
From: Evgeny Novikov @ 2020-09-16 19:40 UTC (permalink / raw)
  To: Miquel Raynal
  Cc: ldv-project, Vignesh Raghavendra, Gustavo A. R. Silva,
	Evgeny Novikov, Richard Weinberger, linux-kernel, Stephen Boyd,
	Masahiro Yamada, Boris Brezillon, linux-mtd, Matthias Brugger,
	linux-mediatek, linux-arm-kernel

If of_get_property() will set nsels to negative values the driver may
allocate insufficient memory for chip. Moreover, there may be underflow
for devm_kzalloc(). This can result in various bad consequences later.
The patch causes mtk_nfc_nand_chip_init() to fail for negative values of
nsels.

Found by Linux Driver Verification project (linuxtesting.org).

Signed-off-by: Evgeny Novikov <novikov@ispras.ru>
---
 drivers/mtd/nand/raw/mtk_nand.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/raw/mtk_nand.c b/drivers/mtd/nand/raw/mtk_nand.c
index ad1b55dab211..df98a2eec240 100644
--- a/drivers/mtd/nand/raw/mtk_nand.c
+++ b/drivers/mtd/nand/raw/mtk_nand.c
@@ -1376,7 +1376,7 @@ static int mtk_nfc_nand_chip_init(struct device *dev, struct mtk_nfc *nfc,
 		return -ENODEV;
 
 	nsels /= sizeof(u32);
-	if (!nsels || nsels > MTK_NAND_MAX_NSELS) {
+	if (nsels <= 0 || nsels > MTK_NAND_MAX_NSELS) {
 		dev_err(dev, "invalid reg property size %d\n", nsels);
 		return -EINVAL;
 	}
-- 
2.26.2


_______________________________________________
Linux-mediatek mailing list
Linux-mediatek@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-mediatek

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] mtd: rawnand: mtk: avoid underflow in mtk_nfc_nand_chip_init()
  2020-09-16 19:40 [PATCH] mtd: rawnand: mtk: avoid underflow in mtk_nfc_nand_chip_init() Evgeny Novikov
@ 2020-09-17 16:30 ` Miquel Raynal
  2020-09-18  9:33   ` Evgeny Novikov
  0 siblings, 1 reply; 3+ messages in thread
From: Miquel Raynal @ 2020-09-17 16:30 UTC (permalink / raw)
  To: Evgeny Novikov
  Cc: ldv-project, Vignesh Raghavendra, Gustavo A. R. Silva,
	Richard Weinberger, linux-kernel, Stephen Boyd, Masahiro Yamada,
	Boris Brezillon, linux-mtd, Matthias Brugger, linux-mediatek,
	linux-arm-kernel

Hi Evgeny,

Evgeny Novikov <novikov@ispras.ru> wrote on Wed, 16 Sep 2020 22:40:45
+0300:

> If of_get_property() will set nsels to negative values the driver may

Is this really a possible case?

Looking at the OF code, I don't think it can ever happen...

> allocate insufficient memory for chip. Moreover, there may be underflow
> for devm_kzalloc(). This can result in various bad consequences later.
> The patch causes mtk_nfc_nand_chip_init() to fail for negative values of
> nsels.
> 
> Found by Linux Driver Verification project (linuxtesting.org).
> 
> Signed-off-by: Evgeny Novikov <novikov@ispras.ru>
> ---
>  drivers/mtd/nand/raw/mtk_nand.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/mtd/nand/raw/mtk_nand.c b/drivers/mtd/nand/raw/mtk_nand.c
> index ad1b55dab211..df98a2eec240 100644
> --- a/drivers/mtd/nand/raw/mtk_nand.c
> +++ b/drivers/mtd/nand/raw/mtk_nand.c
> @@ -1376,7 +1376,7 @@ static int mtk_nfc_nand_chip_init(struct device *dev, struct mtk_nfc *nfc,
>  		return -ENODEV;
>  
>  	nsels /= sizeof(u32);
> -	if (!nsels || nsels > MTK_NAND_MAX_NSELS) {
> +	if (nsels <= 0 || nsels > MTK_NAND_MAX_NSELS) {
>  		dev_err(dev, "invalid reg property size %d\n", nsels);
>  		return -EINVAL;
>  	}

Thanks,
Miquèl

_______________________________________________
Linux-mediatek mailing list
Linux-mediatek@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-mediatek

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] mtd: rawnand: mtk: avoid underflow in mtk_nfc_nand_chip_init()
  2020-09-17 16:30 ` Miquel Raynal
@ 2020-09-18  9:33   ` Evgeny Novikov
  0 siblings, 0 replies; 3+ messages in thread
From: Evgeny Novikov @ 2020-09-18  9:33 UTC (permalink / raw)
  To: Miquel Raynal
  Cc: ldv-project, Vignesh Raghavendra, Gustavo A. R. Silva,
	Richard Weinberger, linux-kernel, Stephen Boyd, Masahiro Yamada,
	Boris Brezillon, linux-mtd, Matthias Brugger, linux-mediatek,
	linux-arm-kernel

Hi Miquel,

17.09.2020, 19:30, "Miquel Raynal" <miquel.raynal@bootlin.com>:
> Hi Evgeny,
>
> Evgeny Novikov <novikov@ispras.ru> wrote on Wed, 16 Sep 2020 22:40:45
> +0300:
>
>>  If of_get_property() will set nsels to negative values the driver may
>
> Is this really a possible case?
>
> Looking at the OF code, I don't think it can ever happen...

I do not know whether this is possible now or it will be possible one day
in the future.

Our tool assumed that this can happen according to the type (int).

Also, I can not find any driver that may suffer from this. Two examples
where negative cases are forbidden intentionally or unintentionally:
- drivers/sbus/char/openprom.c
- drivers/video/fbdev/omap2/omapfb/dss/dsi.c

Best regards,
Evgeny

>>  allocate insufficient memory for chip. Moreover, there may be underflow
>>  for devm_kzalloc(). This can result in various bad consequences later.
>>  The patch causes mtk_nfc_nand_chip_init() to fail for negative values of
>>  nsels.
>>
>>  Found by Linux Driver Verification project (linuxtesting.org).
>>
>>  Signed-off-by: Evgeny Novikov <novikov@ispras.ru>
>>  ---
>>   drivers/mtd/nand/raw/mtk_nand.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>>  diff --git a/drivers/mtd/nand/raw/mtk_nand.c b/drivers/mtd/nand/raw/mtk_nand.c
>>  index ad1b55dab211..df98a2eec240 100644
>>  --- a/drivers/mtd/nand/raw/mtk_nand.c
>>  +++ b/drivers/mtd/nand/raw/mtk_nand.c
>>  @@ -1376,7 +1376,7 @@ static int mtk_nfc_nand_chip_init(struct device *dev, struct mtk_nfc *nfc,
>>                   return -ENODEV;
>>
>>           nsels /= sizeof(u32);
>>  - if (!nsels || nsels > MTK_NAND_MAX_NSELS) {
>>  + if (nsels <= 0 || nsels > MTK_NAND_MAX_NSELS) {
>>                   dev_err(dev, "invalid reg property size %d\n", nsels);
>>                   return -EINVAL;
>>           }
>
> Thanks,
> Miquèl

_______________________________________________
Linux-mediatek mailing list
Linux-mediatek@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-mediatek

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-09-18  9:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-16 19:40 [PATCH] mtd: rawnand: mtk: avoid underflow in mtk_nfc_nand_chip_init() Evgeny Novikov
2020-09-17 16:30 ` Miquel Raynal
2020-09-18  9:33   ` Evgeny Novikov

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).