Skip to content

Commit 4a397a7

Browse files
committed
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. (micropython#17852) Closes: micropython#17841 Signed-off-by: Jeff Epler <jepler@gmail.com> Signed-off-by: Jeff Epler <jepler@unpythonic.net>
1 parent adf6319 commit 4a397a7

4 files changed

Lines changed: 26 additions & 1 deletion

File tree

extmod/vfs_reader.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void mp_reader_new_file(mp_reader_t *reader, qstr filename) {
8383
};
8484
mp_obj_t file = mp_vfs_open(MP_ARRAY_SIZE(args), &args[0], (mp_map_t *)&mp_const_empty_map);
8585

86-
const mp_stream_p_t *stream_p = mp_get_stream(file);
86+
const mp_stream_p_t *stream_p = mp_get_stream_raise(file, MP_STREAM_OP_READ);
8787
int errcode = 0;
8888

8989
#if MICROPY_VFS_ROM

tests/extmod/vfs_open_error.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OSError

tests/micropython/builtin_execfile.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,24 @@ def open(self, file, mode):
7575

7676
# Unmount the VFS object.
7777
vfs.umount(fs)
78+
79+
80+
class EvilFilesystem:
81+
def mount(self, readonly, mkfs):
82+
print("mount", readonly, mkfs)
83+
84+
def umount(self):
85+
print("umount")
86+
87+
def open(self, file, mode):
88+
return None
89+
90+
91+
fs = EvilFilesystem()
92+
vfs.mount(fs, "/test_mnt")
93+
try:
94+
execfile("/test_mnt/test.py")
95+
print("ExecFile succeeded")
96+
except OSError:
97+
print("OSError")
98+
vfs.umount(fs)

tests/micropython/builtin_execfile.py.exp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ open /test.py rb
55
123
66
TypeError
77
umount
8+
mount False False
9+
OSError
10+
umount

0 commit comments

Comments
 (0)