diff --git a/tools/bionic-runtime/handoff.c b/tools/bionic-runtime/handoff.c index 00e7c181cc..377dc97987 100644 --- a/tools/bionic-runtime/handoff.c +++ b/tools/bionic-runtime/handoff.c @@ -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: