summaryrefslogtreecommitdiffhomepage
path: root/src/ldso/pe_find_framework_loader.c
blob: 8096ea8f1f312abb0ba960e6c121cb8b3bcd5ecc (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
/*****************************************************************************/
/*  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"

int32_t pe_find_framework_loader(
	struct pe_framework_runtime_data *	rtdata,
	const wchar16_t *			basename,
	const wchar16_t *			rrelname,
	void *					refaddr,
	uintptr_t *				buffer,
	uint32_t				bufsize,
	uint32_t				flags)
{
	int32_t			status;
	struct pe_unicode_str	path;
	struct os_oa		oa;
	struct os_iosb		iosb;
	void *			himage;
	void *			himgdir;
	void *			hdsodir;
	void *			hloader;
	void *			hparent;
	void *			hprevious;
	const wchar16_t *	wch;
	const wchar16_t *	wch_cap;
	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;

	/* flags */
	if (flags == PE_LDSO_INTEGRAL_ONLY)
		(void)0;
	else if (flags == PE_LDSO_DEFAULT_EXECUTABLE)
		(void)0;
	else if (flags == PE_LDSO_STANDALONE_EXECUTABLE)
		(void)0;
	else
		return OS_STATUS_INVALID_PARAMETER;

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

	/* standalone executable? */
	if (flags == PE_LDSO_STANDALONE_EXECUTABLE) {
		wch     = basename;
		wch_cap = basename + 512;

		for (; *wch && wch<wch_cap; wch++)
			if ((*wch == '\\') != (*wch == '/'))
				return OS_STATUS_INVALID_PARAMETER;

		if (*wch)
			return OS_STATUS_NAME_TOO_LONG;

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

		if ((status = pe_open_image_from_addr(
				&himage,refaddr,
				buffer,bufsize,
				0,
				OS_SEC_SYNCHRONIZE
					| OS_FILE_READ_ACCESS
					| OS_FILE_READ_ATTRIBUTES,
				OS_FILE_SHARE_READ
					| OS_FILE_SHARE_WRITE
					| OS_FILE_SHARE_DELETE,
				0)))
			return status;

		if ((status = pe_open_physical_parent_directory(
				&hdsodir,himage,
				buffer,bufsize,
				0,
				OS_SEC_SYNCHRONIZE
					| OS_FILE_READ_ACCESS
					| OS_FILE_READ_ATTRIBUTES,
				OS_FILE_SHARE_READ
					| OS_FILE_SHARE_WRITE
					| OS_FILE_SHARE_DELETE,
				0)))
			return status;

		oa.root_dir = hdsodir;

		if ((status = zw_open_file(
				&hloader,
				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;

		rtdata->hdsodir = hdsodir;
		rtdata->hloader = hloader;
		rtdata->himage  = himage;

		return OS_STATUS_SUCCESS;
	}

	/* root-relative loader path */
	wch     = rrelname;
	wch_cap = rrelname + 512;

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

	if (*wch)
		return OS_STATUS_NAME_TOO_LONG;

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

	/* inherited root directory? */
	if (rtdata->hroot) {
		oa.root_dir = rtdata->hroot;

		if ((status = zw_open_file(
				&hloader,
				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;

		rtdata->hdsodir = 0;
		rtdata->hloader = hloader;

		return OS_STATUS_SUCCESS;
	}

	/* integral only and no inherited root directory? */
	if (flags == PE_LDSO_INTEGRAL_ONLY)
		return OS_STATUS_COULD_NOT_INTERPRET;

	/* finde Pluto / find Waldo */
	if ((status = pe_open_image_from_addr(
			&himage,refaddr,
			buffer,bufsize,
			0,
			OS_SEC_SYNCHRONIZE
				| OS_FILE_READ_ACCESS
				| OS_FILE_READ_ATTRIBUTES,
			OS_FILE_SHARE_READ
				| OS_FILE_SHARE_WRITE
				| OS_FILE_SHARE_DELETE,
			0)))
		return status;

	if ((status = pe_open_physical_parent_directory(
			&himgdir,himage,
			buffer,bufsize,
			0,
			OS_SEC_SYNCHRONIZE
				| OS_FILE_READ_ACCESS
				| OS_FILE_READ_ATTRIBUTES,
			OS_FILE_SHARE_READ
				| OS_FILE_SHARE_WRITE
				| OS_FILE_SHARE_DELETE,
			0)))
		return status;

	if ((status = pe_open_physical_parent_directory(
			&hparent,himgdir,
			buffer,bufsize,
			OS_OBJ_INHERIT,
			OS_SEC_SYNCHRONIZE
				| OS_FILE_READ_ACCESS
				| OS_FILE_READ_ATTRIBUTES,
			OS_FILE_SHARE_READ
				| OS_FILE_SHARE_WRITE
				| OS_FILE_SHARE_DELETE,
			0)))
		return status;

	zw_close(himgdir);

	hloader     = 0;
	oa.root_dir = hparent;

	status = zw_open_file(
		&hloader,
		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);

	while (!hloader) {
		if (status == OS_STATUS_OBJECT_NAME_NOT_FOUND)
			(void)0;
		else if (status == OS_STATUS_OBJECT_PATH_NOT_FOUND)
			(void)0;
		else
			return status;

		hprevious = hparent;

		if ((status = pe_open_physical_parent_directory(
				&hparent,hprevious,
				buffer,bufsize,
				OS_OBJ_INHERIT,
				OS_SEC_SYNCHRONIZE
					| OS_FILE_READ_ACCESS
					| OS_FILE_READ_ATTRIBUTES,
				OS_FILE_SHARE_READ
					| OS_FILE_SHARE_WRITE
					| OS_FILE_SHARE_DELETE,
				0)))
			return status;

		oa.root_dir = hparent;

		status = zw_open_file(
			&hloader,
			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);

		zw_close(hprevious);
	}

	rtdata->hdsodir = 0;
	rtdata->hloader = hloader;
	rtdata->himage  = himage;
	rtdata->hroot   = hparent;

	return OS_STATUS_SUCCESS;
}