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>
This commit is contained in:
yi chen
2026-07-12 03:30:37 +08:00
parent f70ea602fe
commit daae1fb403

View File

@@ -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) {