dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0272-Verify-modules-on-build-time-rather-than-failing-in-.patch

f725e3
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f725e3
From: Vladimir Serbinenko <phcoder@gmail.com>
f725e3
Date: Thu, 31 Dec 2015 13:09:15 +0100
f725e3
Subject: [PATCH] Verify modules on build-time rather than failing in runtime.
f725e3
f725e3
---
f725e3
 util/grub-module-verifier.c    | 122 ++++++++++++++++
f725e3
 util/grub-module-verifier32.c  |   2 +
f725e3
 util/grub-module-verifier64.c  |   2 +
f725e3
 util/grub-module-verifierXX.c  | 315 +++++++++++++++++++++++++++++++++++++++++
f725e3
 include/grub/module_verifier.h |  19 +++
f725e3
 grub-core/Makefile.am          |   6 +-
f725e3
 grub-core/genmod.sh.in         |   5 +-
f725e3
 7 files changed, 468 insertions(+), 3 deletions(-)
f725e3
 create mode 100644 util/grub-module-verifier.c
f725e3
 create mode 100644 util/grub-module-verifier32.c
f725e3
 create mode 100644 util/grub-module-verifier64.c
f725e3
 create mode 100644 util/grub-module-verifierXX.c
f725e3
 create mode 100644 include/grub/module_verifier.h
