Source code of Windows XP (NT5)
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.

92 lines
2.5 KiB

  1. /***
  2. *seccinit.c - initialize the global buffer overrun security cookie
  3. *
  4. * Copyright (c) 2000-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Define __security_init_cookie, which is called at startup to initialize
  8. * the global buffer overrun security cookie used by the /GS compile flag.
  9. *
  10. * NOTE: The ATLMINCRT library includes a version of this file. If any
  11. * changes are made here, they should be duplicated in the ATL version.
  12. *
  13. *Revision History:
  14. * 01-24-00 PML Created.
  15. * 09-16-00 PML Make sure cookie never initialized to zero (vs7#162619)
  16. * 09-20-00 PML Use a better cookie initialization (vs7#165188)
  17. *
  18. *******************************************************************************/
  19. #include <internal.h>
  20. #include <windows.h>
  21. /*
  22. * The global security cookie. This name is known to the compiler.
  23. */
  24. extern DWORD_PTR __security_cookie;
  25. /*
  26. * Union to facilitate converting from FILETIME to unsigned __int64
  27. */
  28. typedef union {
  29. unsigned __int64 ft_scalar;
  30. FILETIME ft_struct;
  31. } FT;
  32. /***
  33. *__security_init_cookie(cookie) - init buffer overrun security cookie.
  34. *
  35. *Purpose:
  36. * Initialize the global buffer overrun security cookie which is used by
  37. * the /GS compile switch to detect overwrites to local array variables
  38. * the potentially corrupt the return address. This routine is called
  39. * at EXE/DLL startup.
  40. *
  41. *Entry:
  42. *
  43. *Exit:
  44. *
  45. *Exceptions:
  46. *
  47. *******************************************************************************/
  48. void __cdecl __security_init_cookie(void)
  49. {
  50. DWORD_PTR cookie;
  51. FT systime;
  52. LARGE_INTEGER perfctr;
  53. /*
  54. * Initialize the global cookie with an unpredictable value which is
  55. * different for each module in a process. Combine a number of sources
  56. * of randomness.
  57. */
  58. GetSystemTimeAsFileTime(&systime.ft_struct);
  59. #if !defined(_WIN64)
  60. cookie = systime.ft_struct.dwLowDateTime;
  61. cookie ^= systime.ft_struct.dwHighDateTime;
  62. #else
  63. cookie = systime.ft_scalar;
  64. #endif
  65. cookie ^= GetCurrentProcessId();
  66. cookie ^= GetCurrentThreadId();
  67. cookie ^= GetTickCount();
  68. QueryPerformanceCounter(&perfctr);
  69. #if !defined(_WIN64)
  70. cookie ^= perfctr.LowPart;
  71. cookie ^= perfctr.HighPart;
  72. #else
  73. cookie ^= perfctr.QuadPart;
  74. #endif
  75. /*
  76. * Make sure the global cookie is never initialized to zero, since in that
  77. * case an overrun which sets the local cookie and return address to the
  78. * same value would go undetected.
  79. */
  80. __security_cookie = cookie ? cookie : 0xBB40E64E;
  81. }