Skip to content

Commit 3da003c

Browse files
Linkgoronjasnell
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: #38133fixes: #38135 PR-URL: #38180Fixes: #38133Fixes: #38135 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 0180fc5 commit 3da003c

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

‎lib/_tls_wrap.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,9 @@ TLSSocket.prototype._init = function(socket, wrap){
689689
if(event!=='keylog')
690690
return;
691691

692-
ssl.enableKeylogCallback();
692+
// Guard against enableKeylogCallback after destroy
693+
if(!this._handle)return;
694+
this._handle.enableKeylogCallback();
693695

694696
// Remove this listener since it's no longer needed.
695697
this.removeListener('newListener',keylogNewListener);
@@ -733,7 +735,9 @@ TLSSocket.prototype._init = function(socket, wrap){
733735
if(event!=='session')
734736
return;
735737

736-
ssl.enableSessionCallbacks();
738+
// Guard against enableSessionCallbacks after destroy
739+
if(!this._handle)return;
740+
this._handle.enableSessionCallbacks();
737741

738742
// Remove this listener since it's no longer needed.
739743
this.removeListener('newListener',newListener);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
constcommon=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+
consttls=require('tls');
12+
consttlsSocketKeyLog=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+
consttlsSocketSession=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)