This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 03db4966d8c fs/vfs: validate nfds bounds and timeout in select and poll
03db4966d8c is described below
commit 03db4966d8c7bb0f6d59b93a298066011897b719
Author: Swatantra Yadav <[email protected]>
AuthorDate: Thu Nov 12 22:34:02 2026 +0530
fs/vfs: validate nfds bounds and timeout in select and poll
POSIX IEEE Std 1003.1 explicitly specifies parameter validation for
both select() and poll():
For select():
- [EINVAL] The nfds argument is less than 0 or greater than {FD_SETSIZE}.
- [EINVAL] An invalid timeout was specified (negative tv_sec or invalid
tv_usec).
Previously, passing nfds > FD_SETSIZE caused FD_ISSET() to index past
caller fd_set boundaries (CWE-125), and negative timeouts were silently
converted into infinite blocking waits. This change validates nfds
bounds and timeout upfront, clamps large seconds conversion to INT_MAX
to prevent 32-bit signed integer overflow, and correctly applies the
FD_SETSIZE upper bound after fdcheck_restore() when CONFIG_FDCHECK
is enabled.
For poll():
- [EINVAL] The nfds argument is greater than {OPEN_MAX}.
- [EFAULT] The fds argument points outside the process's allocated
address space.
Previously, poll() used DEBUGASSERT(nfds == 0 || fds != NULL), which is
compiled out in release builds, leading to NULL pointer dereference
(CWE-476). Large nfds could also cause integer multiplication wraparound
in fs_heap_malloc(nfds * sizeof(struct pollfd_s)), resulting in heap
buffer overflow (CWE-190/CWE-122). This change enforces runtime bounds
against OPEN_MAX and validates non-NULL fds when nfds > 0.
Assisted-by: gemini-3.8-flash
Signed-off-by: Swatantra Yadav <[email protected]>
---
fs/vfs/fs_poll.c | 19 +++++++++++++----
fs/vfs/fs_select.c | 60 ++++++++++++++++++++++++++++++++++++++----------------
2 files changed, 57 insertions(+), 22 deletions(-)
diff --git a/fs/vfs/fs_poll.c b/fs/vfs/fs_poll.c
index 50a84ed0769..8d28b231765 100644
--- a/fs/vfs/fs_poll.c
+++ b/fs/vfs/fs_poll.c
@@ -26,10 +26,11 @@
#include <nuttx/config.h>
-#include <poll.h>
-#include <time.h>
#include <assert.h>
#include <errno.h>
+#include <limits.h>
+#include <poll.h>
+#include <time.h>
#include <nuttx/debug.h>
#include <nuttx/clock.h>
@@ -428,12 +429,22 @@ int poll(FAR struct pollfd *fds, nfds_t nfds, int timeout)
int ret = OK;
nfds_t i;
- DEBUGASSERT(nfds == 0 || fds != NULL);
-
/* poll() is a cancellation point */
enter_cancellation_point();
+ if (nfds > OPEN_MAX)
+ {
+ ret = -EINVAL;
+ goto out_with_cancelpt;
+ }
+
+ if (nfds > 0 && fds == NULL)
+ {
+ ret = -EFAULT;
+ goto out_with_cancelpt;
+ }
+
/* Copy the user fds to a kernel array, which also holds the internal
* fields of each entry.
*/
diff --git a/fs/vfs/fs_select.c b/fs/vfs/fs_select.c
index 6815fee4321..b3c77b84ec1 100644
--- a/fs/vfs/fs_select.c
+++ b/fs/vfs/fs_select.c
@@ -29,10 +29,12 @@
#include <sys/select.h>
#include <sys/time.h>
-#include <string.h>
-#include <poll.h>
-#include <errno.h>
#include <assert.h>
+#include <errno.h>
+#include <limits.h>
+#include <poll.h>
+#include <string.h>
+#include <nuttx/clock.h>
#include <nuttx/debug.h>
#include <nuttx/kmalloc.h>
@@ -104,10 +106,47 @@ int select(int nfds, FAR fd_set *readfds, FAR fd_set
*writefds,
return ERROR;
}
+ /* Convert the timeout to milliseconds */
+
+ if (timeout != NULL)
+ {
+ if (timeout->tv_sec < 0 || timeout->tv_usec < 0 ||
+ timeout->tv_usec >= USEC_PER_SEC)
+ {
+ set_errno(EINVAL);
+ return ERROR;
+ }
+
+ /* Calculate the timeout in milliseconds, clamping to INT_MAX
+ * to prevent integer overflow.
+ */
+
+ if (timeout->tv_sec > (INT_MAX - 1000) / 1000)
+ {
+ msec = INT_MAX;
+ }
+ else
+ {
+ msec = timeout->tv_sec * 1000 + (timeout->tv_usec + 999) / 1000;
+ }
+ }
+ else
+ {
+ /* Any negative value of msec means no timeout */
+
+ msec = -1;
+ }
+
#ifdef CONFIG_FDCHECK
nfds = fdcheck_restore(nfds - 1) + 1;
#endif
+ if (nfds < 0 || nfds > FD_SETSIZE)
+ {
+ set_errno(EINVAL);
+ return ERROR;
+ }
+
/* How many pollfd structures do we need to allocate? */
/* Initialize the descriptor list for poll() */
@@ -198,21 +237,6 @@ int select(int nfds, FAR fd_set *readfds, FAR fd_set
*writefds,
DEBUGASSERT(ndx == npfds);
- /* Convert the timeout to milliseconds */
-
- if (timeout)
- {
- /* Calculate the timeout in milliseconds */
-
- msec = timeout->tv_sec * 1000 + (timeout->tv_usec + 999) / 1000;
- }
- else
- {
- /* Any negative value of msec means no timeout */
-
- msec = -1;
- }
-
/* Then let poll do all of the real work. */
ret = poll(pollset, npfds, msec);