image-host: small improvements and fixes.

Yocto build can involve very long filenames. These two patches protect the Yocto build from failing without a sensible error message and increase the path length for the cipher key for the kernel from 128 to 256 characters.

--- tools/image-host.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-)
diff --git a/tools/image-host.c b/tools/image-host.c index a6b0a94420..0c92a2ddeb 100644 --- a/tools/image-host.c +++ b/tools/image-host.c @@ -363,6 +363,7 @@ static int fit_image_setup_cipher(struct image_cipher_info *info, char *algo_name; char filename[128]; int ret = -1; + int snprintf_return;
if (fit_image_cipher_get_algo(fit, noffset, &algo_name)) { printf("Can't get algo name for cipher in image '%s'\n", @@ -399,8 +400,20 @@ static int fit_image_setup_cipher(struct image_cipher_info *info, }
/* Read the key in the file */ - snprintf(filename, sizeof(filename), "%s/%s%s", + snprintf_return = snprintf(filename, sizeof(filename), "%s/%s%s", info->keydir, info->keyname, ".bin"); + if (snprintf_return >= sizeof(filename)) + { + printf("Can't format the key filename when setting up the cipher: insufficient buffer space\n"); + ret = -1; + goto out; + } + if (snprintf_return < 0) + { + printf("Can't format the key filename when setting up the cipher: snprintf error\n"); + ret = -1; + goto out; + } info->key = malloc(info->cipher->key_len); if (!info->key) { printf("Can't allocate memory for key\n"); @@ -423,6 +436,18 @@ static int fit_image_setup_cipher(struct image_cipher_info *info, /* Read the IV in the file */ snprintf(filename, sizeof(filename), "%s/%s%s", info->keydir, info->ivname, ".bin"); + if (snprintf_return >= sizeof(filename)) + { + printf("Can't format the IV filename when setting up the cipher: insufficient buffer space\n"); + ret = -1; + goto out; + } + if (snprintf_return < 0) + { + printf("Can't format the IV filename when setting up the cipher: snprintf error\n"); + ret = -1; + goto out; + } ret = fit_image_read_data(filename, (unsigned char *)info->iv, info->cipher->iv_len); } else {

Hi Hugo,
On Wed, 27 Sept 2023 at 06:24, Hugo Cornelis hugo.cornelis@essensium.com wrote:
Please add a commit message
tools/image-host.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-)
diff --git a/tools/image-host.c b/tools/image-host.c index a6b0a94420..0c92a2ddeb 100644 --- a/tools/image-host.c +++ b/tools/image-host.c @@ -363,6 +363,7 @@ static int fit_image_setup_cipher(struct image_cipher_info *info, char *algo_name; char filename[128]; int ret = -1;
int snprintf_return; if (fit_image_cipher_get_algo(fit, noffset, &algo_name)) { printf("Can't get algo name for cipher in image '%s'\n",
@@ -399,8 +400,20 @@ static int fit_image_setup_cipher(struct image_cipher_info *info, }
/* Read the key in the file */
snprintf(filename, sizeof(filename), "%s/%s%s",
snprintf_return = snprintf(filename, sizeof(filename), "%s/%s%s", info->keydir, info->keyname, ".bin");
if (snprintf_return >= sizeof(filename))
{
printf("Can't format the key filename when setting up the cipher: insufficient buffer space\n");
ret = -1;
goto out;
}
if (snprintf_return < 0)
{
printf("Can't format the key filename when setting up the cipher: snprintf error\n");
ret = -1;
goto out;
} info->key = malloc(info->cipher->key_len); if (!info->key) { printf("Can't allocate memory for key\n");
@@ -423,6 +436,18 @@ static int fit_image_setup_cipher(struct image_cipher_info *info, /* Read the IV in the file */ snprintf(filename, sizeof(filename), "%s/%s%s", info->keydir, info->ivname, ".bin");
if (snprintf_return >= sizeof(filename))
{
printf("Can't format the IV filename when setting up the cipher: insufficient buffer space\n");
ret = -1;
goto out;
}
if (snprintf_return < 0)
Please check code style...the { should go at the end of the 'if' line. You can run patman to do it for you.
{
printf("Can't format the IV filename when setting up the cipher: snprintf error\n");
ret = -1;
goto out;
} ret = fit_image_read_data(filename, (unsigned char *)info->iv, info->cipher->iv_len); } else {
-- 2.34.1
REgards, Simon

--- tools/image-host.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/image-host.c b/tools/image-host.c index 0c92a2ddeb..9afcc02192 100644 --- a/tools/image-host.c +++ b/tools/image-host.c @@ -361,7 +361,7 @@ static int fit_image_setup_cipher(struct image_cipher_info *info, int noffset) { char *algo_name; - char filename[128]; + char filename[256]; int ret = -1; int snprintf_return;

On Wed, 27 Sept 2023 at 06:24, Hugo Cornelis hugo.cornelis@essensium.com wrote:
tools/image-host.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Reviewed-by: Simon Glass sjg@chromium.org
but please add commit message (always)
diff --git a/tools/image-host.c b/tools/image-host.c index 0c92a2ddeb..9afcc02192 100644 --- a/tools/image-host.c +++ b/tools/image-host.c @@ -361,7 +361,7 @@ static int fit_image_setup_cipher(struct image_cipher_info *info, int noffset) { char *algo_name;
char filename[128];
char filename[256]; int ret = -1; int snprintf_return;
-- 2.34.1

On 27/09/2023 12:59, Hugo Cornelis wrote:
Yocto build can involve very long filenames. These two patches protect the Yocto build from failing without a sensible error message and increase the path length for the cipher key for the kernel from 128 to 256 characters.
For both patches: see the documentation on sending patches [1], particularly the section on commit message conventions. Your patches are missing 'Signed-off-by' lines at least.
[1]: https://u-boot.readthedocs.io/en/latest/develop/sending_patches.html
Thanks, Paul
participants (3)
-
Hugo Cornelis
-
Paul Barker
-
Simon Glass