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

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