]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Dxe/Misc/InstallConfigurationTable.c
MdeModulePkg: Fix use-after-free error in InstallConfigurationTable()
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Misc / InstallConfigurationTable.c
CommitLineData
23c98c94 1/** @file\r
504214c4 2 UEFI Miscellaneous boot Services InstallConfigurationTable service\r
28a00297 3\r
322d827c 4Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
cd5ebaa0 5This program and the accompanying materials\r
23c98c94 6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
28a00297 12\r
504214c4 13**/\r
28a00297 14\r
9c4ac31c 15#include "DxeMain.h"\r
28a00297 16\r
17#define CONFIG_TABLE_SIZE_INCREASED 0x10\r
18\r
19UINTN mSystemTableAllocateSize = 0;\r
20\r
162ed594 21/**\r
22 Boot Service called to add, modify, or remove a system configuration table from\r
23 the EFI System Table.\r
24\r
022c6d45 25 @param Guid Pointer to the GUID for the entry to add, update, or\r
26 remove\r
27 @param Table Pointer to the configuration table for the entry to add,\r
28 update, or remove, may be NULL.\r
162ed594 29\r
30 @return EFI_SUCCESS Guid, Table pair added, updated, or removed.\r
9f72ced4 31 @return EFI_INVALID_PARAMETER Input GUID is NULL.\r
162ed594 32 @return EFI_NOT_FOUND Attempted to delete non-existant entry\r
33 @return EFI_OUT_OF_RESOURCES Not enough memory available\r
34\r
35**/\r
28a00297 36EFI_STATUS\r
37EFIAPI\r
38CoreInstallConfigurationTable (\r
39 IN EFI_GUID *Guid,\r
40 IN VOID *Table\r
41 )\r
28a00297 42{\r
43 UINTN Index;\r
44 EFI_CONFIGURATION_TABLE *EfiConfigurationTable;\r
322d827c 45 EFI_CONFIGURATION_TABLE *OldTable;\r
28a00297 46\r
47 //\r
48 // If Guid is NULL, then this operation cannot be performed\r
49 //\r
50 if (Guid == NULL) {\r
51 return EFI_INVALID_PARAMETER;\r
52 }\r
53\r
54 EfiConfigurationTable = gDxeCoreST->ConfigurationTable;\r
55\r
56 //\r
57 // Search all the table for an entry that matches Guid\r
58 //\r
59 for (Index = 0; Index < gDxeCoreST->NumberOfTableEntries; Index++) {\r
60 if (CompareGuid (Guid, &(gDxeCoreST->ConfigurationTable[Index].VendorGuid))) {\r
61 break;\r
62 }\r
63 }\r
64\r
65 if (Index < gDxeCoreST->NumberOfTableEntries) {\r
66 //\r
67 // A match was found, so this is either a modify or a delete operation\r
68 //\r
69 if (Table != NULL) {\r
70 //\r
71 // If Table is not NULL, then this is a modify operation.\r
322d827c 72 // Modify the table entry and return.\r
28a00297 73 //\r
74 gDxeCoreST->ConfigurationTable[Index].VendorTable = Table;\r
75\r
76 //\r
77 // Signal Configuration Table change\r
78 //\r
79 CoreNotifySignalList (Guid);\r
80\r
81 return EFI_SUCCESS;\r
82 }\r
83\r
84 //\r
85 // A match was found and Table is NULL, so this is a delete operation.\r
86 //\r
87 gDxeCoreST->NumberOfTableEntries--;\r
88\r
89 //\r
90 // Copy over deleted entry\r
91 //\r
92 CopyMem (\r
93 &(EfiConfigurationTable[Index]),\r
94 &(gDxeCoreST->ConfigurationTable[Index + 1]),\r
95 (gDxeCoreST->NumberOfTableEntries - Index) * sizeof (EFI_CONFIGURATION_TABLE)\r
96 );\r
97\r
98 } else {\r
99\r
100 //\r
101 // No matching GUIDs were found, so this is an add operation.\r
102 //\r
103\r
104 if (Table == NULL) {\r
105 //\r
106 // If Table is NULL on an add operation, then return an error.\r
107 //\r
108 return EFI_NOT_FOUND;\r
109 }\r
110\r
111 //\r
112 // Assume that Index == gDxeCoreST->NumberOfTableEntries\r
113 //\r
114 if ((Index * sizeof (EFI_CONFIGURATION_TABLE)) >= mSystemTableAllocateSize) {\r
115 //\r
116 // Allocate a table with one additional entry.\r
117 //\r
118 mSystemTableAllocateSize += (CONFIG_TABLE_SIZE_INCREASED * sizeof (EFI_CONFIGURATION_TABLE));\r
9c4ac31c 119 EfiConfigurationTable = AllocateRuntimePool (mSystemTableAllocateSize);\r
28a00297 120 if (EfiConfigurationTable == NULL) {\r
121 //\r
122 // If a new table could not be allocated, then return an error.\r
123 //\r
124 return EFI_OUT_OF_RESOURCES;\r
125 }\r
126\r
127 if (gDxeCoreST->ConfigurationTable != NULL) {\r
128 //\r
129 // Copy the old table to the new table.\r
130 //\r
131 CopyMem (\r
132 EfiConfigurationTable,\r
133 gDxeCoreST->ConfigurationTable,\r
134 Index * sizeof (EFI_CONFIGURATION_TABLE)\r
135 );\r
136\r
137 //\r
322d827c 138 // Record the old table pointer.\r
28a00297 139 //\r
322d827c 140 OldTable = gDxeCoreST->ConfigurationTable;\r
28a00297 141\r
322d827c
SS
142 //\r
143 // As the CoreInstallConfigurationTable() may be re-entered by CoreFreePool()\r
144 // in its calling stack, updating System table to the new table pointer must\r
145 // be done before calling CoreFreePool() to free the old table.\r
146 // It can make sure the gDxeCoreST->ConfigurationTable point to the new table\r
147 // and avoid the errors of use-after-free to the old table by the reenter of\r
148 // CoreInstallConfigurationTable() in CoreFreePool()'s calling stack.\r
149 //\r
150 gDxeCoreST->ConfigurationTable = EfiConfigurationTable;\r
151\r
152 //\r
153 // Free the old table after updating System Table to the new table pointer.\r
154 //\r
155 CoreFreePool (OldTable);\r
156 } else {\r
157 //\r
158 // Update System Table\r
159 //\r
160 gDxeCoreST->ConfigurationTable = EfiConfigurationTable;\r
161 }\r
28a00297 162 }\r
163\r
164 //\r
165 // Fill in the new entry\r
166 //\r
e94a9ff7 167 CopyGuid ((VOID *)&EfiConfigurationTable[Index].VendorGuid, Guid);\r
28a00297 168 EfiConfigurationTable[Index].VendorTable = Table;\r
169\r
170 //\r
171 // This is an add operation, so increment the number of table entries\r
172 //\r
173 gDxeCoreST->NumberOfTableEntries++;\r
174 }\r
175\r
176 //\r
177 // Fix up the CRC-32 in the EFI System Table\r
178 //\r
179 CalculateEfiHdrCrc (&gDxeCoreST->Hdr);\r
180\r
181 //\r
182 // Signal Configuration Table change\r
183 //\r
184 CoreNotifySignalList (Guid);\r
185\r
186 return EFI_SUCCESS;\r
187}\r