Skip to contentSkip to conversation

mtd: nand: fix NULL pointer dereference in of_get_nand_ecc_algo

PATCH Superseded Rafał Miłecki 2 participants 5 messages Apr 26, 2016 – Apr 27, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: nand: fix NULL pointer dereference in of_get_nand_ecc_algo

Our array nand_ecc_algos doesn't specify mappings for all available enum nand_ecc_algo values. The one missing there is NAND_ECC_UNKNOWN as this value is reserved for algorithm not being specified at all. It means we have to be careful when iterating this array and handle NULL values.

---
Hi Boris,

Sorry for this crash in nand subsystem :( If you think it's OK you may pick this patch as a fixup for
c6e002a7ca9f ("mtd: nand: add support for "nand-ecc-algo" DT property")

---
 drivers/mtd/nand/nand_base.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/mtd/nand/nand_base.c +2 -0
index a5417a0..0eaa9dc 100644
@@ -4016,6 +4016,8 @@ static int of_get_nand_ecc_algo(struct device_node *np)
 	err = of_property_read_string(np, "nand-ecc-algo", &pm);
 	if (!err) {
 		for (i = 0; i < ARRAY_SIZE(nand_ecc_algos); i++)
+			if (!nand_ecc_algos[i])
+				continue;
 			if (!strcasecmp(pm, nand_ecc_algos[i]))
 				return i;
 		return -ENODEV;
Signature · 1 line
1.8.4.5
#1 lore raw
Quoted 22 lines · On Tue, 26 Apr 2016 20:36:16 +0200 Rafał Miłecki <[email protected]> wrote:

Our array nand_ecc_algos doesn't specify mappings for all available enum nand_ecc_algo values. The one missing there is NAND_ECC_UNKNOWN as this value is reserved for algorithm not being specified at all. It means we have to be careful when iterating this array and handle NULL values.

---
Hi Boris,

Sorry for this crash in nand subsystem :( If you think it's OK you may pick this patch as a fixup for
c6e002a7ca9f ("mtd: nand: add support for "nand-ecc-algo" DT property")

---
 drivers/mtd/nand/nand_base.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/mtd/nand/nand_base.c +0 -0
index a5417a0..0eaa9dc 100644

drivers/mtd/nand/nand_base.c static int of_get_nand_ecc_algo(struct device_node *np) · line 4020

Quoted 5 lines
drivers/mtd/nand/nand_base.c +2 -0
@@ -4016,6 +4016,8 @@ static int of_get_nand_ecc_algo(struct device_node *np)
 	err = of_property_read_string(np, "nand-ecc-algo", &pm);
 	if (!err) {
 		for (i = 0; i < ARRAY_SIZE(nand_ecc_algos); i++)
+			if (!nand_ecc_algos[i])
+				continue;

Can we add an "unknown" entry, or start iterating at NAND_ECC_HAMMING or NAND_ECC_UNKNOWN + 1 instead of adding this extra test?

BTW, since the original commit has been applied recently I'll squash the changes into the previous commit and rebase my branch instead of adding a new patch (I hate breaking bisectability).

Signature · 3 lines
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
#2 lore raw
Quoted 20 lines · Boris Brezillon
Quoted 17 lines · On Tue, 26 Apr 2016 20:36:16 +0200 Rafał Miłecki <[email protected]> wrote:

Our array nand_ecc_algos doesn't specify mappings for all available enum nand_ecc_algo values. The one missing there is NAND_ECC_UNKNOWN as this value is reserved for algorithm not being specified at all. It means we have to be careful when iterating this array and handle NULL values.

---
Hi Boris,

Sorry for this crash in nand subsystem :( If you think it's OK you may pick this patch as a fixup for
c6e002a7ca9f ("mtd: nand: add support for "nand-ecc-algo" DT property")

---
 drivers/mtd/nand/nand_base.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 13 lines

drivers/mtd/nand/nand_base.c static int of_get_nand_ecc_algo(struct device_node *np) · line 4020

Quoted 10 lines
drivers/mtd/nand/nand_base.c +2 -0
index a5417a0..0eaa9dc 100644
@@ -4016,6 +4016,8 @@ static int of_get_nand_ecc_algo(struct device_node *np)
      err = of_property_read_string(np, "nand-ecc-algo", &pm);
      if (!err) {
              for (i = 0; i < ARRAY_SIZE(nand_ecc_algos); i++)
+                     if (!nand_ecc_algos[i])
+                             continue;

Can we add an "unknown" entry, or start iterating at NAND_ECC_HAMMING or NAND_ECC_UNKNOWN + 1 instead of adding this extra test?

Sure, if you think it's better. I'll send V2.

Signature · 1 line
Rafał
#3 lore raw

PATCH v2 1/1 mtd: nand: fix NULL pointer dereference in of_get_nand_ecc_algo

In reply to Rafał Miłecki

Our array nand_ecc_algos doesn't specify mappings for all available enum nand_ecc_algo values. The one missing there is NAND_ECC_UNKNOWN as this value is reserved for algorithm not being specified at all. It means we have to be careful when iterating this array and avoid NULL values.

---
V2: Iterate from NAND_ECC_HAMMING instead of checking for NULL as preferred by Boris.

---
 drivers/mtd/nand/nand_base.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/nand/nand_base.c +1 -1
index a5417a0..2e6ba44 100644
@@ -4015,7 +4015,7 @@ static int of_get_nand_ecc_algo(struct device_node *np)
 
 	err = of_property_read_string(np, "nand-ecc-algo", &pm);
 	if (!err) {
-		for (i = 0; i < ARRAY_SIZE(nand_ecc_algos); i++)
+		for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
 			if (!strcasecmp(pm, nand_ecc_algos[i]))
 				return i;
 		return -ENODEV;
Signature · 1 line
1.8.4.5
#4 lore raw
Quoted 7 lines · On Tue, 26 Apr 2016 23:16:47 +0200 Rafał Miłecki <[email protected]> wrote:

Our array nand_ecc_algos doesn't specify mappings for all available enum nand_ecc_algo values. The one missing there is NAND_ECC_UNKNOWN as this value is reserved for algorithm not being specified at all. It means we have to be careful when iterating this array and avoid NULL values.

Changes squashed into "mtd: nand: add support for "nand-ecc-algo" DT property".

Thanks,

Boris

Quoted 20 lines

---
V2: Iterate from NAND_ECC_HAMMING instead of checking for NULL as preferred by Boris.

---
 drivers/mtd/nand/nand_base.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/nand/nand_base.c +1 -1
index a5417a0..2e6ba44 100644
@@ -4015,7 +4015,7 @@ static int of_get_nand_ecc_algo(struct device_node *np)
 
 	err = of_property_read_string(np, "nand-ecc-algo", &pm);
 	if (!err) {
-		for (i = 0; i < ARRAY_SIZE(nand_ecc_algos); i++)
+		for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
 			if (!strcasecmp(pm, nand_ecc_algos[i]))
 				return i;
 		return -ENODEV;
Signature · 3 lines
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com