fix(runtime): thermal zone binds no longer leak into the stand-in for PCI devices (#3595)

This commit is contained in:
FryMcNugget authored and GitHub committed 2026-12-02 22:22:52 -05:00
1 parent e3faee459f
commit 8cf1e60cc2
3 files changed
+64 -2

No files matched your search

@@ -35,9 +35,40 @@ object LegacyNames {
@Synchronized
fun migrateEtc(root: File) {
for ((old, new) in LINKED_DIRS) moveAndLink(File(root, old), File(root, new))
File(root, "etc/droiddeck/empty").mkdirs()
clearEmptyStandIn(root)
File(root, NO_THERMAL).mkdirs()
}
/**
* It stands in for /sys/bus/pci/devices, and libpci exits the whole process on an entry it cannot
* parse; older builds let thermal bind mount points leak into it. The guest can write here, so a
* link is never followed: one in place of the folder is removed, and one above it stops the cleanup.
*/
private fun clearEmptyStandIn(root: File) {
val linkedAbove = listOf("etc", "etc/droiddeck").firstOrNull { Files.isSymbolicLink(File(root, it).toPath()) }
if (linkedAbove != null) {
Log.w(TAG, "not clearing the empty stand-in: $linkedAbove is a link")
return
}
val empty = File(root, "etc/droiddeck/empty")
if (Files.isSymbolicLink(empty.toPath())) runCatching { Files.delete(empty.toPath()) }
.onFailure { Log.w(TAG, "could not remove the link at $empty", it); return }
empty.mkdirs()
empty.listFiles()?.forEach { entry ->
runCatching { deleteWithoutFollowing(entry.toPath()) }.onFailure { Log.w(TAG, "could not clear $entry", it) }
}
}
private fun deleteWithoutFollowing(path: java.nio.file.Path) {
if (Files.isDirectory(path, java.nio.file.LinkOption.NOFOLLOW_LINKS)) {
Files.newDirectoryStream(path).use { entries -> entries.forEach(::deleteWithoutFollowing) }
}
Files.deleteIfExists(path)
}
/** Stands in for a /sys/class/thermal the kernel won't list; proot adds the bound zones' mount points to it. */
const val NO_THERMAL = "etc/droiddeck/no-thermal"
@Synchronized
fun migrateRootfs(root: File) {
migrateEtc(root)
@@ -248,7 +248,7 @@ public final class LinuxRuntime {
// A kernel that refuses to list /sys/class/thermal (Pixel 10, Android 17) made mangoapp abort
// at start ("filesystem_error ... Permission denied: '/sys/class/thermal/'") until its
// wrapper gave up: it lists the directory for CPU temperatures. Empty there instead.
if (new File("/sys/class/thermal").list() == null) bind(cmd, new File(root, "etc/droiddeck/empty").getPath() + ":/sys/class/thermal");
if (new File("/sys/class/thermal").list() == null) bind(cmd, new File(root, LegacyNames.NO_THERMAL).getPath() + ":/sys/class/thermal");
bind(cmd, context.getFilesDir().getPath());
bind(cmd, context.getCacheDir().getPath());
bind(cmd, liveDir(context).getPath());
@@ -35,6 +35,37 @@ class LegacyNamesTest {
assertEquals("droiddeck", link("etc/bannerlator"))
}
@Test fun theEmptyStandInIsEmptiedAndThermalZonesGetTheirOwn() {
write("etc/droiddeck/empty/thermal_zone26/temp", "")
LegacyNames.migrateEtc(root)
assertEquals(emptyList<String>(), File(root, "etc/droiddeck/empty").list()!!.toList())
assertTrue(File(root, LegacyNames.NO_THERMAL).isDirectory)
}
@Test fun clearingTheEmptyStandInRemovesLinksWithoutTouchingWhatTheyPointAt() {
write("home/keep/file", "kept")
val links = File(root, "etc/droiddeck/empty").apply { mkdirs() }
val made = runCatching {
Files.createSymbolicLink(File(links, "dir").toPath(), File(root, "home/keep").toPath())
Files.createSymbolicLink(File(links, "dangling").toPath(), File(root, "missing").toPath())
}.isSuccess
org.junit.Assume.assumeTrue("this file system cannot make symbolic links", made)
LegacyNames.migrateEtc(root)
assertEquals(emptyList<String>(), links.list()!!.toList())
assertEquals("kept", File(root, "home/keep/file").readText())
}
@Test fun aLinkInPlaceOfTheEmptyStandInIsReplacedWithoutTouchingItsTarget() {
write("home/keep/file", "kept")
val empty = File(root, "etc/droiddeck/empty").also { it.parentFile!!.mkdirs() }
val made = runCatching { Files.createSymbolicLink(empty.toPath(), File(root, "home/keep").toPath()) }.isSuccess
org.junit.Assume.assumeTrue("this file system cannot make symbolic links", made)
LegacyNames.migrateEtc(root)
assertFalse(Files.isSymbolicLink(empty.toPath()))
assertEquals(emptyList<String>(), empty.list()!!.toList())
assertEquals("kept", File(root, "home/keep/file").readText())
}
@Test fun aFreshRuntimeGetsNoOldNames() {
write("usr/local/bin/droiddeck-session", "new")
LegacyNames.migrateRootfs(root)