Skip to content

openssl_pkey_new() memory leak #19910

Closed as not planned
Closed as not planned

Description

@chongwick

Description

The following code:

<?php
function foo($v_288787='\n',)
{
$v_288706 = $v_288706;
}
function openssl_pkey_test_cmp($v_288707,$v_288736='11',)
{
$v_288707 = $v_288707;
$v_288708 = $v_288708;
$v_288812 = 'q';
$v_288709 = '0';
$v_288710 = ltrim($v_288812,$v_288709,);
}
$v_288714 = 'BBF82F090682CE9C2338AC2B9DA871F7368D07EED41043A440D6B6F07454F51F';
$v_288715 = 'B8DFBAAF035C02AB61EA48CEEB6FCD4876ED520D60E1EC4619719D8A5B8B807F';
$v_288716 = $v_288714 . $v_288715;
$v_288717 = 'AFB8E0A3DFC737723EE6B4B7D93A2584EE6A649D060953748834B2454598394E';
$v_288718 = $v_288716 . $v_288717;
$v_288744 = hex2bin($v_288720,);
$v_288723 = '9410B3C7998D6BC465745E5C392669D6870DA2C082A939E37FDCB82EC93EDAC9';
$v_288729 = 'EECFAE81B1B9B3C908810B10A1B5600199EB9F44AEF4FDA493B81A9E3D84F632';
$v_288725 = '7FF3AD5950ACCFBC111C76F1A9529444E56AAF68C56C092CD38DC3BEF5D20A93';
$v_288726 = $v_288729 . $v_288725;
$v_288727 = '9926ED4F74A13EDDFBE1A1CECC4894AF9428C2B7B8883FE4463A4BC85B1CB3C1';
$v_288728 = $v_288726 . $v_288727;
$v_288748 = hex2bin($v_288728,);
$v_288778 = 'dmp1';
$v_288750 = hex2bin($v_288778,);
$v_288735 = '11';
$v_288754 = hex2bin($v_288735,);
$v_288736 = $v_288736;
$v_288756 = hex2bin($v_288736,);
$v_288737 = 'b06c4fdabb6301198d265bdbae9423b380f271f73453885093077fcd39e2119f';
$v_288738 = 'c98632154f5883b167a967bf402b4e9e2e0f9656e698ea3666edfb25798039f7';
$v_288739 = $v_288737 . $v_288738;
$v_288758 = hex2bin($v_288739,);
$v_288742 = array('n' => $v_288744,'e' => $v_288723,'d' => $v_288748,'p' => $v_288750,'q' => $v_288709,'dmp1' => $v_288754,'dmq1' => $v_288756,'iqmp' => $v_288758,);
$v_288740 = array('rsa' => $v_288742,);
$v_288759 = openssl_pkey_new($v_288740,);

Resulted in this output:

Warning: Undefined variable $v_288720 in /home/w023dtc/treebugs/963e5353adfc6e438f99.php.er on line 19

Deprecated: hex2bin(): Passing null to parameter #1 ($string) of type string is deprecated in /home/w023dtc/treebugs/963e5353adfc6e438f99.php.er on line 19

Warning: hex2bin(): Input string must be hexadecimal string in /home/w023dtc/treebugs/963e5353adfc6e438f99.php.er on line 28

Warning: Undefined variable $v_288736 in /home/w023dtc/treebugs/963e5353adfc6e438f99.php.er on line 31

Deprecated: hex2bin(): Passing null to parameter #1 ($string) of type string is deprecated in /home/w023dtc/treebugs/963e5353adfc6e438f99.php.er on line 32

Warning: Undefined variable $v_288709 in /home/w023dtc/treebugs/963e5353adfc6e438f99.php.er on line 37

=================================================================
==2512238==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 72 byte(s) in 3 object(s) allocated from:
    #0 0x6829ad in malloc (/home/w023dtc/nightly_php/php-src/sapi/cli/php+0x6829ad)
    #1 0x1484e4d8f6dd in CRYPTO_zalloc (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1b76dd)

Indirect leak of 72 byte(s) in 2 object(s) allocated from:
    #0 0x6829ad in malloc (/home/w023dtc/nightly_php/php-src/sapi/cli/php+0x6829ad)
    #1 0x1484e4d8f6dd in CRYPTO_zalloc (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1b76dd)

SUMMARY: AddressSanitizer: 144 byte(s) leaked in 5 allocation(s).

PHP Version

nightly

Operating System

ubuntu 22.04

Activity

  1. devnexen commented on Sep 22, 2025

    @devnexen
    Member

    hmm can t see even with valgrind, or tweaking USE_ZEND_ALLOC. What s your openssl version ?

  2. devnexen commented on Sep 22, 2025

    @devnexen
    Member

    By the way for next, would it be possible to set this ? that would display a more thorough report. Cheers.

  3. ndossche commented on Sep 22, 2025

    @ndossche
    Member

    No, they need to set the env var ASAN_OPTIONS="fast_unwind_on_malloc=0" for a more thorough report in this case

  4. chongwick commented on Sep 22, 2025

    @chongwick
    Author
    Warning: Undefined variable $v_288720 in /home/w023dtc/treebugs/963e5353adfc6e438f99.php.er on line 19
    
    Deprecated: hex2bin(): Passing null to parameter #1 ($string) of type string is deprecated in /home/w023dtc/treebugs/963e5353adfc6e438f99.php.er on line 19
    
    Warning: hex2bin(): Input string must be hexadecimal string in /home/w023dtc/treebugs/963e5353adfc6e438f99.php.er on line 28
    
    Warning: Undefined variable $v_288736 in /home/w023dtc/treebugs/963e5353adfc6e438f99.php.er on line 31
    
    Deprecated: hex2bin(): Passing null to parameter #1 ($string) of type string is deprecated in /home/w023dtc/treebugs/963e5353adfc6e438f99.php.er on line 32
    
    Warning: Undefined variable $v_288709 in /home/w023dtc/treebugs/963e5353adfc6e438f99.php.er on line 37
    
    =================================================================
    ==2538082==ERROR: LeakSanitizer: detected memory leaks
    
    Direct leak of 48 byte(s) in 2 object(s) allocated from:
        #0 0x6829ad in malloc (/home/w023dtc/nightly_php/php-src/sapi/cli/php+0x6829ad)
        #1 0x14afa275f6dd in CRYPTO_zalloc (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1b76dd)
        #2 0x14afa2690df3 in BN_new (/lib/x86_64-linux-gnu/libcrypto.so.3+0xe8df3)
        #3 0x14afa2691cdc in BN_lebin2bn (/lib/x86_64-linux-gnu/libcrypto.so.3+0xe9cdc)
        #4 0x14afa2768e35 in OSSL_PARAM_get_BN (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1c0e35)
        #5 0x14afa279742f  (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1ef42f)
        #6 0x14afa2799163  (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1f1163)
        #7 0x14afa2823ba1  (/lib/x86_64-linux-gnu/libcrypto.so.3+0x27bba1)
        #8 0x14afa274e3ba in EVP_PKEY_fromdata (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1a63ba)
        #9 0x9945b7 in php_openssl_pkey_init_rsa /home/w023dtc/nightly_php/php-src/ext/openssl/openssl_backend_v3.c:124:4
        #10 0x930749 in zif_openssl_pkey_new /home/w023dtc/nightly_php/php-src/ext/openssl/openssl.c:2027:11
        #11 0x614302f in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER /home/w023dtc/nightly_php/php-src/Zend/zend_vm_execute.h:1421:2
        #12 0x5c5edbb in execute_ex /home/w023dtc/nightly_php/php-src/Zend/zend_vm_execute.h:115602:12
        #13 0x5c6134c in zend_execute /home/w023dtc/nightly_php/php-src/Zend/zend_vm_execute.h:121314:2
        #14 0x69e2689 in zend_execute_script /home/w023dtc/nightly_php/php-src/Zend/zend.c:1977:3
        #15 0x51c182a in php_execute_script_ex /home/w023dtc/nightly_php/php-src/main/main.c:2640:13
        #16 0x51c2968 in php_execute_script /home/w023dtc/nightly_php/php-src/main/main.c:2680:9
        #17 0x69f759a in do_cli /home/w023dtc/nightly_php/php-src/sapi/cli/php_cli.c:951:5
        #18 0x69f197f in main /home/w023dtc/nightly_php/php-src/sapi/cli/php_cli.c:1362:18
        #19 0x14afa1bb0d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
        #20 0x14afa1bb0e3f in __libc_start_main csu/../csu/libc-start.c:392:3
        #21 0x607ae4 in _start (/home/w023dtc/nightly_php/php-src/sapi/cli/php+0x607ae4)
    
    Direct leak of 24 byte(s) in 1 object(s) allocated from:
        #0 0x6829ad in malloc (/home/w023dtc/nightly_php/php-src/sapi/cli/php+0x6829ad)
        #1 0x14afa275f6dd in CRYPTO_zalloc (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1b76dd)
        #2 0x14afa2690df3 in BN_new (/lib/x86_64-linux-gnu/libcrypto.so.3+0xe8df3)
        #3 0x14afa2691cdc in BN_lebin2bn (/lib/x86_64-linux-gnu/libcrypto.so.3+0xe9cdc)
        #4 0x14afa2768e35 in OSSL_PARAM_get_BN (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1c0e35)
        #5 0x14afa279742f  (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1ef42f)
        #6 0x14afa279918e  (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1f118e)
        #7 0x14afa2823ba1  (/lib/x86_64-linux-gnu/libcrypto.so.3+0x27bba1)
        #8 0x14afa274e3ba in EVP_PKEY_fromdata (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1a63ba)
        #9 0x9945b7 in php_openssl_pkey_init_rsa /home/w023dtc/nightly_php/php-src/ext/openssl/openssl_backend_v3.c:124:4
        #10 0x930749 in zif_openssl_pkey_new /home/w023dtc/nightly_php/php-src/ext/openssl/openssl.c:2027:11
        #11 0x614302f in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER /home/w023dtc/nightly_php/php-src/Zend/zend_vm_execute.h:1421:2
        #12 0x5c5edbb in execute_ex /home/w023dtc/nightly_php/php-src/Zend/zend_vm_execute.h:115602:12
        #13 0x5c6134c in zend_execute /home/w023dtc/nightly_php/php-src/Zend/zend_vm_execute.h:121314:2
        #14 0x69e2689 in zend_execute_script /home/w023dtc/nightly_php/php-src/Zend/zend.c:1977:3
        #15 0x51c182a in php_execute_script_ex /home/w023dtc/nightly_php/php-src/main/main.c:2640:13
        #16 0x51c2968 in php_execute_script /home/w023dtc/nightly_php/php-src/main/main.c:2680:9
        #17 0x69f759a in do_cli /home/w023dtc/nightly_php/php-src/sapi/cli/php_cli.c:951:5
        #18 0x69f197f in main /home/w023dtc/nightly_php/php-src/sapi/cli/php_cli.c:1362:18
        #19 0x14afa1bb0d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
        #20 0x14afa1bb0e3f in __libc_start_main csu/../csu/libc-start.c:392:3
        #21 0x607ae4 in _start (/home/w023dtc/nightly_php/php-src/sapi/cli/php+0x607ae4)
    
    Indirect leak of 64 byte(s) in 1 object(s) allocated from:
        #0 0x6829ad in malloc (/home/w023dtc/nightly_php/php-src/sapi/cli/php+0x6829ad)
        #1 0x14afa275f6dd in CRYPTO_zalloc (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1b76dd)
        #2 0x14afa2690ebc  (/lib/x86_64-linux-gnu/libcrypto.so.3+0xe8ebc)
        #3 0x14afa2691cff in BN_lebin2bn (/lib/x86_64-linux-gnu/libcrypto.so.3+0xe9cff)
        #4 0x14afa2768e35 in OSSL_PARAM_get_BN (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1c0e35)
        #5 0x14afa279742f  (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1ef42f)
        #6 0x14afa279918e  (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1f118e)
        #7 0x14afa2823ba1  (/lib/x86_64-linux-gnu/libcrypto.so.3+0x27bba1)
        #8 0x14afa274e3ba in EVP_PKEY_fromdata (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1a63ba)
        #9 0x9945b7 in php_openssl_pkey_init_rsa /home/w023dtc/nightly_php/php-src/ext/openssl/openssl_backend_v3.c:124:4
        #10 0x930749 in zif_openssl_pkey_new /home/w023dtc/nightly_php/php-src/ext/openssl/openssl.c:2027:11
        #11 0x614302f in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER /home/w023dtc/nightly_php/php-src/Zend/zend_vm_execute.h:1421:2
        #12 0x5c5edbb in execute_ex /home/w023dtc/nightly_php/php-src/Zend/zend_vm_execute.h:115602:12
        #13 0x5c6134c in zend_execute /home/w023dtc/nightly_php/php-src/Zend/zend_vm_execute.h:121314:2
        #14 0x69e2689 in zend_execute_script /home/w023dtc/nightly_php/php-src/Zend/zend.c:1977:3
        #15 0x51c182a in php_execute_script_ex /home/w023dtc/nightly_php/php-src/main/main.c:2640:13
        #16 0x51c2968 in php_execute_script /home/w023dtc/nightly_php/php-src/main/main.c:2680:9
        #17 0x69f759a in do_cli /home/w023dtc/nightly_php/php-src/sapi/cli/php_cli.c:951:5
        #18 0x69f197f in main /home/w023dtc/nightly_php/php-src/sapi/cli/php_cli.c:1362:18
        #19 0x14afa1bb0d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
        #20 0x14afa1bb0e3f in __libc_start_main csu/../csu/libc-start.c:392:3
        #21 0x607ae4 in _start (/home/w023dtc/nightly_php/php-src/sapi/cli/php+0x607ae4)
    
    Indirect leak of 8 byte(s) in 1 object(s) allocated from:
        #0 0x6829ad in malloc (/home/w023dtc/nightly_php/php-src/sapi/cli/php+0x6829ad)
        #1 0x14afa275f6dd in CRYPTO_zalloc (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1b76dd)
        #2 0x14afa2690ebc  (/lib/x86_64-linux-gnu/libcrypto.so.3+0xe8ebc)
        #3 0x14afa2691cff in BN_lebin2bn (/lib/x86_64-linux-gnu/libcrypto.so.3+0xe9cff)
        #4 0x14afa2768e35 in OSSL_PARAM_get_BN (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1c0e35)
        #5 0x14afa279742f  (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1ef42f)
        #6 0x14afa2799163  (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1f1163)
        #7 0x14afa2823ba1  (/lib/x86_64-linux-gnu/libcrypto.so.3+0x27bba1)
        #8 0x14afa274e3ba in EVP_PKEY_fromdata (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1a63ba)
        #9 0x9945b7 in php_openssl_pkey_init_rsa /home/w023dtc/nightly_php/php-src/ext/openssl/openssl_backend_v3.c:124:4
        #10 0x930749 in zif_openssl_pkey_new /home/w023dtc/nightly_php/php-src/ext/openssl/openssl.c:2027:11
        #11 0x614302f in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER /home/w023dtc/nightly_php/php-src/Zend/zend_vm_execute.h:1421:2
        #12 0x5c5edbb in execute_ex /home/w023dtc/nightly_php/php-src/Zend/zend_vm_execute.h:115602:12
        #13 0x5c6134c in zend_execute /home/w023dtc/nightly_php/php-src/Zend/zend_vm_execute.h:121314:2
        #14 0x69e2689 in zend_execute_script /home/w023dtc/nightly_php/php-src/Zend/zend.c:1977:3
        #15 0x51c182a in php_execute_script_ex /home/w023dtc/nightly_php/php-src/main/main.c:2640:13
        #16 0x51c2968 in php_execute_script /home/w023dtc/nightly_php/php-src/main/main.c:2680:9
        #17 0x69f759a in do_cli /home/w023dtc/nightly_php/php-src/sapi/cli/php_cli.c:951:5
        #18 0x69f197f in main /home/w023dtc/nightly_php/php-src/sapi/cli/php_cli.c:1362:18
        #19 0x14afa1bb0d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
        #20 0x14afa1bb0e3f in __libc_start_main csu/../csu/libc-start.c:392:3
        #21 0x607ae4 in _start (/home/w023dtc/nightly_php/php-src/sapi/cli/php+0x607ae4)
    
    SUMMARY: AddressSanitizer: 144 byte(s) leaked in 5 allocation(s).
    

    OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)

  5. devnexen commented on Sep 22, 2025

    @devnexen
    Member

    Ok well, normally 3.0.2 contains this fix (nvm: was taken care of anyway apparently), no idea where this could be from. So I ll let the expert talk :)

  6. ndossche commented on Sep 30, 2025

    @ndossche
    Member

    Repros in 3.0.2, 3.1.0, 3.2.0 but not in 3.3.0.
    So I ran a bisect and ended up at openssl/openssl@f3be536
    Also wow this took an hour to bisect because of how long it takes to build openssl.
    So @chongwick this is an openssl bug not a PHP bug. Closing as such.

  7. bukka commented on Oct 13, 2025

    @bukka
    Member

    @nielsdos is it already fixed in OpenSSL or should it be reported upstream?

  8. ndossche commented on Oct 13, 2025

    @ndossche
    Member

    @bukka This is already fixed upstream

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions