From b3a4270b9a9f3c5cf99378c39fa61fc437e914a8 Mon Sep 17 00:00:00 2001 From: Yunlk <3283559503@qq.com> Date: Fri, 6 Nov 2026 10:06:02 +0800 Subject: [PATCH] [lwp] fix: reject user-supplied counts whose byte size wraps sys_poll() and sys_epoll_pwait() size a kernel buffer from a count that comes straight from the user process: if (!lwp_user_accessable((void *)fds, nfds * sizeof *fds)) /* wraps */ kfds = (struct pollfd *)kmem_get(nfds * sizeof *kfds); /* wraps */ ret = poll(kfds, nfds, timeout); /* full nfds */ On a 32-bit build nfds = 0x20000001 and sizeof(struct pollfd) = 8 make the product 0x1_00000008, which wraps to 8. The access check and the allocation then agree on an 8-byte block, but poll() walks all 536,870,913 entries: do_pollfd() reads pollfd->fd and writes revents far outside that block. 64-bit builds are not affected because the product is promoted to 64 bits and the oversized allocation just fails. sys_epoll_pwait() has the same shape, and its cnt is a signed int, so a negative value has to be rejected as well. Reject the counts that cannot be represented before anything is sized from them, using the same SIZE_MAX / sizeof(...) form already used for msg_iovlen in sys_sendmsg/sys_recvmsg. The guard arithmetic was checked in isolation with 32-bit size_t semantics: 32-bit SIZE_MAX / sizeof(struct pollfd) = 536870911 (0x1FFFFFFF) nfds 0x1FFFFFFF -> accepted nfds 0x20000000 -> rejected nfds 0x20000001 -> wraps to 8 without the guard, rejected with it epoll cnt -1, 0x15555556 -> rejected, 0 and small values -> accepted Note: the RT-Smart build itself was NOT compiled locally. lwp.h requires RT_USING_MUSLLIBC, so an arm-linux-musleabi toolchain is needed and it was not available here. A CI run on the RT-Smart configuration is required before merging. Refs #11925 --- components/lwp/syscall/syscall_file.c | 17 +++++++++++++++++ components/lwp/syscall/syscall_fs.c | 11 +++++++++++ 2 files changed, 28 insertions(+) diff --git a/components/lwp/syscall/syscall_file.c b/components/lwp/syscall/syscall_file.c index 1650215e673..a5e61937d62 100644 --- a/components/lwp/syscall/syscall_file.c +++ b/components/lwp/syscall/syscall_file.c @@ -524,6 +524,18 @@ sysret_t sys_poll(struct pollfd *fds, nfds_t nfds, int timeout) #ifdef ARCH_MM_MMU struct pollfd *kfds = RT_NULL; + /* + * nfds is supplied by the user process. Computing nfds * sizeof(*fds) in a + * 32-bit size_t wraps for large counts (0x20000001 * 8 becomes 8), while the + * poll loop below still walks the original, un-wrapped nfds and reads and + * writes past the end of that allocation. Reject the counts that cannot be + * represented before anything is sized from them. + */ + if (nfds > SIZE_MAX / sizeof(*fds)) + { + return -EINVAL; + } + if (!lwp_user_accessable((void *)fds, nfds * sizeof *fds)) { return -EFAULT; @@ -546,6 +558,11 @@ sysret_t sys_poll(struct pollfd *fds, nfds_t nfds, int timeout) kmem_put(kfds); return ret; #else + if (nfds > SIZE_MAX / sizeof(*fds)) + { + return -EINVAL; + } + if (!lwp_user_accessable((void *)fds, nfds * sizeof *fds)) { return -EFAULT; diff --git a/components/lwp/syscall/syscall_fs.c b/components/lwp/syscall/syscall_fs.c index 08ea40250f0..73d2c2af32e 100644 --- a/components/lwp/syscall/syscall_fs.c +++ b/components/lwp/syscall/syscall_fs.c @@ -697,6 +697,17 @@ sysret_t sys_epoll_pwait(int fd, struct epoll_event *kev = RT_NULL; sigset_t *ksigs = RT_NULL; + /* + * Same problem as sys_poll(): cnt comes from the user process, and the + * multiplication below wraps in a 32-bit size_t while the event loop still + * walks the original count. cnt is a signed int here, so a negative value + * has to be rejected as well. + */ + if (cnt < 0 || (size_t)cnt > SIZE_MAX / sizeof(struct epoll_event)) + { + return -EINVAL; + } + if (!lwp_user_accessable((void *)ev, cnt * sizeof(struct epoll_event))) { return -EFAULT;