Debian Codegraph @

lib/security_policy.c

raw
1 // SPDX-License-Identifier: BSD-2-Clause-Patent
2 /*
3 * Copyright 2012 <[email protected]>
4 *
5 * Install and remove a platform security2 override policy
6 */
7 #include "shim.h"
8
9 #if defined(OVERRIDE_SECURITY_POLICY)
10
11 /*
12 * See the UEFI Platform Initialization manual (Vol2: DXE) for this
13 */
14 struct _EFI_SECURITY2_PROTOCOL;
15 struct _EFI_SECURITY_PROTOCOL;
16 typedef struct _EFI_SECURITY2_PROTOCOL EFI_SECURITY2_PROTOCOL;
17 typedef struct _EFI_SECURITY_PROTOCOL EFI_SECURITY_PROTOCOL;
18 typedef EFI_DEVICE_PATH EFI_DEVICE_PATH_PROTOCOL;
19
20 typedef EFI_STATUS (EFIAPI *EFI_SECURITY_FILE_AUTHENTICATION_STATE) (
21 const EFI_SECURITY_PROTOCOL *This,
22 UINT32 AuthenticationStatus,
23 const EFI_DEVICE_PATH_PROTOCOL *File
24 );
25 typedef EFI_STATUS (EFIAPI *EFI_SECURITY2_FILE_AUTHENTICATION) (
26 const EFI_SECURITY2_PROTOCOL *This,
27 const EFI_DEVICE_PATH_PROTOCOL *DevicePath,
28 VOID *FileBuffer,
29 UINTN FileSize,
30 BOOLEAN BootPolicy
31 );
32
33 struct _EFI_SECURITY2_PROTOCOL {
34 EFI_SECURITY2_FILE_AUTHENTICATION FileAuthentication;
35 };
36
37 struct _EFI_SECURITY_PROTOCOL {
38 EFI_SECURITY_FILE_AUTHENTICATION_STATE FileAuthenticationState;
39 };
40
41
42 static UINT8 *security_policy_esl = NULL;
43 static UINTN security_policy_esl_len;
44 static SecurityHook extra_check = NULL;
45
46 static EFI_SECURITY_FILE_AUTHENTICATION_STATE esfas = NULL;
47 static EFI_SECURITY2_FILE_AUTHENTICATION es2fa = NULL;
48
49 extern EFI_STATUS thunk_security_policy_authentication(
50 const EFI_SECURITY_PROTOCOL *This,
51 UINT32 AuthenticationStatus,
52 const EFI_DEVICE_PATH_PROTOCOL *DevicePath
53 )
54 __attribute__((unused));
55
56 extern EFI_STATUS thunk_security2_policy_authentication(
57 const EFI_SECURITY2_PROTOCOL *This,
58 const EFI_DEVICE_PATH_PROTOCOL *DevicePath,
59 VOID *FileBuffer,
60 UINTN FileSize,
61 BOOLEAN BootPolicy
62 )
63 __attribute__((unused));
64
65 static __attribute__((used)) EFI_STATUS
66 security2_policy_authentication (
67 const EFI_SECURITY2_PROTOCOL *This,
68 const EFI_DEVICE_PATH_PROTOCOL *DevicePath,
69 VOID *FileBuffer,
70 UINTN FileSize,
71 BOOLEAN BootPolicy
72 )
73 {
74 EFI_STATUS efi_status, auth;
75
76 /* Chain original security policy */
77
78 efi_status = es2fa(This, DevicePath, FileBuffer, FileSize, BootPolicy);
79 /* if OK, don't bother with MOK check */
80 if (!EFI_ERROR(efi_status))
81 return efi_status;
82
83 if (extra_check)
84 auth = extra_check(FileBuffer, FileSize);
85 else
86 return EFI_SECURITY_VIOLATION;
87
88 if (auth == EFI_SECURITY_VIOLATION || auth == EFI_ACCESS_DENIED)
89 /* return previous status, which is the correct one
90 * for the platform: may be either EFI_ACCESS_DENIED
91 * or EFI_SECURITY_VIOLATION */
92 return efi_status;
93
94 return auth;
95 }
96
97 static __attribute__((used)) EFI_STATUS
98 security_policy_authentication (
99 const EFI_SECURITY_PROTOCOL *This,
100 UINT32 AuthenticationStatus,
101 const EFI_DEVICE_PATH_PROTOCOL *DevicePathConst
102 )
103 {
104 EFI_STATUS efi_status, fail_status;
105 EFI_DEVICE_PATH *DevPath
106 = DuplicateDevicePath((EFI_DEVICE_PATH *)DevicePathConst),
107 *OrigDevPath = DevPath;
108 EFI_HANDLE h;
109 EFI_FILE *f;
110 VOID *FileBuffer;
111 UINTN FileSize;
112 CHAR16* DevPathStr;
113 EFI_GUID SIMPLE_FS_PROTOCOL = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
114
115 /* Chain original security policy */
116 efi_status = esfas(This, AuthenticationStatus, DevicePathConst);
117 /* if OK avoid checking MOK: It's a bit expensive to
118 * read the whole file in again (esfas already did this) */
119 if (!EFI_ERROR(efi_status))
120 goto out;
121
122 /* capture failure status: may be either EFI_ACCESS_DENIED or
123 * EFI_SECURITY_VIOLATION */
124 fail_status = efi_status;
125
126 efi_status = BS->LocateDevicePath(&SIMPLE_FS_PROTOCOL, &DevPath, &h);
127 if (EFI_ERROR(efi_status))
128 goto out;
129
130 DevPathStr = DevicePathToStr(DevPath);
131
132 efi_status = simple_file_open_by_handle(h, DevPathStr, &f,
133 EFI_FILE_MODE_READ);
134 FreePool(DevPathStr);
135 if (EFI_ERROR(efi_status))
136 goto out;
137
138 efi_status = simple_file_read_all(f, &FileSize, &FileBuffer);
139 f->Close(f);
140 if (EFI_ERROR(efi_status))
141 goto out;
142
143 if (extra_check)
144 efi_status = extra_check(FileBuffer, FileSize);
145 else
146 efi_status = EFI_SECURITY_VIOLATION;
147 FreePool(FileBuffer);
148
149 if (efi_status == EFI_ACCESS_DENIED ||
150 efi_status == EFI_SECURITY_VIOLATION)
151 /* return what the platform originally said */
152 efi_status = fail_status;
153 out:
154 FreePool(OrigDevPath);
155 return efi_status;
156 }
157
158
159 /* Nasty: ELF and EFI have different calling conventions. Here is the map for
160 * calling ELF -> EFI
161 *
162 * 1) rdi -> rcx (32 saved)
163 * 2) rsi -> rdx (32 saved)
164 * 3) rdx -> r8 ( 32 saved)
165 * 4) rcx -> r9 (32 saved)
166 * 5) r8 -> 32(%rsp) (48 saved)
167 * 6) r9 -> 40(%rsp) (48 saved)
168 * 7) pad+0(%rsp) -> 48(%rsp) (64 saved)
169 * 8) pad+8(%rsp) -> 56(%rsp) (64 saved)
170 * 9) pad+16(%rsp) -> 64(%rsp) (80 saved)
171 * 10) pad+24(%rsp) -> 72(%rsp) (80 saved)
172 * 11) pad+32(%rsp) -> 80(%rsp) (96 saved)
173
174 *
175 * So for a five argument callback, the map is ignore the first two arguments
176 * and then map (EFI -> ELF) assuming pad = 0.
177 *
178 * ARG4 -> ARG1
179 * ARG3 -> ARG2
180 * ARG5 -> ARG3
181 * ARG6 -> ARG4
182 * ARG11 -> ARG5
183 *
184 * Calling conventions also differ over volatile and preserved registers in
185 * MS: RBX, RBP, RDI, RSI, R12, R13, R14, and R15 are considered nonvolatile .
186 * In ELF: Registers %rbp, %rbx and %r12 through %r15 “belong” to the calling
187 * function and the called function is required to preserve their values.
188 *
189 * This means when accepting a function callback from MS -> ELF, we have to do
190 * separate preservation on %rdi, %rsi before swizzling the arguments and
191 * handing off to the ELF function.
192 */
193
194 asm (
195 ".type security2_policy_authentication,@function\n"
196 "thunk_security2_policy_authentication:\n\t"
197 "mov 0x28(%rsp), %r10 # ARG5\n\t"
198 "push %rdi\n\t"
199 "push %rsi\n\t"
200 "mov %r10, %rdi\n\t"
201 "subq $8, %rsp # space for storing stack pad\n\t"
202 "mov $0x08, %rax\n\t"
203 "mov $0x10, %r10\n\t"
204 "and %rsp, %rax\n\t"
205 "cmovnz %rax, %r11\n\t"
206 "cmovz %r10, %r11\n\t"
207 "subq %r11, %rsp\n\t"
208 "addq $8, %r11\n\t"
209 "mov %r11, (%rsp)\n\t"
210 "# five argument swizzle\n\t"
211 "mov %rdi, %r10\n\t"
212 "mov %rcx, %rdi\n\t"
213 "mov %rdx, %rsi\n\t"
214 "mov %r8, %rdx\n\t"
215 "mov %r9, %rcx\n\t"
216 "mov %r10, %r8\n\t"
217 "callq security2_policy_authentication@PLT\n\t"
218 "mov (%rsp), %r11\n\t"
219 "addq %r11, %rsp\n\t"
220 "pop %rsi\n\t"
221 "pop %rdi\n\t"
222 "ret\n"
223 );
224
225 asm (
226 ".type security_policy_authentication,@function\n"
227 "thunk_security_policy_authentication:\n\t"
228 "push %rdi\n\t"
229 "push %rsi\n\t"
230 "subq $8, %rsp # space for storing stack pad\n\t"
231 "mov $0x08, %rax\n\t"
232 "mov $0x10, %r10\n\t"
233 "and %rsp, %rax\n\t"
234 "cmovnz %rax, %r11\n\t"
235 "cmovz %r10, %r11\n\t"
236 "subq %r11, %rsp\n\t"
237 "addq $8, %r11\n\t"
238 "mov %r11, (%rsp)\n\t"
239 "# three argument swizzle\n\t"
240 "mov %rcx, %rdi\n\t"
241 "mov %rdx, %rsi\n\t"
242 "mov %r8, %rdx\n\t"
243 "callq security_policy_authentication@PLT\n\t"
244 "mov (%rsp), %r11\n\t"
245 "addq %r11, %rsp\n\t"
246 "pop %rsi\n\t"
247 "pop %rdi\n\t"
248 "ret\n"
249 );
250
251 EFI_STATUS
252 security_policy_install(SecurityHook hook)
253 {
254 EFI_SECURITY_PROTOCOL *security_protocol;
255 EFI_SECURITY2_PROTOCOL *security2_protocol = NULL;
256 EFI_STATUS efi_status;
257
258 if (esfas)
259 /* Already Installed */
260 return EFI_ALREADY_STARTED;
261
262 /* Don't bother with status here. The call is allowed
263 * to fail, since SECURITY2 was introduced in PI 1.2.1
264 * If it fails, use security2_protocol == NULL as indicator */
265 LibLocateProtocol(&SECURITY2_PROTOCOL_GUID,
266 (VOID **) &security2_protocol);
267
268 efi_status = LibLocateProtocol(&SECURITY_PROTOCOL_GUID,
269 (VOID **) &security_protocol);
270 if (EFI_ERROR(efi_status))
271 /* This one is mandatory, so there's a serious problem */
272 return efi_status;
273
274 if (security2_protocol) {
275 es2fa = security2_protocol->FileAuthentication;
276 security2_protocol->FileAuthentication =
277 (EFI_SECURITY2_FILE_AUTHENTICATION) thunk_security2_policy_authentication;
278 }
279
280 esfas = security_protocol->FileAuthenticationState;
281 security_protocol->FileAuthenticationState =
282 (EFI_SECURITY_FILE_AUTHENTICATION_STATE) thunk_security_policy_authentication;
283
284 if (hook)
285 extra_check = hook;
286
287 return EFI_SUCCESS;
288 }
289
290 EFI_STATUS
291 security_policy_uninstall(void)
292 {
293 EFI_STATUS efi_status;
294
295 if (esfas) {
296 EFI_SECURITY_PROTOCOL *security_protocol;
297
298 efi_status = LibLocateProtocol(&SECURITY_PROTOCOL_GUID,
299 (VOID **) &security_protocol);
300 if (EFI_ERROR(efi_status))
301 return efi_status;
302
303 security_protocol->FileAuthenticationState = esfas;
304 esfas = NULL;
305 } else {
306 /* nothing installed */
307 return EFI_NOT_STARTED;
308 }
309
310 if (es2fa) {
311 EFI_SECURITY2_PROTOCOL *security2_protocol;
312
313 efi_status = LibLocateProtocol(&SECURITY2_PROTOCOL_GUID,
314 (VOID **) &security2_protocol);
315 if (EFI_ERROR(efi_status))
316 return efi_status;
317
318 security2_protocol->FileAuthentication = es2fa;
319 es2fa = NULL;
320 }
321
322 if (extra_check)
323 extra_check = NULL;
324
325 return EFI_SUCCESS;
326 }
327
328 void
329 security_protocol_set_hashes(unsigned char *esl, int len)
330 {
331 security_policy_esl = esl;
332 security_policy_esl_len = len;
333 }
334 #endif /* OVERRIDE_SECURITY_POLICY */