aboutsummaryrefslogtreecommitdiff
path: root/src/lib/llharden/winprocess_sys.c
blob: f2c88d8c75474f89adba2eb17cc243219b2fa2cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* Copyright (c) 2018-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * \file winprocess_sys.c
 * \brief Subsystem object for windows process setup.
 **/

#include "orconfig.h"
#include "lib/subsys/subsys.h"
#include "lib/llharden/winprocess_sys.h"

#include <stdbool.h>
#include <stddef.h>

#ifdef _WIN32
#include <windows.h>

#define WINPROCESS_SYS_ENABLED true

static int
subsys_winprocess_initialize(void)
{
#ifndef HeapEnableTerminationOnCorruption
#define HeapEnableTerminationOnCorruption 1
#endif

  /* On heap corruption, just give up; don't try to play along. */
  HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);

  /* SetProcessDEPPolicy is only supported on 32-bit Windows.
   * (On 64-bit Windows it always fails, and some compilers don't like the
   * PSETDEP cast.)
   * 32-bit Windows defines _WIN32.
   * 64-bit Windows defines _WIN32 and _WIN64. */
#ifndef _WIN64
  /* Call SetProcessDEPPolicy to permanently enable DEP.
     The function will not resolve on earlier versions of Windows,
     and failure is not dangerous. */
  HMODULE hMod = GetModuleHandleA("Kernel32.dll");
  if (hMod) {
    typedef BOOL (WINAPI *PSETDEP)(DWORD);
    PSETDEP setdeppolicy = (PSETDEP)GetProcAddress(hMod,
                           "SetProcessDEPPolicy");
    if (setdeppolicy) {
      /* PROCESS_DEP_ENABLE | PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION */
      setdeppolicy(3);
    }
  }
#endif /* !defined(_WIN64) */

  return 0;
}
#else /* !defined(_WIN32) */
#define WINPROCESS_SYS_ENABLED false
#define subsys_winprocess_initialize NULL
#endif /* defined(_WIN32) */

const subsys_fns_t sys_winprocess = {
  .name = "winprocess",
  SUBSYS_DECLARE_LOCATION(),
  /* HeapEnableTerminationOnCorruption and setdeppolicy() are security
   * features, we want them to run first. */
  .level = -100,
  .supported = WINPROCESS_SYS_ENABLED,
  .initialize = subsys_winprocess_initialize,
};