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

Antony Pavlov antonynpavlov at gmail.com
Thu Jun 10 23:39:13 PDT 2021


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

Signed-off-by: Antony Pavlov <antonynpavlov at gmail.com>
---
 drivers/mtd/core.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/drivers/mtd/core.c b/drivers/mtd/core.c
index 37fccda6be..98820dfb4f 100644
--- a/drivers/mtd/core.c
+++ b/drivers/mtd/core.c
@@ -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)
-- 
2.32.0.rc0




More information about the barebox mailing list