diff mbox series

[2/2] Replace CONFIG_DISABLE_CPIO_CRC with dynamic check

Message ID 20210907152312.93144-2-sbabic@denx.de
State Accepted
Headers show
Series [1/2] Simplify footprint for get_cpiohdr | expand

Commit Message

Stefano Babic Sept. 7, 2021, 3:23 p.m. UTC
SWUpdate supports both new ascii and crc ascii (default) formats. If
checksum in CPIO should not be verified, just simply switch to new ascii
and the check is disabled automatically by SWUpdate.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 Kconfig                 | 15 ---------------
 core/cpio_utils.c       | 26 +++++++++++++++++++-------
 core/stream_interface.c |  6 +++---
 include/cpiohdr.h       |  8 ++++++++
 include/util.h          | 16 ----------------
 5 files changed, 30 insertions(+), 41 deletions(-)
diff mbox series

Patch

diff --git a/Kconfig b/Kconfig
index dc86957..cb86d55 100644
--- a/Kconfig
+++ b/Kconfig
@@ -423,21 +423,6 @@  config HASH_VERIFY
 comment "Hash checking needs an SSL implementation"
 	depends on !SSL_IMPL_OPENSSL && !SSL_IMPL_WOLFSSL && !SSL_IMPL_MBEDTLS
 
-config DISABLE_CPIO_CRC
-	bool "Disable cpio CRC verify if SHA 256 is enabled"
-	depends on HASH_VERIFY
-	default n
-	help
-	  Disable CRC check in cpio header if sha256 is enabled.
-	  CRC in CPIO is not a real crc, but it is simply the sum
-	  of all bytes belonging to a file as 32 bit value. It is
-	  very weak and does not add any further safety if sha256
-	  is activated. CPIO in Linux distros has also a bug and
-	  CRC field is set to 0 when a file is larger as 2GB.
-
-	  SWUpdate supports the newc cpio format if this is
-	  selected. That format does not have a checksum.
-
 config SIGNED_IMAGES
 	bool "Enable verification of signed images"
 	depends on SSL_IMPL_OPENSSL || SSL_IMPL_WOLFSSL || SSL_IMPL_MBEDTLS
diff --git a/core/cpio_utils.c b/core/cpio_utils.c
index d1e2d6d..2e4aca3 100644
--- a/core/cpio_utils.c
+++ b/core/cpio_utils.c
@@ -37,12 +37,13 @@  int get_cpiohdr(unsigned char *buf, struct filehdr *fhdr)
 		return -EINVAL;
 
 	cpiohdr = (struct new_ascii_header *)buf;
