nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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