perf: stop proot's fake_id0 and ioctl traps from stopping every stat, sendmsg and GPU ioctl

fake_id0 (loaded by -i for Xwayland's set*id) now traces only the set*id
family and the xattr fixups when the ids given are proot's own, and the
seccomp filter traces ioctl only for the termios and FICLONE requests
proot rewrites. The faccessat2 and statx exit stops are dropped on
kernels that have the calls.
This commit is contained in:
maxjivi05 committed 2026-09-29 18:44:32 -04:00
1 parent 33460f7342
commit cc37cc8a4d
3 files changed
+287

No files matched your search

+15
View File
@@ -55,3 +55,18 @@ Added by DroidDeck:
and `setdomainname` and leaves the auxv as the kernel wrote it. On an SD 8 Gen 2 guest this
took a futex ping-pong from 467 to 97 us, `epoll_pwait` from 60 to 0.8 us and `fcntl` from
40-107 to 0.4 us, and `clock_gettime` back to the vDSO.
- `0012-fake_id0-identity-only.patch` - `-i uid:gid` (for Xwayland's setgid/setuid before it runs
xkbcomp) loads fake_id0, whose filter traps every `fstat`/`newfstatat`/`stat` (entry and exit),
every `sendmsg` (all Wayland, X11, Chromium and PulseAudio traffic), `socket`, `getsockopt`, the
`get*id` family and the chown/chmod family. When the ids given are the ones proot really has and
are not 0, every one of those handlers is a no-op; fake_id0 now traces only the `set*id` family
and the xattr permission fixups, and leaves set-user-ID bits alone on `execve` (Android mounts the
app's data `nosuid`, so the kernel would not honour them either), which keeps the ids fixed. On an
x86_64 host build under the same options: `fstat` 41.7 -> 1.3 us, `sendmsg` 18.0 -> 2.5 us.
- `0013-seccomp-ioctl-by-request-and-kernel-exit-stops.patch` - every `ioctl` stopped on entry and
exit, which is every GPU submit and wait. The filter now traces only the requests enter.c and
exit.c act on (`TCSETSF`, the four termios2 requests, `FICLONE`) and allows the rest at once, and
the ioctl block is emitted first (after WinNative 53836ca9, which traces only the termios2 ones).
The `faccessat2` exit stop (termux 5ba8b95, for glibc's ENOSYS fallback on kernels before 5.8) and
the `statx` one (emulation on kernels before 4.11) are dropped when the running kernel is newer.
Host build: `ioctl` 31.6 -> 0.5 us; `stat`/`statx`/`faccessat2` about 44 -> 28 us (one stop).
@@ -0,0 +1,103 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: DroidDeck <droiddeck@users.noreply.github.com>
Date: Tue, 29 Sep 2026 00:00:00 +0000
Subject: [PATCH] fake_id0: only trace the set*id and xattr calls when the ids
are PRoot's own
---
src/extension/fake_id0/config.h | 5 ++++
src/extension/fake_id0/fake_id0.c | 44 ++++++++++++++++++++++++++++++-
2 files changed, 48 insertions(+), 1 deletion(-)
diff --git a/src/extension/fake_id0/config.h b/src/extension/fake_id0/config.h
index 2e78783..5227ced 100644
--- a/src/extension/fake_id0/config.h
+++ b/src/extension/fake_id0/config.h
@@ -2,6 +2,7 @@
#define FAKE_ID0_CONFIG_H
#include <sys/types.h> /* uid_t, gid_t */
+#include <stdbool.h> /* bool, */
typedef struct {
uid_t ruid;
@@ -15,6 +16,10 @@ typedef struct {
gid_t fsgid;
mode_t umask;
+
+ /* The emulated ids are the ones PRoot really has: see
+ * identity_sysnums in fake_id0.c. */
+ bool identity;
} Config;
#endif /* FAKE_ID0_CONFIG_H */
diff --git a/src/extension/fake_id0/fake_id0.c b/src/extension/fake_id0/fake_id0.c
index 9477f1c..fe08372 100644
--- a/src/extension/fake_id0/fake_id0.c
+++ b/src/extension/fake_id0/fake_id0.c
@@ -352,6 +352,40 @@ static FilteredSysnum filtered_sysnums[] = {
FILTERED_SYSNUM_END,
};
+/* When the emulated ids are the ones PRoot really has, and the tracee
+ * is not root, every handler for the syscalls above but these is a
+ * no-op: stat and get*id report the real ids already, SCM_CREDENTIALS
+ * and SO_PEERCRED carry them, chown swaps an id for itself, and the
+ * permission and chroot fixups only apply to euid 0. The set*id
+ * family is still emulated (Android's app seccomp policy traps it) and
+ * so are the xattr permission fixups, which apply to any euid. An
+ * unprivileged set*id can only take an id the tracee already holds,
+ * and set-user-ID bits are not honoured in this mode (Android mounts
+ * the app's data nosuid, so the kernel would not honour them either),
+ * so the ids never change and the list stays sufficient. */
+static FilteredSysnum identity_sysnums[] = {
+ { PR_fsetxattr, FILTER_SYSEXIT },
+ { PR_lsetxattr, FILTER_SYSEXIT },
+ { PR_setfsgid, FILTER_SYSEXIT },
+ { PR_setfsgid32, FILTER_SYSEXIT },
+ { PR_setfsuid, FILTER_SYSEXIT },
+ { PR_setfsuid32, FILTER_SYSEXIT },
+ { PR_setgid, FILTER_SYSEXIT },
+ { PR_setgid32, FILTER_SYSEXIT },
+ { PR_setregid, FILTER_SYSEXIT },
+ { PR_setregid32, FILTER_SYSEXIT },
+ { PR_setresgid, FILTER_SYSEXIT },
+ { PR_setresgid32, FILTER_SYSEXIT },
+ { PR_setresuid, FILTER_SYSEXIT },
+ { PR_setresuid32, FILTER_SYSEXIT },
+ { PR_setreuid, FILTER_SYSEXIT },
+ { PR_setreuid32, FILTER_SYSEXIT },
+ { PR_setuid, FILTER_SYSEXIT },
+ { PR_setuid32, FILTER_SYSEXIT },
+ { PR_setxattr, FILTER_SYSEXIT },
+ FILTERED_SYSNUM_END,
+};
+
/**
* Restore the @node->mode for the given @node->path.
*
@@ -1051,6 +1085,11 @@ static int handle_sysexit_start(Tracee *tracee, Config *config) {
if ((int) result < 0 || tracee->status < 0 || sysnum != PR_execve)
return 0;
+ /* The auxv already carries the real ids, and set-user-ID bits
+ * are not honoured in this mode: see identity_sysnums. */
+ if (config->identity)
+ return 0;
+
/* This has to be done before PRoot pushes the load
* script into tracee's stack. */
if (!tracee->skip_proot_loader)
@@ -1119,7 +1158,10 @@ int fake_id0_callback(Extension *extension, ExtensionEvent event, intptr_t data1
/* Set the umask to the typical linux value. */
config->umask = 022;
- extension->filtered_sysnums = filtered_sysnums;
+ config->identity = (uid != 0 && uid == (int) getuid() && gid == (int) getgid());
+ extension->filtered_sysnums = config->identity
+ ? identity_sysnums
+ : filtered_sysnums;
return 0;
}
@@ -0,0 +1,169 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: DroidDeck <droiddeck@users.noreply.github.com>
Date: Tue, 29 Sep 2026 00:00:00 +0000
Subject: [PATCH] seccomp: trace ioctl by request, drop exit stops the kernel
does not need
---
src/syscall/seccomp.c | 98 ++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 97 insertions(+), 1 deletion(-)
diff --git a/src/syscall/seccomp.c b/src/syscall/seccomp.c
index 0b381bb..1116ab2 100644
--- a/src/syscall/seccomp.c
+++ b/src/syscall/seccomp.c
@@ -39,6 +39,11 @@
#include <stdint.h> /* uint*_t, UINT*_MAX, */
#include <stdbool.h>
#include <sys/resource.h>
+#include <sys/utsname.h> /* uname(2), */
+#include <stdio.h> /* sscanf(3), */
+#include <asm/ioctls.h> /* TCGETS2, TCSETS*2, */
+#include <asm/termbits.h> /* struct termios2, */
+#include <linux/fs.h> /* FICLONE, */
#include <assert.h> /* assert(3), */
#include "syscall/seccomp.h"
@@ -166,6 +171,46 @@ static int add_trace_prlimit_stack(struct sock_fprog *program, word_t syscall, i
return add_statements(program, LENGTH_TRACE_PRLIMIT_STACK, statements);
}
+/**
+ * Append to @program->filter the statements that trace @syscall
+ * (ioctl) only for the requests enter.c and exit.c act on - the
+ * termios requests Android's policy denies an app on a pty, rewritten
+ * to ones it allows, and FICLONE, whose EACCES is turned into
+ * EOPNOTSUPP - and allow any other request at once. Every GPU submit
+ * and wait is an ioctl, so they no longer stop in the tracer, and the
+ * block is emitted first so they are also spared the rest of the list.
+ * (After WinNative, maxjivi05, 53836ca9.)
+ */
+static int add_trace_ioctl_requests(struct sock_fprog *program, word_t syscall, int flag)
+{
+ const size_t request_offset = offsetof(struct seccomp_data, args[1]);
+
+ #define LENGTH_TRACE_IOCTL 10
+ struct sock_filter statements[LENGTH_TRACE_IOCTL] = {
+ /* Not this syscall: skip the block, the accumulator
+ * still holds the syscall number. */
+ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, syscall, 0, 9),
+
+ /* Load the request's low word into the accumulator. */
+ BPF_STMT(BPF_LD + BPF_W + BPF_ABS, request_offset),
+
+ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, TCSETSF, 5, 0),
+ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, TCGETS2, 4, 0),
+ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, TCSETS2, 3, 0),
+ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, TCSETSW2, 2, 0),
+ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, TCSETSF2, 1, 0),
+ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (uint32_t) FICLONE, 0, 1),
+
+ BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_TRACE + flag),
+ BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW)
+ };
+
+ DEBUG_FILTER("FILTER: trace if syscall == %ld and a termios2/TCSETSF/FICLONE request\n",
+ syscall);
+
+ return add_statements(program, LENGTH_TRACE_IOCTL, statements);
+}
+
static size_t trace_length(Sysnum sysnum)
{
switch (sysnum) {
@@ -174,6 +219,8 @@ static size_t trace_length(Sysnum sysnum)
return LENGTH_TRACE_SYSCALL_IF_ARG;
case PR_prlimit64:
return LENGTH_TRACE_PRLIMIT_STACK;
+ case PR_ioctl:
+ return LENGTH_TRACE_IOCTL;
default:
return LENGTH_TRACE_SYSCALL;
}
@@ -191,6 +238,8 @@ static int add_trace(struct sock_fprog *program, Sysnum sysnum, word_t syscall,
return add_trace_syscall_if_arg(program, syscall, 0, RLIMIT_STACK, flag);
case PR_prlimit64:
return add_trace_prlimit_stack(program, syscall, flag);
+ case PR_ioctl:
+ return add_trace_ioctl_requests(program, syscall, flag);
default:
return add_trace_syscall(program, syscall, flag);
}
@@ -329,7 +378,7 @@ static int set_seccomp_filters(const FilteredSysnum *sysnums)
struct sock_fprog program = { .len = 0, .filter = NULL };
size_t section_length;
- size_t i, j, k;
+ size_t i, j, k, pass;
int status;
status = new_program_filter(&program);
@@ -355,8 +404,14 @@ static int set_seccomp_filters(const FilteredSysnum *sysnums)
if (status < 0)
goto end;
+ /* Two passes: ioctl first, it is by far the most frequent
+ * syscall and almost every one of them is allowed. */
+ for (pass = 0; pass < 2; pass++)
for (j = 0; j < seccomp_archs[i].nb_abis; j++) {
for (k = 0; sysnums[k].value != PR_void; k++) {
+ if ((sysnums[k].value == PR_ioctl) != (pass == 0))
+ continue;
+
/* Get the architecture specific syscall number. */
syscall = detranslate_sysnum(seccomp_archs[i].abis[j], sysnums[k].value);
if (syscall == SYSCALL_AVOIDER)
@@ -572,12 +627,53 @@ int enable_syscall_filtering(const Tracee *tracee)
return 0;
}
+/**
+ * Drop the exit stops PRoot only needs on kernels older than the one it
+ * runs on. The faccessat2 exit stop restores the arguments for glibc's
+ * fallback to faccessat on ENOSYS (kernels before 5.8, termux 5ba8b95);
+ * the statx one emulates the call when it fails, which only a kernel
+ * before 4.11 does for want of it - a statx Android's policy traps is
+ * emulated from SIGSYS instead. Both stops re-read and re-translate
+ * the path, and are paid on every call.
+ */
+static void drop_unneeded_exit_stops(void)
+{
+ static bool done = false;
+ struct utsname utsname;
+ int major, minor;
+ size_t i;
+
+ if (done)
+ return;
+ done = true;
+
+ if (uname(&utsname) < 0 || sscanf(utsname.release, "%d.%d", &major, &minor) != 2)
+ return;
+
+ for (i = 0; proot_sysnums[i].value != PR_void; i++) {
+ switch (proot_sysnums[i].value) {
+ case PR_faccessat2:
+ if (major > 5 || (major == 5 && minor >= 8))
+ proot_sysnums[i].flags &= ~FILTER_SYSEXIT;
+ break;
+ case PR_statx:
+ if (major > 4 || (major == 4 && minor >= 11))
+ proot_sysnums[i].flags &= ~FILTER_SYSEXIT;
+ break;
+ default:
+ break;
+ }
+ }
+}
+
static int collect_filtered_sysnums(TALLOC_CTX *context, const Tracee *tracee,
FilteredSysnum **filtered_sysnums)
{
Extension *extension;
int status;
+ drop_unneeded_exit_stops();
+
/* Add the sysnums required by PRoot to the list of filtered
* sysnums. TODO: only if path translation is required. */
status = merge_filtered_sysnums(context, filtered_sysnums, proot_sysnums);