Skip to content

Commit fbf7f7a

Browse files
committed
crypto: Fix potential null pointer dereference when BIO_meth_new() fails
This function can return null, which will make the calls to BIO_meth_set_* trigger a null deref. Even after fixing this, there is an issue with the `BIOPointer::New(GetMethod())` call in `NodeBIO::New` because the `New` method cannot handle a null pointer despite other code already guarding for this (e.g. the `NodeBIO::New` function already checks `bio`). This patch solves the issues by adding more null checks.
1 parent 4a13a62 commit fbf7f7a

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

deps/ncrypto/ncrypto.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,7 @@ BIOPointer BIOPointer::NewSecMem() {
14701470
}
14711471

14721472
BIOPointer BIOPointer::New(const BIO_METHOD* method) {
1473+
if (method == nullptr) return {};
14731474
return BIOPointer(BIO_new(method));
14741475
}
14751476

src/crypto/crypto_bio.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,15 @@ const BIO_METHOD* NodeBIO::GetMethod() {
226226
// Static initialization ensures that this is safe to use concurrently.
227227
static const BIO_METHOD* method = [&]() {
228228
BIO_METHOD* method = BIO_meth_new(BIO_TYPE_MEM, "node.js SSL buffer");
229-
BIO_meth_set_write(method, Write);
230-
BIO_meth_set_read(method, Read);
231-
BIO_meth_set_puts(method, Puts);
232-
BIO_meth_set_gets(method, Gets);
233-
BIO_meth_set_ctrl(method, Ctrl);
234-
BIO_meth_set_create(method, New);
235-
BIO_meth_set_destroy(method, Free);
229+
if (method != nullptr) {
230+
BIO_meth_set_write(method, Write);
231+
BIO_meth_set_read(method, Read);
232+
BIO_meth_set_puts(method, Puts);
233+
BIO_meth_set_gets(method, Gets);
234+
BIO_meth_set_ctrl(method, Ctrl);
235+
BIO_meth_set_create(method, New);
236+
BIO_meth_set_destroy(method, Free);
237+
}
236238
return method;
237239
}();
238240

0 commit comments

Comments
 (0)