Bug #66942 [Opn->Csd]: memory leak in openssl_seal()

From: Date: Mon, 14 Apr 2014 20:29:42 +0000
Subject: Bug #66942 [Opn->Csd]: memory leak in openssl_seal()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-185249@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66942&edit=1

 ID:                 66942
 Updated by:         stas@php.net
 Reported by:        chuanm at gmail dot com
 Summary:            memory leak in openssl_seal()
-Status:             Open
+Status:             Closed
 Type:               Bug
 Package:            OpenSSL related
 Operating System:   centos 6.4
 PHP Version:        5.4.26
 Block user comment: N
 Private report:     N

 New Comment:

Automatic comment on behalf of Chuan.Ma@avidlifemedia.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=a186312832207437e4783024dcdece5232ac6c39
Log: Fix #66942: openssl_seal() memory leak


Previous Comments:
------------------------------------------------------------------------
[2014-03-24 13:07:43] chuanm at gmail dot com

A similar memory leak on openssl_encrypt() was reported in https://bugs.php.net/bug.php?id=54060

------------------------------------------------------------------------
[2014-03-23 02:23:47] chuanm at gmail dot com

In ext/openssl/openssl.c, adding
         EVP_CIPHER_CTX_cleanup(&ctx);
at the end of the openssl_seal() function only removes one block of the memory leak. The following
block still remains:

==30008==
==30008== HEAP SUMMARY:
==30008==     in use at exit: 83,617 bytes in 2,703 blocks
==30008==   total heap usage: 15,469 allocs, 12,766 frees, 3,235,070 bytes allocated
==30008==
==30008== 1,032 bytes in 1 blocks are definitely lost in loss record 64 of 86
==30008==    at 0x4C279EE: malloc (vg_replace_malloc.c:270)
==30008==    by 0x614AB2D: CRYPTO_malloc (in /usr/lib64/libcrypto.so.1.0.1e)
==30008==    by 0x61CA808: EVP_CipherInit_ex (in /usr/lib64/libcrypto.so.1.0.1e)
==30008==    by 0x45E50B: zif_openssl_seal (openssl.c:4318)
==30008==    by 0x6FD4C9: zend_do_fcall_common_helper_SPEC (zend_vm_execute.h:643)
==30008==    by 0x6F18CF: execute (zend_vm_execute.h:410)
==30008==    by 0x68BA6D: zend_execute_scripts (zend.c:1315)
==30008==    by 0x63190D: php_execute_script (main.c:2502)
==30008==    by 0x731DF2: do_cli (php_cli.c:989)
==30008==    by 0x7324E3: main (php_cli.c:1365)
==30008==
==30008== LEAK SUMMARY:
==30008==    definitely lost: 1,032 bytes in 1 blocks
==30008==    indirectly lost: 0 bytes in 0 blocks
==30008==      possibly lost: 0 bytes in 0 blocks
==30008==    still reachable: 82,585 bytes in 2,702 blocks
==30008==         suppressed: 0 bytes in 0 blocks
==30008== Reachable blocks (those to which a pointer was found) are not shown.
==30008== To see them, rerun with: --leak-check=full --show-reachable=yes
==30008==
==30008== For counts of detected and suppressed errors, rerun with: -v
==30008== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)

------------------------------------------------------------------------
[2014-03-22 21:28:48] chuanm at gmail dot com

Description:
------------
### Compiled with:

./confgiure --with-openssl --with-openssl-dir=/usr/lib64/openssl

### create keys
openssl genrsa -out privkey.pem 2048
openssl rsa -pubout -in privkey.pem -out pubkey.pem

Test script:
---------------
<?php
$data = 'this is test';

$cert = <<<ABC
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA01/7gxSlhLff2iKBNBIA
N77U+cdLl2g/YaJNjp4TQI33QY+P5VtP7coBxY2KyfhZAValyGmCzugSt4IlG/Jd
5FT+JSosUbbKSuSs4lia8mge0h7V5ius5ZvWTIqxtMtoxx36HLoaj4KQ32ToZjYM
/wI2ZMW9KD8WTFdezW7IKzc89DJ3AaoZaobRvUOjZVg8giLvsAFk4T3oWPS37Zed
uE4LkFdTnLxQ5y1rFWELpqm7E2itB+nLxHEq1Sb1w22iyXGqQSmaXC/b4SmjDLVw
W1lPjmYJt4NGNTgi3EAGWpCHnn9z1ERcOYX8lg+vTGftvejDgv/zvAuoWKiUO9lk
ywIDAQAB
-----END PUBLIC KEY-----
ABC;
$pk1 = openssl_get_publickey($cert);
openssl_seal($data, $sealed, $ekeys, array($pk1));

Expected result:
----------------
valgrind run should not complain about memory leak.

Actual result:
--------------
bash-4.1# valgrind --leak-check=full php openssl.php
==25769== Memcheck, a memory error detector
==25769== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==25769== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==25769== Command: php openssl.php
==25769==
==25769==
==25769== HEAP SUMMARY:
==25769==     in use at exit: 84,649 bytes in 2,704 blocks
==25769==   total heap usage: 15,468 allocs, 12,764 frees, 3,234,966 bytes allocated
==25769==
==25769== 1,032 bytes in 1 blocks are definitely lost in loss record 64 of 87
==25769==    at 0x4C279EE: malloc (vg_replace_malloc.c:270)
==25769==    by 0x614AB2D: CRYPTO_malloc (in /usr/lib64/libcrypto.so.1.0.1e)
==25769==    by 0x61CA808: EVP_CipherInit_ex (in /usr/lib64/libcrypto.so.1.0.1e)
==25769==    by 0x45E46B: zif_openssl_seal (openssl.c:4318)
==25769==    by 0x6FD7A9: zend_do_fcall_common_helper_SPEC (zend_vm_execute.h:643)
==25769==    by 0x6F1BAF: execute (zend_vm_execute.h:410)
==25769==    by 0x68BD4D: zend_execute_scripts (zend.c:1315)
==25769==    by 0x631BED: php_execute_script (main.c:2502)
==25769==    by 0x7320D2: do_cli (php_cli.c:989)
==25769==    by 0x7327C3: main (php_cli.c:1365)
==25769==
==25769== 1,032 bytes in 1 blocks are definitely lost in loss record 65 of 87
==25769==    at 0x4C279EE: malloc (vg_replace_malloc.c:270)
==25769==    by 0x614AB2D: CRYPTO_malloc (in /usr/lib64/libcrypto.so.1.0.1e)
==25769==    by 0x61CA808: EVP_CipherInit_ex (in /usr/lib64/libcrypto.so.1.0.1e)
==25769==    by 0x61D0B7C: EVP_SealInit (in /usr/lib64/libcrypto.so.1.0.1e)
==25769==    by 0x45E58D: zif_openssl_seal (openssl.c:4331)
==25769==    by 0x6FD7A9: zend_do_fcall_common_helper_SPEC (zend_vm_execute.h:643)
==25769==    by 0x6F1BAF: execute (zend_vm_execute.h:410)
==25769==    by 0x68BD4D: zend_execute_scripts (zend.c:1315)
==25769==    by 0x631BED: php_execute_script (main.c:2502)
==25769==    by 0x7320D2: do_cli (php_cli.c:989)
==25769==    by 0x7327C3: main (php_cli.c:1365)
==25769==
==25769== LEAK SUMMARY:
==25769==    definitely lost: 2,064 bytes in 2 blocks
==25769==    indirectly lost: 0 bytes in 0 blocks
==25769==      possibly lost: 0 bytes in 0 blocks
==25769==    still reachable: 82,585 bytes in 2,702 blocks
==25769==         suppressed: 0 bytes in 0 blocks
==25769== Reachable blocks (those to which a pointer was found) are not shown.
==25769== To see them, rerun with: --leak-check=full --show-reachable=yes
==25769==
==25769== For counts of detected and suppressed errors, rerun with: -v
==25769== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 6)


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=66942&edit=1


Thread (4 messages)

« previous php.bugs (#185249) next »