Skip to content

Commit 17df962

Browse files
authored
Merge pull request #732 from danielinux/fixes-20260322
Fixes from static analysis
2 parents c52a045 + 9b67b92 commit 17df962

File tree

14 files changed

+342
-23
lines changed

14 files changed

+342
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ tools/unit-tests/unit-image-sha384
170170
tools/unit-tests/unit-store-sbrk
171171
tools/unit-tests/unit-tpm-blob
172172
tools/unit-tests/unit-update-disk
173+
tools/unit-tests/unit-policy-sign
173174

174175

175176

src/boot_arm32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void RAMFUNCTION do_boot(const uint32_t *app_offset)
8686
#ifdef RAM_CODE
8787

8888
#define AIRCR *(volatile uint32_t *)(0xE000ED0C)
89-
#define AIRCR_VKEY (0r05FA << 16)
89+
#define AIRCR_VKEY (0x05FA << 16)
9090
#define AIRCR_SYSRESETREQ (1 << 2)
9191

9292
void RAMFUNCTION arch_reboot(void)

src/pkcs11_store.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,7 @@ int wolfPKCS11_Store_Open(int type, CK_ULONG id1, CK_ULONG id2, int read,
436436
void wolfPKCS11_Store_Close(void* store)
437437
{
438438
struct store_handle *handle = store;
439-
/* This removes all flags (including STORE_FLAGS_OPEN) */
440-
handle->flags = 0;
441-
handle->hdr = NULL;
439+
memset(handle, 0, sizeof(*handle));
442440
}
443441

444442
int wolfPKCS11_Store_Read(void* store, unsigned char* buffer, int len)

src/psa_store.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,7 @@ int wolfPSA_Store_OpenSz(int type, unsigned long id1, unsigned long id2, int rea
442442
void wolfPSA_Store_Close(void* store)
443443
{
444444
struct store_handle *handle = store;
445-
/* This removes all flags (including STORE_FLAGS_OPEN) */
446-
handle->flags = 0;
447-
handle->hdr = NULL;
445+
memset(handle, 0, sizeof(*handle));
448446
}
449447

450448
int wolfPSA_Store_Read(void* store, unsigned char* buffer, int len)

src/update_disk.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ static void disk_crypto_clear(void)
221221
ForceZero(disk_encrypt_nonce, sizeof(disk_encrypt_nonce));
222222
}
223223

224+
static void disk_decrypted_header_clear(uint8_t *hdr)
225+
{
226+
ForceZero(hdr, IMAGE_HEADER_SIZE);
227+
}
228+
224229
#endif /* DISK_ENCRYPT */
225230

226231
extern int wolfBoot_get_dts_size(void *dts_addr);
@@ -267,12 +272,14 @@ void RAMFUNCTION wolfBoot_start(void)
267272
#ifdef DISK_ENCRYPT
268273
/* Initialize encryption - this sets up the cipher with key from storage */
269274
if (wolfBoot_initialize_encryption() != 0) {
275+
disk_decrypted_header_clear(dec_hdr);
270276
disk_crypto_clear();
271277
wolfBoot_printf("Error initializing encryption\r\n");
272278
wolfBoot_panic();
273279
}
274280
/* Retrieve encryption key and nonce for disk decryption */
275281
if (wolfBoot_get_encrypt_key(disk_encrypt_key, disk_encrypt_nonce) != 0) {
282+
disk_decrypted_header_clear(dec_hdr);
276283
disk_crypto_clear();
277284
wolfBoot_printf("Error getting encryption key\r\n");
278285
wolfBoot_panic();
@@ -283,13 +290,15 @@ void RAMFUNCTION wolfBoot_start(void)
283290
ret = disk_init(BOOT_DISK);
284291
if (ret != 0) {
285292
#ifdef DISK_ENCRYPT
293+
disk_decrypted_header_clear(dec_hdr);
286294
disk_crypto_clear();
287295
#endif
288296
wolfBoot_panic();
289297
}
290298

291299
if (disk_open(BOOT_DISK) < 0) {
292300
#ifdef DISK_ENCRYPT
301+
disk_decrypted_header_clear(dec_hdr);
293302
disk_crypto_clear();
294303
#endif
295304
wolfBoot_printf("Error opening disk %d\r\n", BOOT_DISK);
@@ -328,6 +337,7 @@ void RAMFUNCTION wolfBoot_start(void)
328337

329338
if ((pB_ver == 0) && (pA_ver == 0)) {
330339
#ifdef DISK_ENCRYPT
340+
disk_decrypted_header_clear(dec_hdr);
331341
disk_crypto_clear();
332342
#endif
333343
wolfBoot_printf("No valid OS image found in either partition %d or %d\r\n",
@@ -433,6 +443,7 @@ void RAMFUNCTION wolfBoot_start(void)
433443
wolfBoot_printf("Decrypting image...");
434444
BENCHMARK_START();
435445
if ((IMAGE_HEADER_SIZE % ENCRYPT_BLOCK_SIZE) != 0) {
446+
disk_decrypted_header_clear(dec_hdr);
436447
disk_crypto_clear();
437448
wolfBoot_printf("Encrypted disk images require aligned header size\r\n");
438449
wolfBoot_panic();
@@ -482,6 +493,7 @@ void RAMFUNCTION wolfBoot_start(void)
482493

483494
if (failures) {
484495
#ifdef DISK_ENCRYPT
496+
disk_decrypted_header_clear(dec_hdr);
485497
disk_crypto_clear();
486498
#endif
487499
wolfBoot_printf("Unable to find a valid partition!\r\n");
@@ -542,6 +554,7 @@ void RAMFUNCTION wolfBoot_start(void)
542554
wolfBoot_hook_boot(&os_image);
543555
#endif
544556
#ifdef DISK_ENCRYPT
557+
disk_decrypted_header_clear(dec_hdr);
545558
disk_crypto_clear();
546559
#endif
547560
do_boot((uint32_t*)load_address

src/update_flash.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,6 @@ int wolfBoot_unlock_disk(void)
12411241
ret = wolfBoot_get_random(secret, secretSz);
12421242
if (ret == 0) {
12431243
wolfBoot_printf("Creating new secret (%d bytes)\n", secretSz);
1244-
wolfBoot_print_hexstr(secret, secretSz, 0);
12451244

12461245
/* seal new secret */
12471246
ret = wolfBoot_seal(pubkey_hint, policy, policySz, nvIndex,
@@ -1265,15 +1264,13 @@ int wolfBoot_unlock_disk(void)
12651264
}
12661265

12671266
wolfBoot_printf("Secret Check %d bytes\n", secretCheckSz);
1268-
wolfBoot_print_hexstr(secretCheck, secretCheckSz, 0);
12691267
TPM2_ForceZero(secretCheck, sizeof(secretCheck));
12701268
}
12711269
}
12721270
}
12731271

12741272
if (ret == 0) {
12751273
wolfBoot_printf("Secret %d bytes\n", secretSz);
1276-
wolfBoot_print_hexstr(secret, secretSz, 0);
12771274

12781275
/* TODO: Unlock disk */
12791276

src/x86/ahci.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ static int sata_create_and_seal_unlock_secret(const uint8_t *pubkey_hint,
281281
ret = sata_get_random_base64(secret, secret_size);
282282
if (ret == 0) {
283283
wolfBoot_printf("Creating new secret (%d bytes)\r\n", *secret_size);
284-
wolfBoot_printf("%s\r\n", secret);
285284

286285
/* seal new secret */
287286
ret = wolfBoot_seal(pubkey_hint, policy, policy_size,
@@ -305,14 +304,11 @@ static int sata_create_and_seal_unlock_secret(const uint8_t *pubkey_hint,
305304
}
306305

307306
wolfBoot_printf("Secret Check %d bytes\n", secret_check_sz);
308-
wolfBoot_printf("%s\r\n", secret_check);
309307
TPM2_ForceZero(secret_check, sizeof(secret_check));
310308
}
311309

312-
if (ret == 0) {
310+
if (ret == 0)
313311
wolfBoot_printf("Secret %d bytes\n", *secret_size);
314-
wolfBoot_printf("%s\r\n", secret);
315-
}
316312

317313
return ret;
318314
}
@@ -414,9 +410,6 @@ int sata_unlock_disk(int drv, int freeze)
414410
r = sata_get_unlock_secret(secret, &secret_size);
415411
if (r != 0)
416412
return r;
417-
#ifdef TARGET_x86_fsp_qemu
418-
wolfBoot_printf("DISK LOCK SECRET: %s\r\n", secret);
419-
#endif
420413
ata_st = ata_security_get_state(drv);
421414
wolfBoot_printf("ATA: Security state SEC%d\r\n", ata_st);
422415
#if defined(TARGET_x86_fsp_qemu)

tools/elf-parser/elf-parser.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ int main(int argc, char *argv[])
6767
ret = -1;
6868
}
6969
}
70-
fclose(f);
70+
if (f != NULL) {
71+
fclose(f);
72+
}
7173

7274
if (ret == 0) {
7375
ret = elf_load_image_mmu(image, (uint32_t)imageSz, &entry, NULL);

tools/fdt-parser/fdt-parser.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,18 @@ static int fdt_test(void* fdt)
192192
off = fdt_node_offset_by_compatible(fdt, -1, "fsl,qman-portal");
193193
while (off != -FDT_ERR_NOTFOUND) {
194194
const int *ci = fdt_getprop(fdt, off, "cell-index", NULL);
195+
uint32_t portal_idx;
195196
uint32_t liodns[2];
196197
if (!ci)
197198
break;
198-
i = fdt32_to_cpu(*ci);
199+
portal_idx = fdt32_to_cpu(*ci);
200+
if (portal_idx >= QMAN_NUM_PORTALS) {
201+
printf("FDT: Invalid qman-portal cell-index %u at %d\n",
202+
portal_idx, off);
203+
ret = -FDT_ERR_BADSTRUCTURE;
204+
goto exit;
205+
}
206+
i = (int)portal_idx;
199207

200208
liodns[0] = qp_info[i].dliodn;
201209
liodns[1] = qp_info[i].fliodn;

tools/tpm/policy_sign.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ int policy_sign(int argc, char *argv[])
287287
pcrDigestSz = -1;
288288
else
289289
pcrDigestSz = hexToByte(hashHexStr, pcrDigest, hashHexStrlen);
290-
if (pcrDigestSz <= 0) {
290+
if ((int)pcrDigestSz <= 0) {
291291
fprintf(stderr, "Invalid PCR hash length\n");
292292
usage();
293293
return -1;
@@ -300,7 +300,7 @@ int policy_sign(int argc, char *argv[])
300300
digestSz = -1;
301301
else
302302
digestSz = hexToByte(hashHexStr, digest, hashHexStrlen);
303-
if (digestSz <= 0) {
303+
if ((int)digestSz <= 0) {
304304
fprintf(stderr, "Invalid Policy Digest hash length\n");
305305
usage();
306306
return -1;

0 commit comments

Comments
 (0)