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.

100 lines
2.9 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. * 03-07-02 PML Don't re-init if already initialized
  18. *
  19. *******************************************************************************/
  20. #include <internal.h>
  21. #include <windows.h>
  22. /*
  23. * The global security cookie. This name is known to the compiler.
  24. */
  25. extern DWORD_PTR __security_cookie;
  26. /*
  27. * Union to facilitate converting from FILETIME to unsigned __int64
  28. */
  29. typedef union {
  30. unsigned __int64 ft_scalar;
  31. FILETIME ft_struct;
  32. } FT;
  33. /***
  34. *__security_init_cookie(cookie) - init buffer overrun security cookie.
  35. *
  36. *Purpose:
  37. * Initialize the global buffer overrun security cookie which is used by
  38. * the /GS compile switch to detect overwrites to local array variables
  39. * the potentially corrupt the return address. This routine is called
  40. * at EXE/DLL startup.
  41. *
  42. *Entry:
  43. *
  44. *Exit:
  45. *
  46. *Exceptions:
  47. *
  48. *******************************************************************************/
  49. void __cdecl __security_init_cookie(void)
  50. {
  51. DWORD_PTR cookie;
  52. FT systime;
  53. LARGE_INTEGER perfctr;
  54. /*
  55. * Do nothing if the global cookie has already been initialized.
  56. */
  57. if (__security_cookie != 0 && __security_cookie != DEFAULT_SECURITY_COOKIE)
  58. return;
  59. /*
  60. * Initialize the global cookie with an unpredictable value which is
  61. * different for each module in a process. Combine a number of sources
  62. * of randomness.
  63. */
  64. GetSystemTimeAsFileTime(&systime.ft_struct);
  65. #if !defined(_WIN64)
  66. cookie = systime.ft_struct.dwLowDateTime;
  67. cookie ^= systime.ft_struct.dwHighDateTime;
  68. #else
  69. cookie = systime.ft_scalar;
  70. #endif
  71. cookie ^= GetCurrentProcessId();
  72. cookie ^= GetCurrentThreadId();
  73. cookie ^= GetTickCount();
  74. QueryPerformanceCounter(&perfctr);
  75. #if !defined(_WIN64)
  76. cookie ^= perfctr.LowPart;
  77. cookie ^= perfctr.HighPart;
  78. #else
  79. cookie ^= perfctr.QuadPart;
  80. #endif
  81. /*
  82. * Make sure the global cookie is never initialized to zero, since in that
  83. * case an overrun which sets the local cookie and return address to the
  84. * same value would go undetected.
  85. */
  86. __security_cookie = cookie ? cookie : DEFAULT_SECURITY_COOKIE;
  87. }