From 31d3ba907abbab8b3f8be26efc1f343308201c7e Mon Sep 17 00:00:00 2001 From: Adrian Bonislawski Date: Wed, 10 Jul 2024 09:34:37 +0200 Subject: [PATCH] rimage: fix file operations error checks This will fix file operations Signed-off-by: Adrian Bonislawski --- src/ext_manifest.c | 3 ++- src/file_simple.c | 11 +++++++++-- src/manifest.c | 17 ++++++++++------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/ext_manifest.c b/src/ext_manifest.c index 9624156..99b77f2 100644 --- a/src/ext_manifest.c +++ b/src/ext_manifest.c @@ -25,7 +25,8 @@ const struct ext_man_header ext_man_template = { static int ext_man_open_file(struct image *image) { /* open extended manifest outfile for writing */ - sprintf(image->out_ext_man_file, "%s.xman", image->out_file); + snprintf(image->out_ext_man_file, sizeof(image->out_ext_man_file), + "%s.xman", image->out_file); unlink(image->out_ext_man_file); image->out_ext_man_fd = fopen(image->out_ext_man_file, "wb"); diff --git a/src/file_simple.c b/src/file_simple.c index 3c34496..6144e14 100644 --- a/src/file_simple.c +++ b/src/file_simple.c @@ -128,6 +128,8 @@ static int simple_write_module(struct image *image, struct module *module) /* Get the pointer of writing hdr */ ptr_hdr = ftell(image->out_fd); + if (ptr_hdr < 0) + return file_error("cant get file position", image->out_file); count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd); if (count != 1) { fprintf(stderr, "error: failed to write section header %d\n", @@ -180,7 +182,9 @@ static int simple_write_module(struct image *image, struct module *module) -errno); return -errno; } - fseek(image->out_fd, ptr_cur, SEEK_SET); + err = fseek(image->out_fd, ptr_cur, SEEK_SET); + if (err) + return file_error("cant seek", image->out_file); fprintf(stdout, "\n"); /* return padding size */ @@ -324,7 +328,10 @@ int simple_write_firmware(struct image *image) hdr.file_size += ret; } /* overwrite hdr */ - fseek(image->out_fd, 0, SEEK_SET); + ret = fseek(image->out_fd, 0, SEEK_SET); + if (ret) + return file_error("can't seek set", image->out_file); + count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd); if (count != 1) return -errno; diff --git a/src/manifest.c b/src/manifest.c index d3d7f26..baa452e 100644 --- a/src/manifest.c +++ b/src/manifest.c @@ -27,7 +27,8 @@ static int man_open_rom_file(struct image *image) { uint32_t size; - sprintf(image->out_rom_file, "%s.rom", image->out_file); + snprintf(image->out_rom_file, sizeof(image->out_rom_file), + "%s.rom", image->out_file); unlink(image->out_rom_file); size = image->adsp->mem_zones[SOF_FW_BLK_TYPE_ROM].size; @@ -67,7 +68,8 @@ static int man_open_unsigned_file(struct image *image) static int man_open_manifest_file(struct image *image) { /* open manifest outfile for writing */ - sprintf(image->out_man_file, "%s.met", image->out_file); + snprintf(image->out_man_file, sizeof(image->out_man_file), + "%s.met", image->out_file); unlink(image->out_man_file); image->out_man_fd = fopen(image->out_man_file, "wb"); @@ -1361,10 +1363,10 @@ int man_write_fw_v2_5(struct image *image) int verify_image(struct image *image) { FILE *in_file; - int ret, i; + int ret = 0; long size; void *buffer; - size_t read; + size_t read, i; /* is verify supported for target ? */ if (!image->adsp->verify_firmware) { @@ -1432,7 +1434,7 @@ int verify_image(struct image *image) image->verify_file); out: fclose(in_file); - return 0; + return ret; } @@ -1440,7 +1442,8 @@ int resign_image(struct image *image) { int key_size, key_file_size; void *buffer = NULL; - size_t size, read; + size_t read; + int32_t size; FILE *in_file; int ret, i; @@ -1487,7 +1490,7 @@ int resign_image(struct image *image) /* read file into buffer */ read = fread(buffer, 1, size, in_file); if (read != size) { - fprintf(stderr, "error: unable to read %zu bytes from %s err %d\n", + fprintf(stderr, "error: unable to read %d bytes from %s err %d\n", size, image->in_file, errno); ret = errno; goto out;