Skip to contentSkip to conversation

mtd: rawnand: s3c2410: Error out when ->nrsets < 0 or ->sets == NULL

PATCH Superseded Boris Brezillon 2 participants 3 messages Jul 19, 2018

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: rawnand: s3c2410: Error out when ->nrsets < 0 or ->sets == NULL

All of the code in the probe path assumes ->sets != NULL and ->nrsets > 0. Error out if that's not the case to avoid dereferencing a NULL pointer.

---
Hello,

I intentionally did not Cc stable because nobody complained so far. Also didn't add a Fixes tag because it's hard to tell when people started to dereferencing ->sets without checking its value.

Regards,

Boris

---
 drivers/mtd/nand/raw/s3c2410.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/mtd/nand/raw/s3c2410.c +8 -6
index 10d81f367d26..5a4a68790653 100644
@@ -1134,8 +1134,13 @@ static int s3c24xx_nand_probe(struct platform_device *pdev)
 
 	dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
 
-	sets = (plat != NULL) ? plat->sets : NULL;
-	nr_sets = (plat != NULL) ? plat->nr_sets : 1;
+	if (!plat->sets || plat->nr_sets < 1) {
+		err = -EINVAL;
+		goto exit_error;
+	}
+
+	sets = plat->sets;
+	nr_sets = plat->nr_sets;
 
 	info->mtd_count = nr_sets;
 
@@ -1152,7 +1157,7 @@ static int s3c24xx_nand_probe(struct platform_device *pdev)
 
 	nmtd = info->mtds;
 
-	for (setno = 0; setno < nr_sets; setno++, nmtd++) {
+	for (setno = 0; setno < nr_sets; setno++, nmtd++, sets++) {
 		struct mtd_info *mtd = nand_to_mtd(&nmtd->chip);
 
 		pr_debug("initialising set %d (%p, info %p)\n",
@@ -1174,9 +1179,6 @@ static int s3c24xx_nand_probe(struct platform_device *pdev)
 			goto exit_error;
 
 		s3c2410_nand_add_partition(info, nmtd, sets);
-
-		if (sets != NULL)
-			sets++;
 	}
 
 	/* initialise the hardware */
Signature · 1 line
2.14.1
#1 lore raw

drivers/mtd/nand/raw/s3c2410.c static int s3c24xx_nand_probe(struct platform_device *pdev)

Quoted 10 lines · Boris Brezillon
drivers/mtd/nand/raw/s3c2410.c +1 -1
@@ -1152,7 +1157,7 @@ static int s3c24xx_nand_probe(struct platform_device *pdev)
 
 	nmtd = info->mtds;
 
-	for (setno = 0; setno < nr_sets; setno++, nmtd++) {
+	for (setno = 0; setno < nr_sets; setno++, nmtd++, sets++) {
 		struct mtd_info *mtd = nand_to_mtd(&nmtd->chip);
 
 		pr_debug("initialising set %d (%p, info %p)\n",
@@ -1174,9 +1179,6 @@ static int s3c24xx_nand_probe(struct platform_device *pdev)

Can you also remove the NULL check from int the middle of the loop:

		err = nand_scan_ident(mtd, (sets) ? sets->nr_chips : 1, NULL);
                                            ^^^^

drivers/mtd/nand/raw/s3c2410.c static int s3c24xx_nand_probe(struct platform_device *pdev) · line 1182

Quoted 7 lines
drivers/mtd/nand/raw/s3c2410.c +0 -3
 			goto exit_error;
 
 		s3c2410_nand_add_partition(info, nmtd, sets);
-
-		if (sets != NULL)
-			sets++;
 	}

regards,
dan carpenter

#2 lore raw
Quoted 9 lines · On Thu, 19 Jul 2018 13:30:47 +0300 Dan Carpenter <[email protected]>…
Quoted 8 lines · Boris Brezillon
drivers/mtd/nand/raw/s3c2410.c +1 -1
@@ -1152,7 +1157,7 @@ static int s3c24xx_nand_probe(struct platform_device *pdev)
 
 	nmtd = info->mtds;
 
-	for (setno = 0; setno < nr_sets; setno++, nmtd++) {
+	for (setno = 0; setno < nr_sets; setno++, nmtd++, sets++) {
 		struct mtd_info *mtd = nand_to_mtd(&nmtd->chip);
 
Quoted 6 lines

drivers/mtd/nand/raw/s3c2410.c static int s3c24xx_nand_probe(struct platform_device *pdev)

Quoted 2 lines
drivers/mtd/nand/raw/s3c2410.c +0 -0
 		pr_debug("initialising set %d (%p, info %p)\n",
@@ -1174,9 +1179,6 @@ static int s3c24xx_nand_probe(struct platform_device *pdev)  

Can you also remove the NULL check from int the middle of the loop:

		err = nand_scan_ident(mtd, (sets) ? sets->nr_chips : 1, NULL);

Sure.

Quoted 13 lines
                                            ^^^^

drivers/mtd/nand/raw/s3c2410.c static int s3c24xx_nand_probe(struct platform_device *pdev) · line 1182

Quoted 7 lines
drivers/mtd/nand/raw/s3c2410.c +0 -3
 			goto exit_error;
 
 		s3c2410_nand_add_partition(info, nmtd, sets);
-
-		if (sets != NULL)
-			sets++;
 	}  

regards,
dan carpenter