RDFU  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
TableMonitor.c File Reference

Functions for monitoring the state of BootServices and RuntimeServices functions. More...

Include dependency graph for TableMonitor.c:

Go to the source code of this file.

Classes

struct  _func_table
 

Macros

#define write_entry(index, api)   { gEFITables[index].ptr_va = (UINT64)&api; gEFITables[index].last_addr = (UINT64)api; }
 

Typedefs

typedef struct _func_table func_table
 

Functions

EFI_STATUS InitializeEFITables (VOID)
 
BOOLEAN IsFunctionHooked (C_PTR FuncAddr)
 
EFI_STATUS DumpEFITables (BOOLEAN SilentMode)
 
VOID EFIAPI NotifyScannerTimer (IN EFI_EVENT Event, IN VOID *Context)
 
C_PTR EFITableGetAddr (CHAR8 *FuncName)
 
EFI_STATUS InstallEFITableScanner (VOID)
 
EFI_STATUS UnInstallEFITableScanner (VOID)
 

Variables

func_table gEFITables []
 
EFI_EVENT gScannerEvent = 0
 

Detailed Description

Functions for monitoring the state of BootServices and RuntimeServices functions.

Author
Piotr Bania
Date
Feb 2013 Functions for monitoring the state of BootServices and RuntimeServices functions

Definition in file TableMonitor.c.

Macro Definition Documentation

#define write_entry (   index,
  api 
)    { gEFITables[index].ptr_va = (UINT64)&api; gEFITables[index].last_addr = (UINT64)api; }

Referenced by InitializeEFITables().

Typedef Documentation

typedef struct _func_table func_table

Function Documentation

EFI_STATUS DumpEFITables ( BOOLEAN  SilentMode)

Definition at line 208 of file TableMonitor.c.

References FindImageByAddr(), GetAllImagesWithDescription(), _image_entry::ImageBase, _image_entry::ImageSize, IsFunctionHooked(), _func_table::last_addr, LogFileAscii(), LogFullAscii(), _func_table::name, and PRINT_FUNC_ASCII.

Referenced by CommandFunction(), DrawBoxAndMenu(), and NotifyScannerTimer().

209 {
210  int i, change;
211  CHAR8 *p;
212 
213  C_PTR ImageBase;
214  UINT64 ImageSize;
215  C_PTR TempAddr;
216 
217  ULONG32 HookDest;
218  BOOLEAN WasHooked;
219  image_entry *HookDestImageEntry;
220 
221  image_entry_list *ImageList;
222  image_entry *ImageEntry;
223 
224 
225  if (!SilentMode)
226  LogFullAscii(": Dumping EFI Functions tables...\r\n");
227 
228 
229  ImageList = GetAllImagesWithDescription(NULL, FALSE);
230  if (!ImageList)
231  {
232  LogFullAscii("%a: Error executing GetAllImagesWithDescription failed\r\n", __FUNCTION__);
233  return EFI_NOT_FOUND;
234  }
235 
236 
237 
238  for (i = 0; gEFITables[i].name[0] != '?'; i++)
239  {
240  ImageBase = 0;
241  ImageSize = 0;
242  change = 0;
243 
244  TempAddr = *(C_PTR*)gEFITables[i].ptr_va;
245  p = (CHAR8*)&gEFITables[i].name;
246 
247  ImageEntry = FindImageByAddr(ImageList, TempAddr);
248 
249  if (ImageEntry)
250  {
251  // addr in in range of image
252  ImageBase = (C_PTR)ImageEntry->ImageBase;
253  ImageSize = ImageEntry->ImageSize;
254  }
255 
256  // check if there was a change in the values
257  if (TempAddr != gEFITables[i].last_addr)
258  {
259  change = 1;
260 
261  LogFileAscii("[%04d] Modification detected OldAddr=%016lx NewAddr=%016lx Function Name: %a\r\n",
262  i,
263  (MAX_PTR)gEFITables[i].last_addr,
264  (MAX_PTR)TempAddr,
265  p);
266 
267 
268  if (!SilentMode)
269  {
270  PRINT_FUNC_ASCII("[%04d] Modification detected OldAddr=%016lx NewAddr=%016lx Function Name: %a\r\n",
271  i,
272  (MAX_PTR)gEFITables[i].last_addr,
273  (MAX_PTR)TempAddr,
274  p);
275  }
276 
277  }
278 
279  if (!SilentMode)
280  {
281  WasHooked = FALSE;
282 
283  // on mac mini QueryCapsuleCapabilities/CreateEventEx is invalid therefore probing it
284  // will cause an access violation
285  // lets assume on x64 that every address above 4gb is not valid
286 
287 #if defined(_WIN64)
288  if ((ULONG32)(gEFITables[i].last_addr >> 32))
289  LogFullAscii("Addr: %016lx is probably invalid!\r\n", gEFITables[i].last_addr);
290  else
291  WasHooked = IsFunctionHooked(gEFITables[i].last_addr);
292 #else
293  WasHooked = IsFunctionHooked(gEFITables[i].last_addr);
294 #endif
295 
296  LogFullAscii("[%04d] Addr: %016lx M: %d InImage: %016lx-%016lx Function Name: %a \r\n",
297  i,
298  (MAX_PTR)TempAddr,
299  change,
300  (MAX_PTR)ImageBase, (
301  MAX_PTR)(ImageBase + ImageSize),
302  p);
303 
304  if (WasHooked)
305  {
306  // at this point we only know that the function prologue contains
307  // a long rel jump or a rel call
308  // so lets check the destination of it as well
309 
310  HookDest = (ULONG32)(*(INT32*)(gEFITables[i].last_addr + 1) + gEFITables[i].last_addr + 5);
311  HookDestImageEntry = FindImageByAddr(ImageList, HookDest);
312 
313  // function is hooked when HookDestImageEntry = 0 or it points to
314  // an image different from the current image
315  if (!HookDestImageEntry)
316  LogFullAscii("Function %016lx is hooked to %016lx (not in image)\r\n",
317  (MAX_PTR)gEFITables[i].last_addr,
318  (MAX_PTR)HookDest);
319  else if (HookDestImageEntry != ImageEntry)
320  LogFullAscii("Function %016lx is hooked to %016lx (image base=%016lx)\r\n",
321  (MAX_PTR)gEFITables[i].last_addr,
322  (MAX_PTR)HookDest,
323  (MAX_PTR)HookDestImageEntry->ImageBase);
324  }
325  }
326  else if (change == 1)
327  {
328  // we are now in timmer so we log only this modified entry
329 
330  LogFileAscii("[%04d] Addr: %016lx M: %d InImage: %016lx-%016lx Function Name: %a \r\n",
331  i,
332  (MAX_PTR)TempAddr,
333  change,
334  (MAX_PTR)ImageBase,
335  (MAX_PTR)(ImageBase + ImageSize),
336  p);
337 
338 
339  // update the last addr, because otherwise the log will be flooded
340  // with the same information (because of the timer)
341 
342  gEFITables[i].last_addr = TempAddr;
343 
344  }
345 
346  }
347 
348  if (!SilentMode)
349  LogFullAscii(": Dumping EFI Functions tables done.\r\n");
350 
351  return EFI_SUCCESS;
352 }

Here is the call graph for this function:

Here is the caller graph for this function:

C_PTR EFITableGetAddr ( CHAR8 *  FuncName)

Function gets addr of selected function (search is done by function name case sensitive);

Return values
AddrOn success
NULLOn error

Definition at line 398 of file TableMonitor.c.

References _func_table::last_addr, and _func_table::name.

Referenced by CommandFunction().

399 {
400  UINTN i;
401 
402  if (!FuncName)
403  return 0;
404 
405  for (i = 0; gEFITables[i].name[0] != '?'; i++)
406  {
407  if (strcmp(gEFITables[i].name, FuncName) == 0)
408  return gEFITables[i].last_addr;
409  }
410 
411  return 0;
412 }

Here is the caller graph for this function:

EFI_STATUS InitializeEFITables ( VOID  )

Function initializes wanted offsets and values to the EFITables.

Definition at line 103 of file TableMonitor.c.

References write_entry.

Referenced by CommandFunction(), and DrawBoxAndMenu().

104 {
105 #define write_entry(index, api) { gEFITables[index].ptr_va = (UINT64)&api; gEFITables[index].last_addr = (UINT64)api; }
106 
107 
108  write_entry(0, gRT->GetTime);
109  write_entry(1, gRT->SetTime);
110  write_entry(2, gRT->GetWakeupTime);
111  write_entry(3, gRT->SetWakeupTime);
112  write_entry(4, gRT->SetVirtualAddressMap);
113  write_entry(5, gRT->ConvertPointer);
114  write_entry(6, gRT->GetVariable);
115  write_entry(7, gRT->GetNextVariableName);
116  write_entry(8, gRT->SetVariable);
117  write_entry(9, gRT->GetNextHighMonotonicCount);
118  write_entry(10, gRT->ResetSystem);
119  write_entry(11, gRT->UpdateCapsule);
120  write_entry(12, gRT->QueryCapsuleCapabilities);
121  write_entry(13, gRT->QueryVariableInfo);
122 
123 
124  // boot time services
125  write_entry(14, gBS->RaiseTPL);
126  write_entry(15, gBS->RestoreTPL);
127  write_entry(16, gBS->AllocatePages);
128  write_entry(17, gBS->FreePages);
129  write_entry(18, gBS->GetMemoryMap);
130  write_entry(19, gBS->AllocatePool);
131  write_entry(20, gBS->FreePool);
132  write_entry(21, gBS->CreateEvent);
133  write_entry(22, gBS->SetTimer);
134  write_entry(23, gBS->WaitForEvent);
135  write_entry(24, gBS->SignalEvent);
136  write_entry(25, gBS->CloseEvent);
137  write_entry(26, gBS->CheckEvent);
138  write_entry(27, gBS->InstallProtocolInterface);
139  write_entry(28, gBS->ReinstallProtocolInterface);
140  write_entry(29, gBS->UninstallProtocolInterface);
141  write_entry(30, gBS->HandleProtocol);
142  write_entry(31, gBS->RegisterProtocolNotify);
143  write_entry(32, gBS->LocateHandle);
144  write_entry(33, gBS->LocateDevicePath);
145  write_entry(34, gBS->InstallConfigurationTable);
146  write_entry(35, gBS->LoadImage);
147  write_entry(36, gBS->StartImage);
148  write_entry(37, gBS->Exit);
149  write_entry(38, gBS->UnloadImage);
150  write_entry(39, gBS->ExitBootServices);
151  write_entry(40, gBS->GetNextMonotonicCount);
152  write_entry(41, gBS->Stall);
153  write_entry(42, gBS->SetWatchdogTimer);
154  write_entry(43, gBS->ConnectController);
155  write_entry(44, gBS->DisconnectController);
156  write_entry(45, gBS->OpenProtocol);
157  write_entry(46, gBS->CloseProtocol);
158  write_entry(47, gBS->OpenProtocolInformation);
159  write_entry(48, gBS->ProtocolsPerHandle);
160  write_entry(49, gBS->LocateHandleBuffer);
161  write_entry(50, gBS->LocateProtocol);
162  write_entry(51, gBS->InstallMultipleProtocolInterfaces);
163  write_entry(52, gBS->UninstallMultipleProtocolInterfaces);
164  write_entry(53, gBS->CalculateCrc32);
165  write_entry(54, gBS->CopyMem);
166  write_entry(55, gBS->SetMem);
167  write_entry(56, gBS->CreateEventEx);
168 
169 
170  // EFI_SIMPLE_TEXT_INPUT_PROTOCOL *ConIn;
171  write_entry(57, gST->ConIn->ReadKeyStroke);
172 
173 
174 #undef write_entry
175 
176  return EFI_SUCCESS;
177 }

Here is the caller graph for this function:

EFI_STATUS InstallEFITableScanner ( VOID  )

Function installs EFI timer which checks runtime/boottime functions periodically - it also logs every change to file.

Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 426 of file TableMonitor.c.

References EFI_STATUS(), EFI_TPL_NOTIFY, gScannerEvent, LogFullAscii(), NotifyScannerTimer(), and SAMPLE_INTERVAL.

Referenced by DrawBoxAndMenu().

427 {
428  EFI_STATUS Status;
429 
430  LogFullAscii(": Installing the EFI table scanner\r\n");
431 
432  Status = gBS->CreateEvent(EVT_NOTIFY_SIGNAL | EVT_TIMER,
435  NULL,
436  &gScannerEvent);
437  if (EFI_ERROR(Status))
438  {
439  LogFullAscii("%a: Unable to install the table scanner, status = %08x\r\n",
440  __FUNCTION__, Status);
441  return Status;
442  }
443 
444 
445  Status = gBS->SetTimer(gScannerEvent,
446  TimerPeriodic,
447  SAMPLE_INTERVAL);
448 
449 
450  if (EFI_ERROR(Status))
451  {
452  gBS->CloseEvent(gScannerEvent);
453 
454  LogFullAscii("%a: Unable to install the timer for table scanner, status = %08x\r\n",
455  __FUNCTION__, Status);
456 
457  return Status;
458  }
459 
460  return EFI_SUCCESS;
461 }

Here is the call graph for this function:

Here is the caller graph for this function:

BOOLEAN IsFunctionHooked ( C_PTR  FuncAddr)

Definition at line 184 of file TableMonitor.c.

Referenced by DumpEFITables().

185 {
186  UCHAR *p = (UCHAR*)FuncAddr;
187  if ((p[0] == 0xE9) || (p[0] == 0xE8))
188  return TRUE;
189 
190  return FALSE;
191 }

Here is the caller graph for this function:

VOID EFIAPI NotifyScannerTimer ( IN EFI_EVENT  Event,
IN VOID *  Context 
)

The timer notify function Functions checks whether any of the function pointers have changed.

Definition at line 360 of file TableMonitor.c.

References DumpEFITables(), and _func_table::name.

Referenced by InstallEFITableScanner().

364 {
365  int i;
366  C_PTR TempAddr;
367 
368  // if we spot a change then we call the DumpEFITables
369  // otherwise it is too slow
370 
371  for (i = 0; gEFITables[i].name[0] != '?'; i++)
372  {
373  TempAddr = *(C_PTR*)gEFITables[i].ptr_va;
374  if (TempAddr != gEFITables[i].last_addr)
375  {
376 
377  DumpEFITables(TRUE);
378  return;
379  }
380  }
381 }

Here is the call graph for this function:

Here is the caller graph for this function:

EFI_STATUS UnInstallEFITableScanner ( VOID  )

function uninstalls the scanner event

Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 471 of file TableMonitor.c.

References gScannerEvent, and LogFullAscii().

472 {
473  LogFullAscii(": UnInstalling the EFI table scanner\r\n");
474 
475  if (gScannerEvent)
476  gBS->CloseEvent(gScannerEvent);
477 
478  return EFI_SUCCESS;
479 }

Here is the call graph for this function:

Variable Documentation

func_table gEFITables[]

table with imporant EFI functions. runtime services + boot time services + ReadKey

Definition at line 27 of file TableMonitor.c.

EFI_EVENT gScannerEvent = 0

Definition at line 385 of file TableMonitor.c.

Referenced by InstallEFITableScanner(), and UnInstallEFITableScanner().