mem: use wally_memcpy for more secret/potentially secret data
What changed, and why it matters
This commit replaces ordinary memory-copy operations with a special secure-copy function (wally_memcpy) in a handful of places that handle private keys, BIP32 extended keys, and descriptor template secrets. The goal is to reduce the chance that sensitive data is left behind in CPU registers or temporary compiler-generated storage. It is a hardening change rather than a fix for a specific, exploitable bug, and the commit message does not claim it resolves a known vulnerability.
Treat as a routine security-hardening commit. Review whether any other secret-bearing memcpy sites remain unconverted, and ensure wally_memcpy is available and correctly implemented on all supported platforms. No urgent patch or incident response is indicated by the diff alone.
Security signals we found
secret-data handling (private keys, BIP32 extended keys, SLIP77 blinding key)
use of hardened memory-copy primitive (wally_memcpy)
defensive hardening against compiler optimization of sensitive copies
no explicit vulnerability or exploit path described in commit
Evidence from the diff
The patch changes eight call sites in descriptor.c, map.c, and psbt.c from memcpy() to wally_memcpy(). Affected data includes: descriptor variable substitutions and private-key output buffers (KIND_PRIVATE_KEY, SLIP77 blinding key), BIP32 keypath derivation results and allocated key copies, and the ECDSA signing-key extraction step in PSBT signing. wally_memcpy is the project’s constant-time-ish/secure copy helper intended to avoid compiler optimizations that could leave secret material in registers or elide zeroization. The change is defensive: no buffer overflow, use-after-free, or logic flaw is corrected, and no CVE or advisory is referenced.
Changed components
src/descriptor.csrc/map.csrc/psbt.cwally_memcpyBIP32 key derivation and keypath lookupPSBT ECDSA signing-key preparationdescriptor private-key / SLIP77 blinding-key outputInspect captured patch +8 / −8
### src/descriptor.c
@@ -608,13 +608,13 @@ static int canonicalize_impl(const char *descriptor,
const bool is_number = *start >= '0' && *start <= '9';
size_t lookup_len = p - start;
if (!vars_in || lookup_len > VAR_MAX_NAME_LEN || is_number)
- memcpy(out, start, lookup_len);
+ wally_memcpy(out, start, lookup_len);
else {
/* Lookup the potential identifier */
const struct wally_map_item *item;
item = wally_map_get(vars_in, (unsigned char*)start, lookup_len);
lookup_len = item ? item->value_len : lookup_len;
- memcpy(out, item ? (char *)item->value : start, lookup_len);
+ wally_memcpy(out, item ? (char *)item->value : start, lookup_len);
if (item && flags & WALLY_MINISCRIPT_POLICY_TEMPLATE) {
if (p[0] == '/' && p[1] == '*' && p[2] == '*') {
out += lookup_len;
@@ -751,7 +751,7 @@ static int node_derive_key(ms_ctx* ctx, const ms_node* node,
} else if (node->kind == KIND_PRIVATE_KEY) {
int ret;
if (!(flags & BIP32_FLAG_KEY_PUBLIC)) {
- memcpy(output->priv_key + 1, data, sizeof(output->priv_key) - 1);
+ wally_memcpy(output->priv_key + 1, data, sizeof(output->priv_key) - 1);
return WALLY_OK;
}
ret = wally_ec_public_key_from_private_key(data, node->data_len,
@@ -762,7 +762,7 @@ static int node_derive_key(ms_ctx* ctx, const ms_node* node,
} else if (node->kind == KIND_RAW && node->parent &&
node->parent->kind == KIND_DESCRIPTOR_SLIP77) {
/* SLIP77 blinding key is returned as a private key */
- memcpy(output->priv_key + 1, data, sizeof(output->priv_key) - 1);
+ wally_memcpy(output->priv_key + 1, data, sizeof(output->priv_key) - 1);
return WALLY_OK;
} else if ((node->kind & KIND_BIP32) == KIND_BIP32) {
int ret = bip32_key_from_base58_n(node->data, node->data_len, output);
### src/map.c
@@ -510,7 +510,7 @@ int map_keypath_get_bip32_key_from(const struct wally_map *map_in,
if (path_len + hdkey->depth > BIP32_PATH_MAX_LEN)
continue; /* Path too long, cannot be this key */
if (!path_len)
- memcpy(&derived, hdkey, sizeof(derived)); /* Use directly */
+ wally_memcpy(&derived, hdkey, sizeof(derived)); /* Use directly */
else {
/* Derive the key to use */
ret = bip32_key_from_parent_path(hdkey, path, path_len,
@@ -533,7 +533,7 @@ int map_keypath_get_bip32_key_from(const struct wally_map *map_in,
if (ret == WALLY_OK && idx) {
/* Found, return the matching key and its 1-based index */
*written = idx;
- memcpy(output, hdkey, sizeof(*hdkey));
+ wally_memcpy(output, hdkey, sizeof(*hdkey));
}
wally_clear(&derived, sizeof(derived));
return ret;
@@ -570,7 +570,7 @@ int wally_map_keypath_get_bip32_key_from_alloc(const struct wally_map *map_in,
if (!(*output = wally_calloc(sizeof(struct ext_key))))
ret = WALLY_ENOMEM;
else
- memcpy(*output, &found, sizeof(found));
+ wally_memcpy(*output, &found, sizeof(found));
}
wally_clear(&found, sizeof(found));
return ret;
### src/psbt.c
@@ -4864,7 +4864,7 @@ int wally_psbt_sign_input_bip32(struct wally_psbt *psbt,
if (!is_taproot) {
/* ECDSA: Use untweaked private key. Only grinding flag is relevant */
- memcpy(signing_key, hdkey->priv_key + 1, EC_PRIVATE_KEY_LEN);
+ wally_memcpy(signing_key, hdkey->priv_key + 1, EC_PRIVATE_KEY_LEN);
flags = EC_FLAG_ECDSA | (flags & EC_FLAG_GRIND_R);
} else {
/* Schnorr BIP340: Tweak the private key */Why this scored 49/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.