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