VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/memuserkernel-r0drv-nt.cpp@ 92247

Last change on this file since 92247 was 91675, checked in by vboxsync, 3 years ago

IPRT/nt/RTR0MemKernelCopyTo/From: Code doesn't work, made some imperfect improvements and disabled the write version.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1/* $Id: memuserkernel-r0drv-nt.cpp 91675 2021-10-11 20:43:10Z vboxsync $ */
2/** @file
3 * IPRT - User & Kernel Memory, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2009-2020 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#include "the-nt-kernel.h"
32
33#include <iprt/mem.h>
34#include <iprt/errcore.h>
35
36#include "internal-r0drv-nt.h"
37
38
39RTR0DECL(int) RTR0MemUserCopyFrom(void *pvDst, RTR3PTR R3PtrSrc, size_t cb)
40{
41 __try
42 {
43 ProbeForRead((PVOID)R3PtrSrc, cb, 1);
44 memcpy(pvDst, (void const *)R3PtrSrc, cb);
45 }
46 __except(EXCEPTION_EXECUTE_HANDLER)
47 {
48 return VERR_ACCESS_DENIED;
49 }
50 return VINF_SUCCESS;
51}
52
53
54RTR0DECL(int) RTR0MemUserCopyTo(RTR3PTR R3PtrDst, void const *pvSrc, size_t cb)
55{
56 __try
57 {
58 ProbeForWrite((PVOID)R3PtrDst, cb, 1);
59 memcpy((void *)R3PtrDst, pvSrc, cb);
60 }
61 __except(EXCEPTION_EXECUTE_HANDLER)
62 {
63 return VERR_ACCESS_DENIED;
64 }
65 return VINF_SUCCESS;
66}
67
68
69RTR0DECL(bool) RTR0MemUserIsValidAddr(RTR3PTR R3Ptr)
70{
71#ifdef IPRT_TARGET_NT4
72 uintptr_t const uLast = g_puRtMmHighestUserAddress ? *g_puRtMmHighestUserAddress : ~(uintptr_t)0 / 2;
73#else
74 uintptr_t const uLast = (uintptr_t)MM_HIGHEST_USER_ADDRESS;
75#endif
76 return R3Ptr <= uLast;
77}
78
79
80RTR0DECL(bool) RTR0MemKernelIsValidAddr(void *pv)
81{
82#ifdef IPRT_TARGET_NT4
83 uintptr_t const uFirst = g_puRtMmSystemRangeStart ? *g_puRtMmSystemRangeStart : ~(uintptr_t)0 / 2 + 1;
84#else
85 uintptr_t const uFirst = (uintptr_t)MM_SYSTEM_RANGE_START;
86#endif
87 return (uintptr_t)pv >= uFirst;
88}
89
90
91RTR0DECL(bool) RTR0MemAreKrnlAndUsrDifferent(void)
92{
93 return true;
94}
95
96
97RTR0DECL(int) RTR0MemKernelCopyFrom(void *pvDst, void const *pvSrc, size_t cb)
98{
99 if (!RTR0MemKernelIsValidAddr((void *)pvSrc))
100 return VERR_ACCESS_DENIED;
101
102 uint8_t *pbDst = (uint8_t *)pvDst;
103 uint8_t const *pbSrc = (uint8_t const *)pvSrc;
104
105#if 0
106 /*
107 * The try+except stuff does not work for kernel addresses.
108 */
109 __try
110 {
111 while (cb-- > 0)
112 *pbDst++ = *pbSrc++;
113 }
114 __except(EXCEPTION_EXECUTE_HANDLER)
115 {
116 return VERR_ACCESS_DENIED;
117 }
118#else
119 /*
120 * This is the best I can come up with for now: Work page-by-page using MmIsAddressValid.
121 */
122 while (cb > 0)
123 {
124 if (!MmIsAddressValid((PVOID)pbSrc))
125 return VERR_ACCESS_DENIED;
126
127 size_t cbToCopy = (uintptr_t)pbSrc & PAGE_OFFSET_MASK;
128 if (cbToCopy > cb)
129 cbToCopy = cb;
130 cb -= cbToCopy;
131
132 __try /* doesn't work, but can't hurt, right? */
133 {
134 while (cbToCopy-- > 0)
135 *pbDst++ = *pbSrc++;
136 }
137 __except(EXCEPTION_EXECUTE_HANDLER)
138 {
139 return VERR_ACCESS_DENIED;
140 }
141 }
142#endif
143 return VINF_SUCCESS;
144}
145
146
147RTR0DECL(int) RTR0MemKernelCopyTo(void *pvDst, void const *pvSrc, size_t cb)
148{
149 if (!RTR0MemKernelIsValidAddr(pvDst))
150 return VERR_ACCESS_DENIED;
151#if 0
152 uint8_t *pbDst = (uint8_t *)pvDst;
153 uint8_t const *pbSrc = (uint8_t const *)pvSrc;
154# if 0
155 /*
156 * The try+except stuff does not work for kernel addresses.
157 */
158 __try
159 {
160 while (cb-- > 0)
161 *pbDst++ = *pbSrc++;
162 }
163 __except(EXCEPTION_EXECUTE_HANDLER)
164 {
165 return VERR_ACCESS_DENIED;
166 }
167
168# else
169 /*
170 * This is the best I can come up with for now: Work page-by-page using MmIsAddressValid.
171 * Note! MmIsAddressValid does not indicate that it's writable, so we're a bit buggered if it isn't...
172 */
173 while (cb > 0)
174 {
175 if (!MmIsAddressValid((PVOID)pbSrc))
176 return VERR_ACCESS_DENIED;
177
178 size_t cbToCopy = (uintptr_t)pbSrc & PAGE_OFFSET_MASK;
179 if (cbToCopy > cb)
180 cbToCopy = cb;
181 cb -= cbToCopy;
182
183 __try /* doesn't work, but can't hurt, right? */
184 {
185 while (cbToCopy-- > 0)
186 *pbDst++ = *pbSrc++;
187 }
188 __except(EXCEPTION_EXECUTE_HANDLER)
189 {
190 return VERR_ACCESS_DENIED;
191 }
192 }
193# endif
194 return VINF_SUCCESS;
195#else
196 RT_NOREF(pvDst, pvSrc, cb);
197 return VERR_NOT_SUPPORTED;
198#endif
199}
200
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