Skip to content

Fix incomplete implementation of readonly for VfsPosix #17713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jepler
Copy link
Contributor

@jepler jepler commented Jul 19, 2025

Summary

I noticed that operations such as unlink could be performed on a nominally read-only VfsPosix.

Fix all these operations, and then add a compile-time configuration option MICROPY_VFS_POSIX_WRITABLE. Disabling this option ensures that a VfsPosix instance is ALWAYS read-only. This may be useful when fuzzing micropython, which can otherwise make modifications to the host filesystem.

Testing

I added a new test for VfsPosix read-only mode.

Trade-offs and Alternatives

Initially, I structured the test as an importable module so that other filesystems could potentially re-use the same test code; however, most(all?) other filesystems are based on block devices, and I don't think they have the same problems with needing to add readonly checks in each code path since they just ensure the block write function cannot be called. It also turns out ci_webassembly_run_tests failed due to inability to import the auxiliary file, so I went with the one-file implementation after all.

Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:  +176 +0.021% 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:    +0 +0.000% VIRT_RV32

@jepler jepler force-pushed the vfs-posix-readonly branch 2 times, most recently from d707d58 to b795cd2 Compare July 19, 2025 14:58
I noticed that operations such as unlink could be performed
on a nominally read-only VfsPosix.

Signed-off-by: Jeff Epler <jepler@gmail.com>
@jepler jepler force-pushed the vfs-posix-readonly branch from b795cd2 to b1dd470 Compare July 19, 2025 15:06
Copy link

codecov bot commented Jul 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.46%. Comparing base (17fbc5a) to head (cc65036).
Report is 31 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #17713      +/-   ##
==========================================
+ Coverage   98.44%   98.46%   +0.01%     
==========================================
  Files         171      171              
  Lines       22208    22218      +10     
==========================================
+ Hits        21863    21876      +13     
+ Misses        345      342       -3     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Signed-off-by: Jeff Epler <jepler@gmail.com>
@jepler jepler force-pushed the vfs-posix-readonly branch from b1dd470 to cc65036 Compare July 19, 2025 15:27
@dpgeorge dpgeorge added the extmod Relates to extmod/ directory in source label Jul 23, 2025
@@ -137,7 +137,7 @@ static mp_obj_t vfs_posix_make_new(const mp_obj_type_t *type, size_t n_args, siz
vstr_add_char(&vfs->root, '/');
}
vfs->root_len = vfs->root.len;
vfs->readonly = false;
vfs->readonly = MICROPY_VFS_POSIX_READONLY;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be better called MICROPY_VFS_POSIX_WRITABLE, because:

  1. it matches the existing MICROPY_VFS_WRITABLE
  2. it's something you turn on to enable more features, rather than turning something on to disallow things (ie more a positive config rather than a negative config option)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

@@ -160,10 +160,21 @@ static mp_obj_t vfs_posix_umount(mp_obj_t self_in) {
}
static MP_DEFINE_CONST_FUN_OBJ_1(vfs_posix_umount_obj, vfs_posix_umount);

static bool vfs_posix_is_readonly(mp_obj_vfs_posix_t *self) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe make this static inline?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK


# We need a directory for testing that doesn't already exist.
# Skip the test if it does exist.
temp_dir = "micropy_readonly_test_dir"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest vfs_posix_readonly_test_dir (follows the name of this tests file) to match most of the other tests that do this

temp_dir = "micropy_readonly_test_dir"
try:
os.stat(temp_dir)
raise SystemExit("Target directory {} exists".format(temp_dir))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other tests just print "SKIP" here and raise SystemExit; suggest following those

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw this convention in other tests but I'm not sure how I feel about it.

It feels like "a directory happens to exist in the filesystem that prevents a test from running" is an error, not a skip-this-test situation. For instance, if I'm not keeping an eye on things, a botched run of this test that leaves the temporary directory will just SKIP and not actually test anything.

If you're happy with the way other tests do it, I of course can change it.

@dpgeorge dpgeorge added this to the release-1.26.0 milestone Jul 23, 2025
When this configuration flag is set, VfsPosix instances
can be written. Otherwise, they will always be created
"read only".

This flag is useful when fuzzing micropython: Without VfsPosix,
the fuzzing input script cannot be read; but with writable
VfsPosix, fuzzing scripts can potentially perform undesired
operations on the host filesystem.

Signed-off-by: Jeff Epler <jepler@gmail.com>
@jepler jepler force-pushed the vfs-posix-readonly branch from cc65036 to c4a7ce4 Compare July 23, 2025 15:33
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy