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

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