Skip to content

Commit 27a306b

Browse files
committed
Prevent downgrade attacks by default, unless compiled with
ALLOW_DOWNGRADE=1
1 parent 3812dc9 commit 27a306b

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ SWAP?=1
1616
CORTEX_M0?=0
1717
NO_ASM=0
1818
EXT_FLASH=0
19+
ALLOW_DOWNGRADE=0
1920

2021
LSCRIPT:=hal/$(TARGET).ld
2122

@@ -79,6 +80,10 @@ ifeq ($(EXT_FLASH),1)
7980
CFLAGS+=-DEXT_FLASH=1 -DPART_UPDATE_EXT=1 -DPART_SWAP_EXT=1
8081
endif
8182

83+
ifeq ($(ALLOW_DOWNGRADE),1)
84+
CFLAGS+=-DALLOW_DOWNGRADE
85+
endif
86+
8287

8388
ifeq ($(SIGN),ED25519)
8489
OBJS+= ./lib/wolfssl/wolfcrypt/src/sha512.o \

src/loader.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,18 @@ static int wolfBoot_update(void)
8282
/* Check the first sector to detect interrupted update */
8383
if ((wolfBoot_get_sector_flag(PART_UPDATE, 0, &flag) < 0) || (flag == SECT_FLAG_NEW))
8484
{
85-
/* In case this is a new update, check
86-
* integrity/authenticity of the firmware update
85+
/* In case this is a new update, do the required
86+
* checks on the firmware update
8787
* before starting the swap
8888
*/
8989
if (!update.hdr_ok || (wolfBoot_verify_integrity(&update) < 0)
9090
|| (wolfBoot_verify_authenticity(&update) < 0)) {
9191
return -1;
9292
}
93+
#ifndef ALLOW_DOWNGRADE
94+
if (wolfBoot_update_firmware_version() <= wolfBoot_current_firmware_version())
95+
return -1;
96+
#endif
9397
}
9498

9599
hal_flash_unlock();

0 commit comments

Comments
 (0)