ReactOS  0.4.15-dev-2956-g61e7ea5
fxregkeykm.cpp
Go to the documentation of this file.
1 /*++
2 
3 Copyright (c) Microsoft Corporation
4 
5 Module Name:
6 
7  FxRegKey.cpp
8 
9 Abstract:
10 
11 Author:
12 
13 Environment:
14 
15  kernel mode only
16 
17 Revision History:
18 
19 --*/
20 
21 #include "fxsupportpch.hpp"
22 
23 extern "C" {
24 // #include "FxRegKeyKM.tmh"
25 }
26 
27 #define AT_PASSIVE() ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL)
28 
30  PFX_DRIVER_GLOBALS FxDriverGlobals
31  ) :
32  FxPagedObject(FX_TYPE_REG_KEY, sizeof(FxRegKey), FxDriverGlobals),
33  m_Key(NULL),
34  m_Globals(FxDriverGlobals)
35 {
36 }
37 
39 FxRegKey::~FxRegKey()
40 {
41  if (m_Key != NULL) {
42  ZwClose(m_Key);
43  m_Key = NULL;
44  }
45 }
46 
49 FxRegKey::_Close(
51  )
52 {
53  return ZwClose(Key);
54 }
55 
59 FxRegKey::_Create(
62  __out HANDLE* NewKey,
66  )
67 {
69 
70  AT_PASSIVE();
71 
72  //
73  // Force OBJ_KERNEL_HANDLE because we are never passing the handle back
74  // up to a process and we don't want to create a handle in an arbitrary
75  // process from which that process can close the handle out from underneath
76  // us.
77  //
79  &oa,
82  ParentKey,
83  NULL);
84 
85  return ZwCreateKey(NewKey,
87  &oa,
88  0,
89  0,
92 }
93 
97 FxRegKey::_OpenKey(
100  __out HANDLE* Key,
102  )
103 {
105 
106  AT_PASSIVE();
107 
108  //
109  // Force OBJ_KERNEL_HANDLE because we are never passing the handle back
110  // up to a process and we don't want to create a handle in an arbitrary
111  // process from which that process can close the handle out from underneath
112  // us.
113  //
115  &oa,
118  ParentKey,
119  NULL);
120 
121  return ZwOpenKey(Key, DesiredAccess, &oa);
122 }
123 
126 NTSTATUS
127 FxRegKey::_SetValue(
128  _In_ HANDLE Key,
133  )
134 {
135  AT_PASSIVE();
136 
137  return ZwSetValueKey(Key,
139  0,
140  ValueType,
141  Value,
142  ValueLength);
143 }
144 
147 NTSTATUS
148 FxRegKey::_QueryValue(
149  __in PFX_DRIVER_GLOBALS FxDriverGlobals,
150  __in HANDLE Key,
156  )
157 {
158  KEY_VALUE_PARTIAL_INFORMATION *pPartial, partial;
160  ULONG length;
161 
162  if (Value == NULL) {
163  //
164  // Caller wants just the length
165  //
166  pPartial = &partial;
167  length = _ComputePartialSize(0);
168  RtlZeroMemory(&partial, length);
169  }
170  else {
171  length = _ComputePartialSize(ValueLength);
172  pPartial = (PKEY_VALUE_PARTIAL_INFORMATION)
173  MxMemory::MxAllocatePoolWithTag(PagedPool, length, FxDriverGlobals->Tag);
174 
175  if (pPartial == NULL) {
177  }
178  }
179 
180  //
181  // We always pass a buffer of at least sizeof(KEY_VALUE_PARTIAL_INFORMATION)
182  // to ZwQueryValueKey. This means that ZwQueryValueKey will write at least
183  // some information to the buffer it receives, even if the user-supplied data
184  // buffer is NULL or too small.
185  //
186  // According to ZwQueryValueKey's contract, this means that it will never return
187  // STATUS_BUFFER_TOO_SMALL (returned when no data is written). Therefore, if the
188  // user passes a NULL or insufficient buffer and the value exists in the registry,
189  // ZwQueryValueKey will return STATUS_BUFFER_OVERFLOW.
190  //
191  status = ZwQueryValueKey(Key,
194  pPartial,
195  length,
196  &length);
197 
198  if (NT_SUCCESS(status) && Value != NULL && (ValueLength >= pPartial->DataLength)) {
199  RtlCopyMemory(Value, &pPartial->Data[0], pPartial->DataLength);
200  }
201 
203  if (ValueLengthQueried != NULL) {
204  *ValueLengthQueried = pPartial->DataLength;
205  }
206  if (ValueType != NULL) {
207  *ValueType = pPartial->Type;
208  }
209  }
210 
211  if (pPartial != &partial) {
212  MxMemory::MxFreePool(pPartial);
213  }
214 
215  return status;
216 }
217 
220 NTSTATUS
221 FxRegKey::_QueryULong(
222  __in HANDLE Key,
225  )
226 {
228  ULONG length;
229 
232 
233  length = sizeof(buffer);
234  pPartial = (PKEY_VALUE_PARTIAL_INFORMATION) &buffer[0];
235 
236  status = ZwQueryValueKey(Key,
239  pPartial,
240  length,
241  &length);
242 
244  pPartial->Type != REG_DWORD) {
246  }
247 
248  if (NT_SUCCESS(status)) {
249  ASSERT(sizeof(ULONG) == pPartial->DataLength);
250 
251  RtlCopyMemory(Value, &pPartial->Data[0], sizeof(ULONG));
252  }
253 
254  return status;
255 }
256 
259 NTSTATUS
260 FxRegKey::_QueryQuadWord(
261  __in HANDLE Key,
264  )
265 {
267  ULONG length;
268 
271 
272  length = sizeof(buffer);
273  pPartial = (PKEY_VALUE_PARTIAL_INFORMATION) &buffer[0];
274 
275  status = ZwQueryValueKey(Key,
278  pPartial,
279  length,
280  &length);
281 
283  pPartial->Type != REG_QWORD) {
285  }
286 
287  if (NT_SUCCESS(status)) {
288  ASSERT(sizeof(LARGE_INTEGER) == pPartial->DataLength);
289 
290  RtlCopyMemory(Value, &pPartial->Data[0], sizeof(LARGE_INTEGER));
291  }
292 
293  return status;
294 }
295 
296 
#define __in_bcount(x)
Definition: dbghelp.h:41
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK DesiredAccess
Definition: wdfdevice.h:2654
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
ActualNumberDriverObjects * sizeof(PDRIVER_OBJECT)) PDRIVER_OBJECT *DriverObjectList
#define __in_opt
Definition: dbghelp.h:38
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
LONG NTSTATUS
Definition: precomp.h:26
GLuint buffer
Definition: glext.h:5915
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _In_ ULONG _Out_opt_ PULONG _Out_opt_ PULONG ValueType
Definition: wdfregistry.h:279
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
static __inline VOID MxFreePool(__in PVOID Ptr)
Definition: mxmemorykm.h:41
#define __out_opt
Definition: dbghelp.h:65
static __inline PVOID MxAllocatePoolWithTag(__in POOL_TYPE PoolType, __in SIZE_T NumberOfBytes, __in ULONG Tag)
Definition: mxmemorykm.h:30
GLenum GLuint GLenum GLsizei length
Definition: glext.h:5579
#define __out
Definition: dbghelp.h:62
#define _In_
Definition: ms_sal.h:308
_Must_inspect_result_ _In_ WDFDEVICE _In_ PCUNICODE_STRING KeyName
Definition: wdfdevice.h:2697
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:406
#define STATUS_OBJECT_TYPE_MISMATCH
Definition: ntstatus.h:273
_Must_inspect_result_ _In_opt_ WDFKEY _In_ PCUNICODE_STRING _In_ ACCESS_MASK _In_ ULONG _Out_opt_ PULONG CreateDisposition
Definition: wdfregistry.h:112
HANDLE m_Key
Definition: fxregkey.hpp:261
#define ASSERT(a)
Definition: mode.c:44
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
struct _KEY_VALUE_PARTIAL_INFORMATION * PKEY_VALUE_PARTIAL_INFORMATION
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING ValueName
Definition: wdfregistry.h:240
unsigned char UCHAR
Definition: xmlstorage.h:181
union _LARGE_INTEGER LARGE_INTEGER
FxRegKey(PFX_DRIVER_GLOBALS FxDriverGlobals)
Definition: fxregkeykm.cpp:29
#define PASSIVE_LEVEL
Definition: env_spec_w32.h:693
#define _Must_inspect_result_
Definition: ms_sal.h:558
_Must_inspect_result_ _In_opt_ WDFKEY _In_ PCUNICODE_STRING _In_ ACCESS_MASK _In_ ULONG CreateOptions
Definition: wdfregistry.h:112
_Must_inspect_result_ __in WDFKEY __in PCUNICODE_STRING __in ULONG __out_bcount_opt(ValueLength) PVOID Value
_Must_inspect_result_ _In_opt_ WDFKEY ParentKey
Definition: wdfregistry.h:69
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:66
__drv_maxIRQL(PASSIVE_LEVEL) FxRegKey
Definition: fxregkeykm.cpp:38
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
unsigned int * PULONG
Definition: retypes.h:1
PFX_DRIVER_GLOBALS m_Globals
Definition: fxobject.hpp:259
#define NULL
Definition: types.h:112
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _In_ ULONG _Out_opt_ PULONG ValueLengthQueried
Definition: wdfregistry.h:279
#define REG_QWORD
Definition: sdbapi.c:597
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define __in
Definition: dbghelp.h:35
#define REG_DWORD
Definition: sdbapi.c:596
static SERVICE_STATUS status
Definition: service.c:31
ULONG ACCESS_MASK
Definition: nt_native.h:40
#define AT_PASSIVE()
Definition: fxregkeykm.cpp:27
Definition: ps.c:97
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _In_ ULONG ValueLength
Definition: wdfregistry.h:271