Skip to content

Commit 73b5ad4

Browse files
committed
fix store sbrk heap bounds handling
F/725
1 parent 3e706be commit 73b5ad4

6 files changed

Lines changed: 102 additions & 23 deletions

File tree

src/pkcs11_store.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <string.h>
2525

2626
#include "hal.h"
27+
#include "store_sbrk.h"
2728

2829
#ifdef SECURE_PKCS11
2930

@@ -72,17 +73,7 @@ void * _sbrk(unsigned int incr)
7273
{
7374
static uint8_t *heap = NULL;
7475
static uint32_t heapsize = (uint32_t)&_heap_size;
75-
void *old_heap = heap;
76-
(void)heapsize;
77-
if (((incr >> 2) << 2) != incr)
78-
incr = ((incr >> 2) + 1) << 2;
79-
80-
if (heap == NULL) {
81-
heap = (uint8_t*)&_start_heap;
82-
old_heap = heap;
83-
} else
84-
heap += incr;
85-
return old_heap;
76+
return wolfboot_store_sbrk(incr, &heap, (uint8_t *)&_start_heap, heapsize);
8677
}
8778
#endif
8879

src/psa_store.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <string.h>
2525

2626
#include "hal.h"
27+
#include "store_sbrk.h"
2728

2829
#ifdef WOLFCRYPT_TZ_PSA
2930

@@ -71,17 +72,7 @@ void * _sbrk(unsigned int incr)
7172
{
7273
static uint8_t *heap = NULL;
7374
static uint32_t heapsize = (uint32_t)&_heap_size;
74-
void *old_heap = heap;
75-
(void)heapsize;
76-
if (((incr >> 2) << 2) != incr)
77-
incr = ((incr >> 2) + 1) << 2;
78-
79-
if (heap == NULL) {
80-
heap = (uint8_t*)&_start_heap;
81-
old_heap = heap;
82-
} else
83-
heap += incr;
84-
return old_heap;
75+
return wolfboot_store_sbrk(incr, &heap, (uint8_t *)&_start_heap, heapsize);
8576
}
8677
#endif
8778

src/store_sbrk.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stddef.h>
2+
3+
#include "store_sbrk.h"
4+
5+
void *wolfboot_store_sbrk(unsigned int incr, uint8_t **heap,
6+
uint8_t *heap_base, uint32_t heap_size)
7+
{
8+
uint8_t *heap_limit = heap_base + heap_size;
9+
void *old_heap = *heap;
10+
11+
if (((incr >> 2) << 2) != incr)
12+
incr = ((incr >> 2) + 1) << 2;
13+
14+
if (*heap == NULL) {
15+
*heap = heap_base;
16+
old_heap = *heap;
17+
}
18+
19+
if ((uint32_t)(heap_limit - *heap) < incr)
20+
return (void *)-1;
21+
22+
*heap += incr;
23+
24+
return old_heap;
25+
}

src/store_sbrk.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef WOLFBOOT_STORE_SBRK_H
2+
#define WOLFBOOT_STORE_SBRK_H
3+
4+
#include <stdint.h>
5+
6+
void *wolfboot_store_sbrk(unsigned int incr, uint8_t **heap,
7+
uint8_t *heap_base, uint32_t heap_size);
8+
9+
#endif

tools/unit-tests/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ TESTS:=unit-parser unit-extflash unit-string unit-spi-flash unit-aes128 \
4848
unit-enc-nvm-flagshome unit-delta unit-update-flash \
4949
unit-update-flash-enc unit-update-ram unit-pkcs11_store unit-psa_store unit-disk \
5050
unit-update-disk unit-multiboot unit-boot-x86-fsp unit-qspi-flash unit-tpm-rsa-exp \
51-
unit-image-nopart unit-image-sha384 unit-image-sha3-384 \
51+
unit-image-nopart unit-image-sha384 unit-image-sha3-384 unit-store-sbrk \
5252
unit-tpm-blob
5353

5454
all: $(TESTS)
@@ -132,6 +132,9 @@ unit-tpm-blob: ../../include/target.h unit-tpm-blob.c
132132
-DWOLFBOOT_HASH_SHA256 \
133133
-ffunction-sections -fdata-sections $(LDFLAGS) -Wl,--gc-sections
134134

135+
unit-store-sbrk: unit-store-sbrk.c ../../src/store_sbrk.c
136+
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
137+
135138
unit-string: ../../include/target.h unit-string.c
136139
gcc -o $@ $^ $(CFLAGS) -DDEBUG_UART -DPRINTF_ENABLED $(LDFLAGS)
137140

tools/unit-tests/unit-store-sbrk.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* unit-store-sbrk.c
2+
*
3+
* Unit tests for store allocator helper.
4+
*/
5+
6+
#include <check.h>
7+
#include <stdint.h>
8+
9+
#include "../../src/store_sbrk.h"
10+
11+
START_TEST(test_sbrk_first_call_advances_heap)
12+
{
13+
uint8_t heap_buf[32];
14+
uint8_t *heap = NULL;
15+
void *ret;
16+
17+
ret = wolfboot_store_sbrk(5, &heap, heap_buf, sizeof(heap_buf));
18+
19+
ck_assert_ptr_eq(ret, heap_buf);
20+
ck_assert_ptr_eq(heap, heap_buf + 8);
21+
}
22+
END_TEST
23+
24+
START_TEST(test_sbrk_rejects_overflow)
25+
{
26+
uint8_t heap_buf[16];
27+
uint8_t *heap = NULL;
28+
void *ret;
29+
30+
ret = wolfboot_store_sbrk(8, &heap, heap_buf, sizeof(heap_buf));
31+
ck_assert_ptr_eq(ret, heap_buf);
32+
33+
ret = wolfboot_store_sbrk(16, &heap, heap_buf, sizeof(heap_buf));
34+
ck_assert_ptr_eq(ret, (void *)-1);
35+
ck_assert_ptr_eq(heap, heap_buf + 8);
36+
}
37+
END_TEST
38+
39+
Suite *wolfboot_suite(void)
40+
{
41+
Suite *s = suite_create("store-sbrk");
42+
TCase *tcase = tcase_create("store_sbrk");
43+
44+
tcase_add_test(tcase, test_sbrk_first_call_advances_heap);
45+
tcase_add_test(tcase, test_sbrk_rejects_overflow);
46+
suite_add_tcase(s, tcase);
47+
return s;
48+
}
49+
50+
int main(void)
51+
{
52+
int fails;
53+
Suite *s = wolfboot_suite();
54+
SRunner *sr = srunner_create(s);
55+
56+
srunner_run_all(sr, CK_NORMAL);
57+
fails = srunner_ntests_failed(sr);
58+
srunner_free(sr);
59+
return fails;
60+
}

0 commit comments

Comments
 (0)