90dacf5
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
90dacf5
From: Dimitri John Ledkov <dimitri.ledkov@canonical.com>
90dacf5
Date: Fri, 4 Mar 2022 09:31:43 +0100
90dacf5
Subject: [PATCH] grub-core/loader/efi/chainloader.c: do not validate
90dacf5
 chainloader twice
90dacf5
90dacf5
On secureboot systems, with shimlock verifier, call to
90dacf5
grub_file_open(, GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE) will already
90dacf5
pass the chainloader target through shim-lock protocol verify
90dacf5
call. And create a TPM measurement. If verification fails,
90dacf5
grub_cmd_chainloader will fail at file open time.
90dacf5
90dacf5
This makes previous code paths for negative, and zero return codes
90dacf5
from grub_linuxefi_secure_validate unreachable under secureboot. But
90dacf5
also breaking measurements compatibility with 2.04+linuxefi codebases,
90dacf5
as the chainloader file is passed through shim_lock->verify() twice
90dacf5
(via verifier & direct call to grub_linuxefi_secure_validate)
90dacf5
extending the PCRs twice.
90dacf5
90dacf5
This reduces grub_loader options to perform
90dacf5
grub_secureboot_chainloader when secureboot is on, and otherwise
90dacf5
attempt grub_chainloader_boot.
90dacf5
90dacf5
It means that booting with secureboot off, yet still with shim (which
90dacf5
always verifies things successfully), will stop choosing
90dacf5
grub_secureboot_chainloader, and opting for a more regular
90dacf5
loadimage/startimage codepath. If we want to use the
90dacf5
grub_secureboot_chainloader codepath in such scenarios we should adapt
90dacf5
the code to simply check for shim_lock protocol presence /
90dacf5
shim_lock->context() success?! But I am not sure if that is necessary.
90dacf5
90dacf5
This patch must not be ported to older editions of grub code bases
90dacf5
that do not have verifiers framework, or it is not builtin, or
90dacf5
shim-lock-verifier is an optional module.
90dacf5
90dacf5
Signed-off-by: Dimitri John Ledkov <dimitri.ledkov@canonical.com>
90dacf5
---
90dacf5
 grub-core/loader/efi/chainloader.c | 8 ++------
90dacf5
 1 file changed, 2 insertions(+), 6 deletions(-)
90dacf5
90dacf5
diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c
e622855
index 3af6b12292..644cd2e56f 100644
90dacf5
--- a/grub-core/loader/efi/chainloader.c
90dacf5
+++ b/grub-core/loader/efi/chainloader.c
90dacf5
@@ -906,7 +906,6 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
90dacf5
   grub_efi_device_path_t *dp = 0;
90dacf5
   char *filename;
90dacf5
   void *boot_image = 0;
90dacf5
-  int rc;
90dacf5
 
90dacf5
   if (argc == 0)
90dacf5
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
90dacf5
@@ -1082,9 +1081,7 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
90dacf5
       orig_dev = 0;
90dacf5
     }
90dacf5
 
90dacf5
-  rc = grub_linuxefi_secure_validate((void *)(unsigned long)address, fsize);
90dacf5
-  grub_dprintf ("chain", "linuxefi_secure_validate: %d\n", rc);
90dacf5
-  if (rc > 0)
90dacf5
+  if (grub_efi_get_secureboot () == GRUB_EFI_SECUREBOOT_MODE_ENABLED)
90dacf5
     {
90dacf5
       grub_file_close (file);
90dacf5
       grub_device_close (dev);
90dacf5
@@ -1092,7 +1089,7 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
90dacf5
 		       grub_secureboot_chainloader_unload, 0);
90dacf5
       return 0;
90dacf5
     }
90dacf5
-  else if (rc == 0)
90dacf5
+  else
90dacf5
     {
90dacf5
       grub_load_and_start_image(boot_image);
90dacf5
       grub_file_close (file);
90dacf5
@@ -1101,7 +1098,6 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
90dacf5
 
90dacf5
       return 0;
90dacf5
     }
90dacf5
-  // -1 fall-through to fail
90dacf5
 
90dacf5
 fail:
90dacf5
   if (orig_dev)