From daae1fb403b36fc0f9fc57ccdfc20fad458f13e8 Mon Sep 17 00:00:00 2001 From: yi chen <94xhn1@gmail.com> Date: Sun, 12 Jul 2026 03:30:37 +0800 Subject: [PATCH] fix(esp_partition): prevent size_t overflow bypassing bounds checks on linux target esp_partition_write/read/erase_range/mmap in partition_linux.c (the `linux` target backend used by --preview set-target linux / host_test) validated the requested range with `offset + size > partition->size`. When `size` is close to SIZE_MAX, this addition wraps around size_t and can evaluate to a small value, so the check passes even though the request is far out of bounds. A caller passing e.g. esp_partition_write(partition, 1, src, SIZE_MAX) sails through both bounds checks and reaches the byte-copy loop with new_size == SIZE_MAX, causing out-of-bounds reads/writes far past both the caller's buffer and the mmap'd emulated-flash file. Replace all four instances with the overflow-safe form already used by the other esp_partition backends (partition_target.c, partition_bootloader.c, partition_tee.c): `size > partition->size - offset`, which is safe because the preceding check already guarantees offset <= partition->size. Signed-off-by: yi chen <94xhn1@gmail.com> --- components/esp_partition/partition_linux.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/esp_partition/partition_linux.c b/components/esp_partition/partition_linux.c index 6c05685be5c..4144ac9f9b5 100644 --- a/components/esp_partition/partition_linux.c +++ b/components/esp_partition/partition_linux.c @@ -554,7 +554,7 @@ esp_err_t esp_partition_write(const esp_partition_t *partition, size_t dst_offse if (dst_offset > partition->size) { return ESP_ERR_INVALID_ARG; } - if (dst_offset + size > partition->size) { + if (size > partition->size - dst_offset) { return ESP_ERR_INVALID_SIZE; } @@ -610,7 +610,7 @@ esp_err_t esp_partition_read(const esp_partition_t *partition, size_t src_offset if (src_offset > partition->size) { return ESP_ERR_INVALID_ARG; } - if (src_offset + size > partition->size) { + if (size > partition->size - src_offset) { return ESP_ERR_INVALID_SIZE; } @@ -655,7 +655,7 @@ esp_err_t esp_partition_erase_range(const esp_partition_t *partition, size_t off if (offset > partition->size || offset % partition->erase_size != 0) { return ESP_ERR_INVALID_ARG; } - if (offset + size > partition->size || size % partition->erase_size != 0) { + if (size > partition->size - offset || size % partition->erase_size != 0) { return ESP_ERR_INVALID_SIZE; } @@ -707,7 +707,7 @@ esp_err_t esp_partition_mmap(const esp_partition_t *partition, size_t offset, si if (offset > partition->size) { return ESP_ERR_INVALID_ARG; } - if (offset + size > partition->size) { + if (size > partition->size - offset) { return ESP_ERR_INVALID_SIZE; } if (partition->flash_chip != NULL) {