Files
DroidDeck/tools/proot/patches/0002-canon-resolve-parent-at-once.patch
maxjivi05 b8fcef622e proot: build the runtime's proot from source with WinNative's performance fixes
The runtime carries Termux's proot 5.1.107-70 as a prebuilt binary, built from
termux/proot 4dba3afb (every file:line site in it matches that commit). The apk
now builds that same commit with the NDK, with three patches ported from
WinNative's feature/proot-enhancements, and prefers it over the runtime's copy:

- Tracees are hashed by pid, so each ptrace stop finds its tracee without
  walking a list of every thread, and terminated tracees are swept only after
  a termination.
- A clean absolute path three or more directories deep under the rootfs alone
  has its parent opened once and taken as canonical when the kernel's path for
  it is the one proot would build, instead of an lstat per component; the final
  component of a call that does not follow it is no longer lstat'ed. Extensions
  still see the parent's host path, and the f2fs workaround keeps the old walk.
- A clone3 thread keeps its inherited flags when clone_args cannot be read.

talloc is linked in statically from Samba's release tarball, so libproot.so
needs only libc and libdl. On an x86_64 Android 15 emulator the same build ran
a path and thread test suite with output identical to the unpatched commit
(fake id, fake root, link2symlink, with and without seccomp), and a stat-heavy
benchmark ran 25-27% faster, 31-36% with 1500 live threads.
2026-09-23 22:03:57 -04:00

