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.

134 lines
4.3 KiB

  1. /***
  2. *seccook.c - checks buffer overrun security cookie
  3. *
  4. * Copyright (c) 2000-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Defines compiler helper __security_check_cookie, used by the /GS
  8. * compile switch to detect local buffer variable overrun bugs/attacks.
  9. *
  10. * When compiling /GS, the compiler injects code to detect when a local
  11. * array variable has been overwritten, potentially overwriting the
  12. * return address (on machines like x86 where the return address is on
  13. * the stack). A local variable is allocated directly before the return
  14. * address and initialized on entering the function. When exiting the
  15. * function, the compiler inserts code to verify that the local variable
  16. * has not been modified. If it has, then an error reporting routine
  17. * is called.
  18. *
  19. * NOTE: The ATLMINCRT library includes a version of this file. If any
  20. * changes are made here, they should be duplicated in the ATL version.
  21. *
  22. *Revision History:
  23. * 01-24-00 PML Created (as seccook.c)
  24. * 08-09-00 PML Preserve EAX on non-failure case (VS7#147203). Also
  25. * make sure failure case never returns.
  26. * 08-29-00 PML Rename handlers, add extra parameters. Move most of
  27. * system CRT version over to seclocf.c.
  28. * 09-16-00 PML Initialize global cookie earlier, and give it a nonzero
  29. * static initialization (vs7#162619).
  30. * 03-07-02 PML Split out of seccook.c
  31. *
  32. *******************************************************************************/
  33. #include <sect_attribs.h>
  34. #include <internal.h>
  35. #include <windows.h>
  36. #include <stdlib.h>
  37. /*
  38. * The global security cookie. This name is known to the compiler.
  39. */
  40. extern DWORD_PTR __security_cookie;
  41. /*
  42. * Trigger initialization of the global security cookie on program startup.
  43. * Force initialization before any #pragma init_seg() inits by using .CRT$XCAA
  44. * as the startup funcptr section.
  45. */
  46. #pragma data_seg(".CRT$XCAA")
  47. extern void __cdecl __security_init_cookie(void);
  48. static _CRTALLOC(".CRT$XCAA") _PVFV init_cookie = __security_init_cookie;
  49. #pragma data_seg()
  50. static void __cdecl report_failure(void);
  51. #if !defined(_SYSCRT) || !defined(CRTDLL)
  52. /*
  53. * The routine called if a cookie check fails.
  54. */
  55. #define REPORT_ERROR_HANDLER __security_error_handler
  56. #else
  57. /*
  58. * When using an older system CRT, use a local cookie failure reporting
  59. * routine, with a default implementation that calls __security_error_handler
  60. * if available, otherwise displays a default message box.
  61. */
  62. #define REPORT_ERROR_HANDLER __local_security_error_handler
  63. #endif
  64. extern void __cdecl REPORT_ERROR_HANDLER(int, void *);
  65. /***
  66. *__security_check_cookie(cookie) - check for buffer overrun
  67. *
  68. *Purpose:
  69. * Compiler helper. Check if a local copy of the security cookie still
  70. * matches the global value. If not, then report the fatal error.
  71. *
  72. * The actual reporting is split out into static helper report_failure,
  73. * since the cookie check routine must be minimal code that preserves
  74. * any registers used in returning the callee's result.
  75. *
  76. *Entry:
  77. * DWORD_PTR cookie - local security cookie to check
  78. *
  79. *Exit:
  80. * Returns immediately if the local cookie matches the global version.
  81. * Otherwise, calls the failure reporting handler and exits.
  82. *
  83. *Exceptions:
  84. *
  85. *******************************************************************************/
  86. #ifndef _M_IX86
  87. void __fastcall __security_check_cookie(DWORD_PTR cookie)
  88. {
  89. /* Immediately return if the local cookie is OK. */
  90. if (cookie == __security_cookie)
  91. return;
  92. /* Report the failure */
  93. report_failure();
  94. }
  95. #else
  96. void __declspec(naked) __fastcall __security_check_cookie(DWORD_PTR cookie)
  97. {
  98. /* x86 version written in asm to preserve all regs */
  99. __asm {
  100. cmp ecx, __security_cookie
  101. jne failure
  102. ret
  103. failure:
  104. jmp report_failure
  105. }
  106. }
  107. #endif
  108. static void __cdecl report_failure(void)
  109. {
  110. /* Report the failure */
  111. __try {
  112. REPORT_ERROR_HANDLER(_SECERR_BUFFER_OVERRUN, NULL);
  113. }
  114. __except (EXCEPTION_EXECUTE_HANDLER) {
  115. /* nothing */
  116. }
  117. ExitProcess(3);
  118. }