Skip to content

Commit dd0f515

Browse files
author
Yury Kirsanov
committed
clusterer_controller: guard the zero-length cipherstate passthrough
cl_ctr_send_join_req_pkt() sends Noise msg 1 with an empty authenticated payload, passing payload = NULL, pl_len = 0. That reaches the no-key passthrough in cl_ctr_cs_encrypt() as memcpy(ct, NULL, 0). memcpy() declares both pointers nonnull, and passing NULL is undefined even when the length is zero, so gcc-13 at -O9 inlines the whole chain (send_join_req_pkt -> noise_write1 -> ss_encrypt_hash -> cs_encrypt), proves the argument NULL and fails the build under -Werror=nonnull. Skip the copy when there is nothing to copy, in both cs_encrypt() and its cs_decrypt() counterpart, which has the same latent problem. No behaviour change: the branch is only reachable before a key is mixed in, and a zero-length copy moved no bytes anyway.
1 parent 62d44b8 commit dd0f515

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

modules/clusterer_controller/clusterer_controller.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,7 +1926,9 @@ static int cl_ctr_cs_encrypt(cl_ctr_cipherstate_t *cs, const unsigned char *ad,
19261926
const unsigned char *pt, size_t pt_len, unsigned char *ct)
19271927
{
19281928
unsigned char nonce[12]; unsigned long long clen = 0;
1929-
if (!cs->has_key) { memcpy(ct, pt, pt_len); return (int)pt_len; }
1929+
/* pt may legitimately be NULL for an empty payload (Noise msg 1 sends
1930+
* one); memcpy() is nonnull even for a zero length, so guard it. */
1931+
if (!cs->has_key) { if (pt_len) memcpy(ct, pt, pt_len); return (int)pt_len; }
19301932
cl_ctr_cs_nonce(cs->n, nonce);
19311933
crypto_aead_chacha20poly1305_ietf_encrypt(ct, &clen, pt, pt_len,
19321934
ad, ad_len, NULL, nonce, cs->k);
@@ -1937,7 +1939,7 @@ static int cl_ctr_cs_decrypt(cl_ctr_cipherstate_t *cs, const unsigned char *ad,
19371939
const unsigned char *ct, size_t ct_len, unsigned char *pt)
19381940
{
19391941
unsigned char nonce[12]; unsigned long long plen = 0;
1940-
if (!cs->has_key) { memcpy(pt, ct, ct_len); return (int)ct_len; }
1942+
if (!cs->has_key) { if (ct_len) memcpy(pt, ct, ct_len); return (int)ct_len; }
19411943
cl_ctr_cs_nonce(cs->n, nonce);
19421944
if (crypto_aead_chacha20poly1305_ietf_decrypt(pt, &plen, NULL, ct, ct_len,
19431945
ad, ad_len, nonce, cs->k) != 0)

0 commit comments

Comments
 (0)