Skip to content

Commit e73b518

Browse files
committed
Fix delta wb_patch double esc corner case
F/235
1 parent 2552a28 commit e73b518

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/delta.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,17 @@ int wb_patch(WB_PATCH_CTX *ctx, uint8_t *dst, uint32_t len)
135135
continue;
136136
}
137137
if (*pp == ESC) {
138+
if ((ctx->patch_size - ctx->p_off) < 2)
139+
return -1;
138140
if (*(pp + 1) == ESC) {
139141
*(dst + dst_off) = ESC;
140142
/* Two bytes of the patch have been consumed to produce ESC */
141143
ctx->p_off += 2;
142144
dst_off++;
143145
continue;
144146
} else {
147+
if ((ctx->patch_size - ctx->p_off) < BLOCK_HDR_SIZE)
148+
return -1;
145149
hdr = (struct block_hdr *)pp;
146150
src_off = (hdr->off[0] << 16) + (hdr->off[1] << 8) +
147151
hdr->off[2];

tools/unit-tests/unit-delta.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ START_TEST(test_wb_patch_resume_large_len)
114114
}
115115
END_TEST
116116

117+
START_TEST(test_wb_patch_trailing_escape_invalid)
118+
{
119+
WB_PATCH_CTX patch_ctx;
120+
uint8_t src[SRC_SIZE] = {0};
121+
uint8_t patch[1] = {ESC};
122+
uint8_t dst[DELTA_BLOCK_SIZE] = {0};
123+
int ret;
124+
125+
ret = wb_patch_init(&patch_ctx, src, SRC_SIZE, patch, sizeof(patch));
126+
ck_assert_int_eq(ret, 0);
127+
128+
ret = wb_patch(&patch_ctx, dst, sizeof(dst));
129+
ck_assert_int_eq(ret, -1);
130+
}
131+
END_TEST
132+
117133
START_TEST(test_wb_diff_init_invalid)
118134
{
119135
WB_DIFF_CTX ctx;
@@ -230,6 +246,7 @@ Suite *patch_diff_suite(void)
230246
tcase_add_test(tc_wolfboot_delta, test_wb_patch_src_bounds_invalid);
231247
tcase_add_test(tc_wolfboot_delta, test_wb_patch_resume_bounds_invalid);
232248
tcase_add_test(tc_wolfboot_delta, test_wb_patch_resume_large_len);
249+
tcase_add_test(tc_wolfboot_delta, test_wb_patch_trailing_escape_invalid);
233250
tcase_add_test(tc_wolfboot_delta, test_wb_patch_and_diff);
234251
suite_add_tcase(s, tc_wolfboot_delta);
235252

0 commit comments

Comments
 (0)