ReactOS  0.4.15-dev-2720-g5ee0925
aux_klib.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS SDK: Auxiliary Kernel-Mode Library
3  * LICENSE: BSD-2-Clause-Views (https://spdx.org/licenses/BSD-2-Clause-Views)
4  * PURPOSE: Main source file
5  * COPYRIGHT: Copyright 2019-2020 Max Korostil <mrmks04@yandex.ru>
6  * Copyright 2021 Victor Perevertkin <victor.perevertkin@reactos.org>
7  */
8 
9 #include <ntifs.h>
10 #include <ntintsafe.h>
11 #include <ndk/ntndk.h>
12 #include <pseh/pseh2.h>
13 #include <aux_klib.h>
14 
15 #define TAG_AUXK 'AuxK'
16 
18 
21 
22 
23 CODE_SEG("PAGE")
25 NTAPI
27 {
28  RTL_OSVERSIONINFOW osVersion;
29  UNICODE_STRING strRtlQueryModuleInformation = RTL_CONSTANT_STRING(L"RtlQueryModuleInformation");
30 
31  PAGED_CODE();
32 
33  if (!gKlibInitialized)
34  {
35  RtlGetVersion(&osVersion);
36  if (osVersion.dwMajorVersion >= 5)
37  {
38  pfnRtlQueryModuleInformation = MmGetSystemRoutineAddress(&strRtlQueryModuleInformation);
40  }
41  else
42  {
43  return STATUS_NOT_SUPPORTED;
44  }
45  }
46 
47  return STATUS_SUCCESS;
48 }
49 
50 CODE_SEG("PAGE")
52 NTAPI
54  _In_ PULONG InformationLength,
57 {
59 
60  PAGED_CODE();
61 
62  if (gKlibInitialized != 1)
63  {
64  return STATUS_UNSUCCESSFUL;
65  }
66 
67  // if we have the function exported from the kernel, use it
69  {
70  return pfnRtlQueryModuleInformation(InformationLength, SizePerModule, ModuleInfo);
71  }
72 
73  if (SizePerModule != sizeof(AUX_MODULE_BASIC_INFO) &&
75  {
77  }
78 
80  {
82  }
83 
84  // first call the function with a place for only 1 module
85  RTL_PROCESS_MODULES processModulesMinimal;
86  PRTL_PROCESS_MODULES processModules = &processModulesMinimal;
87  ULONG sysInfoLength = sizeof(processModulesMinimal);
88  ULONG resultLength;
89 
90  // loop until we have a large-enough buffer for all modules
91  do
92  {
94  processModules,
95  sysInfoLength,
96  &resultLength);
97 
99  {
100  // free the old buffer if it's not the first one
101  if (processModules != &processModulesMinimal)
102  {
103  ExFreePoolWithTag(processModules, TAG_AUXK);
104  }
105 
106  _SEH2_TRY
107  {
108  // allocate the new one
109  processModules = ExAllocatePoolWithQuotaTag(PagedPool, resultLength, TAG_AUXK);
110  }
112  {
114  }
115  _SEH2_END;
116 
117  if (!processModules)
118  {
120  }
121  sysInfoLength = resultLength;
122  }
123 
124  } while (status == STATUS_INFO_LENGTH_MISMATCH);
125 
126  if (!NT_SUCCESS(status))
127  {
128  goto Cleanup;
129  }
130 
131  ULONG modulesSize;
132  status = RtlULongMult(SizePerModule, processModules->NumberOfModules, &modulesSize);
133  if (!NT_SUCCESS(status))
134  {
135  goto Cleanup;
136  }
137 
138  if (ModuleInfo == NULL)
139  {
141  *InformationLength = modulesSize;
142  goto Cleanup;
143  }
144 
145  if (*InformationLength < modulesSize)
146  {
148  *InformationLength = modulesSize;
149  goto Cleanup;
150  }
151 
152  // copy the information to the input array
153  for (UINT32 i = 0; i < processModules->NumberOfModules; i++)
154  {
155  ModuleInfo[i].BasicInfo.ImageBase = processModules->Modules[i].ImageBase;
156 
158  {
159  ModuleInfo[i].ImageSize = processModules->Modules[i].ImageSize;
160  ModuleInfo[i].FileNameOffset = processModules->Modules[i].OffsetToFileName;
161  RtlCopyMemory(&ModuleInfo[i].FullPathName,
162  processModules->Modules[i].FullPathName,
163  sizeof(processModules->Modules[i].FullPathName));
164  }
165  }
166 
167 Cleanup:
168  // don't accidentally free the stack buffer
169  if (processModules != NULL && processModules != &processModulesMinimal)
170  {
171  ExFreePoolWithTag(processModules, TAG_AUXK);
172  }
173 
174  return status;
175 }
176 
177 NTSTATUS
180 {
181  if (BugCheckData->BugCheckDataSize != sizeof(*BugCheckData))
182  {
184  }
185 
186  BugCheckData->BugCheckCode = KiBugCheckData[0];
187  BugCheckData->Parameter1 = KiBugCheckData[1];
188  BugCheckData->Parameter2 = KiBugCheckData[2];
189  BugCheckData->Parameter3 = KiBugCheckData[3];
190  BugCheckData->Parameter4 = KiBugCheckData[4];
191 
192  return STATUS_SUCCESS;
193 }
194 
197  _In_ PVOID ImageBase)
198 {
199  ULONG size;
201 }
202 
204 CODE_SEG("PAGE")
205 NTSTATUS
206 NTAPI
207 AuxKlibEnumerateSystemFirmwareTables (
208  _In_ ULONG FirmwareTableProviderSignature,
212 {
213  return STATUS_NOT_IMPLEMENTED;
214 }
215 
217 CODE_SEG("PAGE")
218 NTSTATUS
219 NTAPI
220 AuxKlibGetSystemFirmwareTable (
221  _In_ ULONG FirmwareTableProviderSignature,
226 {
227  return STATUS_NOT_IMPLEMENTED;
228 }
IN CINT OUT PVOID IN ULONG OUT PULONG ReturnLength
Definition: dumpinfo.c:39
LONG gKlibInitialized
Definition: aux_klib.c:20
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ ULONG BufferLength
Definition: wdfdevice.h:3767
return STATUS_NOT_SUPPORTED
#define TYPE_ALIGNMENT(t)
Definition: ntbasedef.h:117
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
NTSTATUS NTAPI AuxKlibInitialize(VOID)
Definition: aux_klib.c:26
PVOID NTAPI ExAllocatePoolWithQuotaTag(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes, IN ULONG Tag)
Definition: expool.c:2984
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
NTSTATUS NTAPI RtlGetVersion(IN OUT PRTL_OSVERSIONINFOW lpVersionInformation)
Definition: version.c:158
#define TRUE
Definition: types.h:120
NTSTATUS NTAPI AuxKlibQueryModuleInformation(_In_ PULONG InformationLength, _In_ ULONG SizePerModule, _Inout_ PAUX_MODULE_EXTENDED_INFO ModuleInfo)
Definition: aux_klib.c:53
LONG NTSTATUS
Definition: precomp.h:26
ULONG_PTR KiBugCheckData[5]
Definition: bug.c:27
PFN_RTLQUERYMODULEINFORMATION pfnRtlQueryModuleInformation
Definition: aux_klib.c:19
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
_SEH2_TRY
Definition: create.c:4226
uint32_t ULONG_PTR
Definition: typedefs.h:65
NTSTATUS(NTAPI * PFN_RTLQUERYMODULEINFORMATION)(PULONG, ULONG, PVOID)
Definition: aux_klib.c:17
return STATUS_NOT_IMPLEMENTED
#define _Out_writes_bytes_to_opt_(s, c)
Definition: no_sal2.h:240
#define STATUS_INVALID_PARAMETER_3
Definition: ntstatus.h:477
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
unsigned int UINT32
PVOID NTAPI MmGetSystemRoutineAddress(IN PUNICODE_STRING SystemRoutineName)
Definition: sysldr.c:3514
long LONG
Definition: pedump.c:60
NTSYSAPI NTSTATUS NTAPI ZwQuerySystemInformation(IN SYSTEM_INFORMATION_CLASS SystemInfoClass, OUT PVOID SystemInfoBuffer, IN ULONG SystemInfoBufferSize, OUT PULONG BytesReturned OPTIONAL)
_In_ ULONG _Out_writes_bytes_opt_ InformationLength PAUX_MODULE_EXTENDED_INFO ModuleInfo
Definition: aux_klib.h:65
#define STATUS_INVALID_PARAMETER_2
Definition: ntstatus.h:476
CODE_SEG("INIT")
Definition: Interface.c:1810
void * PVOID
Definition: retypes.h:9
#define _Out_opt_
Definition: no_sal2.h:214
GLsizeiptr size
Definition: glext.h:5919
ULONG dwMajorVersion
Definition: rtltypes.h:247
#define ASSERT(a)
Definition: mode.c:44
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define TAG_AUXK
Definition: aux_klib.c:15
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
static tBugCheckData BugCheckData
_In_ ULONG FirmwareTableID
Definition: aux_klib.h:91
#define _Inout_
Definition: no_sal2.h:162
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
static const WCHAR L[]
Definition: oid.c:1250
#define RtlImageDirectoryEntryToData
Definition: compat.h:668
#define NTSTATUS
Definition: precomp.h:20
static const WCHAR Cleanup[]
Definition: register.c:80
#define PASSIVE_LEVEL
Definition: env_spec_w32.h:693
_In_ ULONG SizePerModule
Definition: aux_klib.h:64
#define IMAGE_DIRECTORY_ENTRY_EXPORT
Definition: compat.h:151
#define InterlockedExchange
Definition: armddk.h:54
#define _In_
Definition: no_sal2.h:158
_SEH2_END
Definition: create.c:4400
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
unsigned int * PULONG
Definition: retypes.h:1
#define NULL
Definition: types.h:112
PIMAGE_EXPORT_DIRECTORY AuxKlibGetImageExportDirectory(_In_ PVOID ImageBase)
Definition: aux_klib.c:196
AUX_MODULE_BASIC_INFO BasicInfo
Definition: aux_klib.h:38
unsigned int ULONG
Definition: retypes.h:1
RTL_PROCESS_MODULE_INFORMATION Modules[1]
Definition: rtltypes.h:1016
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:40
#define STATUS_SUCCESS
Definition: shellext.h:65
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#define _SEH2_YIELD(__stmt)
Definition: pseh2_64.h:168
static SERVICE_STATUS status
Definition: service.c:31
NTSTATUS AuxKlibGetBugCheckData(_Inout_ PKBUGCHECK_DATA BugCheckData)
Definition: aux_klib.c:178
_IRQL_requires_max_(PASSIVE_LEVEL)
Definition: aux_klib.c:203
#define PAGED_CODE()
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
Definition: ps.c:97