ruby-changes:33514
From: nobu <ko1@a...>
Date: Wed, 16 Apr 2014 09:51:25 +0900 (JST)
Subject: [ruby-changes:33514] nobu:r45595 (trunk): ossl_pkey.c: fix memory leak
nobu 2014-04-16 09:51:18 +0900 (Wed, 16 Apr 2014) New Revision: 45595 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45595 Log: ossl_pkey.c: fix memory leak * ext/openssl/ossl_pkey.c (ossl_pkey_verify): as EVP_VerifyFinal() finalizes only a copy of the digest context, the context must be cleaned up after initialization by EVP_MD_CTX_cleanup() or a memory leak will occur. [ruby-core:62038] [Bug #9743] Modified files: trunk/ChangeLog trunk/ext/openssl/ossl_pkey.c trunk/test/openssl/test_pkey_rsa.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 45594) +++ ChangeLog (revision 45595) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Apr 16 09:51:16 2014 Nobuyoshi Nakada <nobu@r...> + + * ext/openssl/ossl_pkey.c (ossl_pkey_verify): as EVP_VerifyFinal() + finalizes only a copy of the digest context, the context must be + cleaned up after initialization by EVP_MD_CTX_cleanup() or a + memory leak will occur. [ruby-core:62038] [Bug #9743] + Tue Apr 15 19:36:42 2014 NAKAMURA Usaku <usa@r...> * include/ruby/win32.h (rb_w32_cmdvector): removed. Index: ext/openssl/ossl_pkey.c =================================================================== --- ext/openssl/ossl_pkey.c (revision 45594) +++ ext/openssl/ossl_pkey.c (revision 45595) @@ -318,13 +318,16 @@ ossl_pkey_verify(VALUE self, VALUE diges https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_pkey.c#L318 { EVP_PKEY *pkey; EVP_MD_CTX ctx; + int result; GetPKey(self, pkey); - EVP_VerifyInit(&ctx, GetDigestPtr(digest)); StringValue(sig); StringValue(data); + EVP_VerifyInit(&ctx, GetDigestPtr(digest)); EVP_VerifyUpdate(&ctx, RSTRING_PTR(data), RSTRING_LEN(data)); - switch (EVP_VerifyFinal(&ctx, (unsigned char *)RSTRING_PTR(sig), RSTRING_LENINT(sig), pkey)) { + result = EVP_VerifyFinal(&ctx, (unsigned char *)RSTRING_PTR(sig), RSTRING_LENINT(sig), pkey); + EVP_MD_CTX_cleanup(&ctx); + switch (result) { case 0: return Qfalse; case 1: Index: test/openssl/test_pkey_rsa.rb =================================================================== --- test/openssl/test_pkey_rsa.rb (revision 45594) +++ test/openssl/test_pkey_rsa.rb (revision 45595) @@ -75,6 +75,36 @@ class OpenSSL::TestPKeyRSA < Test::Unit: https://github.com/ruby/ruby/blob/trunk/test/openssl/test_pkey_rsa.rb#L75 assert(key.verify(digest, sig, data)) end + def test_sign_verify_memory_leak + bug9743 = '[ruby-core:62038] [Bug #9743]' + assert_no_memory_leak(%w[-ropenssl], <<-PREP, <<-CODE, bug9743, rss: true) + data = 'Sign me!' + digest = OpenSSL::Digest::SHA512.new + pkey = OpenSSL::PKey::RSA.new(2048) + signature = pkey.sign(digest, data) + pub_key = pkey.public_key + PREP + 20_000.times { + pub_key.verify(digest, signature, data) + } + CODE + + assert_no_memory_leak(%w[-ropenssl], <<-PREP, <<-CODE, bug9743, rss: true) + data = 'Sign me!' + digest = OpenSSL::Digest::SHA512.new + pkey = OpenSSL::PKey::RSA.new(2048) + signature = pkey.sign(digest, data) + pub_key = pkey.public_key + PREP + 20_000.times { + begin + pub_key.verify(digest, signature, 1) + rescue TypeError + end + } + CODE + end + def test_digest_state_irrelevant_sign key = OpenSSL::TestUtils::TEST_KEY_RSA1024 digest1 = OpenSSL::Digest::SHA1.new -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/