fix: ensure key material is cleared in error paths#1443
Open
jgowdy-godaddy wants to merge 2 commits intomainfrom
Open
fix: ensure key material is cleared in error paths#1443jgowdy-godaddy wants to merge 2 commits intomainfrom
jgowdy-godaddy wants to merge 2 commits intomainfrom
Conversation
aka-bo
requested changes
Aug 12, 2025
Contributor
There was a problem hiding this comment.
This file should be removed.
Contributor
There was a problem hiding this comment.
This file should be removed.
Contributor
There was a problem hiding this comment.
This file should be removed.
Comment on lines
+288
to
+292
| defer func() { | ||
| if output.Plaintext != nil { | ||
| clear(output.Plaintext) | ||
| } | ||
| }() |
Contributor
There was a problem hiding this comment.
No need for a nil check. Per the language spec clear is a no-op if the slice is nil.
Suggested change
| defer func() { | |
| if output.Plaintext != nil { | |
| clear(output.Plaintext) | |
| } | |
| }() | |
| defer clear(output.Plaintext) |
Comment on lines
+192
to
+196
| defer func() { | ||
| if resp.Plaintext != nil { | ||
| clear(resp.Plaintext) | ||
| } | ||
| }() |
Contributor
There was a problem hiding this comment.
No need for a nil check. Per the language spec clear is a no-op if the slice is nil.
Suggested change
| defer func() { | |
| if resp.Plaintext != nil { | |
| clear(resp.Plaintext) | |
| } | |
| }() | |
| defer clear(resp.Plaintext) |
This commit fixes security vulnerabilities where sensitive key material could remain in memory after errors: 1. envelope.go: Fix decryptRow to clear rawDrk buffer even on error - Previously, early return on error skipped the defer statement - Now uses defer with nil check to ensure cleanup 2. AWS KMS plugins: Clear plaintext keys on decrypt errors - Both v1 and v2 implementations now clear resp.Plaintext/output.Plaintext - Uses closure pattern to ensure cleanup even when continuing to next region - Prevents key material from failed regions remaining in memory These changes are critical for security as they prevent sensitive cryptographic material from persisting in memory after failures, reducing the attack surface for memory disclosure vulnerabilities. All changes use Go's built-in clear() function for secure memory wiping.
- Remove extra whitespace that was causing gci formatting errors - Apply gofumpt formatting to ensure consistent code style
7e1fe86 to
24c4f2b
Compare
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.
Summary
clear()functionThe Problem: Key Material Not Being Wiped
1. envelope.go - Memory Leak in decryptRow
Before (VULNERABLE):
Why it's vulnerable: When
crypto.Decryptfails, the function returns immediately. Thedeferstatement is never reached, sorawDrk(which may contain partial key material from failed decryption) is NEVER cleared from memory.After (SECURE):
How it's fixed: The
deferis now placed immediately after we haverawDrk, ensuring it will execute regardless of any subsequent errors.2. AWS KMS - Multi-Region Retry Memory Accumulation
Before (VULNERABLE):
Why it's vulnerable: In a multi-region setup, if region 1 succeeds in KMS decryption but fails in the subsequent crypto operation,
resp.Plaintextcontains the master key but is never cleared. Then we try region 2, potentially accumulating multiple plaintext keys in memory.After (SECURE):
How it's fixed: Each iteration now uses a closure with defer to ensure
resp.Plaintextis cleared before trying the next region. This prevents accumulation of key material across retry attempts.Security Impact
What Could Happen Before These Changes:
What Happens After These Changes:
clear()), ensures proper memory wipingTesting
defer func() { clear(buffer) }()is guaranteed to executeRelated PRs
clear()functionTogether, these PRs fix critical security vulnerabilities in the Asherah Go implementation's memory management.