nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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