crypto: fix handling of null BUF_MEM* in ToV8Value()

The assignment to `bptr` calls `BIO_get_mem_ptr` which can fail and
leave the `bptr` as nullptr. This then later causes a null pointer
deref.
This is inconsistent with uses of the similar function
`BIO_get_mem_data` that do check its return value, e.g.
`node::crypto::X509sToArrayOfStrings()`.
Solve it by checking for a null pointer and handling the `Nothing`
return value at the call sites.

PR-URL: https://github.com/nodejs/node/pull/61885
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Nora Dossche authored and GitHub committed 2026-02-25 13:56:28 +00:00
1 parent e0375be3cf
commit 488a854cf5
2 files changed
+14 -1

No files matched your search

+8 -1
View File
@@ -86,8 +86,15 @@ MaybeLocal<Value> ToV8Value(
Environment* env,
const BIOPointer& bio,
const EVPKeyPointer::AsymmetricKeyEncodingConfig& config) {
if (!bio) return {};
if (!bio) {
THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Invalid BIO pointer");
return {};
}
BUF_MEM* bptr = bio;
if (!bptr) {
THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Unable to create BUF_MEM pointer");
return {};
}
if (config.format == EVPKeyPointer::PKFormatType::PEM) {
// PEM is an ASCII format, so we will return it as a string.
return String::NewFromUtf8(
+6
View File
@@ -106,6 +106,8 @@ MaybeLocal<Value> ToV8Value(Local<Context> context, BIOPointer&& bio) {
if (!bio) [[unlikely]]
return {};
BUF_MEM* mem = bio;
if (!mem) [[unlikely]]
return {};
Local<Value> ret;
if (!String::NewFromUtf8(Isolate::GetCurrent(),
mem->data,
@@ -120,6 +122,8 @@ MaybeLocal<Value> ToV8Value(Local<Context> context, const BIOPointer& bio) {
if (!bio) [[unlikely]]
return {};
BUF_MEM* mem = bio;
if (!mem) [[unlikely]]
return {};
Local<Value> ret;
if (!String::NewFromUtf8(Isolate::GetCurrent(),
mem->data,
@@ -134,6 +138,8 @@ MaybeLocal<Value> ToBuffer(Environment* env, BIOPointer* bio) {
if (bio == nullptr || !*bio) [[unlikely]]
return {};
BUF_MEM* mem = *bio;
if (!mem) [[unlikely]]
return {};
#ifdef V8_ENABLE_SANDBOX
// If the v8 sandbox is enabled, then all array buffers must be allocated
// via the isolate. External buffers are not allowed. So, instead of wrapping