summaryrefslogtreecommitdiffhomepage
path: root/src/system/ntapi_tt_get_system_directory.c
blob: 424281599bfb9fde82ee7107f3799af5301768de (plain)
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
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2016  Z. Gilboa                 */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#include <psxtypes/psxtypes.h>
#include <pemagine/pemagine.h>
#include <ntapi/nt_sysinfo.h>
#include <ntapi/nt_memory.h>
#include <ntapi/ntapi.h>
#include <ntapi/nt_atomic.h>
#include "ntapi_impl.h"

int32_t __stdcall __ntapi_tt_get_system_directory_native_path(
	__out	nt_mem_sec_name *	buffer,
	__in	uint32_t		buffer_size,
	__in	wchar16_t *		base_name,
	__in	uint32_t		base_name_size,
	__out	nt_unicode_string *	nt_path		__optional)
{
	int32_t			status;
	wchar16_t *		wch_src;
	wchar16_t *		wch_dst;
	wchar16_t *		wch_cap;
	size_t			maxlen_saved;
	size_t			info_size;

	/* validation */
	if (!buffer || !buffer_size)
		return NT_STATUS_BUFFER_TOO_SMALL;
	else if (base_name && !base_name_size)
		return NT_STATUS_INVALID_PARAMETER_MIX;

	/* init buffer */
	buffer->section_name.strlen = 0;
	buffer->section_name.maxlen = (uint16_t)(buffer_size - sizeof(nt_unicode_string));
	buffer->section_name.buffer = buffer->section_name_buffer;

	maxlen_saved = buffer->section_name.maxlen;
	info_size    = 0;

	status = __ntapi->zw_query_virtual_memory(
		NT_CURRENT_PROCESS_HANDLE,
		pe_get_ntdll_module_handle(),
		NT_MEMORY_SECTION_NAME,
		buffer,
		buffer_size,
		&info_size);

	if (status != NT_STATUS_SUCCESS)
		return status;

	/* find directory portion */
	wch_dst = buffer->section_name.buffer + (buffer->section_name.strlen / sizeof(wchar16_t));
	wch_dst--;

	while ((*wch_dst != '\\') && (wch_dst > buffer->section_name.buffer))
		wch_dst--;

	if (wch_dst == buffer->section_name.buffer)
		return NT_STATUS_INTERNAL_ERROR;

	/* base_name */
	if (base_name) {
		wch_dst++;
		wch_src	= base_name;
		wch_cap = (wchar16_t *)((uintptr_t)wch_dst + base_name_size);

		if ((uintptr_t)wch_cap - (uintptr_t)(buffer->section_name.buffer) > maxlen_saved)
			return NT_STATUS_BUFFER_TOO_SMALL;

		while (wch_dst < wch_cap) {
			*wch_dst = *wch_src;
			wch_dst++;
			wch_src++;
		}
	}

	/* null termination */
	*wch_dst = 0;

	/* nt_path */
	if (nt_path)
		__ntapi->rtl_init_unicode_string(
			nt_path,
			buffer->section_name.buffer);

	return NT_STATUS_SUCCESS;
}


int32_t __stdcall __ntapi_tt_get_system_directory_handle(
	__out	void **			hsysdir,
	__out	nt_mem_sec_name *	buffer		__optional,
	__in	uint32_t		buffer_size	__optional)
{
	int32_t			status;
	nt_oa			oa;
	nt_iosb			iosb;
	nt_unicode_string	path;
	char			_buffer[256];

	/* validation */
	if (!hsysdir)
		return NT_STATUS_INVALID_PARAMETER_1;
	else if (buffer_size && buffer_size < 0x20)
		return NT_STATUS_BUFFER_TOO_SMALL;

	/* buffer */
	if (!buffer) {
		buffer		= (nt_mem_sec_name *)_buffer;
		buffer_size	= sizeof(_buffer);
		__ntapi->tt_aligned_block_memset(buffer,0,sizeof(buffer));
	}

	/* sysdir path */
	status = __ntapi_tt_get_system_directory_native_path(
		buffer,
		buffer_size,
		(wchar16_t *)0,
		0,
		&path);

	if (status != NT_STATUS_SUCCESS)
		return status;

	/* oa */
	oa.len = sizeof(nt_oa);
	oa.root_dir = (void *)0;
	oa.obj_name = &path;
	oa.obj_attr = 0;
	oa.sec_desc = 0;
	oa.sec_qos = 0;

	/* open file/folder */
	status = __ntapi->zw_open_file(
		hsysdir,
		NT_SEC_SYNCHRONIZE | NT_FILE_READ_ATTRIBUTES | NT_FILE_READ_ACCESS,
		&oa,
		&iosb,
		NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE,
		NT_FILE_DIRECTORY_FILE | NT_FILE_SYNCHRONOUS_IO_ALERT);

	return status;
}


