nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone
1c6ba0
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
1c6ba0
From: Daniel Axtens <dja@axtens.net>
1c6ba0
Date: Tue, 8 Mar 2022 23:47:46 +1100
1c6ba0
Subject: [PATCH] net/netbuff: Block overly large netbuff allocs
1c6ba0
1c6ba0
A netbuff shouldn't be too huge. It's bounded by MTU and TCP segment
1c6ba0
reassembly.
1c6ba0
1c6ba0
This helps avoid some bugs (and provides a spot to instrument to catch
1c6ba0
them at their source).
1c6ba0
1c6ba0
Signed-off-by: Daniel Axtens <dja@axtens.net>
1c6ba0
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
1c6ba0
(cherry picked from commit ee9591103004cd13b4efadda671536090ca7fd57)
1c6ba0
(cherry picked from commit acde668bb9d9fa862a1a63e3bbd5fa47fdfa9183)
1c6ba0
---
1c6ba0
 grub-core/net/netbuff.c | 13 +++++++++++++
1c6ba0
 1 file changed, 13 insertions(+)
1c6ba0
1c6ba0
diff --git a/grub-core/net/netbuff.c b/grub-core/net/netbuff.c
1c6ba0
index dbeeefe478..d5e9e9a0d7 100644
1c6ba0
--- a/grub-core/net/netbuff.c
1c6ba0
+++ b/grub-core/net/netbuff.c
1c6ba0
@@ -79,10 +79,23 @@ grub_netbuff_alloc (grub_size_t len)
1c6ba0
 
1c6ba0
   COMPILE_TIME_ASSERT (NETBUFF_ALIGN % sizeof (grub_properly_aligned_t) == 0);
1c6ba0
 
1c6ba0
+  /*
1c6ba0
+   * The largest size of a TCP packet is 64 KiB, and everything else
1c6ba0
+   * should be a lot smaller - most MTUs are 1500 or less. Cap data
1c6ba0
+   * size at 64 KiB + a buffer.
1c6ba0
+   */
1c6ba0
+  if (len > 0xffffUL + 0x1000UL)
1c6ba0
+    {
1c6ba0
+      grub_error (GRUB_ERR_BUG,
1c6ba0
+                  "attempted to allocate a packet that is too big");
1c6ba0
+      return NULL;
1c6ba0
+    }
1c6ba0
+
1c6ba0
   if (len < NETBUFFMINLEN)
1c6ba0
     len = NETBUFFMINLEN;
1c6ba0
 
1c6ba0
   len = ALIGN_UP (len, NETBUFF_ALIGN);
1c6ba0
+
1c6ba0
 #ifdef GRUB_MACHINE_EMU
1c6ba0
   data = grub_malloc (len + sizeof (*nb));
1c6ba0
 #else