Blame SOURCES/0221-grub-module-verifier-report-the-filename-or-modname-.patch

d9d99f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d9d99f
From: Peter Jones <pjones@redhat.com>
d9d99f
Date: Wed, 1 Aug 2018 10:12:47 -0400
d9d99f
Subject: [PATCH] grub-module-verifier: report the filename or modname in
d9d99f
 errors.
d9d99f
d9d99f
Make it so that when grub-module-verifier complains of an issue, it tells you
d9d99f
which module the issue was with.
d9d99f
d9d99f
Signed-off-by: Peter Jones <pjones@redhat.com>
d9d99f
---
d9d99f
 util/grub-module-verifier.c    |  6 ++---
d9d99f
 util/grub-module-verifierXX.c  | 58 ++++++++++++++++++++++--------------------
d9d99f
 include/grub/module_verifier.h |  4 +--
d9d99f
 3 files changed, 36 insertions(+), 32 deletions(-)
d9d99f
d9d99f
diff --git a/util/grub-module-verifier.c b/util/grub-module-verifier.c
d9d99f
index a79271f6631..03ba1ab437a 100644
d9d99f
--- a/util/grub-module-verifier.c
d9d99f
+++ b/util/grub-module-verifier.c
d9d99f
@@ -157,7 +157,7 @@ main (int argc, char **argv)
d9d99f
     if (strcmp(archs[arch].name, argv[2]) == 0)
d9d99f
       break;
d9d99f
   if (arch == ARRAY_SIZE(archs))
d9d99f
-    grub_util_error("unknown arch: %s", argv[2]);
d9d99f
+    grub_util_error("%s: unknown arch: %s", argv[1], argv[2]);
d9d99f
 
d9d99f
   for (whitelist = 0; whitelist < ARRAY_SIZE(whitelists); whitelist++)
d9d99f
     if (strcmp(whitelists[whitelist].arch, argv[2]) == 0
d9d99f
@@ -169,8 +169,8 @@ main (int argc, char **argv)
d9d99f
   module_size = grub_util_get_image_size (argv[1]);
d9d99f
   module_img = grub_util_read_image (argv[1]);
d9d99f
   if (archs[arch].voidp_sizeof == 8)
d9d99f
-    grub_module_verify64(module_img, module_size, &archs[arch], whitelist_empty);
d9d99f
+    grub_module_verify64(argv[1], module_img, module_size, &archs[arch], whitelist_empty);
d9d99f
   else
d9d99f
-    grub_module_verify32(module_img, module_size, &archs[arch], whitelist_empty);
d9d99f
+    grub_module_verify32(argv[1], module_img, module_size, &archs[arch], whitelist_empty);
d9d99f
   return 0;
d9d99f
 }
d9d99f
diff --git a/util/grub-module-verifierXX.c b/util/grub-module-verifierXX.c
d9d99f
index 597ded14362..a98e2f9b1ac 100644
d9d99f
--- a/util/grub-module-verifierXX.c
d9d99f
+++ b/util/grub-module-verifierXX.c
d9d99f
@@ -160,14 +160,15 @@ find_section (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, const c
d9d99f
 }
d9d99f
 
d9d99f
 static void
d9d99f
-check_license (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e)
d9d99f
+check_license (const char * const filename,
d9d99f
+	       const struct grub_module_verifier_arch *arch, Elf_Ehdr *e)
d9d99f
 {
d9d99f
   Elf_Shdr *s = find_section (arch, e, ".module_license");
d9d99f
   if (s && (strcmp ((char *) e + grub_target_to_host(s->sh_offset), "LICENSE=GPLv3") == 0
d9d99f
 	    || strcmp ((char *) e + grub_target_to_host(s->sh_offset), "LICENSE=GPLv3+") == 0
d9d99f
 	    || strcmp ((char *) e + grub_target_to_host(s->sh_offset), "LICENSE=GPLv2+") == 0))
d9d99f
     return;
d9d99f
-  grub_util_error ("incompatible license");
d9d99f
+  grub_util_error ("%s: incompatible license", filename);
d9d99f
 }
d9d99f
 
d9d99f
 static Elf_Sym *
