Skip to content

Commit 83b54d0

Browse files
committed
implement more checks in coproc module
- check memory address range - check firmware size at an earlier stage
1 parent a4238d8 commit 83b54d0

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

locale/circuitpython.pot

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ msgstr ""
193193

194194
#: ports/atmel-samd/common-hal/pulseio/PulseIn.c
195195
#: ports/cxd56/common-hal/pulseio/PulseIn.c
196+
#: ports/espressif/common-hal/coproc/Coproc.c
196197
#: ports/nrf/common-hal/pulseio/PulseIn.c
197198
#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
198199
#: ports/stm/common-hal/pulseio/PulseIn.c py/argcheck.c
@@ -485,6 +486,7 @@ msgstr ""
485486
msgid "Already have all-matches listener"
486487
msgstr ""
487488

489+
#: ports/espressif/common-hal/coproc/__init__.c
488490
#: shared-module/memorymonitor/AllocationAlarm.c
489491
#: shared-module/memorymonitor/AllocationSize.c
490492
msgid "Already running"
@@ -1015,7 +1017,7 @@ msgstr ""
10151017
msgid "Firmware is invalid"
10161018
msgstr ""
10171019

1018-
#: ports/espressif/common-hal/coproc/__init__.c
1020+
#: ports/espressif/common-hal/coproc/Coproc.c
10191021
#: ports/espressif/common-hal/dualbank/__init__.c
10201022
msgid "Firmware is too big"
10211023
msgstr ""

ports/espressif/common-hal/coproc/Coproc.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,33 @@
2727
#include "shared-bindings/coproc/Coproc.h"
2828
#include "shared-bindings/coproc/CoprocMemory.h"
2929

30+
#include "py/runtime.h"
31+
32+
#if defined(CONFIG_IDF_TARGET_ESP32S2)
33+
#include "esp32s2/ulp.h"
34+
#define ULP_COPROC_RESERVE_MEM (CONFIG_ESP32S2_ULP_COPROC_RESERVE_MEM)
35+
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
36+
#include "esp32s3/ulp.h"
37+
#define ULP_COPROC_RESERVE_MEM (CONFIG_ESP32S3_ULP_COPROC_RESERVE_MEM)
38+
#endif
39+
40+
#define RTC_SLOW_MEM_END ((uint32_t)RTC_SLOW_MEM + ULP_COPROC_RESERVE_MEM)
41+
3042
void common_hal_coproc_coproc_construct(coproc_coproc_obj_t *self,
3143
const uint8_t *buf, const size_t buf_len, coproc_memory_obj_t *coproc_memory) {
3244
// set CoprocMemory object
45+
if (coproc_memory != NULL) {
46+
if (coproc_memory->address < ((uint32_t)RTC_SLOW_MEM + buf_len) ||
47+
coproc_memory->address > (RTC_SLOW_MEM_END - coproc_memory->len)) {
48+
mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_CoprocMemory);
49+
}
50+
}
3351
self->coproc_memory = coproc_memory;
3452

3553
// load buffer
54+
if (buf_len > ULP_COPROC_RESERVE_MEM) {
55+
mp_raise_RuntimeError(translate("Firmware is too big"));
56+
}
3657
self->buf_len = buf_len;
3758
self->buf = (uint8_t *)m_malloc(self->buf_len, false);
3859
memcpy(self->buf, buf, self->buf_len);

ports/espressif/esp-idf-config/sdkconfig-esp32s2.defaults

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ CONFIG_ESP32S2_DATA_CACHE_LINE_32B=y
4242
# CONFIG_ESP32S2_TRAX is not set
4343
CONFIG_ESP32S2_TRACEMEM_RESERVE_DRAM=0x0
4444
CONFIG_ESP32S2_ULP_COPROC_ENABLED=y
45-
CONFIG_ESP32S2_ULP_COPROC_RESERVE_MEM=4096
45+
CONFIG_ESP32S2_ULP_COPROC_RESERVE_MEM=8176
4646
CONFIG_ESP32S2_ULP_COPROC_RISCV=y
4747
CONFIG_ESP32S2_DEBUG_OCDAWARE=y
4848
# CONFIG_ESP32S2_DEBUG_STUBS_ENABLE is not set

ports/espressif/esp-idf-config/sdkconfig-esp32s3.defaults

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ CONFIG_ESP32S3_DATA_CACHE_LINE_SIZE=32
6868
# CONFIG_ESP32S3_TRAX is not set
6969
CONFIG_ESP32S3_TRACEMEM_RESERVE_DRAM=0x0
7070
CONFIG_ESP32S3_ULP_COPROC_ENABLED=y
71-
CONFIG_ESP32S3_ULP_COPROC_RESERVE_MEM=4096
71+
CONFIG_ESP32S3_ULP_COPROC_RESERVE_MEM=8176
7272
CONFIG_ESP32S3_ULP_COPROC_RISCV=y
7373
CONFIG_ESP32S3_BROWNOUT_DET=y
7474
CONFIG_ESP32S3_BROWNOUT_DET_LVL_SEL_7=y

0 commit comments

Comments
 (0)