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

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