Skip to content

Commit 96be95c

Browse files
committed
Bugfix/MICRSE-4914: Fix incorrect memory freeing in build_pkcs11_uri()
- Use a const pointer to prevent modification of memory returned by getenv(). - Free env_result before reassigning it to prevent invalid memory operations and memory leaks. Issue originally reported in PR #11. Reported-by: @andersm Signed-off-by: Mario Castaneda <mario.ignacio.castaneda.lopez@nxp.com>
1 parent b880707 commit 96be95c

2 files changed

Lines changed: 4 additions & 9 deletions

File tree

inc/imx_signer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ typedef struct {
196196
static bool g_debug = 0;
197197
static char *g_cfgfilename = NULL;
198198
extern uint32_t g_image_offset;
199-
static char *g_sig_tool_path = NULL;
200-
static char *g_sig_data_path = NULL;
199+
static const char *g_sig_tool_path = NULL;
200+
static const char *g_sig_data_path = NULL;
201201

202202
unsigned char g_ivt_v1_mask[] = {0xFF,0xFF,0xFF,0xF0};
203203
unsigned char g_ivt_v1[] = {0xD1,0x00,0x20,0x41};

src/imx_signer.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ static char *build_pkcs11_uri(const char *rvalue) {
564564
char *pkcs11_uri = NULL; /* PKCS11 URI string buffer */
565565
char *env_result = NULL; /* Env result for Token*/
566566
char *config_object = NULL; /* Configuration object identifier */
567-
char *pkcs11_token_pin = NULL; /* Token or Pin values*/
567+
const char *pkcs11_token_pin = NULL; /* Token or Pin values*/
568568

569569
/* Allocate buffer for the complete PKCS11 URI */
570570
pkcs11_uri = calloc(PKCS11_URI_BUFFER_SIZE+1, sizeof(char));
@@ -604,7 +604,7 @@ static char *build_pkcs11_uri(const char *rvalue) {
604604
goto err;
605605

606606
pkcs11_token_pin = NULL;
607-
env_result = NULL;
607+
FREE(env_result);
608608

609609
/* Add type=cert */
610610
strncat(pkcs11_uri, ";type=cert", PKCS11_URI_BUFFER_SIZE - strlen(pkcs11_uri));
@@ -619,22 +619,17 @@ static char *build_pkcs11_uri(const char *rvalue) {
619619
strncat(pkcs11_uri, pkcs11_token_pin, PKCS11_URI_BUFFER_SIZE - strlen(pkcs11_uri));
620620
else
621621
strncat(pkcs11_uri, env_result, PKCS11_URI_BUFFER_SIZE - strlen(pkcs11_uri));
622-
FREE(env_result);
623622
} else
624623
goto err;
625624

626625
/* Close the URI string */
627626
strncat(pkcs11_uri, "\"", PKCS11_URI_BUFFER_SIZE - strlen(pkcs11_uri));
628627

629-
FREE(pkcs11_token_pin);
630628
FREE(config_object);
631629
FREE(env_result);
632-
FREE(pkcs11_uri);
633-
634630
return pkcs11_uri;
635631

636632
err:
637-
FREE(pkcs11_token_pin);
638633
FREE(config_object);
639634
FREE(env_result);
640635
FREE(pkcs11_uri);

0 commit comments

Comments
 (0)