fix(bionic): hand off app-data starts only where SELinux lets them run; bare names stay here

Follow-up to 7d95564f7 (review, and a Pocket FIT test of its pack):

- 7d95564f7 kept every start of a file in the app's data here. Wine's preloader is one (a static ELF,
  so execshim.c cannot make it a linker64 start), and where SELinux is permissive, as on AYANEO's
  firmware, it runs: Wine then never reached its loader, and its programs ran here, counted again
  (Pocket FIT: Brawlhalla 26-28 counted vs 16-17 before). Where SELinux enforces, Android refuses the
  exec, here and in a worker alike, and Wine falls back to its loader, which goes as a linker64 start.
  Such a start is now handed off when /sys/fs/selinux/enforce reads 0, and stays here otherwise
  (also when the setting cannot be read).
- The program a start would run is checked with the hook's own ddh_startable() (an ELF the kernel
  loads with its interpreter there, or a script with its interpreter): for a linker64 start, the
  program it loads.
- A file name without '/' stays here: env would look it up in PATH, the exec here in the caller's
  working directory.
- execvpe() skips a PATH entry whose candidate exceeds PATH_MAX (ENAMETOOLONG) instead of copying
  it onto the stack.
This commit is contained in:
The412Banner committed 2026-12-01 08:24:24 -05:00
1 parent 7d95564f7b
commit 9f5c35f088
1 file changed
+27 -11
+27 -11
View File
@@ -108,17 +108,29 @@ static int ddh_regular(const char *path, int mode) {
return path && stat(path, &st) == 0 && S_ISREG(st.st_mode) && access(path, mode) == 0;
}
// Android refuses execve() of a file in the app's data unless SELinux is permissive (execshim.c makes the
// dynamic ones linker64 starts). An unreadable setting counts as enforcing.
static int ddh_selinux_permissive(void) {
char c = 0;
int fd = (int)syscall(SYS_openat, AT_FDCWD, "/sys/fs/selinux/enforce", O_RDONLY | O_CLOEXEC, 0);
if (fd < 0) return 0;
long n = syscall(SYS_read, fd, &c, 1);
syscall(SYS_close, fd);
return n == 1 && c == '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 takes an argument with '=' for a variable, and looks a name without '/' up in PATH
if (!file || strchr(file, '=') || !strchr(file, '/')) return 0;
if (!ddh_regular(file, X_OK) || !realpath(file, real)) return 0;
if (ddh_is_linker(real))
return args && args[0] && args[1] && args[1][0] != '-' && ddh_regular(args[1], R_OK) && ddh_startable(args[1]);
if (strncmp(real, "/data/", 6) == 0 && !ddh_selinux_permissive()) return 0;
// env starts FILE with FILE as argv[0]
return args && args[0] && strcmp(args[0], file) == 0;
return args && args[0] && strcmp(args[0], file) == 0 && ddh_startable(file);
}
void ddh_bionic_exec(const char *file, char *const args[], char *const envp[], const char *path, char *const argv[]) {
@@ -209,11 +221,15 @@ int execvpe(const char *file, char *const argv[], char *const envp[]) {
size_t len = end ? (size_t)(end - p) : strlen(p);
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);
char full[PATH_MAX];
if (len + flen + 2 > sizeof(full)) {
errno = ENAMETOOLONG;
} else {
memcpy(full, dir, len);
full[len] = '/';
memcpy(full + len + 1, file, flen + 1);
execve(full, argv, envp);
}
switch (errno) {
case EISDIR:
case ELOOP: