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

@@ -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