Skip to content

Commit 626fcf0

Browse files
authored
Merge pull request #466 from danielinux/delta_base_version_fix
Bugfix: delta update not rejecting wrong base_version
2 parents 40ccd9e + 32d6592 commit 626fcf0

File tree

5 files changed

+62
-12
lines changed

5 files changed

+62
-12
lines changed

.github/workflows/test-powerfail-simulator.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ jobs:
203203
run: |
204204
tools/scripts/sim-update-powerfail-resume.sh
205205
206+
- name: Rebuild with wrong delta base version
207+
run: |
208+
make clean && make test-sim-internal-flash-with-wrong-delta-update
209+
210+
- name: Run negative update test with wrong base version (DELTA)
211+
run: |
212+
tools/scripts/sim-delta-wrongversion-update.sh
206213
207214
# TEST with encryption (aes128)
208215
- name: make clean

src/update_flash.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot,
215215
uint32_t *img_size;
216216
uint32_t total_size;
217217
WB_PATCH_CTX ctx;
218+
uint32_t cur_v, upd_v, delta_base_v;
218219
#ifdef EXT_ENCRYPTED
219220
uint8_t key[ENCRYPT_KEY_SIZE];
220221
uint8_t nonce[ENCRYPT_NONCE_SIZE];
@@ -237,20 +238,24 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot,
237238
if (wolfBoot_get_delta_info(PART_UPDATE, inverse, &img_offset, &img_size) < 0) {
238239
return -1;
239240
}
241+
cur_v = wolfBoot_current_firmware_version();
242+
upd_v = wolfBoot_update_firmware_version();
243+
delta_base_v = wolfBoot_get_diffbase_version(PART_UPDATE);
240244
if (inverse) {
241-
uint32_t cur_v, upd_v, delta_base_v;
242-
cur_v = wolfBoot_current_firmware_version();
243-
upd_v = wolfBoot_update_firmware_version();
244-
delta_base_v = wolfBoot_get_diffbase_version(PART_UPDATE);
245245
if (((cur_v == upd_v) && (delta_base_v < cur_v)) || resume_inverse) {
246246
ret = wb_patch_init(&ctx, boot->hdr, boot->fw_size +
247247
IMAGE_HEADER_SIZE, update->hdr + *img_offset, *img_size);
248248
} else {
249249
ret = -1;
250250
}
251251
} else {
252-
ret = wb_patch_init(&ctx, boot->hdr, boot->fw_size + IMAGE_HEADER_SIZE,
253-
update->hdr + IMAGE_HEADER_SIZE, *img_size);
252+
if (!resume_inverse && (cur_v != delta_base_v)) {
253+
/* Wrong base image, cannot apply delta patch */
254+
ret = -1;
255+
} else {
256+
ret = wb_patch_init(&ctx, boot->hdr, boot->fw_size + IMAGE_HEADER_SIZE,
257+
update->hdr + IMAGE_HEADER_SIZE, *img_size);
258+
}
254259
}
255260
if (ret < 0)
256261
goto out;
@@ -459,10 +464,7 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
459464
if (flag != SECT_FLAG_NEW &&
460465
wolfBoot_get_partition_state(PART_UPDATE, &st) == 0 &&
461466
st == IMG_STATE_UPDATING) {
462-
if (cur_v == up_v) {
463-
inverse = 0;
464-
}
465-
else if (cur_v < up_v) {
467+
if ((cur_v == 0) || (cur_v == up_v)) {
466468
inverse = 1;
467469
inverse_resume = 1;
468470
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
V=`./wolfboot.elf update_trigger get_version 2>/dev/null`
4+
if [ "x$V" != "x1" ]; then
5+
echo "Failed first boot with update_trigger"
6+
exit 1
7+
fi
8+
9+
# First boot: attempt update, should be rejected
10+
V=`./wolfboot.elf success get_version 2>/dev/null`
11+
if [ "x$V" != "x1" ]; then
12+
echo "Error: Delta update with wrong image reported as successful."
13+
exit 1
14+
fi
15+
16+
# Second boot to verify system is alive
17+
V=`./wolfboot.elf success get_version 2>/dev/null`
18+
if [ "x$V" != "x1" ]; then
19+
echo "Error: System is possibly unrecoverable"
20+
exit 1
21+
fi
22+
echo "Update successfully rejected (V: $V)"
23+
24+
echo Test successful.
25+
exit 0
26+
27+

tools/scripts/sim-update-powerfail-resume.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ if [ "x$V" != "x1" ]; then
3838
fi
3939

4040
if [ "x$V" != "x1" ]; then
41-
echo "Failed fallback (V: $V)"
42-
exit 1
41+
echo "Did not fallback (V: $V)"
42+
echo "Retrying get_version after reboot..."
43+
V=`./wolfboot.elf get_version 2>/dev/null`
44+
if [ "x$V" != "x1" ]; then
45+
echo "Error: failed fallback (V: $V)"
46+
exit 1
47+
fi
4348
fi
4449

4550
echo Test successful.

tools/test.mk

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,15 @@ test-sim-internal-flash-with-delta-update:
237237
$$(($(WOLFBOOT_PARTITION_UPDATE_ADDRESS)-$(ARCH_FLASH_OFFSET))) test-app/image_v$(TEST_UPDATE_VERSION)_signed_diff.bin \
238238
$$(($(WOLFBOOT_PARTITION_SWAP_ADDRESS)-$(ARCH_FLASH_OFFSET))) erased_sec.dd
239239

240+
test-sim-internal-flash-with-wrong-delta-update:
241+
make test-sim-internal-flash-with-update DELTA_UPDATE_OPTIONS="--delta test-app/image_v1_signed.bin"
242+
make test-sim-internal-flash-with-update DELTA_UPDATE_OPTIONS="--delta test-app/image_v2_signed.bin" TEST_UPDATE_VERSION=3
243+
$(Q)$(BINASSEMBLE) internal_flash.dd \
244+
0 wolfboot.bin \
245+
$$(($(WOLFBOOT_PARTITION_BOOT_ADDRESS) - $(ARCH_FLASH_OFFSET))) test-app/image_v1_signed.bin \
246+
$$(($(WOLFBOOT_PARTITION_UPDATE_ADDRESS)-$(ARCH_FLASH_OFFSET))) test-app/image_v3_signed_diff.bin \
247+
$$(($(WOLFBOOT_PARTITION_SWAP_ADDRESS)-$(ARCH_FLASH_OFFSET))) erased_sec.dd
248+
240249
test-sim-update-flash: wolfboot.elf test-sim-internal-flash-with-update FORCE
241250
$(Q)(test `./wolfboot.elf success update_trigger get_version` -eq 1)
242251
$(Q)(test `./wolfboot.elf success get_version` -eq $(TEST_UPDATE_VERSION))

0 commit comments

Comments
 (0)