nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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