Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit
Swift apple-silicond3dmetaldxmtdxvkgame-porting-toolkitgptkmac-gamingmacosmetalfxrosetta-2steamsteam-playwinewinecx
Prevent NotProton from being passed along to tools launched by Steam Helper, which could crash if a user has (unwisely) disabled SIP.
NotProtonNot130cc2a0b23a
diff --git a/Makefile b/Makefileindex 7138d9d..62e40eb 100644--- a/Makefile+++ b/Makefile@@ -282,17 +282,21 @@ spawn-live: $(TARGET)-o $$tmp/steam_osx $(SPAWN_LIVE)/driver.c || exit 1; \cp $$tmp/steam_osx $$tmp/inert; cp $$tmp/child $$tmp/steam_osx_child; \cp $$tmp/child $$tmp/sub/steam_osx; \+ cp $$tmp/steam_osx "$$tmp/Steam Helper"; cp $$tmp/child "$$tmp/sub/Steam Helper"; \fail=0; \for spec in "inert:child:1" "steam_osx:child:0" \- "steam_osx:steam_osx_child:0" "steam_osx:sub/steam_osx:1"; do \- drv=$${spec%%:*}; rest=$${spec#*:}; kid=$${rest%:*}; want=$${rest##*:}; \- got=$$(DYLD_INSERT_LIBRARIES=$$PWD/$(TARGET) $$tmp/$$drv $$tmp/$$kid 2>/dev/null \- | grep -c "^DYLD_INSERT_LIBRARIES=") || true; \+ "steam_osx:steam_osx_child:0" "steam_osx:sub/steam_osx:1" \+ "steam_osx:sub/Steam Helper:1" "Steam Helper:child:0" \+ "Steam Helper:sub/Steam Helper:1" "Steam Helper:child:1:--type=renderer"; do \+ drv=$${spec%%:*}; rest=$${spec#*:}; kid=$${rest%%:*}; rest=$${rest#*:}; \+ want=$${rest%%:*}; arg=$${rest#*:}; [ "$$arg" = "$$rest" ] && arg=""; \+ got=$$(DYLD_INSERT_LIBRARIES=$$PWD/$(TARGET) "$$tmp/$$drv" "$$tmp/$$kid" $$arg 2>/dev/null \+ | grep -c "/notproton.dylib$$") || true; \if [ "$$got" != "$$want" ]; then \- echo "$$drv -> $$kid: insert present $$got, wanted $$want"; fail=1; fi; \+ echo "$$drv $$arg -> $$kid: insert present $$got, wanted $$want"; fail=1; fi; \done; \if [ $$fail -ne 0 ]; then exit 1; fi; \- echo "==> spawn live: the hook lands, steam_osx keeps the insert, other children do not"+ echo "==> spawn live: the hook lands, steam_osx and Steam Helper keep the insert, other children do not"APP_TESTS := app/Testsdiff --git a/dylib/core/loader.c b/dylib/core/loader.cindex 9a9940b..7939b01 100644--- a/dylib/core/loader.c+++ b/dylib/core/loader.c@@ -17,6 +17,7 @@#include <stdint.h>#include <dirent.h>#include <mach-o/dyld.h>+#include <crt_externs.h>#include <libgen.h>#define STEAMCLIENT_DYLIB "steamclient.dylib"@@ -242,10 +243,30 @@ static void np_log_platform_overrides(void) {NP_LOG("np_init: no platform override env set, client reports as macOS");}+// Steam Helper runs /usr/bin/profiles at startup. If a user has SIP disabled, profiles would inherit the insert and crash, yikes.+static int is_main_helper(const char *pn) {+ if (strcmp(pn, "Steam Helper") != 0)+ return 0;+ int argc = *_NSGetArgc();+ char **argv = *_NSGetArgv();+ for (int i = 1; i < argc; i++) {+ if (strncmp(argv[i], "--type=", 7) == 0)+ return 0;+ }+ return 1;+}+__attribute__((constructor))static void np_init(void) {const char *pn = getprogname();- if (!pn || strcmp(pn, "steam_osx") != 0)+ if (!pn)+ return;+ if (is_main_helper(pn)) {+ np_log_attach();+ np_hooks_spawn_install();+ return;+ }+ if (strcmp(pn, "steam_osx") != 0)return;np_log_init();diff --git a/dylib/hooks/hook_spawn.c b/dylib/hooks/hook_spawn.cindex 3ae86fb..fd68f29 100644--- a/dylib/hooks/hook_spawn.c+++ b/dylib/hooks/hook_spawn.c@@ -3,6 +3,7 @@#include "../util/log.h"#include <spawn.h>+#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>@@ -20,10 +21,12 @@ static fn_execve orig_execve;static fn_posix_spawn orig_posix_spawn;static fn_posix_spawn orig_posix_spawnp;+#define NP_INSERT_KEY "DYLD_INSERT_LIBRARIES="+// The SDL block list stops Steam from seeing a second, generic copy of the Steam Controller.// This solves double input issues.static const char *const np_steam_only_keys[] = {- "DYLD_INSERT_LIBRARIES=",+ NP_INSERT_KEY,"SDL_JOYSTICK_BLACKLIST_DEVICES=",};@@ -46,6 +49,54 @@ static int np_target_keeps_insert(const char *path) {|| strcmp(base, "Steam Helper") == 0;}+// LaunchServices hands the caller's environment to every app it opens, so the insert+// lives here instead and only goes back into steam_osx and Steam Helper+static char *np_insert_entry;++static int np_is_insert(const char *entry) {+ return strncmp(entry, NP_INSERT_KEY, strlen(NP_INSERT_KEY)) == 0;+}++static char **np_with_insert(char *const envp[]) {+ if (!np_insert_entry)+ return NULL;++ int count = 0;+ if (envp) {+ for (; envp[count]; count++) {+ if (np_is_insert(envp[count]))+ return NULL;+ }+ }++ char **full = malloc(sizeof(char *) * (size_t)(count + 2));+ if (!full) {+ NP_WARN("[spawn] cannot allocate an environment with the insert, child starts without it");+ return NULL;+ }+ for (int i = 0; i < count; i++)+ full[i] = envp[i];+ full[count] = np_insert_entry;+ full[count + 1] = NULL;+ return full;+}++static void np_take_insert(void) {+ const char *value = getenv("DYLD_INSERT_LIBRARIES");+ if (!value || np_insert_entry)+ return;++ size_t len = strlen(NP_INSERT_KEY) + strlen(value) + 1;+ np_insert_entry = malloc(len);+ if (!np_insert_entry) {+ NP_WARN("[spawn] cannot hold the insert, apps opened from here inherit it");+ return;+ }+ snprintf(np_insert_entry, len, "%s%s", NP_INSERT_KEY, value);+ unsetenv("DYLD_INSERT_LIBRARIES");+ NP_LOG("[spawn] insert taken out of the environment");+}+static char **np_without_insert(char *const envp[]) {if (!envp)return NULL;@@ -76,33 +127,29 @@ static char **np_without_insert(char *const envp[]) {}static int np_hook_execve(const char *path, char *const argv[], char *const envp[]) {- if (np_target_keeps_insert(path))- return orig_execve(path, argv, envp);-- char **clean = np_without_insert(envp);+ int keep = np_target_keeps_insert(path);+ char **clean = keep ? np_with_insert(envp) : np_without_insert(envp);if (!clean)return orig_execve(path, argv, envp);- NP_DBG("[spawn] execve '%s' without the insert", path);+ NP_DBG("[spawn] execve '%s' %s the insert", path, keep ? "with" : "without");int rc = orig_execve(path, argv, (char *const *)clean);// Only reached when the exec failed, since a successful one replaced this image.free(clean);return rc;}-static int np_spawn_without_insert(fn_posix_spawn orig, const char *api,+static int np_spawn_filtered(fn_posix_spawn orig, const char *api,pid_t *pid, const char *path,const posix_spawn_file_actions_t *fa,const posix_spawnattr_t *attr,char *const argv[], char *const envp[]) {- if (np_target_keeps_insert(path))- return orig(pid, path, fa, attr, argv, envp);-- char **clean = np_without_insert(envp);+ int keep = np_target_keeps_insert(path);+ char **clean = keep ? np_with_insert(envp) : np_without_insert(envp);if (!clean)return orig(pid, path, fa, attr, argv, envp);- NP_DBG("[spawn] %s '%s' without the insert", api, path);+ NP_DBG("[spawn] %s '%s' %s the insert", api, path, keep ? "with" : "without");int rc = orig(pid, path, fa, attr, argv, (char *const *)clean);free(clean);return rc;@@ -112,7 +159,7 @@ static int np_hook_posix_spawn(pid_t *pid, const char *path,const posix_spawn_file_actions_t *fa,const posix_spawnattr_t *attr,char *const argv[], char *const envp[]) {- return np_spawn_without_insert(orig_posix_spawn, "posix_spawn",+ return np_spawn_filtered(orig_posix_spawn, "posix_spawn",pid, path, fa, attr, argv, envp);}@@ -120,24 +167,26 @@ static int np_hook_posix_spawnp(pid_t *pid, const char *path,const posix_spawn_file_actions_t *fa,const posix_spawnattr_t *attr,char *const argv[], char *const envp[]) {- return np_spawn_without_insert(orig_posix_spawnp, "posix_spawnp",+ return np_spawn_filtered(orig_posix_spawnp, "posix_spawnp",pid, path, fa, attr, argv, envp);}-static void np_hook_symbol(const char *sym, void *repl, void **orig) {+static int np_hook_symbol(const char *sym, void *repl, void **orig) {void *addr = DobbySymbolResolver("libsystem_kernel.dylib", sym);if (!addr)addr = DobbySymbolResolver(NULL, sym);if (!addr) {NP_WARN("[spawn] %s: unresolved, children keep the insert", sym);- return;+ return 0;}int rc = DobbyHook(addr, repl, orig);- if (rc == 0)+ if (rc == 0) {NP_LOG("[spawn] %s: hooked @ %p", sym, addr);- else- NP_WARN("[spawn] %s: DobbyHook failed rc=%d, children keep the insert", sym, rc);+ return 1;+ }+ NP_WARN("[spawn] %s: DobbyHook failed rc=%d, children keep the insert", sym, rc);+ return 0;}void np_hooks_spawn_install(void) {@@ -146,7 +195,11 @@ void np_hooks_spawn_install(void) {return;}- np_hook_symbol("execve", (void *)np_hook_execve, (void **)&orig_execve);- np_hook_symbol("posix_spawn", (void *)np_hook_posix_spawn, (void **)&orig_posix_spawn);- np_hook_symbol("posix_spawnp", (void *)np_hook_posix_spawnp, (void **)&orig_posix_spawnp);+ int hooked = np_hook_symbol("execve", (void *)np_hook_execve, (void **)&orig_execve)+ + np_hook_symbol("posix_spawn", (void *)np_hook_posix_spawn, (void **)&orig_posix_spawn)+ + np_hook_symbol("posix_spawnp", (void *)np_hook_posix_spawnp, (void **)&orig_posix_spawnp);++ // Without every hook in place, a steam_osx re-exec could start without the dylib+ if (hooked == 3)+ np_take_insert();}diff --git a/dylib/tests/spawn-live/child.c b/dylib/tests/spawn-live/child.cindex 522b891..7507ce0 100644--- a/dylib/tests/spawn-live/child.c+++ b/dylib/tests/spawn-live/child.c@@ -1,4 +1,6 @@-// Prints its own environment so the caller can see whether the insert reached it.+#include <mach-o/dyld.h>#include <stdio.h>-extern char **environ;-int main(void) { for (int i = 0; environ[i]; i++) puts(environ[i]); return 0; }+int main(void) {+ for (uint32_t i = 0; i < _dyld_image_count(); i++) puts(_dyld_get_image_name(i));+ return 0;+}diff --git a/dylib/tests/spawn-live/driver.c b/dylib/tests/spawn-live/driver.cindex 5586670..42bc31d 100644--- a/dylib/tests/spawn-live/driver.c+++ b/dylib/tests/spawn-live/driver.c@@ -1,10 +1,11 @@// Spawns argv[1] the way the client spawns its port check, through posix_spawn with its-// own environment. Named steam_osx by the make target so the dylib installs its hooks.+// own environment. Named steam_osx or Steam Helper by the make target so the dylib+// installs its hooks.#include <spawn.h>#include <sys/wait.h>extern char **environ;int main(int argc, char **argv) {- if (argc != 2) return 2;+ if (argc < 2) return 2;char *a[] = { argv[1], NULL };pid_t pid;int st = 0;