Harden HMAC error paths: fix resource leaks, state bugs, and missing cleansing#3081
Merged
Harden HMAC error paths: fix resource leaks, state bugs, and missing cleansing#3081
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3081 +/- ##
==========================================
- Coverage 78.16% 78.15% -0.01%
==========================================
Files 689 689
Lines 121628 121644 +16
Branches 16981 16987 +6
==========================================
+ Hits 95070 95072 +2
- Misses 25675 25688 +13
- Partials 883 884 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
sgmenda
previously approved these changes
Mar 10, 2026
82667aa to
243ead0
Compare
nebeid
added a commit
to awslabs/aws-lc-verification
that referenced
this pull request
Mar 12, 2026
AWS-LC PR: aws/aws-lc#3081 The HMAC hardening PR adds OPENSSL_cleanse(tmp, sizeof(tmp)) to HMAC_Final's exit path. Add the existing OPENSSL_cleanse_ov override to both HMAC_Final verification calls so SAW can reason about the new memory write.
nebeid
added a commit
to awslabs/aws-lc-verification
that referenced
this pull request
Mar 12, 2026
AWS-LC PR: aws/aws-lc#3081 The HMAC hardening PR adds OPENSSL_cleanse(tmp, sizeof(tmp)) to HMAC_Final's exit path. Add the existing OPENSSL_cleanse_ov override to both HMAC_Final verification calls so SAW can reason about the new memory write.
nebeid
approved these changes
Mar 12, 2026
geedo0
approved these changes
Mar 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of changes:
Tightens up error handling across the HMAC implementation:
p_hmac.c:hmac_copyerror paths were leaking the HMAC context and key, and leftdst->dataas a dangling pointer.hmac_cleanupdidn't guard against NULL and wasn't cleansing the embedded HMAC_CTX.hmac.c(HMAC_Init_ex): Re-initializing a context that was in theIN_PROGRESSstate (same key/md, no-op path) resetmd_ctxbut forgot to transition the state back toINIT_NO_DATA.hmac.c(HMAC_Final): Sensitive intermediate inner hash (tmp) wasn't being cleansed on exit. On error, the context was left in a partially-modified state instead of being cleaned up.hmac.c(HMAC_get_precomputed_key): Invariant checks relied solely onassert(), meaning they silently disappeared in release builds. Replaced with real error returns +OPENSSL_cleanseof partial output.hmac.c(HMAC_Init): Negativekey_len(legacyintAPI) would implicitly convert to a hugesize_t, causing a buffer over-read. Added a bounds check and explicit cast.HMAC_with_precompute: Added the sameout == NULLearly-return thatHMAC()already had.Call-outs:
HMAC_Init_exstate fix is the subtlest change — the no-op re-init path was missing the state transition, which could cause downstream logic that checksstateto misbehave.HMAC_Finalnow callsHMAC_CTX_cleanup(ctx)on error, which zeros the entire context. This is more aggressive than before (caller must re-init to reuse), but the success path already required re-init (READY_NEEDS_INIT), so this should be fine in practice.HMAC_get_precomputed_keychange turns debug-only asserts into real error returns. Worth confirming we're comfortable returning 0 here even though "this should never happen."Testing:
Existing HMAC test suite. No new tests added — all changes are hardening of error/edge-case paths that existing tests exercise implicitly through the normal Init → Update → Final flow.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.