Skip to content

Commit 87a4ffc

Browse files
committed
Addressed reviewers comments
1 parent 8b39675 commit 87a4ffc

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

src/image.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,9 +1308,9 @@ int wolfBoot_open_image_address(struct wolfBoot_image *img, uint8_t *image)
13081308

13091309
#ifdef WOLFBOOT_FIXED_PARTITIONS
13101310
if (img->fw_size > (WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE)) {
1311-
wolfBoot_printf("Image size %d > max %d\n",
1311+
wolfBoot_printf("Image size %u > max %u\n",
13121312
(unsigned int)img->fw_size,
1313-
(WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE));
1313+
(unsigned int)(WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE));
13141314
img->fw_size = WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE;
13151315
return -1;
13161316
}
@@ -1321,7 +1321,7 @@ int wolfBoot_open_image_address(struct wolfBoot_image *img, uint8_t *image)
13211321
#else
13221322
#ifdef WOLFBOOT_RAMBOOT_MAX_SIZE
13231323
if (img->fw_size > WOLFBOOT_RAMBOOT_MAX_SIZE) {
1324-
wolfBoot_printf("Image size %d > max %d\n",
1324+
wolfBoot_printf("Image size %u > max %u\n",
13251325
(unsigned int)img->fw_size,
13261326
(unsigned int)WOLFBOOT_RAMBOOT_MAX_SIZE);
13271327
return -1;

src/update_disk.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ static uint32_t get_decrypted_blob_version(uint8_t *hdr)
121121
return 0;
122122

123123
/* Search for version TLV */
124-
while (p + 4 < max_p) {
124+
while ((size_t)(max_p - p) >= 4U) {
125+
size_t remaining = (size_t)(max_p - p);
126+
size_t tlv_total;
127+
125128
tlv_type = *((uint16_t*)p);
126129
tlv_len = *((uint16_t*)(p + 2));
127130

@@ -134,15 +137,16 @@ static uint32_t get_decrypted_blob_version(uint8_t *hdr)
134137
continue;
135138
}
136139

137-
if (p + 4 + tlv_len > max_p)
140+
tlv_total = 4U + (size_t)tlv_len;
141+
if (remaining < tlv_total)
138142
break;
139143

140144
if (tlv_type == HDR_VERSION && tlv_len == 4) {
141145
uint32_t ver = *((uint32_t*)(p + 4));
142146
return ver;
143147
}
144148

145-
p += 4 + tlv_len;
149+
p += tlv_total;
146150
}
147151
return 0;
148152
}

tools/unit-tests/unit-tpm-blob.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static int oversized_pub_read_attempted;
3939
static int oversized_priv_read_attempted;
4040
static int forcezero_calls;
4141
static word32 last_forcezero_len;
42+
static word32 last_pub_read_request_sz;
4243
static uint8_t test_hdr[64];
4344
static uint8_t test_modulus[256];
4445
static uint8_t test_exponent_der[] = { 0xAA, 0x01, 0x00, 0x01, 0x7B };
@@ -369,22 +370,31 @@ int wolfTPM2_NVReadAuth(WOLFTPM2_DEV* dev, WOLFTPM2_NV* nv,
369370
switch (nvread_calls) {
370371
case 1:
371372
if (current_mode == MOCK_OVERSIZE_PUB) {
372-
*(uint16_t*)dataBuf = (uint16_t)(sizeof(TPM2B_PUBLIC) + 1);
373+
uint16_t value = (uint16_t)(sizeof(TPM2B_PUBLIC) + 1);
374+
memcpy(dataBuf, &value, sizeof(value));
373375
}
374376
else {
375-
*(uint16_t*)dataBuf = 4;
377+
uint16_t value = 4;
378+
memcpy(dataBuf, &value, sizeof(value));
376379
}
377380
*pDataSz = sizeof(uint16_t);
378381
return 0;
379382
case 2:
380383
if (current_mode == MOCK_OVERSIZE_PUB) {
381-
oversized_pub_read_attempted = 1;
384+
last_pub_read_request_sz = *pDataSz;
385+
if (*pDataSz > sizeof(TPM2B_PUBLIC)) {
386+
oversized_pub_read_attempted = 1;
387+
}
382388
return -100;
383389
}
384390
memset(dataBuf, 0, *pDataSz);
385391
return 0;
386392
case 3:
387-
*(uint16_t*)dataBuf = (uint16_t)(sizeof(((WOLFTPM2_KEYBLOB*)0)->priv.buffer) + 1);
393+
{
394+
uint16_t value =
395+
(uint16_t)(sizeof(((WOLFTPM2_KEYBLOB*)0)->priv.buffer) + 1);
396+
memcpy(dataBuf, &value, sizeof(value));
397+
}
388398
*pDataSz = sizeof(uint16_t);
389399
return 0;
390400
case 4:
@@ -410,6 +420,7 @@ static void setup(void)
410420
oversized_priv_read_attempted = 0;
411421
forcezero_calls = 0;
412422
last_forcezero_len = 0;
423+
last_pub_read_request_sz = 0;
413424
memset(test_hdr, 0x22, sizeof(test_hdr));
414425
memset(test_modulus, 0x33, sizeof(test_modulus));
415426
}
@@ -426,6 +437,7 @@ START_TEST(test_wolfBoot_read_blob_rejects_oversized_public_area)
426437

427438
ck_assert_int_eq(rc, BUFFER_E);
428439
ck_assert_int_eq(nvread_calls, 1);
440+
ck_assert_uint_eq(last_pub_read_request_sz, 0);
429441
ck_assert_int_eq(oversized_pub_read_attempted, 0);
430442
}
431443
END_TEST
@@ -527,6 +539,8 @@ START_TEST(test_wolfBoot_unseal_blob_rejects_output_larger_than_capacity)
527539

528540
ck_assert_int_eq(rc, BUFFER_E);
529541
ck_assert_int_eq(secret_sz, 0);
542+
ck_assert_int_eq(forcezero_calls, 1);
543+
ck_assert_uint_eq(last_forcezero_len, sizeof(Unseal_Out));
530544
for (i = 0; i < (int)sizeof(output.canary); i++) {
531545
ck_assert_uint_eq(output.canary[i], 0xA5);
532546
}

tools/unit-tests/unit-update-disk.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,24 +132,29 @@ void disk_close(int drv)
132132
int disk_part_read(int drv, int part, uint64_t off, uint64_t sz, uint8_t *buf)
133133
{
134134
uint8_t *image;
135+
uint64_t max = IMAGE_HEADER_SIZE + TEST_PAYLOAD_SIZE;
135136

136137
(void)drv;
137138
image = (part == BOOT_PART_B) ? part_b_image : part_a_image;
138-
if (off + sz > IMAGE_HEADER_SIZE + TEST_PAYLOAD_SIZE)
139+
if ((off > max) || (sz > (max - off)))
139140
return -1;
140141
memcpy(buf, image + off, (size_t)sz);
141142
return (int)sz;
142143
}
143144

144145
int wolfBoot_open_image_address(struct wolfBoot_image* img, uint8_t* image)
145146
{
146-
uint32_t magic = *(uint32_t *)image;
147+
uint32_t magic;
148+
uint32_t fw_size;
149+
150+
memcpy(&magic, image, sizeof(magic));
147151

148152
if (magic != WOLFBOOT_MAGIC)
149153
return -1;
150154
memset(img, 0, sizeof(*img));
151155
img->hdr = image;
152-
img->fw_size = *(uint32_t *)(image + sizeof(uint32_t));
156+
memcpy(&fw_size, image + sizeof(uint32_t), sizeof(fw_size));
157+
img->fw_size = fw_size;
153158
img->fw_base = image + IMAGE_HEADER_SIZE;
154159
img->hdr_ok = 1;
155160
return 0;

0 commit comments

Comments
 (0)