1 #ifndef INCLUDE_REACTOS_CAPTURE_H
2 #define INCLUDE_REACTOS_CAPTURE_H
4 #if ! defined(_NTOSKRNL_) && ! defined(_WIN32K_)
5 #error Header intended for use by NTOSKRNL/WIN32K only!
9 extern ULONG_PTR MmUserProbeAddress
;
12 static const UNICODE_STRING __emptyUnicodeString
= {0};
13 static const LARGE_INTEGER __emptyLargeInteger
= {{0, 0}};
14 static const ULARGE_INTEGER __emptyULargeInteger
= {{0, 0}};
15 static const IO_STATUS_BLOCK __emptyIoStatusBlock
= {{0}, 0};
19 * NOTE: NTOSKRNL unfortunately doesn't export RtlRaiseStatus!
21 VOID NTAPI
W32kRaiseStatus(NTSTATUS Status
);
22 #define RtlRaiseStatus W32kRaiseStatus
26 * NOTE: Alignment of the pointers is not verified!
28 #define ProbeForWriteGenericType(Ptr, Type) \
30 if ((ULONG_PTR)(Ptr) + sizeof(Type) - 1 < (ULONG_PTR)(Ptr) || \
31 (ULONG_PTR)(Ptr) + sizeof(Type) - 1 >= (ULONG_PTR)MmUserProbeAddress) { \
32 RtlRaiseStatus (STATUS_ACCESS_VIOLATION); \
34 *(volatile Type *)(Ptr) = *(volatile Type *)(Ptr); \
37 #define ProbeForWriteBoolean(Ptr) ProbeForWriteGenericType(Ptr, BOOLEAN)
38 #define ProbeForWriteUchar(Ptr) ProbeForWriteGenericType(Ptr, UCHAR)
39 #define ProbeForWriteChar(Ptr) ProbeForWriteGenericType(Ptr, CHAR)
40 #define ProbeForWriteUshort(Ptr) ProbeForWriteGenericType(Ptr, USHORT)
41 #define ProbeForWriteShort(Ptr) ProbeForWriteGenericType(Ptr, SHORT)
42 #define ProbeForWriteUlong(Ptr) ProbeForWriteGenericType(Ptr, ULONG)
43 #define ProbeForWriteLong(Ptr) ProbeForWriteGenericType(Ptr, LONG)
44 #define ProbeForWriteUint(Ptr) ProbeForWriteGenericType(Ptr, UINT)
45 #define ProbeForWriteInt(Ptr) ProbeForWriteGenericType(Ptr, INT)
46 #define ProbeForWriteUlonglong(Ptr) ProbeForWriteGenericType(Ptr, ULONGLONG)
47 #define ProbeForWriteLonglong(Ptr) ProbeForWriteGenericType(Ptr, LONGLONG)
48 #define ProbeForWritePointer(Ptr) ProbeForWriteGenericType(Ptr, PVOID)
49 #define ProbeForWriteHandle(Ptr) ProbeForWriteGenericType(Ptr, HANDLE)
50 #define ProbeForWriteLangid(Ptr) ProbeForWriteGenericType(Ptr, LANGID)
51 #define ProbeForWriteSize_t(Ptr) ProbeForWriteGenericType(Ptr, SIZE_T)
52 #define ProbeForWriteLargeInteger(Ptr) ProbeForWriteGenericType(&((PLARGE_INTEGER)Ptr)->QuadPart, LONGLONG)
53 #define ProbeForWriteUlargeInteger(Ptr) ProbeForWriteGenericType(&((PULARGE_INTEGER)Ptr)->QuadPart, ULONGLONG)
54 #define ProbeForWriteUnicodeString(Ptr) ProbeForWriteGenericType((PUNICODE_STRING)Ptr, UNICODE_STRING)
55 #define ProbeForWriteIoStatusBlock(Ptr) ProbeForWriteGenericType((PIO_STATUS_BLOCK)Ptr, IO_STATUS_BLOCK)
57 #define ProbeForReadGenericType(Ptr, Type, Default) \
58 (((ULONG_PTR)(Ptr) + sizeof(Type) - 1 < (ULONG_PTR)(Ptr) || \
59 (ULONG_PTR)(Ptr) + sizeof(Type) - 1 >= (ULONG_PTR)MmUserProbeAddress) ? \
60 ExRaiseStatus (STATUS_ACCESS_VIOLATION), Default : \
61 *(const volatile Type *)(Ptr))
63 #define ProbeForReadBoolean(Ptr) ProbeForReadGenericType(Ptr, BOOLEAN, FALSE)
64 #define ProbeForReadUchar(Ptr) ProbeForReadGenericType(Ptr, UCHAR, 0)
65 #define ProbeForReadChar(Ptr) ProbeForReadGenericType(Ptr, CHAR, 0)
66 #define ProbeForReadUshort(Ptr) ProbeForReadGenericType(Ptr, USHORT, 0)
67 #define ProbeForReadShort(Ptr) ProbeForReadGenericType(Ptr, SHORT, 0)
68 #define ProbeForReadUlong(Ptr) ProbeForReadGenericType(Ptr, ULONG, 0)
69 #define ProbeForReadLong(Ptr) ProbeForReadGenericType(Ptr, LONG, 0)
70 #define ProbeForReadUint(Ptr) ProbeForReadGenericType(Ptr, UINT, 0)
71 #define ProbeForReadInt(Ptr) ProbeForReadGenericType(Ptr, INT, 0)
72 #define ProbeForReadUlonglong(Ptr) ProbeForReadGenericType(Ptr, ULONGLONG, 0)
73 #define ProbeForReadLonglong(Ptr) ProbeForReadGenericType(Ptr, LONGLONG, 0)
74 #define ProbeForReadPointer(Ptr) ProbeForReadGenericType(Ptr, PVOID, NULL)
75 #define ProbeForReadHandle(Ptr) ProbeForReadGenericType(Ptr, HANDLE, NULL)
76 #define ProbeForReadLangid(Ptr) ProbeForReadGenericType(Ptr, LANGID, 0)
77 #define ProbeForReadSize_t(Ptr) ProbeForReadGenericType(Ptr, SIZE_T, 0)
78 #define ProbeForReadLargeInteger(Ptr) ProbeForReadGenericType((const LARGE_INTEGER *)(Ptr), LARGE_INTEGER, __emptyLargeInteger)
79 #define ProbeForReadUlargeInteger(Ptr) ProbeForReadGenericType((const ULARGE_INTEGER *)(Ptr), ULARGE_INTEGER, __emptyULargeInteger)
80 #define ProbeForReadUnicodeString(Ptr) ProbeForReadGenericType((const UNICODE_STRING *)(Ptr), UNICODE_STRING, __emptyUnicodeString)
81 #define ProbeForReadIoStatusBlock(Ptr) ProbeForReadGenericType((const IO_STATUS_BLOCK *)(Ptr), IO_STATUS_BLOCK, __emptyIoStatusBlock)
83 #define ProbeAndZeroHandle(Ptr) \
85 if ((ULONG_PTR)(Ptr) + sizeof(HANDLE) - 1 < (ULONG_PTR)(Ptr) || \
86 (ULONG_PTR)(Ptr) + sizeof(HANDLE) - 1 >= (ULONG_PTR)MmUserProbeAddress) { \
87 RtlRaiseStatus (STATUS_ACCESS_VIOLATION); \
89 *(volatile HANDLE *)(Ptr) = NULL; \
93 * Inlined Probing Macros
100 ProbeArrayForRead(IN
const VOID
*ArrayPtr
,
107 /* Check for integer overflow */
108 ArraySize
= ItemSize
* ItemCount
;
109 if (ArraySize
/ ItemSize
!= ItemCount
)
111 RtlRaiseStatus (STATUS_INVALID_PARAMETER
);
114 /* Probe the array */
115 ProbeForRead(ArrayPtr
,
123 ProbeArrayForWrite(IN OUT PVOID ArrayPtr
,
130 /* Check for integer overflow */
131 ArraySize
= ItemSize
* ItemCount
;
132 if (ArraySize
/ ItemSize
!= ItemCount
)
134 RtlRaiseStatus (STATUS_INVALID_PARAMETER
);
137 /* Probe the array */
138 ProbeForWrite(ArrayPtr
,
142 #endif /* _WIN32K_ */
147 ProbeAndCaptureUnicodeString(OUT PUNICODE_STRING Dest
,
148 IN KPROCESSOR_MODE CurrentMode
,
149 IN
const UNICODE_STRING
*UnsafeSrc
)
151 NTSTATUS Status
= STATUS_SUCCESS
;
152 WCHAR
*Buffer
= NULL
;
153 ASSERT(Dest
!= NULL
);
155 /* Probe the structure and buffer*/
156 if(CurrentMode
!= KernelMode
)
160 *Dest
= ProbeForReadUnicodeString(UnsafeSrc
);
161 if(Dest
->Buffer
!= NULL
)
163 if (Dest
->Length
!= 0)
165 ProbeForRead(Dest
->Buffer
,
169 /* Allocate space for the buffer */
170 Buffer
= ExAllocatePoolWithTag(PagedPool
,
171 Dest
->Length
+ sizeof(WCHAR
),
172 TAG('U', 'S', 'T', 'R'));
175 Status
= STATUS_INSUFFICIENT_RESOURCES
;
180 RtlCopyMemory(Buffer
, Dest
->Buffer
, Dest
->Length
);
181 Buffer
[Dest
->Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
183 /* Set it as the buffer */
184 Dest
->Buffer
= Buffer
;
185 Dest
->MaximumLength
= Dest
->Length
+ sizeof(WCHAR
);
189 /* sanitize structure */
190 Dest
->MaximumLength
= 0;
196 /* sanitize structure */
198 Dest
->MaximumLength
= 0;
203 /* Free allocated resources and zero the destination string */
209 Dest
->MaximumLength
= 0;
212 /* Return the error code */
213 Status
= _SEH_GetExceptionCode();
219 /* Just copy the UNICODE_STRING structure, don't allocate new memory!
220 We trust the caller to supply valid pointers and data. */
231 ReleaseCapturedUnicodeString(IN PUNICODE_STRING CapturedString
,
232 IN KPROCESSOR_MODE CurrentMode
)
234 if(CurrentMode
!= KernelMode
&& CapturedString
->Buffer
!= NULL
)
236 ExFreePool(CapturedString
->Buffer
);
240 #endif /* INCLUDE_REACTOS_CAPTURE_H */