-
-
Notifications
You must be signed in to change notification settings - Fork 36.7k
net: reject invalid fds before wrapping Socket #65212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
sankalpsthakur
wants to merge
1
commit into
nodejs:main
from
sankalpsthakur:fix/63308-socket-invalid-fd
+119
−2
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -156,6 +156,7 @@ const { getOptionValue } = require('internal/options'); | |
| // Lazy loaded to improve startup performance. | ||
| let cluster; | ||
| let dns; | ||
| let fs; | ||
| let BlockList; | ||
| let SocketAddress; | ||
| let netPromises; | ||
|
|
@@ -198,6 +199,15 @@ function getFlags(options) { | |
|
|
||
| function createHandle(fd, is_server) { | ||
| validateInt32(fd, 'fd', 0); | ||
| // Validate the descriptor is open before handing it to libuv. An invalid fd | ||
| // must become a JS exception (not a process abort inside uv__io_poll when a | ||
| // later write races with close). See https://github.com/nodejs/node/issues/63308. | ||
| fs ??= require('fs'); | ||
| try { | ||
| fs.fstatSync(fd); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This slows things down but it's also not foolproof, other threads can still close the fd between this check and libuv getting its hands on it. |
||
| } catch (err) { | ||
| throw new ErrnoException(err.errno, 'fstat'); | ||
| } | ||
| const type = guessHandleType(fd); | ||
| if (type === 'PIPE') { | ||
| return new Pipe( | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const net = require('net'); | ||
|
|
||
| // Invalid fds must throw a JS exception from the Socket constructor rather than | ||
| // aborting the process inside libuv (see https://github.com/nodejs/node/issues/63308). | ||
|
|
||
| assert.throws( | ||
| () => new net.Socket({ fd: -1 }), | ||
| { | ||
| code: 'ERR_OUT_OF_RANGE', | ||
| name: 'RangeError', | ||
| } | ||
| ); | ||
|
|
||
| common.runWithInvalidFD((fd) => { | ||
| assert.throws( | ||
| () => { | ||
| new net.Socket({ | ||
| fd, | ||
| readable: false, | ||
| writable: true, | ||
| }); | ||
| }, | ||
| { | ||
| code: 'EBADF', | ||
| syscall: 'fstat', | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| // Wrapping arbitrary existing fds is unsupported on Windows. | ||
| if (common.isWindows) | ||
| return; | ||
|
|
||
| // Iterating arbitrary fds must not abort the process. Unsupported fds throw; | ||
| // in-use libuv fds return EEXIST from open; others may open and emit errors. | ||
| { | ||
| let fd = 3; | ||
| while (fd < 64) { | ||
| try { | ||
| const stream = new net.Socket({ | ||
| fd, | ||
| readable: false, | ||
| writable: true, | ||
| }); | ||
| stream.on('error', () => {}); | ||
| stream.write('might crash'); | ||
| stream.destroy(); | ||
| } catch { | ||
| // Expected for unsupported / invalid / already-watched descriptors. | ||
| } | ||
| fd += 1; | ||
| } | ||
| } | ||
|
|
||
| setImmediate(common.mustCall(() => { | ||
| // If libuv aborted, we never reach here. | ||
| })); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't do this. Changes to dependencies should be upstreamed first.
Since I'm a libuv maintainer, I can tell you now that this approach won't really work because it only considers the file descriptors from this event loop, but there can be many loops.