Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions components/lwp/syscall/syscall_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions components/lwp/syscall/syscall_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading