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

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