nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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