blech32: ensure buffers are cleared if encoding fails
What changed, and why it matters
This commit fixes a small cleanup bug in the confidential address encoding function. Previously, if the final blech32 encoding step failed, the function returned an error immediately without jumping to the shared cleanup code. That meant temporary buffers holding sensitive data (like the public key and hash) might not be securely wiped from memory. The change makes sure cleanup always runs, even on failure, so secrets are cleared properly.
Treat as a low-severity hardening fix. Backport if the library is used in long-lived processes where stack memory disclosure is a concern. No immediate active exploitation path is evident from the diff alone.
Security signals we found
Sensitive buffer not cleared on error path
Use of goto for centralized secure cleanup
Confidential address encoding touches EC public key and witness hash
Reported-by line credits external reporter
Evidence from the diff
In wally_confidential_addr_from_addr_segwit(), a failure of blech32_addr_encode() previously returned WALLY_ERROR directly, bypassing the done: cleanup label. The patch changes this to set ret = WALLY_ERROR and goto done, ensuring that the local stack buffer ‘buf’ (containing pub_key and witness program data) is zeroed via the cleanup path. This is a defense-in-depth fix against possible information disclosure of key material through uninitialized or residual stack contents.
Changed components
src/blech32.cwally_confidential_addr_from_addr_segwit()Inspect captured patch +4 / −2
diff --git a/src/blech32.c b/src/blech32.c
index 2ea1891..4ee8aa4 100644
--- a/src/blech32.c
+++ b/src/blech32.c
@@ -321,8 +321,10 @@ int wally_confidential_addr_from_addr_segwit(
memcpy(buf, pub_key, pub_key_len);
written -= 2; /* ignore witnessVersion & hashSize */
written += EC_PUBLIC_KEY_LEN;
- if (!blech32_addr_encode(result, confidential_addr_family, witver & 0xff, buf, written))
- return WALLY_ERROR;
+ if (!blech32_addr_encode(result, confidential_addr_family, witver & 0xff, buf, written)) {
+ ret = WALLY_ERROR;
+ goto done;
+ }
*output = wally_strdup(result);
ret = (*output) ? WALLY_OK : WALLY_ENOMEM;
Why this scored 42/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.