aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2016-04-24 03:27:13 +0900
committerKazuki Yamaguchi <k@rhe.jp>2016-05-05 18:43:53 +0900
commitfc989306c06bf842b2f8b84de15ab5526727d5da (patch)
tree60232e29af0580f63778db074c5629243805771c
parent67700610044b2bb49dcef4ed4c6bd4878bf7a49f (diff)
downloadruby-fc989306c06bf842b2f8b84de15ab5526727d5da.tar.gz
ext/openssl: avoid SEGV on Cipher.new("ChaCha20-Poly1305")
A temporary workaround. EVP_CipherInit_ex() allows to specify NULL to key and/or iv, however when we use ChaCha20-Poly1305 and set only key (this case), it does memcpy(x, NULL, y) and this causes a segmentation fault.
-rw-r--r--ext/openssl/ossl_cipher.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/ext/openssl/ossl_cipher.c b/ext/openssl/ossl_cipher.c
index e25871e45c..2c82e65b53 100644
--- a/ext/openssl/ossl_cipher.c
+++ b/ext/openssl/ossl_cipher.c
@@ -108,7 +108,17 @@ ossl_cipher_initialize(VALUE self, VALUE str)
EVP_CIPHER_CTX *ctx;
const EVP_CIPHER *cipher;
char *name;
- unsigned char key[EVP_MAX_KEY_LENGTH];
+ /*
+ * EVP_CipherInit_ex() allows to specify NULL to key and iv, however some
+ * ciphers unfortunately don't handle well. [Bug #2768]
+ *
+ * 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.
+ */
+ unsigned char dummy_key[EVP_MAX_KEY_LENGTH] = { 0 };
+ unsigned char dummy_iv[EVP_MAX_IV_LENGTH] = { 0 };
name = StringValuePtr(str);
GetCipherInit(self, ctx);
@@ -119,14 +129,8 @@ ossl_cipher_initialize(VALUE self, VALUE str)
if (!(cipher = EVP_get_cipherbyname(name))) {
ossl_raise(rb_eRuntimeError, "unsupported cipher algorithm (%s)", name);
}
- /*
- * 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)
+
+ if (EVP_CipherInit_ex(ctx, cipher, NULL, dummy_key, dummy_iv, -1) != 1)
ossl_raise(eCipherError, NULL);
return self;