Description
RT-Thread Version
v5.1.0
Hardware Type/Architectures
Not apply
Develop Toolchain
Other
Describe the bug
Summary
I have identified a vulnerability in the sys_select system call in Smart version of RT-Thread v5.1.0. I am opening this issue for your review, as I could not find a reporting email in the security policy of this repository. This vulnerability stems from insufficient pointer validation, particularly for the timeout parameter. The code only checks if the pointer is NULL but fails to verify whether the pointer points to valid memory. If exploited by a compromised user thread, this issue could lead to severe security consequences, including kernel crashes and potential unauthorized memory access.
Vulnerable Code Location
The vulnerability is present in the rt-thread/components/lwp/lwp_syscall.c file. The issue arises from the lack of proper validation of the timeout parameter pointer. The call graph of this vulnerability is as follows:
rt-thread/components/lwp/lwp_syscall.c
:sys_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)rt-thread/components/libc/posix/io/poll/select.c
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
sysret_t sys_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
#ifdef ARCH_MM_MMU
int ret = -1;
fd_set *kreadfds = RT_NULL, *kwritefds = RT_NULL, *kexceptfds = RT_NULL;
//Omitted code
ret = select(nfds, kreadfds, kwritefds, kexceptfds, timeout);
// Omitted code
return (ret < 0 ? GET_ERRNO() : ret);
#endif
}
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
int fd;
int npfds;
int msec;
int ndx;
int ret;
struct pollfd *pollset = RT_NULL;
// Omitted code
/* Convert the timeout to milliseconds */
if (timeout)
{
msec = (int)timeout->tv_sec * 1000 + (int)timeout->tv_usec / 1000; // Vulnerability: Only checks if timeout is NULL, but doesn't verify if it points to valid memory
}
else
{
msec = -1;
}
// Omitted code
return ret;
}
Vulnerability Description
- The vulnerability exists in the select function's handling of the timeout parameter. The code only performs a NULL check on the timeout pointer but fails to verify whether the pointer points to valid memory. This oversight can lead to a memory fault when dereferencing an invalid pointer. The issue is particularly critical because:
- The timeout parameter is passed directly from user space to kernel space
- The code only checks if the pointer is NULL (if (timeout))
- No validation is performed to ensure the pointer points to valid memory before dereferencing it
- When accessing timeout->tv_sec and timeout->tv_usec, a memory fault will occur if the pointer points to invalid memory
Impact
This vulnerability has severe security implications:
-
Kernel Crash: The most immediate impact is a potential kernel crash due to invalid memory access, leading to a Denial of Service (DoS) condition.
-
Privilege Escalation: In certain scenarios, this vulnerability could potentially be exploited to access kernel memory, leading to privilege escalation.
Other additional context
No response