kernel
Loading...
Searching...
No Matches
probe.c
Go to the documentation of this file.
1/*++
2
3Module Name:
4
5 probe.c
6
7Purpose:
8
9 This translation unit contains the implementation of probing user addresses.
10
11Author:
12
13 slep (Matanel) 2025.
14
15Revision History:
16
17--*/
18
20#include "../../assert.h"
21
24 IN const void* Address,
25 IN size_t Length,
26 IN uint32_t Alignment
27)
28
29/*++
30
31 Routine description:
32
33 Checks if the given user address is within the correct bounds and alignment of access.
34
35 Arguments:
36
37 [IN] const void* Address - The user address to check.
38 [IN] size_t Length - Length in bytes of user mode buffer.
39 [IN] uint32_t Alignment - Required alignment (in bytes) of user mode buffer. (1 for char, 2 for word, 4 for int, 8 for long long)
40
41 Return Values:
42
43 MTSTATUS Code.
44
45 MT_SUCCESS - Address is fine and meets boundaries and length + alignment.
46 MT_DATAYPE_MISALIGNMENT - The address given is not aligned properly with the datatype alignment given.
47 MT_ACCESS_VIOLATION - The address given is not within boundaries of the user mode virtual address range.
48
49--*/
50
51{
52 if (!Address) return MT_ACCESS_VIOLATION;
53 // Standard assertion to check if alignment meets function requirements. (and CPU)
54 assert((Alignment == 1) || (Alignment == 2) || (Alignment == 4) || (Alignment == 8));
55
56 // Check Alignment
57 if (((uint64_t)Address & (Alignment - 1)) != 0) {
59 }
60
61 if (Length != 0) {
62 uint64_t Start = (uint64_t)Address;
63 uint64_t End = Start + Length; // Integer addition, exact bytes
64
65 // Check Overflow (Wrap around)
66 if (End < Start) {
68 }
69
70 // Check against Highest User Address
71 // This should not be the MmUserProbeAddress, as the stack lives above that address, and if a user gives something from his stack there, it would resolve in an MT_ACCESS_VIOLATION.
72 if (End > (uint64_t)MmHighestUserAddress) {
74 }
75 }
76 return MT_SUCCESS;
77}
#define IN
Definition annotations.h:8
#define assert(...)
Definition assert.h:57
uint64_t Address
Definition mh.h:4
uint32_t Length
Definition mh.h:6
#define MT_SUCCESS
Definition mtstatus.h:22
#define MT_DATATYPE_MISALIGNMENT
Definition mtstatus.h:135
int32_t MTSTATUS
Definition mtstatus.h:12
#define MT_ACCESS_VIOLATION
Definition mtstatus.h:131
MTSTATUS ProbeForRead(IN const void *Address, IN size_t Length, IN uint32_t Alignment)
Definition probe.c:23
uintptr_t MmHighestUserAddress
Definition process.c:32