d9d99f
@@ -233,10 +234,10 @@ check_symbols (const struct grub_module_verifier_arch *arch,
d9d99f
       s = find_section (arch, e, ".moddeps");
d9d99f
 
d9d99f
       if (!s)
d9d99f
-	grub_util_error ("no symbol table and no .moddeps section");
d9d99f
+	grub_util_error ("%s: no symbol table and no .moddeps section", modname);
d9d99f
 
d9d99f
       if (!s->sh_size)
d9d99f
-	grub_util_error ("no symbol table and empty .moddeps section");
d9d99f
+	grub_util_error ("%s: no symbol table and empty .moddeps section", modname);
d9d99f
 
d9d99f
       return;
d9d99f
     }
d9d99f
@@ -257,7 +258,7 @@ check_symbols (const struct grub_module_verifier_arch *arch,
d9d99f
 	  break;
d9d99f
 
d9d99f
 	default:
d9d99f
-	  return grub_util_error ("unknown symbol type `%d'", (int) type);
d9d99f
+	  return grub_util_error ("%s: unknown symbol type `%d'", modname, (int) type);
d9d99f
 	}
d9d99f
     }
d9d99f
 }
d9d99f
@@ -283,7 +284,8 @@ is_symbol_local(Elf_Sym *sym)
d9d99f
 }
d9d99f
 
d9d99f
 static void
