Skip to contentSkip to conversation

tools: mkimage: copy the FIT in process instead of shelling out to cp

PATCH In review Enrique Hernández Bello 3 participants 3 messages Oct 28 – Nov 5

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: mkimage: copy the FIT in process instead of shelling out to cp

Rebuilding an existing FIT (-F) starts by making a working copy of it, and mkimage asks a shell to do that:

	snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
		 params->imagefile, tmpfile);

so signing a FIT needs /bin/sh and cp on top of mkimage itself. That rules the tool out of the minimal images where firmware tends to be assembled; in one of them the failure reads

mkimage: system(cp "u-boot.itb" "u-boot.itb.tmp") failed:
No such file or directory

which names cp but means there is no shell to run it with: glibc's system() uses posix_spawn and returns -1 with ENOENT when /bin/sh is missing.

copyfile() does the same job in process, is declared in tools/fit_common.h and is already called further down this same function to restore the backup, so use it. Leaving cmd empty makes the existing "if (*cmd && ...)" guard skip system() altogether, the same way the auto-FIT path above already does.

No functional change intended: the temporary file ends up with the same contents.

---
 tools/fit_image.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/fit_image.c +4 -2
index b648e3e2..a3a05cb6 100644
@@ -1654,8 +1654,10 @@ static int fit_handle_file(struct image_tool_params *params)
 			 MKIMAGE_DTC, params->dtc, tmpfile, params->datafile);
 		debug("Trying to execute \"%s\"\n", cmd);
 	} else {
-		snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
-			 params->imagefile, tmpfile);
+		/* Copy the image in process: mkimage must not need a shell. */
+		if (copyfile(params->imagefile, tmpfile) < 0)
+			goto err_system;
+		*cmd = '\0';
 	}
 	if (strlen(cmd) >= MKIMAGE_MAX_DTC_CMDLINE_LEN - 1) {
 		fprintf(stderr, "WARNING: command-line for FIT creation might be truncated and will probably fail.\n");
Signature · 1 line
2.56.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · Enrique Hernández Bello

tools: mkimage: copy the FIT in process instead of shelling out to cp

Rebuilding an existing FIT (-F) starts by making a working copy of it, and mkimage asks a shell to do that:

        snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
                 params->imagefile, tmpfile);

so signing a FIT needs /bin/sh and cp on top of mkimage itself. That rules the tool out of the minimal images where firmware tends to be assembled; in one of them the failure reads

mkimage: system(cp "u-boot.itb" "u-boot.itb.tmp") failed:
No such file or directory

which names cp but means there is no shell to run it with: glibc's system() uses posix_spawn and returns -1 with ENOENT when /bin/sh is missing.

copyfile() does the same job in process, is declared in tools/fit_common.h and is already called further down this same function to restore the backup, so use it. Leaving cmd empty makes the existing "if (*cmd && ...)" guard skip system() altogether, the same way the auto-FIT path above already does.

Quoted 7 lines

No functional change intended: the temporary file ends up with the same contents.

 tools/fit_image.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
#2 lore raw
In reply to Enrique Hernández Bello
Quoted 11 lines · Enrique Hernández Bello

Rebuilding an existing FIT (-F) starts by making a working copy of it, and mkimage asks a shell to do that:

	snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
		 params->imagefile, tmpfile);

so signing a FIT needs /bin/sh and cp on top of mkimage itself. That rules the tool out of the minimal images where firmware tends to be assembled; in one of them the failure reads

[...]

Applied to u-boot/main, thanks!

[1/1] tools: mkimage: copy the FIT in process instead of shelling out to cp

      commit: 58eb0801bca518d6aa81a7b49b063aacc6e98bb2
Signature · 1 line
Tom