fix(bionic): hand off only starts that would succeed here; bionic's execvp fallbacks

Review follow-ups on the bionic runtime's handoff (tools/bionic-runtime/handoff.c):

- A start is handed to a worker only when it would get past execve() in this process: the file is a
  regular executable outside the app's data (Android refuses execve() there), and for a linker64 start
  the program it loads is a readable regular file. A start that failed in the worker failed where the
  caller could not see it, so Wine never tried its next candidate. The hook's own check looked at
  /system/bin/env, not the program.
- A start whose argv[0] is not its file stays here: env starts FILE with FILE as argv[0]. linker64 and
  interpreter starts are unaffected (their argv[0] is the file). An empty argv stays here too.
- Nothing is built for the handoff without DD_SPAWN and DD_HANDOFF.
- execvp()/execvpe()/execlp() follow bionic's: an unset PATH is libc's default, an empty PATH or entry
  is the current directory, and a file the kernel cannot start (ENOEXEC) runs under /system/bin/sh.
- close_range() refuses first > last and unknown flags before it splits the range or unshares the table.
- The debug line reads the table's owner only once the table is seen published.
This commit is contained in:
The412Banner committed 2026-12-01 07:53:20 -05:00
1 parent 501148e542
commit 7d95564f7b
1 file changed
+80 -19
+80 -19
View File
@@ -98,19 +98,46 @@ void exit(int status) {
__builtin_unreachable();
}
static int ddh_is_linker(const char *path) {
const char *base = path ? strrchr(path, '/') : NULL;
return base && strcmp(base + 1, "linker64") == 0;
}
static int ddh_regular(const char *path, int mode) {
struct stat st;
return path && stat(path, &st) == 0 && S_ISREG(st.st_mode) && access(path, mode) == 0;
}
// A handed-off start that fails fails in the worker, where the caller cannot see it and try its next
// candidate (Wine tries its preloader, then its loader), so only a start that would succeed here goes.
// Android refuses execve() of a file in the app's data (execshim.c turns those into linker64 starts).
static int ddh_bionic_startable(const char *file, char *const args[]) {
char real[PATH_MAX];
if (!ddh_regular(file, X_OK) || !realpath(file, real) || strncmp(real, "/data/", 6) == 0) return 0;
// env takes an argument with '=' for a variable
if (strchr(file, '=')) return 0;
if (ddh_is_linker(real)) return args && args[0] && args[1] && args[1][0] != '-' && ddh_regular(args[1], R_OK);
// env starts FILE with FILE as argv[0]
return args && args[0] && strcmp(args[0], file) == 0;
}
void ddh_bionic_exec(const char *file, char *const args[], char *const envp[], const char *path, char *const argv[]) {
if (ddh_env(envp, "DD_HANDOFF_DEBUG")) {
char why[200];
struct ddh_table *own = __atomic_load_n(&g_ddh, __ATOMIC_ACQUIRE);
snprintf(why, sizeof(why), "bionic exec (spawn %d list %d table %d owner %d up %d ppid %d) of", ddh_env(envp, "DD_SPAWN") != NULL,
ddh_env(envp, "DD_HANDOFF") != NULL, own != NULL, (int)g_ddh_owner, g_ddh_up != NULL, (int)getppid());
ddh_env(envp, "DD_HANDOFF") != NULL, own != NULL, own ? (int)g_ddh_owner : 0, g_ddh_up != NULL, (int)getppid());
ddh_debug(envp, why, path ? path : "(null)");
}
if (!ddh_env(envp, "DD_SPAWN") || !ddh_env(envp, "DD_HANDOFF")) return;
if (!ddh_bionic_startable(file, args)) {
ddh_debug(envp, "bionic exec stays here (not startable as a handoff):", path ? path : "(null)");
return;
}
// Wine's own starts through the system linker ("/system/bin/linker64 PROGRAM ARGS") are listed by PROGRAM
const char *match = path;
char *const *match_argv = argv;
const char *base = path ? strrchr(path, '/') : NULL;
if (base && strcmp(base + 1, "linker64") == 0 && argv && argv[0] && argv[1]) {
if (ddh_is_linker(path) && argv && argv[0] && argv[1]) {
match = argv[1];
match_argv = argv + 1;
}
@@ -119,8 +146,9 @@ void ddh_bionic_exec(const char *file, char *const args[], char *const envp[], c
// Android's env instead, which the shell execs and which puts them back for the program: the worker's main
// process ends up as the program all the same. Stack only (this may run in a vfork child).
int nargs = 0, nenv = 0;
while (args && args[nargs]) nargs++;
while (args[nargs]) nargs++;
while (envp && envp[nenv]) nenv++;
// env, up to two loader variables, FILE, args[1..], NULL (nargs >= 1: ddh_bionic_startable)
char *wrapped[nargs + 4];
char *trimmed[nenv + 1];
int w = 0, t = 0;
@@ -147,36 +175,64 @@ extern char **environ;
int execv(const char *path, char *const argv[]) { return execve(path, argv, environ); }
// bionic's execvpe(), through execve() here: an unset PATH is libc's default (Android 14's; an empty one, and an empty
// entry, are the current directory), and a file the kernel cannot start (ENOEXEC) runs under /system/bin/sh.
#define DDH_DEFPATH "/product/bin:/apex/com.android.runtime/bin:/apex/com.android.art/bin:/system_ext/bin:/system/bin:/system/xbin:/odm/bin:/vendor/bin:/vendor/xbin"
static int exec_as_script(const char *file, char *const argv[], char *const envp[]) {
int n = 0;
while (argv && argv[n]) n++;
char *script[(n > 0 ? n : 1) + 2];
int s = 0;
script[s++] = (char *)"sh";
script[s++] = (char *)file;
for (int i = 1; i < n; i++) script[s++] = argv[i];
script[s] = NULL;
return execve("/system/bin/sh", script, envp);
}
int execvpe(const char *file, char *const argv[], char *const envp[]) {
if (!file || !*file) {
errno = ENOENT;
return -1;
}
if (strchr(file, '/')) return execve(file, argv, envp);
if (strchr(file, '/')) {
execve(file, argv, envp);
return errno == ENOEXEC ? exec_as_script(file, argv, envp) : -1;
}
const char *dirs = getenv("PATH");
if (!dirs || !*dirs) dirs = "/system/bin:/system/xbin";
if (!dirs) dirs = DDH_DEFPATH;
int saw_eacces = 0;
size_t flen = strlen(file);
for (const char *p = dirs;;) {
const char *end = strchr(p, ':');
size_t len = end ? (size_t)(end - p) : strlen(p);
char full[PATH_MAX];
if (len + flen + 2 <= sizeof(full)) {
if (len == 0) {
memcpy(full, file, flen + 1); // an empty entry is the current directory
} else {
memcpy(full, p, len);
full[len] = '/';
memcpy(full + len + 1, file, flen + 1);
}
execve(full, argv, envp);
if (errno == EACCES) saw_eacces = 1;
else if (errno != ENOENT && errno != ENOTDIR) return -1;
const char *dir = len ? p : ".";
if (!len) len = 1;
char full[len + flen + 2];
memcpy(full, dir, len);
full[len] = '/';
memcpy(full + len + 1, file, flen + 1);
execve(full, argv, envp);
switch (errno) {
case EISDIR:
case ELOOP:
case ENAMETOOLONG:
case ENOENT:
case ENOTDIR:
break;
case ENOEXEC:
return exec_as_script(full, argv, envp);
case EACCES:
saw_eacces = 1;
break;
default:
return -1;
}
if (!end) break;
p = end + 1;
}
errno = saw_eacces ? EACCES : ENOENT;
if (saw_eacces) errno = EACCES;
return -1;
}
@@ -308,6 +364,11 @@ static int close_range_part(unsigned int first, unsigned int last, void *flags)
}
int close_range(unsigned int first, unsigned int last, int flags) {
// what the kernel refuses, refused before the range is split or the table unshared
if (first > last || (flags & ~(int)(CLOSE_RANGE_UNSHARE | CLOSE_RANGE_CLOEXEC))) {
errno = EINVAL;
return -1;
}
uint64_t ticket = kept_close_begin();
int snapshot[DDNATV_MAX_KEPT_FDS];
const int *kept = (flags & CLOSE_RANGE_CLOEXEC) ? g_cloexec_kept_fds : snapshot;