aboutsummaryrefslogtreecommitdiffstats
path: root/src/interface/efi/efi_shim.c
blob: 500a274a390f129312e0748dc734c52340828a94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (C) 2022 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program 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 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <string.h>
#include <errno.h>
#include <ipxe/image.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_shim.h>
#include <ipxe/efi/Protocol/ShimLock.h>

/** @file
 *
 * UEFI shim handling
 *
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

/** UEFI shim image */
struct image_tag efi_shim __image_tag = {
	.name = "SHIM",
};

/** UEFI shim crutch image */
struct image_tag efi_shim_crutch __image_tag = {
	.name = "SHIMCRUTCH",
};

/** Original ExitBootServices() function */
static EFI_EXIT_BOOT_SERVICES efi_shim_orig_ebs;

/**
 * Unlock UEFI shim
 *
 * @v image		Image handle
 * @v key		Map key
 * @ret efirc		EFI status code
 *
 * The UEFI shim is gradually becoming less capable of directly
 * executing a kernel image, due to an ever increasing list of
 * assumptions that it will only ever be used in conjunction with a
 * second stage loader such as GRUB.
 *
 * For example: shim will erroneously complain if the image that it
 * loads and executes does not call in to the "shim lock protocol"
 * before calling ExitBootServices(), even if there is no valid reason
 * for it to have done so.
 *
 * Reduce the Secure Boot attack surface by removing, where possible,
 * this spurious requirement for the use of an additional second stage
 * loader.
 */
static EFIAPI EFI_STATUS efi_shim_unlock ( EFI_HANDLE image, UINTN key ) {
	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
	uint8_t empty[0];
	union {
		EFI_SHIM_LOCK_PROTOCOL *lock;
		void *interface;
	} u;
	EFI_STATUS efirc;

	//
	DBG ( "******** called\n" );

	/* Locate shim lock protocol */
	if ( ( efirc = bs->LocateProtocol ( &efi_shim_lock_protocol_guid,
					    NULL, &u.interface ) ) == 0 ) {
		u.lock->Verify ( empty, sizeof ( empty ) );
		DBGC ( u.lock, "SHIM unlocked %p\n", u.lock );
	}

	/* Hand off to original ExitBootServices() */
	return efi_shim_orig_ebs ( image, key );
}

/**
 * Install UEFI shim unlocker
 *
 * @ret rc		Return status code
 */
int efi_shim_install (  ) {
	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;

	/* Intercept ExitBootServices() via boot services table */
	efi_shim_orig_ebs = bs->ExitBootServices;
	bs->ExitBootServices = efi_shim_unlock;

	//
	DBG ( "******** hooked in systab %p\n", efi_systab );


	return 0;
}

/**
 * Uninstall UEFI shim unlocker
 *
 */
void efi_shim_uninstall ( void ) {
	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;

	/* Restore original ExitBootServices() */
	bs->ExitBootServices = efi_shim_orig_ebs;
}