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.

88 lines
2.5 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. gs_support.c
  5. Abstract:
  6. This module contains the support for the compiler /GS switch
  7. Author:
  8. Bryan Tuttle (bryant) 01-aug-2000
  9. Revision History:
  10. Initial version copied from CRT source. Code must be generic to link into
  11. usermode or kernemode. Limited to calling ntdll/ntoskrnl exports or using
  12. shared memory data.
  13. --*/
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <overflow.h> // TEMPTEMP - remove once all the users are fixed to walk the init list.
  17. #ifdef _WIN64
  18. #define DEFAULT_SECURITY_COOKIE 0x2B992DDFA23249D6
  19. #else
  20. #define DEFAULT_SECURITY_COOKIE 0xBB40E64E
  21. #endif
  22. DECLSPEC_SELECTANY DWORD_PTR __security_cookie = DEFAULT_SECURITY_COOKIE;
  23. void __cdecl __report_gsfailure(void)
  24. {
  25. //
  26. // Don't call DbgPrint by default since it generates a Ctrl-C
  27. // exception as part of outputting to the debugger and we
  28. // can't trust exception handling at this point.
  29. //
  30. // Fake an exception. We can't raise an exception for real since
  31. // the stack (and therefore exception handling) can't be trusted.
  32. //
  33. EXCEPTION_RECORD ExceptionRecord = {0};
  34. CONTEXT ContextRecord = {0};
  35. EXCEPTION_POINTERS ExceptionPointers;
  36. ExceptionRecord.ExceptionCode = STATUS_STACK_BUFFER_OVERRUN;
  37. ExceptionPointers.ExceptionRecord = &ExceptionRecord;
  38. ExceptionPointers.ContextRecord = &ContextRecord;
  39. RtlUnhandledExceptionFilter(&ExceptionPointers);
  40. NtTerminateProcess(NtCurrentProcess(), STATUS_STACK_BUFFER_OVERRUN);
  41. }
  42. void __cdecl __security_init_cookie(void)
  43. {
  44. if (__security_cookie && (__security_cookie != DEFAULT_SECURITY_COOKIE)) {
  45. // Cookie already initialized - just exit
  46. return;
  47. }
  48. //
  49. // Wrap this in a try-except to handle the "built for NT, running on 9x"
  50. // case -- NtGetTickCount touches an address that's valid on NT but
  51. // generates an AV on 9x.
  52. //
  53. __try {
  54. __security_cookie = NtGetTickCount() ^ (DWORD_PTR) &__security_cookie;
  55. } __except(EXCEPTION_EXECUTE_HANDLER) {
  56. __security_cookie = (DWORD_PTR) NtCurrentTeb() ^ (DWORD_PTR) &__security_cookie;
  57. }
  58. if (!__security_cookie) {
  59. // Make sure it's non zero.
  60. __security_cookie = DEFAULT_SECURITY_COOKIE;
  61. }
  62. }
  63. #pragma data_seg(".CRT$XCC")
  64. void (__cdecl *pSecCookieInit)(void) = __security_init_cookie;
  65. #pragma data_seg()