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: nodejs#38133
fixes: nodejs#38135
  • Loading branch information
Linkgoron committed Apr 9, 2021
1 parent d2f116c commit 581c0be
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,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 @@ -733,7 +735,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
24 changes: 24 additions & 0 deletions test/parallel/test-tls-tlswrap-segfault-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'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', () => {
setTimeout(() => {
tlsSocketKeyLog.on('keylog', () => { });
}, 10);
});

const tlsSocketSession = tls.connect('cause-error-2');
tlsSocketSession.on('error', (e) => {
setTimeout(() => {
tlsSocketSession.on('keylog', () => { });

This comment has been minimized.

Copy link
@zyscoder

zyscoder Apr 12, 2021

session event, not keylog.

}, 10);
});

0 comments on commit 581c0be

Please sign in to comment.