drivers: memc: the memc API oopses when called from user mode
Describe the bug
memc.h declares memc_read(), memc_write(), memc_get_size() and memc_read_id() as __syscall, but the header is never passed to zephyr_syscall_header() and there are no z_vrfy_ handlers. The calls work from kernel threads, and a user thread that makes any of them is killed. K_SYSCALL_LIMIT is 148 in the build below and the four memc IDs are 456 to 459.
Regression
- This is a regression.
Steps to reproduce
- Build an app for
qemu_x86withCONFIG_USERSPACE=yandCONFIG_MEMC=ythat defines a memc device:DEVICE_DEFINE()with aDEVICE_API(memc, ...)that implementsget_mem_base,get_sizeandread_id. - Start a
K_USERthread, grant it the device withk_object_access_grant(), and callmemc_get_size()from it. - The thread is killed with a kernel oops. The same call from a kernel thread returns 0. mps2/an385 behaves the same.
Relevant log output
user memc_get_size() [00:00:00.010,000] <err> os: Bad system call id 456 invoked
[00:00:00.020,000] <err> os: >>> ZEPHYR FATAL ERROR 3: Kernel oops on CPU 0Impact
Functional Limitation – Some features not working as expected, but system usable.
Environment
- OS: Ubuntu 24.04 (VM on macOS 26.6)
- Toolchain: Zephyr SDK 1.0.1
- Commit: 55303e23f59
- Targets: qemu_x86, mps2/an385
Additional Context
Handlers have to check the range, not only the user buffer. memc_read() and memc_write() memcpy() straight from the get_mem_base() address plus addr, so a handler that only validates data would let a user thread read or write any kernel address through addr. The bbram handlers got the same check in 8438d3b9c1a and c3ea409fc4a.
Two ways to fix it:
- Register the header and add
drivers/memc/memc_handlers.c: check the device object and the user buffer, thenaddr + lenagainstmemc_get_size(), and refuse the call when the driver has noget_size. - Drop
__syscalluntil someone needs memc from user mode.
Option 1 is in #120406.
Source: zephyrproject-rtos/zephyr