Skip to content

CSPRNG.__cinit__: set self.ctx = NULL after freeing on init failure - #10648

Merged
ThomasWaldmann merged 1 commit into
borgbackup:masterfrom
underiya-ai:fix-csprng-double-free
Nov 28, 2026
Merged

ThomasWaldmann merged 1 commit into
borgbackup:masterfrom
underiya-ai:fix-csprng-double-free

Conversation

@underiya-ai

Copy link
Copy Markdown
Contributor

What does this PR fix?

In CSPRNG.__cinit__ (src/borg/crypto/low_level.pyx), if EVP_EncryptInit_ex fails, EVP_CIPHER_CTX_free(self.ctx) is called before raising CryptoError, without resetting self.ctx = NULL.

Because Cython automatically invokes __dealloc__ when __cinit__ raises an exception—and __dealloc__ frees self.ctx whenever self.ctx != NULL—this results in a double-free of the EVP_CIPHER_CTX pointer.

Solution

Set self.ctx = NULL immediately after calling EVP_CIPHER_CTX_free(self.ctx) on the error path in CSPRNG.__cinit__, matching the invariant in CSPRNG.__dealloc__.

@ThomasWaldmann

Copy link
Copy Markdown
Member

@underiya-ai Thanks for finding/fixing this!

@codecov

codecov Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.47%. Comparing base (6e04ded) to head (06e0482).
⚠️ Report is 1 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #10648   +/-   ##
=======================================
  Coverage   89.46%   89.47%           
=======================================
  Files         105      105           
  Lines       20343    20343           
  Branches     3199     3199           
=======================================
+ Hits        18200    18201    +1     
- Misses       1488     1489    +1     
+ Partials      655      653    -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@ThomasWaldmann
ThomasWaldmann merged commit 6e9f952 into borgbackup:master Nov 28, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants