Skip to content

tpm2: fix EVP_PKEY/EVP_PKEY_CTX leak in CryptRsaEncrypt - #652

Merged
stefanberger merged 1 commit into
stefanberger:masterfrom
sam-kota:cryptrsaencrypt-evp-leak
Oct 30, 2026
Merged

stefanberger merged 1 commit into
stefanberger:masterfrom
sam-kota:cryptrsaencrypt-evp-leak

Conversation

@sam-kota

@sam-kota sam-kota commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

the no-unpadded-encryption / fips-host guard in the TPM_ALG_NULL case returns TPM_RC_SCHEME directly after pkey and ctx are already allocated, so it skips the Exit: label and leaks an EVP_PKEY + EVP_PKEY_CTX on every TPM2_RSA_Encrypt(scheme=NULL); switched it to ERROR_EXIT like the same guard in CryptRsaDecrypt.

verified with a small harness that drives the real CryptRsaEncrypt under a no-unpadded-encryption profile and counts live OpenSSL allocations: ~11 leaked per call before, 0 after, same TPM_RC_SCHEME return either way. make check stays green (the fuzz state round-trip corpus included).

Summary by CodeRabbit

  • Bug Fixes
    • RSA encryption now consistently reports a scheme error when the active runtime profile disallows unpadded encryption with a null scheme. This makes the reported result consistent with the profile’s encryption restrictions.

When the runtime profile requires NO_UNPADDED_ENCRYPTION (no-unpadded-encryption
or fips-host), the TPM_ALG_NULL case returned TPM_RC_SCHEME directly after pkey
and ctx were already allocated, skipping the Exit label that frees them. Each
TPM2_RSA_Encrypt with scheme TPM_ALG_NULL then leaks an EVP_PKEY and an
EVP_PKEY_CTX. Use ERROR_EXIT like the matching guard in CryptRsaDecrypt.

Signed-off-by: sameera <sameera@research.itecz.au>
@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4be5cf2c-41c4-4c36-b4ee-3c11995f7d90
Commits

Reviewing files that changed from the base of the PR and between a0f832a and 30eceb1.

Files selected for processing: 1
  • src/tpm2/TPMCmd/tpm/src/crypt/CryptRsa.c

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Walkthrough

The OpenSSL-backed CryptRsaEncrypt function now routes a disallowed unpadded-encryption case through its existing cleanup path before returning TPM_RC_SCHEME.

Changes: RSA encryption error handling

Layer / File(s) Summary
Raw RSA error path
src/tpm2/TPMCmd/tpm/src/crypt/CryptRsa.c
When the runtime profile disallows unpadded encryption for TPM_ALG_NULL, the function now uses ERROR_EXIT(TPM_RC_SCHEME) instead of returning directly.

Priority: Low

Estimated code review effort: 2 (Simple) | ~5 minutes

Change: Bug fix

Suggested reviewers: stefanberger

Merge Risk: Minimal · up to 30ece

No actionable merge-blocking risk remains in the reviewed change.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the primary change: fixing an EVP_PKEY/EVP_PKEY_CTX leak in CryptRsaEncrypt. The raw summary says the TPM_ALG_NULL guard now uses ERROR_EXIT instead of returning TPM_RC_SCHEME directly…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the primary change: fixing an EVP_PKEY/EVP_PKEY_CTX leak in CryptRsaEncrypt. The raw summary says the TPM_ALG_NULL guard now uses ERROR_EXIT instead of returning TPM_RC_SCHEME directly, so the error path reaches the existing cleanup. The title matches this change and the PR objectives.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 1 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@stefanberger
stefanberger merged commit e396ee2 into stefanberger:master Oct 30, 2026
5 checks passed
@stefanberger

Copy link
Copy Markdown
Owner

Thanks!

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