d9d99f
-section_check_relocations (const struct grub_module_verifier_arch *arch, void *ehdr,
d9d99f
+section_check_relocations (const char * const modname,
d9d99f
+			   const struct grub_module_verifier_arch *arch, void *ehdr,
d9d99f
 			   Elf_Shdr *s, size_t target_seg_size)
d9d99f
 {
d9d99f
   Elf_Rel *rel, *max;
d9d99f
@@ -292,7 +294,7 @@ section_check_relocations (const struct grub_module_verifier_arch *arch, void *e
d9d99f
 
d9d99f
   symtab = get_symtab (arch, ehdr, &symtabsize, &symtabentsize);
d9d99f
   if (!symtab)
d9d99f
-    grub_util_error ("relocation without symbol table");
d9d99f
+    grub_util_error ("%s: relocation without symbol table", modname);
d9d99f
 
d9d99f
   for (rel = (Elf_Rel *) ((char *) ehdr + grub_target_to_host (s->sh_offset)),
d9d99f
 	 max = (Elf_Rel *) ((char *) rel + grub_target_to_host (s->sh_size));
d9d99f
@@ -303,7 +305,7 @@ section_check_relocations (const struct grub_module_verifier_arch *arch, void *e
d9d99f
       unsigned i;
d9d99f
 
d9d99f
       if (target_seg_size < grub_target_to_host (rel->r_offset))
d9d99f
-	grub_util_error ("reloc offset is out of the segment");
d9d99f
+	grub_util_error ("%s: reloc offset is out of the segment", modname);
d9d99f
 
d9d99f
       grub_uint32_t type = ELF_R_TYPE (grub_target_to_host (rel->r_info));
d9d99f
 
d9d99f
@@ -316,17 +318,17 @@ section_check_relocations (const struct grub_module_verifier_arch *arch, void *e
d9d99f
       if (arch->supported_relocations[i] != -1)
d9d99f
 	continue;
d9d99f
       if (!arch->short_relocations)
d9d99f
-	grub_util_error ("unsupported relocation 0x%x", type);
d9d99f
+	grub_util_error ("%s: unsupported relocation 0x%x", modname, type);
d9d99f
       for (i = 0; arch->short_relocations[i] != -1; i++)
d9d99f
 	if (type == arch->short_relocations[i])
d9d99f
 	  break;
d9d99f
       if (arch->short_relocations[i] == -1)
d9d99f
-	grub_util_error ("unsupported relocation 0x%x", type);
d9d99f
+	grub_util_error ("%s: unsupported relocation 0x%x", modname, type);
d9d99f
       sym = (Elf_Sym *) ((char *) symtab + symtabentsize * ELF_R_SYM (grub_target_to_host (rel->r_info)));
d9d99f
 
d9d99f
       if (is_symbol_local (sym))
d9d99f
 	continue;
d9d99f
-      grub_util_error ("relocation 0x%x is not module-local", type);
d9d99f
+      grub_util_error ("%s: relocation 0x%x is not module-local", modname, type);
d9d99f
     }
d9d99f
 #if defined(MODULEVERIFIER_ELF64)
d9d99f
   if (arch->machine == EM_AARCH64)
d9d99f
@@ -351,11 +353,11 @@ section_check_relocations (const struct grub_module_verifier_arch *arch, void *e
d9d99f
 		    && ELF_R_TYPE (rel2->r_info) == R_AARCH64_LD64_GOT_LO12_NC)
d9d99f
 		  break;
d9d99f
 	      if (rel2 >= (Elf_Rela *) max)
d9d99f
-		grub_util_error ("ADR_GOT_PAGE without matching LD64_GOT_LO12_NC");
d9d99f
+		grub_util_error ("%s: ADR_GOT_PAGE without matching LD64_GOT_LO12_NC", modname);
d9d99f
 	      break;
d9d99f
 	    case R_AARCH64_LD64_GOT_LO12_NC:
d9d99f
 	      if (unmatched_adr_got_page == 0)
d9d99f
-		grub_util_error ("LD64_GOT_LO12_NC without matching ADR_GOT_PAGE");
d9d99f
+		grub_util_error ("%s: LD64_GOT_LO12_NC without matching ADR_GOT_PAGE", modname);
d9d99f
 	      unmatched_adr_got_page--;
d9d99f
 	      break;
d9d99f
 	    }
d9d99f
@@ -365,7 +367,8 @@ section_check_relocations (const struct grub_module_verifier_arch *arch, void *e
d9d99f
 }
d9d99f
 
d9d99f
 static void
d9d99f
-check_relocations (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e)
d9d99f
+check_relocations (const char * const modname,
d9d99f
+		   const struct grub_module_verifier_arch *arch, Elf_Ehdr *e)
d9d99f
 {
d9d99f
   Elf_Shdr *s;
d9d99f
   unsigned i;
d9d99f
@@ -378,21 +381,22 @@ check_relocations (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e)
d9d99f
 	Elf_Shdr *ts;
d9d99f
 
d9d99f
 	if (grub_target_to_host32 (s->sh_type) == SHT_REL && !(arch->flags & GRUB_MODULE_VERIFY_SUPPORTS_REL))
d9d99f
-	  grub_util_error ("unsupported SHT_REL");
d9d99f
+	  grub_util_error ("%s: unsupported SHT_REL", modname);
d9d99f
 	if (grub_target_to_host32 (s->sh_type) == SHT_RELA && !(arch->flags & GRUB_MODULE_VERIFY_SUPPORTS_RELA))
d9d99f
-	  grub_util_error ("unsupported SHT_RELA");
d9d99f
+	  grub_util_error ("%s: unsupported SHT_RELA", modname);
d9d99f
 
d9d99f
 	/* Find the target segment.  */
d9d99f
 	if (grub_target_to_host32 (s->sh_info) >= grub_target_to_host16 (e->e_shnum))
d9d99f
-	  grub_util_error ("orphaned reloc section");
d9d99f
+	  grub_util_error ("%s: orphaned reloc section", modname);
d9d99f
 	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));
d9d99f
 
d9d99f
-	section_check_relocations (arch, e, s, grub_target_to_host (ts->sh_size));
d9d99f
+	section_check_relocations (modname, arch, e, s, grub_target_to_host (ts->sh_size));
d9d99f
       }
d9d99f
 }
d9d99f
 
d9d99f
 void
d9d99f
-SUFFIX(grub_module_verify) (void *module_img, size_t size,
d9d99f
+SUFFIX(grub_module_verify) (const char * const filename,
d9d99f
+			    void *module_img, size_t size,
d9d99f
 			    const struct grub_module_verifier_arch *arch,
d9d99f
 			    const char **whitelist_empty)
d9d99f
 {
d9d99f
@@ -400,7 +404,7 @@ SUFFIX(grub_module_verify) (void *module_img, size_t size,
d9d99f
 
d9d99f
   /* Check the header size.  */
d9d99f
   if (size < sizeof (Elf_Ehdr))
d9d99f
-    grub_util_error ("ELF header smaller than expected");
d9d99f
+    grub_util_error ("%s: ELF header smaller than expected", filename);
d9d99f
 
d9d99f
   /* Check the magic numbers.  */
d9d99f
   if (e->e_ident[EI_MAG0] != ELFMAG0
d9d99f
@@ -409,36 +413,36 @@ SUFFIX(grub_module_verify) (void *module_img, size_t size,
d9d99f
       || e->e_ident[EI_MAG3] != ELFMAG3
d9d99f
       || e->e_ident[EI_VERSION] != EV_CURRENT
d9d99f
       || grub_target_to_host32 (e->e_version) != EV_CURRENT)
d9d99f
-    grub_util_error ("invalid arch-independent ELF magic");
d9d99f
+    grub_util_error ("%s: invalid arch-independent ELF magic", filename);
d9d99f
 
d9d99f
   if (e->e_ident[EI_CLASS] != ELFCLASSXX
d9d99f
       || e->e_ident[EI_DATA] != (arch->bigendian ? ELFDATA2MSB : ELFDATA2LSB)
d9d99f
       || grub_target_to_host16 (e->e_machine) != arch->machine)
d9d99f
-    grub_util_error ("invalid arch-dependent ELF magic");
d9d99f
+    grub_util_error ("%s: invalid arch-dependent ELF magic", filename);
d9d99f
 
d9d99f
   if (grub_target_to_host16 (e->e_type) != ET_REL)
d9d99f
     {
d9d99f
-      grub_util_error ("this ELF file is not of the right type");
d9d99f
+      grub_util_error ("%s: this ELF file is not of the right type", filename);
d9d99f
     }
d9d99f
 
d9d99f
   /* Make sure that every section is within the core.  */
d9d99f
   if (size < grub_target_to_host (e->e_shoff)
d9d99f
       + (grub_uint32_t) grub_target_to_host16 (e->e_shentsize) * grub_target_to_host16(e->e_shnum))
d9d99f
     {
d9d99f
-      grub_util_error ("ELF sections outside core");
d9d99f
+      grub_util_error ("%s: ELF sections outside core", filename);
d9d99f
     }
d9d99f
 
d9d99f
-  check_license (arch, e);
d9d99f
+  check_license (filename, arch, e);
d9d99f
 
d9d99f
   Elf_Shdr *s;
d9d99f
   const char *modname;
d9d99f
 
d9d99f
   s = find_section (arch, e, ".modname");
d9d99f
   if (!s)
d9d99f
-    grub_util_error ("no module name found");
d9d99f
+    grub_util_error ("%s: no module name found", filename);
d9d99f
 
d9d99f
   modname = (const char *) e + grub_target_to_host (s->sh_offset);
d9d99f
 
d9d99f
   check_symbols(arch, e, modname, whitelist_empty);
d9d99f
-  check_relocations(arch, e);
d9d99f
+  check_relocations(modname, arch, e);
d9d99f
 }
d9d99f
diff --git a/include/grub/module_verifier.h b/include/grub/module_verifier.h
d9d99f
index f4870cb9c2d..ba21c75e2eb 100644
d9d99f
--- a/include/grub/module_verifier.h
d9d99f
+++ b/include/grub/module_verifier.h
d9d99f
@@ -16,5 +16,5 @@ struct grub_module_verifier_arch {
d9d99f
   const int *short_relocations;
d9d99f
 };
d9d99f
 
d9d99f
-void grub_module_verify64(void *module_img, size_t module_size, const struct grub_module_verifier_arch *arch, const char **whitelist_empty);
d9d99f
-void grub_module_verify32(void *module_img, size_t module_size, const struct grub_module_verifier_arch *arch, const char **whitelist_empty);
d9d99f
+void grub_module_verify64(const char * const filename, void *module_img, size_t module_size, const struct grub_module_verifier_arch *arch, const char **whitelist_empty);
d9d99f
+void grub_module_verify32(const char * const filename, void *module_img, size_t module_size, const struct grub_module_verifier_arch *arch, const char **whitelist_empty);