f725e3
f725e3
diff --git a/util/grub-module-verifier.c b/util/grub-module-verifier.c
f725e3
new file mode 100644
f725e3
index 00000000000..d2d6984033c
f725e3
--- /dev/null
f725e3
+++ b/util/grub-module-verifier.c
f725e3
@@ -0,0 +1,122 @@
f725e3
+#include <stdio.h>
f725e3
+#include <string.h>
f725e3
+
f725e3
+#include <grub/elf.h>
f725e3
+#include <grub/module_verifier.h>
f725e3
+#include <grub/misc.h>
f725e3
+#include <grub/util/misc.h>
f725e3
+
f725e3
+struct grub_module_verifier_arch archs[] = {
f725e3
+  { "i386", 4, 0, EM_386, GRUB_MODULE_VERIFY_SUPPORTS_REL, (int[]){
f725e3
+      R_386_32,
f725e3
+      R_386_PC32,
f725e3
+      -1
f725e3
+    } },
f725e3
+  { "x86_64", 8, 0, EM_X86_64, GRUB_MODULE_VERIFY_SUPPORTS_RELA, (int[]){
f725e3
+      R_X86_64_64,
f725e3
+      R_X86_64_PC64,
f725e3
+      /* R_X86_64_32, R_X86_64_32S, R_X86_64_PC32 are supported but shouldn't be used because of their limited range.  */
f725e3
+      -1
f725e3
+    } },
f725e3
+  { "powerpc", 4, 1, EM_PPC, GRUB_MODULE_VERIFY_SUPPORTS_RELA, (int[]){
f725e3
+      GRUB_ELF_R_PPC_ADDR16_LO,
f725e3
+      GRUB_ELF_R_PPC_REL24, /* It has limited range but GRUB adds trampolines when necessarry.  */
f725e3
+      GRUB_ELF_R_PPC_ADDR16_HA,
f725e3
+      GRUB_ELF_R_PPC_ADDR32,
f725e3
+      GRUB_ELF_R_PPC_REL32,
f725e3
+      -1
f725e3
+    } },
f725e3
+  { "sparc64", 8, 1, EM_SPARCV9, GRUB_MODULE_VERIFY_SUPPORTS_RELA, (int[]){
f725e3
+      R_SPARC_WDISP30, /* It has limited range but GRUB adds trampolines when necessarry. */
f725e3
+      R_SPARC_HH22,
f725e3
+      R_SPARC_HM10,
f725e3
+      R_SPARC_LM22,
f725e3
+      R_SPARC_LO10,
f725e3
+      R_SPARC_64,
f725e3
+      R_SPARC_OLO10,
f725e3
+      /* R_SPARC_32, R_SPARC_HI22  are supported but shouldn't be used because of their limited range.  */
f725e3
+      -1
f725e3
+    } },
f725e3
+  { "ia64", 8, 0, EM_IA_64, GRUB_MODULE_VERIFY_SUPPORTS_RELA, (int[]){
f725e3
+      R_IA64_PCREL21B, /* We should verify that it's pointing either
f725e3
+			  to a function or to a section in the same module.  */
f725e3
+      R_IA64_SEGREL64LSB,
f725e3
+      R_IA64_FPTR64LSB,
f725e3
+      R_IA64_DIR64LSB,
f725e3
+      R_IA64_PCREL64LSB,
f725e3
+      R_IA64_GPREL22,  /* We should verify that it's pointing  to a section in the same module.  */
f725e3
+      R_IA64_LTOFF22X,
f725e3
+      R_IA64_LTOFF22,
f725e3
+      R_IA64_LTOFF_FPTR22,
f725e3
+      R_IA64_LDXMOV,
f725e3
+      -1
f725e3
+    } },
f725e3
+  { "mipsel", 4, 0, EM_MIPS, GRUB_MODULE_VERIFY_SUPPORTS_REL | GRUB_MODULE_VERIFY_SUPPORTS_RELA, (int[]){
f725e3
+      R_MIPS_HI16,
f725e3
+      R_MIPS_LO16,
f725e3
+      R_MIPS_32,
f725e3
+      R_MIPS_GPREL32,
f725e3
+      R_MIPS_26,
f725e3
+      R_MIPS_GOT16,
f725e3
+      R_MIPS_CALL16,
f725e3
+      R_MIPS_JALR,
f725e3
+      -1
f725e3
+    } },
f725e3
+  { "mips", 4, 1, EM_MIPS, GRUB_MODULE_VERIFY_SUPPORTS_REL | GRUB_MODULE_VERIFY_SUPPORTS_RELA, (int[]){
f725e3
+      R_MIPS_HI16,
f725e3
+      R_MIPS_LO16,
f725e3
+      R_MIPS_32,
f725e3
+      R_MIPS_GPREL32,
f725e3
+      R_MIPS_26,
f725e3
+      R_MIPS_GOT16,
f725e3
+      R_MIPS_CALL16,
f725e3
+      R_MIPS_JALR,
f725e3
+      -1
f725e3
+    } },
f725e3
+  { "arm", 4, 0, EM_ARM, GRUB_MODULE_VERIFY_SUPPORTS_REL, (int[]){
f725e3
+      /* Some relocations are range-limited but trampolines are added when necessarry. */
f725e3
+      R_ARM_ABS32,
f725e3
+      R_ARM_CALL,
f725e3
+      R_ARM_JUMP24,
f725e3
+      R_ARM_THM_CALL,
f725e3
+      R_ARM_THM_JUMP24,
f725e3
+      R_ARM_V4BX,
f725e3
+      R_ARM_THM_MOVW_ABS_NC,
f725e3
+      R_ARM_THM_MOVT_ABS,
f725e3
+      R_ARM_THM_JUMP19,
f725e3
+      -1
f725e3
+    } },
f725e3
+  { "arm64", 8, 0, EM_AARCH64, GRUB_MODULE_VERIFY_SUPPORTS_REL | GRUB_MODULE_VERIFY_SUPPORTS_RELA, (int[]){
f725e3
+      R_AARCH64_ABS64,
f725e3
+      R_AARCH64_CALL26,
f725e3
+      R_AARCH64_JUMP26,
f725e3
+      -1
f725e3
+    } },
f725e3
+};
f725e3
+
f725e3
+
f725e3
+int
f725e3
+main (int argc, char **argv)
f725e3
+{
f725e3
+  size_t module_size;
f725e3
+  unsigned arch;
f725e3
+  char *module_img;
f725e3
+  if (argc != 3) {
f725e3
+    fprintf (stderr, "usage: %s FILE ARCH\n", argv[0]);
f725e3
+    return 1;
f725e3
+  }
f725e3
+
f725e3
+  for (arch = 0; arch < ARRAY_SIZE(archs); arch++)
f725e3
+    if (strcmp(archs[arch].name, argv[2]) == 0)
f725e3
+      break;
f725e3
+  if (arch == ARRAY_SIZE(archs))
f725e3
+    grub_util_error("unknown arch: %s", argv[2]);
f725e3
+
f725e3
+  module_size = grub_util_get_image_size (argv[1]);
f725e3
+  module_img = grub_util_read_image (argv[1]);
f725e3
+  if (archs[arch].voidp_sizeof == 8)
f725e3
+    grub_module_verify64(module_img, module_size, &archs[arch]);
f725e3
+  else
f725e3
+    grub_module_verify32(module_img, module_size, &archs[arch]);
f725e3
+  return 0;
f725e3
+}
f725e3
diff --git a/util/grub-module-verifier32.c b/util/grub-module-verifier32.c
f725e3
new file mode 100644
f725e3
index 00000000000..257229f8f08
f725e3
--- /dev/null
f725e3
+++ b/util/grub-module-verifier32.c
f725e3
@@ -0,0 +1,2 @@
f725e3
+#define MODULEVERIFIER_ELF32 1
f725e3
+#include "grub-module-verifierXX.c"
f725e3
diff --git a/util/grub-module-verifier64.c b/util/grub-module-verifier64.c
f725e3
new file mode 100644
f725e3
index 00000000000..4db6b4bedd1
f725e3
--- /dev/null
f725e3
+++ b/util/grub-module-verifier64.c
f725e3
@@ -0,0 +1,2 @@
f725e3
+#define MODULEVERIFIER_ELF64 1
f725e3
+#include "grub-module-verifierXX.c"
f725e3
diff --git a/util/grub-module-verifierXX.c b/util/grub-module-verifierXX.c
f725e3
new file mode 100644
f725e3
index 00000000000..904be27d39b
f725e3
--- /dev/null
f725e3
+++ b/util/grub-module-verifierXX.c
f725e3
@@ -0,0 +1,315 @@
f725e3
+#include <string.h>
f725e3
+
f725e3
+#include <grub/elf.h>
f725e3
+#include <grub/module_verifier.h>
f725e3
+#include <grub/util/misc.h>
f725e3
+
f725e3
+#if defined(MODULEVERIFIER_ELF32)
f725e3
+# define SUFFIX(x)	x ## 32
f725e3
+# define ELFCLASSXX	ELFCLASS32
f725e3
+# define Elf_Ehdr	Elf32_Ehdr
f725e3
+# define Elf_Phdr	Elf32_Phdr
f725e3
+# define Elf_Nhdr	Elf32_Nhdr
f725e3
+# define Elf_Addr	Elf32_Addr
f725e3
+# define Elf_Sym	Elf32_Sym
f725e3
+# define Elf_Off	Elf32_Off
f725e3
+# define Elf_Shdr	Elf32_Shdr
f725e3
+# define Elf_Rela       Elf32_Rela
f725e3
+# define Elf_Rel        Elf32_Rel
f725e3
+# define Elf_Word       Elf32_Word
f725e3
+# define Elf_Half       Elf32_Half
f725e3
+# define Elf_Section    Elf32_Section
f725e3
+# define ELF_R_SYM(val)		ELF32_R_SYM(val)
f725e3
+# define ELF_R_TYPE(val)		ELF32_R_TYPE(val)
f725e3
+# define ELF_ST_TYPE(val)		ELF32_ST_TYPE(val)
f725e3
+#elif defined(MODULEVERIFIER_ELF64)
f725e3
+# define SUFFIX(x)	x ## 64
f725e3
+# define ELFCLASSXX	ELFCLASS64
f725e3
+# define Elf_Ehdr	Elf64_Ehdr
f725e3
+# define Elf_Phdr	Elf64_Phdr
f725e3
+# define Elf_Nhdr	Elf64_Nhdr
f725e3
+# define Elf_Addr	Elf64_Addr
f725e3
+# define Elf_Sym	Elf64_Sym
f725e3
+# define Elf_Off	Elf64_Off
f725e3
+# define Elf_Shdr	Elf64_Shdr
f725e3
+# define Elf_Rela       Elf64_Rela
f725e3
+# define Elf_Rel        Elf64_Rel
f725e3
+# define Elf_Word       Elf64_Word
f725e3
+# define Elf_Half       Elf64_Half
f725e3
+# define Elf_Section    Elf64_Section
f725e3
+# define ELF_R_SYM(val)		ELF64_R_SYM(val)
f725e3
+# define ELF_R_TYPE(val)		ELF64_R_TYPE(val)
f725e3
+# define ELF_ST_TYPE(val)		ELF64_ST_TYPE(val)
f725e3
+#else
f725e3
+#error "I'm confused"
f725e3
+#endif
f725e3
+
f725e3
+#define grub_target_to_host32(x) (grub_target_to_host32_real (arch, (x)))
f725e3
+#define grub_host_to_target32(x) (grub_host_to_target32_real (arch, (x)))
f725e3
+#define grub_target_to_host64(x) (grub_target_to_host64_real (arch, (x)))
f725e3
+#define grub_host_to_target64(x) (grub_host_to_target64_real (arch, (x)))
f725e3
+#define grub_host_to_target_addr(x) (grub_host_to_target_addr_real (arch, (x)))
f725e3
+#define grub_target_to_host16(x) (grub_target_to_host16_real (arch, (x)))
f725e3
+#define grub_host_to_target16(x) (grub_host_to_target16_real (arch, (x)))
f725e3
+#define grub_target_to_host(val) grub_target_to_host_real(arch, (val))
f725e3
+
f725e3
+static inline grub_uint32_t
f725e3
+grub_target_to_host32_real (const struct grub_module_verifier_arch *arch,
f725e3
+			    grub_uint32_t in)
f725e3
+{
f725e3
+  if (arch->bigendian)
f725e3
+    return grub_be_to_cpu32 (in);
f725e3
+  else
f725e3
+    return grub_le_to_cpu32 (in);
f725e3
+}
f725e3
+
f725e3
+static inline grub_uint64_t
f725e3
+grub_target_to_host64_real (const struct grub_module_verifier_arch *arch,
f725e3
+			    grub_uint64_t in)
f725e3
+{
f725e3
+  if (arch->bigendian)
f725e3
+    return grub_be_to_cpu64 (in);
f725e3
+  else
f725e3
+    return grub_le_to_cpu64 (in);
f725e3
+}
f725e3
+
f725e3
+static inline grub_uint64_t
f725e3
+grub_host_to_target64_real (const struct grub_module_verifier_arch *arch,
f725e3
+			    grub_uint64_t in)
f725e3
+{
f725e3
+  if (arch->bigendian)
f725e3
+    return grub_cpu_to_be64 (in);
f725e3
+  else
f725e3
+    return grub_cpu_to_le64 (in);
f725e3
+}
f725e3
+
f725e3
+static inline grub_uint32_t
f725e3
+grub_host_to_target32_real (const struct grub_module_verifier_arch *arch,
f725e3
+			    grub_uint32_t in)
f725e3
+{
f725e3
+  if (arch->bigendian)
f725e3
+    return grub_cpu_to_be32 (in);
f725e3
+  else
f725e3
+    return grub_cpu_to_le32 (in);
f725e3
+}
f725e3
+
f725e3
+static inline grub_uint16_t
f725e3
+grub_target_to_host16_real (const struct grub_module_verifier_arch *arch,
f725e3
+			    grub_uint16_t in)
f725e3
+{
f725e3
+  if (arch->bigendian)
f725e3
+    return grub_be_to_cpu16 (in);
f725e3
+  else
f725e3
+    return grub_le_to_cpu16 (in);
f725e3
+}
f725e3
+
f725e3
+static inline grub_uint16_t
f725e3
+grub_host_to_target16_real (const struct grub_module_verifier_arch *arch,
f725e3
+			    grub_uint16_t in)
f725e3
+{
f725e3
+  if (arch->bigendian)
f725e3
+    return grub_cpu_to_be16 (in);
f725e3
+  else
f725e3
+    return grub_cpu_to_le16 (in);
f725e3
+}
f725e3
+
f725e3
+static inline grub_uint64_t
f725e3
+grub_host_to_target_addr_real (const struct grub_module_verifier_arch *arch, grub_uint64_t in)
f725e3
+{
f725e3
+  if (arch->voidp_sizeof == 8)
f725e3
+    return grub_host_to_target64_real (arch, in);
f725e3
+  else
f725e3
+    return grub_host_to_target32_real (arch, in);
f725e3
+}
f725e3
+
f725e3
+static inline grub_uint64_t
f725e3
+grub_target_to_host_real (const struct grub_module_verifier_arch *arch, grub_uint64_t in)
f725e3
+{
f725e3
+  if (arch->voidp_sizeof == 8)
f725e3
+    return grub_target_to_host64_real (arch, in);
f725e3
+  else
f725e3
+    return grub_target_to_host32_real (arch, in);
f725e3
+}
f725e3
+
f725e3
+
f725e3
+static Elf_Shdr *
f725e3
+find_section (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, const char *name)
f725e3
+{
f725e3
+  Elf_Shdr *s;
f725e3
+  const char *str;
f725e3
+  unsigned i;
f725e3
+
f725e3
+  s = (Elf_Shdr *) ((char *) e + grub_target_to_host (e->e_shoff) + grub_target_to_host16 (e->e_shstrndx) * grub_target_to_host16 (e->e_shentsize));
f725e3
+  str = (char *) e + grub_target_to_host (s->sh_offset);
f725e3
+
f725e3
+  for (i = 0, s = (Elf_Shdr *) ((char *) e + grub_target_to_host (e->e_shoff));
f725e3
+       i < grub_target_to_host16 (e->e_shnum);
f725e3
+       i++, s = (Elf_Shdr *) ((char *) s + grub_target_to_host16 (e->e_shentsize)))
f725e3
+    if (strcmp (str + grub_target_to_host32 (s->sh_name), name) == 0)
f725e3
+      return s;
f725e3
+  return NULL;
f725e3
+}
f725e3
+
f725e3
+static void
f725e3
+check_license (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e)
f725e3
+{
f725e3
+  Elf_Shdr *s = find_section (arch, e, ".module_license");
f725e3
+  if (s && (strcmp ((char *) e + grub_target_to_host(s->sh_offset), "LICENSE=GPLv3") == 0
f725e3
+	    || strcmp ((char *) e + grub_target_to_host(s->sh_offset), "LICENSE=GPLv3+") == 0
f725e3
+	    || strcmp ((char *) e + grub_target_to_host(s->sh_offset), "LICENSE=GPLv2+") == 0))
f725e3
+    return;
f725e3
+  grub_util_error ("incompatible license");
f725e3
+}
f725e3
+
f725e3
+static void
f725e3
+check_symbols (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e)
f725e3
+{
f725e3
+  unsigned i;
f725e3
+  Elf_Shdr *s, *sections;
f725e3
+  Elf_Sym *sym;
f725e3
+  const char *str;
f725e3
+  Elf_Word size, entsize;
f725e3
+
f725e3
+  sections = (Elf_Shdr *) ((char *) e + grub_target_to_host (e->e_shoff));
f725e3
+  for (i = 0, s = sections;
f725e3
+       i < grub_target_to_host16 (e->e_shnum);
f725e3
+       i++, s = (Elf_Shdr *) ((char *) s + grub_target_to_host16 (e->e_shentsize)))
f725e3
+    if (grub_target_to_host32 (s->sh_type) == SHT_SYMTAB)
f725e3
+      break;
f725e3
+
f725e3
+  if (i == grub_target_to_host16 (e->e_shnum))
f725e3
+    grub_util_error ("no symbol table");
f725e3
+
f725e3
+  sym = (Elf_Sym *) ((char *) e + grub_target_to_host (s->sh_offset));
f725e3
+  size = grub_target_to_host (s->sh_size);
f725e3
+  entsize = grub_target_to_host (s->sh_entsize);
f725e3
+
f725e3
+  s = (Elf_Shdr *) ((char *) sections + grub_target_to_host16 (e->e_shentsize) * grub_target_to_host32 (s->sh_link));
f725e3
+  str = (char *) e + grub_target_to_host (s->sh_offset);
f725e3
+
f725e3
+  for (i = 0;
f725e3
+       i < size / entsize;
f725e3
+       i++, sym = (Elf_Sym *) ((char *) sym + entsize))
f725e3
+    {
f725e3
+      unsigned char type = ELF_ST_TYPE (sym->st_info);
f725e3
+
f725e3
+      switch (type)
f725e3
+	{
f725e3
+	case STT_NOTYPE:
f725e3
+	case STT_OBJECT:
f725e3
+	case STT_FUNC:
f725e3
+	case STT_SECTION:
f725e3
+	case STT_FILE:
f725e3
+	  break;
f725e3
+
f725e3
+	default:
f725e3
+	  return grub_util_error ("unknown symbol type `%d'", (int) type);
f725e3
+	}
f725e3
+    }
f725e3
+}
f725e3
+
f725e3
+/* Relocate symbols.  */
f725e3
+static void
f725e3
+section_check_relocations (const struct grub_module_verifier_arch *arch, void *ehdr,
f725e3
+			   Elf_Shdr *s, size_t target_seg_size)
f725e3
+{
f725e3
+  Elf_Rel *rel, *max;
f725e3
+
f725e3
+  for (rel = (Elf_Rel *) ((char *) ehdr + grub_target_to_host (s->sh_offset)),
f725e3
+	 max = (Elf_Rel *) ((char *) rel + grub_target_to_host (s->sh_size));
f725e3
+       rel < max;
f725e3
+       rel = (Elf_Rel *) ((char *) rel + grub_target_to_host (s->sh_entsize)))
f725e3
+    {
f725e3
+      Elf_Word *addr;
f725e3
+      Elf_Sym *sym;
f725e3
+      unsigned i;
f725e3
+
f725e3
+      if (target_seg_size < grub_target_to_host (rel->r_offset))
f725e3
+	grub_util_error ("reloc offset is out of the segment");
f725e3
+
f725e3
+      grub_uint32_t type = ELF_R_TYPE (grub_target_to_host (rel->r_info));
f725e3
+
f725e3
+      if (arch->machine == EM_SPARCV9)
f725e3
+	type &= 0xff;
f725e3
+
f725e3
+      for (i = 0; arch->supported_relocations[i] != -1; i++)
f725e3
+	if (type == arch->supported_relocations[i])
f725e3
+	  break;
f725e3
+      if (arch->supported_relocations[i] == -1)
f725e3
+	grub_util_error ("unsupported relocation 0x%x", type);
f725e3
+    }
f725e3
+}
f725e3
+
f725e3
+static void
f725e3
+check_relocations (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e)
f725e3
+{
f725e3
+  Elf_Shdr *s;
f725e3
+  unsigned i;
f725e3
+
f725e3
+  for (i = 0, s = (Elf_Shdr *) ((char *) e + grub_target_to_host (e->e_shoff));
f725e3
+       i < grub_target_to_host16 (e->e_shnum);
f725e3
+       i++, s = (Elf_Shdr *) ((char *) s + grub_target_to_host16 (e->e_shentsize)))
f725e3
+    if (grub_target_to_host32 (s->sh_type) == SHT_REL || grub_target_to_host32 (s->sh_type) == SHT_RELA)
f725e3
+      {
f725e3
+	Elf_Shdr *ts;
f725e3
+
f725e3
+	if (grub_target_to_host32 (s->sh_type) == SHT_REL && !(arch->flags & GRUB_MODULE_VERIFY_SUPPORTS_REL))
f725e3
+	  grub_util_error ("unsupported SHT_REL");
f725e3
+	if (grub_target_to_host32 (s->sh_type) == SHT_RELA && !(arch->flags & GRUB_MODULE_VERIFY_SUPPORTS_RELA))
f725e3
+	  grub_util_error ("unsupported SHT_RELA");
f725e3
+
f725e3
+	/* Find the target segment.  */
f725e3
+	if (grub_target_to_host32 (s->sh_info) >= grub_target_to_host16 (e->e_shnum))
f725e3
+	  grub_util_error ("orphaned reloc section");
f725e3
+	ts = (Elf_Shdr *) ((char *) e + grub_target_to_host (e->e_shoff) + grub_target_to_host32 (s->sh_info) * grub_target_to_host16 (e->e_shentsize));
f725e3
+
f725e3
+	section_check_relocations (arch, e, s, grub_target_to_host (ts->sh_size));
f725e3
+      }
f725e3
+}
f725e3
+
f725e3
+void
f725e3
+SUFFIX(grub_module_verify) (void *module_img, size_t size, const struct grub_module_verifier_arch *arch)
f725e3
+{
f725e3
+  Elf_Ehdr *e = module_img;
f725e3
+
f725e3
+  /* Check the header size.  */
f725e3
+  if (size < sizeof (Elf_Ehdr))
f725e3
+    grub_util_error ("ELF header smaller than expected");
f725e3
+
f725e3
+  /* Check the magic numbers.  */
f725e3
+  if (e->e_ident[EI_MAG0] != ELFMAG0
f725e3
+      || e->e_ident[EI_MAG1] != ELFMAG1
f725e3
+      || e->e_ident[EI_MAG2] != ELFMAG2
f725e3
+      || e->e_ident[EI_MAG3] != ELFMAG3
f725e3
+      || e->e_ident[EI_VERSION] != EV_CURRENT
f725e3
+      || grub_target_to_host32 (e->e_version) != EV_CURRENT)
f725e3
+    grub_util_error ("invalid arch-independent ELF magic");
f725e3
+
f725e3
+  if (e->e_ident[EI_CLASS] != ELFCLASSXX
f725e3
+      || e->e_ident[EI_DATA] != (arch->bigendian ? ELFDATA2MSB : ELFDATA2LSB)
f725e3
+      || grub_target_to_host16 (e->e_machine) != arch->machine)
f725e3
+    grub_util_error ("invalid arch-dependent ELF magic");
f725e3
+
f725e3
+  if (grub_target_to_host16 (e->e_type) != ET_REL)
f725e3
+    {
f725e3
+      grub_util_error ("this ELF file is not of the right type");
f725e3
+    }
f725e3
+
f725e3
+  /* Make sure that every section is within the core.  */
f725e3
+  if (size < grub_target_to_host (e->e_shoff)
f725e3
+      + grub_target_to_host16 (e->e_shentsize) * grub_target_to_host16(e->e_shnum))
f725e3
+    {
f725e3
+      grub_util_error ("ELF sections outside core");
f725e3
+    }
f725e3
+
f725e3
+  check_license (arch, e);
f725e3
+
f725e3
+  Elf_Shdr *s;
f725e3
+
f725e3
+  s = find_section (arch, e, ".modname");
f725e3
+  if (!s)
f725e3
+    grub_util_error ("no module name found");
f725e3
+
f725e3
+  check_symbols(arch, e);
f725e3
+  check_relocations(arch, e);
f725e3
+}
f725e3
diff --git a/include/grub/module_verifier.h b/include/grub/module_verifier.h
f725e3
new file mode 100644
f725e3
index 00000000000..9e3a2ba720f
f725e3
--- /dev/null
f725e3
+++ b/include/grub/module_verifier.h
f725e3
@@ -0,0 +1,19 @@
f725e3
+#include <stdint.h>
f725e3
+#include <stdlib.h>
f725e3
+
f725e3
+#include <grub/types.h>
f725e3
+
f725e3
+#define GRUB_MODULE_VERIFY_SUPPORTS_REL 1
f725e3
+#define GRUB_MODULE_VERIFY_SUPPORTS_RELA 2
f725e3
+
f725e3
+struct grub_module_verifier_arch {
f725e3
+  const char *name;
f725e3
+  int voidp_sizeof;
f725e3
+  int bigendian;
f725e3
+  int machine;
f725e3
+  int flags;
f725e3
+  const int *supported_relocations;
f725e3
+};
f725e3
+
f725e3
+void grub_module_verify64(void *module_img, size_t module_size, const struct grub_module_verifier_arch *arch);
f725e3
+void grub_module_verify32(void *module_img, size_t module_size, const struct grub_module_verifier_arch *arch);
f725e3
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
f725e3
index be29e327f77..77205f16358 100644
f725e3
--- a/grub-core/Makefile.am
f725e3
+++ b/grub-core/Makefile.am
f725e3
@@ -39,6 +39,10 @@ gentrigtables$(BUILD_EXEEXT): gentrigtables.c
f725e3
 	$(BUILD_CC) -o $@ -I$(top_srcdir)/include $(BUILD_CFLAGS) $(BUILD_CPPFLAGS) $(BUILD_LDFLAGS) $< $(BUILD_LIBM)
