Skip to content

Commit 6586288

Browse files
Shivay-98trivikr
authored andcommitted
net: handle undefined parent in _unrefTimer and _destroy
The fix and approach are from #64491 by Shivay-98; this reopens it to get it landed, since the original stalled awaiting requested changes. `Socket.prototype._unrefTimer` and `Socket.prototype._destroy` both walk the `_parent` chain with a strict `!== null` check. During connection teardown a socket's `_parent` can be left `undefined` (for example a TLS socket layered over another stream), so the loop steps onto `undefined` and reads a property off it, throwing a TypeError: Cannot read properties of undefined (reading 'Symbol(timeout)') from an uncaught I/O callback and crashing the process. Using a nullish (`!= null`) check terminates the walk on both `null` and `undefined`. [petter@hightouch.io: apply the same fix to the identical loop in `_destroy`, which the original regression test already exercised via `destroy()`; add direct unit coverage for both paths.] Fixes: #64490 Refs: #64491 Signed-off-by: Petter Häggholm <petter@hightouch.io> PR-URL: #64644 Fixes: #64490 Refs: #64491 Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 34db39b commit 6586288

3 files changed

Lines changed: 70 additions & 2 deletions

File tree

lib/net.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,9 @@ ObjectSetPrototypeOf(Socket, stream.Duplex);
740740

741741
// Refresh existing timeouts.
742742
Socket.prototype._unrefTimer = function _unrefTimer() {
743-
for (let s = this; s !== null; s = s._parent) {
743+
// `_parent` may be null; we use a loose `!= null` check in case external
744+
// code sets it to undefined.
745+
for (let s = this; s != null; s = s._parent) {
744746
if (s[kTimeout])
745747
s[kTimeout].refresh();
746748
}
@@ -1101,7 +1103,9 @@ Socket.prototype._destroy = function(exception, cb) {
11011103

11021104
this.connecting = false;
11031105

1104-
for (let s = this; s !== null; s = s._parent) {
1106+
// `_parent` may be null; we use a loose `!= null` check in case external
1107+
// code sets it to undefined.
1108+
for (let s = this; s != null; s = s._parent) {
11051109
clearTimeout(s[kTimeout]);
11061110
}
11071111

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// Walking the `_parent` chain must stop on a nullish link, not only strict
5+
// `null`. During connection teardown a socket's `_parent` can be left
6+
// `undefined`, which previously caused `_unrefTimer()` and `_destroy()` to read
7+
// a property off `undefined` and throw.
8+
// Refs: https://github.com/nodejs/node/issues/64490
9+
10+
const assert = require('assert');
11+
const net = require('net');
12+
13+
{
14+
const socket = new net.Socket();
15+
socket._parent = undefined;
16+
socket._unrefTimer();
17+
}
18+
19+
{
20+
const socket = new net.Socket();
21+
socket._parent = undefined;
22+
socket.on('error', common.mustNotCall());
23+
socket.destroy();
24+
assert.strictEqual(socket.destroyed, true);
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
if (!common.hasCrypto)
6+
common.skip('missing crypto');
7+
8+
const tls = require('tls');
9+
const fixtures = require('../common/fixtures');
10+
11+
// A TLS socket whose `_parent` is left `undefined` during teardown must not
12+
// crash when reads land on it (`onStreamRead` -> `_unrefTimer`) or when it is
13+
// destroyed (`_destroy`). Both walk the `_parent` chain.
14+
// Refs: https://github.com/nodejs/node/issues/64490
15+
16+
const options = {
17+
key: fixtures.readKey('agent1-key.pem'),
18+
cert: fixtures.readKey('agent1-cert.pem'),
19+
};
20+
21+
const server = tls.createServer(options, common.mustCall((conn) => {
22+
setTimeout(() => conn.write('x'), 50);
23+
}));
24+
25+
server.listen(0, common.mustCall(() => {
26+
const client = tls.connect({
27+
port: server.address().port,
28+
rejectUnauthorized: false,
29+
}, common.mustCall(() => {
30+
client._parent = undefined;
31+
}));
32+
33+
client.on('data', common.mustCall(() => {
34+
server.close();
35+
client.destroy();
36+
}));
37+
38+
client.on('error', common.mustNotCall());
39+
}));

0 commit comments

Comments
 (0)