4ea945
From 4b7d9cd4a018898d7714ce06f3faf2626c14582b Mon Sep 17 00:00:00 2001
4ea945
From: Werner Koch <wk@gnupg.org>
4ea945
Date: Wed, 5 Oct 2022 14:19:06 +0200
4ea945
Subject: [PATCH] Detect a possible overflow directly in the TLV parser.
4ea945
4ea945
* src/ber-help.c (_ksba_ber_read_tl): Check for overflow of a commonly
4ea945
used sum.
4ea945
--
4ea945
4ea945
It is quite common to have checks like
4ea945
4ea945
    if (ti.nhdr + ti.length >= DIM(tmpbuf))
4ea945
       return gpg_error (GPG_ERR_TOO_LARGE);
4ea945
4ea945
This patch detects possible integer overflows immmediately when
4ea945
creating the TI object.
4ea945
4ea945
Reported-by: ZDI-CAN-18927, ZDI-CAN-18928, ZDI-CAN-18929
4ea945
---
4ea945
 src/ber-help.c | 6 ++++++
4ea945
 1 file changed, 6 insertions(+)
4ea945
4ea945
diff --git a/src/ber-help.c b/src/ber-help.c
4ea945
index 81c31ed..56efb6a 100644
4ea945
--- a/src/ber-help.c
4ea945
+++ b/src/ber-help.c
4ea945
@@ -182,6 +182,12 @@ _ksba_ber_read_tl (ksba_reader_t reader, struct tag_info *ti)
4ea945
       ti->length = len;
4ea945
     }
4ea945
 
4ea945
+  if (ti->length > ti->nhdr && (ti->nhdr + ti->length) < ti->length)
4ea945
+    {
4ea945
+      ti->err_string = "header+length would overflow";
4ea945
+      return gpg_error (GPG_ERR_EOVERFLOW);
4ea945
+    }
4ea945
+
4ea945
   /* Without this kludge some example certs can't be parsed */
4ea945
   if (ti->class == CLASS_UNIVERSAL && !ti->tag)
4ea945
     ti->length = 0;
4ea945
-- 
4ea945
2.37.3
4ea945
d0e4bc
commit f61a5ea4e0f6a80fd4b28ef0174bee77793cf070
d0e4bc
Author: Werner Koch <wk@gnupg.org>
d0e4bc
Date:   Tue Nov 22 16:36:46 2022 +0100
d0e4bc
d0e4bc
    Fix an integer overflow in the CRL signature parser.
d0e4bc
    
d0e4bc
    * src/crl.c (parse_signature): N+N2 now checked for overflow.
d0e4bc
    
d0e4bc
    * src/ocsp.c (parse_response_extensions): Do not accept too large
d0e4bc
    values.
d0e4bc
    (parse_single_extensions): Ditto.
d0e4bc
    --
d0e4bc
    
d0e4bc
    The second patch is an extra safegourd not related to the reported
d0e4bc
    bug.
d0e4bc
    
d0e4bc
    GnuPG-bug-id: 6284
d0e4bc
    Reported-by: Joseph Surin, elttam
d0e4bc
d0e4bc
diff --git a/src/crl.c b/src/crl.c
d0e4bc
index 9f71c85..2e6ca29 100644
d0e4bc
--- a/src/crl.c
d0e4bc
+++ b/src/crl.c
d0e4bc
@@ -1349,7 +1349,7 @@ parse_signature (ksba_crl_t crl)
d0e4bc
          && !ti.is_constructed) )
d0e4bc
     return gpg_error (GPG_ERR_INV_CRL_OBJ);
d0e4bc
   n2 = ti.nhdr + ti.length;
d0e4bc
-  if (n + n2 >= DIM(tmpbuf))
d0e4bc
+  if (n + n2 >= DIM(tmpbuf) || (n + n2) < n)
d0e4bc
     return gpg_error (GPG_ERR_TOO_LARGE);
d0e4bc
   memcpy (tmpbuf+n, ti.buf, ti.nhdr);
d0e4bc
   err = read_buffer (crl->reader, tmpbuf+n+ti.nhdr, ti.length);
d0e4bc
diff --git a/src/ocsp.c b/src/ocsp.c
d0e4bc
index d4cba04..657d15f 100644
d0e4bc
--- a/src/ocsp.c
d0e4bc
+++ b/src/ocsp.c
d0e4bc
@@ -721,6 +721,12 @@ parse_response_extensions (ksba_ocsp_t ocsp,
d0e4bc
           else
d0e4bc
             ocsp->good_nonce = 1;
d0e4bc
         }
d0e4bc
+      if (ti.length > (1<<24))
d0e4bc
+        {
d0e4bc
+          /* Bail out on much too large objects.  */
d0e4bc
+          err = gpg_error (GPG_ERR_BAD_BER);
d0e4bc
+          goto leave;
d0e4bc
+        }
d0e4bc
       ex = xtrymalloc (sizeof *ex + strlen (oid) + ti.length);
d0e4bc
       if (!ex)
d0e4bc
         {
d0e4bc
@@ -788,6 +794,12 @@ parse_single_extensions (struct ocsp_reqitem_s *ri,
d0e4bc
       err = parse_octet_string (&data, &datalen, &ti);
d0e4bc
       if (err)
d0e4bc
         goto leave;
d0e4bc
+      if (ti.length > (1<<24))
d0e4bc
+        {
d0e4bc
+          /* Bail out on much too large objects.  */
d0e4bc
+          err = gpg_error (GPG_ERR_BAD_BER);
d0e4bc
+          goto leave;
d0e4bc
+        }
d0e4bc
       ex = xtrymalloc (sizeof *ex + strlen (oid) + ti.length);
d0e4bc
       if (!ex)
d0e4bc
         {