forked from ZEROWyt/Patchguard-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rtx.c
198 lines (169 loc) · 5.03 KB
/
Rtx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
*
* Copyright (c) 2015 - 2021 by blindtiger. All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License Version
* 2.0 (the "License")); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. SEe the License
* for the specific language governing rights and limitations under the
* License.
*
* The Initial Developer of the Original Code is blindtiger.
*
*/
#include <defs.h>
#include "Rtx.h"
#include "Ctx.h"
#include "Guard.h"
#include "Scan.h"
void
NTAPI
AsyncDispatcher(
__in PKAPC Apc,
__in PKNORMAL_ROUTINE * NormalRoutine,
__in ptr * NormalContext,
__in ptr * SystemArgument1,
__in ptr * SystemArgument2
)
{
PATX Atx = NULL;
Atx = CONTAINING_RECORD(Apc, ATX, Apc);
Atx->Rtx.Routines.Result = GuardCall(
Atx->Rtx.Routines.KernelRoutine,
Atx->Rtx.Routines.SystemRoutine,
Atx->Rtx.Routines.RundownRoutine,
Atx->Rtx.Routines.NormalRoutine);
KeSetEvent(&Atx->Rtx.Notify, LOW_PRIORITY, FALSE);
}
status
NTAPI
AsyncCall(
__in ptr UniqueThread,
__in_opt PGKERNEL_ROUTINE KernelRoutine,
__in_opt PGSYSTEM_ROUTINE SystemRoutine,
__in_opt PGRUNDOWN_ROUTINE RundownRoutine,
__in_opt PGNORMAL_ROUTINE NormalRoutine
)
{
status Status = STATUS_SUCCESS;
PETHREAD Thread = NULL;
ATX Atx = { 0 };
LARGE_INTEGER Timeout = { 0 };
Status = PsLookupThreadByThreadId(
UniqueThread,
&Thread);
if (TRACE(Status)) {
Atx.Rtx.Routines.KernelRoutine = KernelRoutine;
Atx.Rtx.Routines.SystemRoutine = SystemRoutine;
Atx.Rtx.Routines.RundownRoutine = RundownRoutine;
Atx.Rtx.Routines.NormalRoutine = NormalRoutine;
KeInitializeEvent(
&Atx.Rtx.Notify,
SynchronizationEvent,
FALSE);
if ((u)KeGetCurrentThread() != (u)Thread) {
KeInitializeApc(
&Atx.Apc,
Thread,
OriginalApcEnvironment,
AsyncDispatcher,
NULL,
NULL,
KernelMode,
NULL);
Timeout.QuadPart = Int32x32To64(10, -10 * 1000 * 1000);
if (FALSE != KeInsertQueueApc(
&Atx.Apc,
NULL,
NULL,
LOW_PRIORITY)) {
Status = KeWaitForSingleObject(
&Atx.Rtx.Notify,
Executive,
KernelMode,
FALSE,
&Timeout);
if (STATUS_SUCCESS == Status) {
Status = Atx.Rtx.Routines.Result;
}
}
else {
Status = STATUS_UNSUCCESSFUL;
}
}
else {
AsyncDispatcher(&Atx.Apc, NULL, NULL, NULL, NULL);
Status = Atx.Rtx.Routines.Result;
}
ObDereferenceObject(Thread);
}
return Status;
}
void
NTAPI
IpiDispatcher(
__in PRTX Rtx
)
{
if (-1 == Rtx->Processor) {
GuardCall(
Rtx->Routines.KernelRoutine,
Rtx->Routines.SystemRoutine,
Rtx->Routines.RundownRoutine,
Rtx->Routines.NormalRoutine);
}
else {
if (KeGetCurrentProcessorNumber() == Rtx->Processor) {
Rtx->Routines.Result = GuardCall(
Rtx->Routines.KernelRoutine,
Rtx->Routines.SystemRoutine,
Rtx->Routines.RundownRoutine,
Rtx->Routines.NormalRoutine);
}
}
}
u
NTAPI
IpiSingleCall(
__in_opt PGKERNEL_ROUTINE KernelRoutine,
__in_opt PGSYSTEM_ROUTINE SystemRoutine,
__in_opt PGRUNDOWN_ROUTINE RundownRoutine,
__in_opt PGNORMAL_ROUTINE NormalRoutine
)
{
u Result = 0;
RTX Rtx = { 0 };
Rtx.Processor = KeGetCurrentProcessorNumber();
Rtx.Routines.KernelRoutine = KernelRoutine;
Rtx.Routines.SystemRoutine = SystemRoutine;
Rtx.Routines.RundownRoutine = RundownRoutine;
Rtx.Routines.NormalRoutine = NormalRoutine;
KeIpiGenericCall(
(PKIPI_BROADCAST_WORKER)IpiDispatcher,
(u)&Rtx);
Result = Rtx.Routines.Result;
return Result;
}
void
NTAPI
IpiGenericCall(
__in_opt PGKERNEL_ROUTINE KernelRoutine,
__in_opt PGSYSTEM_ROUTINE SystemRoutine,
__in_opt PGRUNDOWN_ROUTINE RundownRoutine,
__in_opt PGNORMAL_ROUTINE NormalRoutine
)
{
RTX Rtx = { 0 };
Rtx.Processor = -1;
Rtx.Routines.KernelRoutine = KernelRoutine;
Rtx.Routines.SystemRoutine = SystemRoutine;
Rtx.Routines.RundownRoutine = RundownRoutine;
Rtx.Routines.NormalRoutine = NormalRoutine;
KeIpiGenericCall(
(PKIPI_BROADCAST_WORKER)IpiDispatcher,
(u)&Rtx);
}