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

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