]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/SecurityStubDxe/SecurityStub.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Universal / SecurityStubDxe / SecurityStub.c
1 /** @file
2 This driver produces Security2 and Security architectural protocol based on SecurityManagementLib.
3
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9
10 #include <Uefi.h>
11 #include <Protocol/Security.h>
12 #include <Protocol/Security2.h>
13 #include <Library/DebugLib.h>
14 #include <Library/UefiBootServicesTableLib.h>
15 #include <Library/UefiDriverEntryPoint.h>
16 #include <Library/SecurityManagementLib.h>
17 #include "Defer3rdPartyImageLoad.h"
18
19 //
20 // Handle for the Security Architectural Protocol instance produced by this driver
21 //
22 EFI_HANDLE mSecurityArchProtocolHandle = NULL;
23
24 /**
25 The EFI_SECURITY_ARCH_PROTOCOL (SAP) is used to abstract platform-specific
26 policy from the DXE core response to an attempt to use a file that returns a
27 given status for the authentication check from the section extraction protocol.
28
29 The possible responses in a given SAP implementation may include locking
30 flash upon failure to authenticate, attestation logging for all signed drivers,
31 and other exception operations. The File parameter allows for possible logging
32 within the SAP of the driver.
33
34 If File is NULL, then EFI_INVALID_PARAMETER is returned.
35
36 If the file specified by File with an authentication status specified by
37 AuthenticationStatus is safe for the DXE Core to use, then EFI_SUCCESS is returned.
38
39 If the file specified by File with an authentication status specified by
40 AuthenticationStatus is not safe for the DXE Core to use under any circumstances,
41 then EFI_ACCESS_DENIED is returned.
42
43 If the file specified by File with an authentication status specified by
44 AuthenticationStatus is not safe for the DXE Core to use right now, but it
45 might be possible to use it at a future time, then EFI_SECURITY_VIOLATION is
46 returned.
47
48 @param This The EFI_SECURITY_ARCH_PROTOCOL instance.
49 @param AuthenticationStatus
50 This is the authentication type returned from the Section
51 Extraction protocol. See the Section Extraction Protocol
52 Specification for details on this type.
53 @param File This is a pointer to the device path of the file that is
54 being dispatched. This will optionally be used for logging.
55
56 @retval EFI_SUCCESS Do nothing and return success.
57 @retval EFI_INVALID_PARAMETER File is NULL.
58 **/
59 EFI_STATUS
60 EFIAPI
61 SecurityStubAuthenticateState (
62 IN CONST EFI_SECURITY_ARCH_PROTOCOL *This,
63 IN UINT32 AuthenticationStatus,
64 IN CONST EFI_DEVICE_PATH_PROTOCOL *File
65 )
66 {
67 EFI_STATUS Status;
68
69 Status = ExecuteSecurity2Handlers (EFI_AUTH_OPERATION_AUTHENTICATION_STATE,
70 AuthenticationStatus,
71 File,
72 NULL,
73 0,
74 FALSE
75 );
76 if (Status == EFI_SUCCESS) {
77 Status = ExecuteSecurityHandlers (AuthenticationStatus, File);
78 }
79
80 return Status;
81 }
82
83 /**
84 The DXE Foundation uses this service to measure and/or verify a UEFI image.
85
86 This service abstracts the invocation of Trusted Computing Group (TCG) measured boot, UEFI
87 Secure boot, and UEFI User Identity infrastructure. For the former two, the DXE Foundation
88 invokes the FileAuthentication() with a DevicePath and corresponding image in
89 FileBuffer memory. The TCG measurement code will record the FileBuffer contents into the
90 appropriate PCR. The image verification logic will confirm the integrity and provenance of the
91 image in FileBuffer of length FileSize . The origin of the image will be DevicePath in
92 these cases.
93 If the FileBuffer is NULL, the interface will determine if the DevicePath can be connected
94 in order to support the User Identification policy.
95
96 @param This The EFI_SECURITY2_ARCH_PROTOCOL instance.
97 @param File A pointer to the device path of the file that is
98 being dispatched. This will optionally be used for logging.
99 @param FileBuffer A pointer to the buffer with the UEFI file image.
100 @param FileSize The size of the file.
101 @param BootPolicy A boot policy that was used to call LoadImage() UEFI service. If
102 FileAuthentication() is invoked not from the LoadImage(),
103 BootPolicy must be set to FALSE.
104
105 @retval EFI_SUCCESS The file specified by DevicePath and non-NULL
106 FileBuffer did authenticate, and the platform policy dictates
107 that the DXE Foundation may use the file.
108 @retval EFI_SUCCESS The device path specified by NULL device path DevicePath
109 and non-NULL FileBuffer did authenticate, and the platform
110 policy dictates that the DXE Foundation may execute the image in
111 FileBuffer.
112 @retval EFI_SUCCESS FileBuffer is NULL and current user has permission to start
113 UEFI device drivers on the device path specified by DevicePath.
114 @retval EFI_SECURITY_VIOLATION The file specified by DevicePath and FileBuffer did not
115 authenticate, and the platform policy dictates that the file should be
116 placed in the untrusted state. The image has been added to the file
117 execution table.
118 @retval EFI_ACCESS_DENIED The file specified by File and FileBuffer did not
119 authenticate, and the platform policy dictates that the DXE
120 Foundation many not use File.
121 @retval EFI_SECURITY_VIOLATION FileBuffer is NULL and the user has no
122 permission to start UEFI device drivers on the device path specified
123 by DevicePath.
124 @retval EFI_SECURITY_VIOLATION FileBuffer is not NULL and the user has no permission to load
125 drivers from the device path specified by DevicePath. The
126 image has been added into the list of the deferred images.
127 **/
128 EFI_STATUS
129 EFIAPI
130 Security2StubAuthenticate (
131 IN CONST EFI_SECURITY2_ARCH_PROTOCOL *This,
132 IN CONST EFI_DEVICE_PATH_PROTOCOL *File,
133 IN VOID *FileBuffer,
134 IN UINTN FileSize,
135 IN BOOLEAN BootPolicy
136 )
137 {
138 EFI_STATUS Status;
139
140 if (FileBuffer != NULL) {
141 Status = Defer3rdPartyImageLoad (File, BootPolicy);
142 if (EFI_ERROR (Status)) {
143 return Status;
144 }
145 }
146
147 return ExecuteSecurity2Handlers (EFI_AUTH_OPERATION_VERIFY_IMAGE |
148 EFI_AUTH_OPERATION_DEFER_IMAGE_LOAD |
149 EFI_AUTH_OPERATION_MEASURE_IMAGE |
150 EFI_AUTH_OPERATION_CONNECT_POLICY,
151 0,
152 File,
153 FileBuffer,
154 FileSize,
155 BootPolicy
156 );
157 }
158
159 //
160 // Security2 and Security Architectural Protocol instance produced by this driver
161 //
162 EFI_SECURITY_ARCH_PROTOCOL mSecurityStub = {
163 SecurityStubAuthenticateState
164 };
165
166 EFI_SECURITY2_ARCH_PROTOCOL mSecurity2Stub = {
167 Security2StubAuthenticate
168 };
169
170 /**
171 Installs Security2 and Security Architectural Protocol.
172
173 @param ImageHandle The image handle of this driver.
174 @param SystemTable A pointer to the EFI System Table.
175
176 @retval EFI_SUCCESS Install the sample Security Architectural Protocol successfully.
177
178 **/
179 EFI_STATUS
180 EFIAPI
181 SecurityStubInitialize (
182 IN EFI_HANDLE ImageHandle,
183 IN EFI_SYSTEM_TABLE *SystemTable
184 )
185 {
186 EFI_STATUS Status;
187
188 //
189 // Make sure the Security Architectural Protocol is not already installed in the system
190 //
191 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiSecurity2ArchProtocolGuid);
192 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiSecurityArchProtocolGuid);
193
194 //
195 // Install the Security Architectural Protocol onto a new handle
196 //
197 Status = gBS->InstallMultipleProtocolInterfaces (
198 &mSecurityArchProtocolHandle,
199 &gEfiSecurity2ArchProtocolGuid,
200 &mSecurity2Stub,
201 &gEfiSecurityArchProtocolGuid,
202 &mSecurityStub,
203 NULL
204 );
205 ASSERT_EFI_ERROR (Status);
206
207 Defer3rdPartyImageLoadInitialize ();
208
209 return EFI_SUCCESS;
210 }