Skip to content

crypto: fix use-after-free risk in ManagedX509 assignment#62742

Open
om-ghante wants to merge 1 commit intonodejs:mainfrom
om-ghante:fix/56926-managed-x509-double-free
Open

crypto: fix use-after-free risk in ManagedX509 assignment#62742
om-ghante wants to merge 1 commit intonodejs:mainfrom
om-ghante:fix/56926-managed-x509-double-free

Conversation

@om-ghante
Copy link
Copy Markdown
Contributor

@om-ghante om-ghante commented Apr 14, 2026

Description

This PR resolves a potential double-free/use-after-free vulnerability identified by Coverity (Issue 367349) within ManagedX509::operator=.

Previously, cert_.reset(that.get()) was destructing the underlying X509 structure prior to correctly tracking the reference count out-of-band via X509_up_ref(cert_.get()). If multiple ManagedX509 instances mistakenly managed the same underlying raw pointer, it could cause the OpenSSL backing object's internal reference count to reach zero and prematurely free the memory before up_ref executes.

The refactored logic now ensures strict adherence to OpenSSL smart pointer semantics:

  • Added a self-assignment check to return early.
  • Guarantees X509_up_ref(cert) increments the reference limits first before the object acts on .reset(), strictly guaranteeing the memory boundary.

Fixes: #56926

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. crypto Issues and PRs related to the crypto subsystem. needs-ci PRs that need a full CI run. labels Apr 14, 2026
Fixes a potential double-free issue where ManagedX509::operator=
resets the underlying smart pointer using a raw pointer from another
instance before incrementing the reference count. If both instances
were managing the same underlying OpenSSL object, the reset could
decrement the reference count to 0 and free the object before the
reference count could be incremented.

This fixes Coverity issue 367349 where different smart pointers
were seemingly managing the same raw pointer.

Fixes: nodejs#56926
@om-ghante om-ghante force-pushed the fix/56926-managed-x509-double-free branch from 49fa1be to 9da6f80 Compare April 14, 2026 23:23
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 15, 2026

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.69%. Comparing base (ed05549) to head (9da6f80).

Files with missing lines Patch % Lines
src/crypto/crypto_x509.cc 0.00% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62742      +/-   ##
==========================================
- Coverage   89.69%   89.69%   -0.01%     
==========================================
  Files         706      706              
  Lines      218127   218130       +3     
  Branches    41734    41739       +5     
==========================================
- Hits       195651   195641      -10     
- Misses      14400    14419      +19     
+ Partials     8076     8070       -6     
Files with missing lines Coverage Δ
src/crypto/crypto_x509.cc 72.13% <0.00%> (-0.32%) ⬇️

... and 47 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. crypto Issues and PRs related to the crypto subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Coverity issue 367349: Different smart pointers managing same raw pointer

2 participants