Blame SOURCES/0524-net-netbuff-Block-overly-large-netbuff-allocs.patch

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