Make execve() linger when it can't spoof parent

It's now possible to use execve() when the parent process isn't built by
cosmo. In such cases, the current process will kill all threads and then
linger around, waiting for the newly created process to die, and then we
propagate its exit code to the parent. This should help bazel and others

Allocating private anonymous memory is now 5x faster on Windows. This is
thanks to VirtualAlloc() which is faster than the file mapping APIs. The
fork() function also now goes 30% faster, since we are able to avoid the
VirtualProtect() calls on mappings in most cases now.

Fixes #1253
This commit is contained in:
Justine Tunney 2025-01-04 21:11:53 -08:00
parent c97a858470
commit 42a3bb729a
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
40 changed files with 612 additions and 656 deletions

View file

@ -30,6 +30,7 @@ LIBC_PROC_A_DIRECTDEPS = \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_NT_KERNEL32 \
LIBC_NT_NTDLL \
LIBC_NT_PSAPI \
LIBC_RUNTIME \
LIBC_STR \

View file

@ -24,16 +24,23 @@
#include "libc/calls/syscall-nt.internal.h"
#include "libc/errno.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/dll.h"
#include "libc/intrin/fds.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/strace.h"
#include "libc/mem/mem.h"
#include "libc/nt/accounting.h"
#include "libc/nt/enum/processaccess.h"
#include "libc/nt/enum/startf.h"
#include "libc/nt/enum/status.h"
#include "libc/nt/errors.h"
#include "libc/nt/files.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/processinformation.h"
#include "libc/nt/struct/startupinfo.h"
#include "libc/nt/synchronization.h"
#include "libc/nt/thread.h"
#include "libc/nt/thunk/msabi.h"
#include "libc/proc/describefds.internal.h"
#include "libc/proc/ntspawn.h"
@ -41,6 +48,7 @@
#include "libc/str/str.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/errfuns.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"
@ -65,13 +73,11 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
_pthread_lock(); // order matters
// new process should be a child of our parent
int64_t hParentProcess;
int ppid = sys_getppid_nt();
if (!(hParentProcess = OpenProcess(
kNtProcessDupHandle | kNtProcessCreateProcess, false, ppid))) {
sys_execve_nt_abort(sigmask);
return -1;
}
int64_t hParentProcess =
sys_getppid_nt_win32
? OpenProcess(kNtProcessDupHandle | kNtProcessCreateProcess, false,
sys_getppid_nt_win32)
: 0;
// inherit pid
char pidvar[11 + 21];
@ -81,6 +87,16 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
char maskvar[6 + 21];
FormatUint64(stpcpy(maskvar, "_MASK="), sigmask);
// inherit parent process id
char ppidvar[12 + 21 + 1 + 21 + 1], *p = ppidvar;
p = stpcpy(p, "_COSMO_PPID=");
if (hParentProcess) {
p = FormatUint64(p, sys_getppid_nt_win32);
*p++ = ':';
p = FormatUint64(p, __pid);
setenv("_COSMO_PPID", ppidvar, true);
}
// define stdio handles for the spawned subprocess
struct NtStartupInfo si = {
.cb = sizeof(struct NtStartupInfo),
@ -94,13 +110,22 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
}
}
// which process is responsible for spawning the child?
int64_t hCreatorProcess;
if (hParentProcess) {
hCreatorProcess = hParentProcess;
} else {
hCreatorProcess = GetCurrentProcess();
}
// pass serialized file descriptor table in environment
char *fdspec;
int64_t *lpExplicitHandles;
uint32_t dwExplicitHandleCount;
if (!(fdspec = __describe_fds(g_fds.p, g_fds.n, &si, hParentProcess,
if (!(fdspec = __describe_fds(g_fds.p, g_fds.n, &si, hCreatorProcess,
&lpExplicitHandles, &dwExplicitHandleCount))) {
CloseHandle(hParentProcess);
if (hParentProcess)
CloseHandle(hParentProcess);
sys_execve_nt_abort(sigmask);
return -1;
}
@ -114,12 +139,14 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
// launch the process
struct NtProcessInformation pi;
int rc = ntspawn(&(struct NtSpawnArgs){
AT_FDCWD, program, argv, envp, (char *[]){fdspec, maskvar, pidvar, 0}, 0,
0, hParentProcess, lpExplicitHandles, dwExplicitHandleCount, &si, &pi});
__undescribe_fds(hParentProcess, lpExplicitHandles, dwExplicitHandleCount);
AT_FDCWD, program, argv, envp,
(char *[]){fdspec, maskvar, pidvar, ppidvar, 0}, 0, 0, hCreatorProcess,
lpExplicitHandles, dwExplicitHandleCount, &si, &pi});
__undescribe_fds(hCreatorProcess, lpExplicitHandles, dwExplicitHandleCount);
if (rc == -1) {
free(fdspec);
CloseHandle(hParentProcess);
if (hParentProcess)
CloseHandle(hParentProcess);
sys_execve_nt_abort(sigmask);
if (GetLastError() == kNtErrorSharingViolation) {
return etxtbsy();
@ -128,18 +155,55 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
}
}
// give child to libc/proc/proc.c worker thread in parent
int64_t handle;
if (DuplicateHandle(GetCurrentProcess(), pi.hProcess, hParentProcess, &handle,
0, false, kNtDuplicateSameAccess)) {
unassert(!(handle & 0xFFFFFFFFFF000000));
__imp_TerminateProcess(-1, 0x23000000u | handle);
} else {
// TODO(jart): Why does `make loc` print this?
// kprintf("DuplicateHandle failed w/ %d\n", GetLastError());
__imp_TerminateProcess(-1, ECHILD);
// check if parent spoofing worked
if (hParentProcess) {
// give child to libc/proc/proc.c worker thread in parent
int64_t handle;
if (DuplicateHandle(GetCurrentProcess(), pi.hProcess, hParentProcess,
&handle, 0, false, kNtDuplicateSameAccess)) {
unassert(!(handle & 0xFFFFFFFFFF000000));
__imp_TerminateProcess(-1, 0x23000000u | handle);
} else {
// TODO(jart): Why does `make loc` print this?
// kprintf("DuplicateHandle failed w/ %d\n", GetLastError());
__imp_TerminateProcess(-1, ECHILD);
}
__builtin_unreachable();
}
// we couldn't reparent the new process
STRACE("warning: execve() lingering due to non-cosmo parent process");
// terminate other threads
struct Dll *e;
struct PosixThread *me = _pthread_self();
for (e = dll_first(_pthread_list); e; e = dll_next(_pthread_list, e)) {
struct PosixThread *pt = POSIXTHREAD_CONTAINER(e);
if (pt == me)
continue;
TerminateThread(
atomic_load_explicit(&pt->tib->tib_syshand, memory_order_relaxed),
SIGKILL);
}
// wait for child to terminate and propagate exit code
for (;;) {
uint32_t status;
WaitForSingleObject(pi.hProcess, -1u);
GetExitCodeProcess(pi.hProcess, &status);
if (status != kNtStillActive) {
if ((status & 0xFF000000u) == 0x23000000u) {
// handle child execve()
CloseHandle(pi.hProcess);
pi.hProcess = status & 0x00FFFFFF;
} else {
// handle child _Exit()
if (status == 0xc9af3d51u)
status = kNtStillActive;
TerminateThisProcess(status);
}
}
}
__builtin_unreachable();
}
#endif /* __x86_64__ */

View file

@ -57,11 +57,6 @@
* compiled by MSVC or Cygwin is launched instead, then only the stdio
* file descriptors can be passed along.
*
* On Windows, the parent process must be a cosmo program. If you're
* calling execve() from a program that wasn't launched by cosmopolitan
* bash, or some similar program, then ask yourself if what you really
* want is to either (a) call fork() first, or (b) use posix_spawn().
*
* On Windows, `argv` and `envp` can't contain binary strings. They need
* to be valid UTF-8 in order to round-trip the WIN32 API, without being
* corrupted.

View file

@ -46,6 +46,7 @@
#include "libc/nt/winsock.h"
#include "libc/proc/proc.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
@ -211,8 +212,6 @@ textwindows static int sys_fork_nt_parent(uint32_t dwCreationFlags) {
// let's go
bool ok = true;
uint32_t child_old_protect;
uint32_t parent_old_protect;
// copy memory manager maps
for (struct MapSlab *slab =
@ -225,11 +224,12 @@ textwindows static int sys_fork_nt_parent(uint32_t dwCreationFlags) {
}
// copy private memory maps
int alloc_prot = -1;
for (struct Map *map = __maps_first(); map; map = __maps_next(map)) {
if ((map->flags & MAP_TYPE) == MAP_SHARED)
continue;
continue; // shared memory doesn't need to be copied to subprocess
if ((map->flags & MAP_NOFORK) && (map->flags & MAP_TYPE) != MAP_FILE)
continue;
continue; // ignore things like signal worker stack memory
if (__maps_isalloc(map)) {
size_t allocsize = map->size;
for (struct Map *m2 = __maps_next(map); m2; m2 = __maps_next(m2)) {
@ -240,22 +240,41 @@ textwindows static int sys_fork_nt_parent(uint32_t dwCreationFlags) {
}
}
if ((map->flags & MAP_NOFORK) && (map->flags & MAP_TYPE) == MAP_FILE) {
ok = ok && !!VirtualProtectEx(procinfo.hProcess, map->addr, allocsize,
kNtPageReadwrite, &child_old_protect);
// portable executable segment
if (!(map->prot & PROT_WRITE)) {
uint32_t child_old_protect;
ok = ok && !!VirtualProtectEx(procinfo.hProcess, map->addr, allocsize,
kNtPageReadwrite, &child_old_protect);
alloc_prot = PROT_READ | PROT_WRITE;
} else {
alloc_prot = map->prot;
}
} else {
// private mapping
uint32_t page_flags;
if (!(alloc_prot & PROT_WRITE)) {
page_flags = kNtPageReadwrite;
alloc_prot = PROT_READ | PROT_WRITE;
} else {
page_flags = __prot2nt(alloc_prot, false);
}
ok = ok && !!VirtualAllocEx(procinfo.hProcess, map->addr, allocsize,
kNtMemReserve | kNtMemCommit,
kNtPageExecuteReadwrite);
kNtMemReserve | kNtMemCommit, page_flags);
}
}
uint32_t parent_old_protect;
if (!(map->prot & PROT_READ))
ok = ok && !!VirtualProtect(map->addr, map->size, kNtPageReadwrite,
&parent_old_protect);
ok = ok && !!WriteProcessMemory(procinfo.hProcess, map->addr, map->addr,
map->size, 0);
ok = ok &&
!!VirtualProtectEx(procinfo.hProcess, map->addr, map->size,
__prot2nt(map->prot, false), &child_old_protect);
!!WriteProcessMemory(procinfo.hProcess, map->addr, map->addr,
(map->size + __pagesize - 1) & -__pagesize, 0);
if (map->prot != alloc_prot) {
uint32_t child_old_protect;
ok = ok &&
!!VirtualProtectEx(procinfo.hProcess, map->addr, map->size,
__prot2nt(map->prot, false), &child_old_protect);
}
if (!(map->prot & PROT_READ))
ok = ok && !!VirtualProtect(map->addr, map->size, parent_old_protect,
&parent_old_protect);

View file

@ -20,6 +20,7 @@
#include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h"
#include "libc/calls/state.internal.h"
#include "libc/calls/struct/metasigaltstack.h"
#include "libc/calls/struct/sigset.internal.h"
#include "libc/calls/struct/timespec.h"
#include "libc/calls/syscall-nt.internal.h"
@ -43,6 +44,7 @@
#include "libc/runtime/syslib.internal.h"
#include "libc/stdio/internal.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/ss.h"
#include "libc/thread/itimer.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"
@ -120,8 +122,7 @@ static void fork_prepare(void) {
if (_weaken(__dlopen_lock))
_weaken(__dlopen_lock)();
if (IsWindows())
if (_weaken(__proc_lock))
_weaken(__proc_lock)();
__proc_lock();
if (_weaken(cosmo_stack_lock))
_weaken(cosmo_stack_lock)();
__cxa_lock();
@ -155,8 +156,7 @@ static void fork_parent(void) {
if (_weaken(cosmo_stack_unlock))
_weaken(cosmo_stack_unlock)();
if (IsWindows())
if (_weaken(__proc_unlock))
_weaken(__proc_unlock)();
__proc_unlock();
if (_weaken(__dlopen_unlock))
_weaken(__dlopen_unlock)();
if (_weaken(__localtime_unlock))
@ -167,7 +167,7 @@ static void fork_parent(void) {
_pthread_mutex_unlock(&supreme_lock);
}
static void fork_child(void) {
static void fork_child(int ppid_win32, int ppid_cosmo) {
if (_weaken(__rand64_wipe))
_weaken(__rand64_wipe)();
_pthread_mutex_wipe_np(&__fds_lock_obj);
@ -194,6 +194,8 @@ static void fork_child(void) {
_pthread_mutex_wipe_np(&__sig_worker_lock);
if (_weaken(__sig_init))
_weaken(__sig_init)();
if (_weaken(sys_getppid_nt_wipe))
_weaken(sys_getppid_nt_wipe)(ppid_win32, ppid_cosmo);
}
if (_weaken(_pthread_onfork_child))
_weaken(_pthread_onfork_child)();
@ -202,8 +204,9 @@ static void fork_child(void) {
int _fork(uint32_t dwCreationFlags) {
struct Dll *e;
int ax, dx, tid, parent;
parent = __pid;
int ax, dx, tid, ppid_win32, ppid_cosmo;
ppid_win32 = IsWindows() ? GetCurrentProcessId() : 0;
ppid_cosmo = __pid;
BLOCK_SIGNALS;
fork_prepare();
if (!IsWindows()) {
@ -223,7 +226,7 @@ int _fork(uint32_t dwCreationFlags) {
// get new thread id
struct CosmoTib *tib = __get_tls();
struct PosixThread *pt = (struct PosixThread *)tib->tib_pthread;
struct PosixThread *me = (struct PosixThread *)tib->tib_pthread;
tid = IsLinux() || IsXnuSilicon() ? dx : sys_gettid();
atomic_init(&tib->tib_ctid, tid);
atomic_init(&tib->tib_ptid, tid);
@ -243,10 +246,10 @@ int _fork(uint32_t dwCreationFlags) {
// turn other threads into zombies
// we can't free() them since we're monopolizing all locks
// we assume the operating system already reclaimed system handles
dll_remove(&_pthread_list, &pt->list);
dll_remove(&_pthread_list, &me->list);
struct Dll *old_threads = _pthread_list;
_pthread_list = 0;
dll_make_first(&_pthread_list, &pt->list);
dll_make_first(&_pthread_list, &me->list);
atomic_init(&_pthread_count, 1);
// get new system thread handle
@ -264,25 +267,38 @@ int _fork(uint32_t dwCreationFlags) {
atomic_init(&tib->tib_sigpending, 0);
// we can't be canceled if the canceler no longer exists
atomic_init(&pt->pt_canceled, false);
atomic_init(&me->pt_canceled, false);
// forget locks
bzero(tib->tib_locks, sizeof(tib->tib_locks));
// xnu fork() doesn't preserve sigaltstack()
if (IsXnu() && me->tib->tib_sigstack_addr) {
struct sigaltstack_bsd ss;
ss.ss_sp = me->tib->tib_sigstack_addr;
ss.ss_size = me->tib->tib_sigstack_size;
ss.ss_flags = me->tib->tib_sigstack_flags;
if (IsXnuSilicon()) {
__syslib->__sigaltstack(&ss, 0);
} else {
sys_sigaltstack(&ss, 0);
}
}
// run user fork callbacks
fork_child();
fork_child(ppid_win32, ppid_cosmo);
// free threads
if (_weaken(_pthread_free)) {
while ((e = dll_first(old_threads))) {
pt = POSIXTHREAD_CONTAINER(e);
struct PosixThread *pt = POSIXTHREAD_CONTAINER(e);
atomic_init(&pt->tib->tib_syshand, 0);
dll_remove(&old_threads, e);
_weaken(_pthread_free)(pt);
}
}
STRACE("fork() → 0 (child of %d)", parent);
STRACE("fork() → 0 (child of %d)", ppid_cosmo);
} else {
// this is the parent process
fork_parent();

93
libc/proc/getppid-nt.c Normal file
View file

@ -0,0 +1,93 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/atomic.h"
#include "libc/calls/syscall-nt.internal.h"
#include "libc/cosmo.h"
#include "libc/dce.h"
#include "libc/fmt/itoa.h"
#include "libc/nt/enum/status.h"
#include "libc/nt/nt/process.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/processbasicinformation.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
int sys_getppid_nt_win32;
int sys_getppid_nt_cosmo;
textwindows static int sys_getppid_nt_ntdll(void) {
struct NtProcessBasicInformation ProcessInformation;
uint32_t gotsize = 0;
if (!NtError(
NtQueryInformationProcess(GetCurrentProcess(), 0, &ProcessInformation,
sizeof(ProcessInformation), &gotsize)) &&
gotsize >= sizeof(ProcessInformation) &&
ProcessInformation.InheritedFromUniqueProcessId) {
return ProcessInformation.InheritedFromUniqueProcessId;
}
return 0;
}
static void sys_getppid_nt_extract(const char *str) {
int c;
int win32 = 0;
int cosmo = 0;
if (str) {
for (;;) {
c = *str;
if (!('0' <= c && c <= '9'))
break;
win32 *= 10;
win32 += c - '0';
++str;
}
if (win32 && *str++ == ':') {
for (;;) {
c = *str;
if (!('0' <= c && c <= '9'))
break;
cosmo *= 10;
cosmo += c - '0';
++str;
}
if (win32 == sys_getppid_nt_ntdll()) {
sys_getppid_nt_win32 = win32;
sys_getppid_nt_cosmo = cosmo;
}
}
}
}
__attribute__((__constructor__(90))) static void init(void) {
if (!IsWindows())
return;
sys_getppid_nt_extract(getenv("_COSMO_PPID"));
}
textwindows int sys_getppid_nt(void) {
if (sys_getppid_nt_cosmo)
return sys_getppid_nt_cosmo;
return sys_getppid_nt_ntdll();
}
textwindows void sys_getppid_nt_wipe(int win32, int cosmo) {
sys_getppid_nt_win32 = win32;
sys_getppid_nt_cosmo = cosmo;
}

49
libc/proc/getppid.c Normal file
View file

@ -0,0 +1,49 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/calls/syscall-nt.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/intrin/strace.h"
/**
* Returns parent process id.
*
* @return parent process id (always successful)
* @note slow on Windows; needs to iterate process tree
* @asyncsignalsafe
* @vforksafe
*/
int getppid(void) {
int rc;
if (IsMetal()) {
rc = 1;
} else if (!IsWindows()) {
if (!IsNetbsd()) {
rc = sys_getppid();
} else {
rc = sys_getpid().dx;
}
} else {
rc = sys_getppid_nt();
}
npassert(rc >= 0);
STRACE("%s() → %d", "getppid", rc);
return rc;
}

View file

@ -51,6 +51,7 @@
#include "libc/nt/enum/processcreationflags.h"
#include "libc/nt/enum/startf.h"
#include "libc/nt/files.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/processinformation.h"
#include "libc/nt/struct/startupinfo.h"
@ -59,6 +60,7 @@
#include "libc/proc/posix_spawn.h"
#include "libc/proc/posix_spawn.internal.h"
#include "libc/proc/proc.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/sock/sock.h"
#include "libc/stdio/stdio.h"
@ -396,6 +398,14 @@ static textwindows errno_t posix_spawn_nt_impl(
}
FormatUint64(stpcpy(maskvar, "_MASK="), childmask);
// inherit parent process id
char ppidvar[12 + 21 + 1 + 21 + 1], *p = ppidvar;
p = stpcpy(p, "_COSMO_PPID=");
p = FormatUint64(p, GetCurrentProcessId());
*p++ = ':';
p = FormatUint64(p, __pid);
setenv("_COSMO_PPID", ppidvar, true);
// launch process
int rc = -1;
struct NtProcessInformation procinfo;