Skip to content

valgrind: Conditional jump or move depends on uninitialised value(s) #19719

Closed as not planned

Description

@alfredh

OpenSSL 3.0.5 or git HEAD on Linux

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== 

Here is the original issue: baresip/re#566

Update:

Assembly:

  • asm: warning
  • no-asm: no warning

Architecture:

  • i686: no warning
  • x86_64: warning

Affected versions:

  • 1.1.1: no
  • 3.0.0: yes
  • 3.0.5: yes
  • 3.0.7: yes
  • git: yes

Activity

  1. added
    issue: bug report
    on Nov 19, 2022
  2. kroeckx commented on Nov 19, 2022

    @kroeckx
    Member

    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.

  3. alfredh commented on Nov 19, 2022

    @alfredh
    ContributorAuthor

    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:

    int aes_authenticate(struct aes *aes, const uint8_t *tag, size_t taglen)
    {
    	int tmplen;
    
    	if (!aes || !tag || !taglen)
    		return EINVAL;
    
    	switch (aes->mode) {
    
    	case AES_MODE_GCM:
    		if (!EVP_CIPHER_CTX_ctrl(aes->ctx, EVP_CTRL_GCM_SET_TAG,
    					 (int)taglen, (void *)tag)) {
    			ERR_clear_error();
    			return EPROTO;
    		}
    
    		if (EVP_DecryptFinal_ex(aes->ctx, NULL, &tmplen) <= 0) {
    			ERR_clear_error();
    			return EAUTH;
    		}
    
    		return 0;
    
    	default:
    		return ENOTSUP;
    	}
    }

    The valgrind warning seems to be present only with GCM and when AAD is used.
    With no AAD there is no warning.

  4. added
    triaged: bug
    branch: master
    branch: 3.0
    branch: 3.1
    and removed
    issue: bug report
    on Nov 21, 2022
  5. t8m commented on Nov 21, 2022

    @t8m
    Member

    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.

  6. slontis commented on Nov 24, 2022

    @slontis
    Member

    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.

  7. self-assigned this
    on Nov 25, 2022
  8. alfredh commented on Nov 25, 2022

    @alfredh
    ContributorAuthor
    #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
     */
    
    #define KEY_LEN 32    /* 256 bits */
    #define IV_LEN  12    /*  96 bits */
    #define MSG_LEN 16    /* 128 bits */
    #define AAD_LEN 16    /* 128 bits */
    #define TAG_LEN 16    /* 128 bits */
    
    
    static const uint8_t test_vector[MSG_LEN] =
    	"\x2d\x71\xbc\xfa\x91\x4e\x4a\xc0\x45\xb2\xaa\x60\x95\x5f\xad\x24";
    
    static const uint8_t encr_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";
    
    static const uint8_t aad[AAD_LEN] =
    	"\x1e\x08\x89\x01\x6f\x67\x60\x1c\x8e\xbe\xa4\x94\x3b\xc2\x3a\xd6";
    
    static const uint8_t iv[IV_LEN] =
    	"\xac\x93\xa1\xa6\x14\x52"
    	"\x99\xbd\xe9\x02\xf2\x1a";
    
    static const uint8_t tag_ref[TAG_LEN] =
    	"\xec\xa5\xaa\x77\xd5\x1d\x4a\x0a\x14\xd9\xc5\x1e\x1d\xa4\x74\xab";
    
    static const uint8_t cipher_text[MSG_LEN] =
    	"\x89\x95\xae\x2e\x6d\xf3\xdb\xf9\x6f\xac\x7b\x71\x37\xba\xe6\x7f";
    
    
    struct aes_state {
    	EVP_CIPHER_CTX *ctx;
    };
    
    
    static int aes_alloc(struct aes_state *st, const uint8_t *key,
    		     const uint8_t *iv, bool encr)
    {
    	int r;
    
    	memset(st, 0, sizeof(*st));
    
    	const EVP_CIPHER *cipher = EVP_aes_256_gcm();
    	if (!cipher)
    		return ENOTSUP;
    
    	st->ctx = EVP_CIPHER_CTX_new();
    	if (!st->ctx) {
    		ERR_clear_error();
    		return ENOMEM;
    	}
    
    	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();
    		return EPROTO;
    	}
    
    	return 0;
    }
    
    
    static void aes_reset(struct aes_state *st)
    {
    	if (st->ctx)
    		EVP_CIPHER_CTX_free(st->ctx);
    }
    
    
    static int aes_encr(struct aes_state *aes, uint8_t *out,
    		    const uint8_t *in, size_t len)
    {
    	int c_len = (int)len;
    
    	if (!EVP_EncryptUpdate(aes->ctx, out, &c_len, in, (int)len)) {
    		ERR_clear_error();
    		return EPROTO;
    	}
    
    	return 0;
    }
    
    
    static int aes_decr(struct aes_state *aes, uint8_t *out,
    		    const uint8_t *in, size_t len)
    {
    	int c_len = (int)len;
    
    	if (!EVP_DecryptUpdate(aes->ctx, out, &c_len, in, (int)len)) {
    		ERR_clear_error();
    		return EPROTO;
    	}
    
    	return 0;
    }
    
    
    static int aes_get_authtag(struct aes_state *aes, uint8_t *tag, size_t taglen)
    {
    	int tmplen = 0;
    
    	if (!EVP_EncryptFinal_ex(aes->ctx, NULL, &tmplen)) {
    		ERR_clear_error();
    		return EPROTO;
    	}
    
    	if (!EVP_CIPHER_CTX_ctrl(aes->ctx, EVP_CTRL_GCM_GET_TAG,
    				 (int)taglen, tag)) {
    		ERR_clear_error();
    		return EPROTO;
    	}
    
    	return 0;
    }
    
    
    static int aes_authenticate(struct aes_state *aes, const uint8_t *tag, size_t taglen)
    {
    	int tmplen;
    
    	if (!EVP_CIPHER_CTX_ctrl(aes->ctx, EVP_CTRL_GCM_SET_TAG,
    				 (int)taglen, (void *)tag)) {
    		ERR_clear_error();
    		return EPROTO;
    	}
    
    	if (EVP_DecryptFinal_ex(aes->ctx, NULL, &tmplen) <= 0) {
    		ERR_clear_error();
    		return EPROTO;
    	}
    
    	return 0;
    }
    
    
    static int test_aes_gcm(void)
    {
    	struct aes_state enc, dec;
    	uint8_t out[MSG_LEN];
    	uint8_t clear[MSG_LEN];
    	uint8_t tag[TAG_LEN];
    	int err = 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))
    		return EBADMSG;
    
    #if 1
    	/*
    	 * 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))
    		return EBADMSG;
    #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))
    		return EBADMSG;
    
     out:
    	aes_reset(&enc);
    	aes_reset(&dec);
    
    	return err;
    }
    
    
    int main()
    {
    	printf("%s\n", OpenSSL_version(OPENSSL_VERSION));
    
    	int err = test_aes_gcm();
    	if (err) {
    		fprintf(stderr, "ERROR: %d\n", err);
    	}
    
    	return err;
    }

    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;
        }
  9. alfredh commented on Nov 25, 2022

    @alfredh
    ContributorAuthor

    I have written a test program to trigger the valgrind warning.

    Some additional clues to help the debugging:

    1. The warning is only present in release builds

    2. The warning is only present if AAD is used

    3. The warning is only present if EVP_CTRL_GCM_GET_TAG is used on the encryption

    4. Tested 3 compilers, warning present on all of them

  10. t8m commented on Nov 25, 2022

    @t8m
    Member

    Thank you for the nice reproducer. We will investigate.

  11. faramir-dev commented on Nov 25, 2022

    @faramir-dev
    Contributor

    @alfredh Thank you, It looks I can simulate the issue. I am investigating.

  12. 4 remaining items

  13. kroeckx commented on Nov 25, 2022

    @kroeckx
    Member
  14. alfredh commented on Nov 25, 2022

    @alfredh
    ContributorAuthor

    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-asm and there are no warnings.

  15. alfredh commented on Dec 3, 2022

    @alfredh
    ContributorAuthor

    here is a simplified testprogram:

    #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
     *
     */
    
    #define KEY_LEN 16    /* 128 bits */
    #define IV_LEN  12    /*  96 bits */
    #define MSG_LEN 16    /* 128 bits */  // note: 1-15 no warning
    #define AAD_LEN 16    /* 128 bits */  // note: 1-15 no warning
    #define TAG_LEN 16    /* 128 bits */
    
    
    static const uint8_t encr_key[KEY_LEN]    = { 0 };
    static const uint8_t iv[IV_LEN]           = { 0 };
    static const uint8_t aad[AAD_LEN]         = { 0 };
    static const uint8_t tag_ref[TAG_LEN]     = { 0 };
    static const uint8_t cipher_text[MSG_LEN] = { 0 };
    
    
    static EVP_CIPHER_CTX *aes_alloc(const uint8_t *key, const uint8_t *iv, bool encr)
    {
    	int r;
    
    	const EVP_CIPHER *cipher = EVP_aes_128_gcm();
    	if (!cipher)
    		return 0;
    
    	EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
    	if (!ctx)
    		return NULL;
    
    	if (encr) {
    		r = EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv);
    	}
    	else {
    		// note: If IV is NULL here, then no warning
    		r = EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv);
    	}
    
    	if (!r) {
    		return 0;
    	}
    
    	return ctx;
    }
    
    
    static int aes_decr(EVP_CIPHER_CTX *ctx, uint8_t *out,
    		    const uint8_t *in, size_t len)
    {
    	int c_len = (int)len;
    
    	EVP_DecryptUpdate(ctx, out, &c_len, in, (int)len);
    
    	return 0;
    }
    
    
    static int aes_get_authtag(EVP_CIPHER_CTX *ctx, uint8_t *tag, size_t taglen)
    {
    	int tmplen = 0;
    
    	EVP_EncryptFinal_ex(ctx, NULL, &tmplen);
    
    	EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, (int)taglen, tag);
    
    	return 0;
    }
    
    
    static int aes_authenticate(EVP_CIPHER_CTX *ctx, const uint8_t *tag, size_t taglen)
    {
    	int tmplen;
    
    	EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, (int)taglen, (void *)tag);
    
    	EVP_DecryptFinal_ex(ctx, NULL, &tmplen);
    
    	return 0;
    }
    
    
    int main()
    {
    	EVP_CIPHER_CTX *enc, *dec;
    	uint8_t tag[TAG_LEN] = {0};
    	uint8_t clear[MSG_LEN] = {0};
    
    	enc = aes_alloc(encr_key, iv, true);
    	dec = aes_alloc(encr_key, iv, false);
    
    	// note: affected
    	aes_decr(dec, NULL, aad, AAD_LEN);
    
    	// note: affected
    	aes_get_authtag(enc, tag, TAG_LEN);
    
    	// note: affected
    	aes_decr(dec, clear, cipher_text, MSG_LEN);
    
    	// note: valgrind warning inside here
    	printf("aes_authenticate\n");
    	aes_authenticate(dec, tag_ref, TAG_LEN);
    	printf("aes_authenticate done\n");
    
    	EVP_CIPHER_CTX_free(enc);
    	EVP_CIPHER_CTX_free(dec);
    
    	return 0;
    }

    I have also noticed something interesting:

    1. if the AAD length or Message length is less than 16 bytes, there is no valgrind warning
    2. if the AAD length or Message length is 16 bytes or more, the valgrind warning is present
  16. hlandau commented on Dec 5, 2022

    @hlandau
    Member

    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.

  17. huitema commented on Dec 9, 2022

    @huitema

    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)
    

    This bug appears very similar to bug #8460.

  18. added
    branch: 3.2
    on Oct 26, 2023
  19. nhorman commented on Jun 7, 2024

    @nhorman
    Contributor

    marking as inactive, will be closed at the completion of 3.4 dev without further input

  20. chenbq83 commented on Jan 13, 2025

    @chenbq83

    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().

  21. whitty commented on May 13, 2026

    @whitty

    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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

branch: 3.0branch: 3.1branch: 3.2branch: mastertriaged: bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions