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 <maxjividen@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-11-26 15:18:51 -05:00
1 parent 46f99b9e08
commit 49c5ff188b
3 files changed
+101 -6

No files matched your search

@@ -199,6 +199,7 @@ public final class LinuxRuntime {
}
public static List<String> prootPrefix(Context context, File root, String cwd, boolean fakeRoot) {
NativeGuest.relinkAfterProot(root);
List<String> cmd = new ArrayList<>();
cmd.addAll(com.droiddeck.launcher.core.HostProcess.dataExecutable(prootBinary(context).getPath()));
cmd.add("--kill-on-exit");
@@ -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<List<String>> = ::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<List<String>>): Boolean =
commands.any { args -> args.take(2).any { it == proot } }
private fun procCommands(): Sequence<List<String>> = (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
@@ -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 {