mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 22:36:00 +00:00
fix(proot): harden relay state and unwinding
This commit is contained in:
1 parent
ddb8ed32cb
commit
4998b04636
10 files changed
+553
-64
No files matched your search
@@ -39,7 +39,7 @@ static int svc_cache_name(const struct dl_phdr_info *info, char *path, size_t si
|
||||
id[2 * j + 1] = hex[b & 15];
|
||||
}
|
||||
id[2 * h.n_descsz] = 0;
|
||||
int n = snprintf(path, size, "/tmp/.droiddeck-svc-v1-%d-%s", svc_android_api, id);
|
||||
int n = snprintf(path, size, "/tmp/.droiddeck-svc-v2-%d-%s", svc_android_api, id);
|
||||
return n > 0 && (size_t)n < size;
|
||||
}
|
||||
at += name + desc;
|
||||
@@ -57,7 +57,7 @@ static int svc_cache_read(const struct dl_phdr_info *info, const char *path) {
|
||||
st.st_uid == (uid_t)sc(SYS_getuid, 0, 0, 0, 0, 0) && !(st.st_mode & 0077) &&
|
||||
st.st_size == sizeof table && sc(SYS_read, fd, (long)&table, sizeof table, 0, 0) == sizeof table;
|
||||
sc(SYS_close, fd, 0, 0, 0, 0);
|
||||
if (!valid || table.magic != 0x314356534444ULL || table.api != (unsigned)svc_android_api ||
|
||||
if (!valid || table.magic != 0x324356534444ULL || table.api != (unsigned)svc_android_api ||
|
||||
!table.count || table.count > 256 || table.count > 256 - svc_planned) return 0;
|
||||
for (unsigned i = 0; i < table.count; i++) {
|
||||
uint64_t offset = table.offset[i];
|
||||
@@ -75,7 +75,7 @@ static int svc_cache_read(const struct dl_phdr_info *info, const char *path) {
|
||||
|
||||
static void svc_cache_write(const struct dl_phdr_info *info, const char *path, size_t first) {
|
||||
if (svc_planned == first || svc_planned == 256) return;
|
||||
struct svc_cached_sites table = {.magic = 0x314356534444ULL, .api = svc_android_api, .count = svc_planned - first};
|
||||
struct svc_cached_sites table = {.magic = 0x324356534444ULL, .api = svc_android_api, .count = svc_planned - first};
|
||||
for (unsigned i = 0; i < table.count; i++)
|
||||
table.offset[i] = (uintptr_t)svc_plan[first + i].site - info->dlpi_addr;
|
||||
char temporary[256];
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
static int svc_android_api;
|
||||
static __thread struct { long head, tid; } svc_robust __attribute__((tls_model("initial-exec")));
|
||||
|
||||
static int svc_host_android_api(void) {
|
||||
const char *value = getenv("PROOT_FP_ANDROID_API");
|
||||
@@ -34,7 +33,7 @@ static int svc_supported(int nr) {
|
||||
case SYS_truncate: case SYS_statfs: case SYS_getxattr: case SYS_lgetxattr:
|
||||
case SYS_listxattr: case SYS_llistxattr: case SYS_inotify_add_watch: case SYS_getcwd:
|
||||
return 1;
|
||||
case SYS_set_robust_list: case SYS_get_robust_list: case SYS_faccessat2:
|
||||
case SYS_faccessat2:
|
||||
return svc_android_api > 0;
|
||||
case SYS_clone3:
|
||||
return svc_android_api > 0 && svc_android_api < 35;
|
||||
@@ -45,24 +44,13 @@ static int svc_supported(int nr) {
|
||||
static long svc_blocked(const long *r) {
|
||||
if (!svc_android_api) return FP_SLOW;
|
||||
if (r[8] == SYS_faccessat2 || (r[8] == SYS_clone3 && svc_android_api < 35)) return -ENOSYS;
|
||||
if (r[8] == SYS_set_robust_list) {
|
||||
svc_robust.head = r[0];
|
||||
svc_robust.tid = sc(SYS_gettid, 0, 0, 0, 0, 0);
|
||||
return -ENOSYS;
|
||||
}
|
||||
if (r[8] == SYS_get_robust_list) {
|
||||
long tid = sc(SYS_gettid, 0, 0, 0, 0, 0);
|
||||
if ((r[0] && r[0] != tid) || svc_robust.tid != tid) return FP_SLOW;
|
||||
if (!svc_robust.head) return -ENOSYS;
|
||||
size_t size = 3 * sizeof(long);
|
||||
if (svc_memory(&svc_robust.head, r[1], sizeof(long), 1) != sizeof(long) ||
|
||||
svc_memory(&size, r[2], sizeof size, 1) != sizeof size) return -EFAULT;
|
||||
return 0;
|
||||
}
|
||||
return FP_SLOW;
|
||||
}
|
||||
|
||||
static long svc_path_call(const long *r) {
|
||||
#ifdef FP_SVC_TEST_HOOK
|
||||
FP_SVC_TEST_HOOK(r);
|
||||
#endif
|
||||
char path[PATH_MAX], other[PATH_MAX];
|
||||
struct timespec times[2];
|
||||
const struct timespec *when = (const struct timespec *)r[2];
|
||||
|
||||
@@ -47,13 +47,15 @@ static inline int svc_branch(uintptr_t from, uintptr_t to, uint32_t *out) {
|
||||
#include <asm/hwcap.h>
|
||||
#include <link.h>
|
||||
#include <sys/uio.h>
|
||||
#include <signal.h>
|
||||
|
||||
static __thread int svc_busy __attribute__((tls_model("initial-exec")));
|
||||
static __thread uintptr_t svc_busy __attribute__((tls_model("initial-exec")));
|
||||
static unsigned svc_sites, svc_pages;
|
||||
static unsigned long svc_fast, svc_slow;
|
||||
static unsigned long long svc_scan_ns, svc_patch_ns;
|
||||
|
||||
#include "svc-dispatch.h"
|
||||
#include "svc-stack.h"
|
||||
|
||||
static long svc_original(const long *r) {
|
||||
register long x0 __asm__("x0") = r[0];
|
||||
@@ -71,10 +73,12 @@ __attribute__((used, visibility("hidden")))
|
||||
long fp_svc_dispatch(const long *r) {
|
||||
int saved_errno = errno;
|
||||
long result = svc_blocked(r);
|
||||
if (result == FP_SLOW && !svc_busy) {
|
||||
svc_busy = 1;
|
||||
result = svc_path_call(r);
|
||||
svc_busy = 0;
|
||||
uintptr_t frame = (uintptr_t)r;
|
||||
if (result == FP_SLOW && (!svc_busy || frame >= svc_busy)) {
|
||||
uintptr_t previous = svc_busy;
|
||||
svc_busy = frame;
|
||||
if (svc_stack_room(frame)) { result = svc_path_call(r); svc_busy = 0; }
|
||||
else svc_busy = previous;
|
||||
}
|
||||
if (result == FP_SLOW) {
|
||||
__atomic_fetch_add(&svc_slow, 1, __ATOMIC_RELAXED);
|
||||
@@ -88,7 +92,8 @@ void fp_svc_entry(void) __attribute__((visibility("hidden")));
|
||||
/* A replaced svc may have live values in every register, including NZCV and SIMD. */
|
||||
__asm__(
|
||||
".text\n.p2align 4\n.hidden fp_svc_entry\n.type fp_svc_entry,%function\nfp_svc_entry:\n"
|
||||
"bti j\nsub sp, sp, #800\n"
|
||||
".cfi_startproc\n.cfi_def_cfa sp, 16\n.cfi_return_column 16\n"
|
||||
"bti j\nsub sp, sp, #800\n.cfi_def_cfa_offset 816\n"
|
||||
"stp x0, x1, [sp, #0]\nstp x2, x3, [sp, #16]\n"
|
||||
"stp x4, x5, [sp, #32]\nstp x6, x7, [sp, #48]\n"
|
||||
"stp x8, x9, [sp, #64]\nstp x10, x11, [sp, #80]\n"
|
||||
@@ -97,6 +102,13 @@ __asm__(
|
||||
"stp x22, x23, [sp, #160]\nstp x24, x25, [sp, #176]\n"
|
||||
"stp x26, x27, [sp, #192]\nstp x28, x29, [sp, #208]\n"
|
||||
"stp x30, x17, [sp, #224]\nmrs x0, nzcv\nstr x0, [sp, #240]\n"
|
||||
"ldr x0, [x17, #8]\nstr x0, [sp, #784]\n"
|
||||
".cfi_offset 16, -32\n.cfi_offset 19, -680\n.cfi_offset 20, -672\n"
|
||||
".cfi_offset 21, -664\n.cfi_offset 22, -656\n.cfi_offset 23, -648\n"
|
||||
".cfi_offset 24, -640\n.cfi_offset 25, -632\n.cfi_offset 26, -624\n"
|
||||
".cfi_offset 27, -616\n.cfi_offset 28, -608\n.cfi_offset 29, -600\n.cfi_offset 30, -592\n"
|
||||
".cfi_offset 72, -432\n.cfi_offset 73, -416\n.cfi_offset 74, -400\n.cfi_offset 75, -384\n"
|
||||
".cfi_offset 76, -368\n.cfi_offset 77, -352\n.cfi_offset 78, -336\n.cfi_offset 79, -320\n"
|
||||
"mrs x0, fpcr\nstr x0, [sp, #768]\nmrs x0, fpsr\nstr x0, [sp, #776]\n"
|
||||
"stp q0, q1, [sp, #256]\nstp q2, q3, [sp, #288]\n"
|
||||
"stp q4, q5, [sp, #320]\nstp q6, q7, [sp, #352]\n"
|
||||
@@ -125,7 +137,7 @@ __asm__(
|
||||
"ldp x22, x23, [sp, #160]\nldp x24, x25, [sp, #176]\n"
|
||||
"ldp x26, x27, [sp, #192]\nldp x28, x29, [sp, #208]\n"
|
||||
"ldr x30, [sp, #224]\nldr x16, [sp, #232]\n"
|
||||
"add sp, sp, #800\nbr x16\n.size fp_svc_entry, .-fp_svc_entry\n"
|
||||
"add sp, sp, #800\n.cfi_def_cfa_offset 16\nbr x16\n.cfi_endproc\n.size fp_svc_entry, .-fp_svc_entry\n"
|
||||
);
|
||||
|
||||
static int svc_emit(uint32_t *code, uint32_t *site, uint32_t *branch) {
|
||||
@@ -133,13 +145,15 @@ static int svc_emit(uint32_t *code, uint32_t *site, uint32_t *branch) {
|
||||
if (!svc_branch((uintptr_t)site, (uintptr_t)code, branch) ||
|
||||
!svc_branch((uintptr_t)(code + 5), (uintptr_t)(site + 1), &back)) return 0;
|
||||
code[0] = 0xa9bf47f0U; /* stp x16, x17, [sp, #-16]! */
|
||||
code[1] = 0x580000b0U; /* ldr x16, literal at +24 */
|
||||
code[1] = 0x580000f0U; /* ldr x16, literal at +32 */
|
||||
code[2] = 0x10000051U; /* adr x17, +8 */
|
||||
code[3] = 0xd61f0200U; /* br x16 */
|
||||
code[4] = 0xa8c147f0U; /* ldp x16, x17, [sp], #16 */
|
||||
code[5] = back;
|
||||
uintptr_t entry = (uintptr_t)fp_svc_entry;
|
||||
memcpy(code + 6, &entry, sizeof entry);
|
||||
uintptr_t resume = (uintptr_t)(site + 1);
|
||||
memcpy(code + 6, &resume, sizeof resume);
|
||||
memcpy(code + 8, &entry, sizeof entry);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -190,6 +204,8 @@ static uint32_t *svc_relay_page(uintptr_t site, size_t page) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#include "svc-publish.h"
|
||||
|
||||
static int svc_single_thread(void) {
|
||||
struct entry { uint64_t ino; int64_t off; unsigned short len; unsigned char type; char name[]; };
|
||||
char buf[4096] __attribute__((aligned(8)));
|
||||
@@ -245,32 +261,7 @@ __attribute__((constructor)) static void svc_patch_init(void) {
|
||||
dl_iterate_phdr(svc_object, NULL);
|
||||
svc_scan_ns = svc_clock() - start;
|
||||
start = svc_clock();
|
||||
for (size_t first = 0; first < svc_planned;) {
|
||||
uint32_t *relay = svc_relay_page((uintptr_t)svc_plan[first].site, page);
|
||||
if (!relay) { first++; continue; }
|
||||
size_t end = first;
|
||||
for (; end < svc_planned && (end - first + 1) * 32 <= page; end++) {
|
||||
uint32_t *code = relay + (end - first) * 8;
|
||||
if (!svc_emit(code, svc_plan[end].site, &svc_plan[end].branch)) break;
|
||||
}
|
||||
__builtin___clear_cache((char *)relay, (char *)relay + page);
|
||||
if (end == first || mprotect(relay, page, PROT_READ | PROT_EXEC)) {
|
||||
munmap(relay, page);
|
||||
first++;
|
||||
continue;
|
||||
}
|
||||
unsigned patched = 0;
|
||||
for (size_t i = first; i < end; i++) {
|
||||
struct svc_site *s = &svc_plan[i];
|
||||
long written = sc(SYS_pwrite64, mem, (long)&s->branch, sizeof s->branch, (long)s->site, 0);
|
||||
if (written != sizeof s->branch) continue;
|
||||
__builtin___clear_cache((char *)s->site, (char *)(s->site + 1));
|
||||
patched++;
|
||||
}
|
||||
if (patched) { svc_sites += patched; svc_pages++; }
|
||||
else munmap(relay, page);
|
||||
first = end;
|
||||
}
|
||||
svc_publish(mem, page);
|
||||
sc(SYS_close, mem, 0, 0, 0, 0);
|
||||
svc_patch_ns = svc_clock() - start;
|
||||
if (getenv("PROOT_FP_STATS")) atexit(svc_stats);
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
static void svc_publish(int mem, size_t page) {
|
||||
for (size_t first = 0; first < svc_planned;) {
|
||||
uint32_t *relay = svc_relay_page((uintptr_t)svc_plan[first].site, page);
|
||||
if (!relay) { first++; continue; }
|
||||
size_t end = first;
|
||||
for (; end < svc_planned && (end - first + 1) * 40 <= page; end++) {
|
||||
uint32_t *code = relay + (end - first) * 10;
|
||||
if (!svc_emit(code, svc_plan[end].site, &svc_plan[end].branch)) break;
|
||||
}
|
||||
__builtin___clear_cache((char *)relay, (char *)relay + page);
|
||||
if (end == first || mprotect(relay, page, PROT_READ | PROT_EXEC)) {
|
||||
munmap(relay, page);
|
||||
first++;
|
||||
continue;
|
||||
}
|
||||
unsigned patched = 0;
|
||||
for (size_t i = first; i < end; i++) {
|
||||
struct svc_site *s = &svc_plan[i];
|
||||
long written = sc(SYS_pwrite64, mem, (long)&s->branch, sizeof s->branch, (long)s->site, 0);
|
||||
if (written != sizeof s->branch) continue;
|
||||
__builtin___clear_cache((char *)s->site, (char *)(s->site + 1));
|
||||
patched++;
|
||||
}
|
||||
if (patched) { svc_sites += patched; svc_pages++; }
|
||||
else munmap(relay, page);
|
||||
first = end;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
#include <sys/resource.h>
|
||||
#include <string.h>
|
||||
|
||||
static __thread uintptr_t svc_stack_low __attribute__((tls_model("initial-exec")));
|
||||
static __thread uintptr_t svc_stack_high __attribute__((tls_model("initial-exec")));
|
||||
static __thread uintptr_t svc_stack_gap __attribute__((tls_model("initial-exec")));
|
||||
static __thread int svc_stack_main __attribute__((tls_model("initial-exec")));
|
||||
|
||||
static int svc_hex(const char **text, uintptr_t *value) {
|
||||
const char *p = *text;
|
||||
uintptr_t n = 0;
|
||||
int digits = 0;
|
||||
for (;; p++) {
|
||||
unsigned c = (unsigned char)*p;
|
||||
unsigned d = c >= '0' && c <= '9' ? c - '0' : c >= 'a' && c <= 'f' ? c - 'a' + 10 : 16;
|
||||
if (d == 16) break;
|
||||
if (++digits > (int)(2 * sizeof n)) return 0;
|
||||
n = (n << 4) | d;
|
||||
}
|
||||
*text = p;
|
||||
*value = n;
|
||||
return digits > 0;
|
||||
}
|
||||
|
||||
static int svc_stack_line(const char *line, uintptr_t sp, uintptr_t *low, uintptr_t *high) {
|
||||
uintptr_t a, b;
|
||||
if (!svc_hex(&line, &a) || *line++ != '-' || !svc_hex(&line, &b) ||
|
||||
*line++ != ' ' || line[0] != 'r' || line[1] != 'w' || a >= b || sp < a || sp >= b) return 0;
|
||||
*low = a;
|
||||
*high = b;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) int svc_stack_find(uintptr_t sp) {
|
||||
char chunk[512], line[96];
|
||||
size_t at = 0;
|
||||
long fd = sc(SYS_openat, AT_FDCWD, (long)"/proc/self/maps", O_RDONLY | O_CLOEXEC, 0, 0);
|
||||
if (fd < 0) return 0;
|
||||
long n;
|
||||
uintptr_t low = 0, high = 0, previous = 0;
|
||||
int found = 0;
|
||||
while (!found && (n = sc(SYS_read, fd, (long)chunk, sizeof chunk, 0, 0)) > 0) {
|
||||
for (long i = 0; i < n; i++) {
|
||||
if (chunk[i] == '\n') {
|
||||
line[at] = 0;
|
||||
found = svc_stack_line(line, sp, &low, &high);
|
||||
if (!found) {
|
||||
const char *p = line;
|
||||
uintptr_t a, b;
|
||||
if (svc_hex(&p, &a) && *p++ == '-' && svc_hex(&p, &b) && b <= sp) previous = b;
|
||||
}
|
||||
at = 0;
|
||||
if (found) break;
|
||||
} else if (at < sizeof line - 1) line[at++] = chunk[i];
|
||||
}
|
||||
}
|
||||
sc(SYS_close, fd, 0, 0, 0, 0);
|
||||
if (found) {
|
||||
svc_stack_low = 0;
|
||||
svc_stack_high = high;
|
||||
svc_stack_main = strstr(line, "[stack]") != NULL;
|
||||
svc_stack_gap = previous < low && low - previous > 16 * 1024 * 1024 ? previous + 16 * 1024 * 1024 : low;
|
||||
svc_stack_low = low;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
static int svc_stack_room(uintptr_t sp) {
|
||||
stack_t alt;
|
||||
if (sc(SYS_sigaltstack, 0, (long)&alt, 0, 0, 0) < 0 || (alt.ss_flags & SS_ONSTACK)) return 0;
|
||||
if (!svc_stack_low || sp < svc_stack_low || sp >= svc_stack_high)
|
||||
if (!svc_stack_find(sp)) return 0;
|
||||
uintptr_t low = svc_stack_low;
|
||||
if (svc_stack_main) {
|
||||
struct rlimit limit;
|
||||
if (sc(SYS_prlimit64, 0, RLIMIT_STACK, 0, (long)&limit, 0) < 0) return 0;
|
||||
uintptr_t grow = limit.rlim_cur >= svc_stack_high ? 0 : svc_stack_high - limit.rlim_cur;
|
||||
if (grow < svc_stack_gap) grow = svc_stack_gap;
|
||||
if (grow < low) low = grow;
|
||||
}
|
||||
/* Path translation nests several PATH_MAX-sized frames. */
|
||||
return sp - low >= 96 * 1024;
|
||||
}
|
||||
Reference in new issue
Block a user