Skip to content

Commit 661e980

Browse files
Linkgorontargos
authored andcommitted
tls: fix session and keylog add listener segfault
Fix an issue where adding a session or keylog listener on a tlsSocket after it was destroyed caused a segfault. fixes: #38133 fixes: #38135 PR-URL: #38180 Fixes: #38133 Fixes: #38135 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent c61f363 commit 661e980

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/_tls_wrap.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,9 @@ TLSSocket.prototype._init = function(socket, wrap) {
675675
if (event !== 'keylog')
676676
return;
677677

678-
ssl.enableKeylogCallback();
678+
// Guard against enableKeylogCallback after destroy
679+
if (!this._handle) return;
680+
this._handle.enableKeylogCallback();
679681

680682
// Remove this listener since it's no longer needed.
681683
this.removeListener('newListener', keylogNewListener);
@@ -719,7 +721,9 @@ TLSSocket.prototype._init = function(socket, wrap) {
719721
if (event !== 'session')
720722
return;
721723

722-
ssl.enableSessionCallbacks();
724+
// Guard against enableSessionCallbacks after destroy
725+
if (!this._handle) return;
726+
this._handle.enableSessionCallbacks();
723727

724728
// Remove this listener since it's no longer needed.
725729
this.removeListener('newListener', newListener);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
const common = require('../common');
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
6+
// This test ensures that Node.js doesn't incur a segfault while
7+
// adding session or keylog listeners after destroy.
8+
// https://github.com./nodejs/node/issues/38133
9+
// https://github.com./nodejs/node/issues/38135
10+
11+
const tls = require('tls');
12+
const tlsSocketKeyLog = tls.connect('cause-error');
13+
tlsSocketKeyLog.on('error', common.mustCall());
14+
tlsSocketKeyLog.on('close', common.mustCall(() => {
15+
tlsSocketKeyLog.on('keylog', common.mustNotCall());
16+
}));
17+
18+
const tlsSocketSession = tls.connect('cause-error-2');
19+
tlsSocketSession.on('error', common.mustCall());
20+
tlsSocketSession.on('close', common.mustCall(() => {
21+
tlsSocketSession.on('session', common.mustNotCall());
22+
}));

0 commit comments

Comments
 (0)