winprocess_sys.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* Copyright (c) 2018-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file winprocess_sys.c
  5. * \brief Subsystem object for windows process setup.
  6. **/
  7. #include "orconfig.h"
  8. #include "lib/subsys/subsys.h"
  9. #include "lib/process/winprocess_sys.h"
  10. #include <stdbool.h>
  11. #include <stddef.h>
  12. #ifdef _WIN32
  13. #include <windows.h>
  14. #define WINPROCESS_SYS_ENABLED true
  15. static int
  16. subsys_winprocess_initialize(void)
  17. {
  18. #ifndef HeapEnableTerminationOnCorruption
  19. #define HeapEnableTerminationOnCorruption 1
  20. #endif
  21. /* On heap corruption, just give up; don't try to play along. */
  22. HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
  23. /* SetProcessDEPPolicy is only supported on 32-bit Windows.
  24. * (On 64-bit Windows it always fails, and some compilers don't like the
  25. * PSETDEP cast.)
  26. * 32-bit Windows defines _WIN32.
  27. * 64-bit Windows defines _WIN32 and _WIN64. */
  28. #ifndef _WIN64
  29. /* Call SetProcessDEPPolicy to permanently enable DEP.
  30. The function will not resolve on earlier versions of Windows,
  31. and failure is not dangerous. */
  32. HMODULE hMod = GetModuleHandleA("Kernel32.dll");
  33. if (hMod) {
  34. typedef BOOL (WINAPI *PSETDEP)(DWORD);
  35. PSETDEP setdeppolicy = (PSETDEP)GetProcAddress(hMod,
  36. "SetProcessDEPPolicy");
  37. if (setdeppolicy) {
  38. /* PROCESS_DEP_ENABLE | PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION */
  39. setdeppolicy(3);
  40. }
  41. }
  42. #endif /* !defined(_WIN64) */
  43. return 0;
  44. }
  45. #else /* !defined(_WIN32) */
  46. #define WINPROCESS_SYS_ENABLED false
  47. #define subsys_winprocess_initialize NULL
  48. #endif /* defined(_WIN32) */
  49. const subsys_fns_t sys_winprocess = {
  50. .name = "winprocess",
  51. /* HeapEnableTerminationOnCorruption and setdeppolicy() are security
  52. * features, we want them to run first. */
  53. .level = -100,
  54. .supported = WINPROCESS_SYS_ENABLED,
  55. .initialize = subsys_winprocess_initialize,
  56. };