dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0416-disk-lvm-Don-t-blast-past-the-end-of-the-circular-me.patch

b1bcb2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b1bcb2
From: Daniel Axtens <dja@axtens.net>
b1bcb2
Date: Thu, 21 Jan 2021 18:19:51 +1100
b1bcb2
Subject: [PATCH] disk/lvm: Don't blast past the end of the circular metadata
b1bcb2
 buffer
b1bcb2
b1bcb2
This catches at least some OOB reads, and it's possible I suppose that
b1bcb2
if 2 * mda_size is less than GRUB_LVM_MDA_HEADER_SIZE it might catch some
b1bcb2
OOB writes too (although that hasn't showed up as a crash in fuzzing yet).
b1bcb2
b1bcb2
It's a bit ugly and I'd appreciate better suggestions.
b1bcb2
b1bcb2
Signed-off-by: Daniel Axtens <dja@axtens.net>
b1bcb2
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
b1bcb2
---
b1bcb2
 grub-core/disk/lvm.c | 10 ++++++++++
b1bcb2
 1 file changed, 10 insertions(+)
b1bcb2
b1bcb2
diff --git a/grub-core/disk/lvm.c b/grub-core/disk/lvm.c
b1bcb2
index 43e2f6ddf3c..7a8d61ffb20 100644
b1bcb2
--- a/grub-core/disk/lvm.c
b1bcb2
+++ b/grub-core/disk/lvm.c
b1bcb2
@@ -215,6 +215,16 @@ grub_lvm_detect (grub_disk_t disk,
b1bcb2
   if (grub_le_to_cpu64 (rlocn->offset) + grub_le_to_cpu64 (rlocn->size) >
b1bcb2
       grub_le_to_cpu64 (mdah->size))
b1bcb2
     {
b1bcb2
+      if (2 * mda_size < GRUB_LVM_MDA_HEADER_SIZE ||
b1bcb2
+          (grub_le_to_cpu64 (rlocn->offset) + grub_le_to_cpu64 (rlocn->size) -
b1bcb2
+	   grub_le_to_cpu64 (mdah->size) > mda_size - GRUB_LVM_MDA_HEADER_SIZE))
b1bcb2
+	{
b1bcb2
+#ifdef GRUB_UTIL
b1bcb2
+	  grub_util_info ("cannot copy metadata wrap in circular buffer");
b1bcb2
+#endif
b1bcb2
+	  goto fail2;
b1bcb2
+	}
b1bcb2
+
b1bcb2
       /* Metadata is circular. Copy the wrap in place. */
b1bcb2
       grub_memcpy (metadatabuf + mda_size,
b1bcb2
 		   metadatabuf + GRUB_LVM_MDA_HEADER_SIZE,