mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 21:35:59 +00:00
feat: target Android 14 so Play Protect installs DroidDeck without a warning (#2683)
* fix(proot): ship proot in the APK and run it from the native library directory * fix(native): start rootfs programs through an APK-shipped exec loader where data files may not be executed * fix(audio): start PulseAudio through the system linker where data files may not be executed * feat: target Android 14 (SDK 34) so Play Protect installs DroidDeck without a warning * fix: ask for all-files access on Android 11+, reject unloadable ELFs before exec, keep checking the proot pack * fix: hand host scripts to the kernel, skip startup launch while all-files Settings is open * fix: run proot from its add-on pack through Android's linker, so the APK carries no proot * fix: declare high sampling rate sensors for controller gyro at target 34 * fix: offer the rootfs proot fallback only where Android lets proot exec its loader * test: install the runtime's proot pack in GameFileSync fixtures
This commit is contained in:
1 parent
6188c9ac0d
commit
dc98b6b96a
28 files changed
+601
-58
No files matched your search
@@ -38,6 +38,7 @@
|
||||
#include <shadow.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/wait.h>
|
||||
#include <elf.h>
|
||||
#include <sys/sendfile.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sched.h>
|
||||
|
||||
@@ -96,6 +96,7 @@ static char **prepare_ddnatv_env(char *const envp[], const char *real_exe) {
|
||||
int has_binds = 0;
|
||||
int has_hostname = 0;
|
||||
int has_link_root = 0;
|
||||
int has_exec_loader = 0;
|
||||
int has_preload_list = 0;
|
||||
const char *preload_value = NULL;
|
||||
while (envp && envp[count]) {
|
||||
@@ -125,6 +126,7 @@ static char **prepare_ddnatv_env(char *const envp[], const char *real_exe) {
|
||||
else if (strncmp(e, "DDNATV_PRELOAD=", 15) == 0) has_preload_list = 1;
|
||||
else if (strncmp(e, "DDNATV_HOSTNAME=", 16) == 0) has_hostname = 1;
|
||||
else if (strncmp(e, "DDNATV_LINK_ROOT=", 17) == 0) has_link_root = 1;
|
||||
else if (strncmp(e, "DDNATV_EXEC=", 12) == 0) has_exec_loader = 1;
|
||||
else if (strncmp(e, "DPKG_DEB_THREADS_MAX=", 21) == 0) has_threads_max = 1;
|
||||
else if (strncmp(e, "DEBIAN_FRONTEND=", 16) == 0) has_frontend = 1;
|
||||
else if (strncmp(e, "DEBCONF_FRONTEND=", 17) == 0) has_debconf_frontend = 1;
|
||||
@@ -136,7 +138,7 @@ static char **prepare_ddnatv_env(char *const envp[], const char *real_exe) {
|
||||
count++;
|
||||
}
|
||||
|
||||
char **new_env = calloc(count + 24, sizeof(char *));
|
||||
char **new_env = calloc(count + 25, sizeof(char *));
|
||||
int dst = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (has_preload && envp[i][0] == 'L' && strncmp(envp[i], "LD_PRELOAD=", 11) == 0) {
|
||||
@@ -223,6 +225,15 @@ static char **prepare_ddnatv_env(char *const envp[], const char *real_exe) {
|
||||
new_env[dst++] = str;
|
||||
}
|
||||
}
|
||||
if (!has_exec_loader && g_exec_loader[0] != '\0') {
|
||||
size_t len = strlen(g_exec_loader);
|
||||
char *str = malloc(12 + len + 1);
|
||||
if (str) {
|
||||
memcpy(str, "DDNATV_EXEC=", 12);
|
||||
memcpy(str + 12, g_exec_loader, len + 1);
|
||||
new_env[dst++] = str;
|
||||
}
|
||||
}
|
||||
if (!has_link_root && g_link_root_len) {
|
||||
char *str = malloc(17 + g_link_root_len + 1);
|
||||
if (str) {
|
||||
|
||||
@@ -135,12 +135,103 @@ static int in_exec_rehearsal(void) {
|
||||
pthread_equal(pthread_self(), g_exec_rehearsal_thread);
|
||||
}
|
||||
|
||||
// Android's own programs, and the app's extracted libraries, may still be executed directly.
|
||||
static int exec_directly(const char *path) {
|
||||
static const char *const prefixes[] = {"/system/", "/vendor/", "/apex/", "/product/", "/odm/", "/system_ext/", "/data/app/"};
|
||||
if (g_exec_loader[0] == '\0' || !path || path[0] != '/' || strcmp(path, g_exec_loader) == 0) return 1;
|
||||
for (size_t i = 0; i < sizeof(prefixes) / sizeof(prefixes[0]); i++)
|
||||
if (strncmp(path, prefixes[i], strlen(prefixes[i])) == 0) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ENOENT when the program names a dynamic linker that is not there (a glibc program's
|
||||
// "/lib/ld-linux-aarch64.so.1" on Android), as the kernel answers: callers try another way.
|
||||
static int exec_interp_missing(int fd) {
|
||||
Elf64_Ehdr eh;
|
||||
if (syscall(SYS_pread64, fd, &eh, sizeof(eh), 0) != (long)sizeof(eh) || eh.e_ident[EI_CLASS] != ELFCLASS64 ||
|
||||
eh.e_machine != EM_AARCH64 || (eh.e_type != ET_EXEC && eh.e_type != ET_DYN) ||
|
||||
eh.e_phentsize != sizeof(Elf64_Phdr) || eh.e_phnum == 0 || eh.e_phnum > 64)
|
||||
return ENOEXEC;
|
||||
for (int i = 0; i < eh.e_phnum; i++) {
|
||||
Elf64_Phdr ph;
|
||||
if (syscall(SYS_pread64, fd, &ph, sizeof(ph), (long)(eh.e_phoff + (Elf64_Off)i * sizeof(ph))) != (long)sizeof(ph))
|
||||
return ENOEXEC;
|
||||
if (ph.p_type != PT_INTERP) continue;
|
||||
char interp[PATH_MAX];
|
||||
if (ph.p_filesz == 0 || ph.p_filesz >= sizeof(interp) ||
|
||||
syscall(SYS_pread64, fd, interp, ph.p_filesz, (long)ph.p_offset) != (long)ph.p_filesz)
|
||||
return ENOEXEC;
|
||||
interp[ph.p_filesz] = 0;
|
||||
int ifd = (int)syscall(SYS_openat, AT_FDCWD, interp, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
|
||||
if (ifd < 0) return errno;
|
||||
syscall(SYS_close, ifd);
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The errors execve() itself would give for [path], which the loader cannot report once it runs:
|
||||
// a shell walking $PATH moves on at ENOENT and EACCES, and runs a file without "#!" as a script at
|
||||
// ENOEXEC.
|
||||
static int exec_preflight(const char *path) {
|
||||
int fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
|
||||
if (fd < 0) return errno;
|
||||
struct stat st;
|
||||
int err = 0;
|
||||
unsigned char magic[4];
|
||||
if (syscall(SYS_fstat, fd, &st) != 0) err = errno;
|
||||
else if (!S_ISREG(st.st_mode)) err = EACCES;
|
||||
else if (syscall(SYS_faccessat, AT_FDCWD, path, X_OK, 0) != 0) err = EACCES;
|
||||
else if (syscall(SYS_read, fd, magic, 2) == 2 && magic[0] == '#' && magic[1] == '!') err = -1;
|
||||
else if (syscall(SYS_pread64, fd, magic, sizeof(magic), 0) != (long)sizeof(magic) ||
|
||||
magic[0] != 0x7f || magic[1] != 'E' || magic[2] != 'L' || magic[3] != 'F') err = ENOEXEC;
|
||||
else err = exec_interp_missing(fd);
|
||||
syscall(SYS_close, fd);
|
||||
return err;
|
||||
}
|
||||
|
||||
// "libddexec.so <path> <argv...>"; NULL when [path] is executed directly, as a "#!" script the
|
||||
// kernel hands to its interpreter is. *err is set when the exec would fail.
|
||||
static char **loader_argv(const char *path, char *const argv[], int *err) {
|
||||
*err = 0;
|
||||
if (exec_directly(path)) return NULL;
|
||||
int preflight = exec_preflight(path);
|
||||
if (preflight == -1) return NULL;
|
||||
*err = preflight;
|
||||
if (*err) return NULL;
|
||||
int argc = 0;
|
||||
while (argv && argv[argc]) argc++;
|
||||
char **out = (char **)malloc(sizeof(char *) * (size_t)(argc + 4));
|
||||
if (!out) {
|
||||
*err = ENOMEM;
|
||||
return NULL;
|
||||
}
|
||||
out[0] = g_exec_loader;
|
||||
out[1] = (char *)path;
|
||||
int n = 2;
|
||||
if (argc == 0) out[n++] = (char *)path;
|
||||
for (int i = 0; i < argc; i++) out[n++] = argv[i];
|
||||
out[n] = NULL;
|
||||
return out;
|
||||
}
|
||||
|
||||
static int run_execve(orig_execve_f_type f, const char *path, char *const argv[], char *const envp[]) {
|
||||
if (in_exec_rehearsal()) {
|
||||
errno = 0;
|
||||
return 0;
|
||||
}
|
||||
return f(path, argv, envp);
|
||||
int err;
|
||||
char **via = loader_argv(path, argv, &err);
|
||||
if (err) {
|
||||
errno = err;
|
||||
return -1;
|
||||
}
|
||||
if (!via) return f(path, argv, envp);
|
||||
int ret = f(g_exec_loader, via, envp);
|
||||
int saved_errno = errno;
|
||||
free(via);
|
||||
errno = saved_errno;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// ldconfig does nothing in The native userland (its cache is for paths that ld.so opens without the hook), but the
|
||||
@@ -701,6 +792,17 @@ static orig_posix_spawn_f_type real_posix_spawn(void) {
|
||||
return orig_posix_spawn;
|
||||
}
|
||||
|
||||
static int run_spawn(orig_posix_spawn_f_type f, pid_t *pid, const char *path, const posix_spawn_file_actions_t *file_actions,
|
||||
const posix_spawnattr_t *attrp, char *const argv[], char *const envp[]) {
|
||||
int err;
|
||||
char **via = loader_argv(path, argv, &err);
|
||||
if (err) return err;
|
||||
if (!via) return f(pid, path, file_actions, attrp, argv, envp);
|
||||
int ret = f(pid, g_exec_loader, file_actions, attrp, via, envp);
|
||||
free(via);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int ddnatv_posix_spawn(pid_t *pid, const char *path,
|
||||
const posix_spawn_file_actions_t *file_actions,
|
||||
const posix_spawnattr_t *attrp,
|
||||
@@ -856,10 +958,10 @@ static int ddnatv_posix_spawn(pid_t *pid, const char *path,
|
||||
ret = ENOMEM;
|
||||
} else {
|
||||
if (orig_posix_spawn) {
|
||||
ret = orig_posix_spawn(pid, ld_so, file_actions, attrp, new_argv, new_envp);
|
||||
ret = run_spawn(orig_posix_spawn, pid, ld_so, file_actions, attrp, new_argv, new_envp);
|
||||
if (ret == EACCES && chmod(ld_so, 0755) == 0) {
|
||||
// ld.so without its exec bit (an unusual restore): fix it once and retry
|
||||
ret = orig_posix_spawn(pid, ld_so, file_actions, attrp, new_argv, new_envp);
|
||||
ret = run_spawn(orig_posix_spawn, pid, ld_so, file_actions, attrp, new_argv, new_envp);
|
||||
}
|
||||
}
|
||||
free(new_argv);
|
||||
@@ -867,7 +969,7 @@ static int ddnatv_posix_spawn(pid_t *pid, const char *path,
|
||||
} else {
|
||||
if (in_root && probe.is_elf && !probe.is_dynamic) prepare_static_program(elf_target);
|
||||
if (orig_posix_spawn) {
|
||||
ret = orig_posix_spawn(pid, target, file_actions, attrp, argv, new_envp);
|
||||
ret = run_spawn(orig_posix_spawn, pid, target, file_actions, attrp, argv, new_envp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,10 +11,28 @@
|
||||
static char g_ddnatv_root[PATH_MAX] = {0};
|
||||
static size_t g_ddnatv_root_len = 0;
|
||||
|
||||
// The path the kernel reports for [path] (/proc/self/fd, getcwd(), realpath()): for a targetSdk 30+
|
||||
// app, Android's own /data/user/0 is a link to /data/data, so the app's paths are not what the kernel
|
||||
// gives back for them. Into out, or path itself when it cannot be resolved; returns out's length.
|
||||
static size_t ddnatv_kernel_path(const char *path, size_t len, char *out, size_t outsz) {
|
||||
static char *(*libc_realpath)(const char *, char *) = NULL;
|
||||
if (!libc_realpath) libc_realpath = (char *(*)(const char *, char *))dlsym(RTLD_NEXT, "realpath");
|
||||
char given[PATH_MAX];
|
||||
if (len >= sizeof(given)) len = sizeof(given) - 1;
|
||||
memcpy(given, path, len);
|
||||
given[len] = '\0';
|
||||
char resolved[PATH_MAX];
|
||||
const char *use = (libc_realpath && libc_realpath(given, resolved)) ? resolved : given;
|
||||
size_t n = strlen(use);
|
||||
if (n >= outsz) n = outsz - 1;
|
||||
memcpy(out, use, n);
|
||||
out[n] = '\0';
|
||||
return n;
|
||||
}
|
||||
|
||||
static void ddnatv_set_root(const char *root, size_t len) {
|
||||
if (!root) len = 0;
|
||||
if (len > sizeof(g_ddnatv_root) - 1) len = sizeof(g_ddnatv_root) - 1;
|
||||
if (len > 0) memcpy(g_ddnatv_root, root, len);
|
||||
if (len > 0) len = ddnatv_kernel_path(root, len, g_ddnatv_root, sizeof(g_ddnatv_root));
|
||||
g_ddnatv_root[len] = '\0';
|
||||
g_ddnatv_root_len = len;
|
||||
}
|
||||
@@ -61,6 +79,11 @@ static void ddnatv_load_binds(const char *spec) {
|
||||
if (colon) *colon = '\0';
|
||||
if (src[0] != '/' || dst[0] != '/' || strcmp(dst, "/") == 0) continue;
|
||||
struct ddnatv_bind *b = &g_binds[g_nbinds++];
|
||||
char kernel_src[PATH_MAX];
|
||||
if (strcmp(src, dst) != 0) {
|
||||
ddnatv_kernel_path(src, strlen(src), kernel_src, sizeof(kernel_src));
|
||||
src = kernel_src;
|
||||
}
|
||||
b->src = strdup(src);
|
||||
b->dst = strdup(dst);
|
||||
b->src_len = strlen(src);
|
||||
|
||||
@@ -7,6 +7,9 @@
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
|
||||
static char g_real_exe[PATH_MAX] = {0};
|
||||
// DDNATV_EXEC: the app's libddexec.so. A targetSdk 29+ app may not execve() its data files; this
|
||||
// executable maps one and starts it as the kernel would ("libddexec.so <file> <argv0> [args...]").
|
||||
static char g_exec_loader[PATH_MAX] = {0};
|
||||
// DDNATV_PRELOAD: guest paths of libraries every program loads before the hook (what
|
||||
// /etc/ld.so.preload does for programs run by the rootfs's own ld.so), and the same as host paths.
|
||||
static char *g_preload_guest = NULL;
|
||||
@@ -34,12 +37,14 @@ static void init_ddnatv_hook(void) {
|
||||
}
|
||||
}
|
||||
ddnatv_load_binds(getenv("DDNATV_BINDS"));
|
||||
const char *exec_loader = getenv("DDNATV_EXEC");
|
||||
if (exec_loader && exec_loader[0] == '/' && strlen(exec_loader) < sizeof(g_exec_loader))
|
||||
ddnatv_copy_cstr(g_exec_loader, sizeof(g_exec_loader), exec_loader);
|
||||
const char *hostname = getenv("DDNATV_HOSTNAME");
|
||||
if (hostname && hostname[0] && strlen(hostname) < sizeof(g_hostname)) ddnatv_copy_cstr(g_hostname, sizeof(g_hostname), hostname);
|
||||
const char *link_root = getenv("DDNATV_LINK_ROOT");
|
||||
if (link_root && link_root[0] == '/' && strlen(link_root) < sizeof(g_link_root)) {
|
||||
ddnatv_copy_cstr(g_link_root, sizeof(g_link_root), link_root);
|
||||
g_link_root_len = strlen(g_link_root);
|
||||
g_link_root_len = ddnatv_kernel_path(link_root, strlen(link_root), g_link_root, sizeof(g_link_root));
|
||||
while (g_link_root_len > 1 && g_link_root[g_link_root_len - 1] == '/') g_link_root[--g_link_root_len] = 0;
|
||||
}
|
||||
const char *preload = getenv("DDNATV_PRELOAD");
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
Built by `tools/proot/build.sh` with the NDK on top of termux/proot `4dba3afb` (Termux's
|
||||
5.1.107-70 package, the proot the Linux runtime ships at `opt/android-host/proot`; every
|
||||
`file.c:line` site in that binary matches this commit). talloc is linked in statically from
|
||||
Samba's release tarball, so the apk carries one self-contained `libproot.so` plus
|
||||
`libproot-loader.so`, and `LinuxRuntime` prefers them over the runtime's copy.
|
||||
Samba's release tarball, so the proot add-on pack carries one self-contained `libproot.so`, which
|
||||
Android's linker runs from app data, and the apk carries `libproot-loader.so`, which proot execs.
|
||||
|
||||
Ported from WinNative (`feature/proot-enhancements`, e7af0f24):
|
||||
|
||||
|
||||
@@ -17,8 +17,10 @@ PACKED = {'assets/linuxfs/usr/local/lib/droiddeck-xbox/': 'xbox', 'assets/linuxf
|
||||
'assets/linuxfs/usr/local/lib/droiddeck-ubisoft/': 'ubisoft', 'assets/linuxfs/usr/local/lib/droiddeck-rockstar/': 'rockstar',
|
||||
'assets/linuxfs/usr/local/lib/droiddeck-dirt/': 'dirt',
|
||||
'assets/directaudio/': 'directaudio', 'assets/hybris/': 'powervr'}
|
||||
PACKED_FILES = {'lib/arm64-v8a/libproot.so': ('proot', 'libproot.so'),
|
||||
'lib/arm64-v8a/libproot-loader.so': ('proot', 'libproot-loader.so')}
|
||||
# DrWeb flags proot itself as Tool.Linux.Proot; the app runs the pack's copy through Android's linker.
|
||||
PACKED_FILES = {'lib/arm64-v8a/libproot.so': ('proot', 'libproot.so')}
|
||||
# The loader is execve'd, which targetSdk 29+ allows only from the APK's native library directory.
|
||||
PROOT_LOADER = {'lib/arm64-v8a/libproot-loader.so': 'libproot-loader.so'}
|
||||
PACKED_LIBRARIES = ['lib/arm64-v8a/libopencv_java4.so', 'lib/arm64-v8a/libmediapipe_tasks_jni.so']
|
||||
PACK_NAMES = ['proot', 'directaudio', 'tracking', 'xbox', 'comet', 'butler', 'powervr', 'ubisoft', 'rockstar', 'dirt']
|
||||
SOURCE_PATHS = ['app/build.gradle', 'app/src/main/cpp/fakeinput_steam.cpp', 'app/src/main/assets/pulseaudio.tzst',
|
||||
@@ -227,6 +229,9 @@ def check_apk(apk, root=ROOT):
|
||||
data = verify(root)
|
||||
packs = {name: pack_digests(root, name) for name in PACK_NAMES
|
||||
if name not in ('ubisoft', 'rockstar', 'dirt') or (root / PACKS / (name + '.tar.zst')).is_file()}
|
||||
for name, rel in PROOT_LOADER.items():
|
||||
if packs['proot'].get(rel) != data['outputs'][name]['sha256']:
|
||||
raise ValueError('Add-on pack proot is missing or has a stale ' + rel)
|
||||
with ZipFile(apk) as package:
|
||||
names = set(package.namelist())
|
||||
for name in sorted(names):
|
||||
|
||||
@@ -59,6 +59,8 @@ class RuntimeInputsTest(unittest.TestCase):
|
||||
proot = {}
|
||||
for name, (pack, rel) in runtime.PACKED_FILES.items():
|
||||
proot[rel] = (self.root / runtime.outputs(self.root)[name]).read_bytes()
|
||||
for name, rel in runtime.PROOT_LOADER.items():
|
||||
proot[rel] = (self.root / runtime.outputs(self.root)[name]).read_bytes()
|
||||
self.pack('proot', proot)
|
||||
self.pack('butler', {'butler': b'butler', '7z.so': b'7z', 'libc7zip.so': b'c7zip', 'version': b'15.32.0'})
|
||||
for prefix, name in runtime.PACKED.items():
|
||||
@@ -84,6 +86,19 @@ class RuntimeInputsTest(unittest.TestCase):
|
||||
with self.assertRaisesRegex(ValueError, 'proot pack provides'):
|
||||
runtime.check_apk(apk, self.root)
|
||||
|
||||
def test_a_stale_proot_loader_in_the_pack_is_rejected(self):
|
||||
apk = self.current_apk(self.root / 'current.apk')
|
||||
loader = (self.root / 'app/src/main/jniLibs/arm64-v8a/libproot-loader.so').read_bytes()
|
||||
self.pack('proot', {'libproot.so': (self.root / 'app/src/main/jniLibs/arm64-v8a/libproot.so').read_bytes(),
|
||||
'libproot-loader.so': loader + b'older'})
|
||||
with self.assertRaisesRegex(ValueError, 'proot is missing or has a stale libproot-loader.so'):
|
||||
runtime.check_apk(apk, self.root)
|
||||
|
||||
def test_only_the_proot_loader_ships_in_the_apk(self):
|
||||
self.assertFalse(self.in_apk('lib/arm64-v8a/libproot.so'))
|
||||
self.assertTrue(self.in_apk('lib/arm64-v8a/libproot-loader.so'))
|
||||
runtime.check_apk(self.current_apk(self.root / 'current.apk'), self.root)
|
||||
|
||||
def test_missing_proot_cannot_be_attested(self):
|
||||
(self.root / 'app/src/main/jniLibs/arm64-v8a/libproot.so').unlink()
|
||||
with self.assertRaisesRegex(ValueError, 'libproot.so'):
|
||||
|
||||
Reference in new issue
Block a user