Skip to contentSkip to conversation

kern/dl: Add module version check

RFC PATCH Superseded Zhang Boyang 3 participants 5 messages Dec 21, 2022 – Jan 10, 2023

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/1 kern/dl: Add module version check

This patch add version information to GRUB modules. Specifically, PACKAGE_VERSION is embedded as null-terminated string in .modver section. This string is checked at module loading time.

If GRUB is locked down, modules with mismatched version will be rejected. This is necessary for implementing external signed modules securely (in future).

For compatibility reasons, if GRUB isn't locked down, modules can still be loaded even if they have mismatched version. A warning message will be emited in that situation.

It should be pointed out that the version string is only consisted of PACKAGE_VERSION. Any difference not reflected in PACKAGE_VERSION is not noticed by version check (e.g. configure options).

---
 grub-core/Makefile.am         |  2 +-
 grub-core/genmod.sh.in        | 28 +++++++++++++++++-----------
 grub-core/kern/dl.c           | 33 +++++++++++++++++++++++++++++++++
 util/grub-module-verifierXX.c | 10 ++++++++++
 4 files changed, 61 insertions(+), 12 deletions(-)
grub-core/Makefile.am +1 -1
index 80e7a83ed..d76aa80f4 100644
@@ -40,7 +40,7 @@ gentrigtables$(BUILD_EXEEXT): gentrigtables.c
 CLEANFILES += gentrigtables$(BUILD_EXEEXT)
 
 build-grub-module-verifier$(BUILD_EXEEXT): $(top_srcdir)/util/grub-module-verifier.c $(top_srcdir)/util/grub-module-verifier32.c $(top_srcdir)/util/grub-module-verifier64.c $(top_srcdir)/grub-core/kern/emu/misc.c $(top_srcdir)/util/misc.c
-	$(BUILD_CC) -o $@ -I$(top_srcdir)/include $(BUILD_CFLAGS) $(BUILD_CPPFLAGS) $(BUILD_LDFLAGS) -DGRUB_BUILD=1 -DGRUB_UTIL=1 -DGRUB_BUILD_PROGRAM_NAME=\"build-grub-module-verifier\" $^
+	$(BUILD_CC) -o $@ -I$(top_srcdir)/include $(BUILD_CFLAGS) $(BUILD_CPPFLAGS) $(BUILD_LDFLAGS) -DGRUB_BUILD=1 -DGRUB_UTIL=1 -DGRUB_BUILD_PROGRAM_NAME=\"build-grub-module-verifier\" -DPACKAGE_VERSION=\"$(PACKAGE_VERSION)\" $^
 CLEANFILES += build-grub-module-verifier$(BUILD_EXEEXT)
 
 # trigtables.c
grub-core/genmod.sh.in +17 -11
index e57c4d920..58a4cdcbe 100644
@@ -36,22 +36,25 @@ deps=`grep ^$modname: $moddep | sed s@^.*:@@`
 rm -f $tmpfile $outfile
 
 if test x@TARGET_APPLE_LINKER@ != x1; then
-    # stripout .modname and .moddeps sections from input module
-    @TARGET_OBJCOPY@ -R .modname -R .moddeps $infile $tmpfile
+    # stripout .modname and .moddeps and .modver sections from input module
+    @TARGET_OBJCOPY@ -R .modname -R .moddeps -R .modver $infile $tmpfile
 
-    # Attach .modname and .moddeps sections
+    # Attach .modname and .moddeps and .modver sections
     t1=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
     printf "$modname\0" >$t1
 
     t2=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
     for dep in $deps; do printf "$dep\0" >> $t2; done
 
+    t3=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
+    printf "@PACKAGE_VERSION@\0" >$t3
+
     if test -n "$deps"; then
-	@TARGET_OBJCOPY@ --add-section .modname=$t1 --add-section .moddeps=$t2 $tmpfile
+	@TARGET_OBJCOPY@ --add-section .modname=$t1 --add-section .moddeps=$t2 --add-section .modver=$t3 $tmpfile
     else
-	@TARGET_OBJCOPY@ --add-section .modname=$t1 $tmpfile
+	@TARGET_OBJCOPY@ --add-section .modname=$t1 --add-section .modver=$t3 $tmpfile
     fi
-    rm -f $t1 $t2
+    rm -f $t1 $t2 $t3
 
 	if test x@platform@ != xemu; then
 	    @TARGET_STRIP@ --strip-unneeded \
