Blame SOURCES/libksba-1.5.1-overflow.patch

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