[OpenWrt-Devel] [PATCH 3/3] kernel: don't auto attach ubi on read error

Mathias Kresin dev at kresin.me
Thu Jul 19 13:05:34 EDT 2018


With a10a204aab26cc ("kernel: make ubi auto-attach check for a tar file
magic") the check for the magic was added without considering a failing
mtd_read(). If the read fails, no check is done and the mount code is
called straight away.

Failing with an error message for such cases seems to me the cleaner way,
as it would allow to spot hidden/workaround issues.

Signed-off-by: Mathias Kresin <dev at kresin.me>
---
 ...auto-attach-mtd-device-named-ubi-or-data-on-boot.patch | 15 ++++++++++-----
 ...auto-attach-mtd-device-named-ubi-or-data-on-boot.patch | 15 ++++++++++-----
 2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/target/linux/generic/pending-4.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch b/target/linux/generic/pending-4.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
index b6890e3..ae93d12 100644
--- a/target/linux/generic/pending-4.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
+++ b/target/linux/generic/pending-4.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
@@ -8,7 +8,7 @@ Signed-off-by: Daniel Golle <daniel at makrotopia.org>
 
 --- a/drivers/mtd/ubi/build.c
 +++ b/drivers/mtd/ubi/build.c
-@@ -1171,6 +1171,67 @@ static struct mtd_info * __init open_mtd
+@@ -1171,6 +1171,72 @@ static struct mtd_info * __init open_mtd
  	return mtd;
  }
  
@@ -44,10 +44,15 @@ Signed-off-by: Daniel Golle <daniel at makrotopia.org>
 +			}
 +		}
 +
-+	/* check for a valid ubi magic if read from flash was successful */
++	/* check if the read from flash was successful */
 +	err = mtd_read(mtd, offset, 4, &len, (void *) magic);
-+	if ((!err || mtd_is_bitflip(err)) &&
-+	    len == 4 && strncmp(magic, "UBI#", 4)) {
++	if ((err && !mtd_is_bitflip(err)) || len != 4) {
++		pr_err("UBI error: unable to read from mtd%d\n", mtd->index);
++		goto cleanup;
++	}
++
++	/* check for a valid ubi magic */
++	if (strncmp(magic, "UBI#", 4)) {
 +		pr_err("UBI error: no valid UBI magic found inside mtd%d\n", mtd->index);
 +		goto cleanup;
 +	}
@@ -76,7 +81,7 @@ Signed-off-by: Daniel Golle <daniel at makrotopia.org>
  static int __init ubi_init(void)
  {
  	int err, i, k;
-@@ -1254,6 +1315,12 @@ static int __init ubi_init(void)
+@@ -1254,6 +1320,12 @@ static int __init ubi_init(void)
  		}
  	}
  
diff --git a/target/linux/generic/pending-4.9/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch b/target/linux/generic/pending-4.9/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
index 4051a01..8ce508e 100644
--- a/target/linux/generic/pending-4.9/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
+++ b/target/linux/generic/pending-4.9/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
@@ -8,7 +8,7 @@ Signed-off-by: Daniel Golle <daniel at makrotopia.org>
 
 --- a/drivers/mtd/ubi/build.c
 +++ b/drivers/mtd/ubi/build.c
-@@ -1226,6 +1226,67 @@ static struct mtd_info * __init open_mtd
+@@ -1226,6 +1226,72 @@ static struct mtd_info * __init open_mtd
  	return mtd;
  }
  
@@ -44,10 +44,15 @@ Signed-off-by: Daniel Golle <daniel at makrotopia.org>
 +			}
 +		}
 +
-+	/* check for a valid ubi magic if read from flash was successful */
++	/* check if the read from flash was successful */
 +	err = mtd_read(mtd, offset, 4, &len, (void *) magic);
-+	if ((!err || mtd_is_bitflip(err)) &&
-+	    len == 4 && strncmp(magic, "UBI#", 4)) {
++	if ((err && !mtd_is_bitflip(err)) || len != 4) {
++		pr_err("UBI error: unable to read from mtd%d\n", mtd->index);
++		goto cleanup;
++	}
++
++	/* check for a valid ubi magic */
++	if (strncmp(magic, "UBI#", 4)) {
 +		pr_err("UBI error: no valid UBI magic found inside mtd%d\n", mtd->index);
 +		goto cleanup;
 +	}
@@ -76,7 +81,7 @@ Signed-off-by: Daniel Golle <daniel at makrotopia.org>
  static int __init ubi_init(void)
  {
  	int err, i, k;
-@@ -1309,6 +1370,12 @@ static int __init ubi_init(void)
+@@ -1309,6 +1375,12 @@ static int __init ubi_init(void)
  		}
  	}
  
-- 
2.7.4


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list