VirtualBox

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

Last change on this file since 76553 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 8.9 KB
Line 
1/* $Id: krnlmod-win.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - Kernel module, Windows.
4 */
5
6/*
7 * Copyright (C) 2017-2019 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 AssertReturn(VALID_PTR(pahKrnlModInfo) || cEntriesMax == 0, VERR_INVALID_PARAMETER);
189
190 PRTL_PROCESS_MODULES pKrnlMods = NULL;
191 int rc = rtKrnlModWinQueryKrnlMods(&pKrnlMods);
192 if (RT_SUCCESS(rc))
193 {
194 if (pKrnlMods->NumberOfModules <= cEntriesMax)
195 {
196 for (unsigned i = 0; i < pKrnlMods->NumberOfModules; i++)
197 {
198 pKrnlMods->Modules[i].FullPathName[255] = '\0'; /* Paranoia */
199 rc = rtKrnlModWinInfoCreate(&pKrnlMods->Modules[i], &pahKrnlModInfo[i]);
200 if (RT_FAILURE(rc))
201 {
202 while (i-- > 0)
203 RTKrnlModInfoRelease(pahKrnlModInfo[i]);
204 break;
205 }
206 }
207 }
208 else
209 rc = VERR_BUFFER_OVERFLOW;
210
211 if (pcEntries)
212 *pcEntries = pKrnlMods->NumberOfModules;
213
214 RTMemFree(pKrnlMods);
215 }
216
217 return rc;
218}
219
220
221RTDECL(uint32_t) RTKrnlModInfoRetain(RTKRNLMODINFO hKrnlModInfo)
222{
223 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
224 AssertPtrReturn(pThis, UINT32_MAX);
225
226 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
227 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
228 return cRefs;
229}
230
231
232RTDECL(uint32_t) RTKrnlModInfoRelease(RTKRNLMODINFO hKrnlModInfo)
233{
234 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
235 if (!pThis)
236 return 0;
237 AssertPtrReturn(pThis, UINT32_MAX);
238
239 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
240 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
241 if (cRefs == 0)
242 rtKrnlModInfoDestroy(pThis);
243 return cRefs;
244}
245
246
247RTDECL(uint32_t) RTKrnlModInfoGetRefCnt(RTKRNLMODINFO hKrnlModInfo)
248{
249 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
250 AssertPtrReturn(pThis, 0);
251
252 return pThis->cRefKrnlMod;
253}
254
255
256RTDECL(const char *) RTKrnlModInfoGetName(RTKRNLMODINFO hKrnlModInfo)
257{
258 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
259 AssertPtrReturn(pThis, NULL);
260
261 return pThis->pszName;
262}
263
264
265RTDECL(const char *) RTKrnlModInfoGetFilePath(RTKRNLMODINFO hKrnlModInfo)
266{
267 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
268 AssertPtrReturn(pThis, NULL);
269
270 return &pThis->achFilePath[0];
271}
272
273
274RTDECL(size_t) RTKrnlModInfoGetSize(RTKRNLMODINFO hKrnlModInfo)
275{
276 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
277 AssertPtrReturn(pThis, 0);
278
279 return pThis->cbKrnlMod;
280}
281
282
283RTDECL(RTR0UINTPTR) RTKrnlModInfoGetLoadAddr(RTKRNLMODINFO hKrnlModInfo)
284{
285 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
286 AssertPtrReturn(pThis, 0);
287
288 return pThis->uLoadAddr;
289}
290
291
292RTDECL(int) RTKrnlModInfoQueryRefModInfo(RTKRNLMODINFO hKrnlModInfo, uint32_t idx,
293 PRTKRNLMODINFO phKrnlModInfoRef)
294{
295 RT_NOREF3(hKrnlModInfo, idx, phKrnlModInfoRef);
296 return VERR_NOT_SUPPORTED;
297}
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