Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1p7I9L-00053I-JC for mharc-grub-devel@gnu.org; Mon, 19 Dec 2022 10:33:55 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p7I9J-000530-HF for grub-devel@gnu.org; Mon, 19 Dec 2022 10:33:53 -0500 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p7I9H-0005SH-Ji for grub-devel@gnu.org; Mon, 19 Dec 2022 10:33:53 -0500 Received: by mail-pl1-x634.google.com with SMTP id t2so9396245ply.2 for ; Mon, 19 Dec 2022 07:33:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=SU120659edU+ktpkXaBNwRhAi/mYDDJAMRcXZTeJ6TU=; b=BNMRyK1ZRXHmkQi4AVmHWpvUetf9Lva2bzJ6ncxzWta9ZNzdwuU/AHKZvVCt63tCGD /VuXfDPDp8iuzPsvQb9l1KX+2UjfC8Rk8krggKyRPQyylS2VvPUm0fCk1Gd6TRSHCwYe 33szF38Akv+pSGRKei5D/MtwDOWVeWsWxOqWkSXZ8wZ8RzHz6um3lSsMY3L9UI2lDptN 4gpzxifSFFG88xoJ4mxDbqEO1K832DybCoEFu7fee7xvxyphJrix7XJod7Afvsu0k9Pz vGFTXJLNB8G1K5ZZMRsz60+vkQr9ECnroVGyhE1Gmy2n+/LVZcIlrj577WgUwdWSDwJd i+Bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=SU120659edU+ktpkXaBNwRhAi/mYDDJAMRcXZTeJ6TU=; b=TTV+arvtrlMTw3Csuz/QgTJTenbppjbCVpvs7BYTqB+TKem01St/B+BaL6EQhc/Ac2 PUgmHlXQPF9WiFhnofXLRcB1YLST9OoxIOeccg9y8rE2MrkKtZduODTlXbBFz5odyiMv GqbpwAmRFXSpni1ujwoSX8FD865eNiP1QNrrimEpkPwAJAmLbzLIELr8MJinnZMXQFMa I+UoR20+e2zNIA+KpGWKqJeIOg1RENZA6bdB9OhCGX3vUI2zxaGvHxyoEsZxq5enzkNt viRSu+zCJAPK5hXeTp3XWO04CbOxesIhdguCJ3F16F+zsyNe8hu7d6sIa6iIOtCB2T78 dxJA== X-Gm-Message-State: ANoB5plqNzNsMOKcKZU2836NHFSB1F1TDLez2BxFidsq4tCoAdnAtEEz 5JAzLBfhSCGd5ND02d+Lhgks7Yn1ZHA= X-Google-Smtp-Source: AA0mqf75g6kcywTK9XJ13R6gbkRE0sNfDvd3EDffDOc2hRycpML5OUOrE9430dmpfBRQkqVdo78gSA== X-Received: by 2002:a17:90a:1b0b:b0:219:396c:9e32 with SMTP id q11-20020a17090a1b0b00b00219396c9e32mr45357060pjq.16.1671464029746; Mon, 19 Dec 2022 07:33:49 -0800 (PST) Received: from localhost.localdomain (ec2-13-113-80-70.ap-northeast-1.compute.amazonaws.com. [13.113.80.70]) by smtp.gmail.com with ESMTPSA id nh6-20020a17090b364600b00212c27abcaesm9587009pjb.17.2022.12.19.07.33.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Dec 2022 07:33:49 -0800 (PST) From: Zhang Boyang To: grub-devel@gnu.org Cc: Zhang Boyang Subject: [RFC PATCH] kern/dl: Add module version check Date: Mon, 19 Dec 2022 23:33:29 +0800 Message-Id: <20221219153329.154238-1-zhangboyang.id@gmail.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=zhangboyang.id@gmail.com; helo=mail-pl1-x634.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 19 Dec 2022 15:33:53 -0000 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. That module will be rejected if mismatch is found. This will prevent loading modules from different versions of GRUB. 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). Signed-off-by: Zhang Boyang --- grub-core/Makefile.am | 2 +- grub-core/genmod.sh.in | 28 +++++++++++++++++----------- grub-core/kern/dl.c | 18 ++++++++++++++++++ util/grub-module-verifierXX.c | 10 ++++++++++ 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am index 80e7a83ed..d76aa80f4 100644 --- a/grub-core/Makefile.am +++ b/grub-core/Makefile.am @@ -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 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 @@ -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 diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c index e447fd0fa..5fbcfc147 100644 --- a/grub-core/kern/dl.c +++ b/grub-core/kern/dl.c @@ -475,6 +475,23 @@ 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"); + + if (s == NULL) + return grub_error (GRUB_ERR_BAD_MODULE, + N_("no version section in module %.63s"), mod->name); + + if (grub_strcmp ((char *) e + s->sh_offset, PACKAGE_VERSION) != 0) + return grub_error (GRUB_ERR_BAD_MODULE, + N_("version mismatch in module %.63s: %.63s"), mod->name, + (char *) e + s->sh_offset); + + return GRUB_ERR_NONE; +} + static grub_err_t grub_dl_resolve_name (grub_dl_t mod, Elf_Ehdr *e) { @@ -650,6 +667,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) diff --git a/util/grub-module-verifierXX.c b/util/grub-module-verifierXX.c index d5907f268..fde111016 100644 --- a/util/grub-module-verifierXX.c +++ b/util/grub-module-verifierXX.c @@ -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); -- 2.30.2