Blame SOURCES/0298-mm-Assert-that-we-preserve-header-vs-region-alignmen.patch

b35c50
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b35c50
From: Daniel Axtens <dja@axtens.net>
b35c50
Date: Thu, 21 Apr 2022 15:24:14 +1000
b35c50
Subject: [PATCH] mm: Assert that we preserve header vs region alignment
b35c50
b35c50
grub_mm_region_init() does:
b35c50
b35c50
  h = (grub_mm_header_t) (r + 1);
b35c50
b35c50
where h is a grub_mm_header_t and r is a grub_mm_region_t.
b35c50
b35c50
Cells are supposed to be GRUB_MM_ALIGN aligned, but while grub_mm_dump
b35c50
ensures this vs the region header, grub_mm_region_init() does not.
b35c50
b35c50
It's better to be explicit than implicit here: rather than changing
b35c50
grub_mm_region_init() to ALIGN_UP(), require that the struct is
b35c50
explicitly a multiple of the header size.
b35c50
b35c50
Signed-off-by: Daniel Axtens <dja@axtens.net>
b35c50
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
b35c50
Tested-by: Patrick Steinhardt <ps@pks.im>
b35c50
(cherry picked from commit 1df8fe66c57087eb33bd6dc69f786ed124615aa7)
b35c50
---
b35c50
 include/grub/mm_private.h | 14 ++++++++++++++
b35c50
 1 file changed, 14 insertions(+)
b35c50
b35c50
diff --git a/include/grub/mm_private.h b/include/grub/mm_private.h
b35c50
index 203533cc3d..a688b92a83 100644
b35c50
--- a/include/grub/mm_private.h
b35c50
+++ b/include/grub/mm_private.h
b35c50
@@ -20,6 +20,7 @@
b35c50
 #define GRUB_MM_PRIVATE_H	1
b35c50
 
b35c50
 #include <grub/mm.h>
b35c50
+#include <grub/misc.h>
b35c50
 
b35c50
 /* For context, see kern/mm.c */
b35c50
 
b35c50
@@ -89,4 +90,17 @@ typedef struct grub_mm_region
b35c50
 extern grub_mm_region_t EXPORT_VAR (grub_mm_base);
b35c50
 #endif
b35c50
 
b35c50
+static inline void
b35c50
+grub_mm_size_sanity_check (void) {
b35c50
+  /* Ensure we preserve alignment when doing h = (grub_mm_header_t) (r + 1). */
b35c50
+  COMPILE_TIME_ASSERT ((sizeof (struct grub_mm_region) %
b35c50
+		        sizeof (struct grub_mm_header)) == 0);
b35c50
+
b35c50
+  /*
b35c50
+   * GRUB_MM_ALIGN is supposed to represent cell size, and a mm_header is
b35c50
+   * supposed to be 1 cell.
b35c50
+   */
b35c50
+  COMPILE_TIME_ASSERT (sizeof (struct grub_mm_header) == GRUB_MM_ALIGN);
b35c50
+}
b35c50
+
b35c50
 #endif