summaryrefslogtreecommitdiffstats
path: root/lib/win32-sysdbg.c
blob: 99ce607cb834b10925f135b4ad36aca9aa1f5103 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 *      The PCI Library -- PCI config space access using NT SysDbg interface
 *
 *      Copyright (c) 2022 Pali Rohár <pali@kernel.org>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL v2+.
 *
 *	SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <windows.h>

#include "internal.h"
#include "win32-helpers.h"

#ifndef NTSTATUS
#define NTSTATUS LONG
#endif
#ifndef STATUS_UNSUCCESSFUL
#define STATUS_UNSUCCESSFUL (NTSTATUS)0xC0000001
#endif
#ifndef STATUS_NOT_IMPLEMENTED
#define STATUS_NOT_IMPLEMENTED (NTSTATUS)0xC0000002
#endif
#ifndef STATUS_INVALID_INFO_CLASS
#define STATUS_INVALID_INFO_CLASS (NTSTATUS)0xC0000003
#endif
#ifndef STATUS_ACCESS_DENIED
#define STATUS_ACCESS_DENIED (NTSTATUS)0xC0000022
#endif
#ifndef STATUS_DEBUGGER_INACTIVE
#define STATUS_DEBUGGER_INACTIVE (NTSTATUS)0xC0000354
#endif

#ifndef BUS_DATA_TYPE
#define BUS_DATA_TYPE LONG
#endif
#ifndef PCIConfiguration
#define PCIConfiguration (BUS_DATA_TYPE)4
#endif

#ifndef SYSDBG_COMMAND
#define SYSDBG_COMMAND ULONG
#endif
#ifndef SysDbgReadBusData
#define SysDbgReadBusData (SYSDBG_COMMAND)18
#endif
#ifndef SysDbgWriteBusData
#define SysDbgWriteBusData (SYSDBG_COMMAND)19
#endif

#ifndef SYSDBG_BUS_DATA
typedef struct _SYSDBG_BUS_DATA {
  ULONG Address;
  PVOID Buffer;
  ULONG Request;
  BUS_DATA_TYPE BusDataType;
  ULONG BusNumber;
  ULONG SlotNumber;
} SYSDBG_BUS_DATA, *PSYSDBG_BUS_DATA;
#define SYSDBG_BUS_DATA SYSDBG_BUS_DATA
#endif

#ifndef PCI_SLOT_NUMBER
typedef struct _PCI_SLOT_NUMBER {
  union {
    struct {
      ULONG DeviceNumber:5;
      ULONG FunctionNumber:3;
      ULONG Reserved:24;
    } bits;
    ULONG AsULONG;
  } u;
} PCI_SLOT_NUMBER, *PPCI_SLOT_NUMBER;
#define PCI_SLOT_NUMBER PCI_SLOT_NUMBER
#endif

#ifdef NtSystemDebugControl
#undef NtSystemDebugControl
#endif
static NTSTATUS (NTAPI *MyNtSystemDebugControl)(SYSDBG_COMMAND Command, PVOID InputBuffer, ULONG InputBufferLength, PVOID OutputBuffer, ULONG OutputBufferLength, PULONG ReturnLength);
#define NtSystemDebugControl MyNtSystemDebugControl

static BOOL debug_privilege_enabled;
static LUID luid_debug_privilege;
static BOOL revert_only_privilege;
static HANDLE revert_token;
static HMODULE ntdll;

static int win32_sysdbg_initialized;

static NTSTATUS
win32_sysdbg_pci_bus_data(BOOL WriteBusData, BYTE BusNumber, BYTE DeviceNumber, BYTE FunctionNumber, BYTE Address, PVOID Buffer, BYTE BufferSize, PULONG Length)
{
  SYSDBG_BUS_DATA sysdbg_cmd;
  PCI_SLOT_NUMBER pci_slot;

  if (!NtSystemDebugControl)
    return STATUS_NOT_IMPLEMENTED;

  memset(&pci_slot, 0, sizeof(pci_slot));
  memset(&sysdbg_cmd, 0, sizeof(sysdbg_cmd));

  sysdbg_cmd.Address = Address;
  sysdbg_cmd.Buffer = Buffer;
  sysdbg_cmd.Request = BufferSize;
  sysdbg_cmd.BusDataType = PCIConfiguration;
  sysdbg_cmd.BusNumber = BusNumber;
  pci_slot.u.bits.DeviceNumber = DeviceNumber;
  pci_slot.u.bits.FunctionNumber = FunctionNumber;
  sysdbg_cmd.SlotNumber = pci_slot.u.AsULONG;

  *Length = 0;
  return NtSystemDebugControl(WriteBusData ? SysDbgWriteBusData : SysDbgReadBusData, &sysdbg_cmd, sizeof(sysdbg_cmd), NULL, 0, Length);
}

