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
|
/********************************************************/
/* ntapi: Native API core library */
/* Copyright (C) 2013--2017 Z. Gilboa */
/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
/********************************************************/
#include <ntapi/nt_atomic.h>
#include <ntapi/nt_status.h>
#include <ntapi/nt_thread.h>
#include <ntapi/nt_port.h>
#include <ntapi/nt_daemon.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"
int32_t __attr_hidden__ __ntapi_tt_seh_frame(void *, void *, void *, int32_t (*)(nt_daemon_params *));
static int32_t __stdcall __ntapi_dsr_once(nt_daemon_params * params);
int32_t __stdcall __ntapi_dsr_init(nt_daemon_params * params)
{
int32_t status;
nt_thread_params tparams;
/* report status */
at_store_32(
¶ms->exit_code_daemon_loop,
NT_STATUS_PENDING);
at_store_32(
¶ms->exit_code_daemon_start,
NT_STATUS_PENDING);
at_store_32(
¶ms->exit_code_internal_client,
NT_STATUS_PENDING);
/* port_keys */
if (params->flags & NT_DSR_INIT_GENERATE_KEYS)
if ((status = __ntapi->tt_port_generate_keys(params->port_keys)))
return status;
/* port_name_keys */
if (params->flags & NT_DSR_INIT_FORMAT_KEYS)
__ntapi->tt_port_format_keys(
params->port_keys,
params->port_name_keys);
/* 'port-created' event */
if (!params->hevent_daemon_port) {
if ((status = __ntapi->tt_create_private_event(
¶ms->hevent_daemon_port,
NT_NOTIFICATION_EVENT,
NT_EVENT_NOT_SIGNALED)))
return status;
if (params->pevent_daemon_port)
at_store(
(intptr_t *)params->pevent_daemon_port,
(intptr_t)params->hevent_daemon_port);
}
/* 'daemon-is-ready' event */
if (!params->hevent_daemon_ready) {
if ((status = __ntapi->tt_create_private_event(
¶ms->hevent_daemon_ready,
NT_NOTIFICATION_EVENT,
NT_EVENT_NOT_SIGNALED)))
return status;
if (params->pevent_daemon_ready)
at_store(
(intptr_t *)params->pevent_daemon_ready,
(intptr_t)params->hevent_daemon_ready);
}
/* 'internal-client-is-ready' event */
if (!params->hevent_internal_client_ready) {
if ((status = __ntapi->tt_create_private_event(
¶ms->hevent_internal_client_ready,
NT_NOTIFICATION_EVENT,
NT_EVENT_NOT_SIGNALED)))
return status;
if (params->pevent_internal_client_ready)
at_store(
(intptr_t *)params->pevent_internal_client_ready,
(intptr_t)params->hevent_internal_client_ready);
}
/* memory barrier */
__ntapi->tt_aligned_block_memlock(
params,
sizeof(*params));
__ntapi->tt_aligned_block_memlock(
params->port_name,
sizeof(*params->port_name));
/* daemon dedicated thread: general parameters */
__ntapi->tt_aligned_block_memset(
&tparams,0,sizeof(tparams));
tparams.start = (nt_thread_start_routine *)__ntapi_dsr_start;
tparams.arg = params;
/* daemon dedicated thread: stack parameters (optional) */
tparams.stack_size_commit = params->stack_size_commit;
tparams.stack_size_reserve = params->stack_size_reserve;
tparams.stack_info = params->stack_info;
/* daemon dedicated thread: create */
if ((status = __ntapi->tt_create_local_thread(&tparams)))
return status;
at_store(
(intptr_t *)¶ms->hthread_daemon_loop,
(intptr_t)tparams.hthread);
/* daemon dedicated thread: actual stack size */
params->stack_size_commit = tparams.stack_size_commit;
params->stack_size_reserve = tparams.stack_size_reserve;
/* wait for the port to be created */
if ((status = __ntapi->zw_wait_for_single_object(
params->hevent_daemon_port,
NT_SYNC_NON_ALERTABLE,0)))
return status;
/* overcome potential scheduling bug in recent (10.0) kernel */
while (params->exit_code_daemon_start == NT_STATUS_PENDING)
__ntapi->zw_delay_execution(
NT_SYNC_NON_ALERTABLE,
&(nt_timeout){{0,0}});
/* verify the port's successful creation */
if (at_locked_xadd_32(¶ms->exit_code_daemon_start,0))
return NT_STATUS_PORT_NOT_SET;
/* internal connection task-specific thread */
__ntapi->tt_aligned_block_memset(
&tparams,0,sizeof(tparams));
tparams.start = (nt_thread_start_routine *)__ntapi_dsr_internal_client_connect;
tparams.arg = params;
if ((status = __ntapi->tt_create_local_thread(&tparams)))
return status;
at_store(
(intptr_t *)¶ms->hthread_internal_client,
(intptr_t)tparams.hthread);
/* wait for the task-specific thread to exit */
if ((status = __ntapi->zw_wait_for_single_object(
params->hthread_internal_client,
NT_SYNC_NON_ALERTABLE,0)))
return status;
/* overcome potential scheduling bug in recent (10.0) kernel */
while (params->exit_code_internal_client == NT_STATUS_PENDING)
__ntapi->zw_delay_execution(
NT_SYNC_NON_ALERTABLE,
&(nt_timeout){{0,0}});
/* verify a successful internal connection */
if (at_locked_xadd_32(¶ms->exit_code_internal_client,0))
return params->exit_code_internal_client;
/* all_done */
__ntapi->zw_set_event(
params->hevent_daemon_ready,
0);
if (params->flags & NT_DSR_INIT_CLOSE_EVENTS) {
__ntapi->zw_close(params->hevent_daemon_port);
__ntapi->zw_close(params->hevent_daemon_ready);
__ntapi->zw_close(params->hevent_internal_client_ready);
}
return NT_STATUS_SUCCESS;;
}
/* __ntapi_dsr_start executes in the daemon's dedicated thread */
static int32_t __ntapi_dsr_start_impl(nt_daemon_params * params)
{
int32_t status;
void * ctx;
int32_t (*loop)(void *);
ctx = params->daemon_loop_context;
loop = params->daemon_loop_routine;
if ((status = __ntapi_dsr_once(params)))
return status;
if ((status = __ntapi_dsr_create_port(params)))
return status;
if ((status = __ntapi_dsr_connect_internal_client(params)))
return status;
if ((status = loop(ctx)))
return status;
return NT_STATUS_SUCCESS;
}
static int32_t __ntapi_dsr_start_routine(nt_daemon_params * params)
{
at_store_32(
¶ms->exit_code_daemon_loop,
NT_STATUS_MORE_PROCESSING_REQUIRED);
return __ntapi->zw_terminate_thread(
NT_CURRENT_THREAD_HANDLE,
__ntapi_dsr_start_impl(params));
}
int32_t __stdcall __ntapi_dsr_start(nt_daemon_params * params)
{
return __ntapi_tt_seh_frame(
params,0,0,
__ntapi_dsr_start_routine);
}
/* __ntapi_dsr_once executes in the daemon's dedicated thread */
static int32_t __stdcall __ntapi_dsr_once(nt_daemon_params * params)
{
int32_t status;
if (!params->daemon_once_routine)
return NT_STATUS_SUCCESS;
if ((status = params->daemon_once_routine(params->daemon_loop_context)))
at_store_32(
¶ms->exit_code_daemon_start,
status);
return status;
}
/* __ntapi_dsr_create_port executes in the daemon's dedicated thread */
static int32_t __stdcall __ntapi_dsr_create_port_exit(
nt_daemon_params * params,
int32_t status)
{
at_store_32(
¶ms->exit_code_daemon_start,
status);
__ntapi->zw_set_event(
params->hevent_daemon_port,
0);
return status;
}
int32_t __stdcall __ntapi_dsr_create_port(nt_daemon_params * params)
{
int32_t status;
nt_object_attributes oa;
nt_security_quality_of_service sqos;
nt_unicode_string server_name;
/* init server_name */
server_name.strlen = (uint16_t)__ntapi->tt_string_null_offset_short((int16_t *)params->port_name);
server_name.maxlen = 0;
server_name.buffer = params->port_name;
/* init security structure */
sqos.length = sizeof(sqos);
sqos.impersonation_level = NT_SECURITY_IMPERSONATION;
sqos.context_tracking_mode = NT_SECURITY_TRACKING_DYNAMIC;
sqos.effective_only = 1;
/* init the port's object attributes */
oa.len = sizeof(oa);
oa.root_dir = 0;
oa.obj_name = &server_name;
oa.obj_attr = 0;
oa.sec_desc = params->port_sd;
oa.sec_qos = &sqos;
/* create the port */
if ((status = __ntapi->zw_create_port(
¶ms->hport_daemon,
&oa,0,(uint32_t)params->port_msg_size,
0)))
return __ntapi_dsr_create_port_exit(
params,status);
/* return port info */
if (params->pport_daemon)
at_store(
(intptr_t *)params->pport_daemon,
(intptr_t)params->hport_daemon);
/* set status */
at_store_32(
¶ms->exit_code_daemon_start,
NT_STATUS_SUCCESS);
/* signal the port-created event */
__ntapi->zw_set_event(
params->hevent_daemon_port,
0);
return NT_STATUS_SUCCESS;
}
|