You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
valgrind: Conditional jump or move depends on uninitialised value(s) #19719
When using AES GCM we get a valgrind warning from inside the OpenSSL code.
The warning is NOT present in OpenSSL 1.1.1
Debug build: No warning
Release build: Warning
==283064== Conditional jump or move depends on uninitialised value(s)
==283064== at 0x5058AD5: gcm_cipher_internal (ciphercommon_gcm.c:425)
==283064== by 0x5058AD5: ossl_gcm_stream_final (ciphercommon_gcm.c:327)
==283064== by 0x4F1619E: EVP_DecryptFinal_ex (evp_enc.c:917)
==283064== by 0x48E7142: aes_authenticate (in /home/alfredh/tmp/re/libre.so.10.9.0)
==283064== by 0x11BF00: test_aes_gcm (aes.c:342)
==283064== by 0x1587E9: test_unit (test.c:477)
==283064== by 0x158FF3: test_reg (test.c:711)
==283064== by 0x13827F: main (main.c:233)
==283064==
Can you point to the code? I can not find any "test_aes_gcm" in that repository, and line numbers seem to be larger that the file is big. Running valgrind on OpenSSL's test suite does not generate any warnings.
We would need some more concise testcase to investigate further. Or at least if you could investigate what is the concrete variable that is uninitialized.
Try putting a printf above line 425 to see what the parameters are, this may give you some indication of what parameter is not set up. My guess is it passes in an uninited parameter from the test.
#include<string.h>#include<stdint.h>#include<stdbool.h>#include<openssl/evp.h>#include<openssl/err.h>/* * Test program to demonstrate valgrind warning * with OpenSSL 3.x.x and AES-GCM on Linux * * Build: * * gcc -Iopenssl/include -Lopenssl -Wall -O2 aes_gcm.c -lcrypto * * * Info: * * gcc (Debian 10.2.1-6) 10.2.1 20210110 * Debian clang version 11.0.1-2 * * O0: no warning * O3: warning * * No AAD: no warning * AAD: warning * * gcc-9: warning * gcc-10: warning * clang: warning */#defineKEY_LEN 32 /* 256 bits */#defineIV_LEN 12 /* 96 bits */#defineMSG_LEN 16 /* 128 bits */#defineAAD_LEN 16 /* 128 bits */#defineTAG_LEN 16 /* 128 bits */staticconstuint8_ttest_vector[MSG_LEN] ="\x2d\x71\xbc\xfa\x91\x4e\x4a\xc0\x45\xb2\xaa\x60\x95\x5f\xad\x24";
staticconstuint8_tencr_key[KEY_LEN] ="\x92\xe1\x1d\xcd\xaa\x86\x6f\x5c\xe7\x90\xfd\x24\x50\x1f\x92\x50""\x9a\xac\xf4\xcb\x8b\x13\x39\xd5\x0c\x9c\x12\x40\x93\x5d\xd0\x8b";
staticconstuint8_taad[AAD_LEN] ="\x1e\x08\x89\x01\x6f\x67\x60\x1c\x8e\xbe\xa4\x94\x3b\xc2\x3a\xd6";
staticconstuint8_tiv[IV_LEN] ="\xac\x93\xa1\xa6\x14\x52""\x99\xbd\xe9\x02\xf2\x1a";
staticconstuint8_ttag_ref[TAG_LEN] ="\xec\xa5\xaa\x77\xd5\x1d\x4a\x0a\x14\xd9\xc5\x1e\x1d\xa4\x74\xab";
staticconstuint8_tcipher_text[MSG_LEN] ="\x89\x95\xae\x2e\x6d\xf3\xdb\xf9\x6f\xac\x7b\x71\x37\xba\xe6\x7f";
structaes_state {
EVP_CIPHER_CTX*ctx;
};
staticintaes_alloc(structaes_state*st, constuint8_t*key,
constuint8_t*iv, boolencr)
{
intr;
memset(st, 0, sizeof(*st));
constEVP_CIPHER*cipher=EVP_aes_256_gcm();
if (!cipher)
returnENOTSUP;
st->ctx=EVP_CIPHER_CTX_new();
if (!st->ctx) {
ERR_clear_error();
returnENOMEM;
}
if (encr) {
r=EVP_EncryptInit_ex(st->ctx, cipher, NULL, key, iv);
}
else {
r=EVP_DecryptInit_ex(st->ctx, cipher, NULL, key, iv);
}
if (!r) {
ERR_clear_error();
returnEPROTO;
}
return0;
}
staticvoidaes_reset(structaes_state*st)
{
if (st->ctx)
EVP_CIPHER_CTX_free(st->ctx);
}
staticintaes_encr(structaes_state*aes, uint8_t*out,
constuint8_t*in, size_tlen)
{
intc_len= (int)len;
if (!EVP_EncryptUpdate(aes->ctx, out, &c_len, in, (int)len)) {
ERR_clear_error();
returnEPROTO;
}
return0;
}
staticintaes_decr(structaes_state*aes, uint8_t*out,
constuint8_t*in, size_tlen)
{
intc_len= (int)len;
if (!EVP_DecryptUpdate(aes->ctx, out, &c_len, in, (int)len)) {
ERR_clear_error();
returnEPROTO;
}
return0;
}
staticintaes_get_authtag(structaes_state*aes, uint8_t*tag, size_ttaglen)
{
inttmplen=0;
if (!EVP_EncryptFinal_ex(aes->ctx, NULL, &tmplen)) {
ERR_clear_error();
returnEPROTO;
}
if (!EVP_CIPHER_CTX_ctrl(aes->ctx, EVP_CTRL_GCM_GET_TAG,
(int)taglen, tag)) {
ERR_clear_error();
returnEPROTO;
}
return0;
}
staticintaes_authenticate(structaes_state*aes, constuint8_t*tag, size_ttaglen)
{
inttmplen;
if (!EVP_CIPHER_CTX_ctrl(aes->ctx, EVP_CTRL_GCM_SET_TAG,
(int)taglen, (void*)tag)) {
ERR_clear_error();
returnEPROTO;
}
if (EVP_DecryptFinal_ex(aes->ctx, NULL, &tmplen) <= 0) {
ERR_clear_error();
returnEPROTO;
}
return0;
}
staticinttest_aes_gcm(void)
{
structaes_stateenc, dec;
uint8_tout[MSG_LEN];
uint8_tclear[MSG_LEN];
uint8_ttag[TAG_LEN];
interr=0;
err=aes_alloc(&enc, encr_key, iv, true);
if (err)
goto out;
err=aes_alloc(&dec, encr_key, iv, false);
if (err)
goto out;
/* AAD */err=aes_decr(&dec, NULL, aad, sizeof(aad));
if (err)
goto out;
err=aes_encr(&enc, NULL, aad, sizeof(aad));
if (err)
goto out;
/* Encrypt */err=aes_encr(&enc, out, test_vector, sizeof(test_vector));
if (err)
goto out;
if (0!=memcmp(cipher_text, out, MSG_LEN))
returnEBADMSG;
#if1/* * NOTE: If this section is commented out, there is no valgrind warning */err=aes_get_authtag(&enc, tag, TAG_LEN);
if (err)
goto out;
if (0!=memcmp(tag_ref, tag, TAG_LEN))
returnEBADMSG;
#endif/* Decrypt */err=aes_decr(&dec, clear, out, MSG_LEN);
if (err)
goto out;
err=aes_authenticate(&dec, tag_ref, TAG_LEN);
if (err) {
printf("aes_authenticate error\n");
goto out;
}
if (0!=memcmp(test_vector, clear, MSG_LEN))
returnEBADMSG;
out:
aes_reset(&enc);
aes_reset(&dec);
returnerr;
}
intmain()
{
printf("%s\n", OpenSSL_version(OPENSSL_VERSION));
interr=test_aes_gcm();
if (err) {
fprintf(stderr, "ERROR: %d\n", err);
}
returnerr;
}
output:
alfredh@debian:~/tmp$ gcc -Iopenssl/include -Lopenssl -Wall -O2 aes_gcm.c -lcrypto
alfredh@debian:~/tmp$ LD_LIBRARY_PATH=openssl valgrind ./a.out
==195662== Memcheck, a memory error detector
==195662== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==195662== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==195662== Command: ./a.out
==195662==
OpenSSL 3.2.0-dev
openssl: ctx=0x5073690 out=(nil) padlen=0x1fff000258 in=0x10a070 len=16
openssl: ctx=0x5073180 out=(nil) padlen=0x1fff000278 in=0x10a070 len=16
openssl: ctx=0x5073180 out=0x1fff0002c0 padlen=0x1fff000278 in=0x10a0a0 len=16
openssl: ctx=0x5073180 out=(nil) padlen=0x1fff000258 in=(nil) len=0
openssl: ctx=0x5073690 out=0x1fff0002d0 padlen=0x1fff000258 in=0x1fff0002c0 len=16
openssl: ctx=0x5073690 out=(nil) padlen=0x1fff000268 in=(nil) len=0
==195662== Conditional jump or move depends on uninitialised value(s)
==195662== at 0x4C2EDD4: gcm_cipher_internal (ciphercommon_gcm.c:435)
==195662== by 0x4C2EDD4: ossl_gcm_stream_final (ciphercommon_gcm.c:335)
==195662== by 0x4A3898E: EVP_DecryptFinal_ex (evp_enc.c:935)
==195662== by 0x109302: main (in /home/alfredh/tmp/a.out)
==195662==
==195662==
==195662== HEAP SUMMARY:
==195662== in use at exit: 0 bytes in 0 blocks
==195662== total heap usage: 5,110 allocs, 5,110 frees, 542,447 bytes allocated
==195662==
==195662== All heap blocks were freed -- no leaks are possible
==195662==
==195662== Use --track-origins=yes to see where uninitialised values come from
==195662== For lists of detected and suppressed errors, rerun with: -s
==195662== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Relevant OpenSSL code:
} else {
/* The tag must be set before actually decrypting data */if (!ctx->enc&&ctx->taglen==UNINITIALISED_SIZET)
goto err;
if (!hw->cipherfinal(ctx, ctx->buf)) // <---------------- here!
goto err;
ctx->iv_state=IV_STATE_FINISHED; /* Don't reuse the IV */
goto finish;
}
I've investigated this and suspect it to be some kind of Valgrind bug, but cannot be sure.
The bug occurs in the AVX GHASH implementation, specifically at line 1218 of ghash-x86_64.pl:
vmovdqu .Lbswap_mask(%rip),$bswap
Placing a vpxor $bswap, $bswap, $bswap after this line causes the Valgrind warning to stop (and obviously breaks the implementation). Unclear to me how this instruction can be faulty though.
I just spent a couple of days tracking this bug, in tests where picoquic is using the openssl implementation of AES GCM 128. The tests are all passing, which means encryption and decryption calls are working as expected, but when running valgrind I get reports like:
==4420==
==4420== HEAP SUMMARY:
==4420== in use at exit: 0 bytes in 0 blocks
==4420== total heap usage: 869,646 allocs, 869,646 frees, 55,527,461 bytes allocated
==4420==
==4420== All heap blocks were freed -- no leaks are possible
==4420==
==4420== ERROR SUMMARY: 50 errors from 1 contexts (suppressed: 0 from 0)
==4420==
==4420== 50 errors in context 1 of 1:
==4420== Conditional jump or move depends on uninitialised value(s)
==4420== at 0x4AF62A4: ??? (in /usr/lib/x86_64-linux-gnu/libcrypto.so.3)
==4420== by 0x4AF6581: ??? (in /usr/lib/x86_64-linux-gnu/libcrypto.so.3)
==4420== by 0x49F70F4: EVP_DecryptFinal_ex (in /usr/lib/x86_64-linux-gnu/libcrypto.so.3)
==4420== by 0x1B5624: aead_do_decrypt (openssl.c:962)
==4420== by 0x1A7513: picoquic_server_encrypt_ticket_call_back (in /home/runner/work/picoquic/picoquic/build/picoquic_ct)
==4420== by 0x1CA626: try_psk_handshake (picotls.c:3544)
==4420== by 0x1CA626: server_handle_hello (picotls.c:3994)
==4420== by 0x1BF915: handle_handshake_record.part.0 (picotls.c:4742)
==4420== by 0x1CE2EF: handle_handshake_record (picotls.c:4717)
==4420== by 0x1CE2EF: ptls_server_handle_message (picotls.c:5463)
==4420== by 0x1AC1E3: picoquic_tls_stream_process (in /home/runner/work/picoquic/picoquic/build/picoquic_ct)
==4420== by 0x187014: picoquic_incoming_client_initial (in /home/runner/work/picoquic/picoquic/build/picoquic_ct)
==4420== by 0x1892D0: picoquic_incoming_segment (in /home/runner/work/picoquic/picoquic/build/picoquic_ct)
==4420== by 0x1896C0: picoquic_incoming_packet_ex (in /home/runner/work/picoquic/picoquic/build/picoquic_ct)
==4420== Uninitialised value was created by a stack allocation
==4420== at 0x1850A0: picoquic_remove_header_protection (in /home/runner/work/picoquic/picoquic/build/picoquic_ct)
==4420==
==4420== ERROR SUMMARY: 50 errors from 1 contexts (suppressed: 0 from 0)
I just meet this issue. And I find a workaround.
No valgrind warning if I set the authenication tag (EVP_CIPHER_CTX_ctrl) first then call EVP_DecryptUpdate().
I just meet this issue. And I find a workaround. No valgrind warning if I set the authenication tag (EVP_CIPHER_CTX_ctrl) first then call EVP_DecryptUpdate().
@chenbq83 That's an excellent find. Does that push this more in the direction of a genuine bug rather than valgrind bug? Is there a way to get this reopened? or re-looked at?
Activity
kroeckx commentedon Nov 19, 2022
Can you point to the code? I can not find any "test_aes_gcm" in that repository, and line numbers seem to be larger that the file is big. Running valgrind on OpenSSL's test suite does not generate any warnings.
alfredh commentedon Nov 19, 2022
hi,
The library is called "libre" and the test program is called "retest":
https://github.com/baresip/re/blob/main/src/aes/openssl/aes.c#L240
https://github.com/baresip/retest/blob/main/src/aes.c#L342
Here is how the OpenSSL api is called:
The valgrind warning seems to be present only with GCM and when AAD is used.
With no AAD there is no warning.
t8m commentedon Nov 21, 2022
We would need some more concise testcase to investigate further. Or at least if you could investigate what is the concrete variable that is uninitialized.
slontis commentedon Nov 24, 2022
Try putting a printf above line 425 to see what the parameters are, this may give you some indication of what parameter is not set up. My guess is it passes in an uninited parameter from the test.
alfredh commentedon Nov 25, 2022
output:
Relevant OpenSSL code:
alfredh commentedon Nov 25, 2022
I have written a test program to trigger the valgrind warning.
Some additional clues to help the debugging:
The warning is only present in release builds
The warning is only present if AAD is used
The warning is only present if EVP_CTRL_GCM_GET_TAG is used on the encryption
Tested 3 compilers, warning present on all of them
t8m commentedon Nov 25, 2022
Thank you for the nice reproducer. We will investigate.
faramir-dev commentedon Nov 25, 2022
@alfredh Thank you, It looks I can simulate the issue. I am investigating.
4 remaining items
kroeckx commentedon Nov 25, 2022
alfredh commentedon Nov 25, 2022
I tested on 32-bit intel system, and there are no warnings.
The system used was Debian Testing with gcc-12 and openssl 3.0.7
Also I tested with
no-asmand there are no warnings.alfredh commentedon Dec 3, 2022
here is a simplified testprogram:
I have also noticed something interesting:
hlandau commentedon Dec 5, 2022
I've investigated this and suspect it to be some kind of Valgrind bug, but cannot be sure.
The bug occurs in the AVX GHASH implementation, specifically at line 1218 of
ghash-x86_64.pl:Placing a
vpxor $bswap, $bswap, $bswapafter this line causes the Valgrind warning to stop (and obviously breaks the implementation). Unclear to me how this instruction can be faulty though.huitema commentedon Dec 9, 2022
I just spent a couple of days tracking this bug, in tests where picoquic is using the openssl implementation of AES GCM 128. The tests are all passing, which means encryption and decryption calls are working as expected, but when running valgrind I get reports like:
This bug appears very similar to bug #8460.
run-test-valgrind.supp: Suppress OpenSSL EVP_DecryptFinal_ex() use of…
nhorman commentedon Jun 7, 2024
marking as inactive, will be closed at the completion of 3.4 dev without further input
add valgrind suppression for EVP_DecryptFinal_ex
chenbq83 commentedon Jan 13, 2025
I just meet this issue. And I find a workaround.
No valgrind warning if I set the authenication tag (EVP_CIPHER_CTX_ctrl) first then call EVP_DecryptUpdate().
add valgrind suppression for EVP_DecryptFinal_ex
add valgrind suppression for EVP_DecryptFinal_ex
whitty commentedon May 13, 2026
@chenbq83 That's an excellent find. Does that push this more in the direction of a genuine bug rather than valgrind bug? Is there a way to get this reopened? or re-looked at?