summaryrefslogtreecommitdiff
path: root/src/ldso/pe_load_framework_loader.c
blob: 347d3ab72d21431a5cd599b99df2693180ebf1ee (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
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
/*****************************************************************************/
/*  pemagination: a (virtual) tour into portable bits and executable bytes   */
/*  Copyright (C) 2013--2017  Z. Gilboa                                      */
/*  Released under GPLv2 and GPLv3; see COPYING.PEMAGINE.                    */
/*****************************************************************************/

#include <psxtypes/psxtypes.h>
#include <pemagine/pemagine.h>
#include "pe_os.h"

static int32_t pe_get_device_dos_drive_letter(
	void *			hntdll,
	os_zw_query_object *	zw_query_object,
	uintptr_t *		dbuffer,
	uint32_t		dbufsize,
	wchar16_t *		devname,
	size_t			devnamelen,
	wchar16_t *		letter)
{
	int32_t			status;
	void *			hdevice;
	int			sigh;
	size_t			idx;
	size_t			cap;
	uint32_t		len;
	struct os_oa		oa;
	struct os_iosb		iosb;
	wchar16_t *		src;
	wchar16_t *		dst;
	struct pe_unicode_str	path;
	struct pe_unicode_str * dpath;
	os_zw_open_file *	zw_open_file;
	os_zw_close *		zw_close;

	wchar16_t		namebuf[8] = {
				'\\','?','?','\\',
				'X',':','\\',0};

	unsigned char		letters[26] = {
				'C','Z','Y','X','W','V',
				'E','H','F','G','D','I',
				'P','Q','R','S','T','U',
				'J','K','L','M','N','O',
				'A','B'};

	/* init */
	if (!(zw_open_file = (os_zw_open_file *)pe_get_procedure_address(
			hntdll,"ZwOpenFile")))
		return OS_STATUS_INTERNAL_ERROR;

	if (!(zw_close = (os_zw_close *)pe_get_procedure_address(
			hntdll,"ZwClose")))
		return OS_STATUS_INTERNAL_ERROR;

	/* path */
	path.buffer = namebuf;
	path.strlen = 7 * sizeof(wchar16_t);
	path.maxlen = 0;

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

	/* just a few rounds of submissiveness */
	for (sigh=0; sigh<26; sigh++) {
		namebuf[4] = letters[sigh];

		status = zw_open_file(
			&hdevice,
			OS_SEC_SYNCHRONIZE
				| OS_FILE_READ_ATTRIBUTES
				| OS_FILE_READ_ACCESS,
			&oa,&iosb,
			OS_FILE_SHARE_READ
				| OS_FILE_SHARE_WRITE
				| OS_FILE_SHARE_DELETE,
			OS_FILE_DIRECTORY_FILE);

		if (status == OS_STATUS_SUCCESS) {
			status = zw_query_object(
				hdevice,
				OS_OBJECT_NAME_INFORMATION,
				dbuffer,dbufsize,&len);

			if (status == OS_STATUS_SUCCESS) {
				dpath = (struct pe_unicode_str *)dbuffer;

				src = devname;
				dst = dpath->buffer;

				idx = 0;
				cap = devnamelen / sizeof(wchar16_t);

				for (; idx<cap && src[idx]==dst[idx]; )
					idx++;

				if (idx==cap && dst[idx]=='\\') {
					zw_close(hdevice);
					*letter = letters[sigh];

					return OS_STATUS_SUCCESS;
				}
			}

			zw_close(hdevice);
		}
	}

	return OS_STATUS_NOT_SUPPORTED;
}


static int32_t pe_load_library_impl(
	void **		baseaddr,
	void *		hdsolib,
	uintptr_t *	buffer,
	uint32_t	bufsize,
	uint32_t *	flags)
{
	int32_t			status;
	struct pe_unicode_str	path;
	struct pe_unicode_str *	npath;
	uintptr_t		addr;
	uintptr_t *		dbuffer;
	uint32_t		dbufsize;
	wchar16_t *		ldrdir;
	wchar16_t *		slash;
	wchar16_t *		wch;
	wchar16_t *		cap;
	wchar16_t		letter;
	uint32_t		len;
	int			mup;
	void *			hntdll;
	os_zw_query_object *	zw_query_object;
	os_ldr_load_dll *	ldr_load_dll;

	/* init */
	if (!(hntdll = pe_get_ntdll_module_handle()))
		return OS_STATUS_INTERNAL_ERROR;

	if (!(zw_query_object = (os_zw_query_object *)pe_get_procedure_address(
			hntdll,"ZwQueryObject")))
		return OS_STATUS_INTERNAL_ERROR;

	if (!(ldr_load_dll = (os_ldr_load_dll *)pe_get_procedure_address(
			hntdll,"LdrLoadDll")))
		return OS_STATUS_INTERNAL_ERROR;

	/* loader native path */
	if ((status = zw_query_object(
			hdsolib,
			OS_OBJECT_NAME_INFORMATION,
			buffer,bufsize,&len)))
		return status;

	/* integrity */
	if (len == sizeof(struct pe_unicode_str))
		return OS_STATUS_BAD_FILE_TYPE;

	/* first sigh */
	npath = (struct pe_unicode_str *)buffer;

	wch = npath->buffer;
	cap = npath->buffer + (npath->strlen / sizeof(wchar16_t));

	if ((cap < &wch[8])
			|| (wch[0] != '\\')
			|| (wch[1] != 'D')
			|| (wch[2] != 'e')
			|| (wch[3] != 'v')
			|| (wch[4] != 'i')
			|| (wch[5] != 'c')
			|| (wch[6] != 'e')
			|| (wch[7] != '\\'))
		return OS_STATUS_NOT_SUPPORTED;

	mup = (cap > &wch[11])
		&& (wch[8]=='M')
		&& (wch[9]=='u')
		&& (wch[10]=='p')
		&& (wch[11]=='\\');

	slash = mup ? &wch[12] : &wch[8];

	for (; (*slash != '\\') && (slash < cap); )
		slash++;

	if (slash == cap)
		return OS_STATUS_INTERNAL_ERROR;

	if (mup)
		for (++slash; (*slash != '\\') && (slash < cap); )
			slash++;

	if (slash == cap)
		return OS_STATUS_INTERNAL_ERROR;

	/* second sigh */
	addr  = (uintptr_t)cap;
	addr += sizeof(uintptr_t) - 1;
	addr /= sizeof(uintptr_t);
	addr *= sizeof(uintptr_t);

	dbuffer   = (uintptr_t *)addr;
	dbufsize  = bufsize;
	dbufsize -= (dbuffer - buffer) * sizeof(uintptr_t);

	if ((status = pe_get_device_dos_drive_letter(
			hntdll,
			zw_query_object,
			dbuffer,dbufsize,
			npath->buffer,
			(slash - npath->buffer) * sizeof(wchar16_t),
			&letter)))
		return status;

	slash    = &slash[-2];
	slash[0] = letter;
	slash[1] = ':';

	path.buffer = slash;
	path.strlen = (cap - slash) * sizeof(wchar16_t);

	/* third sigh */
	for (slash=cap; (slash > path.buffer) && (*slash != '\\'); )
		slash--;

	if (slash == path.buffer)
		return OS_STATUS_INTERNAL_ERROR;

	ldrdir = path.buffer;
	*slash = 0;

	path.strlen -= (++slash - ldrdir) * sizeof(wchar16_t);
	path.maxlen  = path.strlen;
	path.buffer  = slash;

	/* hoppla */
	return ldr_load_dll(
		ldrdir,flags,
		&path,baseaddr);
}


int32_t pe_load_framework_loader(
	void **					baseaddr,
	struct pe_framework_runtime_data *	rtdata,
	uintptr_t *				buffer,
	uint32_t				bufsize,
	uint32_t *				sysflags)
{
	return pe_load_library_impl(
		baseaddr,
		rtdata->hloader,
		buffer,bufsize,
		sysflags);
}


int32_t pe_load_framework_library(
	void **					baseaddr,
	void *					hat,
	const wchar16_t *			atrelname,
	uintptr_t *				buffer,
	uint32_t				bufsize,
	uint32_t *				sysflags)
{
	int32_t			status;
	struct pe_unicode_str	path;
	struct os_oa		oa;
	struct os_iosb		iosb;
	const wchar16_t *	wch;
	const wchar16_t *	wch_cap;
	void *			hdsolib;
	void *			hntdll;
	os_zw_close *		zw_close;
	os_zw_open_file *	zw_open_file;

	/* init */
	if (!(hntdll = pe_get_ntdll_module_handle()))
		return OS_STATUS_INTERNAL_ERROR;

	if (!(zw_close = (os_zw_close *)pe_get_procedure_address(
			hntdll,"ZwClose")))
		return OS_STATUS_INTERNAL_ERROR;

	if (!(zw_open_file = (os_zw_open_file *)pe_get_procedure_address(
			hntdll,"ZwOpenFile")))
		return OS_STATUS_INTERNAL_ERROR;

	/* oa */
	oa.len      = sizeof(struct os_oa);
	oa.root_dir = hat;
	oa.obj_name = &path;
	oa.obj_attr = 0;
	oa.sec_desc = 0;
	oa.sec_qos  = 0;

	/* at-relative path */
	wch     = atrelname;
	wch_cap = atrelname + 512;

	for (; *wch && wch<wch_cap; )
		wch++;

	if (*wch)
		return OS_STATUS_NAME_TOO_LONG;

	path.buffer = (wchar16_t *)atrelname;
	path.strlen = sizeof(wchar16_t) * (wch - atrelname);
	path.maxlen = 0;

	/* open at */
	if ((status = zw_open_file(
			&hdsolib,
			OS_SEC_SYNCHRONIZE
				| OS_FILE_READ_ACCESS
				| OS_FILE_READ_ATTRIBUTES,
			&oa,&iosb,
			OS_FILE_SHARE_READ
				| OS_FILE_SHARE_WRITE
				| OS_FILE_SHARE_DELETE,
			OS_FILE_NON_DIRECTORY_FILE)))
		return status;

	/* hoppla */
	status = pe_load_library_impl(
		baseaddr,hdsolib,
		buffer,bufsize,
		sysflags);

	/* all done */
	zw_close(hdsolib);

	return status;
}