summaryrefslogtreecommitdiffstats
path: root/SecurityPkg/Library/DxeImageVerificationLib/Measurement.c
blob: 86d8eb484052635086bb9942900cd4e6f0ed4644 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/** @file
  Measure TCG required variable.

Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <PiDxe.h>
#include <Guid/ImageAuthentication.h>
#include <IndustryStandard/UefiTcgPlatform.h>

#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/BaseLib.h>
#include <Library/TpmMeasurementLib.h>

typedef struct {
  CHAR16                                 *VariableName;
  EFI_GUID                               *VendorGuid;
} VARIABLE_TYPE;

typedef struct {
  CHAR16                                 *VariableName;
  EFI_GUID                               *VendorGuid;
  VOID                                   *Data;
  UINTN                                  Size;
} VARIABLE_RECORD;

#define  MEASURED_AUTHORITY_COUNT_MAX  0x100

UINTN            mMeasuredAuthorityCount    = 0;
UINTN            mMeasuredAuthorityCountMax = 0;
VARIABLE_RECORD  *mMeasuredAuthorityList    = NULL;

VARIABLE_TYPE  mVariableType[] = {
  {EFI_IMAGE_SECURITY_DATABASE,  &gEfiImageSecurityDatabaseGuid},
};

/**
  This function will check if VarName should be recorded and return the address of VarName if it is needed.

  @param[in]  VarName           A Null-terminated string that is the name of the vendor's variable.

  @return the address of VarName.
**/
CHAR16 *
AssignVarName (
  IN      CHAR16                    *VarName
  )
{
  UINTN  Index;

  for (Index = 0; Index < sizeof(mVariableType)/sizeof(mVariableType[0]); Index++) {
    if (StrCmp (VarName, mVariableType[Index].VariableName) == 0) {
      return mVariableType[Index].VariableName;
    }
  }

  return NULL;
}

/**
  This function will check if VendorGuid should be recorded and return the address of VendorGuid if it is needed.

  @param[in]  VendorGuid        A unique identifier for the vendor.

  @return the address of VendorGuid.
**/
EFI_GUID *
AssignVendorGuid (
  IN      EFI_GUID                  *VendorGuid
  )
{
  UINTN  Index;

  for (Index = 0; Index < sizeof(mVariableType)/sizeof(mVariableType[0]); Index++) {
    if (CompareGuid (VendorGuid, mVariableType[Index].VendorGuid)) {
      return mVariableType[Index].VendorGuid;
    }
  }

  return NULL;
}

/**
  This function will add variable information to MeasuredAuthorityList.

  @param[in]  VarName           A Null-terminated string that is the name of the vendor's variable.
  @param[in]  VendorGuid        A unique identifier for the vendor.
  @param[in]  VarData           The content of the variable data.
  @param[in]  VarSize           The size of the variable data.

  @retval EFI_SUCCESS           Operation completed successfully.
  @retval EFI_OUT_OF_RESOURCES  Out of memory.
**/
EFI_STATUS
AddDataMeasured (
  IN      CHAR16                    *VarName,
  IN      EFI_GUID                  *VendorGuid,
  IN      VOID                      *Data,
  IN      UINTN                     Size
  )
{
  VARIABLE_RECORD  *NewMeasuredAuthorityList;

  ASSERT (mMeasuredAuthorityCount <= mMeasuredAuthorityCountMax);
  if (mMeasuredAuthorityCount == mMeasuredAuthorityCountMax) {
    //
    // Need enlarge
    //
    NewMeasuredAuthorityList = AllocateZeroPool (sizeof(VARIABLE_RECORD) * (mMeasuredAuthorityCountMax + MEASURED_AUTHORITY_COUNT_MAX));
    if (NewMeasuredAuthorityList == NULL) {
      return EFI_OUT_OF_RESOURCES;
    }
    if (mMeasuredAuthorityList != NULL) {
      CopyMem (NewMeasuredAuthorityList, mMeasuredAuthorityList, sizeof(VARIABLE_RECORD) * mMeasuredAuthorityCount);
      FreePool (mMeasuredAuthorityList);
    }
    mMeasuredAuthorityList     = NewMeasuredAuthorityList;
    mMeasuredAuthorityCountMax += MEASURED_AUTHORITY_COUNT_MAX;
  }

  //
  // Add new entry
  //
  mMeasuredAuthorityList[mMeasuredAuthorityCount].VariableName = AssignVarName (VarName);
  mMeasuredAuthorityList[mMeasuredAuthorityCount].VendorGuid   = AssignVendorGuid (VendorGuid);
  mMeasuredAuthorityList[mMeasuredAuthorityCount].Size         = Size;
  mMeasuredAuthorityList[mMeasuredAuthorityCount].Data         = AllocatePool (Size);
  if (mMeasuredAuthorityList[mMeasuredAuthorityCount].Data == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }
  CopyMem (mMeasuredAuthorityList[mMeasuredAuthorityCount].Data, Data, Size);
  mMeasuredAuthorityCount++;

  return EFI_SUCCESS;
}

/**
  This function will return if this variable is already measured.

  @param[in]  VarName           A Null-terminated string that is the name of the vendor's variable.
  @param[in]  VendorGuid        A unique identifier for the vendor.
  @param[in]  VarData           The content of the variable data.
  @param[in]  VarSize           The size of the variable data.

  @retval TRUE  The data is already measured.
  @retval FALSE The data is not measured yet.
**/
BOOLEAN
IsDataMeasured (
  IN      CHAR16                    *VarName,
  IN      EFI_GUID                  *VendorGuid,
  IN      VOID                      *Data,
  IN      UINTN                     Size
  )
{
  UINTN  Index;

  for (Index = 0; Index < mMeasuredAuthorityCount; Index++) {
    if ((StrCmp (VarName, mMeasuredAuthorityList[Index].VariableName) == 0) &&
        (CompareGuid (VendorGuid, mMeasuredAuthorityList[Index].VendorGuid)) &&
        (CompareMem (Data, mMeasuredAuthorityList[Index].Data, Size) == 0) &&
        (Size == mMeasuredAuthorityList[Index].Size)) {
      return TRUE;
    }
  }

  return FALSE;
}

/**
  This function will return if this variable is SecureAuthority Variable.

  @param[in]  VariableName      A Null-terminated string that is the name of the vendor's variable.
  @param[in]  VendorGuid        A unique identifier for the vendor.

  @retval TRUE  This is SecureAuthority Variable
  @retval FALSE This is not SecureAuthority Variable
**/
BOOLEAN
IsSecureAuthorityVariable (
  IN CHAR16                                 *VariableName,
  IN EFI_GUID                               *VendorGuid
  )
{
  UINTN   Index;

  for (Index = 0; Index < sizeof(mVariableType)/sizeof(mVariableType[0]); Index++) {
    if ((StrCmp (VariableName, mVariableType[Index].VariableName) == 0) &&
        (CompareGuid (VendorGuid, mVariableType[Index].VendorGuid))) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
  Measure and log an EFI variable, and extend the measurement result into a specific PCR.

  @param[in]  VarName           A Null-terminated string that is the name of the vendor's variable.
  @param[in]  VendorGuid        A unique identifier for the vendor.
  @param[in]  VarData           The content of the variable data.
  @param[in]  VarSize           The size of the variable data.

  @retval EFI_SUCCESS           Operation completed successfully.
  @retval EFI_OUT_OF_RESOURCES  Out of memory.
  @retval EFI_DEVICE_ERROR      The operation was unsuccessful.

**/
EFI_STATUS
EFIAPI
MeasureVariable (
  IN      CHAR16                    *VarName,
  IN      EFI_GUID                  *VendorGuid,
  IN      VOID                      *VarData,
  IN      UINTN                     VarSize
  )
{
  EFI_STATUS                        Status;
  UINTN                             VarNameLength;
  UEFI_VARIABLE_DATA                *VarLog;
  UINT32                            VarLogSize;

  //
  // The UEFI_VARIABLE_DATA.VariableData value shall be the EFI_SIGNATURE_DATA value
  // from the EFI_SIGNATURE_LIST that contained the authority that was used to validate the image
  //
  VarNameLength      = StrLen (VarName);
  VarLogSize = (UINT32)(sizeof (*VarLog) + VarNameLength * sizeof (*VarName) + VarSize
                        - sizeof (VarLog->UnicodeName) - sizeof (VarLog->VariableData));

  VarLog = (UEFI_VARIABLE_DATA *) AllocateZeroPool (VarLogSize);
  if (VarLog == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  CopyMem (&VarLog->VariableName, VendorGuid, sizeof(VarLog->VariableName));
  VarLog->UnicodeNameLength  = VarNameLength;
  VarLog->VariableDataLength = VarSize;
  CopyMem (
     VarLog->UnicodeName,
     VarName,
     VarNameLength * sizeof (*VarName)
     );
  CopyMem (
     (CHAR16 *)VarLog->UnicodeName + VarNameLength,
     VarData,
     VarSize
     );

  DEBUG ((EFI_D_INFO, "DxeImageVerification: MeasureVariable (Pcr - %x, EventType - %x, ", (UINTN)7, (UINTN)EV_EFI_VARIABLE_AUTHORITY));
  DEBUG ((EFI_D_INFO, "VariableName - %s, VendorGuid - %g)\n", VarName, VendorGuid));

  Status = TpmMeasureAndLogData (
             7,
             EV_EFI_VARIABLE_AUTHORITY,
             VarLog,
             VarLogSize,
             VarLog,
             VarLogSize
             );
  FreePool (VarLog);

  return Status;
}

/**
  SecureBoot Hook for processing image verification.

  @param[in] VariableName                 Name of Variable to be found.
  @param[in] VendorGuid                   Variable vendor GUID.
  @param[in] DataSize                     Size of Data found. If size is less than the
                                          data, this value contains the required size.
  @param[in] Data                         Data pointer.

**/
VOID
EFIAPI
SecureBootHook (
  IN CHAR16                                 *VariableName,
  IN EFI_GUID                               *VendorGuid,
  IN UINTN                                  DataSize,
  IN VOID                                   *Data
  )
{
  EFI_STATUS                        Status;

  if (!IsSecureAuthorityVariable (VariableName, VendorGuid)) {
    return ;
  }

  if (IsDataMeasured (VariableName, VendorGuid, Data, DataSize)) {
    DEBUG ((EFI_D_ERROR, "MeasureSecureAuthorityVariable - IsDataMeasured\n"));
    return ;
  }

  Status = MeasureVariable (
             VariableName,
             VendorGuid,
             Data,
             DataSize
             );
  DEBUG ((EFI_D_INFO, "MeasureBootPolicyVariable - %r\n", Status));

  if (!EFI_ERROR (Status)) {
    AddDataMeasured (VariableName, VendorGuid, Data, DataSize);
  }

  return ;
}