-#ifdef CONFIG_DISABLE_CPIO_CRC
-	if (strncmp(cpiohdr->c_magic, "070701", 6) != 0)
-#endif
-	if (strncmp(cpiohdr->c_magic, "070702", 6) != 0) {
+	if (!strncmp(cpiohdr->c_magic, "070701", 6))
+		fhdr->format = CPIO_NEWASCII;
+	else if (!strncmp(cpiohdr->c_magic, "070702", 6))
+		fhdr->format = CPIO_CRCASCII;
+	else {
 		ERROR("CPIO Format not recognized: magic not found");
-			return -EINVAL;
+		return -EINVAL;
 	}
 	fhdr->size = FROM_HEX(cpiohdr->c_filesize);
 	fhdr->namesize = FROM_HEX(cpiohdr->c_namesize);
@@ -736,7 +737,7 @@  off_t extract_next_file(int fd, int fdout, off_t start, int compressed,
 		(unsigned long)checksum,
 		(checksum == fdh.chksum) ? "VERIFIED" : "WRONG");
 
-	if (!swupdate_verify_chksum(checksum, fdh.chksum)) {
+	if (!swupdate_verify_chksum(checksum, &fdh)) {
 		return -EINVAL;
 	}
 
@@ -780,7 +781,7 @@  int cpio_scan(int fd, struct swupdate_cfg *cfg, off_t start)
 			return -1;
 		}
 
-		if (!swupdate_verify_chksum(fdh.chksum, checksum)) {
+		if (!swupdate_verify_chksum(checksum, &fdh)) {
 			return -1;
 		}
 
@@ -794,3 +795,14 @@  int cpio_scan(int fd, struct swupdate_cfg *cfg, off_t start)
 
 	return 0;
 }
+
+bool swupdate_verify_chksum(const uint32_t chk1, struct filehdr *fhdr) {
+	bool ret = (chk1 == fhdr->chksum);
+	if (fhdr->format == CPIO_NEWASCII)
+		return true;
+	if (!ret) {
+		ERROR("Checksum WRONG ! Computed 0x%ux, it should be 0x%ux",
+			chk1, (uint32_t)fhdr->chksum);
+	}
+	return ret;
+}
diff --git a/core/stream_interface.c b/core/stream_interface.c
index 32d1da0..19ce3ce 100644
--- a/core/stream_interface.c
+++ b/core/stream_interface.c
@@ -108,7 +108,7 @@  static int extract_file_to_tmp(int fd, const char *fname, unsigned long *poffs,
 		close(fdout);
 		return -1;
 	}
-	if (!swupdate_verify_chksum(checksum, fdh.chksum)) {
+	if (!swupdate_verify_chksum(checksum, &fdh)) {
 		close(fdout);
 		return -1;
 	}
@@ -230,7 +230,7 @@  static int extract_files(int fd, struct swupdate_cfg *software)
 					close(fdout);
 					return -1;
 				}
-				if (!swupdate_verify_chksum(checksum, fdh.chksum)) {
+				if (!swupdate_verify_chksum(checksum, &fdh)) {
 					close(fdout);
 					return -1;
 				}
@@ -241,7 +241,7 @@  static int extract_files(int fd, struct swupdate_cfg *software)
 				if (copyfile(fd, &fdout, fdh.size, &offset, 0, skip, 0, &checksum, NULL, false, NULL, NULL) < 0) {
 					return -1;
 				}
-				if (!swupdate_verify_chksum(checksum, fdh.chksum)) {
+				if (!swupdate_verify_chksum(checksum, &fdh)) {
 					return -1;
 				}
 				break;
diff --git a/include/cpiohdr.h b/include/cpiohdr.h
index 9d4bb92..d263776 100644
--- a/include/cpiohdr.h
+++ b/include/cpiohdr.h
@@ -15,7 +15,10 @@ 
 #define _CPIOHDR_SWUPD_H
 
 /* Global swupdate defines */
+#include <stdbool.h>
+#include <sys/types.h>
 #include "globals.h"
+#include <stdint.h>
 
 /*
  * cpio header - swupdate does not
@@ -24,6 +27,9 @@ 
  * documentation is supported.
  */
 
+#define CPIO_NEWASCII 070701
+#define CPIO_CRCASCII 070702
+
 struct new_ascii_header
 {
   char c_magic[6];
@@ -43,6 +49,7 @@  struct new_ascii_header
 };
 
 struct filehdr {
+	unsigned long format;
 	unsigned long size;
 	unsigned long namesize;
 	unsigned long chksum;
@@ -53,5 +60,6 @@  int get_cpiohdr(unsigned char *buf, struct filehdr *fhdr);
 int extract_cpio_header(int fd, struct filehdr *fhdr, unsigned long *offset);
 int extract_img_from_cpio(int fd, unsigned long offset, struct filehdr *fdh);
 void extract_padding(int fd, unsigned long *offset);
+bool swupdate_verify_chksum(const uint32_t chk1, struct filehdr *fhdr);
 
 #endif
diff --git a/include/util.h b/include/util.h
index 9f29f5f..3d1b2ff 100644
--- a/include/util.h
+++ b/include/util.h
@@ -134,22 +134,6 @@  void notifier_set_color(int level, char *col);
 
 #define LG_16 4
 #define FROM_HEX(f) from_ascii (f, sizeof f, LG_16)
-#if !defined(CONFIG_DISABLE_CPIO_CRC)
-static inline bool swupdate_verify_chksum(const uint32_t chk1, const uint32_t chk2) {
-	bool ret = (chk1 == chk2);
-	if (!ret) {
-		ERROR("Checksum WRONG ! Computed 0x%ux, it should be 0x%ux",
-			chk1, chk2);
-	}
-	return ret;
-}
-#else
-static inline bool swupdate_verify_chksum(
-		const uint32_t  __attribute__ ((__unused__))chk1,
-		const uint32_t  __attribute__ ((__unused__))chk2) {
-	return true;
-}
-#endif
 uintmax_t
 from_ascii (char const *where, size_t digs, unsigned logbase);
 int ascii_to_hash(unsigned char *hash, const char *s);