Blame SOURCES/0527-net-dns-Don-t-read-past-the-end-of-the-string-we-re-.patch

b9d01e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b9d01e
From: Daniel Axtens <dja@axtens.net>
b9d01e
Date: Mon, 20 Dec 2021 21:55:43 +1100
b9d01e
Subject: [PATCH] net/dns: Don't read past the end of the string we're checking
b9d01e
 against
b9d01e
b9d01e
I don't really understand what's going on here but fuzzing found
b9d01e
a bug where we read past the end of check_with. That's a C string,
b9d01e
so use grub_strlen() to make sure we don't overread it.
b9d01e
b9d01e
Signed-off-by: Daniel Axtens <dja@axtens.net>
b9d01e
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
b9d01e
(cherry picked from commit 6a97b3f4b1d5173aa516edc6dedbc63de7306d21)
b9d01e
(cherry picked from commit e0589624e86bc96666cbdb62f6e55cafec2871b3)
b9d01e
(cherry picked from commit 95ecbc0b9aacfd43ba96cccc50daaf39eccd9f7f)
b9d01e
---
b9d01e
 grub-core/net/dns.c | 19 ++++++++++++++++---
b9d01e
 1 file changed, 16 insertions(+), 3 deletions(-)
b9d01e
b9d01e
diff --git a/grub-core/net/dns.c b/grub-core/net/dns.c
b9d01e
index 135faac035..17961a9f18 100644
b9d01e
--- a/grub-core/net/dns.c
b9d01e
+++ b/grub-core/net/dns.c
b9d01e
@@ -146,11 +146,18 @@ check_name_real (const grub_uint8_t *name_at, const grub_uint8_t *head,
b9d01e
 		 int *length, char *set)
b9d01e
 {
b9d01e
   const char *readable_ptr = check_with;
b9d01e
+  int readable_len;
b9d01e
   const grub_uint8_t *ptr;
b9d01e
   char *optr = set;
b9d01e
   int bytes_processed = 0;
b9d01e
   if (length)
b9d01e
     *length = 0;
b9d01e
+
b9d01e
+  if (readable_ptr != NULL)
b9d01e
+    readable_len = grub_strlen (readable_ptr);
b9d01e
+  else
b9d01e
+    readable_len = 0;
b9d01e
+
b9d01e
   for (ptr = name_at; ptr < tail && bytes_processed < tail - head + 2; )
b9d01e
     {
b9d01e
       /* End marker.  */
b9d01e
@@ -172,13 +179,16 @@ check_name_real (const grub_uint8_t *name_at, const grub_uint8_t *head,
b9d01e
 	  ptr = head + (((ptr[0] & 0x3f) << 8) | ptr[1]);
b9d01e
 	  continue;
b9d01e
 	}
b9d01e
-      if (readable_ptr && grub_memcmp (ptr + 1, readable_ptr, *ptr) != 0)
b9d01e
+      if (readable_ptr != NULL && (*ptr > readable_len || grub_memcmp (ptr + 1, readable_ptr, *ptr) != 0))
b9d01e
 	return 0;
b9d01e
       if (grub_memchr (ptr + 1, 0, *ptr) 
b9d01e
 	  || grub_memchr (ptr + 1, '.', *ptr))
b9d01e
 	return 0;
b9d01e
       if (readable_ptr)
b9d01e
-	readable_ptr += *ptr;
b9d01e
+	{
b9d01e
+	  readable_ptr += *ptr;
b9d01e
+	  readable_len -= *ptr;
b9d01e
+	}
b9d01e
       if (readable_ptr && *readable_ptr != '.' && *readable_ptr != 0)
b9d01e
 	return 0;
b9d01e
       bytes_processed += *ptr + 1;
b9d01e
@@ -192,7 +202,10 @@ check_name_real (const grub_uint8_t *name_at, const grub_uint8_t *head,
b9d01e
       if (optr)
b9d01e
 	*optr++ = '.';
b9d01e
       if (readable_ptr && *readable_ptr)
b9d01e
-	readable_ptr++;
b9d01e
+	{
b9d01e
+	  readable_ptr++;
b9d01e
+	  readable_len--;
b9d01e
+	}
b9d01e
       ptr += *ptr + 1;
b9d01e
     }
b9d01e
   return 0;