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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
/********************************************************/
/* ntapi: Native API core library */
/* Copyright (C) 2013--2021 SysDeer Technologies, LLC */
/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
/********************************************************/
#include <psxtypes/psxtypes.h>
#include <ntapi/nt_atomic.h>
#include <ntapi/nt_status.h>
#include <ntapi/nt_object.h>
#include <ntapi/nt_memory.h>
#include <ntapi/nt_thread.h>
#include <ntapi/nt_process.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"
static intptr_t __fork_retry_stats = 0;
static intptr_t __fork_resume_stats = 0;
static int __ipc_memfn(
struct dalist_ex * dlist,
void ** addr,
size_t * alloc_size)
{
(void)dlist;
(void)addr;
(void)alloc_size;
return DALIST_EMEMFN;
}
static int32_t __fastcall __ntapi_tt_fork_finalize(void ** hprocess)
{
int32_t status;
int page;
nt_rtdata * rtdata;
ntapi_internals * __internals;
__internals = __ntapi_internals();
rtdata = __internals->rtdata;
rtdata->cid_parent.process_id = rtdata->cid_self.process_id;
rtdata->cid_parent.thread_id = rtdata->cid_self.thread_id;
rtdata->cid_self.process_id = pe_get_current_process_id();
rtdata->cid_self.thread_id = pe_get_current_thread_id();
if ((status = __ntapi->zw_duplicate_object(
__internals->hprocess,
__internals->hprocess,
__internals->hprocess,
hprocess,0,0,
NT_DUPLICATE_SAME_ATTRIBUTES
|NT_DUPLICATE_SAME_ACCESS)))
return status;
if ((status = dalist_init_ex(
&__internals->ipc_conns,
sizeof(nt_ipc_conn),
NT_ALLOCATION_GRANULARITY,
__ipc_memfn,
DALIST_MEMFN_CUSTOM)))
return status;
dalist_deposit_memory_block(
&__internals->ipc_conns,
__internals->ntapi_img_sec_bss->ipc_buffer,
__NT_BSS_IPC_BUFFER_SIZE);
for (page=0; page<__internals->ipc_page; page++)
dalist_deposit_memory_block(
&__internals->ipc_conns,
__internals->ipc_pages[page],
NT_ALLOCATION_GRANULARITY);
rtdata->hsemctl = 0;
rtdata->hsempid = 0;
rtdata->hmsqctl = 0;
rtdata->hmsqpid = 0;
rtdata->haflctl = 0;
rtdata->haflpid = 0;
rtdata->ipc_keys[0] = 0;
rtdata->ipc_keys[1] = 0;
rtdata->ipc_keys[2] = 0;
rtdata->ipc_keys[3] = 0;
rtdata->ipc_keys[4] = 0;
rtdata->ipc_keys[5] = 0;
return NT_STATUS_SUCCESS;
}
static int32_t __stdcall __fork_thread(void * ctx)
{
intptr_t * pstate;
intptr_t state;
void * hready;
pstate = (intptr_t *)ctx;
state = *pstate;
hready = (void *)state;
at_store(
pstate,
0);
return __ntapi->zw_terminate_thread(
NT_CURRENT_THREAD_HANDLE,
__ntapi->zw_set_event(
hready,0));
}
static int32_t __fastcall __ntapi_tt_fork_child(
void * hresumed,
void * hready,
void ** hthread)
{
int32_t status;
nt_thread_params tparams;
nt_timeout timeout;
nt_timeout zerowait;
intptr_t state;
nt_oa oa;
nt_cid cid;
ntapi_internals * __internals;
oa.len = sizeof(oa);
oa.root_dir = 0;
oa.obj_name = 0;
oa.obj_attr = 0;
oa.sec_desc = &__internals->seq_desc;
oa.sec_qos = &__internals->seq_qos;
cid.process_id = pe_get_current_process_id();
cid.thread_id = pe_get_current_thread_id();
__internals = __ntapi_internals();
status = __ntapi->zw_open_process(
&__internals->hprocess,
NT_PROCESS_ALL_ACCESS,
&oa,&cid);
if (status == NT_STATUS_SUCCESS)
status = __ntapi->zw_open_thread(
hthread,
NT_THREAD_ALL_ACCESS,
&oa,&cid);
if (status) {
__ntapi->zw_set_event(
hresumed,0);
__ntapi->zw_terminate_process(
NT_CURRENT_PROCESS_HANDLE,
status);
}
at_store(
&state,
(intptr_t)hready);
__ntapi->tt_aligned_block_memset(
&tparams,0,sizeof(tparams));
tparams.start = __fork_thread;
tparams.arg = &state;
tparams.stack_size_commit = 0x10000;
tparams.stack_size_reserve = 0x20000;
status = __ntapi->tt_create_local_thread(
&tparams);
__ntapi->zw_set_event(
hresumed,0);
if (status)
__ntapi->zw_terminate_process(
NT_CURRENT_PROCESS_HANDLE,
status);
if (!state) {
__ntapi->zw_close(hresumed);
__ntapi->zw_close(hready);
__ntapi->zw_close(tparams.hthread);
return NT_STATUS_SUCCESS;
}
timeout.quad = (-1) * 10 * 1000 * 250;
status = __ntapi->zw_wait_for_single_object(
hready,
NT_SYNC_NON_ALERTABLE,
&timeout);
if (status == NT_STATUS_SUCCESS) {
__ntapi->zw_close(hresumed);
__ntapi->zw_close(hready);
__ntapi->zw_close(tparams.hthread);
return NT_STATUS_SUCCESS;
}
__ntapi->zw_terminate_thread(
tparams.hthread,
NT_STATUS_MORE_PROCESSING_REQUIRED);
zerowait.quad = 0;
status = __ntapi->zw_wait_for_single_object(
hready,
NT_SYNC_NON_ALERTABLE,
&zerowait);
if (status == NT_STATUS_SUCCESS) {
__ntapi->zw_close(hresumed);
__ntapi->zw_close(hready);
__ntapi->zw_close(tparams.hthread);
return NT_STATUS_SUCCESS;
}
return __ntapi->zw_terminate_process(
NT_CURRENT_PROCESS_HANDLE,
status);
}
static intptr_t __fastcall __ntapi_tt_fork_parent(
void ** hprocess,
void ** hthread,
void * hresumed,
void * hready)
{
int32_t status;
nt_timeout timeout;
nt_timeout zerowait;
uint32_t prev;
__ntapi->zw_wait_for_single_object(
hresumed,
NT_SYNC_NON_ALERTABLE,
0);
timeout.quad = (-1) * 10 * 1000 * 500;
status = __ntapi->zw_wait_for_single_object(
hready,
NT_SYNC_NON_ALERTABLE,
&timeout);
if (status == NT_STATUS_SUCCESS)
return NT_STATUS_SUCCESS;
__ntapi->zw_suspend_thread(
*hthread,&prev);
zerowait.quad = 0;
status = __ntapi->zw_wait_for_single_object(
hready,
NT_SYNC_NON_ALERTABLE,
&zerowait);
if (status == NT_STATUS_SUCCESS) {
at_locked_inc(
&__fork_resume_stats);
__ntapi->zw_resume_thread(
*hthread,0);
return NT_STATUS_SUCCESS;
}
at_locked_inc(
&__fork_retry_stats);
__ntapi->zw_terminate_process(
*hprocess,
status);
__ntapi->zw_close(*hprocess);
__ntapi->zw_close(*hthread);
return status;
}
int32_t __fastcall __ntapi_tt_fork(
__out void ** hprocess,
__out void ** hthread,
__out nt_cid * cid)
{
int32_t status;
void * hresumed;
void * hready;
int i;
if ((status = __ntapi->tt_create_inheritable_event(
&hresumed,
NT_NOTIFICATION_EVENT,
NT_EVENT_NOT_SIGNALED)))
return status;
status = __ntapi->tt_create_inheritable_event(
&hready,
NT_NOTIFICATION_EVENT,
NT_EVENT_NOT_SIGNALED);
if (status) {
__ntapi->zw_close(hresumed);
return status;
}
for (i=0; i<32; i++) {
if (__ntapi->zw_create_user_process)
status = __ntapi_tt_fork_v2(hprocess,hthread,cid);
else
status = __ntapi_tt_fork_v1(hprocess,hthread,cid);
if (status) {
__ntapi->zw_close(hresumed);
__ntapi->zw_close(hready);
return status;
}
if (cid->process_id == 0) {
__ntapi_tt_fork_child(
hresumed,hready,hthread);
return __ntapi_tt_fork_finalize(
hprocess);
}
status = __ntapi_tt_fork_parent(
hprocess,hthread,
hresumed,hready);
if (status == NT_STATUS_SUCCESS) {
__ntapi->zw_close(hresumed);
__ntapi->zw_close(hready);
return NT_STATUS_SUCCESS;
}
}
__ntapi->zw_close(hresumed);
__ntapi->zw_close(hready);
return NT_STATUS_UNSUCCESSFUL;
}
|