Skip to content

Commit 313c49e

Browse files
ycsincarlescufi
authored andcommitted
subsys/mgmt/hawkbit: Check the hash of stored firmware
Previously, the hash of the firmware is checked while we are downloading the firmware. This isn't ideal as the validity of the firmware written into the flash is not verified and can be corrupted. Furthermore, checking while downloading will have an negative impact to the download speed as the CPU need to do more work during the data transfer. This PR removes the previous verify-hash-while-download implementation and use the flash_img_check API instead. Signed-off-by: Yong Cong Sin <[email protected]>
1 parent 77d829e commit 313c49e

File tree

2 files changed

+17
-48
lines changed

2 files changed

+17
-48
lines changed

subsys/mgmt/hawkbit/Kconfig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ menuconfig HAWKBIT
77
select FLASH
88
select REBOOT
99
select HWINFO
10-
select MBEDTLS
11-
select MBEDTLS_ENABLE_HEAP
1210
select NET_TCP
1311
select NET_SOCKETS
1412
select IMG_MANAGER
@@ -18,6 +16,7 @@ menuconfig HAWKBIT
1816
select JSON_LIBRARY
1917
select BOOTLOADER_MCUBOOT
2018
select MPU_ALLOW_FLASH_WRITE
19+
select IMG_ENABLE_IMAGE_CHECK
2120
select IMG_ERASE_PROGRESSIVELY
2221
select NET_SOCKETS_POSIX_NAMES
2322
help

subsys/mgmt/hawkbit/hawkbit.c

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ LOG_MODULE_REGISTER(hawkbit, CONFIG_HAWKBIT_LOG_LEVEL);
3333
#include "mgmt/hawkbit.h"
3434
#include "hawkbit_firmware.h"
3535

36-
#include "mbedtls/md.h"
37-
3836
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
3937
#define CA_CERTIFICATE_TAG 1
4038
#include <net/tls_credentials.h>
@@ -71,7 +69,6 @@ struct hawkbit_download {
7169
int download_progress;
7270
size_t downloaded_size;
7371
size_t http_content_size;
74-
mbedtls_md_context_t hash_ctx;
7572
uint8_t file_hash[SHA256_HASH_SIZE];
7673
};
7774

@@ -805,14 +802,6 @@ static void response_cb(struct http_response *rsp,
805802
body_data = rsp->body_frag_start;
806803
body_len = rsp->body_frag_len;
807804

808-
ret = mbedtls_md_update(&hb_context.dl.hash_ctx, body_data,
809-
body_len);
810-
if (ret != 0) {
811-
LOG_ERR("mbedTLS md update error: %d", ret);
812-
hb_context.code_status = HAWKBIT_DOWNLOAD_ERROR;
813-
break;
814-
}
815-
816805
ret = flash_img_buffered_write(
817806
&hb_context.flash_ctx, body_data, body_len,
818807
final_data == HTTP_DATA_FINAL);
@@ -1037,8 +1026,7 @@ enum hawkbit_response hawkbit_probe(void)
10371026
int ret;
10381027
int32_t action_id;
10391028
int32_t file_size = 0;
1040-
uint8_t response_hash[SHA256_HASH_SIZE] = { 0 };
1041-
const mbedtls_md_info_t *hash_info;
1029+
struct flash_img_check fic;
10421030
char device_id[DEVICE_ID_HEX_MAX_SIZE] = { 0 },
10431031
cancel_base[CANCEL_BASE_SIZE] = { 0 },
10441032
download_http[DOWNLOAD_HTTP_SIZE] = { 0 },
@@ -1228,67 +1216,49 @@ enum hawkbit_response hawkbit_probe(void)
12281216

12291217
flash_img_init(&hb_context.flash_ctx);
12301218

1231-
hash_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
1232-
if (!hash_info) {
1233-
LOG_ERR("Unable to request hash type from mbedTLS");
1234-
hb_context.code_status = HAWKBIT_METADATA_ERROR;
1235-
goto cleanup;
1236-
}
1237-
1238-
mbedtls_md_init(&hb_context.dl.hash_ctx);
1239-
if (mbedtls_md_setup(&hb_context.dl.hash_ctx, hash_info, 0) < 0) {
1240-
LOG_ERR("Can't setup mbedTLS hash engine");
1241-
mbedtls_md_free(&hb_context.dl.hash_ctx);
1242-
hb_context.code_status = HAWKBIT_METADATA_ERROR;
1243-
goto free_md;
1244-
}
1245-
1246-
mbedtls_md_starts(&hb_context.dl.hash_ctx);
1247-
12481219
ret = (int)send_request(HTTP_GET, HAWKBIT_DOWNLOAD,
12491220
HAWKBIT_STATUS_FINISHED_NONE,
12501221
HAWKBIT_STATUS_EXEC_NONE);
12511222

1252-
mbedtls_md_finish(&hb_context.dl.hash_ctx, response_hash);
1253-
12541223
if (!ret) {
12551224
LOG_ERR("Send request failed (HAWKBIT_DOWNLOAD): %d", ret);
12561225
hb_context.code_status = HAWKBIT_NETWORKING_ERROR;
1257-
goto free_md;
1226+
goto cleanup;
12581227
}
12591228

12601229
if (hb_context.code_status == HAWKBIT_DOWNLOAD_ERROR) {
1261-
goto free_md;
1230+
goto cleanup;
12621231
}
12631232

1233+
/* Check if download finished */
12641234
if (!hb_context.final_data_received) {
12651235
LOG_ERR("Download is not complete");
12661236
hb_context.code_status = HAWKBIT_DOWNLOAD_ERROR;
1267-
goto free_md;
1237+
goto cleanup;
12681238
}
12691239

1270-
if (memcmp(response_hash, hb_context.dl.file_hash, mbedtls_md_get_size(hash_info)) != 0) {
1271-
LOG_ERR("Hash mismatch");
1272-
LOG_HEXDUMP_DBG(response_hash, sizeof(response_hash), "resp");
1273-
LOG_HEXDUMP_DBG(hb_context.dl.file_hash, sizeof(hb_context.dl.file_hash), "file");
1240+
/* Verify the hash of the stored firmware */
1241+
fic.match = hb_context.dl.file_hash;
1242+
fic.clen = hb_context.dl.downloaded_size;
1243+
if (flash_img_check(&hb_context.flash_ctx, &fic, FLASH_AREA_ID(image_1))) {
1244+
LOG_ERR("Firmware - flash validation has failed");
12741245
hb_context.code_status = HAWKBIT_DOWNLOAD_ERROR;
1275-
goto free_md;
1246+
goto cleanup;
12761247
}
12771248

1249+
/* Request mcuboot to upgrade */
12781250
if (boot_request_upgrade(BOOT_UPGRADE_TEST)) {
12791251
LOG_ERR("Failed to mark the image in slot 1 as pending");
12801252
hb_context.code_status = HAWKBIT_DOWNLOAD_ERROR;
1281-
goto free_md;
1253+
goto cleanup;
12821254
}
12831255

1284-
hb_context.code_status = HAWKBIT_UPDATE_INSTALLED;
1285-
hawkbit_device_acid_update(hb_context.json_action_id);
1256+
/* If everything is successful */
1257+
hb_context.code_status = HAWKBIT_UPDATE_INSTALLED;
1258+
hawkbit_device_acid_update(hb_context.json_action_id);
12861259

12871260
hb_context.dl.http_content_size = 0;
12881261

1289-
free_md:
1290-
mbedtls_md_free(&hb_context.dl.hash_ctx);
1291-
12921262
cleanup:
12931263
cleanup_connection();
12941264

0 commit comments

Comments
 (0)