Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mach_inject.c « mach_inject - github.com/mumble-voip/mach_override.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c021c6d71a171ac1c92fdff3b5289a4cddf6a2c (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
 /*******************************************************************************
	mach_inject.c
		Copyright (c) 2003-2009 Jonathan 'Wolf' Rentzsch: <http://rentzsch.com>
		Some rights reserved: <http://opensource.org/licenses/mit-license.php>

	***************************************************************************/

#include	"mach_inject.h"

#include <mach-o/dyld.h>
#include <mach-o/getsect.h>
#include <mach/mach.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <assert.h>
#include <stdlib.h> // for malloc()
#include <stdio.h>  // for printf()
#include <mach-o/fat.h> // for fat structure decoding
#include <mach-o/arch.h> // to know which is local arch
#include <fcntl.h> // for open/close
// for mmap()
#include <sys/types.h>
#include <sys/mman.h>

#ifndef	COMPILE_TIME_ASSERT( exp )
	#define COMPILE_TIME_ASSERT( exp ) { switch (0) { case 0: case (exp):; } }
#endif
#define ASSERT_CAST( CAST_TO, CAST_FROM ) \
	COMPILE_TIME_ASSERT( sizeof(CAST_TO)==sizeof(CAST_FROM) )

#if defined(__i386__)
void* fixedUpImageFromImage (
		const void *image, 
		unsigned long imageSize, 
		unsigned int jumpTableOffset, 
		unsigned int jumpTableSize,
		ptrdiff_t fixUpOffset);
#endif /* __i386__ */

#include <mach/MACH_ERROR.h>
#define MACH_ERROR(msg, err) { if(err != err_none) mach_error(msg, err); }

/*******************************************************************************
*	
*	Interface
*	
*******************************************************************************/
#pragma mark	-
#pragma mark	(Interface)

	mach_error_t
mach_inject(
		const mach_inject_entry	threadEntry,
		const void				*paramBlock,
		size_t					paramSize,
		pid_t					targetProcess,
		vm_size_t				stackSize )
{
	assert( threadEntry );
	assert( targetProcess > 0 );
	assert( stackSize == 0 || stackSize > 1024 );
	
	//	Find the image.
	const void		*image;
	unsigned long	imageSize;
	unsigned int	jumpTableOffset;
	unsigned int	jumpTableSize;
	mach_error_t	err = machImageForPointer( threadEntry, &image, &imageSize, &jumpTableOffset, &jumpTableSize );
	//fprintf(stderr, "mach_inject: found threadEntry image at: %p with size: %lu\n", image, imageSize);
	
	//	Initialize stackSize to default if requested.
	if( stackSize == 0 )
		/** @bug
			We only want an 8K default, fix the plop-in-the-middle code below.
		*/
		stackSize = 16 * 1024;
	
	//	Convert PID to Mach Task ref.
	mach_port_t	remoteTask = 0;
	if( !err ) {
		err = task_for_pid( mach_task_self(), targetProcess, &remoteTask );
#if defined(__i386__) || defined(__x86_64__)
		if (err == 5) fprintf(stderr, "Could not access task for pid %d. You probably need to add user to procmod group\n", targetProcess);
#endif
	}
		
	/** @todo
		Would be nice to just allocate one block for both the remote stack
		*and* the remoteCode (including the parameter data block once that's
		written.
	*/
	
	//	Allocate the remoteStack.
	vm_address_t remoteStack = (vm_address_t)NULL;
	if( !err )
		err = vm_allocate( remoteTask, &remoteStack, stackSize, 1 );
	
	
	//	Allocate the code.
	vm_address_t remoteCode = (vm_address_t)NULL;
	if( !err )
		err = vm_allocate( remoteTask, &remoteCode, imageSize, 1 );
		err = vm_protect(remoteTask, remoteCode, imageSize, 0, VM_PROT_EXECUTE | VM_PROT_WRITE | VM_PROT_READ);
	if( !err ) {
		ASSERT_CAST( pointer_t, image );
#if defined (__ppc__) || defined (__ppc64__) || defined(__x86_64__)
		err = vm_write( remoteTask, remoteCode, (pointer_t) image, imageSize );
#elif defined (__i386__)
		// on x86, jump table use relative jump instructions (jmp), which means
		// the offset needs to be corrected. We thus copy the image and fix the offset by hand. 
		ptrdiff_t fixUpOffset = (ptrdiff_t) (image - remoteCode); 
		void * fixedUpImage = fixedUpImageFromImage(image, imageSize, jumpTableOffset, jumpTableSize, fixUpOffset);
		err = vm_write( remoteTask, remoteCode, (pointer_t) fixedUpImage, imageSize );
		free(fixedUpImage);
#endif
	}
	
	//	Allocate the paramBlock if specified.
	vm_address_t remoteParamBlock = (vm_address_t)NULL;
	if( !err && paramBlock != NULL && paramSize ) {
		err = vm_allocate( remoteTask, &remoteParamBlock, paramSize, 1 );
		if( !err ) {
			ASSERT_CAST( pointer_t, paramBlock );
			err = vm_write( remoteTask, remoteParamBlock,
					(pointer_t) paramBlock, paramSize );
		}
	}
		
	//	Calculate offsets.
	ptrdiff_t	threadEntryOffset, imageOffset;
	if( !err ) {
		//assert( (void*)threadEntry >= image && (void*)threadEntry <= (image+imageSize) );
		ASSERT_CAST( void*, threadEntry );
		threadEntryOffset = ((void*) threadEntry) - image;
		
#if defined(__x86_64__)
		imageOffset = 0; // RIP-relative addressing
#else
		//ASSERT_CAST( void*, remoteCode );
		//imageOffset = ((void*) remoteCode) - image;
		// WARNING: See bug https://github.com/rentzsch/mach_star/issues/11 . Not sure about this.
		imageOffset = 0;
#endif
	}
	
	//	Allocate the thread.
	thread_act_t remoteThread;
#if defined (__ppc__) || defined (__ppc64__)
	if( !err ) {
		ppc_thread_state_t remoteThreadState;
		
		/** @bug
			Stack math should be more sophisticated than this (ala redzone).
		*/
		remoteStack += stackSize / 2;
		
		bzero( &remoteThreadState, sizeof(remoteThreadState) );
		
		ASSERT_CAST( unsigned int, remoteCode );
		remoteThreadState.__srr0 = (unsigned int) remoteCode;
		remoteThreadState.__srr0 += threadEntryOffset;
		assert( remoteThreadState.__srr0 < (remoteCode + imageSize) );
		
		ASSERT_CAST( unsigned int, remoteStack );
		remoteThreadState.__r1 = (unsigned int) remoteStack;
		
		ASSERT_CAST( unsigned int, imageOffset );
		remoteThreadState.__r3 = (unsigned int) imageOffset;
		
		ASSERT_CAST( unsigned int, remoteParamBlock );
		remoteThreadState.__r4 = (unsigned int) remoteParamBlock;
		
		ASSERT_CAST( unsigned int, paramSize );
		remoteThreadState.__r5 = (unsigned int) paramSize;
		
		ASSERT_CAST( unsigned int, 0xDEADBEEF );
		remoteThreadState.__lr = (unsigned int) 0xDEADBEEF;
		
#if 0
		printf( "remoteCode start: %p\n", (void*) remoteCode );
		printf( "remoteCode size: %ld\n", imageSize );
		printf( "remoteCode pc: %p\n", (void*) remoteThreadState.srr0 );
		printf( "remoteCode end: %p\n",
			(void*) (((char*)remoteCode)+imageSize) );
		fflush(0);
#endif
		
		err = thread_create_running( remoteTask, PPC_THREAD_STATE,
				(thread_state_t) &remoteThreadState, PPC_THREAD_STATE_COUNT,
				&remoteThread );
	}
#elif defined (__i386__)
	if( !err ) {
		
		i386_thread_state_t remoteThreadState;
		bzero( &remoteThreadState, sizeof(remoteThreadState) );
		
		vm_address_t dummy_thread_struct = remoteStack;
		remoteStack += (stackSize / 2); // this is the real stack
		// (*) increase the stack, since we're simulating a CALL instruction, which normally pushes return address on the stack
		remoteStack -= 4;
		
#define PARAM_COUNT 4
#define STACK_CONTENTS_SIZE ((1+PARAM_COUNT) * sizeof(unsigned int))
		unsigned int stackContents[1 + PARAM_COUNT]; // 1 for the return address and 1 for each param
		// first entry is return address (see above *)
		stackContents[0] = 0xDEADBEEF; // invalid return address.
		// then we push function parameters one by one.
		stackContents[1] =  imageOffset;
		stackContents[2] = remoteParamBlock;
		stackContents[3] = paramSize;
		// We use the remote stack we allocated as the fake thread struct. We should probably use a dedicated memory zone. 
		// We don't fill it with 0, vm_allocate did it for us
		stackContents[4] = dummy_thread_struct; 
		
		// push stackContents
		err = vm_write( remoteTask, remoteStack,
						(pointer_t) stackContents, STACK_CONTENTS_SIZE);
		
		// set remote Program Counter
		remoteThreadState.__eip = (unsigned int) (remoteCode);
		remoteThreadState.__eip += threadEntryOffset;
		
		// set remote Stack Pointer
		ASSERT_CAST( unsigned int, remoteStack );
		remoteThreadState.__esp = (unsigned int) remoteStack;

		// create thread and launch it
		err = thread_create_running( remoteTask, i386_THREAD_STATE,
									 (thread_state_t) &remoteThreadState, i386_THREAD_STATE_COUNT,
									 &remoteThread );
	}
#elif defined(__x86_64__)
	if( !err ) {
		
		x86_thread_state64_t remoteThreadState;
		bzero( &remoteThreadState, sizeof(remoteThreadState) );
			
		vm_address_t dummy_thread_struct = remoteStack;
		remoteStack += (stackSize / 2); // this is the real stack
		// (*) increase the stack, since we're simulating a CALL instruction, which normally pushes return address on the stack
		remoteStack -= 4;
		
#define PARAM_COUNT 0
#define STACK_CONTENTS_SIZE ((1+PARAM_COUNT) * sizeof(unsigned long long))
		unsigned long long stackContents[1 + PARAM_COUNT]; // 1 for the return address and 1 for each param
		// first entry is return address (see above *)
		stackContents[0] = 0x00000DEADBEA7DAD; // invalid return address.
		
		// push stackContents
		err = vm_write( remoteTask, remoteStack,
						(pointer_t) stackContents, STACK_CONTENTS_SIZE);

		remoteThreadState.__rdi = (unsigned long long) (imageOffset);
		remoteThreadState.__rsi = (unsigned long long) (remoteParamBlock);
		remoteThreadState.__rdx = (unsigned long long) (paramSize);
		remoteThreadState.__rcx = (unsigned long long) (dummy_thread_struct);
		
		// set remote Program Counter
		remoteThreadState.__rip = (unsigned long long) (remoteCode);
		remoteThreadState.__rip += threadEntryOffset;  
		
		// set remote Stack Pointer
		ASSERT_CAST( unsigned long long, remoteStack );
		remoteThreadState.__rsp = (unsigned long long) remoteStack;
				
		// create thread and launch it
		err = thread_create_running( remoteTask, x86_THREAD_STATE64,
									 (thread_state_t) &remoteThreadState, x86_THREAD_STATE64_COUNT,
									 &remoteThread );
	}
#else
#error architecture not supported
#endif
	
	if( err ) {
		MACH_ERROR("mach_inject failing..", err);
		if( remoteParamBlock )
			vm_deallocate( remoteTask, remoteParamBlock, paramSize );
		if( remoteCode )
			vm_deallocate( remoteTask, remoteCode, imageSize );
		if( remoteStack )
			vm_deallocate( remoteTask, remoteStack, stackSize );
	}
		
	return err;
}

	mach_error_t
machImageForPointer(
		const void *pointer,
		const void **image,
		unsigned long *size,
		unsigned int *jumpTableOffset,
		unsigned int *jumpTableSize )
{
	assert( pointer );
	assert( image );
	assert( size );
	
	unsigned long p = (unsigned long) pointer;
	
	if (jumpTableOffset && jumpTableSize) {
		*jumpTableOffset = 0;
		*jumpTableSize = 0;
	}
	
	unsigned long imageIndex, imageCount = _dyld_image_count();
	for( imageIndex = 0; imageIndex < imageCount; imageIndex++ ) {
#if defined(__x86_64__)
		const struct mach_header_64 *header = (const struct mach_header_64 *)_dyld_get_image_header( imageIndex ); // why no function that returns mach_header_64
		const struct section_64 *section = getsectbynamefromheader_64( header, SEG_TEXT, SECT_TEXT );
#else
		const struct mach_header *header = (const struct mach_header *)_dyld_get_image_header( imageIndex );
		const struct section *section = getsectbynamefromheader( header, SEG_TEXT, SECT_TEXT );
#endif
		if (section == 0) continue;
		long start = section->addr + _dyld_get_image_vmaddr_slide( imageIndex );
		long stop = start + section->size;
		//printf("start %ld %p %s b\n", start, header, _dyld_get_image_name(imageIndex));
		if( p >= start && p <= stop ) {
			//	It is truely insane we have to stat() the file system in order
			//	to discover the size of an in-memory data structure.
			const char *imageName = _dyld_get_image_name( imageIndex );
			assert( imageName );
			struct stat sb;
			if( stat( imageName, &sb ) )
				return unix_err( errno );
			if( image ) {
				ASSERT_CAST( void*, header );
				*image = (void*) header;
			}
			if( size ) {
				;//assertUInt32( st_size );
				*size = sb.st_size;
				
				// needed for Universal binaries. Check if file is fat and get image size from there.
				int fd = open (imageName, O_RDONLY);
				size_t mapSize = *size;
				char * fileImage = mmap (NULL, mapSize, PROT_READ, MAP_FILE|MAP_SHARED, fd, 0);
				
				assert(fileImage != MAP_FAILED);
				struct fat_header* fatHeader = (struct fat_header *)fileImage;
				if (fatHeader->magic == OSSwapBigToHostInt32(FAT_MAGIC)) {
					//printf("This is a fat binary\n");
					uint32_t archCount = OSSwapBigToHostInt32(fatHeader->nfat_arch);
					
					NXArchInfo const *localArchInfo = NXGetLocalArchInfo();
					
					struct fat_arch* arch = (struct fat_arch *)(fileImage + sizeof(struct fat_header));
					struct fat_arch* matchingArch = NULL;
					
					int archIndex = 0;
					for (archIndex = 0; archIndex < archCount; archIndex++) {
						cpu_type_t cpuType = OSSwapBigToHostInt32(arch[archIndex].cputype);
						cpu_subtype_t cpuSubtype = OSSwapBigToHostInt32(arch[archIndex].cpusubtype);
						
						if (localArchInfo->cputype == cpuType) {
							matchingArch = arch + archIndex;
							if (localArchInfo->cpusubtype == cpuSubtype) break;
						}
					}
					
					if (matchingArch) {
						*size = OSSwapBigToHostInt32(matchingArch->size);
						//printf ("found arch-specific size : %p\n", *size);
					}
				}
				
				munmap (fileImage, mapSize);
				close (fd);
			}
#if defined(__i386__) // this segment is only available on IA-32
			if (jumpTableOffset && jumpTableSize) {
			  const struct section * jumpTableSection = getsectbynamefromheader( header, SEG_IMPORT, "__jump_table" );

			  if (!jumpTableSection) {
			    unsigned char *start, *end;
			    jumpTableSection = getsectbynamefromheader( header, SEG_TEXT, "__symbol_stub" );
			    /*
			    start = end = (char *) header + jumpTableSection->offset;
			    end += jumpTableSection->size;

			    fprintf(stderr, "start: %p\n", start);
			    for (; start < end; start += 6) {
			      fprintf(stderr, "%p: %p: %p\n",
				      start,
				      *(void **)(start+2),
				      **(void ***)(start+2));
			    }
			    */
			  }
			  
			  if (jumpTableSection) {
			    *jumpTableOffset = jumpTableSection->offset;
			    *jumpTableSize = jumpTableSection->size;
			  }
			}
#endif
			return err_none;
		}
	}
	
	return err_threadEntry_image_not_found;
}

#if defined(__i386__)
void* fixedUpImageFromImage (
		const void *image, 
		unsigned long imageSize, 
		unsigned int jumpTableOffset, 
		unsigned int jumpTableSize, 
		ptrdiff_t fixUpOffset)
{
	// first copy the full image
	void *fixedUpImage = (void *) malloc ((size_t)imageSize);
	bcopy(image, fixedUpImage, imageSize);
	
	// address of jump table in copied image
	void *jumpTable = fixedUpImage + jumpTableOffset;

	
	/* indirect jump table */
	if (*(unsigned char *) jumpTable == 0xff) {
	  // each indirect JMP instruction is 6 bytes (FF xx xx xx xx xx) where FF is the opcode for JMP
	  int jumpTableCount = jumpTableSize / 6;
	
	  // skip first "ff xx"
	  jumpTable += 2;
	
	  int entry=0;
	  for (entry = 0; entry < jumpTableCount; entry++) {
	    void *jmpValue = *((void **)jumpTable);
	    /*
	    fprintf(stderr, "at %p correcting %p to %p\n",
		    (char *)jumpTable -2,
		    jmpValue, jmpValue + fixUpOffset);
	    */
	    jmpValue -= fixUpOffset;
	    *((void **)jumpTable) = jmpValue;
	    jumpTable+=6;
	  }
	}
	else {
	  // each JMP instruction is 5 bytes (E9 xx xx xx xx) where E9 is the opcode for JMP
	  int jumpTableCount = jumpTableSize / 5;
	
	  // skip first "E9"
	  jumpTable++;
	
	  int entry=0;
	  for (entry = 0; entry < jumpTableCount; entry++) {
	    unsigned int jmpValue = *((unsigned int *)jumpTable);
	    jmpValue += fixUpOffset;
	    *((unsigned int *)jumpTable) = jmpValue;
	    jumpTable+=5;
	  }
	}
	
	return fixedUpImage;
}
#endif /* __i386__ */