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