mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-24 23:36:04 +00:00
fix(native): programs started through the rootfs loader show their own command line (#3055)
* fix(native): programs started through the rootfs loader show their own command line Without proot every dynamic guest program runs as "ld.so --preload <hook> --library-path <dirs> --argv0 <name> <program> <args>", and the kernel's /proc/<pid>/cmdline kept all of it. Launchers and the session match programs by their command line, and Wine only moves its Windows command line over its loader's name when its arguments follow each other, so the Battle.net launcher never saw Battle.net.exe, gave up after five minutes and ended the session with the game still running. The hook now moves the program's own arguments to the front of that area at start, as an exec of the program would have laid them out. * fix(native): programs the app starts natively keep their path for /proc/<pid>/exe * fix(native): the rewritten command line keeps AT_EXECFN naming the program --------- Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
This commit is contained in:
1 parent
3c4ac03656
commit
1772291da7
8 files changed
+335
-31
No files matched your search
+118
-17
@@ -134,8 +134,11 @@ static void init_ddnatv_hook(void) {
|
||||
// environment is kept in. A program that had changed its environment (putenv, setenv) and passed it
|
||||
// to execv() before its first call into the hook handed the exec code a block that was freed by the
|
||||
// time it was read.
|
||||
__attribute__((constructor(103))) static void init_ddnatv_hook_early(void) {
|
||||
static void compact_loader_args(int argc, char **argv);
|
||||
|
||||
__attribute__((constructor(103))) static void init_ddnatv_hook_early(int argc, char **argv) {
|
||||
init_ddnatv_hook();
|
||||
compact_loader_args(argc, argv);
|
||||
// The kernel names the process after what it executed, the rootfs ld.so: process monitors, and
|
||||
// the app's own checks of /proc/<pid>/comm (steam, wineserver), saw only "ld-linux-aarch64.so.1"
|
||||
const char *name = program_invocation_short_name;
|
||||
@@ -232,27 +235,39 @@ static ssize_t (*real_readlinkat_fn(void))(int, const char *, char *, size_t) {
|
||||
return fn;
|
||||
}
|
||||
|
||||
static int proc_env_value(pid_t pid, const char *key, char *out, size_t outsz) {
|
||||
char path[64];
|
||||
snprintf(path, sizeof(path), "/proc/%d/environ", (int)pid);
|
||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) return 0;
|
||||
size_t cap = 65536, used = 0, keylen = strlen(key);
|
||||
char *env = malloc(cap + 1);
|
||||
ssize_t got;
|
||||
while (env && (got = read(fd, env + used, cap - used)) > 0) {
|
||||
static char *read_proc_file(const char *path, size_t *len, int *whole) {
|
||||
*len = 0;
|
||||
*whole = 0;
|
||||
int fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) return NULL;
|
||||
size_t cap = 65536, used = 0;
|
||||
char *text = malloc(cap + 1);
|
||||
ssize_t got = 0;
|
||||
while (text && (got = (ssize_t)syscall(SYS_read, fd, text + used, cap - used)) > 0) {
|
||||
used += (size_t)got;
|
||||
if (used == cap) {
|
||||
char *bigger = cap < (8u << 20) ? realloc(env, cap * 2 + 1) : NULL;
|
||||
char *bigger = cap < (8u << 20) ? realloc(text, cap * 2 + 1) : NULL;
|
||||
if (!bigger) break;
|
||||
env = bigger;
|
||||
text = bigger;
|
||||
cap *= 2;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
syscall(SYS_close, fd);
|
||||
if (!text) return NULL;
|
||||
text[used] = '\0';
|
||||
*len = used;
|
||||
*whole = got == 0;
|
||||
return text;
|
||||
}
|
||||
|
||||
static int proc_env_value(pid_t pid, const char *key, char *out, size_t outsz) {
|
||||
char path[64];
|
||||
snprintf(path, sizeof(path), "/proc/%d/environ", (int)pid);
|
||||
size_t used, keylen = strlen(key);
|
||||
int whole;
|
||||
char *env = read_proc_file(path, &used, &whole);
|
||||
int found = 0;
|
||||
if (env) {
|
||||
env[used] = '\0';
|
||||
for (char *e = env; e < env + used; e += strlen(e) + 1) {
|
||||
if (strncmp(e, key, keylen) == 0 && e[keylen] == '=' && e[keylen + 1] != '\0') {
|
||||
ddnatv_copy_cstr(out, outsz, e + keylen + 1);
|
||||
@@ -265,6 +280,94 @@ static int proc_env_value(pid_t pid, const char *key, char *out, size_t outsz) {
|
||||
return found;
|
||||
}
|
||||
|
||||
static int is_loader_option(const char *arg) {
|
||||
return strcmp(arg, "--preload") == 0 || strcmp(arg, "--library-path") == 0 || strcmp(arg, "--argv0") == 0;
|
||||
}
|
||||
|
||||
static int is_exec_loader(const char *path) {
|
||||
const char *base = strrchr(path, '/');
|
||||
return strcmp(base ? base + 1 : path, "libddexec.so") == 0;
|
||||
}
|
||||
|
||||
static size_t next_arg(const char *text, size_t pos) {
|
||||
return pos + strlen(text + pos) + 1;
|
||||
}
|
||||
|
||||
static uintptr_t *execfn_entry(int argc, char **argv) {
|
||||
if (argv[argc]) return NULL;
|
||||
char **env = argv + argc + 1;
|
||||
while (*env) env++;
|
||||
for (uintptr_t *aux = (uintptr_t *)(env + 1); aux[0] != AT_NULL; aux += 2) {
|
||||
if (aux[0] == AT_EXECFN) return aux + 1;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void compact_loader_args(int argc, char **argv) {
|
||||
if (g_ddnatv_root[0] == '\0' || argc < 1 || !argv || !argv[0]) return;
|
||||
size_t len;
|
||||
int whole;
|
||||
char *text = read_proc_file("/proc/self/cmdline", &len, &whole);
|
||||
size_t *at = text && whole && len > 0 && text[len - 1] == '\0' ? malloc(sizeof(size_t) * (size_t)argc) : NULL;
|
||||
size_t pos = 0, program = 0;
|
||||
if (at && is_exec_loader(text)) {
|
||||
pos = next_arg(text, pos);
|
||||
if (pos < len) pos = next_arg(text, pos);
|
||||
}
|
||||
int ok = at && pos < len && is_ld_linux(text + pos), named = 0;
|
||||
if (ok) pos = next_arg(text, pos);
|
||||
while (ok && pos < len && is_loader_option(text + pos)) {
|
||||
size_t value = next_arg(text, pos);
|
||||
ok = value < len;
|
||||
if (ok && strcmp(text + pos, "--argv0") == 0) {
|
||||
at[0] = value;
|
||||
named = 1;
|
||||
}
|
||||
if (ok) pos = next_arg(text, value);
|
||||
}
|
||||
int count = 0;
|
||||
if (ok && named && pos < len) {
|
||||
program = pos;
|
||||
pos = next_arg(text, pos);
|
||||
for (count = 1; pos < len && count < argc; count++) {
|
||||
at[count] = pos;
|
||||
pos = next_arg(text, pos);
|
||||
}
|
||||
}
|
||||
char *base = count == argc && pos == len ? argv[0] - at[0] : NULL;
|
||||
for (int i = 1; base && i < argc; i++) {
|
||||
if (argv[i] != base + at[i]) base = NULL;
|
||||
}
|
||||
if (base && memcmp(base, text, len) != 0) base = NULL;
|
||||
uintptr_t *execfn = base ? execfn_entry(argc, argv) : NULL;
|
||||
char *execfn_text = NULL;
|
||||
if (execfn && *execfn >= (uintptr_t)base && *execfn < (uintptr_t)base + len) {
|
||||
execfn_text = strdup(text + (*execfn - (uintptr_t)base));
|
||||
if (!execfn_text) base = NULL;
|
||||
}
|
||||
if (base) {
|
||||
char *old_name = argv[0];
|
||||
size_t name_len = strlen(old_name);
|
||||
char *name = program_invocation_name, *short_name = program_invocation_short_name;
|
||||
size_t used = 0;
|
||||
for (int i = 0; i < argc; i++) {
|
||||
size_t n = strlen(text + at[i]) + 1;
|
||||
memcpy(base + used, text + at[i], n);
|
||||
argv[i] = base + used;
|
||||
used += n;
|
||||
}
|
||||
memset(base + used, 0, len - used);
|
||||
if (name >= old_name && name <= old_name + name_len) program_invocation_name = argv[0] + (name - old_name);
|
||||
if (short_name >= old_name && short_name <= old_name + name_len) {
|
||||
program_invocation_short_name = argv[0] + (short_name - old_name);
|
||||
}
|
||||
if (g_real_exe[0] == '\0') ddnatv_copy_cstr(g_real_exe, sizeof(g_real_exe), text + program);
|
||||
if (execfn_text) *execfn = (uintptr_t)execfn_text;
|
||||
}
|
||||
free(at);
|
||||
free(text);
|
||||
}
|
||||
|
||||
// Overlay filesystems report a different device to stat() than in /proc/<pid>/maps; the path, as the
|
||||
// kernel resolved it, then stands in for the device.
|
||||
static int proc_maps_file(pid_t pid, dev_t dev, ino_t ino, const char *resolved) {
|
||||
@@ -320,9 +423,7 @@ static ssize_t handle_proc_other_exe(const char *path, char *buf, size_t bufsiz)
|
||||
ssize_t n = real_readlinkat(AT_FDCWD, path, loader, sizeof(loader) - 1);
|
||||
if (n <= 0) return -1;
|
||||
loader[n] = '\0';
|
||||
const char *base = strrchr(loader, '/');
|
||||
base = base ? base + 1 : loader;
|
||||
if (strcmp(base, "libddexec.so") != 0 && !is_ld_linux(loader)) return -1;
|
||||
if (!is_exec_loader(loader) && !is_ld_linux(loader)) return -1;
|
||||
|
||||
char real_exe[PATH_MAX] = "";
|
||||
if (!proc_env_value(pid, "DDNATV_REAL_EXE", real_exe, sizeof(real_exe))) {
|
||||
|
||||
Reference in new issue
Block a user