From 49c5ff188b6bb10f552de171db9be5190f350fae Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Thu, 26 Nov 2026 15:18:51 -0500 Subject: [PATCH] fix(native): relink absolute symlinks again after any proot run (#2934) * fix(native): relink absolute symlinks again after any proot run * fix(native): keep links into shared storage and relink rootfs trees made since the last walk A link into /root/Storage (the Downloads and Documents links) points at a mount, which a relative link would miss; it keeps its guest path. The stamp version moves on so rootfs trees that already ran a proot session after the last walk are walked once more. * fix(native): leave the relink stamp unwritten while a proot run is alive --------- Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Co-authored-by: maxjivi05 --- .../launcher/runtime/LinuxRuntime.java | 1 + .../droiddeck/launcher/runtime/NativeGuest.kt | 28 +++++-- .../runtime/LinuxRuntimeRootPrefixTest.kt | 78 +++++++++++++++++++ 3 files changed, 101 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java index f0e959d0c7..279a78530f 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java +++ b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java @@ -199,6 +199,7 @@ public final class LinuxRuntime { } public static List prootPrefix(Context context, File root, String cwd, boolean fakeRoot) { + NativeGuest.relinkAfterProot(root); List cmd = new ArrayList<>(); cmd.addAll(com.droiddeck.launcher.core.HostProcess.dataExecutable(prootBinary(context).getPath())); cmd.add("--kill-on-exit"); diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt b/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt index 61f91598d5..49541416eb 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt @@ -169,8 +169,15 @@ object NativeGuest { } } + private const val RELINK_STAMP = "etc/droiddeck/native-symlinks" + + @JvmStatic + fun relinkAfterProot(root: File) { + File(root, RELINK_STAMP).delete() + } + private val HOST_LINK_TARGETS = listOf("/proc", "/dev", "/sys", "/sdcard", "/storage", "/data", "/system", "/vendor", "/apex", - "/system_ext", "/product", "/odm", "/linkerconfig", "/mnt") + "/system_ext", "/product", "/odm", "/linkerconfig", "/mnt", "/root/Storage") /** * Symbolic links the kernel follows by itself (ld.so, Steam, anything that skips the hook) read @@ -178,9 +185,9 @@ object NativeGuest { * which reads the target inside the rootfs, so each is rewritten as a relative link, which * resolves the same way under proot and without it. Rerun when the runtime changes. */ - fun relinkAbsoluteSymlinks(context: Context, root: File): Int { - val stamp = File(root, "etc/droiddeck/native-symlinks") - val version = com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.installedVersion(context).orEmpty() + "/2" + fun relinkAbsoluteSymlinks(context: Context, root: File, commands: () -> Sequence> = ::procCommands): Int { + val stamp = File(root, RELINK_STAMP) + val version = com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.installedVersion(context).orEmpty() + "/3" if (stamp.isFile && stamp.readText() == version) return 0 val rootPath = root.toPath() var rewritten = 0 @@ -206,11 +213,20 @@ object NativeGuest { override fun visitFileFailed(file: java.nio.file.Path, exc: java.io.IOException) = java.nio.file.FileVisitResult.CONTINUE }) - stamp.parentFile?.mkdirs() - stamp.writeText(version) + if (!prootRunning(LinuxRuntime.prootBinary(context).path, commands())) { + stamp.parentFile?.mkdirs() + stamp.writeText(version) + } return rewritten } + internal fun prootRunning(proot: String, commands: Sequence>): Boolean = + commands.any { args -> args.take(2).any { it == proot } } + + private fun procCommands(): Sequence> = (File("/proc").listFiles() ?: emptyArray()).asSequence() + .filter { it.name.all(Char::isDigit) } + .mapNotNull { runCatching { File(it, "cmdline").readText().split('\u0000') }.getOrNull() } + /** * The guest path that host path [target] stands for in a session with [binds] ("src:dst"), or null * when it is a guest path already: the rootfs, or the longest mount source holding it (the first diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/LinuxRuntimeRootPrefixTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/LinuxRuntimeRootPrefixTest.kt index e956781db9..9560641597 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/LinuxRuntimeRootPrefixTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/LinuxRuntimeRootPrefixTest.kt @@ -14,6 +14,8 @@ import org.robolectric.annotation.Config import org.robolectric.annotation.Implementation import org.robolectric.annotation.Implements import java.io.File +import java.nio.file.Files +import java.nio.file.Paths @RunWith(RobolectricTestRunner::class) @Config(sdk = [33], manifest = Config.NONE, application = Application::class, shadows = [LinuxRuntimeRootPrefixTest.Uname::class]) @@ -44,6 +46,82 @@ class LinuxRuntimeRootPrefixTest { assertEquals(LinuxRuntime.prootPrefix(context, root, "/root"), cmd.dropLast(guest.size)) } + @Test fun aLinkProotMadeAfterTheNativeRelinkIsMadeRelativeAtTheNextNativeStart() { + val home = File(root, "root/.steam").apply { mkdirs() } + File(root, "root/.local/share/Steam/logs").mkdirs() + val steam = File(home, "steam").toPath() + val card = File(home, "card").toPath() + Files.createSymbolicLink(card, Paths.get("/storage/emulated/0")) + NativeGuest.relinkAbsoluteSymlinks(context, root) + Files.createSymbolicLink(steam, Paths.get("/root/.local/share/Steam")) + assertEquals(0, NativeGuest.relinkAbsoluteSymlinks(context, root)) + assertTrue(Files.readSymbolicLink(steam).isAbsolute) + + LinuxRuntime.prootPrefix(context, root, "/root") + + assertEquals(1, NativeGuest.relinkAbsoluteSymlinks(context, root)) + assertEquals(Paths.get("../.local/share/Steam"), Files.readSymbolicLink(steam)) + assertTrue(File(home, "steam/logs").isDirectory) + assertEquals(Paths.get("/storage/emulated/0"), Files.readSymbolicLink(card)) + } + + @Test fun linksIntoSharedStorageKeepTheirGuestPath() { + val home = File(root, "root").apply { mkdirs() } + File(home, "Storage/Download").mkdirs() + File(root, "root/.local/share/Steam").mkdirs() + val downloads = File(home, "Downloads").toPath() + val storage = File(home, "Storage").toPath() + val steam = File(home, "steam").toPath() + Files.createSymbolicLink(downloads, Paths.get("/root/Storage/Download")) + Files.createSymbolicLink(File(home, "share").toPath(), Paths.get("/root/Storage")) + Files.createSymbolicLink(steam, Paths.get("/root/.local/share/Steam")) + Files.createSymbolicLink(File(home, "StorageX").toPath(), Paths.get("/root/StorageX")) + + assertEquals(2, NativeGuest.relinkAbsoluteSymlinks(context, root)) + + assertEquals(Paths.get("/root/Storage/Download"), Files.readSymbolicLink(downloads)) + assertEquals(Paths.get("/root/Storage"), Files.readSymbolicLink(File(home, "share").toPath())) + assertEquals(Paths.get(".local/share/Steam"), Files.readSymbolicLink(steam)) + assertEquals(Paths.get("StorageX"), Files.readSymbolicLink(File(home, "StorageX").toPath())) + assertTrue(Files.isDirectory(storage)) + } + + @Test fun aWalkDuringALiveProotRunLeavesTheNextNativeStartToWalkAgain() { + val home = File(root, "root").apply { mkdirs() } + File(root, "root/.local/share/Steam").mkdirs() + val proot = LinuxRuntime.prootBinary(context).path + LinuxRuntime.prootPrefix(context, root, "/root") + NativeGuest.relinkAbsoluteSymlinks(context, root) { sequenceOf(listOf("/system/bin/linker64", proot, "--kill-on-exit")) } + val steam = File(home, "steam").toPath() + Files.createSymbolicLink(steam, Paths.get("/root/.local/share/Steam")) + + assertEquals(1, NativeGuest.relinkAbsoluteSymlinks(context, root) { emptySequence() }) + assertEquals(Paths.get(".local/share/Steam"), Files.readSymbolicLink(steam)) + assertEquals(0, NativeGuest.relinkAbsoluteSymlinks(context, root) { emptySequence() }) + } + + @Test fun onlyTheProotTracerCountsAsALiveProotRun() { + val proot = "/data/app/x/lib/arm64/libproot.so" + assertTrue(NativeGuest.prootRunning(proot, sequenceOf(listOf("/system/bin/linker64", proot, "-r", "/rootfs")))) + assertTrue(NativeGuest.prootRunning(proot, sequenceOf(listOf(proot, "-r", "/rootfs")))) + assertFalse(NativeGuest.prootRunning(proot, sequenceOf(listOf("/usr/bin/bash", "-c", proot)))) + assertFalse(NativeGuest.prootRunning(proot, sequenceOf(listOf("$proot.old"), emptyList()))) + } + + @Test fun packageLinksAFakeRootProotRunMadeAreRelinkedToo() { + val bin = File(root, "usr/bin").apply { mkdirs() } + File(root, "usr/share/tool").mkdirs() + NativeGuest.relinkAbsoluteSymlinks(context, root) + val tool = File(bin, "tool").toPath() + Files.createSymbolicLink(tool, Paths.get("/usr/share/tool")) + + LinuxRuntime.commandWithBinds(context, root, emptyList(), guest, true) + + assertEquals(1, NativeGuest.relinkAbsoluteSymlinks(context, root)) + assertEquals(Paths.get("../share/tool"), Files.readSymbolicLink(tool)) + assertEquals(0, NativeGuest.relinkAbsoluteSymlinks(context, root)) + } + @Implements(Os::class) class Uname { companion object {