int32_t __stdcall __ntapi_tt_get_system_directory_dos_path(
	__in	void *			hsysdir		__optional,
	__out	wchar16_t *		buffer,
	__in	uint32_t		buffer_size,
	__in	wchar16_t *		base_name,
	__in	uint32_t		base_name_size,
	__out	nt_unicode_string *	nt_path		__optional)
{
	int32_t			status;
	nt_statfs		statfs;
	wchar16_t *		wch;
	wchar16_t *		wch_src;
	wchar16_t *		wch_cap;
	nt_iosb			iosb;
	nt_fni *		fni;
	uint32_t		fni_length;

	/* validation */
	if (!buffer)
		return NT_STATUS_INVALID_PARAMETER_2;

	/* hsysdir */
	if (!hsysdir) {
		status = __ntapi_tt_get_system_directory_handle(
			&hsysdir,
			(nt_mem_sec_name *)buffer,
			buffer_size);

		if (status != NT_STATUS_SUCCESS)
			return status;
	}

	/* statfs */
	status = __ntapi->tt_statfs(
			hsysdir,
			&statfs,sizeof(statfs),
			(uintptr_t *)buffer,buffer_size,
			NT_STATFS_DOS_DRIVE_LETTER);

	if (status != NT_STATUS_SUCCESS)
		return status;

	/* dos path name (always shorter than the native path, so buffer_size must be ok) */
	wch = buffer;
	*wch = '\\'; wch++;
	*wch = '?';  wch++;
	*wch = '?';  wch++;
	*wch = '\\'; wch++;
	*wch = statfs.nt_drive_letter; wch++;
	*wch = ':';  wch++;

	/* alignment */
	fni  = (nt_fni *)((uintptr_t)buffer + 0x10);

	status = __ntapi->zw_query_information_file(
		hsysdir,
		&iosb,
		fni,
		buffer_size - 8 * sizeof(wchar16_t),
		NT_FILE_NAME_INFORMATION);

	if (status != NT_STATUS_SUCCESS)
		return status;

	/* fni->file_name_length: save */
	fni_length = fni->file_name_length;

	/* overwrite */
	wch_src  = fni->file_name;
	wch_cap  = (wchar16_t *)((uintptr_t)wch_src + fni_length);

	while (wch_src < wch_cap) {
		*wch = *wch_src;
		wch++;
		wch_src++;
	}

	/* ultimate path separator */
	*wch = '\\'; wch++;

	/* base_name */
	if (base_name) {
		wch_src	= base_name;
		wch_cap = (wchar16_t *)((uintptr_t)wch + base_name_size);

		if ((uintptr_t)wch_cap - (uintptr_t)buffer - sizeof(wchar16_t) > buffer_size)
			return NT_STATUS_BUFFER_TOO_SMALL;

		while (wch < wch_cap) {
			*wch = *wch_src;
			wch++;
			wch_src++;
		}
	}

	/* null termination */
	*wch = 0;

	/* nt_path */
	if (nt_path)
		__ntapi->rtl_init_unicode_string(
			nt_path,
			buffer);

	return NT_STATUS_SUCCESS;
}