Skip to content

Conversation

dpgeorge
Copy link
Member

Summary

While testing ROMFS as part of #8381 I found that it was relatively easy to end up with a corrupt filesystem on the device (eg due to the ROMFS deploy process stopping half way through), which could lead to hard crashes. Notably: boot loops trying to mount a corrupt filesystem, crashes when importing modules like os that first scan the filesystem for os.py, and crashing when deploying a new ROMFS in certain cases because the old one is removed while still mounted.

@robert-hh also encountered similar issues when testing #8381.

This PR adds full bounds checking for all ROMFS filesystem accesses.

Testing

The new code should be fully covered by the new tests that are added here, and they run in CI.

Trade-offs and Alternatives

I was hoping such bounds checking would not be necessary, because it increases the size and complexity of the ROMFS driver. But after playing around with ROMFS in a project, it's definitely necessary to have full bounds checking.

@dpgeorge dpgeorge added the extmod Relates to extmod/ directory in source label Feb 25, 2025
@dpgeorge dpgeorge requested a review from projectgus February 25, 2025 02:04
Copy link

codecov bot commented Feb 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.53%. Comparing base (78728dc) to head (7429b8f).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #16810   +/-   ##
=======================================
  Coverage   98.53%   98.53%           
=======================================
  Files         169      169           
  Lines       21822    21852   +30     
=======================================
+ Hits        21502    21532   +30     
  Misses        320      320           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

github-actions bot commented Feb 25, 2025

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:  +232 +0.027% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:  +120 +0.026% VIRT_RV32

@dpgeorge dpgeorge added this to the release-1.25.0 milestone Feb 25, 2025
@dpgeorge dpgeorge force-pushed the extmod-vfs-rom-check-corrupt branch from b299a42 to a06c9b0 Compare February 25, 2025 04:15
@dpgeorge
Copy link
Member Author

I needed to increase the MicroPython heap for the qemu Sabrelite board, because the new ROMFS test is quite large when compiled using the ARM (non-Thumb) native emitter.

Signed-off-by: Damien George <damien@micropython.org>
Testing with ROMFS shows that it is relatively easy to end up with a
corrupt filesystem on the device -- eg due to the ROMFS deploy process
stopping half way through -- which could lead to hard crashes.  Notably,
there can be boot loops trying to mount a corrupt filesystem, crashes when
importing modules like `os` that first scan the filesystem for `os.py`, and
crashing when deploying a new ROMFS in certain cases because the old one is
removed while still mounted.

The main problem is that `mp_decode_uint()` has an loop that keeps going as
long as it reads 0xff byte values, which can happen in the case of erased
and unwritten flash.

This commit adds full bounds checking in the new `mp_decode_uint_checked()`
function, and that makes all ROMFS filesystem accesses robust.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the extmod-vfs-rom-check-corrupt branch 2 times, most recently from 7429b8f to 14ba32b Compare February 27, 2025 00:31
@dpgeorge dpgeorge merged commit 14ba32b into micropython:master Feb 27, 2025
63 of 64 checks passed
@dpgeorge dpgeorge deleted the extmod-vfs-rom-check-corrupt branch February 27, 2025 00:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extmod Relates to extmod/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants