Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: delta update not rejecting wrong base_version #466

Merged
merged 4 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/test-powerfail-simulator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ jobs:
run: |
tools/scripts/sim-update-powerfail-resume.sh

- name: Rebuild with wrong delta base version
run: |
make clean && make test-sim-internal-flash-with-wrong-delta-update

- name: Run negative update test with wrong base version (DELTA)
run: |
tools/scripts/sim-delta-wrongversion-update.sh

# TEST with encryption (aes128)
- name: make clean
Expand Down
22 changes: 12 additions & 10 deletions src/update_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot,
uint32_t *img_size;
uint32_t total_size;
WB_PATCH_CTX ctx;
uint32_t cur_v, upd_v, delta_base_v;
#ifdef EXT_ENCRYPTED
uint8_t key[ENCRYPT_KEY_SIZE];
uint8_t nonce[ENCRYPT_NONCE_SIZE];
Expand All @@ -237,20 +238,24 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot,
if (wolfBoot_get_delta_info(PART_UPDATE, inverse, &img_offset, &img_size) < 0) {
return -1;
}
cur_v = wolfBoot_current_firmware_version();
upd_v = wolfBoot_update_firmware_version();
delta_base_v = wolfBoot_get_diffbase_version(PART_UPDATE);
if (inverse) {
uint32_t cur_v, upd_v, delta_base_v;
cur_v = wolfBoot_current_firmware_version();
upd_v = wolfBoot_update_firmware_version();
delta_base_v = wolfBoot_get_diffbase_version(PART_UPDATE);
if (((cur_v == upd_v) && (delta_base_v < cur_v)) || resume_inverse) {
ret = wb_patch_init(&ctx, boot->hdr, boot->fw_size +
IMAGE_HEADER_SIZE, update->hdr + *img_offset, *img_size);
} else {
ret = -1;
}
} else {
ret = wb_patch_init(&ctx, boot->hdr, boot->fw_size + IMAGE_HEADER_SIZE,
update->hdr + IMAGE_HEADER_SIZE, *img_size);
if (!resume_inverse && (cur_v != delta_base_v)) {
/* Wrong base image, cannot apply delta patch */
ret = -1;
} else {
ret = wb_patch_init(&ctx, boot->hdr, boot->fw_size + IMAGE_HEADER_SIZE,
update->hdr + IMAGE_HEADER_SIZE, *img_size);
}
}
if (ret < 0)
goto out;
Expand Down Expand Up @@ -459,10 +464,7 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
if (flag != SECT_FLAG_NEW &&
wolfBoot_get_partition_state(PART_UPDATE, &st) == 0 &&
st == IMG_STATE_UPDATING) {
if (cur_v == up_v) {
inverse = 0;
}
else if (cur_v < up_v) {
if ((cur_v == 0) || (cur_v == up_v)) {
inverse = 1;
inverse_resume = 1;
}
Expand Down
27 changes: 27 additions & 0 deletions tools/scripts/sim-delta-wrongversion-update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

V=`./wolfboot.elf update_trigger get_version 2>/dev/null`
if [ "x$V" != "x1" ]; then
echo "Failed first boot with update_trigger"
exit 1
fi

# First boot: attempt update, should be rejected
V=`./wolfboot.elf success get_version 2>/dev/null`
if [ "x$V" != "x1" ]; then
echo "Error: Delta update with wrong image reported as successful."
exit 1
fi

# Second boot to verify system is alive
V=`./wolfboot.elf success get_version 2>/dev/null`
if [ "x$V" != "x1" ]; then
echo "Error: System is possibly unrecoverable"
exit 1
fi
echo "Update successfully rejected (V: $V)"

echo Test successful.
exit 0


9 changes: 7 additions & 2 deletions tools/scripts/sim-update-powerfail-resume.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ if [ "x$V" != "x1" ]; then
fi

if [ "x$V" != "x1" ]; then
echo "Failed fallback (V: $V)"
exit 1
echo "Did not fallback (V: $V)"
echo "Retrying get_version after reboot..."
V=`./wolfboot.elf get_version 2>/dev/null`
if [ "x$V" != "x1" ]; then
echo "Error: failed fallback (V: $V)"
exit 1
fi
fi

echo Test successful.
Expand Down
9 changes: 9 additions & 0 deletions tools/test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,15 @@ test-sim-internal-flash-with-delta-update:
$$(($(WOLFBOOT_PARTITION_UPDATE_ADDRESS)-$(ARCH_FLASH_OFFSET))) test-app/image_v$(TEST_UPDATE_VERSION)_signed_diff.bin \
$$(($(WOLFBOOT_PARTITION_SWAP_ADDRESS)-$(ARCH_FLASH_OFFSET))) erased_sec.dd

test-sim-internal-flash-with-wrong-delta-update:
make test-sim-internal-flash-with-update DELTA_UPDATE_OPTIONS="--delta test-app/image_v1_signed.bin"
make test-sim-internal-flash-with-update DELTA_UPDATE_OPTIONS="--delta test-app/image_v2_signed.bin" TEST_UPDATE_VERSION=3
$(Q)$(BINASSEMBLE) internal_flash.dd \
0 wolfboot.bin \
$$(($(WOLFBOOT_PARTITION_BOOT_ADDRESS) - $(ARCH_FLASH_OFFSET))) test-app/image_v1_signed.bin \
$$(($(WOLFBOOT_PARTITION_UPDATE_ADDRESS)-$(ARCH_FLASH_OFFSET))) test-app/image_v3_signed_diff.bin \
$$(($(WOLFBOOT_PARTITION_SWAP_ADDRESS)-$(ARCH_FLASH_OFFSET))) erased_sec.dd

test-sim-update-flash: wolfboot.elf test-sim-internal-flash-with-update FORCE
$(Q)(test `./wolfboot.elf success update_trigger get_version` -eq 1)
$(Q)(test `./wolfboot.elf success get_version` -eq $(TEST_UPDATE_VERSION))
Expand Down
Loading