278 lines
8.7 KiB
C
278 lines
8.7 KiB
C
/*
|
|
* VAS_EBOOT -- GRand Unified Bootloader
|
|
* Copyright (C) 2020 Free Software Foundation, Inc.
|
|
*
|
|
* VAS_EBOOT is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* VAS_EBOOT is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
* UEFI Secure Boot related checkings.
|
|
*/
|
|
|
|
#include <VasEBoot/efi/efi.h>
|
|
#include <VasEBoot/efi/pe32.h>
|
|
#include <VasEBoot/efi/sb.h>
|
|
#include <VasEBoot/env.h>
|
|
#include <VasEBoot/err.h>
|
|
#include <VasEBoot/file.h>
|
|
#include <VasEBoot/i386/linux.h>
|
|
#include <VasEBoot/kernel.h>
|
|
#include <VasEBoot/mm.h>
|
|
#include <VasEBoot/types.h>
|
|
#include <VasEBoot/verify.h>
|
|
|
|
static VasEBoot_guid_t shim_lock_guid = VAS_EBOOT_EFI_SHIM_LOCK_GUID;
|
|
static VasEBoot_guid_t shim_loader_guid = VAS_EBOOT_EFI_SHIM_IMAGE_LOADER_GUID;
|
|
|
|
static VasEBoot_efi_loader_t *shim_loader = NULL;
|
|
static VasEBoot_efi_shim_lock_protocol_t *shim_lock = NULL;
|
|
|
|
static VasEBoot_efi_handle_t last_verified_image_handle = NULL;
|
|
|
|
/*
|
|
* Determine whether we're in secure boot mode.
|
|
*
|
|
* Please keep the logic in sync with the Linux kernel,
|
|
* drivers/firmware/efi/libstub/secureboot.c:efi_get_secureboot().
|
|
*/
|
|
VasEBoot_uint8_t
|
|
VasEBoot_efi_get_secureboot (void)
|
|
{
|
|
static VasEBoot_guid_t efi_variable_guid = VAS_EBOOT_EFI_GLOBAL_VARIABLE_GUID;
|
|
VasEBoot_efi_status_t status;
|
|
VasEBoot_efi_uint32_t attr = 0;
|
|
VasEBoot_size_t size = 0;
|
|
VasEBoot_uint8_t *secboot = NULL;
|
|
VasEBoot_uint8_t *setupmode = NULL;
|
|
VasEBoot_uint8_t *moksbstate = NULL;
|
|
VasEBoot_uint8_t secureboot = VAS_EBOOT_EFI_SECUREBOOT_MODE_UNKNOWN;
|
|
const char *secureboot_str = "UNKNOWN";
|
|
|
|
status = VasEBoot_efi_get_variable ("SecureBoot", &efi_variable_guid,
|
|
&size, (void **) &secboot);
|
|
|
|
if (status == VAS_EBOOT_EFI_NOT_FOUND)
|
|
{
|
|
secureboot = VAS_EBOOT_EFI_SECUREBOOT_MODE_DISABLED;
|
|
goto out;
|
|
}
|
|
|
|
if (status != VAS_EBOOT_EFI_SUCCESS)
|
|
goto out;
|
|
|
|
status = VasEBoot_efi_get_variable ("SetupMode", &efi_variable_guid,
|
|
&size, (void **) &setupmode);
|
|
|
|
if (status != VAS_EBOOT_EFI_SUCCESS)
|
|
goto out;
|
|
|
|
if ((*secboot == 0) || (*setupmode == 1))
|
|
{
|
|
secureboot = VAS_EBOOT_EFI_SECUREBOOT_MODE_DISABLED;
|
|
goto out;
|
|
}
|
|
|
|
/*
|
|
* See if a user has put the shim into insecure mode. If so, and if the
|
|
* variable doesn't have the runtime attribute set, we might as well
|
|
* honor that.
|
|
*/
|
|
status = VasEBoot_efi_get_variable_with_attributes ("MokSBState", &shim_lock_guid,
|
|
&size, (void **) &moksbstate, &attr);
|
|
|
|
/* If it fails, we don't care why. Default to secure. */
|
|
if (status != VAS_EBOOT_EFI_SUCCESS)
|
|
{
|
|
secureboot = VAS_EBOOT_EFI_SECUREBOOT_MODE_ENABLED;
|
|
goto out;
|
|
}
|
|
|
|
if (!(attr & VAS_EBOOT_EFI_VARIABLE_RUNTIME_ACCESS) && *moksbstate == 1)
|
|
{
|
|
secureboot = VAS_EBOOT_EFI_SECUREBOOT_MODE_DISABLED;
|
|
goto out;
|
|
}
|
|
|
|
secureboot = VAS_EBOOT_EFI_SECUREBOOT_MODE_ENABLED;
|
|
|
|
out:
|
|
VasEBoot_free (moksbstate);
|
|
VasEBoot_free (setupmode);
|
|
VasEBoot_free (secboot);
|
|
|
|
if (secureboot == VAS_EBOOT_EFI_SECUREBOOT_MODE_DISABLED)
|
|
secureboot_str = "Disabled";
|
|
else if (secureboot == VAS_EBOOT_EFI_SECUREBOOT_MODE_ENABLED)
|
|
secureboot_str = "Enabled";
|
|
|
|
VasEBoot_dprintf ("efi", "UEFI Secure Boot state: %s\n", secureboot_str);
|
|
|
|
return secureboot;
|
|
}
|
|
|
|
static VasEBoot_err_t
|
|
shim_lock_verifier_init (VasEBoot_file_t io __attribute__ ((unused)),
|
|
enum VasEBoot_file_type type,
|
|
void **context __attribute__ ((unused)),
|
|
enum VasEBoot_verify_flags *flags)
|
|
{
|
|
*flags = VAS_EBOOT_VERIFY_FLAGS_NONE;
|
|
|
|
switch (type & VAS_EBOOT_FILE_TYPE_MASK)
|
|
{
|
|
/* Files we check. */
|
|
case VAS_EBOOT_FILE_TYPE_LINUX_KERNEL:
|
|
case VAS_EBOOT_FILE_TYPE_MULTIBOOT_KERNEL:
|
|
case VAS_EBOOT_FILE_TYPE_BSD_KERNEL:
|
|
case VAS_EBOOT_FILE_TYPE_XNU_KERNEL:
|
|
case VAS_EBOOT_FILE_TYPE_PLAN9_KERNEL:
|
|
case VAS_EBOOT_FILE_TYPE_EFI_CHAINLOADED_IMAGE:
|
|
*flags = VAS_EBOOT_VERIFY_FLAGS_SINGLE_CHUNK;
|
|
return VAS_EBOOT_ERR_NONE;
|
|
|
|
/* Files that do not affect secureboot state. */
|
|
case VAS_EBOOT_FILE_TYPE_NONE:
|
|
case VAS_EBOOT_FILE_TYPE_LOOPBACK:
|
|
case VAS_EBOOT_FILE_TYPE_LINUX_INITRD:
|
|
case VAS_EBOOT_FILE_TYPE_OPENBSD_RAMDISK:
|
|
case VAS_EBOOT_FILE_TYPE_XNU_RAMDISK:
|
|
case VAS_EBOOT_FILE_TYPE_SIGNATURE:
|
|
case VAS_EBOOT_FILE_TYPE_PUBLIC_KEY:
|
|
case VAS_EBOOT_FILE_TYPE_PUBLIC_KEY_TRUST:
|
|
case VAS_EBOOT_FILE_TYPE_PRINT_BLOCKLIST:
|
|
case VAS_EBOOT_FILE_TYPE_TESTLOAD:
|
|
case VAS_EBOOT_FILE_TYPE_GET_SIZE:
|
|
case VAS_EBOOT_FILE_TYPE_ZFS_ENCRYPTION_KEY:
|
|
case VAS_EBOOT_FILE_TYPE_CRYPTODISK_ENCRYPTION_KEY:
|
|
case VAS_EBOOT_FILE_TYPE_CRYPTODISK_DETACHED_HEADER:
|
|
case VAS_EBOOT_FILE_TYPE_CAT:
|
|
case VAS_EBOOT_FILE_TYPE_HEXCAT:
|
|
case VAS_EBOOT_FILE_TYPE_CMP:
|
|
case VAS_EBOOT_FILE_TYPE_HASHLIST:
|
|
case VAS_EBOOT_FILE_TYPE_TO_HASH:
|
|
case VAS_EBOOT_FILE_TYPE_KEYBOARD_LAYOUT:
|
|
case VAS_EBOOT_FILE_TYPE_PIXMAP:
|
|
case VAS_EBOOT_FILE_TYPE_VAS_EBOOT_MODULE_LIST:
|
|
case VAS_EBOOT_FILE_TYPE_CONFIG:
|
|
case VAS_EBOOT_FILE_TYPE_THEME:
|
|
case VAS_EBOOT_FILE_TYPE_GETTEXT_CATALOG:
|
|
case VAS_EBOOT_FILE_TYPE_FS_SEARCH:
|
|
case VAS_EBOOT_FILE_TYPE_LOADENV:
|
|
case VAS_EBOOT_FILE_TYPE_SAVEENV:
|
|
case VAS_EBOOT_FILE_TYPE_VERIFY_SIGNATURE:
|
|
*flags = VAS_EBOOT_VERIFY_FLAGS_SKIP_VERIFICATION;
|
|
return VAS_EBOOT_ERR_NONE;
|
|
|
|
/* Other files. */
|
|
default:
|
|
return VasEBoot_error (VAS_EBOOT_ERR_ACCESS_DENIED, N_("prohibited by secure boot policy"));
|
|
}
|
|
}
|
|
|
|
static VasEBoot_err_t
|
|
shim_lock_verifier_write (void *context __attribute__ ((unused)), void *buf, VasEBoot_size_t size)
|
|
{
|
|
VasEBoot_efi_handle_t image_handle;
|
|
|
|
if (shim_loader != NULL)
|
|
{
|
|
if (last_verified_image_handle != NULL)
|
|
{
|
|
/*
|
|
* Unload the previous image because ownership of the handle was
|
|
* not transfered to a loader, and a new image is being loaded.
|
|
*/
|
|
shim_loader->unload_image (last_verified_image_handle);
|
|
last_verified_image_handle = NULL;
|
|
}
|
|
|
|
if (shim_loader->load_image (false, VasEBoot_efi_image_handle, NULL, buf, size, &image_handle) != VAS_EBOOT_EFI_SUCCESS)
|
|
/* If verification fails no handle is produced. */
|
|
return VasEBoot_error (VAS_EBOOT_ERR_BAD_SIGNATURE, N_("bad shim loader signature"));
|
|
|
|
/*
|
|
* Not unloading the image here because chainloader and linux
|
|
* might use this handle to avoid double TPM measurements.
|
|
*/
|
|
last_verified_image_handle = image_handle;
|
|
return VAS_EBOOT_ERR_NONE;
|
|
}
|
|
if (shim_lock != NULL)
|
|
{
|
|
if (shim_lock->verify (buf, size) != VAS_EBOOT_EFI_SUCCESS)
|
|
return VasEBoot_error (VAS_EBOOT_ERR_BAD_SIGNATURE, N_("bad shim lock signature"));
|
|
return VAS_EBOOT_ERR_NONE;
|
|
}
|
|
|
|
return VasEBoot_error (VAS_EBOOT_ERR_ACCESS_DENIED, N_("shim protocols not found"));
|
|
}
|
|
|
|
struct VasEBoot_file_verifier shim_lock_verifier =
|
|
{
|
|
.name = "shim_lock_verifier",
|
|
.init = shim_lock_verifier_init,
|
|
.write = shim_lock_verifier_write
|
|
};
|
|
|
|
void
|
|
VasEBoot_shim_lock_verifier_setup (void)
|
|
{
|
|
struct VasEBoot_module_header *header;
|
|
|
|
/* Secure Boot is off. Ignore shim. */
|
|
if (VasEBoot_efi_get_secureboot () != VAS_EBOOT_EFI_SECUREBOOT_MODE_ENABLED)
|
|
return;
|
|
|
|
/* Find both shim protocols. */
|
|
shim_loader = VasEBoot_efi_locate_protocol (&shim_loader_guid, 0);
|
|
shim_lock = VasEBoot_efi_locate_protocol (&shim_lock_guid, 0);
|
|
|
|
/* shim is missing, check if VAS_EBOOT image is built with --disable-shim-lock. */
|
|
if (shim_loader == NULL && shim_lock == NULL)
|
|
{
|
|
FOR_MODULES (header)
|
|
{
|
|
if (header->type == OBJ_TYPE_DISABLE_SHIM_LOCK)
|
|
return;
|
|
}
|
|
}
|
|
|
|
/* Enforce shim_lock_verifier. */
|
|
VasEBoot_verifier_register (&shim_lock_verifier);
|
|
|
|
/* Register shim loader if supported. */
|
|
VasEBoot_efi_register_loader (shim_loader);
|
|
|
|
VasEBoot_env_set ("shim_lock", "y");
|
|
VasEBoot_env_export ("shim_lock");
|
|
}
|
|
|
|
bool
|
|
VasEBoot_is_using_legacy_shim_lock_protocol (void)
|
|
{
|
|
return (shim_loader == NULL && shim_lock != NULL) ? true : false;
|
|
}
|
|
|
|
VasEBoot_efi_handle_t
|
|
VasEBoot_efi_get_last_verified_image_handle (void)
|
|
{
|
|
VasEBoot_efi_handle_t tmp = last_verified_image_handle;
|
|
|
|
/*
|
|
* This function is intended to act as a "transfer of ownership"
|
|
* of the handle. We set it to NULL so that it cannot be buggily
|
|
* retrieved more than once and reused for the wrong image.
|
|
*/
|
|
last_verified_image_handle = NULL;
|
|
return tmp;
|
|
}
|