Skip to contentSkip to conversation

mtd: core: fix use of uninitialized struct member

PATCH In review Antony Pavlov 3 participants 5 messages Jun 11, 2021 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

cover mtd: core: fix use of uninitialized struct member

Antony Pavlov (2):
mtd: core: fix use of uninitialized struct member
mtd: core: fix whitespaces

 drivers/mtd/core.c | 32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)
Signature · 7 lines
2.32.0.rc0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH 1/2 mtd: core: fix use of uninitialized struct member

E.g. mtd_read() calls mtd_read_oob(), mtd_read_oob() can check uninitialized ops->oobbuf. As a result mtd_read_oob() can return -EOPNOTSUPP.

Found on a MIPS board during /dev/m25p0 read, e.g.

barebox:/ md -s /dev/m25p0
read: error 95

---
 drivers/mtd/core.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
drivers/mtd/core.c +10 -8
index 37fccda6be..98820dfb4f 100644
@@ -379,12 +379,13 @@ int mtd_block_markgood(struct mtd_info *mtd, loff_t ofs)
 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
              u_char *buf)
 {
-	struct mtd_oob_ops ops = {
-		.len = len,
-		.datbuf = buf,
-	};
+	struct mtd_oob_ops ops;
 	int ret;
 
+	memset(&ops, 0, sizeof(ops));
+	ops.len = len;
+	ops.datbuf = buf;
+
 	if (from < 0 || from >= mtd->size || len > mtd->size - from)
 		return -EINVAL;
 	if (!len)
@@ -422,12 +423,13 @@ int mtd_write_oob(struct mtd_info *mtd, loff_t to,
 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
               const u_char *buf)
 {
-        struct mtd_oob_ops ops = {
-                .len = len,
-                .datbuf = (u8 *)buf,
-        };
+	struct mtd_oob_ops ops;
         int ret;
 
+	memset(&ops, 0, sizeof(ops));
+	ops.len = len;
+	ops.datbuf = buf;
+
 	if (to < 0 || to >= mtd->size || len > mtd->size - to)
 		return -EINVAL;
 	if (!len)
Signature · 7 lines
2.32.0.rc0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

Hi Antony,

Quoted 22 lines · Antony Pavlov

E.g. mtd_read() calls mtd_read_oob(), mtd_read_oob() can check uninitialized ops->oobbuf. As a result mtd_read_oob() can return -EOPNOTSUPP.

Found on a MIPS board during /dev/m25p0 read, e.g.

barebox:/ md -s /dev/m25p0
read: error 95

---
 drivers/mtd/core.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
drivers/mtd/core.c +0 -0
index 37fccda6be..98820dfb4f 100644
@@ -379,12 +379,13 @@ int mtd_block_markgood(struct mtd_info *mtd, loff_t ofs)
 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
              u_char *buf)
 {

drivers/mtd/core.c int mtd_block_markgood(struct mtd_info *mtd, loff_t ofs) · line 387

Quoted 10 lines
drivers/mtd/core.c +4 -4
-	struct mtd_oob_ops ops = {
-		.len = len,
-		.datbuf = buf,
-	};
+	struct mtd_oob_ops ops;
 	int ret;
 
+	memset(&ops, 0, sizeof(ops));
+	ops.len = len;
+	ops.datbuf = buf;

If this fixes something for you then you have severe problems with your compiler. Omitted fields are implicitly initialized to zero, often you can even find the corresponding call to memset in the objdump.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw
Quoted 32 lines · Sascha Hauer

Hi Antony,

Quoted 29 lines · Antony Pavlov

E.g. mtd_read() calls mtd_read_oob(), mtd_read_oob() can check uninitialized ops->oobbuf. As a result mtd_read_oob() can return -EOPNOTSUPP.

Found on a MIPS board during /dev/m25p0 read, e.g.

barebox:/ md -s /dev/m25p0
read: error 95

---
 drivers/mtd/core.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
drivers/mtd/core.c +1 -4
index 37fccda6be..98820dfb4f 100644
@@ -379,12 +379,13 @@ int mtd_block_markgood(struct mtd_info *mtd, loff_t ofs)
 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
              u_char *buf)
 {
-	struct mtd_oob_ops ops = {
-		.len = len,
-		.datbuf = buf,
-	};
+	struct mtd_oob_ops ops;
 	int ret;
 
Quoted 7 lines

drivers/mtd/core.c int mtd_block_markgood(struct mtd_info *mtd, loff_t ofs) · line 387

Quoted 3 lines
drivers/mtd/core.c +3 -0
+	memset(&ops, 0, sizeof(ops));
+	ops.len = len;
+	ops.datbuf = buf;

If this fixes something for you then you have severe problems with your compiler. Omitted fields are implicitly initialized to zero, often you can even find the corresponding call to memset in the objdump.

Perhaps elsewhere something is interpreting the padding, which may not be zeroed when using an initializer list. This warrants further investigation for sure.

Cheers,
Ahmad

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw

PATCH 2/2 mtd: core: fix whitespaces

In reply to Antony Pavlov
---
 drivers/mtd/core.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/mtd/core.c +7 -7
index 98820dfb4f..2029e06d9e 100644
@@ -377,7 +377,7 @@ int mtd_block_markgood(struct mtd_info *mtd, loff_t ofs)
 }
 
 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
-             u_char *buf)
+	     u_char *buf)
 {
 	struct mtd_oob_ops ops;
 	int ret;
@@ -421,10 +421,10 @@ int mtd_write_oob(struct mtd_info *mtd, loff_t to,
 }
 
 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
-              const u_char *buf)
+	      const u_char *buf)
 {
 	struct mtd_oob_ops ops;
-        int ret;
+	int ret;
 
 	memset(&ops, 0, sizeof(ops));
 	ops.len = len;
@@ -435,10 +435,10 @@ int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
 	if (!len)
 		return 0;
 
-        ret = mtd_write_oob(mtd, to, &ops);
-        *retlen = ops.retlen;
+	ret = mtd_write_oob(mtd, to, &ops);
+	*retlen = ops.retlen;
 
-        return ret;
+	return ret;
 }
 
 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
@@ -759,7 +759,7 @@ err:
 	return ret;
 }
 
-int del_mtd_device (struct mtd_info *mtd)
+int del_mtd_device(struct mtd_info *mtd)
 {
 	struct mtddev_hook *hook;
 
Signature · 7 lines
2.32.0.rc0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox