Skip to content

Commit

Permalink
tls: fix session and keylog add listener segfault
Browse files Browse the repository at this point in the history
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 <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
Linkgoron authored and targos committed Jun 11, 2021
1 parent c61f363 commit 661e980
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/_tls_wrap.js
Expand Up @@ -675,7 +675,9 @@ TLSSocket.prototype._init = function(socket, wrap) {
if (event !== 'keylog')
return;

ssl.enableKeylogCallback();
// Guard against enableKeylogCallback after destroy
if (!this._handle) return;
this._handle.enableKeylogCallback();

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

ssl.enableSessionCallbacks();
// Guard against enableSessionCallbacks after destroy
if (!this._handle) return;
this._handle.enableSessionCallbacks();

// Remove this listener since it's no longer needed.
this.removeListener('newListener', newListener);
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-tls-tlswrap-segfault-2.js
@@ -0,0 +1,22 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

// This test ensures that Node.js doesn't incur a segfault while
// adding session or keylog listeners after destroy.
// https://github.com/nodejs/node/issues/38133
// https://github.com/nodejs/node/issues/38135

const tls = require('tls');
const tlsSocketKeyLog = tls.connect('cause-error');
tlsSocketKeyLog.on('error', common.mustCall());
tlsSocketKeyLog.on('close', common.mustCall(() => {
tlsSocketKeyLog.on('keylog', common.mustNotCall());
}));

const tlsSocketSession = tls.connect('cause-error-2');
tlsSocketSession.on('error', common.mustCall());
tlsSocketSession.on('close', common.mustCall(() => {
tlsSocketSession.on('session', common.mustNotCall());
}));

0 comments on commit 661e980

Please sign in to comment.