Skip to contentSkip to conversation

mkimage usability fixes

PATCH Superseded Joel Stanley 2 participants 9 messages Nov 11, 2020 – Dec 8, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover mkimage usability fixes

Hello,

I was learning about signed FIT today and was unable to convince mkimage to produce a signature node in my control device tree. It turns out that Debian's packaged mkimage doesn't enable FIT_SIGNATURE, so the options I was passing were silently ignored.

There's an existing Debian bug for that[1] but in the mean time this changes mkimage's behaviour to fail loudly when FIT_SIGNATURE is disabled.

The first two patches fix some issues I found when testing the third patch under sandbox_defconfig with FIT_SIGNATURE=n.

[1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=972513

Joel Stanley (3):
tools/Makefile: FIT_CIPHER requires libssl
image-cipher: Fix FIT_CIPHER linking
mkimge: Reject signing-related flags without FIT_SIGNATURE

 common/image-cipher.c | 14 ++++++++++++++
 tools/Makefile        |  2 +-
 tools/mkimage.c       |  9 +++++++--
 3 files changed, 22 insertions(+), 3 deletions(-)
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/3 tools/Makefile: FIT_CIPHER requires libssl

If CONFIG_FIT_CIPHER=y and CONFIG_FIT_SIGNATURE=n then mkimage and dumpimage will fail to link:

/usr/bin/ld: tools/common/image-cipher.o: in function `fit_image_decrypt_data':
image-cipher.c:(.text+0x9a): undefined reference to image_get_host_blob' /usr/bin/ld: tools/common/image-cipher.o:(.data.rel+0x10): undefined reference to EVP_aes_128_cbc' /usr/bin/ld: tools/common/image-cipher.o:(.data.rel+0x40): undefined reference to EVP_aes_192_cbc' /usr/bin/ld: tools/common/image-cipher.o:(.data.rel+0x70): undefined reference to EVP_aes_256_cbc' /usr/bin/ld: tools/lib/aes/aes-encrypt.o: in function `image_aes_encrypt':
aes-encrypt.c:(.text+0x22): undefined reference to EVP_CIPHER_CTX_new' /usr/bin/ld: aes-encrypt.c:(.text+0x6f): undefined reference to EVP_EncryptInit_ex' /usr/bin/ld: aes-encrypt.c:(.text+0x8d): undefined reference to EVP_EncryptUpdate' /usr/bin/ld: aes-encrypt.c:(.text+0xac): undefined reference to EVP_CIPHER_CTX_free' /usr/bin/ld: aes-encrypt.c:(.text+0xf2): undefined reference to `EVP_EncryptFinal_ex'
collect2: error: ld returned 1 exit status

---
 tools/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/Makefile +1 -1
index 51123fd92983..103b3ab8a7f2 100644
@@ -154,7 +154,7 @@ HOSTCFLAGS_kwbimage.o += -DCONFIG_KWB_SECURE
 endif
 
 # MXSImage needs LibSSL
-ifneq ($(CONFIG_MX23)$(CONFIG_MX28)$(CONFIG_ARMADA_38X)$(CONFIG_ARMADA_39X)$(CONFIG_FIT_SIGNATURE),)
+ifneq ($(CONFIG_MX23)$(CONFIG_MX28)$(CONFIG_ARMADA_38X)$(CONFIG_ARMADA_39X)$(CONFIG_FIT_SIGNATURE)$(CONFIG_FIT_CIPHER),)
 HOSTCFLAGS_kwbimage.o += \
 	$(shell pkg-config --cflags libssl libcrypto 2> /dev/null || echo "")
 HOSTLDLIBS_mkimage += \
Signature · 1 line
2.28.0
#2 lore raw

PATCH 1/3 tools/Makefile: FIT_CIPHER requires libssl

Hi Joel,
Sorry, for this very late answer.

Le 11/11/2020 ? 12:18, Joel Stanley a ?crit?:

Quoted 16 lines

If CONFIG_FIT_CIPHER=y and CONFIG_FIT_SIGNATURE=n then mkimage and dumpimage will fail to link:

/usr/bin/ld: tools/common/image-cipher.o: in function `fit_image_decrypt_data':
image-cipher.c:(.text+0x9a): undefined reference to image_get_host_blob' /usr/bin/ld: tools/common/image-cipher.o:(.data.rel+0x10): undefined reference to EVP_aes_128_cbc' /usr/bin/ld: tools/common/image-cipher.o:(.data.rel+0x40): undefined reference to EVP_aes_192_cbc' /usr/bin/ld: tools/common/image-cipher.o:(.data.rel+0x70): undefined reference to EVP_aes_256_cbc' /usr/bin/ld: tools/lib/aes/aes-encrypt.o: in function `image_aes_encrypt':
aes-encrypt.c:(.text+0x22): undefined reference to EVP_CIPHER_CTX_new' /usr/bin/ld: aes-encrypt.c:(.text+0x6f): undefined reference to EVP_EncryptInit_ex' /usr/bin/ld: aes-encrypt.c:(.text+0x8d): undefined reference to EVP_EncryptUpdate' /usr/bin/ld: aes-encrypt.c:(.text+0xac): undefined reference to EVP_CIPHER_CTX_free' /usr/bin/ld: aes-encrypt.c:(.text+0xf2): undefined reference to `EVP_EncryptFinal_ex'
collect2: error: ld returned 1 exit status

Quoted 1 line
Quoted 9 lines
---
  tools/Makefile | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/Makefile +0 -0
index 51123fd92983..103b3ab8a7f2 100644

tools/Makefile HOSTCFLAGS_kwbimage.o += -DCONFIG_KWB_SECURE · line 160

Quoted 8 lines
tools/Makefile +1 -1
@@ -154,7 +154,7 @@ HOSTCFLAGS_kwbimage.o += -DCONFIG_KWB_SECURE
  endif
 
  # MXSImage needs LibSSL
-ifneq ($(CONFIG_MX23)$(CONFIG_MX28)$(CONFIG_ARMADA_38X)$(CONFIG_ARMADA_39X)$(CONFIG_FIT_SIGNATURE),)
+ifneq ($(CONFIG_MX23)$(CONFIG_MX28)$(CONFIG_ARMADA_38X)$(CONFIG_ARMADA_39X)$(CONFIG_FIT_SIGNATURE)$(CONFIG_FIT_CIPHER),)
  HOSTCFLAGS_kwbimage.o += \
  	$(shell pkg-config --cflags libssl libcrypto 2> /dev/null || echo "")
  HOSTLDLIBS_mkimage += \

Regards,
Philippe

#3 lore raw

PATCH 2/3 image-cipher: Fix FIT_CIPHER linking

In reply to Joel Stanley

When CONFIG_FIT_CIPHER=y and CONFIG_FIT_SIGNATURE=n is there is no implementation of image_get_host_blob for mkimage or dumpimage:

/usr/bin/ld: tools/common/image-cipher.o: in function `fit_image_decrypt_data':
image-cipher.c:(.text+0x9a): undefined reference to `image_get_host_blob'

The implementation is the same as common/image-fit-sig.c.

---
 common/image-cipher.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
common/image-cipher.c +14 -0
index 4ca9eec4ef15..fcbbceb847a5 100644
@@ -15,6 +15,20 @@ DECLARE_GLOBAL_DATA_PTR;
 #include <uboot_aes.h>
 #include <u-boot/aes.h>
 
+#ifdef USE_HOSTCC
+void *host_blob;
+
+void image_set_host_blob(void *blob)
+{
+	host_blob = blob;
+}
+
+void *image_get_host_blob(void)
+{
+	return host_blob;
+}
+#endif
+
 struct cipher_algo cipher_algos[] = {
 	{
 		.name = "aes128",
Signature · 1 line
2.28.0
#4 lore raw

PATCH 2/3 image-cipher: Fix FIT_CIPHER linking

Hi Joel,

sorry for this very late answer ..

This patch fix this issue when only the ciphering is enabled. But it breaks the compilation when signature and ciphering are enabled, because both functions image_set_host_blob and image_get_host_blob are defined twice.
So it is a NAK for me.

A simple way to fix it to move this block of code from image-fit-sig.c to iamge-fit.c

Regards,
Philippe

Le 11/11/2020 ? 12:18, Joel Stanley a ?crit?:

Quoted 38 lines

When CONFIG_FIT_CIPHER=y and CONFIG_FIT_SIGNATURE=n is there is no implementation of image_get_host_blob for mkimage or dumpimage:

/usr/bin/ld: tools/common/image-cipher.o: in function `fit_image_decrypt_data':
image-cipher.c:(.text+0x9a): undefined reference to `image_get_host_blob'

The implementation is the same as common/image-fit-sig.c.

---
  common/image-cipher.c | 14 ++++++++++++++
  1 file changed, 14 insertions(+)
common/image-cipher.c +14 -0
index 4ca9eec4ef15..fcbbceb847a5 100644
@@ -15,6 +15,20 @@ DECLARE_GLOBAL_DATA_PTR;
  #include <uboot_aes.h>
  #include <u-boot/aes.h>
 
+#ifdef USE_HOSTCC
+void *host_blob;
+
+void image_set_host_blob(void *blob)
+{
+	host_blob = blob;
+}
+
+void *image_get_host_blob(void)
+{
+	return host_blob;
+}
+#endif
+
  struct cipher_algo cipher_algos[] = {
  	{
  		.name = "aes128",
#5 lore raw

PATCH 2/3 image-cipher: Fix FIT_CIPHER linking

Quoted 6 lines · Philippe REYNES

Hi Joel,

sorry for this very late answer ..

Quoted 8 lines

This patch fix this issue when only the ciphering is enabled. But it breaks the compilation when signature and ciphering are enabled, because both functions image_set_host_blob and image_get_host_blob are defined twice.
So it is a NAK for me.

A simple way to fix it to move this block of code from image-fit-sig.c to iamge-fit.c

Agreed, that is a better idea.

I'll send a v2.

Quoted 44 lines

Regards,
Philippe

Le 11/11/2020 ? 12:18, Joel Stanley a ?crit :

Quoted 38 lines

When CONFIG_FIT_CIPHER=y and CONFIG_FIT_SIGNATURE=n is there is no implementation of image_get_host_blob for mkimage or dumpimage:

/usr/bin/ld: tools/common/image-cipher.o: in function `fit_image_decrypt_data':
image-cipher.c:(.text+0x9a): undefined reference to `image_get_host_blob'

The implementation is the same as common/image-fit-sig.c.

---
  common/image-cipher.c | 14 ++++++++++++++
  1 file changed, 14 insertions(+)
common/image-cipher.c +14 -0
index 4ca9eec4ef15..fcbbceb847a5 100644
@@ -15,6 +15,20 @@ DECLARE_GLOBAL_DATA_PTR;
  #include <uboot_aes.h>
  #include <u-boot/aes.h>
 
+#ifdef USE_HOSTCC
+void *host_blob;
+
+void image_set_host_blob(void *blob)
+{
+     host_blob = blob;
+}
+
+void *image_get_host_blob(void)
+{
+     return host_blob;
+}
+#endif
+
  struct cipher_algo cipher_algos[] = {
      {
              .name = "aes128",
#6 lore raw

PATCH 3/3 mkimge: Reject signing-related flags without FIT_SIGNATURE

In reply to Joel Stanley

When CONFIG_FIT_SIGNATURE=n the signing options are not available. If a user is careful they will notice this when looking at the help output.

If they are not careful they will waste several hours wondering what they got wrong, as mkimage will silently ignore the signing related options.

Make it obvious that the commands don't work by removing them from the getopt opt_string.

The tool will now behave as follows:

 $ mkimage -f machine.its -k keys -K u-boot-pubkey.dtb -r image.fit
 mkimage: invalid option -- 'k'
 Error: Invalid option
---
 tools/mkimage.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/mkimage.c +7 -2
index e78608293e72..10a1b3dc8c18 100644
@@ -142,6 +142,12 @@ static int add_content(int type, const char *fname)
 	return 0;
 }
 
+#ifdef CONFIG_FIT_SIGNATURE
+#define OPT_STRING "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx"
+#else
+#define OPT_STRING "a:A:b:C:d:D:e:f:i:ln:O:R:qstT:vVx"
+#endif
+
 static void process_args(int argc, char **argv)
 {
 	char *ptr;
@@ -149,8 +155,7 @@ static void process_args(int argc, char **argv)
 	char *datafile = NULL;
 	int opt;
 
-	while ((opt = getopt(argc, argv,
-		   "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx")) != -1) {
+	while ((opt = getopt(argc, argv, OPT_STRING)) != -1) {
 		switch (opt) {
 		case 'a':
 			params.addr = strtoull(optarg, &ptr, 16);
Signature · 1 line
2.28.0
#7 lore raw

PATCH 3/3 mkimge: Reject signing-related flags without FIT_SIGNATURE

Hi Joel,

Sorry for this very late answer.

You're right, this is a bad behaviour, but I think that this solution remove too many options. For example, If signature is disabled, this solution also disable the padding in the fit image.

Looking a bit deeper, this patch removes all options that are not displayed by the help of mkimage when signature is disabled. But I think that this help is not correct. If the signature is disabled, the padding is still available.
So I think that we have an issue in the help too.

Regards,
Philippe

Le 11/11/2020 ? 12:18, Joel Stanley a ?crit?:

Quoted 48 lines

When CONFIG_FIT_SIGNATURE=n the signing options are not available. If a user is careful they will notice this when looking at the help output.

If they are not careful they will waste several hours wondering what they got wrong, as mkimage will silently ignore the signing related options.

Make it obvious that the commands don't work by removing them from the getopt opt_string.

The tool will now behave as follows:

  $ mkimage -f machine.its -k keys -K u-boot-pubkey.dtb -r image.fit
  mkimage: invalid option -- 'k'
  Error: Invalid option
---
  tools/mkimage.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
tools/mkimage.c +7 -2
index e78608293e72..10a1b3dc8c18 100644
@@ -142,6 +142,12 @@ static int add_content(int type, const char *fname)
  	return 0;
  }
 
+#ifdef CONFIG_FIT_SIGNATURE
+#define OPT_STRING "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx"
+#else
+#define OPT_STRING "a:A:b:C:d:D:e:f:i:ln:O:R:qstT:vVx"
+#endif
+
  static void process_args(int argc, char **argv)
  {
  	char *ptr;
@@ -149,8 +155,7 @@ static void process_args(int argc, char **argv)
  	char *datafile = NULL;
  	int opt;
 
-	while ((opt = getopt(argc, argv,
-		   "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx")) != -1) {
+	while ((opt = getopt(argc, argv, OPT_STRING)) != -1) {
  		switch (opt) {
  		case 'a':
  			params.addr = strtoull(optarg, &ptr, 16);
#8 lore raw

PATCH 3/3 mkimge: Reject signing-related flags without FIT_SIGNATURE

Quoted 8 lines · Philippe REYNES

Hi Joel,

Sorry for this very late answer.

You're right, this is a bad behaviour, but I think that this solution remove too many options. For example, If signature is disabled, this solution also disable the padding in the fit image.

Ok. I can amend my patch but it wasn't obvious which commands should be moved from outside the FIT_SIGNATURE guard.

Just -E and -B?

Quoted 59 lines

Looking a bit deeper, this patch removes all options that are not displayed by the help of mkimage when signature is disabled. But I think that this help is not correct. If the signature is disabled, the padding is still available.
So I think that we have an issue in the help too.

Regards,
Philippe

Le 11/11/2020 ? 12:18, Joel Stanley a ?crit :

Quoted 48 lines

When CONFIG_FIT_SIGNATURE=n the signing options are not available. If a user is careful they will notice this when looking at the help output.

If they are not careful they will waste several hours wondering what they got wrong, as mkimage will silently ignore the signing related options.

Make it obvious that the commands don't work by removing them from the getopt opt_string.

The tool will now behave as follows:

  $ mkimage -f machine.its -k keys -K u-boot-pubkey.dtb -r image.fit
  mkimage: invalid option -- 'k'
  Error: Invalid option
---
  tools/mkimage.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
tools/mkimage.c +7 -2
index e78608293e72..10a1b3dc8c18 100644
@@ -142,6 +142,12 @@ static int add_content(int type, const char *fname)
      return 0;
  }
 
+#ifdef CONFIG_FIT_SIGNATURE
+#define OPT_STRING "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx"
+#else
+#define OPT_STRING "a:A:b:C:d:D:e:f:i:ln:O:R:qstT:vVx"
+#endif
+
  static void process_args(int argc, char **argv)
  {
      char *ptr;
@@ -149,8 +155,7 @@ static void process_args(int argc, char **argv)
      char *datafile = NULL;
      int opt;
 
-     while ((opt = getopt(argc, argv,
-                "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx")) != -1) {
+     while ((opt = getopt(argc, argv, OPT_STRING)) != -1) {
              switch (opt) {
              case 'a':
                      params.addr = strtoull(optarg, &ptr, 16);