]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
sm2: check buffer size before writing ciphertext
authorGGAutomaton <gga7n@proton.me>
Sat, 28 Mar 2026 07:41:33 +0000 (00:41 -0700)
committerTomas Mraz <tomas@openssl.foundation>
Wed, 6 May 2026 16:50:27 +0000 (18:50 +0200)
The SM2 encryption may write past the caller-provided output buffer
when the required ciphertext size exceeds the supplied buffer length.

Reject outputs that do not fit in the caller-provided buffer.

Reviewed-by: Viktor Dukhovni <viktor@openssl.org>
Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.foundation>
MergeDate: Wed May  6 16:51:00 2026
(Merged from https://github.com/openssl/openssl/pull/30614)

crypto/sm2/sm2_crypt.c

index 37993bc8c3f8a5da7fd7ecd54aca22ec18b78de1..e7ae6a8bd0bd89ba069b62801a767938121bae8e 100644 (file)
@@ -253,12 +253,23 @@ again:
         goto done;
     }
 
-    ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
+    ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, NULL);
     /* Ensure cast to size_t is safe */
     if (ciphertext_leni < 0) {
         ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
         goto done;
     }
+
+    if (*ciphertext_len < (size_t)ciphertext_leni) {
+        ERR_raise(ERR_LIB_SM2, SM2_R_BUFFER_TOO_SMALL);
+        goto done;
+    }
+
+    ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
+    if (ciphertext_leni < 0) {
+        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
+        goto done;
+    }
     *ciphertext_len = (size_t)ciphertext_leni;
 
     rc = 1;