Blame SOURCES/libksba-1.5.1-overflow.patch

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