static int
win32_sysdbg_setup(struct pci_access *a)
{
  UINT prev_error_mode;
  NTSTATUS status;
  ULONG ret_len;
  DWORD id;

  if (win32_sysdbg_initialized)
    return 1;

  prev_error_mode = win32_change_error_mode(SEM_FAILCRITICALERRORS);
  ntdll = LoadLibrary(TEXT("ntdll.dll"));
  win32_change_error_mode(prev_error_mode);
  if (!ntdll)
    {
      a->debug("Cannot open ntdll.dll library.");
      return 0;
    }

  NtSystemDebugControl = (LPVOID)GetProcAddress(ntdll, "NtSystemDebugControl");
  if (!NtSystemDebugControl)
    {
      a->debug("Function NtSystemDebugControl() is not supported.");
      FreeLibrary(ntdll);
      ntdll = NULL;
      return 0;
    }

  /*
   * Try to read PCI id register from PCI device 00:00.0.
   * If this device does not exist and NT SysDbg API is working then
   * NT SysDbg returns STATUS_UNSUCCESSFUL.
   */
  status = win32_sysdbg_pci_bus_data(FALSE, 0, 0, 0, 0, &id, sizeof(id), &ret_len);
  if ((status >= 0 && ret_len == sizeof(id)) || status == STATUS_UNSUCCESSFUL)
    {
      win32_sysdbg_initialized = 1;
      return 1;
    }
  else if (status != STATUS_ACCESS_DENIED)
    {
      if (status == STATUS_NOT_IMPLEMENTED || status == STATUS_INVALID_INFO_CLASS)
        a->debug("NT SysDbg is not supported.");
      else if (status == STATUS_DEBUGGER_INACTIVE)
        a->debug("NT SysDbg is disabled.");
      else
        a->debug("NT SysDbg returned error 0x%lx.", status);
      FreeLibrary(ntdll);
      ntdll = NULL;
      NtSystemDebugControl = NULL;
      return 0;
    }

  a->debug("NT SysDbg returned Access Denied, trying again with Debug privilege...");

  if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid_debug_privilege))
    {
      a->debug("Debug privilege is not supported.");
      FreeLibrary(ntdll);
      ntdll = NULL;
      NtSystemDebugControl = NULL;
      return 0;
    }

  if (!win32_enable_privilege(luid_debug_privilege, &revert_token, &revert_only_privilege))
    {
      a->debug("Cannot enable Debug privilege.");
      FreeLibrary(ntdll);
      ntdll = NULL;
      NtSystemDebugControl = NULL;
      return 0;
    }

  status = win32_sysdbg_pci_bus_data(FALSE, 0, 0, 0, 0, &id, sizeof(id), &ret_len);
  if ((status >= 0 && ret_len == sizeof(id)) || status == STATUS_UNSUCCESSFUL)
    {
      a->debug("Succeeded.");
      debug_privilege_enabled = TRUE;
      win32_sysdbg_initialized = 1;
      return 1;
    }

  win32_revert_privilege(luid_debug_privilege, revert_token, revert_only_privilege);
  revert_token = NULL;
  revert_only_privilege = FALSE;

  FreeLibrary(ntdll);
  ntdll = NULL;
  NtSystemDebugControl = NULL;

  if (status == STATUS_NOT_IMPLEMENTED || status == STATUS_INVALID_INFO_CLASS)
    a->debug("NT SysDbg is not supported.");
  else if (status == STATUS_DEBUGGER_INACTIVE)
    a->debug("NT SysDbg is disabled.");
  else if (status == STATUS_ACCESS_DENIED)
    a->debug("NT SysDbg returned Access Denied.");
  else
    a->debug("NT SysDbg returned error 0x%lx.", status);

  return 0;
}

static int
win32_sysdbg_detect(struct pci_access *a)
{
  if (!win32_sysdbg_setup(a))
    return 0;

  return 1;
}

static void
win32_sysdbg_init(struct pci_access *a)
{
  if (!win32_sysdbg_setup(a))
    {
      a->debug("\n");
      a->error("NT SysDbg PCI Bus Data interface cannot be accessed.");
    }
}

static void
win32_sysdbg_cleanup(struct pci_access *a UNUSED)
{
  if (!win32_sysdbg_initialized)
    return;

  if (debug_privilege_enabled)
    {
      win32_revert_privilege(luid_debug_privilege, revert_token, revert_only_privilege);
      revert_token = NULL;
      revert_only_privilege = FALSE;
      debug_privilege_enabled = FALSE;
    }

  FreeLibrary(ntdll);
  ntdll = NULL;
  NtSystemDebugControl = NULL;

  win32_sysdbg_initialized = 0;
}

static int
win32_sysdbg_read(struct pci_dev *d, int pos, byte *buf, int len)
{
  NTSTATUS status;
  ULONG ret_len;

  if ((unsigned int)d->domain > 0 || (unsigned int)pos > 255 || (unsigned int)(pos+len) > 256)
    return 0;

  status = win32_sysdbg_pci_bus_data(FALSE, d->bus, d->dev, d->func, pos, buf, len, &ret_len);
  if (status < 0 || ret_len != (unsigned int)len)
    return 0;

  return 1;
}

static int
win32_sysdbg_write(struct pci_dev *d, int pos, byte *buf, int len)
{
  NTSTATUS status;
  ULONG ret_len;

  if ((unsigned int)d->domain > 0 || (unsigned int)pos > 255 || (unsigned int)(pos+len) > 256)
    return 0;

  status = win32_sysdbg_pci_bus_data(TRUE, d->bus, d->dev, d->func, pos, buf, len, &ret_len);
  if (status < 0 || ret_len != (unsigned int)len)
    return 0;

  return 1;
}

struct pci_methods pm_win32_sysdbg = {
  "win32-sysdbg",
  "Win32 PCI config space access using NT SysDbg Bus Data interface",
  NULL,					/* config */
  win32_sysdbg_detect,
  win32_sysdbg_init,
  win32_sysdbg_cleanup,
  pci_generic_scan,
  pci_generic_fill_info,
  win32_sysdbg_read,
  win32_sysdbg_write,
  NULL,					/* read_vpd */
  NULL,					/* init_dev */
  NULL					/* cleanup_dev */
};