d18179
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d18179
From: Daniel Axtens <dja@axtens.net>
d18179
Date: Mon, 20 Dec 2021 19:41:21 +1100
d18179
Subject: [PATCH] net/ip: Do IP fragment maths safely
d18179
d18179
This avoids an underflow and subsequent unpleasantness.
d18179
d18179
Fixes: CVE-2022-28733
d18179
d18179
Signed-off-by: Daniel Axtens <dja@axtens.net>
d18179
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
d18179
(cherry picked from commit eb74e5743ca7e18a5e75c392fe0b21d1549a1936)
d18179
(cherry picked from commit 552ad34583e788542e9ca08524a0d4bc8f98c297)
d18179
(cherry picked from commit 2c8cb7e3b8b48b136a950e5692fa6251b76df90e)
d18179
(cherry picked from commit ec7f66954d81f3bb28f4c93581e0c562c8608fa1)
d18179
---
d18179
 grub-core/net/ip.c | 10 +++++++++-
d18179
 1 file changed, 9 insertions(+), 1 deletion(-)
d18179
d18179
diff --git a/grub-core/net/ip.c b/grub-core/net/ip.c
d18179
index daa5b03516..bca6504c2e 100644
d18179
--- a/grub-core/net/ip.c
d18179
+++ b/grub-core/net/ip.c
d18179
@@ -25,6 +25,7 @@
d18179
 #include <grub/net/netbuff.h>
d18179
 #include <grub/mm.h>
d18179
 #include <grub/priority_queue.h>
d18179
+#include <grub/safemath.h>
d18179
 #include <grub/time.h>
d18179
 
d18179
 struct iphdr {
d18179
@@ -536,7 +537,14 @@ grub_net_recv_ip4_packets (struct grub_net_buff *nb,
d18179
     {
d18179
       rsm->total_len = (8 * (grub_be_to_cpu16 (iph->frags) & OFFSET_MASK)
d18179
 			+ (nb->tail - nb->data));
d18179
-      rsm->total_len -= ((iph->verhdrlen & 0xf) * sizeof (grub_uint32_t));
d18179
+
d18179
+      if (grub_sub (rsm->total_len, (iph->verhdrlen & 0xf) * sizeof (grub_uint32_t),
d18179
+		    &rsm->total_len))
d18179
+	{
d18179
+	  grub_dprintf ("net", "IP reassembly size underflow\n");
d18179
+	  return GRUB_ERR_NONE;
d18179
+	}
d18179
+
d18179
       rsm->asm_netbuff = grub_netbuff_alloc (rsm->total_len);
d18179
       if (!rsm->asm_netbuff)
d18179
 	{