VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/krnlmod-win.cpp@ 96124

Last change on this file since 96124 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 9.3 KB
Line 
1/* $Id: krnlmod-win.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * IPRT - Kernel module, Windows.
4 */
5
6/*
7 * Copyright (C) 2017-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_SYSTEM
32#include <iprt/nt/nt.h>
33
34#include <iprt/krnlmod.h>
35#include <iprt/asm.h>
36#include <iprt/assert.h>
37#include <iprt/dir.h>
38#include <iprt/errcore.h>
39#include <iprt/mem.h>
40#include <iprt/string.h>
41#include <iprt/types.h>
42
43
44/**
45 * Internal kernel information record state.
46 */
47typedef struct RTKRNLMODINFOINT
48{
49 /** Reference counter. */
50 volatile uint32_t cRefs;
51 /** Reference count for the kernel module. */
52 uint32_t cRefKrnlMod;
53 /** Load address of the kernel module. */
54 RTR0UINTPTR uLoadAddr;
55 /** Size of the kernel module. */
56 size_t cbKrnlMod;
57 /** Pointer to the driver name. */
58 const char *pszName;
59 /** Size of the name in characters including the zero terminator. */
60 size_t cchFilePath;
61 /** Module name - variable in size. */
62 char achFilePath[1];
63} RTKRNLMODINFOINT;
64/** Pointer to the internal kernel module information record. */
65typedef RTKRNLMODINFOINT *PRTKRNLMODINFOINT;
66/** Pointer to a const internal kernel module information record. */
67typedef const RTKRNLMODINFOINT *PCRTKRNLMODINFOINT;
68
69
70/**
71 * Destroy the given kernel module information record.
72 *
73 * @returns nothing.
74 * @param pThis The record to destroy.
75 */
76static void rtKrnlModInfoDestroy(PRTKRNLMODINFOINT pThis)
77{
78 RTMemFree(pThis);
79}
80
81
82/**
83 * Queries the complete kernel modules structure and returns a pointer to it.
84 *
85 * @returns IPRT status code.
86 * @param ppKrnlMods Where to store the pointer to the kernel module list on success.
87 * Free with RTMemFree().
88 */
89static int rtKrnlModWinQueryKrnlMods(PRTL_PROCESS_MODULES *ppKrnlMods)
90{
91 int rc = VINF_SUCCESS;
92 RTL_PROCESS_MODULES KrnlModsSize;
93
94 NTSTATUS rcNt = NtQuerySystemInformation(SystemModuleInformation, &KrnlModsSize, sizeof(KrnlModsSize), NULL);
95 if (NT_SUCCESS(rcNt) || rcNt == STATUS_INFO_LENGTH_MISMATCH)
96 {
97 ULONG cbKrnlMods = RT_UOFFSETOF_DYN(RTL_PROCESS_MODULES, Modules[KrnlModsSize.NumberOfModules]);
98 PRTL_PROCESS_MODULES pKrnlMods = (PRTL_PROCESS_MODULES)RTMemAllocZ(cbKrnlMods);
99 if (RT_LIKELY(pKrnlMods))
100 {
101 rcNt = NtQuerySystemInformation(SystemModuleInformation, pKrnlMods, cbKrnlMods, NULL);
102 if (NT_SUCCESS(rcNt))
103 *ppKrnlMods = pKrnlMods;
104 else
105 rc = RTErrConvertFromNtStatus(rcNt);
106 }
107 else
108 rc = VERR_NO_MEMORY;
109 }
110 else
111 rc = RTErrConvertFromNtStatus(rcNt);
112
113 return rc;
114}
115
116/**
117 * Creates a new kernel module information record for the given module.
118 *
119 * @returns IPRT status code.
120 * @param pModInfo The kernel module information.
121 * @param phKrnlModInfo Where to store the handle to the kernel module information record
122 * on success.
123 */
124static int rtKrnlModWinInfoCreate(PRTL_PROCESS_MODULE_INFORMATION pModInfo, PRTKRNLMODINFO phKrnlModInfo)
125{
126 int rc = VINF_SUCCESS;
127 RT_NOREF2(pModInfo, phKrnlModInfo);
128 size_t cchFilePath = strlen((const char *)&pModInfo->FullPathName[0]) + 1;
129 PRTKRNLMODINFOINT pThis = (PRTKRNLMODINFOINT)RTMemAllocZ(RT_UOFFSETOF_DYN(RTKRNLMODINFOINT, achFilePath[cchFilePath]));
130 if (RT_LIKELY(pThis))
131 {
132 memcpy(&pThis->achFilePath[0], &pModInfo->FullPathName[0], cchFilePath);
133 pThis->cchFilePath = cchFilePath;
134 pThis->cRefs = 1;
135 pThis->cbKrnlMod = pModInfo->ImageSize;
136 pThis->uLoadAddr = (RTR0UINTPTR)pModInfo->ImageBase;
137 pThis->pszName = pModInfo->OffsetToFileName >= cchFilePath
138 ? NULL
139 : pThis->achFilePath + pModInfo->OffsetToFileName;
140
141 *phKrnlModInfo = pThis;
142 }
143 else
144 rc = VERR_NO_MEMORY;
145
146 return rc;
147}
148
149
150RTDECL(int) RTKrnlModQueryLoaded(const char *pszName, bool *pfLoaded)
151{
152 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
153 AssertPtrReturn(pfLoaded, VERR_INVALID_POINTER);
154
155 int rc = VERR_NOT_IMPLEMENTED;
156
157 return rc;
158}
159
160
161RTDECL(int) RTKrnlModLoadedQueryInfo(const char *pszName, PRTKRNLMODINFO phKrnlModInfo)
162{
163 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
164 AssertPtrReturn(phKrnlModInfo, VERR_INVALID_POINTER);
165
166 int rc = VERR_NOT_IMPLEMENTED;
167
168 return rc;
169}
170
171
172RTDECL(uint32_t) RTKrnlModLoadedGetCount(void)
173{
174 uint32_t cKrnlMods = 0;
175 RTL_PROCESS_MODULES ProcMods;
176
177 NTSTATUS rcNt = NtQuerySystemInformation(SystemModuleInformation, &ProcMods, sizeof(ProcMods), NULL);
178 if (NT_SUCCESS(rcNt) || rcNt == STATUS_INFO_LENGTH_MISMATCH)
179 cKrnlMods = ProcMods.NumberOfModules;
180
181 return cKrnlMods;
182}
183
184
185RTDECL(int) RTKrnlModLoadedQueryInfoAll(PRTKRNLMODINFO pahKrnlModInfo, uint32_t cEntriesMax,
186 uint32_t *pcEntries)
187{
188 if (cEntriesMax > 0)
189 AssertPtrReturn(pahKrnlModInfo, VERR_INVALID_POINTER);
190
191 PRTL_PROCESS_MODULES pKrnlMods = NULL;
192 int rc = rtKrnlModWinQueryKrnlMods(&pKrnlMods);
193 if (RT_SUCCESS(rc))
194 {
195 if (pKrnlMods->NumberOfModules <= cEntriesMax)
196 {
197 for (unsigned i = 0; i < pKrnlMods->NumberOfModules; i++)
198 {
199 pKrnlMods->Modules[i].FullPathName[255] = '\0'; /* Paranoia */
200 rc = rtKrnlModWinInfoCreate(&pKrnlMods->Modules[i], &pahKrnlModInfo[i]);
201 if (RT_FAILURE(rc))
202 {
203 while (i-- > 0)
204 RTKrnlModInfoRelease(pahKrnlModInfo[i]);
205 break;
206 }
207 }
208 }
209 else
210 rc = VERR_BUFFER_OVERFLOW;
211
212 if (pcEntries)
213 *pcEntries = pKrnlMods->NumberOfModules;
214
215 RTMemFree(pKrnlMods);
216 }
217
218 return rc;
219}
220
221
222RTDECL(uint32_t) RTKrnlModInfoRetain(RTKRNLMODINFO hKrnlModInfo)
223{
224 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
225 AssertPtrReturn(pThis, UINT32_MAX);
226
227 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
228 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
229 return cRefs;
230}
231
232
233RTDECL(uint32_t) RTKrnlModInfoRelease(RTKRNLMODINFO hKrnlModInfo)
234{
235 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
236 if (!pThis)
237 return 0;
238 AssertPtrReturn(pThis, UINT32_MAX);
239
240 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
241 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
242 if (cRefs == 0)
243 rtKrnlModInfoDestroy(pThis);
244 return cRefs;
245}
246
247
248RTDECL(uint32_t) RTKrnlModInfoGetRefCnt(RTKRNLMODINFO hKrnlModInfo)
249{
250 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
251 AssertPtrReturn(pThis, 0);
252
253 return pThis->cRefKrnlMod;
254}
255
256
257RTDECL(const char *) RTKrnlModInfoGetName(RTKRNLMODINFO hKrnlModInfo)
258{
259 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
260 AssertPtrReturn(pThis, NULL);
261
262 return pThis->pszName;
263}
264
265
266RTDECL(const char *) RTKrnlModInfoGetFilePath(RTKRNLMODINFO hKrnlModInfo)
267{
268 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
269 AssertPtrReturn(pThis, NULL);
270
271 return &pThis->achFilePath[0];
272}
273
274
275RTDECL(size_t) RTKrnlModInfoGetSize(RTKRNLMODINFO hKrnlModInfo)
276{
277 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
278 AssertPtrReturn(pThis, 0);
279
280 return pThis->cbKrnlMod;
281}
282
283
284RTDECL(RTR0UINTPTR) RTKrnlModInfoGetLoadAddr(RTKRNLMODINFO hKrnlModInfo)
285{
286 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
287 AssertPtrReturn(pThis, 0);
288
289 return pThis->uLoadAddr;
290}
291
292
293RTDECL(int) RTKrnlModInfoQueryRefModInfo(RTKRNLMODINFO hKrnlModInfo, uint32_t idx,
294 PRTKRNLMODINFO phKrnlModInfoRef)
295{
296 RT_NOREF3(hKrnlModInfo, idx, phKrnlModInfoRef);
297 return VERR_NOT_SUPPORTED;
298}
299
300
301RTDECL(int) RTKrnlModLoadByName(const char *pszName)
302{
303 AssertPtrReturn(pszName, VERR_INVALID_PARAMETER);
304
305 return VERR_NOT_SUPPORTED;
306}
307
308
309RTDECL(int) RTKrnlModLoadByPath(const char *pszPath)
310{
311 AssertPtrReturn(pszPath, VERR_INVALID_PARAMETER);
312
313 return VERR_NOT_SUPPORTED;
314}
315
316
317RTDECL(int) RTKrnlModUnloadByName(const char *pszName)
318{
319 AssertPtrReturn(pszName, VERR_INVALID_PARAMETER);
320
321 return VERR_NOT_SUPPORTED;
322}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette