extmod/vfs_reader: Check that open() resulted in a file-like object.

That is, an object whose type defines the protocol slot.

Note that due to protocol confusion, a variant of the original crasher that
returned e.g., a machine.Pin instance could still lead to a crash (#17852).

Fixes issue #17841.

Signed-off-by: Jeff Epler <jepler@gmail.com>
Signed-off-by: Jeff Epler <jepler@unpythonic.net>
This commit is contained in:
Jeff Epler
2025-08-08 08:53:14 -05:00
committed by Damien George
parent adf6319884
commit 4b013ec3ff
3 changed files with 25 additions and 1 deletions

View File

@@ -83,7 +83,7 @@ void mp_reader_new_file(mp_reader_t *reader, qstr filename) {
};
mp_obj_t file = mp_vfs_open(MP_ARRAY_SIZE(args), &args[0], (mp_map_t *)&mp_const_empty_map);
const mp_stream_p_t *stream_p = mp_get_stream(file);
const mp_stream_p_t *stream_p = mp_get_stream_raise(file, MP_STREAM_OP_READ);
int errcode = 0;
#if MICROPY_VFS_ROM

View File

@@ -75,3 +75,24 @@ except TypeError:
# Unmount the VFS object.
vfs.umount(fs)
class EvilFilesystem:
def mount(self, readonly, mkfs):
print("mount", readonly, mkfs)
def umount(self):
print("umount")
def open(self, file, mode):
return None
fs = EvilFilesystem()
vfs.mount(fs, "/test_mnt")
try:
execfile("/test_mnt/test.py")
print("ExecFile succeeded")
except OSError:
print("OSError")
vfs.umount(fs)

View File

@@ -5,3 +5,6 @@ open /test.py rb
123
TypeError
umount
mount False False
OSError
umount