Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.3 KiB

  1. /*++
  2. Copyright (c) 2000-2002 Microsoft Corporation
  3. Module Name:
  4. SierraCartRacing.cpp
  5. Abstract:
  6. Sierra Cart Racing passes a bad pointer to InitializeSecurityDescriptor which overwrites
  7. part of the SECURITY_ATTRIBUTES structure and some other stack memory.
  8. The original version of this shim would fail the call to InitializeSecurityDescriptor,
  9. and force a NULL security descriptor to CreateSemaphoreA. To reduce the security risk,
  10. the shim was modified to only pass a NULL security descriptor to CreateSemaphoreA if
  11. it detects that the LPSECURITY_ATTRIBUTES was overwritten by InitializeSecurityDescriptor,
  12. and restores the memory overwritten by InitializeSecurityDescriptor.
  13. Notes:
  14. This is an app specific shim.
  15. History:
  16. 11/03/1999 linstev Created
  17. 03/15/2002 robkenny Re-created to pass security muster.
  18. --*/
  19. #include "precomp.h"
  20. IMPLEMENT_SHIM_BEGIN(SierraCartRacing)
  21. #include "ShimHookMacro.h"
  22. APIHOOK_ENUM_BEGIN
  23. APIHOOK_ENUM_ENTRY(CreateSemaphoreA)
  24. APIHOOK_ENUM_ENTRY(InitializeSecurityDescriptor)
  25. APIHOOK_ENUM_END
  26. BOOL g_BLastSecurityDescriptorSet = FALSE;
  27. SECURITY_DESCRIPTOR g_LastSecurityDescriptor;
  28. /*++
  29. Use the default security descriptor.
  30. --*/
  31. HANDLE
  32. APIHOOK(CreateSemaphoreA)(
  33. LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
  34. LONG lInitialCount,
  35. LONG lMaximumCount,
  36. LPCSTR lpName
  37. )
  38. {
  39. if (lpSemaphoreAttributes && g_BLastSecurityDescriptorSet)
  40. {
  41. // Initialize a security descriptor
  42. SECURITY_DESCRIPTOR securityDescriptor;
  43. InitializeSecurityDescriptor(&securityDescriptor, SECURITY_DESCRIPTOR_REVISION);
  44. // Check to see if them memory starting at lpSemaphoreAttributes->lpSecurityDescriptor
  45. // contains the same memory as a security descriptor.
  46. int compareResult = memcmp(&securityDescriptor,
  47. &lpSemaphoreAttributes->lpSecurityDescriptor,
  48. sizeof(securityDescriptor));
  49. if (compareResult == 0)
  50. {
  51. // Restore the overwritten memory
  52. memcpy(&lpSemaphoreAttributes->lpSecurityDescriptor, &g_LastSecurityDescriptor, sizeof(g_LastSecurityDescriptor));
  53. // lpSemaphoreAttributes is bogus
  54. lpSemaphoreAttributes = NULL;
  55. }
  56. }
  57. return ORIGINAL_API(CreateSemaphoreA)(
  58. lpSemaphoreAttributes,
  59. lInitialCount,
  60. lMaximumCount,
  61. lpName);
  62. }
  63. /*++
  64. Returns false for InitializeSecurityDescriptor. i.e. do nothing so we don't
  65. touch the stack.
  66. --*/
  67. BOOL
  68. APIHOOK(InitializeSecurityDescriptor)(
  69. PSECURITY_DESCRIPTOR pSecurityDescriptor,
  70. DWORD dwRevision
  71. )
  72. {
  73. // Save the memory that will be overwritten.
  74. if (pSecurityDescriptor)
  75. {
  76. g_BLastSecurityDescriptorSet = TRUE;
  77. memcpy(&g_LastSecurityDescriptor, pSecurityDescriptor, sizeof(g_LastSecurityDescriptor));
  78. }
  79. return ORIGINAL_API(InitializeSecurityDescriptor)(pSecurityDescriptor, dwRevision);
  80. }
  81. /*++
  82. Register hooked functions
  83. --*/
  84. HOOK_BEGIN
  85. APIHOOK_ENTRY(KERNEL32.DLL, CreateSemaphoreA)
  86. APIHOOK_ENTRY(ADVAPI32.DLL, InitializeSecurityDescriptor)
  87. HOOK_END
  88. IMPLEMENT_SHIM_END