Skip to content

Commit e431a20

Browse files
committed
tls: zero SSL_CTX freelist for a singleUse socket
When connecting to server with `keepAlive` turned off - make sure that the read/write buffers won't be kept in a single use SSL_CTX instance after the socket will be destroyed. Fix: #1522
1 parent 5bd62bc commit e431a20

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

lib/_tls_common.js

+4
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ exports.createSecureContext = function createSecureContext(options, context) {
133133
}
134134
}
135135

136+
// Do not keep read/write buffers in free list
137+
if (options.singleUse)
138+
c.context.setFreeListLength(0);
139+
136140
return c;
137141
};
138142

lib/_tls_wrap.js

+2
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,8 @@ exports.connect = function(/* [port, host], options, cb */) {
862862
};
863863

864864
options = util._extend(defaults, options || {});
865+
if (options.keepAlive === false)
866+
options.singleUse = true;
865867

866868
assert(typeof options.checkServerIdentity === 'function');
867869

src/node_crypto.cc

+8
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ void SecureContext::Initialize(Environment* env, Handle<Object> target) {
264264
env->SetProtoMethod(t, "loadPKCS12", SecureContext::LoadPKCS12);
265265
env->SetProtoMethod(t, "getTicketKeys", SecureContext::GetTicketKeys);
266266
env->SetProtoMethod(t, "setTicketKeys", SecureContext::SetTicketKeys);
267+
env->SetProtoMethod(t, "setFreeListLength", SecureContext::SetFreeListLength);
267268
env->SetProtoMethod(t, "getCertificate", SecureContext::GetCertificate<true>);
268269
env->SetProtoMethod(t, "getIssuer", SecureContext::GetCertificate<false>);
269270

@@ -932,6 +933,13 @@ void SecureContext::SetTicketKeys(const FunctionCallbackInfo<Value>& args) {
932933
}
933934

934935

936+
void SecureContext::SetFreeListLength(const FunctionCallbackInfo<Value>& args) {
937+
SecureContext* wrap = Unwrap<SecureContext>(args.Holder());
938+
939+
wrap->ctx_->freelist_max_len = args[0]->Int32Value();
940+
}
941+
942+
935943
void SecureContext::CtxGetter(Local<String> property,
936944
const PropertyCallbackInfo<Value>& info) {
937945
HandleScope scope(info.GetIsolate());

src/node_crypto.h

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class SecureContext : public BaseObject {
8585
static void LoadPKCS12(const v8::FunctionCallbackInfo<v8::Value>& args);
8686
static void GetTicketKeys(const v8::FunctionCallbackInfo<v8::Value>& args);
8787
static void SetTicketKeys(const v8::FunctionCallbackInfo<v8::Value>& args);
88+
static void SetFreeListLength(
89+
const v8::FunctionCallbackInfo<v8::Value>& args);
8890
static void CtxGetter(v8::Local<v8::String> property,
8991
const v8::PropertyCallbackInfo<v8::Value>& info);
9092

0 commit comments

Comments
 (0)