Skip to content

Commit 96228bd

Browse files
committed
Added boundary checks for delta image
1 parent 9eca74d commit 96228bd

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

src/delta.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ int wb_patch(WB_PATCH_CTX *ctx, uint8_t *dst, uint32_t len)
118118
sz = ctx->blk_sz;
119119
if (sz > len)
120120
sz = len;
121+
if (ctx->blk_off > ctx->src_size ||
122+
sz > ctx->src_size - ctx->blk_off)
123+
return -1;
121124
memcpy(dst + dst_off, ctx->src_base + ctx->blk_off, sz);
122125
if (ctx->blk_sz > len) {
123126
ctx->blk_sz -= len;
@@ -150,6 +153,9 @@ int wb_patch(WB_PATCH_CTX *ctx, uint8_t *dst, uint32_t len)
150153
} else {
151154
copy_sz = sz;
152155
}
156+
if (src_off > ctx->src_size ||
157+
copy_sz > ctx->src_size - src_off)
158+
return -1;
153159
memcpy(dst + dst_off, ctx->src_base + src_off, copy_sz);
154160
if (sz == copy_sz) {
155161
/* End of the block, reset counters and matching state */

tools/unit-tests/unit-delta.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,49 @@ START_TEST(test_wb_patch_init_invalid)
4848
}
4949
END_TEST
5050

51+
START_TEST(test_wb_patch_src_bounds_invalid)
52+
{
53+
WB_PATCH_CTX patch_ctx;
54+
uint8_t src[SRC_SIZE] = {0};
55+
uint8_t patch[PATCH_SIZE] = {0};
56+
uint8_t dst[DELTA_BLOCK_SIZE] = {0};
57+
int ret;
58+
59+
/* ESC + header with src_off beyond src_size */
60+
patch[0] = ESC;
61+
patch[1] = 0x00; /* off[0] */
62+
patch[2] = 0x10; /* off[1] -> 0x001000 */
63+
patch[3] = 0x00; /* off[2] */
64+
patch[4] = 0x00; /* sz[0] */
65+
patch[5] = 0x10; /* sz[1] -> 16 */
66+
67+
ret = wb_patch_init(&patch_ctx, src, SRC_SIZE, patch, BLOCK_HDR_SIZE);
68+
ck_assert_int_eq(ret, 0);
69+
70+
ret = wb_patch(&patch_ctx, dst, sizeof(dst));
71+
ck_assert_int_eq(ret, -1);
72+
}
73+
END_TEST
74+
75+
START_TEST(test_wb_patch_resume_bounds_invalid)
76+
{
77+
WB_PATCH_CTX patch_ctx;
78+
uint8_t src[SRC_SIZE] = {0};
79+
uint8_t patch[PATCH_SIZE] = {0};
80+
uint8_t dst[DELTA_BLOCK_SIZE] = {0};
81+
int ret;
82+
83+
ret = wb_patch_init(&patch_ctx, src, SRC_SIZE, patch, BLOCK_HDR_SIZE);
84+
ck_assert_int_eq(ret, 0);
85+
86+
patch_ctx.matching = 1;
87+
patch_ctx.blk_off = SRC_SIZE + 1;
88+
patch_ctx.blk_sz = 4;
89+
90+
ret = wb_patch(&patch_ctx, dst, sizeof(dst));
91+
ck_assert_int_eq(ret, -1);
92+
}
93+
END_TEST
5194

5295
START_TEST(test_wb_diff_init_invalid)
5396
{
@@ -162,6 +205,8 @@ Suite *patch_diff_suite(void)
162205

163206
tcase_add_test(tc_wolfboot_delta, test_wb_patch_init_invalid);
164207
tcase_add_test(tc_wolfboot_delta, test_wb_diff_init_invalid);
208+
tcase_add_test(tc_wolfboot_delta, test_wb_patch_src_bounds_invalid);
209+
tcase_add_test(tc_wolfboot_delta, test_wb_patch_resume_bounds_invalid);
165210
tcase_add_test(tc_wolfboot_delta, test_wb_patch_and_diff);
166211
suite_add_tcase(s, tc_wolfboot_delta);
167212

0 commit comments

Comments
 (0)