From 30acb16ad34babf25d126a7b471616421167cb35 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 3 Jan 2025 12:02:12 +1100 Subject: [PATCH] extmod/vfs_rom: Remove ability to create VfsRom from an address. It's not necessary to support this, which allows an arbitrary memory address to be specified and potentially allows invalid memory accesses. Requiring an object with the buffer protocol is safer, and also means that the length of the region is always specified. Signed-off-by: Damien George --- extmod/vfs_rom.c | 12 +++++------- tests/extmod/vfs_rom.py | 3 ++- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/extmod/vfs_rom.c b/extmod/vfs_rom.c index b8b6f8e40d..99ddaba95e 100644 --- a/extmod/vfs_rom.c +++ b/extmod/vfs_rom.c @@ -198,15 +198,13 @@ static mp_obj_t vfs_rom_make_new(const mp_obj_type_t *type, size_t n_args, size_ self->base.type = type; self->memory = args[0]; + // Get the ROMFS memory region. mp_buffer_info_t bufinfo; - if (mp_get_buffer(self->memory, &bufinfo, MP_BUFFER_READ)) { - if (bufinfo.len < ROMFS_SIZE_MIN) { - mp_raise_OSError(MP_ENODEV); - } - self->filesystem = bufinfo.buf; - } else { - self->filesystem = (const uint8_t *)(uintptr_t)mp_obj_get_int_truncated(self->memory); + mp_get_buffer_raise(self->memory, &bufinfo, MP_BUFFER_READ); + if (bufinfo.len < ROMFS_SIZE_MIN) { + mp_raise_OSError(MP_ENODEV); } + self->filesystem = bufinfo.buf; // Verify it is a ROMFS. if (!(self->filesystem[0] == ROMFS_HEADER_BYTE0 diff --git a/tests/extmod/vfs_rom.py b/tests/extmod/vfs_rom.py index d416d83870..f7958a9396 100644 --- a/tests/extmod/vfs_rom.py +++ b/tests/extmod/vfs_rom.py @@ -226,7 +226,8 @@ class TestEdgeCases(unittest.TestCase): class TestStandalone(TestBase): def test_constructor(self): self.assertIsInstance(vfs.VfsRom(self.romfs), vfs.VfsRom) - self.assertIsInstance(vfs.VfsRom(self.romfs_addr), vfs.VfsRom) + with self.assertRaises(TypeError): + vfs.VfsRom(self.romfs_addr) def test_mount(self): vfs.VfsRom(self.romfs).mount(True, False)