Skip to content

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

Open
omghante wants to merge 1 commit into
nodejs:mainfrom
omghante:fix/56926-managed-x509-double-free
Open

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

Conversation

@omghante

@omghante omghante commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

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
@omghante
omghante force-pushed the fix/56926-managed-x509-double-free branch from 49fa1be to 9da6f80 Compare April 14, 2026 23:23
@codecov

codecov Bot commented Apr 15, 2026

Copy link
Copy Markdown

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).
⚠️ Report is 1511 commits behind head on main.

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.

@omghante

omghante commented May 1, 2026

Copy link
Copy Markdown
Contributor Author

Gentle ping - this PR has been open for ~3 weeks without review.

This fixes the Coverity 367349 use-after-free risk in ManagedX509::operator=
(#56926) by ensuring X509_up_ref runs before cert_.reset(), with a
self-assignment guard. It's a minimal, 1-file, 6-line net change.

CI is green and the fix is ready for review. Would anyone from the crypto
team be able to take a look?

@targos (as the issue author) @tniessen @panva would appreciate your
eyes on this when you get a chance. Thank you!

@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

This pull request has been marked as stale due to 90 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

@github-actions github-actions Bot added the stale label Aug 3, 2026
@omghante

omghante commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Keep this open

@github-actions github-actions Bot removed the stale label Aug 4, 2026
@trivikr trivikr added request-ci Add this label to start a Jenkins CI on a PR. author ready PRs that have at least one approval, no outstanding review comments, and a CI started. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Aug 22, 2026
@trivikr

trivikr commented Aug 22, 2026

Copy link
Copy Markdown
Member

@omghante Can you please rebase from main?

@trivikr trivikr removed the author ready PRs that have at least one approval, no outstanding review comments, and a CI started. label Aug 23, 2026
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

4 participants