f725e3
 CLEANFILES += gentrigtables$(BUILD_EXEEXT)
f725e3
 
f725e3
+build-grub-module-verifier$(BUILD_EXEEXT): $(top_srcdir)/util/grub-module-verifier.c $(top_srcdir)/util/grub-module-verifier32.c $(top_srcdir)/util/grub-module-verifier64.c $(top_srcdir)/grub-core/kern/emu/misc.c $(top_srcdir)/util/misc.c
f725e3
+	$(BUILD_CC) -o $@ -I$(top_srcdir)/include $(BUILD_CFLAGS) $(BUILD_CPPFLAGS) $(BUILD_LDFLAGS) -DGRUB_BUILD=1 -DGRUB_UTIL=1 -DGRUB_BUILD_PROGRAM_NAME=\"build-grub-module-verifier\" $^
f725e3
+CLEANFILES += build-grub-module-verifier$(BUILD_EXEEXT)
f725e3
+
f725e3
 # trigtables.c
f725e3
 trigtables.c: gentrigtables$(BUILD_EXEEXT) gentrigtables.c $(top_srcdir)/configure.ac
f725e3
 	./gentrigtables$(BUILD_EXEEXT) > $@
f725e3
@@ -387,7 +391,7 @@ moddep.lst: syminfo.lst genmoddep.awk video.lst
f725e3
 platform_DATA += moddep.lst
