net: reject invalid fds before wrapping Socket - #65212
Open
sankalpsthakur wants to merge 1 commit into
Open
Conversation
Collaborator
|
Review requested:
|
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
force-pushed
the
fix/63308-socket-invalid-fd
branch
from
August 11, 2026 10:53
8a142f4 to
d86fd2d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
new net.Socket({ fd })can abort the process when callers pass arbitrary or already-in-use descriptors. On Linux this showed up as:after
epoll_ctl(ADD)returnedEBADFbecause a wrapped fd was closed before the watcher was registered.This PR:
fstatincreateHandle()so closed/invalid descriptors throw a JSErrnoException(EBADF) instead of reaching libuv.uv__fd_exists()so loop-private fds (backend, signal pipe, async pipe) are treated as already in use (EEXISTon open).epoll_ctlpath to treatEBADF/ENOENT/EPERMas recoverable instead of aborting.Fixes #63308
Validation
node --check lib/net.jsnode --check test/parallel/test-net-socket-invalid-fd.jsnode:24-bookworm-slim) with the issue's fd-iteration snippet; root cause confirmed viastrace(epoll_ctl ... EBADFthen theerrno == EEXISTassert)../noderebuild was too heavy in this environment (no local Release binary for this worktree; incremental rebuild pulled V8). New coverage is intest/parallel/test-net-socket-invalid-fd.jsfor CI.AI/LLM disclosure
Made with Cursor