ReactOS 0.4.16-dev-1946-g52006dd
common.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS WdfLdr driver
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: WdfLdr driver - common functions
5 * COPYRIGHT: Copyright 2019 Max Korostil <mrmks04@yandex.ru>
6 * Copyright 2021 Victor Perevertkin <victor.perevertkin@reactos.org>
7 */
8
9#include "wdfloader.h"
10
11
12VOID
14{
17}
18
19VOID
21{
24}
25
26VOID
30{
31 PWCHAR pNextSym;
32 PWCHAR pCurrSym;
33
34 if (Path->Length < sizeof(WCHAR))
35 {
36 Name->Length = 0;
37 Name->Buffer = NULL;
38 return;
39 }
40
41 Name->Buffer = Path->Buffer + (Path->Length / 2) - 1;
42 Name->Length = sizeof(WCHAR);
43
44 for (pNextSym = Name->Buffer; ; Name->Buffer = pNextSym)
45 {
46 if (pNextSym < Path->Buffer)
47 {
48 Name->Length -= sizeof(WCHAR);
49 ++Name->Buffer;
50 goto end;
51 }
52 pCurrSym = Name->Buffer;
53
54 if (*pCurrSym == '\\')
55 {
56 break;
57 }
58 pNextSym = pCurrSym - 1;
59 Name->Length += sizeof(WCHAR);
60 }
61
62 ++Name->Buffer;
63 Name->Length -= sizeof(WCHAR);
64
65 if (Name->Length == sizeof(WCHAR))
66 {
67 Name->Buffer = NULL;
68 Name->Length = 0;
69 }
70
71end:
72 Name->MaximumLength = Name->Length;
73}
74
79{
81 OBJECT_ATTRIBUTES objectAttributes;
84 PKEY_VALUE_PARTIAL_INFORMATION pKeyValPartial = NULL;
86 UNICODE_STRING valueName = RTL_CONSTANT_STRING(L"ImagePath");
87
88 InitializeObjectAttributes(&objectAttributes,
91 NULL,
92 NULL);
93
94 status = ZwOpenKey(&keyHandle, KEY_READ, &objectAttributes);
95
96 if (!NT_SUCCESS(status))
97 {
98 __DBGPRINT(("ERROR: GetImageName failed with status 0x%x\n", status));
99 return status;
100 }
101
102 status = FxLdrQueryData(keyHandle, &valueName, WDFLDR_TAG, &pKeyValPartial);
103 if (!NT_SUCCESS(status))
104 {
105 __DBGPRINT(("ERROR: GetImageName failed with status 0x%x\n", status));
106 return status;
107 }
108
109 if (pKeyValPartial->Type != REG_SZ &&
110 pKeyValPartial->Type != REG_EXPAND_SZ)
111 {
113 __DBGPRINT(("ERROR: GetImageName failed with status 0x%x\n", status));
114 }
115
116 if (pKeyValPartial->DataLength == 0 ||
117 pKeyValPartial->DataLength > 0xFFFF)
118 {
120 __DBGPRINT(("ERROR: GetImageName failed with status 0x%x\n", status));
121 }
122
123 path.Buffer = (PWCH)pKeyValPartial->Data;
124 path.Length = (USHORT)pKeyValPartial->DataLength;
125 path.MaximumLength = (USHORT)pKeyValPartial->DataLength;
126
127 if (pKeyValPartial->DataLength >= sizeof(WCHAR) &&
128 !*(((WCHAR*)&pKeyValPartial->Data) + pKeyValPartial->DataLength / sizeof(WCHAR)))
129 {
130 path.Length = (USHORT)pKeyValPartial->DataLength - sizeof(WCHAR);
131 }
132
134
135 if (name.Length == 0)
136 {
138 __DBGPRINT(("ERROR: GetNameFromPathW could not find a name, status 0x%x\n", status));
139 goto clean;
140 }
141
142 status = RtlUShortAdd(name.Length, 2, &ImageName->Length);
143
144 if (!NT_SUCCESS(status))
145 {
147 __DBGPRINT(("ERROR: size computation failed with Status 0x%x\n", status));
148 goto clean;
149 }
150
152
153 if (ImageName->Buffer != NULL)
154 {
155 RtlZeroMemory(ImageName->Buffer, ImageName->Length);
156 ImageName->MaximumLength = ImageName->Length;
157 ImageName->Length = 0;
159
160 __DBGPRINT(("Version Image Name \"%wZ\"\n", ImageName));
161 }
162 else
163 {
165 __DBGPRINT(("ERROR: ExAllocatePoolWithTag failed with Status 0x%x\n", status));
166 }
167
168clean:
169 if (keyHandle)
170 {
172 }
173 if (pKeyValPartial)
174 {
175 ExFreePoolWithTag(pKeyValPartial, WDFLDR_TAG);
176 }
177
178 return status;
179}
180
184 _Out_ PVOID* ImageBase,
185 _Out_ PULONG ImageSize)
186{
187 ANSI_STRING ansiImageName;
189
191
192 if (!NT_SUCCESS(status))
193 {
194 __DBGPRINT(("ERROR: RtlUnicodeStringToAnsiString failed with Status 0x%x\n", status));
195 return status;
196 }
197
198 PAUX_MODULE_EXTENDED_INFO modulesBuffer;
199 ULONG modulesSize;
200 for (;;)
201 {
204 NULL);
205 if (!NT_SUCCESS(status))
206 {
207 goto clean;
208 }
209
210 modulesBuffer = ExAllocatePoolZero(PagedPool, modulesSize, WDFLDR_TAG);
211 if (modulesBuffer == NULL)
212 {
214 __DBGPRINT(("ERROR: ExAllocatePoolWithTag failed\n"));
215
216 goto clean;
217 }
218
221 modulesBuffer);
223 {
224 ExFreePoolWithTag(modulesBuffer, WDFLDR_TAG);
225 continue;
226 }
227 else
228 {
229 break;
230 }
231 }
232
233 if (NT_SUCCESS(status))
234 {
235 for (SIZE_T i = 0; i < modulesSize / sizeof(AUX_MODULE_EXTENDED_INFO); i++)
236 {
237 // Compare our image name to names returned by AuxKlib
238 if (modulesBuffer[i].FileNameOffset < AUX_KLIB_MODULE_PATH_LEN &&
239 _strnicmp(&modulesBuffer[i].FullPathName[modulesBuffer[i].FileNameOffset],
240 ansiImageName.Buffer, ansiImageName.Length) == 0)
241 {
242 *ImageBase = modulesBuffer[i].BasicInfo.ImageBase;
243 *ImageSize = modulesBuffer[i].ImageSize;
244 break;
245 }
246 }
247 }
248 else
249 {
250 __DBGPRINT(("ERROR: AuxKlibQueryModuleInformation failed with Status 0x%x\n", status));
251 }
252
253 ExFreePoolWithTag(modulesBuffer, WDFLDR_TAG);
254
255clean:
256 RtlFreeAnsiString(&ansiImageName);
257 return status;
258}
PRTL_UNICODE_STRING_BUFFER Path
NTSTATUS NTAPI AuxKlibQueryModuleInformation(_In_ PULONG InformationLength, _In_ ULONG SizePerModule, _Inout_ PAUX_MODULE_EXTENDED_INFO ModuleInfo)
Definition: aux_klib.c:53
struct _AUX_MODULE_EXTENDED_INFO AUX_MODULE_EXTENDED_INFO
#define AUX_KLIB_MODULE_PATH_LEN
Definition: aux_klib.h:31
LONG NTSTATUS
Definition: precomp.h:26
#define STATUS_INTEGER_OVERFLOW
Definition: log.c:20
Definition: bufpool.h:45
#define STATUS_OBJECT_TYPE_MISMATCH
Definition: d3dkmdt.h:46
LPWSTR Name
Definition: desk.c:124
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define _strnicmp(_String1, _String2, _MaxCount)
Definition: compat.h:23
#define L(x)
Definition: resources.c:13
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define ExAcquireResourceExclusiveLite(res, wait)
Definition: env_spec_w32.h:615
#define PagedPool
Definition: env_spec_w32.h:308
WDFKEY keyHandle
GLuint GLuint end
Definition: gl.h:1545
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define KeLeaveCriticalRegion()
Definition: ke_x.h:119
#define KeEnterCriticalRegion()
Definition: ke_x.h:88
#define REG_SZ
Definition: layer.c:22
_In_ PCUNICODE_STRING ServicePath
Definition: library.c:55
if(dx< 0)
Definition: linetemp.h:194
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
FORCEINLINE PVOID ExAllocatePoolZero(ULONG PoolType, SIZE_T NumberOfBytes, ULONG Tag)
Definition: precomp.h:45
static const char * ImageName
Definition: image.c:34
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
NTSYSAPI VOID NTAPI RtlCopyUnicodeString(PUNICODE_STRING DestinationString, PUNICODE_STRING SourceString)
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToAnsiString(PANSI_STRING DestinationString, PUNICODE_STRING SourceString, BOOLEAN AllocateDestinationString)
NTSYSAPI VOID NTAPI RtlFreeAnsiString(PANSI_STRING AnsiString)
#define KEY_READ
Definition: nt_native.h:1026
#define REG_EXPAND_SZ
Definition: nt_native.h:1497
WCHAR * PWCH
Definition: ntbasedef.h:422
VOID FASTCALL ExReleaseResourceLite(IN PERESOURCE Resource)
Definition: resource.c:1822
unsigned short USHORT
Definition: pedump.c:61
VOID FxLdrAcquireLoadedModuleLock(VOID)
Definition: common.c:13
NTSTATUS GetImageName(_In_ PCUNICODE_STRING ServicePath, _Out_ PUNICODE_STRING ImageName)
Definition: common.c:76
VOID GetNameFromPath(_In_ PCUNICODE_STRING Path, _Out_ PUNICODE_STRING Name)
Definition: common.c:27
VOID FxLdrReleaseLoadedModuleLock(VOID)
Definition: common.c:20
NTSTATUS GetImageInfo(_In_ PCUNICODE_STRING ImageName, _Out_ PVOID *ImageBase, _Out_ PULONG ImageSize)
Definition: common.c:182
NTSTATUS FxLdrQueryData(_In_ HANDLE KeyHandle, _In_ PUNICODE_STRING ValueName, _In_ ULONG Tag, _Out_ PKEY_VALUE_PARTIAL_INFORMATION *KeyValPartialInfo)
Definition: registry.c:267
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
AUX_MODULE_BASIC_INFO BasicInfo
Definition: aux_klib.h:38
ERESOURCE LoadedModulesListLock
Definition: wdfloader.h:72
Definition: name.c:39
Definition: ps.c:97
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
uint32_t * PULONG
Definition: typedefs.h:59
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint16_t * PWCHAR
Definition: typedefs.h:56
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
WDF_LDR_GLOBALS WdfLdrGlobals
Definition: wdfldr.c:28
#define WDFLDR_TAG
Definition: wdfloader.h:17
#define __DBGPRINT(_x_)
Definition: wdfloader.h:62
__wchar_t WCHAR
Definition: xmlstorage.h:180