Blame SOURCES/0273-module-verifier-Check-range-limited-relative-relocat.patch

f731ee
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f731ee
From: Vladimir Serbinenko <phcoder@gmail.com>
f731ee
Date: Thu, 31 Dec 2015 15:29:28 +0100
f731ee
Subject: [PATCH] module-verifier: Check range-limited relative relocations.
f731ee
f731ee
Check that they point to the same module, so will end up in the same
f731ee
chunk of memory.
f731ee
---
f731ee
 util/grub-module-verifier.c    | 19 ++++++++++---
f731ee
 util/grub-module-verifierXX.c  | 62 ++++++++++++++++++++++++++++++++++--------
f731ee
 include/grub/module_verifier.h |  1 +
f731ee
 3 files changed, 67 insertions(+), 15 deletions(-)
f731ee
f731ee
diff --git a/util/grub-module-verifier.c b/util/grub-module-verifier.c
f731ee
index d2d6984033c..c027f0a0fb1 100644
f731ee
--- a/util/grub-module-verifier.c
f731ee
+++ b/util/grub-module-verifier.c
f731ee
@@ -15,9 +15,13 @@ struct grub_module_verifier_arch archs[] = {
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
+      /* R_X86_64_32, R_X86_64_32S are supported but shouldn't be used because of their limited range.  */
f731ee
       -1
f731ee
-    } },
f731ee
+    }, (int[]){
f731ee
+      R_X86_64_PC32,
f731ee
+      -1
f731ee
+    }
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
@@ -39,17 +43,24 @@ struct grub_module_verifier_arch archs[] = {
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
+			  to a function or to a section in the same module.
f731ee
+			  Checking that external symbol is a function is
f731ee
+			  non-trivial and I have never seen this relocation used
f731ee
+			  for anything else, so assume that it always points to a
f731ee
+			  function.
f731ee
+		       */
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
+    }, (int[]){
f731ee
+      R_IA64_GPREL22,
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
diff --git a/util/grub-module-verifierXX.c b/util/grub-module-verifierXX.c
f731ee
index 904be27d39b..25988ebc21d 100644
f731ee
--- a/util/grub-module-verifierXX.c
f731ee
+++ b/util/grub-module-verifierXX.c
f731ee
@@ -161,14 +161,12 @@ check_license (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e)
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
+static Elf_Sym *
f731ee
+get_symtab (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, Elf_Word *size, Elf_Word *entsize)
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
@@ -181,11 +179,19 @@ check_symbols (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e)
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
+  *size = grub_target_to_host (s->sh_size);
f731ee
+  *entsize = grub_target_to_host (s->sh_entsize);
f731ee
+  return sym;
f731ee
+}
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
+static void
f731ee
+check_symbols (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e)
f731ee
+{
f731ee
+  Elf_Sym *sym;
f731ee
+  Elf_Word size, entsize;
f731ee
+  unsigned i;
f731ee
+
f731ee
+  sym = get_symtab (arch, e, &size, &entsize);
f731ee
 
f731ee
   for (i = 0;
f731ee
        i < size / entsize;
f731ee
@@ -208,19 +214,41 @@ check_symbols (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e)
f731ee
     }
f731ee
 }
f731ee
 
f731ee
-/* Relocate symbols.  */
f731ee
+static int
f731ee
+is_symbol_local(Elf_Sym *sym)
f731ee
+{
f731ee
+  switch (ELF_ST_TYPE (sym->st_info))
f731ee
+    {
f731ee
+    case STT_NOTYPE:
f731ee
+    case STT_OBJECT:
f731ee
+      if (sym->st_name != 0 && sym->st_shndx == 0)
f731ee
+	return 0;
f731ee
+      return 1;
f731ee
+
f731ee
+    case STT_FUNC:
f731ee
+    case STT_SECTION:
f731ee
+      return 1;
f731ee
+
f731ee
+    default:
f731ee
+      return 0;
f731ee
+    }
f731ee
+}
f731ee
+
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
+  Elf_Sym *symtab;
f731ee
+  Elf_Word symtabsize, symtabentsize;
f731ee
+
f731ee
+  symtab = get_symtab (arch, ehdr, &symtabsize, &symtabentsize);
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
@@ -235,8 +263,20 @@ section_check_relocations (const struct grub_module_verifier_arch *arch, void *e
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
+      if (arch->supported_relocations[i] != -1)
f731ee
+	continue;
f731ee
+      if (!arch->short_relocations)
f731ee
 	grub_util_error ("unsupported relocation 0x%x", type);
f731ee
+      for (i = 0; arch->short_relocations[i] != -1; i++)
f731ee
+	if (type == arch->short_relocations[i])
f731ee
+	  break;
f731ee
+      if (arch->short_relocations[i] == -1)
f731ee
+	grub_util_error ("unsupported relocation 0x%x", type);
f731ee
+      sym = (Elf_Sym *) ((char *) symtab + symtabentsize * ELF_R_SYM (grub_target_to_host (rel->r_info)));
f731ee
+
f731ee
+      if (is_symbol_local (sym))
f731ee
+	continue;
f731ee
+      grub_util_error ("relocation 0x%x is not module-local", type);
f731ee
     }
f731ee
 }
f731ee
 
f731ee
diff --git a/include/grub/module_verifier.h b/include/grub/module_verifier.h
f731ee
index 9e3a2ba720f..6cddff30f2e 100644
f731ee
--- a/include/grub/module_verifier.h
f731ee
+++ b/include/grub/module_verifier.h
f731ee
@@ -13,6 +13,7 @@ struct grub_module_verifier_arch {
f731ee
   int machine;
f731ee
   int flags;
f731ee
   const int *supported_relocations;
f731ee
+  const int *short_relocations;
f731ee
 };
f731ee
 
f731ee
 void grub_module_verify64(void *module_img, size_t module_size, const struct grub_module_verifier_arch *arch);