Skip to content

Commit

Permalink
stm32/storage: Prevent attempts to read/write invalid block addresses.
Browse files Browse the repository at this point in the history
A corrupt filesystem may lead to a request for a block which is out of
range of the block device limits.  Return an error instead of passing the
request down to the lower layer.
  • Loading branch information
pi-anl authored and dpgeorge committed Mar 9, 2021
1 parent 680ce45 commit 59a129f
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions ports/stm32/storage.c
Expand Up @@ -340,8 +340,12 @@ STATIC mp_obj_t pyb_flash_readblocks(size_t n_args, const mp_obj_t *args) {
else if (self != &pyb_flash_obj) {
// Extended block read on a sub-section of the flash storage
uint32_t offset = mp_obj_get_int(args[3]);
block_num += self->start / PYB_FLASH_NATIVE_BLOCK_SIZE;
ret = MICROPY_HW_BDEV_READBLOCKS_EXT(bufinfo.buf, block_num, offset, bufinfo.len);
if ((block_num * PYB_FLASH_NATIVE_BLOCK_SIZE) >= self->len) {
ret = -MP_EFAULT; // Bad address
} else {
block_num += self->start / PYB_FLASH_NATIVE_BLOCK_SIZE;
ret = MICROPY_HW_BDEV_READBLOCKS_EXT(bufinfo.buf, block_num, offset, bufinfo.len);
}
}
#endif
return MP_OBJ_NEW_SMALL_INT(ret);
Expand All @@ -363,8 +367,12 @@ STATIC mp_obj_t pyb_flash_writeblocks(size_t n_args, const mp_obj_t *args) {
else if (self != &pyb_flash_obj) {
// Extended block write on a sub-section of the flash storage
uint32_t offset = mp_obj_get_int(args[3]);
block_num += self->start / PYB_FLASH_NATIVE_BLOCK_SIZE;
ret = MICROPY_HW_BDEV_WRITEBLOCKS_EXT(bufinfo.buf, block_num, offset, bufinfo.len);
if ((block_num * PYB_FLASH_NATIVE_BLOCK_SIZE) >= self->len) {
ret = -MP_EFAULT; // Bad address
} else {
block_num += self->start / PYB_FLASH_NATIVE_BLOCK_SIZE;
ret = MICROPY_HW_BDEV_WRITEBLOCKS_EXT(bufinfo.buf, block_num, offset, bufinfo.len);
}
}
#endif
return MP_OBJ_NEW_SMALL_INT(ret);
Expand Down

0 comments on commit 59a129f

Please sign in to comment.