Skip to content

net: reject invalid fds before wrapping Socket - #65212

Open
sankalpsthakur wants to merge 1 commit into
nodejs:mainfrom
sankalpsthakur:fix/63308-socket-invalid-fd
Open

net: reject invalid fds before wrapping Socket#65212
sankalpsthakur wants to merge 1 commit into
nodejs:mainfrom
sankalpsthakur:fix/63308-socket-invalid-fd

Conversation

@sankalpsthakur

Copy link
Copy Markdown

Summary

new net.Socket({ fd }) can abort the process when callers pass arbitrary or already-in-use descriptors. On Linux this showed up as:

node: ../deps/uv/src/unix/linux.c:1427: uv__io_poll: Assertion `errno == EEXIST' failed.

after epoll_ctl(ADD) returned EBADF because a wrapped fd was closed before the watcher was registered.

This PR:

  • Validates the fd with fstat in createHandle() so closed/invalid descriptors throw a JS ErrnoException (EBADF) instead of reaching libuv.
  • Extends uv__fd_exists() so loop-private fds (backend, signal pipe, async pipe) are treated as already in use (EEXIST on open).
  • Softens the Linux epoll_ctl path to treat EBADF / ENOENT / EPERM as recoverable instead of aborting.

Fixes #63308

Validation

  • node --check lib/net.js
  • node --check test/parallel/test-net-socket-invalid-fd.js
  • Reproduced the abort on Linux (node:24-bookworm-slim) with the issue's fd-iteration snippet; root cause confirmed via strace (epoll_ctl ... EBADF then the errno == EEXIST assert).
  • Full ./node rebuild was too heavy in this environment (no local Release binary for this worktree; incremental rebuild pulled V8). New coverage is in test/parallel/test-net-socket-invalid-fd.js for CI.

AI/LLM disclosure

  • AI coding tools (including Grok and/or Codex agent-assisted editing) were used to help draft or modify code and this PR description.
  • I reviewed the complete change, understand the reasoning, and ran the reported local tests before submitting.
  • This submission is original work of authorship under the project CLA / contributor terms; AI output was not pasted unreviewed.

Made with Cursor

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/net
  • @nodejs/security-wg

@nodejs-github-bot nodejs-github-bot added libuv Issues and PRs related to the libuv dependency or the uv binding. needs-ci PRs that need a full CI run. net Issues and PRs related to the net subsystem. labels Aug 11, 2026
Invalid or closed file descriptors passed to `net.Socket({ fd })`
could abort the process inside libuv when a write raced with close.
Validate the fd with fstat first, treat loop-private fds as already
in use, and treat EBADF/ENOENT/EPERM from epoll_ctl as recoverable
instead of aborting.

Fixes: nodejs#63308
Signed-off-by: Sankalp Thakur <sankalphimself@gmail.com>
@sankalpsthakur
sankalpsthakur force-pushed the fix/63308-socket-invalid-fd branch from 8a142f4 to d86fd2d Compare August 11, 2026 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libuv Issues and PRs related to the libuv dependency or the uv binding. needs-ci PRs that need a full CI run. net Issues and PRs related to the net subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

net.Socket({ fd }) can abort the Node.js process when iterating invalid file descriptors

2 participants