@@ -71,23 +74,26 @@ else
     tmpfile2=${outfile}.tmp2
     t1=${outfile}.t1.c
     t2=${outfile}.t2.c
+    t3=${outfile}.t3.c
 
     # remove old files if any
-    rm -f $t1 $t2
+    rm -f $t1 $t2 $t3
 
     cp $infile $tmpfile
 
-    # Attach .modname and .moddeps sections
+    # Attach .modname and .moddeps and .modver sections
     echo "char modname[]  __attribute__ ((section(\"_modname, _modname\"))) = \"$modname\";" >$t1
 
     for dep in $deps; do echo "char moddep_$dep[] __attribute__ ((section(\"_moddeps, _moddeps\"))) = \"$dep\";" >>$t2; done
 
+    echo "char modver[]  __attribute__ ((section(\"_modver, _modver\"))) = \"@PACKAGE_VERSION@\";" >$t3
+
     if test -n "$deps"; then
-	@TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 $t2 $tmpfile -Wl,-r
+	@TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 $t2 $t3 $tmpfile -Wl,-r
     else
-	@TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 $tmpfile -Wl,-r
+	@TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 $t3 $tmpfile -Wl,-r
     fi
-    rm -f $t1 $t2 $tmpfile
+    rm -f $t1 $t2 $t3 $tmpfile
     mv $tmpfile2 $tmpfile
 
 	cp $tmpfile $tmpfile.bin
grub-core/kern/dl.c +33 -0
index e447fd0fa..0292b162c 100644
@@ -32,6 +32,7 @@
 #include <grub/env.h>
 #include <grub/cache.h>
 #include <grub/i18n.h>
+#include <grub/lockdown.h>
 
 /* Platforms where modules are in a readonly area of memory.  */
 #if defined(GRUB_MACHINE_QEMU)
@@ -475,6 +476,37 @@ grub_dl_check_license (grub_dl_t mod, Elf_Ehdr *e)
 		     (char *) e + s->sh_offset);
 }
 
