Skip to content

Commit e4b12e4

Browse files
committed
Remove/ForceZeor secrets from stack after use
F/445
1 parent b9710fd commit e4b12e4

1 file changed

Lines changed: 31 additions & 9 deletions

File tree

src/update_flash.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ void RAMFUNCTION wolfBoot_check_self_update(void)
224224
static int RAMFUNCTION wolfBoot_copy_sector(struct wolfBoot_image *src,
225225
struct wolfBoot_image *dst, uint32_t sector)
226226
{
227+
int ret = 0;
227228
uint32_t pos = 0;
228229
uint32_t src_sector_offset = (sector * WOLFBOOT_SECTOR_SIZE);
229230
uint32_t dst_sector_offset = src_sector_offset;
@@ -245,8 +246,10 @@ static int RAMFUNCTION wolfBoot_copy_sector(struct wolfBoot_image *src,
245246
dst_sector_offset = 0;
246247

247248
#ifdef EXT_ENCRYPTED
248-
if (wolfBoot_initialize_encryption() < 0)
249-
return -1;
249+
if (wolfBoot_initialize_encryption() < 0) {
250+
ret = -1;
251+
goto out;
252+
}
250253

251254
wolfBoot_get_encrypt_key(key, nonce);
252255
if (src->part == PART_SWAP)
@@ -286,7 +289,8 @@ static int RAMFUNCTION wolfBoot_copy_sector(struct wolfBoot_image *src,
286289
}
287290
pos += FLASHBUFFER_SIZE;
288291
}
289-
return pos;
292+
ret = pos;
293+
goto out;
290294
}
291295
#endif
292296
wb_flash_erase(dst, dst_sector_offset, WOLFBOOT_SECTOR_SIZE);
@@ -298,12 +302,19 @@ static int RAMFUNCTION wolfBoot_copy_sector(struct wolfBoot_image *src,
298302
}
299303
pos += FLASHBUFFER_SIZE;
300304
}
301-
return pos;
305+
ret = pos;
306+
out:
307+
#ifdef EXT_ENCRYPTED
308+
ForceZero(key, sizeof(key));
309+
ForceZero(nonce, sizeof(nonce));
310+
#endif
311+
return ret;
302312
}
303313

304314
#ifdef EXT_ENCRYPTED
305315
static int RAMFUNCTION wolfBoot_backup_last_boot_sector(uint32_t sector)
306316
{
317+
int ret = 0;
307318
uint32_t pos = 0;
308319
uint32_t src_sector_offset = (sector * WOLFBOOT_SECTOR_SIZE);
309320
uint32_t dst_sector_offset = 0;
@@ -325,8 +336,10 @@ static int RAMFUNCTION wolfBoot_backup_last_boot_sector(uint32_t sector)
325336

326337
iv_counter = src_sector_offset;
327338
iv_counter /= ENCRYPT_BLOCK_SIZE;
328-
if (wolfBoot_initialize_encryption() < 0)
329-
return -1;
339+
if (wolfBoot_initialize_encryption() < 0) {
340+
ret = -1;
341+
goto out;
342+
}
330343
/*
331344
* Preserve the IV sequence used by the source sector so that the staging
332345
* copy in SWAP can be decrypted with exactly the same keystream when it is
@@ -345,9 +358,14 @@ static int RAMFUNCTION wolfBoot_backup_last_boot_sector(uint32_t sector)
345358
wb_flash_write(dst, dst_sector_offset + pos, encrypted_block, ENCRYPT_BLOCK_SIZE);
346359
pos += ENCRYPT_BLOCK_SIZE;
347360
}
348-
return 0;
349-
} else
350-
return wolfBoot_copy_sector(src, dst, sector);
361+
ret = 0;
362+
} else {
363+
ret = wolfBoot_copy_sector(src, dst, sector);
364+
}
365+
out:
366+
ForceZero(key, sizeof(key));
367+
ForceZero(nonce, sizeof(nonce));
368+
return ret;
351369
}
352370
#else
353371
#define wolfBoot_backup_last_boot_sector(sec) wolfBoot_copy_sector(boot, swap, sec)
@@ -701,6 +719,10 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot,
701719
sector++;
702720
}
703721
out:
722+
#ifdef EXT_ENCRYPTED
723+
ForceZero(key, sizeof(key));
724+
ForceZero(nonce, sizeof(nonce));
725+
#endif
704726
#ifdef EXT_FLASH
705727
ext_flash_lock();
706728
#endif

0 commit comments

Comments
 (0)