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

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