199 lines
6.6 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: maxjivi05 <maxjividen@gmail.com>
Date: Wed, 23 Sep 2026 21:44:45 -0400
Subject: [PATCH] canon: resolve a deep parent directory at once, and skip the
lstat of a final component that is not followed
---
src/path/canon.c | 94 +++++++++++++++++++++++++++++++++++++++++++--
src/path/f2fs-bug.c | 7 +++-
src/path/f2fs-bug.h | 1 +
3 files changed, 97 insertions(+), 5 deletions(-)
diff --git a/src/path/canon.c b/src/path/canon.c
index 6c5d4c0..cabd0c0 100644
--- a/src/path/canon.c
+++ b/src/path/canon.c
@@ -24,6 +24,7 @@
#include <limits.h> /* PATH_MAX, */
#include <sys/param.h> /* MAXSYMLINKS, */
#include <errno.h> /* E*, */
+#include <fcntl.h>
#include <sys/stat.h> /* lstat(2), S_ISREG(), */
#include <unistd.h> /* access(2), lstat(2), */
#include <string.h> /* string(3), */
@@ -133,7 +134,8 @@ static inline Finality next_component(char component[NAME_MAX], const char **cur
* (returned value is 1), otherwise it returns -errno (-ENOENT or
* -ENOTDIR).
*/
-static inline int substitute_binding_stat(Tracee *tracee, Finality finality, unsigned int recursion_level,
+static inline int substitute_binding_stat(Tracee *tracee, Finality finality, bool deref_final,
+ unsigned int recursion_level,
const char guest_path[PATH_MAX], char host_path[PATH_MAX])
{
struct stat statl;
@@ -152,6 +154,9 @@ static inline int substitute_binding_stat(Tracee *tracee, Finality finality, uns
return status;
}
+ if (finality == FINAL_NORMAL && !deref_final && tracee->glue_type == 0)
+ return 0;
+
statl.st_mode = 0;
if (should_skip_file_access_due_to_f2fs_bug(tracee, host_path)) {
status = -ENOENT;
@@ -186,6 +191,87 @@ static inline int substitute_binding_stat(Tracee *tracee, Finality finality, uns
return (S_ISLNK(statl.st_mode) ? 1 : 0);
}
+#define RESOLVE_AT_ONCE_MIN_DEPTH 3
+
+static size_t resolve_parent_at_once(Tracee *tracee, const char *user_path,
+ char guest_path[PATH_MAX])
+{
+ char parent[PATH_MAX];
+ char expected[PATH_MAX];
+ char resolved[PATH_MAX];
+ char link[32];
+ const Binding *binding;
+ const char *last;
+ const char *cursor;
+ size_t parent_length;
+ size_t prefix_length;
+ size_t expected_length;
+ ssize_t length;
+ int depth;
+ int fd;
+
+ if (tracee->glue_type != 0 || f2fs_bug_workaround_active(tracee))
+ return 0;
+
+ last = strrchr(user_path, '/');
+ if (last == NULL || last == user_path || last[1] == '\0'
+ || strcmp(last + 1, ".") == 0 || strcmp(last + 1, "..") == 0)
+ return 0;
+
+ parent_length = last - user_path;
+ if (parent_length >= PATH_MAX)
+ return 0;
+
+ depth = 0;
+ for (cursor = user_path; cursor < last; ) {
+ const char *end;
+
+ end = memchr(cursor + 1, '/', last - cursor);
+ if (end == NULL)
+ end = last;
+ if (end == cursor + 1
+ || (end == cursor + 2 && cursor[1] == '.')
+ || (end == cursor + 3 && cursor[1] == '.' && cursor[2] == '.'))
+ return 0;
+ depth++;
+ cursor = end;
+ }
+ if (depth < RESOLVE_AT_ONCE_MIN_DEPTH)
+ return 0;
+
+ memcpy(parent, user_path, parent_length);
+ parent[parent_length] = '\0';
+
+ binding = get_binding(tracee, GUEST, parent);
+ if (binding == NULL || binding->guest.length != 1)
+ return 0;
+
+ prefix_length = (binding->host.length == 1 ? 0 : binding->host.length);
+ expected_length = prefix_length + parent_length;
+ if (expected_length >= PATH_MAX)
+ return 0;
+ memcpy(expected, binding->host.path, prefix_length);
+ strcpy(expected + prefix_length, parent);
+
+ fd = open(expected, O_PATH | O_DIRECTORY | O_CLOEXEC);
+ if (fd < 0)
+ return 0;
+
+ snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
+ length = readlink(link, resolved, sizeof(resolved));
+ close(fd);
+
+ if (length < 0 || (size_t) length != expected_length
+ || memcmp(resolved, expected, expected_length) != 0)
+ return 0;
+
+ if (notify_extensions(tracee, HOST_PATH, (intptr_t) expected, false) < 0)
+ return 0;
+
+ strcpy(guest_path, parent);
+ return last + 1 - user_path;
+}
+
/**
* Copy in @guest_path the canonicalization (see `man 3 realpath`) of
* @user_path regarding to @tracee->root. The path to canonicalize
@@ -227,6 +313,8 @@ int canonicalize(Tracee *tracee, const char *user_path, bool deref_final,
/* Canonicalize recursely 'user_path' into 'guest_path'. */
cursor = user_path;
+ if (user_path[0] == '/')
+ cursor += resolve_parent_at_once(tracee, user_path, guest_path);
finality = NOT_FINAL;
while (!IS_FINAL(finality)) {
Comparison comparison;
@@ -260,7 +348,7 @@ int canonicalize(Tracee *tracee, const char *user_path, bool deref_final,
* symlink. For this latter case, we check that the
* symlink points to a directory once it is
* canonicalized, at the end of this loop. */
- status = substitute_binding_stat(tracee, finality, recursion_level, scratch_path, host_path);
+ status = substitute_binding_stat(tracee, finality, deref_final, recursion_level, scratch_path, host_path);
if (status < 0)
return status;
@@ -338,7 +426,7 @@ int canonicalize(Tracee *tracee, const char *user_path, bool deref_final,
/* Check that a non-final canonicalized/dereferenced
* symlink exists and is a directory. */
- status = substitute_binding_stat(tracee, finality, recursion_level, guest_path, host_path);
+ status = substitute_binding_stat(tracee, finality, deref_final, recursion_level, guest_path, host_path);
if (status < 0)
return status;
diff --git a/src/path/f2fs-bug.c b/src/path/f2fs-bug.c
index 4a5ee45..e4bddb9 100644
--- a/src/path/f2fs-bug.c
+++ b/src/path/f2fs-bug.c
@@ -113,7 +113,7 @@ end:
return result;
}
-bool should_skip_file_access_due_to_f2fs_bug(const Tracee *tracee, const char *path) {
+bool f2fs_bug_workaround_active(const Tracee *tracee) {
/* On first call check if workaround should be enabled */
static bool f2fs_bug_probed;
static bool f2fs_bug_detected;
@@ -130,9 +130,12 @@ bool should_skip_file_access_due_to_f2fs_bug(const Tracee *tracee, const char *p
}
f2fs_bug_probed = true;
}
+ return f2fs_bug_detected;
+}
+bool should_skip_file_access_due_to_f2fs_bug(const Tracee *tracee, const char *path) {
/* If workaround is not active don't skip access to file */
- if (!f2fs_bug_detected) {
+ if (!f2fs_bug_workaround_active(tracee)) {
return false;
}
diff --git a/src/path/f2fs-bug.h b/src/path/f2fs-bug.h
index b8eabdf..360f149 100644
--- a/src/path/f2fs-bug.h
+++ b/src/path/f2fs-bug.h
@@ -4,6 +4,7 @@
#include <stdbool.h> /* bool, true, false, */
#include "tracee/tracee.h" /* Tracee, */
+bool f2fs_bug_workaround_active(const Tracee *tracee);
bool should_skip_file_access_due_to_f2fs_bug(const Tracee *tracee, const char *path);
#endif /* F2FS_BUG_H */