Skip to content
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

tls: fix session and keylog add listener segfault #38180

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
Linkgoron marked this conversation as resolved.
Show resolved Hide resolved

const tlsSocketSession = tls.connect('cause-error-2');
tlsSocketSession.on('error', (e) => {
setTimeout(() => {
tlsSocketSession.on('session', () => { });
}, 10);
});
Linkgoron marked this conversation as resolved.
Show resolved Hide resolved