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