| 1 |
/* |
| 2 |
* libmowgli: A collection of useful routines for programming. |
| 3 |
* fork.c: Fastest possible NT fork() implementation |
| 4 |
* |
| 5 |
* Copyright (c) 2012 TortoiseLabs, LLC. |
| 6 |
* |
| 7 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 8 |
* purpose with or without fee is hereby granted, provided that the above |
| 9 |
* copyright notice and this permission notice is present in all copies. |
| 10 |
* |
| 11 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 12 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 13 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 14 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 15 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 16 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 17 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 18 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 19 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 20 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 21 |
* POSSIBILITY OF SUCH DAMAGE. |
| 22 |
*/ |
| 23 |
|
| 24 |
/* |
| 25 |
* References: |
| 26 |
* - undocumented.ntinternals.net |
| 27 |
* - http://www.cs.miami.edu/~burt/journal/NT/processinit.html |
| 28 |
*/ |
| 29 |
|
| 30 |
#include "mowgli.h" |
| 31 |
|
| 32 |
#ifdef NOTYET |
| 33 |
|
| 34 |
# ifdef _WIN32 |
| 35 |
|
| 36 |
# ifndef _WIN32_WINNT |
| 37 |
|
| 38 |
int |
| 39 |
fork(void) |
| 40 |
{ |
| 41 |
# warning fork is not possible on your platform in any sane way, sorry :( |
| 42 |
return -ENOSYS; |
| 43 |
} |
| 44 |
|
| 45 |
# else |
| 46 |
|
| 47 |
extern NTSTATUS NTAPI CsrCallClientServer(void *message, void *userdata, uint32_t opcode, uint32_t size); |
| 48 |
|
| 49 |
/* |
| 50 |
* Definition of a message sent to an NT port on the CSRSS server. |
| 51 |
* |
| 52 |
* Not sure what dummy1/dummy2 do, but they're junk as far as I can see. |
| 53 |
*/ |
| 54 |
struct csrss_message |
| 55 |
{ |
| 56 |
uint32_t dummy1; |
| 57 |
uint32_t opcode; |
| 58 |
uint32_t status; |
| 59 |
uint32_t dummy2; |
| 60 |
}; |
| 61 |
|
| 62 |
static inline void |
| 63 |
inherit_handles(void) |
| 64 |
{ |
| 65 |
uint32_t n = 0x1000; |
| 66 |
uint32_t *p = mowgli_alloc_array(sizeof(uint32_t), n); |
| 67 |
uint32_t pid, i; |
| 68 |
SYSTEM_HANDLE_INFORMATION *info; |
| 69 |
|
| 70 |
while (ZwQuerySystemInformation(SystemHandleInformation, p, n * sizeof(*p), 0) == STATUS_INFO_LENGTH_MISMATCH) |
| 71 |
{ |
| 72 |
n *= 2; |
| 73 |
|
| 74 |
mowgli_free(p); |
| 75 |
p = mowgli_alloc_array(sizeof(uint32_t), n); |
| 76 |
} |
| 77 |
|
| 78 |
info = (SYSTEM_HANDLE_INFORMATION *) (p + 1); |
| 79 |
pid = GetCurrentProcessId(); |
| 80 |
|
| 81 |
for (i = 0; i < *p; i++) |
| 82 |
if (info[i].ProcessId == pid) |
| 83 |
SetHandleInformation((HANDLE) h[i].Handle, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); |
| 84 |
|
| 85 |
mowgli_free(p); |
| 86 |
} |
| 87 |
|
| 88 |
static inline void |
| 89 |
request_csrss_session(HANDLE proc_handle, HANDLE thread_handle, uint32_t pid, uint32_t tid) |
| 90 |
{ |
| 91 |
struct |
| 92 |
{ |
| 93 |
PORT_MESSAGE port_message; |
| 94 |
struct csrss_message csrss_message; |
| 95 |
|
| 96 |
PROCESS_INFORMATION process_information; |
| 97 |
CLIENT_ID debugger; |
| 98 |
uint32_t flags; |
| 99 |
uint32_t vdminfo[2]; |
| 100 |
} csrmsg = |
| 101 |
{ |
| 102 |
{ 0 }, { 0 }, { proc_handle, thread_handle, pid, tid }, { 0 }, 0, { 0 } |
| 103 |
}; |
| 104 |
|
| 105 |
CsrCallClientServer(&csrmsg, NULL, 0x10000, sizeof csrmsg); |
| 106 |
} |
| 107 |
|
| 108 |
int |
| 109 |
child(void) |
| 110 |
{ |
| 111 |
typedef BOOL (*CsrpConnectToServer)(PWSTR); |
| 112 |
|
| 113 |
CsrpConnectToServer(0x77F8F65D) (L"\\Windows"); |
| 114 |
__asm__("mov eax, 0; mov esp, ebp; pop ebp; ret"); |
| 115 |
} |
| 116 |
|
| 117 |
int |
| 118 |
fork(void) |
| 119 |
{ |
| 120 |
HANDLE proc_handle, thread_handle; |
| 121 |
OBJECT_ATTRIBUTES oa = { sizeof(oa) }; |
| 122 |
CONTEXT context = { CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS | CONTEXT_FLOATING_POINT }; |
| 123 |
MEMORY_BASIC_INFORMATION mbi; |
| 124 |
THREAD_BASIC_INFORMATION tbi; |
| 125 |
PNT_TIB tib; |
| 126 |
USER_STACK stack; |
| 127 |
CLIENT_ID cid; |
| 128 |
|
| 129 |
/* ensure the child has the same handles and ports */ |
| 130 |
inherit_handles(); |
| 131 |
|
| 132 |
/* create the actual LWP using ZwCreateProcess() */ |
| 133 |
ZwCreateProcess(&proc_handle, PROCESS_ALL_ACCESS, &oa, NtCurrentProcess(), TRUE, 0, 0, 0); |
| 134 |
|
| 135 |
/* now set up a thread for that process using a context, cloning the current thread ... */ |
| 136 |
ZwGetContextThread(NtCurrentThread(), &context); |
| 137 |
context.Eip = (unsigned long) child; |
| 138 |
|
| 139 |
/* set up a stack for the thread now that the child sentinel is set up ... */ |
| 140 |
ZwQueryVirtualMemory(NtCurrentProcess(), (void *) context.Esp, MemoryBasicInformation, |
| 141 |
&mbi, sizeof mbi, 0); |
| 142 |
|
| 143 |
stack = (USER_STACK) { 0, 0, ((char *) mbi.BaseAddress) + mbi.RegionSize, |
| 144 |
mbi.BaseAddress, mbi.AllocationBase }; |
| 145 |
|
| 146 |
/* now spawn the thread! */ |
| 147 |
ZwCreateThread(&thread_handle, THREAD_ALL_ACCESS, &oa, proc_handle, &cid, &context, &stack, TRUE); |
| 148 |
|
| 149 |
/* thread is spawned, but frozen for inspection -- fix up memory protection before unfreezing */ |
| 150 |
ZwQueryInformationThread(NtCurrentThread(), ThreadBasicInformation, &tbi, sizeof tbi, 0); |
| 151 |
tib = tbi.TebBaseAddress; |
| 152 |
|
| 153 |
ZwQueryInformationThread(thread_handle, ThreadBasicInformation, &tbi, sizeof tbi, 0); |
| 154 |
ZwWriteVirtualMemory(process_handle, tbi.TebBaseAddress, &tib->ExceptionList, sizeof(tib->ExceptionList), 0); |
| 155 |
|
| 156 |
/* ready to go, now request a CSRSS session */ |
| 157 |
request_csrss_session(process_handle, thread_handle, (uint32_t) cid.UniqueProcess, (uint32_t) cid.UniqueThread); |
| 158 |
|
| 159 |
/* CSRSS session set up or we segfaulted by now, so unfreeze the child... */ |
| 160 |
ZwResumeThread(thread_handle, 0); |
| 161 |
|
| 162 |
/* release handle refcount now that process is freestanding */ |
| 163 |
ZwClose(thread_handle); |
| 164 |
ZwClose(process_handle); |
| 165 |
|
| 166 |
return (int) cid.UniqueProcess; |
| 167 |
} |
| 168 |
|
| 169 |
# endif |
| 170 |
|
| 171 |
# endif |
| 172 |
|
| 173 |
#endif |