fw: Limit flash offsets to be within sane limits

Limit flash offsets passed to syscalls. Be sure to check the limits
before doing any form of calculation with the passed values.

Co-authored-by: Mikael Ågren <mikael@tillitis.se>
This commit is contained in:
Michael Cardell Widerkrantz 2025-04-25 15:16:41 +02:00
parent 506b4c8269
commit 632b6d8fc7
No known key found for this signature in database
GPG key ID: D3DB3DDF57E704E5
2 changed files with 30 additions and 2 deletions

View file

@ -67,7 +67,15 @@ int preload_store(struct partition_table *part_table, uint32_t offset,
return -1;
}
if ((offset + size) > SIZE_PRE_LOADED_APP || size > 4096) {
if (offset > SIZE_PRE_LOADED_APP) {
return -2;
}
if (size > 4096) {
return -2;
}
if ((offset + size) > SIZE_PRE_LOADED_APP) {
/* Writing outside of area */
return -2;
}

View file

@ -176,6 +176,10 @@ int storage_erase_sector(struct partition_table *part_table, uint32_t offset,
return -3;
}
if (offset > SIZE_STORAGE_AREA) {
return -2;
}
/* Cannot only erase entire sectors */
if (offset % 4096 != 0) {
return -2;
@ -227,7 +231,15 @@ int storage_write_data(struct partition_table *part_table, uint32_t offset,
return -3;
}
if ((offset + size) > SIZE_STORAGE_AREA || size > 4096) {
if (offset > SIZE_STORAGE_AREA) {
return -2;
}
if (size > 4096) {
return -2;
}
if ((offset + size) > SIZE_STORAGE_AREA) {
/* Writing outside of area */
return -2;
}
@ -263,6 +275,14 @@ int storage_read_data(struct partition_table *part_table, uint32_t offset,
return -3;
}
if (offset > SIZE_STORAGE_AREA) {
return -2;
}
if (size > 4096) {
return -2;
}
if ((offset + size) > SIZE_STORAGE_AREA) {
/* Reading outside of area */
return -2;