dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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