ruby-changes:20676
From: nahi <ko1@a...>
Date: Thu, 28 Jul 2011 22:53:07 +0900 (JST)
Subject: [ruby-changes:20676] nahi:r32724 (ruby_1_9_3): * backport r32723 from trunk.
nahi 2011-07-28 22:52:57 +0900 (Thu, 28 Jul 2011) New Revision: 32724 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=32724 Log: * backport r32723 from trunk. * ext/openssl/ossl_cipher.c (ossl_cipher_initialize): Avoid possible SEGV from AES encryption/decryption. Processing data by Cipher#update without initializing key (meaningless usage of Cipher object since we don't offer a way to export a key) could cause SEGV. In OpenSSL, the EVP which has EVP_CIPH_RAND_KEY flag (such as DES3) allows uninitialized key, but other EVPs (such as AES) does not allow it. Calling EVP_CipherUpdate() without initializing key causes SEGV so we set the data filled with "\0" as the key by default. See #2768. * test/openssl/test_cipher.rb: test it. Modified files: branches/ruby_1_9_3/ChangeLog branches/ruby_1_9_3/ext/openssl/ossl_cipher.c branches/ruby_1_9_3/test/openssl/test_cipher.rb Index: ruby_1_9_3/ChangeLog =================================================================== --- ruby_1_9_3/ChangeLog (revision 32723) +++ ruby_1_9_3/ChangeLog (revision 32724) @@ -1,3 +1,20 @@ +Thu Jul 28 22:51:27 2011 Hiroshi Nakamura <nahi@r...> + + * backport r32723 from trunk. + + * ext/openssl/ossl_cipher.c (ossl_cipher_initialize): Avoid possible + SEGV from AES encryption/decryption. Processing data by + Cipher#update without initializing key (meaningless usage of Cipher + object since we don't offer a way to export a key) could cause SEGV. + + In OpenSSL, the EVP which has EVP_CIPH_RAND_KEY flag (such as DES3) + allows uninitialized key, but other EVPs (such as AES) does not + allow it. Calling EVP_CipherUpdate() without initializing key causes + SEGV so we set the data filled with "\0" as the key by default. See + #2768. + + * test/openssl/test_cipher.rb: test it. + Thu Jul 28 04:53:31 2011 Eric Hodel <drbrain@s...> * lib/delegate.rb: Move file-level documentation to the appropriate Index: ruby_1_9_3/ext/openssl/ossl_cipher.c =================================================================== --- ruby_1_9_3/ext/openssl/ossl_cipher.c (revision 32723) +++ ruby_1_9_3/ext/openssl/ossl_cipher.c (revision 32724) @@ -102,6 +102,7 @@ EVP_CIPHER_CTX *ctx; const EVP_CIPHER *cipher; char *name; + unsigned char key[EVP_MAX_KEY_LENGTH]; name = StringValuePtr(str); GetCipherInit(self, ctx); @@ -113,7 +114,14 @@ if (!(cipher = EVP_get_cipherbyname(name))) { ossl_raise(rb_eRuntimeError, "unsupported cipher algorithm (%s)", name); } - if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, -1) != 1) + /* + * The EVP which has EVP_CIPH_RAND_KEY flag (such as DES3) allows + * uninitialized key, but other EVPs (such as AES) does not allow it. + * Calling EVP_CipherUpdate() without initializing key causes SEGV so we + * set the data filled with "\0" as the key by default. + */ + memset(key, 0, EVP_MAX_KEY_LENGTH); + if (EVP_CipherInit_ex(ctx, cipher, NULL, key, NULL, -1) != 1) ossl_raise(eCipherError, NULL); return self; Index: ruby_1_9_3/test/openssl/test_cipher.rb =================================================================== --- ruby_1_9_3/test/openssl/test_cipher.rb (revision 32723) +++ ruby_1_9_3/test/openssl/test_cipher.rb (revision 32724) @@ -90,6 +90,15 @@ assert_equal(pt, c2.update(ct) + c2.final) } end + + def test_AES_crush + 500.times do + assert_nothing_raised("[Bug #2768]") do + # it caused OpenSSL SEGV by uninitialized key + OpenSSL::Cipher::AES128.new("ECB").update "." * 17 + end + end + end end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/