summaryrefslogtreecommitdiffstats
path: root/php-gh22187.patch
diff options
context:
space:
mode:
authorRemi Collet <remi@remirepo.net>2026-07-02 08:53:31 +0200
committerRemi Collet <remi@php.net>2026-07-02 08:53:31 +0200
commitde3a042206b3a0671d2c76eb268d58c79b5649b2 (patch)
treeb624015f250b3216e2c21a0ff436ab323e95720c /php-gh22187.patch
parentfea2058e67e90dab8a7366fbde98941a3b61bf33 (diff)
Fix Memory corruption in openssl_encrypt with AES-WRAP-PADHEADmaster
Diffstat (limited to 'php-gh22187.patch')
-rw-r--r--php-gh22187.patch112
1 files changed, 112 insertions, 0 deletions
diff --git a/php-gh22187.patch b/php-gh22187.patch
new file mode 100644
index 0000000..ff14469
--- /dev/null
+++ b/php-gh22187.patch
@@ -0,0 +1,112 @@
+From 09cccab30d53614bb826e4390ad23ad7451b6d6c Mon Sep 17 00:00:00 2001
+From: David Carlier <devnexen@gmail.com>
+Date: Fri, 29 May 2026 21:44:14 +0100
+Subject: [PATCH] ext/openssl: openssl_encrypt() zend mm heap overflow on
+ AES-WRAP-PAD mode.
+
+Fix #22186
+
+close GH-22187
+
+(cherry picked from commit cbc0489126a7682796aad1e5fb4e51de74af162c)
+(cherry picked from commit 95e9851111d249e43948b76663cff1baeb5e758d)
+(cherry picked from commit 2a73e91a9f9136fbbfcc9177573b6af71e3d5dce)
+(cherry picked from commit e058b01e1bd23421a425cffae9f458b0fa8db222)
+---
+ NEWS | 6 ++++++
+ ext/openssl/openssl.c | 15 ++++++++++++++-
+ ext/openssl/tests/gh22186.phpt | 32 ++++++++++++++++++++++++++++++++
+ 3 files changed, 52 insertions(+), 1 deletion(-)
+ create mode 100644 ext/openssl/tests/gh22186.phpt
+
+diff --git a/NEWS b/NEWS
+index fe425c4f5c..fa18e93cb3 100644
+--- a/NEWS
++++ b/NEWS
+@@ -1,6 +1,12 @@
+ PHP NEWS
+ |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
+
++Backported from 8.2.32
++
++- OpenSSL:
++ . Fixed bug GH-22187 (Memory corruption (zend_mm_heap corrupted) in
++ openssl_encrypt with AES-WRAP-PAD). (David Carlier)
++
+ Backported from 8.2.31
+
+ - FPM:
+diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c
+index 2dc8eb3333..da9da1454e 100644
+--- a/ext/openssl/openssl.c
++++ b/ext/openssl/openssl.c
+@@ -6566,6 +6566,7 @@ static int php_openssl_cipher_update(const EVP_CIPHER *cipher_type,
+ char *aad, size_t aad_len, int enc) /* {{{ */
+ {
+ int i = 0;
++ size_t outlen = data_len + EVP_CIPHER_block_size(cipher_type);
+
+ if (mode->is_single_run_aead && !EVP_CipherUpdate(cipher_ctx, NULL, &i, NULL, (int)data_len)) {
+ php_openssl_store_errors();
+@@ -6579,7 +6580,19 @@ static int php_openssl_cipher_update(const EVP_CIPHER *cipher_type,
+ return FAILURE;
+ }
+
+- *poutbuf = zend_string_alloc((int)data_len + EVP_CIPHER_block_size(cipher_type), 0);
++#ifdef EVP_CIPH_WRAP_MODE
++ if ((EVP_CIPHER_mode(cipher_type)) == EVP_CIPH_WRAP_MODE) {
++ /*
++ * RFC 5649 wrap-with-padding rounds the input up to the block size
++ * and prepends an integrity block, we reserve one extra block.
++ * See EVP_EncryptUpdate(3): wrap mode may write up to
++ * inl + cipher_block_size bytes.
++ */
++ outlen += EVP_CIPHER_block_size(cipher_type);
++ }
++#endif
++
++ *poutbuf = zend_string_alloc(outlen, 0);
+
+ if (!EVP_CipherUpdate(cipher_ctx, (unsigned char*)ZSTR_VAL(*poutbuf),
+ &i, (unsigned char *)data, (int)data_len)) {
+diff --git a/ext/openssl/tests/gh22186.phpt b/ext/openssl/tests/gh22186.phpt
+new file mode 100644
+index 0000000000..8f28e6c45b
+--- /dev/null
++++ b/ext/openssl/tests/gh22186.phpt
+@@ -0,0 +1,32 @@
++--TEST--
++GH-22186 (Heap buffer overflow in openssl_encrypt with AES-WRAP-PAD)
++--EXTENSIONS--
++openssl
++--SKIPIF--
++<?php
++/* openssl_get_cipher_methods() enumerates provider ciphers, but openssl_encrypt()
++ * resolves names via the legacy EVP_get_cipherbyname(), so on some builds the
++ * cipher is listed yet not usable. Probe the actual call path instead. */
++if (!@openssl_encrypt("test", "aes-128-wrap-pad", str_repeat("k", 16),
++ OPENSSL_RAW_DATA | OPENSSL_DONT_ZERO_PAD_KEY, str_repeat("\0", 4))) {
++ die('skip aes-128-wrap-pad not usable on this OpenSSL build');
++}
++?>
++--FILE--
++<?php
++$pass = str_repeat("k", 16);
++$iv = str_repeat("\0", 4);
++
++for ($i = 1; $i < 258; $i++) {
++ $data = str_repeat("a", $i);
++ $enc = openssl_encrypt($data, 'aes-128-wrap-pad', $pass, OPENSSL_RAW_DATA | OPENSSL_DONT_ZERO_PAD_KEY, $iv);
++ $dec = openssl_decrypt($enc, 'aes-128-wrap-pad', $pass, OPENSSL_RAW_DATA | OPENSSL_DONT_ZERO_PAD_KEY, $iv);
++ if ($dec !== $data) {
++ die("mismatch at $i\n");
++ }
++}
++
++echo "done\n";
++?>
++--EXPECT--
++done
+--
+2.54.0
+