diff mbox series

Check if sw-description is really extracted when installed from file

Message ID 1532593284-10004-1-git-send-email-sbabic@denx.de
State Accepted
Headers show
Series Check if sw-description is really extracted when installed from file | expand

Commit Message

Stefano Babic July 26, 2018, 8:21 a.m. UTC
The -c (check) option verifies sw-description and signature. However,
it does not check if the files are correctly extracted before verifying.
If files were already extracted by a previous run, the result can still be
successful even if the SWU is completely wrong.

This does not happen in case of network install because files are cleaned up
after run.

Check that the extraction of sw-description and sw-description.sig is successful
before parsing them.

Signed-off-by: Stefano Babic <sbabic@denx.de>
Reported-by: Karim Lazhard <karim.lazhard@gmail.com>
---
 core/cpio_utils.c  |  8 +++++---
 core/swupdate.c    | 14 +++++++++++---
 include/swupdate.h |  1 -
 include/util.h     |  2 +-
 4 files changed, 17 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/core/cpio_utils.c b/core/cpio_utils.c
index 4930277..bd713af 100644
--- a/core/cpio_utils.c
+++ b/core/cpio_utils.c
@@ -509,10 +509,10 @@  int extract_cpio_header(int fd, struct filehdr *fhdr, unsigned long *offset)
 	return 0;
 }
 
-off_t extract_sw_description(int fd, const char *descfile, off_t start)
+int extract_sw_description(int fd, const char *descfile, off_t *offs)
 {
 	struct filehdr fdh;
-	unsigned long offset = start;
+	unsigned long offset = *offs;
 	char output_file[64];
 	uint32_t checksum;
 	int fdout;
@@ -562,7 +562,9 @@  off_t extract_sw_description(int fd, const char *descfile, off_t start)
 		return -1;
 	}
 
-	return offset;
+	*offs = offset;
+
+	return 0;
 }
 
 int extract_img_from_cpio(int fd, unsigned long offset, struct filehdr *fdh)
diff --git a/core/swupdate.c b/core/swupdate.c
index ec944d0..3f872e4 100644
--- a/core/swupdate.c
+++ b/core/swupdate.c
@@ -299,11 +299,19 @@  static int install_from_file(char *fname, int check)
 		}
 	}
 
-	pos = extract_sw_description(fdsw, SW_DESCRIPTION_FILENAME, 0);
+	pos = 0;
+	ret = extract_sw_description(fdsw, SW_DESCRIPTION_FILENAME, &pos);
 #ifdef CONFIG_SIGNED_IMAGES
-	pos = extract_sw_description(fdsw, SW_DESCRIPTION_FILENAME ".sig",
-		pos);
+	ret = extract_sw_description(fdsw, SW_DESCRIPTION_FILENAME ".sig",
+		&pos);
 #endif
+	/*
+	 * Check if files could be extracted
+	 */
+	if (ret) {
+		ERROR("Failed to extract meta information");
+		exit(1);
+	}
 
 	char* swdescfilename = alloca(strlen(get_tmpdir())+strlen(SW_DESCRIPTION_FILENAME)+1);
 	sprintf(swdescfilename, "%s%s", get_tmpdir(), SW_DESCRIPTION_FILENAME);
diff --git a/include/swupdate.h b/include/swupdate.h
index 455e4ad..741d24c 100644
--- a/include/swupdate.h
+++ b/include/swupdate.h
@@ -150,7 +150,6 @@  struct swupdate_cfg {
 	} \
 } while(0)
 
-off_t extract_sw_description(int fd, const char *descfile, off_t start);
 int cpio_scan(int fd, struct swupdate_cfg *cfg, off_t start);
 struct swupdate_cfg *get_swupdate_cfg(void);
 void free_image(struct img_type *img);
diff --git a/include/util.h b/include/util.h
index 48dc538..c64e462 100644
--- a/include/util.h
+++ b/include/util.h
@@ -139,7 +139,7 @@  int copyfile(int fdin, void *out, unsigned int nbytes, unsigned long *offs,
 	int skip_file, int compressed, uint32_t *checksum,
 	unsigned char *hash, int encrypted, writeimage callback);
 int copyimage(void *out, struct img_type *img, writeimage callback);
-off_t extract_sw_description(int fd, const char *descfile, off_t start);
+int extract_sw_description(int fd, const char *descfile, off_t *offs);
 off_t extract_next_file(int fd, int fdout, off_t start, int compressed,
 			int encrypted, unsigned char *hash);
 int openfileoutput(const char *filename);