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

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