+static grub_err_t
+grub_dl_check_version (grub_dl_t mod, Elf_Ehdr *e)
+{
+  Elf_Shdr *s = grub_dl_find_section (e, ".modver");
+  const char *modver;
+  
+  modver = _("(unknown)");
+  if (s == NULL)
+    goto fail;
+
+  modver = (char *) e + s->sh_offset;
+  if (grub_strcmp (modver, PACKAGE_VERSION) != 0)
+    goto fail;
+
+  return GRUB_ERR_NONE;
+
+fail:
+  if (grub_is_lockdown () == GRUB_LOCKDOWN_ENABLED)
+    {
+      return grub_error (GRUB_ERR_BAD_MODULE,
+			 N_("refuse to load module %.63s of incorrect version: %.63s != %s"),
+			 mod->name, modver, PACKAGE_VERSION);
+    }
+  else
+    {
+      grub_printf_ (N_("warning: module %.63s has incorrect version: %.63s != %s\n"),
+		    mod->name, modver, PACKAGE_VERSION);
+      return GRUB_ERR_NONE;
+    }
+}
+
 static grub_err_t
 grub_dl_resolve_name (grub_dl_t mod, Elf_Ehdr *e)
 {
@@ -650,6 +682,7 @@ grub_dl_load_core_noinit (void *addr, grub_size_t size)
      Be sure to understand your license obligations.
   */
   if (grub_dl_resolve_name (mod, e)
+      || grub_dl_check_version (mod, e)
       || grub_dl_check_license (mod, e)
       || grub_dl_resolve_dependencies (mod, e)
       || grub_dl_load_segments (mod, e)
util/grub-module-verifierXX.c +10 -0
index d5907f268..fde111016 100644
@@ -491,8 +491,18 @@ SUFFIX(grub_module_verify) (const char * const filename,
   check_license (filename, arch, e);
 
   Elf_Shdr *s;
+  const char *modver;
   const char *modname;
 
+  s = find_section (arch, e, ".modver");
+  if (!s)
+    grub_util_error ("%s: no module version found", filename);
+
+  modver = (const char *) e + grub_target_to_host (s->sh_offset);
+
+  if (strcmp (modver, PACKAGE_VERSION) != 0)
+    grub_util_error ("%s: bad module version", filename);
+
   s = find_section (arch, e, ".modname");
   if (!s)
     grub_util_error ("%s: no module name found", filename);
Signature · 1 line
2.30.2
#1 lore raw

Hi again Zhang,

I hadn't had a chance to properly look at your v2 before replying earlier, and it looks like it addresses the elements I had an issue with.

Quoted 10 lines · Zhang Boyang

This patch add version information to GRUB modules. Specifically, PACKAGE_VERSION is embedded as null-terminated string in .modver section. This string is checked at module loading time.

If GRUB is locked down, modules with mismatched version will be rejected. This is necessary for implementing external signed modules securely (in future).

For compatibility reasons, if GRUB isn't locked down, modules can still be loaded even if they have mismatched version.

This alteration to your original patch looks like a good compromise to both our issues. Thanks for this!

I had a worry that lockdown was something that could be enabled for both BIOS and UEFI, but looking at the current lockdown.h, grub_is_lockdown() always returns GRUB_LOCKDOWN_DISABLED for BIOS, so this looks like a good solution to me.

Quoted 1 line

A warning message will be emited in that situation.

For BIOS boot, please don't.

If someone explicitly could not enable lockdown, they are unlikely to want lockdown-related warnings to be produced, especially for BIOS where lockdown cannot apply anyway.

So I think we want to add a '#ifdef GRUB_MACHINE_EFI' for that warning (see below). Is that something that you agree with?

If you submit a v3 with the changes I propose below, then I think I will have no objection with this proposal.

Quoted 124 lines

It should be pointed out that the version string is only consisted of PACKAGE_VERSION. Any difference not reflected in PACKAGE_VERSION is not noticed by version check (e.g. configure options).

---
  grub-core/Makefile.am         |  2 +-
  grub-core/genmod.sh.in        | 28 +++++++++++++++++-----------
  grub-core/kern/dl.c           | 33 +++++++++++++++++++++++++++++++++
  util/grub-module-verifierXX.c | 10 ++++++++++
  4 files changed, 61 insertions(+), 12 deletions(-)
grub-core/Makefile.am +1 -1
index 80e7a83ed..d76aa80f4 100644
@@ -40,7 +40,7 @@ gentrigtables$(BUILD_EXEEXT): gentrigtables.c
  CLEANFILES += gentrigtables$(BUILD_EXEEXT)
 
  build-grub-module-verifier$(BUILD_EXEEXT): $(top_srcdir)/util/grub-module-verifier.c $(top_srcdir)/util/grub-module-verifier32.c $(top_srcdir)/util/grub-module-verifier64.c $(top_srcdir)/grub-core/kern/emu/misc.c $(top_srcdir)/util/misc.c
-	$(BUILD_CC) -o $@ -I$(top_srcdir)/include $(BUILD_CFLAGS) $(BUILD_CPPFLAGS) $(BUILD_LDFLAGS) -DGRUB_BUILD=1 -DGRUB_UTIL=1 -DGRUB_BUILD_PROGRAM_NAME=\"build-grub-module-verifier\" $^
+	$(BUILD_CC) -o $@ -I$(top_srcdir)/include $(BUILD_CFLAGS) $(BUILD_CPPFLAGS) $(BUILD_LDFLAGS) -DGRUB_BUILD=1 -DGRUB_UTIL=1 -DGRUB_BUILD_PROGRAM_NAME=\"build-grub-module-verifier\" -DPACKAGE_VERSION=\"$(PACKAGE_VERSION)\" $^
  CLEANFILES += build-grub-module-verifier$(BUILD_EXEEXT)
 
  # trigtables.c
grub-core/genmod.sh.in +17 -11
index e57c4d920..58a4cdcbe 100644
@@ -36,22 +36,25 @@ deps=`grep ^$modname: $moddep | sed s@^.*:@@`
  rm -f $tmpfile $outfile
 
  if test x@TARGET_APPLE_LINKER@ != x1; then
-    # stripout .modname and .moddeps sections from input module
-    @TARGET_OBJCOPY@ -R .modname -R .moddeps $infile $tmpfile
+    # stripout .modname and .moddeps and .modver sections from input module
+    @TARGET_OBJCOPY@ -R .modname -R .moddeps -R .modver $infile $tmpfile
 
-    # Attach .modname and .moddeps sections
+    # Attach .modname and .moddeps and .modver sections
      t1=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
      printf "$modname\0" >$t1
 
      t2=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
      for dep in $deps; do printf "$dep\0" >> $t2; done
 
+    t3=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
+    printf "@PACKAGE_VERSION@\0" >$t3
+
      if test -n "$deps"; then
-	@TARGET_OBJCOPY@ --add-section .modname=$t1 --add-section .moddeps=$t2 $tmpfile
+	@TARGET_OBJCOPY@ --add-section .modname=$t1 --add-section .moddeps=$t2 --add-section .modver=$t3 $tmpfile
      else
-	@TARGET_OBJCOPY@ --add-section .modname=$t1 $tmpfile
+	@TARGET_OBJCOPY@ --add-section .modname=$t1 --add-section .modver=$t3 $tmpfile
      fi
-    rm -f $t1 $t2
+    rm -f $t1 $t2 $t3
 
  	if test x@platform@ != xemu; then
  	    @TARGET_STRIP@ --strip-unneeded \
@@ -71,23 +74,26 @@ else
      tmpfile2=${outfile}.tmp2
      t1=${outfile}.t1.c
      t2=${outfile}.t2.c
+    t3=${outfile}.t3.c
 
      # remove old files if any
-    rm -f $t1 $t2
+    rm -f $t1 $t2 $t3
 
      cp $infile $tmpfile
 
-    # Attach .modname and .moddeps sections
+    # Attach .modname and .moddeps and .modver sections
      echo "char modname[]  __attribute__ ((section(\"_modname, _modname\"))) = \"$modname\";" >$t1
 
      for dep in $deps; do echo "char moddep_$dep[] __attribute__ ((section(\"_moddeps, _moddeps\"))) = \"$dep\";" >>$t2; done
 
+    echo "char modver[]  __attribute__ ((section(\"_modver, _modver\"))) = \"@PACKAGE_VERSION@\";" >$t3
+
      if test -n "$deps"; then
-	@TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 $t2 $tmpfile -Wl,-r
+	@TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 $t2 $t3 $tmpfile -Wl,-r
      else
-	@TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 $tmpfile -Wl,-r
+	@TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 $t3 $tmpfile -Wl,-r
      fi
-    rm -f $t1 $t2 $tmpfile
+    rm -f $t1 $t2 $t3 $tmpfile
      mv $tmpfile2 $tmpfile
 
  	cp $tmpfile $tmpfile.bin
grub-core/kern/dl.c +14 -0
index e447fd0fa..0292b162c 100644
@@ -32,6 +32,7 @@
  #include <grub/env.h>
  #include <grub/cache.h>
  #include <grub/i18n.h>
+#include <grub/lockdown.h>
 
  /* Platforms where modules are in a readonly area of memory.  */
  #if defined(GRUB_MACHINE_QEMU)
@@ -475,6 +476,37 @@ grub_dl_check_license (grub_dl_t mod, Elf_Ehdr *e)
  		     (char *) e + s->sh_offset);
  }
 
+static grub_err_t
+grub_dl_check_version (grub_dl_t mod, Elf_Ehdr *e)
+{
+  Elf_Shdr *s = grub_dl_find_section (e, ".modver");
+  const char *modver;
+
+  modver = _("(unknown)");
+  if (s == NULL)
+    goto fail;
+
+  modver = (char *) e + s->sh_offset;
+  if (grub_strcmp (modver, PACKAGE_VERSION) != 0)
+    goto fail;

grub-core/kern/dl.c grub_dl_check_license (grub_dl_t mod, Elf_Ehdr *e) · line 501

Quoted 10 lines
grub-core/kern/dl.c +10 -0
+
+  return GRUB_ERR_NONE;
+
+fail:
+  if (grub_is_lockdown () == GRUB_LOCKDOWN_ENABLED)
+    {
+      return grub_error (GRUB_ERR_BAD_MODULE,
+			 N_("refuse to load module %.63s of incorrect version: %.63s != %s"),
+			 mod->name, modver, PACKAGE_VERSION);
+    }

#ifdef GRUB_MACHINE_EFI

grub-core/kern/dl.c grub_dl_check_license (grub_dl_t mod, Elf_Ehdr *e) · line 507

Quoted 6 lines
grub-core/kern/dl.c +6 -0
+  else
+    {
+      grub_printf_ (N_("warning: module %.63s has incorrect version: %.63s != %s\n"),
+		    mod->name, modver, PACKAGE_VERSION);
+      return GRUB_ERR_NONE;
+    }

#endif

Quoted 26 lines
grub-core/kern/dl.c +3 -0
+}
+
  static grub_err_t
  grub_dl_resolve_name (grub_dl_t mod, Elf_Ehdr *e)
  {
@@ -650,6 +682,7 @@ grub_dl_load_core_noinit (void *addr, grub_size_t size)
       Be sure to understand your license obligations.
    */
    if (grub_dl_resolve_name (mod, e)
+      || grub_dl_check_version (mod, e)
        || grub_dl_check_license (mod, e)
        || grub_dl_resolve_dependencies (mod, e)
        || grub_dl_load_segments (mod, e)
util/grub-module-verifierXX.c +3 -0
index d5907f268..fde111016 100644
@@ -491,8 +491,18 @@ SUFFIX(grub_module_verify) (const char * const filename,
    check_license (filename, arch, e);
 
    Elf_Shdr *s;
+  const char *modver;
    const char *modname;
 
+  s = find_section (arch, e, ".modver");
+  if (!s)

util/grub-module-verifierXX.c SUFFIX(grub_module_verify) (const char * const filename, · line 508

Quoted 10 lines
util/grub-module-verifierXX.c +7 -0
+    grub_util_error ("%s: no module version found", filename);
+
+  modver = (const char *) e + grub_target_to_host (s->sh_offset);
+
+  if (strcmp (modver, PACKAGE_VERSION) != 0)
+    grub_util_error ("%s: bad module version", filename);
+
    s = find_section (arch, e, ".modname");
    if (!s)
      grub_util_error ("%s: no module name found", filename);

Regards,

/Pete

#2 lore raw
In reply to Zhang Boyang
Quoted 152 lines · On Wed, 21 Dec 2022 20:11:57 +0800 Zhang Boyang <[email protected]>…

This patch add version information to GRUB modules. Specifically, PACKAGE_VERSION is embedded as null-terminated string in .modver section. This string is checked at module loading time.

If GRUB is locked down, modules with mismatched version will be rejected. This is necessary for implementing external signed modules securely (in future).

For compatibility reasons, if GRUB isn't locked down, modules can still be loaded even if they have mismatched version. A warning message will be emited in that situation.

It should be pointed out that the version string is only consisted of PACKAGE_VERSION. Any difference not reflected in PACKAGE_VERSION is not noticed by version check (e.g. configure options).

Link:
https://lists.gnu.org/archive/html/grub-devel/2022-12/msg00228.html
Link:
https://lists.gnu.org/archive/html/grub-devel/2022-12/msg00229.html

grub-core/Makefile.am | 2 +- grub-core/genmod.sh.in |
28 +++++++++++++++++----------- grub-core/kern/dl.c | 33
+++++++++++++++++++++++++++++++++ util/grub-module-verifierXX.c | 10 ++++++++++ 4 files changed, 61 insertions(+), 12 deletions(-)

grub-core/Makefile.am +0 -0
index 80e7a83ed..d76aa80f4 100644
@@ -40,7 +40,7 @@ gentrigtables$(BUILD_EXEEXT): gentrigtables.c
 CLEANFILES += gentrigtables$(BUILD_EXEEXT)
 
 build-grub-module-verifier$(BUILD_EXEEXT):

$(top_srcdir)/util/grub-module-verifier.c $(top_srcdir)/util/grub-module-verifier32.c $(top_srcdir)/util/grub-module-verifier64.c $(top_srcdir)/grub-core/kern/emu/misc.c $(top_srcdir)/util/misc.c

grub-core/Makefile.am +0 -1
-	$(BUILD_CC) -o $@ -I$(top_srcdir)/include $(BUILD_CFLAGS)

$(BUILD_CPPFLAGS) $(BUILD_LDFLAGS) -DGRUB_BUILD=1 -DGRUB_UTIL=1

grub-core/Makefile.am +1 -1
-DGRUB_BUILD_PROGRAM_NAME=\"build-grub-module-verifier\" $^
+	$(BUILD_CC) -o $@ -I$(top_srcdir)/include $(BUILD_CFLAGS)

$(BUILD_CPPFLAGS) $(BUILD_LDFLAGS) -DGRUB_BUILD=1 -DGRUB_UTIL=1

grub-core/Makefile.am +0 -2
-DGRUB_BUILD_PROGRAM_NAME=\"build-grub-module-verifier\"
-DPACKAGE_VERSION=\"$(PACKAGE_VERSION)\" $^ CLEANFILES +=

build-grub-module-verifier$(BUILD_EXEEXT) # trigtables.c diff --git a/grub-core/genmod.sh.in b/grub-core/genmod.sh.in index e57c4d920..58a4cdcbe 100644 --- a/grub-core/genmod.sh.in +++ b/grub-core/genmod.sh.in

grub-core/Makefile.am +1 -2
@@ -36,22 +36,25 @@ deps=`grep ^$modname: $moddep | sed s@^.*:@@`
 rm -f $tmpfile $outfile
 
 if test x@TARGET_APPLE_LINKER@ != x1; then
-    # stripout .modname and .moddeps sections from input module
-    @TARGET_OBJCOPY@ -R .modname -R .moddeps $infile $tmpfile
+    # stripout .modname and .moddeps and .modver sections from input

module
+ @TARGET_OBJCOPY@ -R .modname -R .moddeps -R .modver $infile
$tmpfile

grub-core/Makefile.am +4 -2
-    # Attach .modname and .moddeps sections
+    # Attach .modname and .moddeps and .modver sections
     t1=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
     printf "$modname\0" >$t1
 
     t2=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
     for dep in $deps; do printf "$dep\0" >> $t2; done
 
+    t3=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
+    printf "@PACKAGE_VERSION@\0" >$t3
+
     if test -n "$deps"; then
-	@TARGET_OBJCOPY@ --add-section .modname=$t1 --add-section

.moddeps=$t2 $tmpfile

grub-core/Makefile.am +1 -0
+	@TARGET_OBJCOPY@ --add-section .modname=$t1 --add-section

.moddeps=$t2 --add-section .modver=$t3 $tmpfile else

grub-core/Makefile.am +1 -1
-	@TARGET_OBJCOPY@ --add-section .modname=$t1 $tmpfile
+	@TARGET_OBJCOPY@ --add-section .modname=$t1 --add-section

.modver=$t3 $tmpfile fi

grub-core/Makefile.am +4 -3
-    rm -f $t1 $t2
+    rm -f $t1 $t2 $t3
 
 	if test x@platform@ != xemu; then
 	    @TARGET_STRIP@ --strip-unneeded \
@@ -71,23 +74,26 @@ else
     tmpfile2=${outfile}.tmp2
     t1=${outfile}.t1.c
     t2=${outfile}.t2.c
+    t3=${outfile}.t3.c
 
     # remove old files if any
-    rm -f $t1 $t2
+    rm -f $t1 $t2 $t3
 
     cp $infile $tmpfile
 
-    # Attach .modname and .moddeps sections
+    # Attach .modname and .moddeps and .modver sections
     echo "char modname[]  __attribute__ ((section(\"_modname,

_modname\"))) = \"$modname\";" >$t1

     for dep in $deps; do echo "char moddep_$dep[] __attribute__

((section(\"_moddeps, _moddeps\"))) = \"$dep\";" >>$t2; done
+ echo "char modver[] __attribute__ ((section(\"_modver,
_modver\"))) = \"@PACKAGE_VERSION@\";" >$t3 +
if test -n "$deps"; then

grub-core/Makefile.am +0 -1
-	@TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o

$tmpfile2 $t1 $t2 $tmpfile -Wl,-r

grub-core/Makefile.am +1 -0
+	@TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o

$tmpfile2 $t1 $t2 $t3 $tmpfile -Wl,-r else

grub-core/Makefile.am +0 -1
-	@TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o

$tmpfile2 $t1 $tmpfile -Wl,-r

grub-core/Makefile.am +1 -0
+	@TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o

$tmpfile2 $t1 $t3 $tmpfile -Wl,-r fi

grub-core/Makefile.am +1 -1
-    rm -f $t1 $t2 $tmpfile
+    rm -f $t1 $t2 $t3 $tmpfile
     mv $tmpfile2 $tmpfile
 
 	cp $tmpfile $tmpfile.bin
grub-core/kern/dl.c +20 -0
index e447fd0fa..0292b162c 100644
@@ -32,6 +32,7 @@
 #include <grub/env.h>
 #include <grub/cache.h>
 #include <grub/i18n.h>
+#include <grub/lockdown.h>
 
 /* Platforms where modules are in a readonly area of memory.  */
 #if defined(GRUB_MACHINE_QEMU)
@@ -475,6 +476,37 @@ grub_dl_check_license (grub_dl_t mod, Elf_Ehdr
 *e) (char *) e + s->sh_offset);
 }
 
+static grub_err_t
+grub_dl_check_version (grub_dl_t mod, Elf_Ehdr *e)
+{
+  Elf_Shdr *s = grub_dl_find_section (e, ".modver");
+  const char *modver;
+  
+  modver = _("(unknown)");
+  if (s == NULL)
+    goto fail;
+
+  modver = (char *) e + s->sh_offset;
+  if (grub_strcmp (modver, PACKAGE_VERSION) != 0)
+    goto fail;
+
+  return GRUB_ERR_NONE;
+
+fail:
+  if (grub_is_lockdown () == GRUB_LOCKDOWN_ENABLED)
+    {

grub-core/kern/dl.c grub_dl_check_license (grub_dl_t mod, Elf_Ehdr · line 507

Quoted 10 lines
grub-core/kern/dl.c +8 -0
+      return grub_error (GRUB_ERR_BAD_MODULE,
+			 N_("refuse to load module %.63s of
 incorrect version: %.63s != %s"),
+			 mod->name, modver, PACKAGE_VERSION);
+    }
+  else
+    {
+      grub_printf_ (N_("warning: module %.63s has incorrect version:
 %.63s != %s\n"),
+		    mod->name, modver, PACKAGE_VERSION);

I don't quite like this, but I could live with it. I mostly don't like it because I don't want to get spammed with a lot of these warning messages. But maybe this is the best of the bad set of options.

One thing to note is I believe that this will take GRUB out of graphical mode and put it into text mode. This doesn't affect me (right now) though.

Another thing is that, for the use case of loading a grub.cfg from a different GRUB installation, this message from version mismatched modules loaded in the sourcing of the script will be displayed for a fraction of a second until the menu from the sourced grub.cfg is displayed. So it may not be helpful for the user.

This isn't likely an issue for loopback.cfg because usually the module loading is done implicitly and all the commands are generally inside the menu entries. In this case, printing to the screen seems like a good idea because directly after the module loading the commands will run, which might crash GRUB. So if the crash hangs the machine the user will see this message and have a clue.

Glenn

Quoted 38 lines
grub-core/kern/dl.c +4 -0
+      return GRUB_ERR_NONE;
+    }
+}
+
 static grub_err_t

grub_dl_resolve_name (grub_dl_t mod, Elf_Ehdr *e) {

grub-core/kern/dl.c +0 -0
@@ -650,6 +682,7 @@ grub_dl_load_core_noinit (void *addr, grub_size_t

size) Be sure to understand your license obligations.
*/
if (grub_dl_resolve_name (mod, e)

grub-core/kern/dl.c +1 -0
+      || grub_dl_check_version (mod, e)
       || grub_dl_check_license (mod, e)
       || grub_dl_resolve_dependencies (mod, e)
       || grub_dl_load_segments (mod, e)
(quoted diff) +0 -0

b/util/grub-module-verifierXX.c index d5907f268..fde111016 100644

util/grub-module-verifierXX.c +10 -0
@@ -491,8 +491,18 @@ SUFFIX(grub_module_verify) (const char * const
 filename, check_license (filename, arch, e);
 
   Elf_Shdr *s;
+  const char *modver;
   const char *modname;
 
+  s = find_section (arch, e, ".modver");
+  if (!s)
+    grub_util_error ("%s: no module version found", filename);
+
+  modver = (const char *) e + grub_target_to_host (s->sh_offset);
+
+  if (strcmp (modver, PACKAGE_VERSION) != 0)
+    grub_util_error ("%s: bad module version", filename);
+
   s = find_section (arch, e, ".modname");
   if (!s)
     grub_util_error ("%s: no module name found", filename);
#3 lore raw

Hi,

Quoted 10 lines · Glenn Washburn

Review

Quoted 3 lines · On Wed, 21 Dec 2022 20:11:57 +0800 Zhang Boyang <[email protected]>…

+ grub_printf_ (N_("warning: module %.63s has incorrect version:
%.63s != %s\n"),

(quoted diff) +1 -0
+		    mod->name, modver, PACKAGE_VERSION);

I don't quite like this, but I could live with it. I mostly don't like it because I don't want to get spammed with a lot of these warning messages. But maybe this is the best of the bad set of options.

I sent a V3 patch at https://lists.gnu.org/archive/html/grub-devel/2022-12/msg00280.html , in which can disable this warning at build time, however I personally don't like it.

I think preloading modules is a good solution for you. e.g. run insmod foobar before switching $root/$prefix. This also eliminate the possibility of GRUB crashing because of mismatched modules. For a comprehensive list of modules, please see $GRUB_MODULES in https://salsa.debian.org/grub-team/grub/-/blob/master/debian/build-efi-images

If you think this solution is good to you, I want to drop V3 and submit a improved V2 as V4, because it's cleaner.

Quoted 4 lines

One thing to note is I believe that this will take GRUB out of graphical mode and put it into text mode. This doesn't affect me (right now) though.

I tested on i386-pc and x86_64-efi and there is no such behavior.

Quoted 6 lines

Another thing is that, for the use case of loading a grub.cfg from a different GRUB installation, this message from version mismatched modules loaded in the sourcing of the script will be displayed for a fraction of a second until the menu from the sourced grub.cfg is displayed. So it may not be helpful for the user.

What about adding a 100ms sleep when the warning is displayed? By the way, this can also be workaround by preloading modules.

Quoted 7 lines

This isn't likely an issue for loopback.cfg because usually the module loading is done implicitly and all the commands are generally inside the menu entries. In this case, printing to the screen seems like a good idea because directly after the module loading the commands will run, which might crash GRUB. So if the crash hangs the machine the user will see this message and have a clue.

Best Regards,
Zhang Boyang

Quoted 2 lines

Glenn

#4 lore raw
Quoted 1 line · On Fri, 23 Dec 2022 16:18:43 +0800 Zhang Boyang <[email protected]>…

Hi,

Hi, sorry I missed this til now.

Quoted 18 lines · Glenn Washburn
Quoted 11 lines

Review

Quoted 3 lines · On Wed, 21 Dec 2022 20:11:57 +0800 Zhang Boyang <[email protected]>…

+ grub_printf_ (N_("warning: module %.63s has incorrect
version: %.63s != %s\n"),

(quoted diff) +1 -0
+		    mod->name, modver, PACKAGE_VERSION);

I don't quite like this, but I could live with it. I mostly don't like it because I don't want to get spammed with a lot of these warning messages. But maybe this is the best of the bad set of options.

I sent a V3 patch at
https://lists.gnu.org/archive/html/grub-devel/2022-12/msg00280.html , in which can disable this warning at build time, however I personally don't like it.

Quoted 8 lines

I think preloading modules is a good solution for you. e.g. run insmod foobar before switching $root/$prefix. This also eliminate the possibility of GRUB crashing because of mismatched modules. For a comprehensive list of modules, please see $GRUB_MODULES in
https://salsa.debian.org/grub-team/grub/-/blob/master/debian/build-efi-images

If you think this solution is good to you, I want to drop V3 and submit a improved V2 as V4, because it's cleaner.

I don't like that because it requires a priori knowledge of what modules are being loaded in the sourced config file. However, I do already preload some modules. Perhaps out of scope for this patch, but I've been thinking about have a variable modules_prefix, which is used only to load modules, or some way of decoupling the module load path from $root. Then I can set that and not worry about how $root is changed in the script.

Quoted 6 lines
Quoted 4 lines

One thing to note is I believe that this will take GRUB out of graphical mode and put it into text mode. This doesn't affect me (right now) though.

I tested on i386-pc and x86_64-efi and there is no such behavior.

Your test was with loading a mismatched module, where the warning is triggered?

Quoted 9 lines
Quoted 6 lines

Another thing is that, for the use case of loading a grub.cfg from a different GRUB installation, this message from version mismatched modules loaded in the sourcing of the script will be displayed for a fraction of a second until the menu from the sourced grub.cfg is displayed. So it may not be helpful for the user.

What about adding a 100ms sleep when the warning is displayed? By the way, this can also be workaround by preloading modules.

Is that long enough to make it readable? It would be nice if GRUB had a log buffer for the debug log messages. Then it could be put in the debug log and the user could see that there was a version mismatch.

Anyway, since its just a warning message an doesn't prevent the module from loading (when not in lockdown), its not really worse than the current state. Except for getting spammed, which isn't a huge deal. So I can live with this implementation.

Glenn

Quoted 15 lines
Quoted 9 lines

This isn't likely an issue for loopback.cfg because usually the module loading is done implicitly and all the commands are generally inside the menu entries. In this case, printing to the screen seems like a good idea because directly after the module loading the commands will run, which might crash GRUB. So if the crash hangs the machine the user will see this message and have a clue.

Best Regards,
Zhang Boyang

Quoted 2 lines

Glenn