f725e3
 CLEANFILES += config.log syminfo.lst moddep.lst
f725e3
 
f725e3
-$(MOD_FILES): %.mod : genmod.sh moddep.lst %.module$(EXEEXT)
f725e3
+$(MOD_FILES): %.mod : genmod.sh moddep.lst %.module$(EXEEXT) build-grub-module-verifier
f725e3
 	TARGET_OBJ2ELF=@TARGET_OBJ2ELF@ sh $^ $@
f725e3
 platform_DATA += $(MOD_FILES)
f725e3
 platform_DATA += modinfo.sh
f725e3
diff --git a/grub-core/genmod.sh.in b/grub-core/genmod.sh.in
f725e3
index 789732b108b..7dcafd9d370 100644
f725e3
--- a/grub-core/genmod.sh.in
f725e3
+++ b/grub-core/genmod.sh.in
f725e3
@@ -15,12 +15,12 @@ set -e
f725e3
 #
f725e3
 # Example:
f725e3
 #
f725e3
-# genmod.sh moddep.lst normal.module normal.mod
f725e3
+# genmod.sh moddep.lst normal.module build-grub-module-verifier normal.mod
f725e3
 #
f725e3
 
f725e3
 moddep=$1
f725e3
 infile=$2
f725e3
-outfile=$3
f725e3
+outfile=$4
f725e3
 
f725e3
 tmpfile=${outfile}.tmp
f725e3
 modname=`echo $infile | sed -e 's@\.module.*$@@'`
f725e3
@@ -93,4 +93,5 @@ else
f725e3
 	    -wd1106 -nu -nd $tmpfile.bin $tmpfile || exit 1
f725e3
 	rm -f $name.bin
f725e3
 fi
f725e3
+./build-grub-module-verifier $tmpfile @target_cpu@
f725e3
 mv $tmpfile $outfile