ReactOS  0.4.13-dev-52-g0efcfec
fltsupport.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS kernel-mode tests
3  * LICENSE: GPLv2+ - See COPYING in the top level directory
4  * PURPOSE: File system mini-filter support routines
5  * PROGRAMMER: Ged Murphy <gedmurphy@reactos.org>
6  */
7 
8 #include <kmt_test.h>
9 
10 #define KMT_FLT_USER_MODE
11 #include "kmtest.h"
12 #include <kmt_public.h>
13 
14 #include <assert.h>
15 #include <debug.h>
16 
17 /*
18  * We need to call the internal function in the service.c file
19  */
20 DWORD
23  IN PCWSTR ServicePath,
24  IN PCWSTR DisplayName OPTIONAL,
25  IN DWORD ServiceType,
26  OUT SC_HANDLE *ServiceHandle);
27 
29  _In_z_ LPWSTR lpPrivName,
30  _In_ BOOL bEnable);
31 
32 // move to a shared location
33 typedef struct _KMTFLT_MESSAGE_HEADER
34 {
38 
40 
41 extern HANDLE KmtestHandle;
43 
44 
45 
62 DWORD
65  _In_z_ PCWSTR DisplayName,
66  _Out_ SC_HANDLE *ServiceHandle)
67 {
68  WCHAR ServicePath[MAX_PATH];
69 
70  StringCbCopy(ServicePath, sizeof ServicePath, ServiceName);
71  StringCbCat(ServicePath, sizeof ServicePath, L"_drv.sys");
72 
73  StringCbCopy(TestServiceName, sizeof TestServiceName, L"Kmtest-");
75 
77  ServicePath,
78  DisplayName,
80  ServiceHandle);
81 }
82 
96 DWORD
99  _Inout_ SC_HANDLE *ServiceHandle)
100 {
101  return KmtDeleteService(ServiceName, ServiceHandle);
102 }
103 
111 DWORD
113  _In_ BOOLEAN EnableDriverLoadPrivlege,
114  _In_ BOOLEAN RestartIfRunning,
115  _In_ BOOLEAN ConnectComms,
116  _Out_ HANDLE *hPort
117 )
118 {
119  DWORD Error;
120 
121  if (EnableDriverLoadPrivlege)
122  {
124  if (Error)
125  {
126  return Error;
127  }
128  }
129 
131  if ((Error == ERROR_SERVICE_ALREADY_RUNNING) && RestartIfRunning)
132  {
134  if (Error)
135  {
136  // TODO
137  __debugbreak();
138  }
139 
141  }
142 
143  if (Error)
144  {
145  return Error;
146  }
147 
148  if (ConnectComms)
149  {
150  Error = KmtFltConnectComms(hPort);
151  }
152 
153  return Error;
154 }
155 
168 DWORD
170  _In_ HANDLE *hPort,
171  _In_ BOOLEAN DisonnectComms)
172 {
174 
175  if (DisonnectComms)
176  {
177  Error = KmtFltDisconnect(hPort);
178 
179  if (Error)
180  {
181  return Error;
182  }
183  }
184 
186 
187  if (Error)
188  {
189  // TODO
190  __debugbreak();
191  }
192 
193  return Error;
194 }
195 
206 DWORD
208  _Out_ HANDLE *hPort)
209 {
210  return KmtFltConnect(TestServiceName, hPort);
211 }
212 
223 DWORD
225  _In_ HANDLE hPort)
226 {
227  return KmtFltDisconnect(hPort);
228 }
229 
230 
243  _Inout_ SC_HANDLE *ServiceHandle)
244 {
245  return KmtCloseService(ServiceHandle);
246 }
247 
260 DWORD
262  _In_ HANDLE hPort,
263  _In_z_ PCSTR TestName)
264 {
265  return KmtFltSendStringToDriver(hPort, KMTFLT_RUN_TEST, TestName);
266 }
267 
280 DWORD
282  _In_ HANDLE hPort,
284 {
285  assert(hPort);
286  return KmtFltSendBufferToDriver(hPort, Message, NULL, 0, NULL, 0, NULL);
287 }
288 
304 DWORD
306  _In_ HANDLE hPort,
308  _In_ PCSTR String)
309 {
310  assert(hPort);
311  assert(String);
313 }
314 
329 DWORD
331  _In_ HANDLE hPort,
334 {
335  return KmtFltSendBufferToDriver(hPort, Message, (PVOID)String, (DWORD)wcslen(String) * sizeof(WCHAR), NULL, 0, NULL);
336 }
337 
352 DWORD
354  _In_ HANDLE hPort,
356  _In_ DWORD Value)
357 {
358  return KmtFltSendBufferToDriver(hPort, Message, &Value, sizeof(Value), NULL, 0, NULL);
359 }
360 
383 DWORD
385  _In_ HANDLE hPort,
392 {
397  DWORD Error;
398 
399  assert(hPort);
400 
401  if (BufferSize)
402  {
403  assert(InBuffer);
404 
407  if (!Ptr)
408  {
410  }
411  FreeMemory = TRUE;
412  }
413  else
414  {
416  Ptr = &Header;
417  }
418 
419  Ptr->Message = Message;
420  if (BufferSize)
421  {
422  Ptr->Buffer = (Ptr + 1);
423  StringCbCopy(Ptr->Buffer, BufferSize, InBuffer);
424  Ptr->BufferSize = BufferSize;
425  }
426 
428 
429  if (FreeMemory)
430  {
431  HeapFree(GetProcessHeap(), 0, Ptr);
432  }
433 
434  return Error;
435 }
436 
447 DWORD
450 {
451  WCHAR DefaultInstance[128];
452  WCHAR KeyPath[256];
453  HKEY hKey = NULL;
454  HKEY hSubKey = NULL;
455  DWORD Zero = 0;
456  LONG Error;
457 
458  StringCbCopy(KeyPath, sizeof KeyPath, L"SYSTEM\\CurrentControlSet\\Services\\");
459  StringCbCat(KeyPath, sizeof KeyPath, TestServiceName);
460  StringCbCat(KeyPath, sizeof KeyPath, L"\\Instances\\");
461 
463  KeyPath,
464  0,
465  NULL,
468  NULL,
469  &hKey,
470  NULL);
471  if (Error != ERROR_SUCCESS)
472  {
473  return Error;
474  }
475 
476  StringCbCopy(DefaultInstance, sizeof DefaultInstance, TestServiceName);
477  StringCbCat(DefaultInstance, sizeof DefaultInstance, L" Instance");
478 
479  Error = RegSetValueExW(hKey,
480  L"DefaultInstance",
481  0,
482  REG_SZ,
483  (LPBYTE)DefaultInstance,
484  (wcslen(DefaultInstance) + 1) * sizeof(WCHAR));
485  if (Error != ERROR_SUCCESS)
486  {
487  goto Quit;
488  }
489 
490  Error = RegCreateKeyW(hKey, DefaultInstance, &hSubKey);
491  if (Error != ERROR_SUCCESS)
492  {
493  goto Quit;
494  }
495 
496  Error = RegSetValueExW(hSubKey,
497  L"Altitude",
498  0,
499  REG_SZ,
500  (LPBYTE)Altitude,
501  (wcslen(Altitude) + 1) * sizeof(WCHAR));
502  if (Error != ERROR_SUCCESS)
503  {
504  goto Quit;
505  }
506 
507  Error = RegSetValueExW(hSubKey,
508  L"Flags",
509  0,
510  REG_DWORD,
511  (LPBYTE)&Zero,
512  sizeof(DWORD));
513 
514 Quit:
515  if (hSubKey)
516  {
517  RegCloseKey(hSubKey);
518  }
519  if (hKey)
520  {
521  RegCloseKey(hKey);
522  }
523 
524  return Error;
525 
526 }
527 
528 /*
529 * Private functions, not meant for use in kmtests
530 */
531 
533  _In_ HANDLE hToken,
534  _In_z_ LPWSTR lpPrivName,
535  _In_ BOOL bEnable)
536 {
538  LUID luid;
539  BOOL bSuccess;
540  DWORD dwError = ERROR_SUCCESS;
541 
542  /* Get the luid for this privilege */
543  if (!LookupPrivilegeValueW(NULL, lpPrivName, &luid))
544  return GetLastError();
545 
546  /* Setup the struct with the priv info */
547  TokenPrivileges.PrivilegeCount = 1;
548  TokenPrivileges.Privileges[0].Luid = luid;
549  TokenPrivileges.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0;
550 
551  /* Enable the privilege info in the token */
553  FALSE,
555  sizeof(TOKEN_PRIVILEGES),
556  NULL,
557  NULL);
558  if (bSuccess == FALSE) dwError = GetLastError();
559 
560  /* return status */
561  return dwError;
562 }
563 
565  _In_z_ LPWSTR lpPrivName,
566  _In_ BOOL bEnable)
567 {
568  HANDLE hToken;
569  BOOL bSuccess;
570  DWORD dwError = ERROR_SUCCESS;
571 
572  /* Get a handle to our token */
575  &hToken);
576  if (bSuccess == FALSE) return GetLastError();
577 
578  /* Enable the privilege in the agent token */
579  dwError = EnablePrivilege(hToken, lpPrivName, bEnable);
580 
581  /* We're done with this now */
582  CloseHandle(hToken);
583 
584  /* return status */
585  return dwError;
586 }
_In_opt_ ULONG _Out_ PULONG Value
Definition: rtlfuncs.h:2327
const uint16_t * PCWSTR
Definition: typedefs.h:55
#define IN
Definition: typedefs.h:38
static VOID FreeMemory(PCREATE_DATA Data)
Definition: create.c:134
#define _In_opt_z_
Definition: no_sal2.h:221
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:398
BOOL WINAPI LookupPrivilegeValueW(LPCWSTR lpSystemName, LPCWSTR lpPrivilegeName, PLUID lpLuid)
Definition: misc.c:782
DWORD KmtFltRunKernelTest(_In_ HANDLE hPort, _In_z_ PCSTR TestName)
Definition: fltsupport.c:261
#define ERROR_SUCCESS
Definition: deptool.c:10
DWORD KmtFltSendMessage(_In_ HANDLE hPort, _In_reads_bytes_(dwInBufferSize) LPVOID InBuffer, _In_ DWORD InBufferSize, _Out_writes_bytes_to_opt_(dutBufferSize, *BytesReturned) LPVOID OutBuffer, _In_ DWORD OutBufferSize, _Out_opt_ LPDWORD BytesReturned)
Definition: filter.c:187
#define KEY_SET_VALUE
Definition: nt_native.h:1017
#define _Out_writes_bytes_to_opt_(size, count)
Definition: no_sal2.h:375
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
DWORD KmtFltUnloadDriver(_In_ HANDLE *hPort, _In_ BOOLEAN DisonnectComms)
Definition: fltsupport.c:169
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
_In_ PIRP _In_ ULONG _In_ ULONG _In_ ULONG _In_ ULONG OutBufferSize
Definition: classpnp.h:429
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define assert(x)
Definition: debug.h:53
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
struct _KMTFLT_MESSAGE_HEADER * PKMTFLT_MESSAGE_HEADER
static WCHAR String[]
Definition: stringtable.c:55
struct _KMTFLT_MESSAGE_HEADER KMTFLT_MESSAGE_HEADER
DWORD KmtCloseService(IN OUT SC_HANDLE *ServiceHandle)
Definition: service.c:392
void __cdecl __debugbreak(void)
Definition: intrin_ppc.h:698
static BOOLEAN bSuccess
Definition: drive.cpp:417
DWORD KmtFltCreateService(_In_z_ PCWSTR ServiceName, _In_z_ PCWSTR DisplayName, _Out_ SC_HANDLE *ServiceHandle)
Definition: fltsupport.c:63
DWORD KmtpCreateService(IN PCWSTR ServiceName, IN PCWSTR ServicePath, IN PCWSTR DisplayName OPTIONAL, IN DWORD ServiceType, OUT SC_HANDLE *ServiceHandle)
Definition: service.c:414
#define SE_PRIVILEGE_ENABLED
Definition: setypes.h:63
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned char * LPBYTE
Definition: typedefs.h:52
Definition: Header.h:8
DWORD KmtFltDeleteService(_In_opt_z_ PCWSTR ServiceName, _Inout_ SC_HANDLE *ServiceHandle)
Definition: fltsupport.c:97
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
DWORD EnablePrivilege(_In_ HANDLE hToken, _In_z_ LPWSTR lpPrivName, _In_ BOOL bEnable)
Definition: fltsupport.c:532
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
DWORD KmtFltSendBufferToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_reads_bytes_(BufferSize) LPVOID InBuffer, _In_ DWORD BufferSize, _Out_writes_bytes_to_opt_(OutBufferSize, *BytesReturned) LPVOID OutBuffer, _In_ DWORD OutBufferSize, _Out_opt_ LPDWORD BytesReturned)
Definition: fltsupport.c:384
DWORD EnablePrivilegeInCurrentProcess(_In_z_ LPWSTR lpPrivName, _In_ BOOL bEnable)
Definition: fltsupport.c:564
#define _Out_
Definition: no_sal2.h:323
#define KMTFLT_RUN_TEST
Definition: kmt_public.h:30
#define SERVICE_FILE_SYSTEM_DRIVER
Definition: cmtypes.h:952
_Must_inspect_result_ _Inout_ PFLT_VOLUME _In_ PCUNICODE_STRING Altitude
Definition: fltkernel.h:1172
#define REG_OPTION_NON_VOLATILE
Definition: nt_native.h:1057
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4917
DWORD KmtFltSendUlongToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ DWORD Value)
Definition: fltsupport.c:353
#define _Out_opt_
Definition: no_sal2.h:339
#define StringCbCat
Definition: strsafe.h:334
DWORD KmtFltAddAltitude(_In_z_ LPWSTR Altitude)
Definition: fltsupport.c:448
_In_ PIRP _In_ ULONG _In_ ULONG _In_ ULONG InBufferSize
Definition: classpnp.h:429
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define TOKEN_QUERY
Definition: setypes.h:874
static WCHAR TestServiceName[MAX_PATH]
Definition: fltsupport.c:42
__wchar_t WCHAR
Definition: xmlstorage.h:180
HANDLE WINAPI GetCurrentProcess(VOID)
Definition: proc.c:1168
#define MAX_PATH
Definition: compat.h:26
DWORD KmtDeleteService(IN PCWSTR ServiceName OPTIONAL, IN OUT SC_HANDLE *ServiceHandle)
Definition: service.c:356
#define BufferSize
Definition: classpnp.h:419
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD KmtFltDisconnectComms(_In_ HANDLE hPort)
Definition: fltsupport.c:224
BOOL Error
Definition: chkdsk.c:66
#define _Inout_
Definition: no_sal2.h:244
DWORD KmtFltLoad(_In_z_ PCWSTR ServiceName)
Definition: filter.c:30
CHAR Message[80]
Definition: alive.c:5
DWORD KmtFltSendWStringToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ PCWSTR String)
Definition: fltsupport.c:330
HANDLE KmtestHandle
Definition: kmtest.c:32
#define StringCbCopy
Definition: strsafe.h:155
static const WCHAR L[]
Definition: oid.c:1250
LONG WINAPI RegCreateKeyW(HKEY hKey, LPCWSTR lpSubKey, PHKEY phkResult)
Definition: reg.c:1202
DWORD KmtFltSendToDriver(_In_ HANDLE hPort, _In_ DWORD Message)
Definition: fltsupport.c:281
#define _In_reads_bytes_(size)
Definition: no_sal2.h:229
BOOL WINAPI AdjustTokenPrivileges(HANDLE TokenHandle, BOOL DisableAllPrivileges, PTOKEN_PRIVILEGES NewState, DWORD BufferLength, PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength)
Definition: security.c:376
#define _In_
Definition: no_sal2.h:204
_In_ BOOL bEnable
Definition: winddi.h:3426
#define _In_z_
Definition: no_sal2.h:239
_In_ FILTER_INFORMATION_CLASS _In_ ULONG _Out_ PULONG BytesReturned
Definition: fltkernel.h:1716
DWORD KmtFltLoadDriver(_In_ BOOLEAN EnableDriverLoadPrivlege, _In_ BOOLEAN RestartIfRunning, _In_ BOOLEAN ConnectComms, _Out_ HANDLE *hPort)
Definition: fltsupport.c:112
BOOL WINAPI OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle)
Definition: security.c:296
DWORD KmtFltConnectComms(_Out_ HANDLE *hPort)
Definition: fltsupport.c:207
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
IN PVCB IN VBO IN ULONG OUT PBCB OUT PVOID IN BOOLEAN IN BOOLEAN Zero
Definition: fatprocs.h:402
#define OUT
Definition: typedefs.h:39
uint32_t * LPDWORD
Definition: typedefs.h:57
unsigned int ULONG
Definition: retypes.h:1
DWORD KmtFltUnload(_In_z_ PCWSTR ServiceName)
Definition: filter.c:336
DWORD KmtFltConnect(_In_z_ PCWSTR ServiceName, _Out_ HANDLE *hPort)
Definition: filter.c:119
const char * PCSTR
Definition: typedefs.h:51
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define ERROR_SERVICE_ALREADY_RUNNING
Definition: winerror.h:607
DWORD KmtFltCloseService(_Inout_ SC_HANDLE *ServiceHandle)
Definition: fltsupport.c:242
#define REG_DWORD
Definition: sdbapi.c:596
#define TOKEN_ADJUST_PRIVILEGES
Definition: setypes.h:876
#define KEY_CREATE_SUB_KEY
Definition: nt_native.h:1018
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define HeapFree(x, y, z)
Definition: compat.h:394
#define RegCreateKeyEx
Definition: winreg.h:501
#define SE_LOAD_DRIVER_NAME
Definition: winnt_old.h:374
DWORD KmtFltSendStringToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ PCSTR String)
Definition: fltsupport.c:305
DWORD KmtFltDisconnect(_In_ HANDLE hPort)
Definition: filter.c:151
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
#define REG_SZ
Definition: layer.c:22
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68