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
|
/********************************************************/
/* ntapi: Native API core library */
/* Copyright (C) 2013,2014,2015 Z. Gilboa */
/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
/********************************************************/
#include <psxtypes/psxtypes.h>
#include <ntapi/nt_port.h>
#include <ntapi/nt_tty.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"
#include "ntapi_pty.h"
static int32_t __stdcall __ntapi_pty_open_close(
nt_pty * pty,
nt_iosb * iosb,
int32_t opcode)
{
int32_t status;
nt_pty_fd_msg msg;
__ntapi->tt_aligned_block_memset(&msg,0,sizeof(msg));
msg.header.msg_type = NT_LPC_NEW_MESSAGE;
msg.header.data_size = sizeof(msg.data);
msg.header.msg_size = sizeof(msg);
msg.data.ttyinfo.opcode = opcode;
msg.data.fdinfo.hpty = pty->hpty;
msg.data.fdinfo.access = pty->access;
msg.data.fdinfo.flags = pty->flags;
msg.data.fdinfo.share = pty->share;
msg.data.fdinfo.options = pty->options;
msg.data.fdinfo.luid.high = pty->luid.high;
msg.data.fdinfo.luid.low = pty->luid.low;
__ntapi_tt_guid_copy(
&msg.data.fdinfo.guid,
&pty->guid);
if ((status = __ntapi->zw_request_wait_reply_port(pty->hport,&msg,&msg)))
return status;
else if (msg.data.ttyinfo.status)
return msg.data.ttyinfo.status;
pty->hpty = msg.data.fdinfo.hpty;
pty->section = msg.data.fdinfo.section;
pty->section_size = msg.data.fdinfo.section_size;
pty->luid.high = msg.data.fdinfo.luid.high;
pty->luid.low = msg.data.fdinfo.luid.low;
iosb->status = msg.data.ttyinfo.status;
iosb->info = 0;
return NT_STATUS_SUCCESS;
}
static int32_t __fastcall __ntapi_pty_free(nt_pty * pty)
{
void * addr;
size_t size;
/* unmap section */
if (pty->section_addr)
__ntapi->zw_unmap_view_of_section(
NT_CURRENT_PROCESS_HANDLE,
pty->section_addr);
/* free control block */
addr = pty->addr;
size = pty->size;
return __ntapi->zw_free_virtual_memory(
NT_CURRENT_PROCESS_HANDLE,
&addr,
&size,
NT_MEM_RELEASE);
}
static int32_t __fastcall __ntapi_pty_fail(nt_pty * pty,int32_t status)
{
__ntapi_pty_free(pty);
return status;
}
static int32_t __fastcall __ntapi_pty_alloc(nt_pty ** pty)
{
int32_t status;
nt_pty * ctx;
size_t ctx_size;
/* allocate control block */
ctx = 0;
ctx_size = sizeof(nt_pty);
if ((status = __ntapi->zw_allocate_virtual_memory(
NT_CURRENT_PROCESS_HANDLE,
(void **)&ctx,
0,&ctx_size,
NT_MEM_COMMIT,
NT_PAGE_READWRITE)))
return status;
/* init control block */
__ntapi->tt_aligned_block_memset(
ctx,0,ctx_size);
ctx->addr = ctx;
ctx->size = ctx_size;
*pty = ctx;
return NT_STATUS_SUCCESS;
}
static int32_t __ntapi_pty_connect(
void * hport,
nt_pty * ctx,
nt_iosb * iosb)
{
int32_t status;
ctx->hport = hport
? hport
: __ntapi_internals()->hport_tty_session;
/* request */
iosb = iosb ? iosb : &ctx->iosb;
if ((status = __ntapi_pty_open_close(ctx,iosb,NT_TTY_PTY_OPEN)))
return __ntapi_pty_fail(ctx,status);
/* map section */
if ((status = __ntapi->zw_map_view_of_section(
ctx->section,
NT_CURRENT_PROCESS_HANDLE,
&ctx->section_addr,
0,ctx->section_size,
0,&ctx->section_size,
NT_VIEW_UNMAP,0,
NT_PAGE_READWRITE)))
return __ntapi_pty_fail(ctx,status);
/* assume conforming clients, config for single lock try */
__ntapi->tt_sync_block_init(&ctx->sync[__PTY_READ],0,0,1,0,0);
__ntapi->tt_sync_block_init(&ctx->sync[__PTY_WRITE],0,0,1,0,0);
return NT_STATUS_SUCCESS;
}
int32_t __stdcall __ntapi_pty_open(
void * hport,
nt_pty ** pty,
uint32_t desired_access,
nt_object_attributes* obj_attr,
nt_iosb * iosb,
uint32_t share_access,
uint32_t open_options)
{
int32_t status;
uint32_t hash;
nt_guid guid;
nt_uuid_str_utf16 * guid_str;
nt_pty * ctx;
if (!obj_attr || !obj_attr->obj_name || !obj_attr->obj_name->buffer)
return NT_STATUS_INVALID_PARAMETER;
if (obj_attr->obj_name->strlen != __DEVICE_PATH_PREFIX_LEN + sizeof(nt_guid_str_utf16))
return NT_STATUS_OBJECT_PATH_INVALID;
hash = __ntapi->tt_buffer_crc32(
0,
obj_attr->obj_name->buffer,
__DEVICE_PATH_PREFIX_LEN);
if (hash != __DEVICE_PATH_PREFIX_HASH)
return NT_STATUS_OBJECT_PATH_INVALID;
guid_str = (nt_uuid_str_utf16 *)
((uintptr_t)obj_attr->obj_name->buffer + __DEVICE_PATH_PREFIX_LEN);
if (__ntapi->tt_utf16_string_to_guid(guid_str,&guid))
return NT_STATUS_OBJECT_NAME_INVALID;
/* control block */
if ((status = __ntapi_pty_alloc(&ctx)))
return status;
__ntapi_tt_guid_copy(
&ctx->guid,
&guid);
ctx->access = desired_access;
ctx->flags = obj_attr->obj_attr;
ctx->share = share_access;
ctx->options = open_options;
/* pts */
if (obj_attr->root_dir) {
ctx->luid.high = ((nt_pty *)obj_attr->root_dir)->luid.high;
ctx->luid.low = ((nt_pty *)obj_attr->root_dir)->luid.low;
}
if ((status = __ntapi_pty_connect(hport,ctx,iosb)))
return status;
*pty = ctx;
return NT_STATUS_SUCCESS;
}
int32_t __stdcall __ntapi_pty_inherit(
__in void * hport,
__out nt_pty ** pty,
__in nt_pty_client_info * client_info)
{
int32_t status;
nt_iosb iosb;
nt_pty_inherit_info inherit;
nt_pty * ctx;
inherit.any[0] = client_info->any[0];
inherit.any[1] = client_info->any[1];
inherit.any[2] = client_info->any[2];
inherit.any[3] = client_info->any[3];
if ((status = __ntapi_pty_query(
hport,&iosb,
&inherit,sizeof(inherit),
NT_PTY_INHERIT_INFORMATION)))
return status;
/* control block */
if ((status = __ntapi_pty_alloc(&ctx)))
return status;
__ntapi_tt_guid_copy(
&ctx->guid,
&inherit.guid);
ctx->access = inherit.access;
ctx->flags = inherit.flags;
ctx->share = inherit.share;
ctx->options = inherit.options;
ctx->luid.low = inherit.luid.low;
ctx->luid.high = inherit.luid.high;
if ((status = __ntapi_pty_connect(hport,ctx,&iosb)))
return status;
*pty = ctx;
return NT_STATUS_SUCCESS;
}
int32_t __stdcall __ntapi_pty_reopen(
__in void * hport,
__in nt_pty * pty)
{
return __ntapi_pty_connect(hport,pty,0);
}
int32_t __stdcall __ntapi_pty_close(nt_pty * pty)
{
if (!pty || (pty->addr != pty))
return NT_STATUS_INVALID_PARAMETER;
__ntapi_pty_open_close(
pty,&pty->iosb,NT_TTY_PTY_CLOSE);
return __ntapi_pty_free(pty);
}
|