diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 510946aa18..555d8b673c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -195,6 +195,7 @@ jobs: !app/src/main/assets/linuxfs/i386/libfaultreport.so !app/src/main/assets/linuxfs/libblaudit.so !app/src/main/assets/linuxfs/libblfastpath.so + !app/src/main/assets/linuxfs/libddnatv-hook.so !app/src/main/assets/linuxfs/libblsession.so !app/src/main/assets/linuxfs/libfakeinput.so !app/src/main/assets/linuxfs/libssbs.so diff --git a/app/src/main/cpp/fakeinput_steam.cpp b/app/src/main/cpp/fakeinput_steam.cpp index 498461992f..8941abda20 100644 --- a/app/src/main/cpp/fakeinput_steam.cpp +++ b/app/src/main/cpp/fakeinput_steam.cpp @@ -2322,7 +2322,15 @@ __attribute__((visibility("hidden"))) static bool is_deck_sysfs_path(const char if (!path) return false; if (!strncmp(path, "/sys/devices/droiddeck", 22) || !strncmp(path, "/sys/class/hidraw", 17)) return true; const char *listing = deck_listing_dir(); - return listing && !strncmp(path, listing, strlen(listing)); + if (!listing) return false; + if (!strncmp(path, listing, strlen(listing))) return true; + // Without proot, a descriptor's /proc/self/fd link names the stand-in where it really is (the + // listing's parent holds devices/ and class-hidraw/), not /sys + const char *slash = strrchr(listing, '/'); + if (!slash || slash == listing) return false; + size_t root = static_cast(slash - listing); + return !strncmp(path, listing, root) && path[root] == '/' && + (!strncmp(path + root, "/devices", 8) || !strncmp(path + root, "/class-hidraw", 13)); } __attribute__((visibility("hidden"))) static bool is_deck_sysfs_fd(int fd) { diff --git a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt index 6bdfbbca38..f429efcbf7 100644 --- a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt @@ -185,6 +185,7 @@ class FrontEndActivity : ComponentActivity() { private var gpuClockPin by mutableStateOf(false) private var prootNoSeccomp by mutableStateOf(false) private var prootFastPath by mutableStateOf(true) + private var nativeUserland by mutableStateOf(false) private var cursorComposite by mutableStateOf(false) private var guestHostname by mutableStateOf(SessionPrefs.DEFAULT_GUEST_HOSTNAME) private var phantomWarning by mutableStateOf(null) @@ -2557,7 +2558,7 @@ class FrontEndActivity : ComponentActivity() { deviceRamMb = (android.app.ActivityManager.MemoryInfo() .also((getSystemService(ACTIVITY_SERVICE) as android.app.ActivityManager)::getMemoryInfo).totalMem / (1024 * 1024)).toInt(), ramCapAvailable = com.droiddeck.launcher.core.ZramSupport.supported(), - prootNoSeccomp = prootNoSeccomp, prootFastPath = prootFastPath, cursorComposite = cursorComposite, + prootNoSeccomp = prootNoSeccomp, prootFastPath = prootFastPath, nativeUserland = nativeUserland, cursorComposite = cursorComposite, guestHostname = guestHostname, phantomWarning = phantomWarning, onClientOverride = { on -> SessionPrefs.setClientCpusOverride(this, on); clientOverride = on }, onTuSysmem = { on -> SessionPrefs.setTuSysmem(this, on); tuSysmem = on }, @@ -2575,6 +2576,7 @@ class FrontEndActivity : ComponentActivity() { onVramCapMb = { mb -> SessionPrefs.setVramCapMb(this, mb); vramCapMb = mb }, onProotNoSeccomp = { on -> SessionPrefs.setProotNoSeccomp(this, on); prootNoSeccomp = on }, onProotFastPath = { on -> SessionPrefs.setProotFastPath(this, on); prootFastPath = on }, + onNativeUserland = { on -> SessionPrefs.setNativeUserland(this, on); nativeUserland = on }, onCursorComposite = { on -> SessionPrefs.setCursorComposite(this, on); cursorComposite = on }, onGuestHostname = { name -> SessionPrefs.setGuestHostname(this, name) }, onClientCore = { core, on -> @@ -2797,6 +2799,7 @@ class FrontEndActivity : ComponentActivity() { gpuClockPin = SessionPrefs.gpuClockPin(this) prootNoSeccomp = SessionPrefs.prootNoSeccomp(this) prootFastPath = SessionPrefs.prootFastPath(this) + nativeUserland = SessionPrefs.nativeUserland(this) cursorComposite = SessionPrefs.cursorComposite(this) guestHostname = SessionPrefs.guestHostname(this) refreshPhantomStatus() diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentBridgeProvider.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentBridgeProvider.kt index c1955caa05..6b055221e5 100644 --- a/app/src/main/java/com/droiddeck/launcher/agent/AgentBridgeProvider.kt +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentBridgeProvider.kt @@ -85,6 +85,10 @@ class AgentBridgeProvider : ContentProvider() { AgentAccess.requireCommands(context) guest(context, JSONObject(request.toString()).put("kind", "exec"), timeoutMs(request, 30.0, 10_000)) } + "native" -> { + AgentAccess.requireCommands(context) + AgentNative.run(context, request, timeoutMs(request, 30.0, 0)) + } "cdp" -> { AgentAccess.requireCommands(context) guest(context, JSONObject(request.toString()).put("kind", "cdp"), timeoutMs(request, 15.0, 5_000)) diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentNative.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentNative.kt new file mode 100644 index 0000000000..613cbe2c39 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentNative.kt @@ -0,0 +1,65 @@ +package com.droiddeck.launcher.agent + +import android.content.Context +import com.droiddeck.launcher.runtime.LinuxRuntime +import com.droiddeck.launcher.runtime.NativeGuest +import org.json.JSONArray +import org.json.JSONObject +import java.util.concurrent.TimeUnit + +/** Runs a guest program without proot (`droiddeckctl native`), for testing the native userland. */ +object AgentNative { + private const val OUTPUT_CAP = 256 * 1024 + + fun run(context: Context, request: JSONObject, timeoutMs: Long): JSONObject { + val root = LinuxRuntime.rootDir(context) + com.droiddeck.launcher.session.SessionFiles.stage(context, root) + // with the setting off, staging leaves the rootfs as proot wants it + if (!com.droiddeck.launcher.session.SessionPrefs.nativeUserland(context)) NativeGuest.prepare(context, root) + if (!NativeGuest.available(root)) throw AgentException("NATIVE_UNAVAILABLE", "The rootfs has no native hook or glibc loader yet; start a session once") + val argvJson = request.optJSONArray("argv") ?: throw AgentException("INVALID_REQUEST", "argv is required") + val argv = (0 until argvJson.length()).map { argvJson.getString(it) } + if (argv.isEmpty()) throw AgentException("INVALID_REQUEST", "argv is empty") + val env = linkedMapOf() + request.optJSONObject("env")?.let { obj -> obj.keys().forEach { env[it] = obj.getString(it) } } + val preload = request.optJSONArray("preload")?.let { a -> (0 until a.length()).map { a.getString(it) } } ?: emptyList() + val builder = NativeGuest.builder(root, request.optString("cwd", "/root"), argv, env, preload) + val started = System.nanoTime() + val process = builder.start() + val out = StringBuilder() + val err = StringBuilder() + val readers = listOf( + Thread { drain(process.inputStream, out) }, + Thread { drain(process.errorStream, err) }, + ).onEach { it.isDaemon = true; it.start() } + // written beside the wait: a program that never reads more than a pipe holds would block it + val input = request.optString("stdin").toByteArray() + Thread { + try { + process.outputStream.use { stdin -> if (input.isNotEmpty()) stdin.write(input) } + } catch (_: java.io.IOException) { + } + }.apply { isDaemon = true; start() } + val finished = process.waitFor(timeoutMs, TimeUnit.MILLISECONDS) + if (!finished) process.destroyForcibly().waitFor() + readers.forEach { it.join(2000) } + val code = if (finished) process.exitValue() else -1 + return JSONObject().put("ok", true).put("command", "native") + .put("exitCode", code).put("timedOut", !finished) + .put("ms", (System.nanoTime() - started) / 1_000_000) + .put("stdout", JSONObject().put("text", out.toString())) + .put("stderr", JSONObject().put("text", err.toString())) + .put("argv", JSONArray(argv)) + } + + private fun drain(input: java.io.InputStream, into: StringBuilder) { + val buffer = ByteArray(8192) + val text = java.io.ByteArrayOutputStream() + while (true) { + val n = try { input.read(buffer) } catch (e: java.io.IOException) { -1 } + if (n < 0) break + if (text.size() < OUTPUT_CAP) text.write(buffer, 0, minOf(n, OUTPUT_CAP - text.size())) + } + into.append(text.toString(Charsets.UTF_8.name())) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt b/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt index cb0b9487b8..63037a24ac 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt @@ -278,6 +278,7 @@ object DeviceReport { k("RAM cap", SessionPrefs.ramCapMb(context).let { if (it > 0) "$it MB" else "off" }) k("VRAM cap", SessionPrefs.vramCapMb(context).let { if (it > 0) "$it MB" else "driver default" }) k("proot without seccomp", SessionPrefs.prootNoSeccomp(context)) + k("native userland (no proot)", SessionPrefs.nativeUserland(context)) k("proot fast path", SessionPrefs.prootFastPath(context)) k("proot binary", LinuxRuntime.prootSource(context) + " " + LinuxRuntime.prootBinary(context).path) k("proot fast path (eligible)", com.droiddeck.launcher.runtime.ProotFastPath.enabled(context)) diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt b/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt index 2d1a9aa401..4a98fdedec 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt @@ -182,6 +182,7 @@ object BwrapSpawner { val argv = request.getJSONArray("argv").let { a -> (0 until a.length()).map { a.getString(it) } } val env = LinkedHashMap() request.optJSONObject("env")?.let { e -> e.keys().forEach { k -> env[k] = e.getString(k) } } + withoutRootfsSettings(env, rootfs) val envTool = request.optString("envTool").takeIf { it.startsWith("/") } if (envTool == null && binds.any { it.guest == "/dev" }) gpu(rootfs, root, env)?.forEach { addBind(it) } val libc = listOf("/usr/lib/aarch64-linux-gnu/libc.so.6", "/lib/aarch64-linux-gnu/libc.so.6").map { hostPath(binds, root.path, it) } @@ -191,6 +192,24 @@ object BwrapSpawner { return Plan(root, binds, request.optString("cwd", "/").ifEmpty { "/" }, argv, env, envTool ?: "/usr/bin/env") } + private val ROOTFS_PIPEWIRE = listOf("PIPEWIRE_MODULE_DIR", "SPA_PLUGIN_DIR", "PIPEWIRE_CONFIG_DIR") + + /** + * The session's Flatpak passes settings for the session's own root on: the native hook's, + * DroidDeck's PipeWire build, which a runtime does not have (its apps found no PipeWire plugins), + * and the session's video driver for SDL. + */ + internal fun withoutRootfsSettings(env: MutableMap, rootfs: String) { + env.keys.removeAll { it.startsWith("DDNATV_") } + for (key in ROOTFS_PIPEWIRE) if (env[key]?.startsWith("/usr/local/lib/droiddeck-pipewire") == true) env.remove(key) + // the session's choice of X11 for SDL, in an app that Flatpak gave Wayland and no X display + if (env["SDL_VIDEODRIVER"] == "x11" && env["DISPLAY"].isNullOrEmpty() && !env["WAYLAND_DISPLAY"].isNullOrEmpty()) env.remove("SDL_VIDEODRIVER") + env["LD_PRELOAD"]?.let { preload -> + val kept = preload.split(':').filter { it.isNotEmpty() && it != rootfs && !it.startsWith("$rootfs/") } + if (kept.isEmpty()) env.remove("LD_PRELOAD") else env["LD_PRELOAD"] = kept.joinToString(":") + } + } + private const val GPU_DIR = "/run/droiddeck-gpu" private val LIB_DIRS = listOf("usr/lib/aarch64-linux-gnu", "usr/lib") @@ -347,22 +366,201 @@ object BwrapSpawner { try { Os.chmod(f.path, mode) } catch (e: Exception) { } } + private val SANDBOX_LOADERS = listOf("/usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1", "/usr/lib/ld-linux-aarch64.so.1", + "/lib/ld-linux-aarch64.so.1", "/lib64/ld-linux-aarch64.so.1") + private val SANDBOX_LIBCS = listOf("/usr/lib/aarch64-linux-gnu/libc.so.6", "/usr/lib/libc.so.6", "/lib/aarch64-linux-gnu/libc.so.6") + private val SANDBOX_LIB_DIRS = listOf("/app/lib/aarch64-linux-gnu", "/app/lib", "/usr/lib/aarch64-linux-gnu", "/usr/lib") + private val seccompPatched = java.util.concurrent.ConcurrentHashMap.newKeySet() + + /** + * The sandbox started by the runtime's own glibc loader with the native hook, which maps the + * sandbox's mounts; null when the runtime cannot load the hook (older glibc), so proot runs it. + */ + private fun nativeLaunch(context: Context, plan: Plan): Pair, Map>? { + val rootfs = LinuxRuntime.rootDir(context) + val sandbox = plan.root.path + fun host(guest: String) = File(hostPath(plan.binds, sandbox, guest)) + if (!hasEnvTool(plan)) { + val program = plan.argv.firstOrNull()?.takeIf { it.startsWith("/") }?.let(::host)?.canonicalFile + ?.takeIf { it.isFile && isStaticElf(it) } ?: return null + if (!linkMounts(plan)) return null + return (listOf(program.path) + plan.argv.drop(1)) to plan.env + } + val loader = SANDBOX_LOADERS.map(::host).firstOrNull { it.isFile }?.canonicalFile ?: return null + val libc = SANDBOX_LIBCS.map(::host).firstOrNull { it.isFile }?.canonicalFile ?: return null + val hook = NativeGuest.hook(rootfs) + val need = glibcMinor(hook, GLIBC_NEED) { it.maxOrNull() } ?: return null + val have = glibcMinor(libc, GLIBC_RELEASE) { it.firstOrNull() } ?: return null + if (have < need) { + Log.i(TAG, "native userland: this runtime's glibc 2.$have cannot load the hook (2.$need); proot runs the sandbox") + return null + } + for (file in listOf(loader, libc)) { + val key = "${file.path}:${file.length()}:${file.lastModified()}" + if (key in seccompPatched) continue + // an unpatched loader or libc dies under Android's filter before the hook loads: proot runs it + val patched = runCatching { ElfSeccompPatch.patchFile(file) } + .onFailure { Log.e(TAG, "native userland: could not patch $file for Android's seccomp filter; proot runs the sandbox", it) } + if (patched.isFailure) return null + seccompPatched.add("${file.path}:${file.length()}:${file.lastModified()}") + } + val program = host(plan.envTool).canonicalFile.takeIf { it.isFile } ?: return null + if (!linkMounts(plan)) return null + writeRunpathConf(plan, ::host) + val launch = listOf(loader.path, "--argv0", program.name, program.path, "-i") + + plan.env.map { (k, v) -> "$k=$v" } + plan.argv + val env = linkedMapOf( + "DDNATV_ROOT" to sandbox, + "DDNATV_LINK_ROOT" to rootfs.path, + "DDNATV_BINDS" to plan.binds.joinToString("|") { "${it.host}:${it.guest}" }, + "DDNATV_HOSTNAME" to SessionPrefs.guestHostname(context), + "LD_LIBRARY_PATH" to SANDBOX_LIB_DIRS.map(::host).filter { it.isDirectory }.joinToString(":") { it.path }, + "LD_PRELOAD" to hook.path, + ).filterValues { it.isNotEmpty() } + return (NativeGuest.simulator(rootfs)?.let { it + launch } ?: launch) to env + } + + private val runpathDirs = java.util.concurrent.ConcurrentHashMap>() + + /** + * ld.so opens a library's absolute RUNPATH (libpulse's pulseaudio/ directory) without the hook, + * on Android's own filesystem, so those directories are listed for the hook in the sandbox's + * ld.so.conf too, as [NativeGuest.syncLibraryDirs] does for the rootfs. A checkout never changes. + */ + private fun writeRunpathConf(plan: Plan, host: (String) -> File) { + val confDir = inRoot(plan.root, "/run/flatpak/ld.so.conf.d")?.takeIf { it.isDirectory } ?: return + val dirs = sortedSetOf() + for (top in listOf("/usr/lib", "/app/lib")) { + val real = host(top).takeIf { it.isDirectory }?.canonicalFile ?: continue + dirs += runpathDirs.getOrPut(real.path) { + real.walkTopDown().maxDepth(4).onEnter { !Files.isSymbolicLink(it.toPath()) } + .filter { it.isFile && ".so" in it.name && !Files.isSymbolicLink(it.toPath()) } + .flatMap { runCatching { ElfRunpath.read(it) }.getOrDefault(emptyList()) } + .filter { it.startsWith("/usr/") || it.startsWith("/app/") || it.startsWith("/lib") } + .distinct().toList() + } + } + if (dirs.isEmpty()) return + runCatching { File(confDir, "runtime-999-droiddeck-runpath.conf").writeText(dirs.joinToString("\n", postfix = "\n")) } + } + + /** A runtime extension's apply_extra runs in a sandbox with nothing but the extension itself. */ + private fun hasEnvTool(plan: Plan): Boolean = + File(hostPath(plan.binds, plan.root.path, plan.envTool)).let { it.exists() || Files.isSymbolicLink(it.toPath()) } + + internal fun isStaticElf(file: File): Boolean = try { + java.io.RandomAccessFile(file, "r").use { f -> + val head = ByteArray(64).also { f.readFully(it) } + if (head[0] != 0x7f.toByte() || head[1] != 'E'.code.toByte() || head[4] != 2.toByte() || head[5] != 1.toByte()) return false + val le = java.nio.ByteBuffer.wrap(head).order(java.nio.ByteOrder.LITTLE_ENDIAN) + val phoff = le.getLong(32) + val phentsize = le.getShort(54).toInt() and 0xffff + val phnum = le.getShort(56).toInt() and 0xffff + val entry = ByteArray(4) + (0 until phnum).none { i -> + f.seek(phoff + i.toLong() * phentsize) + f.readFully(entry) + java.nio.ByteBuffer.wrap(entry).order(java.nio.ByteOrder.LITTLE_ENDIAN).int == 3 // PT_INTERP + } + } + } catch (_: java.io.IOException) { + false + } + + /** + * The kernel follows paths the hook does not see (ld.so, symbolic links), so each top-level + * mount of the sandbox is also a link to its source in the sandbox's own root, and the + * sandbox's links with absolute guest targets are made relative, which reaches them through those. + * False when a mount's place could not be made; proot then runs the sandbox, whose mounts hide + * whatever was left at their places. + */ + private fun linkMounts(plan: Plan): Boolean { + val root = plan.root.toPath() + plan.root.walkTopDown().onEnter { !Files.isSymbolicLink(it.toPath()) }.map { it.toPath() } + .filter { Files.isSymbolicLink(it) }.toList().forEach { link -> + runCatching { + val target = Files.readSymbolicLink(link).toString() + if (!target.startsWith("/")) return@runCatching + Files.delete(link) + Files.createSymbolicLink(link, link.parent.relativize(root.resolve(target.removePrefix("/")))) + } + } + // a mount inside another mount (a GL extension at /usr/lib/aarch64-linux-gnu/GL) needs its own + // place: the links of the mounts it is in become directories of links to their entries + val entryLinks = HashSet() + for (bind in plan.binds.sortedBy { it.guest.length }) { + if (bind.guest == "/") continue + val at = try { + mountPlace(plan.root, bind.guest, entryLinks)?.also { at -> + val path = at.toPath() + if (path in entryLinks) { + Files.delete(path) + entryLinks.remove(path) + } + } + } catch (e: java.io.IOException) { + Log.e(TAG, "native userland: could not make a place for ${bind.guest}; proot runs the sandbox", e) + return false + } ?: continue + val path = at.toPath() + if (at.exists() || Files.isSymbolicLink(path)) continue + at.parentFile?.mkdirs() + runCatching { Files.createSymbolicLink(path, java.nio.file.Paths.get(bind.host)) } + } + return true + } + + private fun mountPlace(root: File, guest: String, entryLinks: MutableSet): File? { + val parts = guest.split('/').filter { it.isNotEmpty() } + var f = root + for ((i, part) in parts.withIndex()) { + if (part == "..") return null + f = File(f, part) + val path = f.toPath() + if (i == parts.lastIndex || !Files.isSymbolicLink(path)) continue + val target = Files.readSymbolicLink(path) + // a link of the sandbox's own tree, made relative above, is not a mount + if (!target.isAbsolute) return null + Files.delete(path) + entryLinks.remove(path) + Files.createDirectory(path) + target.toFile().listFiles()?.forEach { entry -> + val link = path.resolve(entry.name) + Files.createSymbolicLink(link, entry.toPath()) + entryLinks.add(link) + } + } + return f + } + private fun run(context: Context, plan: Plan, client: LocalSocket, out: DataOutputStream, id: Int) { - val cmd = LinuxRuntime.prootPrefix(context, plan.root, plan.cwd) val verbose = plan.env["DROIDDECK_PROOT_VERBOSE"]?.toIntOrNull() - if (verbose != null) { cmd.add(1, "-v"); cmd.add(2, verbose.toString()) } - plan.binds.forEach { cmd.add("-b"); cmd.add(it.spec) } - cmd.add(plan.envTool) - cmd.add("-i") - plan.env.forEach { (k, v) -> cmd.add("$k=$v") } - cmd.addAll(plan.argv) - val builder = ProcessBuilder(cmd).redirectErrorStream(true).directory(plan.root) - builder.environment().apply { - clear() - put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) - put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(context).path) - if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") - LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + val native = if (NativeGuest.forSession(context)) nativeLaunch(context, plan) else null + val builder = if (native != null) { + ProcessBuilder(native.first).redirectErrorStream(true) + .directory(File(hostPath(plan.binds, plan.root.path, plan.cwd)).takeIf { it.isDirectory } ?: plan.root) + .also { b -> b.environment().apply { clear(); putAll(native.second) } } + } else { + val cmd = LinuxRuntime.prootPrefix(context, plan.root, plan.cwd) + if (verbose != null) { cmd.add(1, "-v"); cmd.add(2, verbose.toString()) } + plan.binds.forEach { cmd.add("-b"); cmd.add(it.spec) } + val envTool = hasEnvTool(plan) + if (envTool) { + cmd.add(plan.envTool) + cmd.add("-i") + plan.env.forEach { (k, v) -> cmd.add("$k=$v") } + } + cmd.addAll(plan.argv) + ProcessBuilder(cmd).redirectErrorStream(true).directory(plan.root).also { b -> + b.environment().apply { + clear() + if (!envTool) putAll(plan.env) + put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) + put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(context).path) + if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") + LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + } + } } val process = builder.start() val pid = HostProcess.pidOf(process) diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/ElfRunpath.kt b/app/src/main/java/com/droiddeck/launcher/runtime/ElfRunpath.kt new file mode 100644 index 0000000000..dc772c49a1 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/runtime/ElfRunpath.kt @@ -0,0 +1,72 @@ +package com.droiddeck.launcher.runtime + +import java.io.File +import java.io.RandomAccessFile +import java.nio.ByteBuffer +import java.nio.ByteOrder + +/** The absolute DT_RUNPATH and DT_RPATH directories of a 64-bit little-endian ELF object. */ +object ElfRunpath { + private const val PT_LOAD = 1 + private const val PT_DYNAMIC = 2 + private const val DT_STRTAB = 5 + private const val DT_RPATH = 15 + private const val DT_RUNPATH = 29 + + fun read(file: File): List { + if (file.length() < 64) return emptyList() + RandomAccessFile(file, "r").use { raf -> + val channel = raf.channel + val header = ByteBuffer.allocate(64).order(ByteOrder.LITTLE_ENDIAN) + channel.read(header, 0) + if (header.getInt(0) != 0x464c457f || header.get(4).toInt() != 2 || header.get(5).toInt() != 1) return emptyList() + val phoff = header.getLong(32) + val phentsize = header.getShort(54).toInt() and 0xffff + val phnum = header.getShort(56).toInt() and 0xffff + if (phnum == 0 || phnum > 128 || phentsize < 56) return emptyList() + val table = ByteBuffer.allocate(phentsize * phnum).order(ByteOrder.LITTLE_ENDIAN) + channel.read(table, phoff) + class Load(val vaddr: Long, val offset: Long, val size: Long) + val loads = ArrayList() + var dynOffset = -1L + var dynSize = 0L + for (i in 0 until phnum) { + val at = i * phentsize + val type = table.getInt(at) + val offset = table.getLong(at + 8) + val vaddr = table.getLong(at + 16) + val filesz = table.getLong(at + 32) + if (type == PT_LOAD) loads.add(Load(vaddr, offset, filesz)) + if (type == PT_DYNAMIC) { dynOffset = offset; dynSize = filesz } + } + if (dynOffset < 0 || dynSize <= 0 || dynSize > 1 shl 20) return emptyList() + val dynamic = ByteBuffer.allocate(dynSize.toInt()).order(ByteOrder.LITTLE_ENDIAN) + channel.read(dynamic, dynOffset) + var strtab = -1L + val names = ArrayList() + var at = 0 + while (at + 16 <= dynamic.capacity()) { + val tag = dynamic.getLong(at) + val value = dynamic.getLong(at + 8) + if (tag == 0L) break + if (tag == DT_STRTAB.toLong()) strtab = value + if (tag == DT_RPATH.toLong() || tag == DT_RUNPATH.toLong()) names.add(value) + at += 16 + } + if (strtab < 0 || names.isEmpty()) return emptyList() + val strtabOffset = loads.firstOrNull { strtab >= it.vaddr && strtab < it.vaddr + it.size } + ?.let { strtab - it.vaddr + it.offset } ?: return emptyList() + val result = ArrayList() + for (name in names) { + val buffer = ByteBuffer.allocate(4096) + val n = channel.read(buffer, strtabOffset + name) + if (n <= 0) continue + val bytes = buffer.array() + var end = 0 + while (end < n && bytes[end].toInt() != 0) end++ + String(bytes, 0, end, Charsets.UTF_8).split(':').filter { it.startsWith("/") }.forEach { result.add(it.trimEnd('/')) } + } + return result + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/ElfSeccompPatch.kt b/app/src/main/java/com/droiddeck/launcher/runtime/ElfSeccompPatch.kt new file mode 100644 index 0000000000..817cbc9757 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/runtime/ElfSeccompPatch.kt @@ -0,0 +1,85 @@ +package com.droiddeck.launcher.runtime + +import java.io.File +import java.nio.ByteBuffer +import java.nio.ByteOrder + +/** + * Android's app seccomp filter kills a process with SIGSYS for rseq, set_robust_list and clone3. glibc's + * dynamic linker makes the first two before any preloaded library (so before the hook's SIGSYS + * handler) exists, so the call sites in the rootfs's ld.so and libc answer by themselves instead: the + * `svc` after `mov x8, #nr` becomes a no-op for set_robust_list (glibc ignores the result) and + * `mov x0, #-ENOSYS` for rseq and clone3 (glibc falls back from both). Under proot the same + * answers are what proot gives. + */ +object ElfSeccompPatch { + private const val SVC = 0xd4000001.toInt() + private const val NOP = 0xd503201f.toInt() + private const val MOV_X0_ENOSYS = 0x928004a0.toInt() + private const val MOV_X8_SET_ROBUST_LIST = 0xd2800c68.toInt() + private const val MOV_X8_CLONE3 = 0xd2803668.toInt() + private const val MOV_X8_RSEQ = 0xd28024a8.toInt() + private const val WINDOW = 16 + + /** Patches [data] in place and returns the number of call sites changed. */ + fun patch(data: ByteArray): Int { + if (data.size < 64 || data[0] != 0x7f.toByte() || data[1] != 'E'.code.toByte() || data[4].toInt() != 2 || data[5].toInt() != 1) return 0 + val buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN) + if (buffer.getShort(18).toInt() != 183) return 0 + val phoff = buffer.getLong(32).toInt() + val phentsize = buffer.getShort(54).toInt() and 0xffff + val phnum = buffer.getShort(56).toInt() and 0xffff + var patched = 0 + for (i in 0 until phnum) { + val at = phoff + i * phentsize + if (at + 56 > data.size) break + if (buffer.getInt(at) != 1 || (buffer.getInt(at + 4) and 1) == 0) continue + val start = ((buffer.getLong(at + 8).toInt()) + 3) and 3.inv() + val end = minOf(data.size, buffer.getLong(at + 8).toInt() + buffer.getLong(at + 32).toInt()) and 3.inv() + var pos = start + while (pos + 4 <= end) { + val replacement = when (buffer.getInt(pos)) { + MOV_X8_SET_ROBUST_LIST -> NOP + MOV_X8_CLONE3, MOV_X8_RSEQ -> MOV_X0_ENOSYS + else -> null + } + if (replacement != null) { + var next = pos + 4 + val limit = minOf(end, pos + 4 + WINDOW * 4) + while (next < limit) { + val insn = buffer.getInt(next) + if (insn == SVC) { + buffer.putInt(next, replacement) + patched++ + break + } + // A site patched before, or x8 set again: an svc after it is another call's + // (a second run found clone3's thread exit there and turned it into -ENOSYS) + if (insn == MOV_X0_ENOSYS || (insn and 0x1f) == 8) break + next += 4 + } + } + pos += 4 + } + } + return patched + } + + /** Patches the file behind [path] (symlinks followed) if it still has unpatched call sites. */ + fun patchFile(path: File): Int { + val target = path.canonicalFile + if (!target.isFile) return 0 + val data = target.readBytes() + val count = patch(data) + if (count == 0) return 0 + val temp = File(target.parentFile, "." + target.name + ".seccomp") + temp.writeBytes(data) + temp.setExecutable(true, false) + temp.setReadable(true, false) + if (!temp.renameTo(target)) { + temp.delete() + throw java.io.IOException("could not replace ${target.path}") + } + return count + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/GuestCommand.kt b/app/src/main/java/com/droiddeck/launcher/runtime/GuestCommand.kt index 8e5769c312..0560752a31 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/GuestCommand.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/GuestCommand.kt @@ -35,12 +35,10 @@ object GuestCommand { SessionFiles.stage(context, root) LinuxNetworkLinkComponent(context, root).publish() val runtimeDir = File(context.filesDir, ".flatpak-rt").apply { mkdirs() } - val cmd = LinuxRuntime.prootPrefix(context, root, "/root", fakeRoot) - if (linkDir != null) cmd.add(1, "--link2symlink") val binds = LinuxRuntime.binds(context, null, runtimeDir, Environment.getExternalStorageDirectory(), extraBinds) - binds.forEach { cmd.add("-b"); cmd.add(it) } - val fastPathKey = if (fastPath && !fakeRoot && linkDir == null && ProotFastPath.enabled(context)) ProotFastPath.key(root, binds) else null - cmd += listOf( + val native = NativeGuest.forSession(context) + val fastPathKey = if (!native && fastPath && !fakeRoot && linkDir == null && ProotFastPath.enabled(context)) ProotFastPath.key(root, binds) else null + val guest = mutableListOf( "/usr/bin/env", "-i", "HOME=/root", "USER=root", "LANG=C.UTF-8", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games", "XDG_RUNTIME_DIR=${LinuxRuntime.GUEST_RUNTIME_DIR}", "XDG_DATA_HOME=/root/.local/share", "FLATPAK_BWRAP=${FlatpakManager.BWRAP}", @@ -48,22 +46,31 @@ object GuestCommand { "XDG_DATA_DIRS=/root/.local/share/flatpak/exports/share:/usr/local/share:/usr/share", ) DownloadSource.proxyHostPort(context).takeIf { it.isNotEmpty() }?.let { hostPort -> - cmd += listOf( + guest += listOf( "http_proxy=http://$hostPort", "https_proxy=http://$hostPort", "HTTP_PROXY=http://$hostPort", "HTTPS_PROXY=http://$hostPort", "no_proxy=localhost,127.0.0.1,::1", "NO_PROXY=localhost,127.0.0.1,::1", ) } - fastPathKey?.let { cmd += ProotFastPath.guestEnv(root, binds, it) } - cmd += argv - val builder = ProcessBuilder(cmd).directory(root).redirectErrorStream(true) - builder.environment().apply { - put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) - put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(context).path) - if (linkDir != null) put("PROOT_L2S_DIR", linkDir.path) - if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") - fastPathKey?.let { ProotFastPath.hostEnv(it).let { (k, v) -> put(k, v) } } - LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + fastPathKey?.let { guest += ProotFastPath.guestEnv(root, binds, it) } + guest += argv + val builder = if (native) { + NativeGuest.processBuilder(context, root, binds, guest).redirectErrorStream(true) + } else { + val cmd = LinuxRuntime.prootPrefix(context, root, "/root", fakeRoot) + if (linkDir != null) cmd.add(1, "--link2symlink") + binds.forEach { cmd.add("-b"); cmd.add(it) } + cmd += guest + ProcessBuilder(cmd).directory(root).redirectErrorStream(true).also { b -> + b.environment().apply { + put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) + put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(context).path) + if (linkDir != null) put("PROOT_L2S_DIR", linkDir.path) + if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") + fastPathKey?.let { ProotFastPath.hostEnv(it).let { (k, v) -> put(k, v) } } + LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + } + } } if (cancelled?.get() == true) return 130 val process = builder.start() 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 3e5b0a29cb..0253604021 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java +++ b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java @@ -155,6 +155,11 @@ public final class LinuxRuntime { private static volatile List lastBinds; + /** The mounts of a session started without proot, which sandboxes and root helpers see too. */ + public static void rememberSessionBinds(List binds) { + lastBinds = binds; + } + public static List lastBinds(Context context) { List binds = lastBinds; if (binds != null) return binds; @@ -306,7 +311,17 @@ public final class LinuxRuntime { private static boolean unreadablePciDevice() { File[] devices = new File("/sys/bus/pci/devices").listFiles(); - if (devices == null) return false; + if (devices == null) { + // The directory is there but the app may not list it (Android 13 on the AYN Thor): libpci then + // aborts the whole program with "Cannot open /sys/bus/pci/devices", which Chromium's GPU + // initialisation runs into. Not there at all (no PCI bus) is fine as it is. + try { + android.system.Os.stat("/sys/bus/pci/devices"); + return true; + } catch (android.system.ErrnoException e) { + return e.errno == android.system.OsConstants.EACCES; + } + } for (File device : devices) { if (!new File(device, "vendor").canRead()) return true; } diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt b/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt new file mode 100644 index 0000000000..ae4106a081 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt @@ -0,0 +1,269 @@ +package com.droiddeck.launcher.runtime + +import android.content.Context +import com.droiddeck.launcher.session.SessionPrefs +import java.io.File +import java.nio.file.Files + +/** + * Starts guest programs directly on the Android kernel, with no proot: the glibc loader of the + * rootfs runs the program and [HOOK] translates the guest's absolute paths, execs and the system + * calls Android's seccomp policy blocks, from inside the process. + */ +object NativeGuest { + private const val TAG = "NativeGuest" + const val HOOK = "usr/lib/libddnatv-hook.so" + private val LIB_DIRS = listOf("usr/lib", "usr/lib/aarch64-linux-gnu", "usr/local/lib", "usr/lib/systemd") + + /** Libraries every native program loads before the hook, as /etc/ld.so.preload does for proot. */ + fun preloadList(root: File): List { + val listed = File(root, "etc/droiddeck/native-preload").takeIf { it.isFile }?.readLines().orEmpty() + return listed.map { it.trim() }.filter { it.startsWith("/") && File(root, it.removePrefix("/")).isFile } + } + + fun enabled(context: Context): Boolean = + SessionPrefs.nativeUserland(context) && available(LinuxRuntime.rootDir(context)) + + /** The mode the running session started in; null between sessions. */ + @Volatile var sessionMode: Boolean? = null + + /** + * Whether a session's helpers (root commands, Flatpak sandboxes, installers) run natively: the + * session's own mode while one runs, as the setting can change under it and the rootfs is only + * prepared for native programs at a session's start. + */ + fun forSession(context: Context): Boolean = sessionMode ?: enabled(context) + + /** + * Development switch: etc/droiddeck/seccomp-sim names a helper (a guest path, then its arguments) + * that applies Android's app seccomp filter before the program starts, for devices that run apps + * without one (eng builds, or a permissive SELinux, where zygote skips it). Guest paths among the + * arguments are given as their places in the rootfs, as the helper runs before the hook. + */ + fun simulator(root: File): List? { + val words = File(root, "etc/droiddeck/seccomp-sim").takeIf { it.isFile }?.readText()?.trim() + ?.split(Regex("\\s+"))?.filter { it.isNotEmpty() } ?: return null + val helper = words.firstOrNull()?.takeIf { it.startsWith("/") }?.let { File(root, it.removePrefix("/")) } + ?.takeIf { it.isFile } ?: return null + return listOf(helper.path) + words.drop(1).map { if (it.startsWith("/")) File(root, it.removePrefix("/")).path else it } + } + + fun hook(root: File): File = File(root, HOOK) + + fun loader(root: File): File? = + listOf("usr/lib/ld-linux-aarch64.so.1", "lib/ld-linux-aarch64.so.1").map { File(root, it) }.firstOrNull { it.exists() } + + fun available(root: File): Boolean = hook(root).isFile && loader(root) != null + + fun libraryPath(root: File): String = LIB_DIRS.joinToString(":") { File(root, it).path } + + /** Environment every native process starts with; a guest `env -i` is re-seeded by the hook. */ + fun environment(root: File, preload: List = emptyList()): Map = linkedMapOf( + "DDNATV_ROOT" to root.path, + "LD_LIBRARY_PATH" to libraryPath(root), + "LD_PRELOAD" to (preload.map { File(root, it.removePrefix("/")).path } + hook(root).path).joinToString(":"), + ) + + /** + * The host command line for the guest program at [program] (an absolute guest path to a real + * file, not a symlink). + */ + fun command(root: File, program: String, args: List): List { + val loader = checkNotNull(loader(root)) { "no glibc loader in ${root.path}" } + val host = File(root, program.removePrefix("/")) + val launch = listOf(loader.path, "--argv0", host.name, host.path) + args + return simulator(root)?.let { it + launch } ?: launch + } + + /** The environment of the session process: the hook's root, the mounts proot used to make, the preload list. */ + fun sessionEnvironment(root: File, binds: List, hostname: String? = null): Map { + val preload = preloadList(root) + return linkedMapOf( + "DDNATV_ROOT" to root.path, + "DDNATV_HOSTNAME" to hostname.orEmpty(), + "DDNATV_BINDS" to binds.joinToString("|") { if (it.contains(':')) it else "$it:$it" }, + "DDNATV_PRELOAD" to preload.joinToString(":"), + "LD_LIBRARY_PATH" to libraryPath(root), + "LD_PRELOAD" to (preload.map { File(root, it.removePrefix("/")).path } + hook(root).path).joinToString(":"), + ).filterValues { it.isNotEmpty() } + } + + /** Where guest directory [cwd] is, through the session's mounts ([binds], "src:dst"); the rootfs if it is nowhere. */ + fun workingDirectory(root: File, binds: List, cwd: String): File = + File(BwrapSpawner.hostPath(binds.map { BwrapSpawner.Bind.parse(it) }, root.path, cwd)).takeIf { it.isDirectory } ?: root + + /** A process for [guest] (argv) started as proot would with [binds], in guest directory [cwd]. */ + fun processBuilder(context: Context, root: File, binds: List, guest: List, cwd: String = "/root"): ProcessBuilder = + ProcessBuilder(command(root, guest.first(), guest.drop(1))) + .directory(workingDirectory(root, binds, cwd)) + .also { builder -> + builder.environment().apply { + remove("LD_AUDIT") + putAll(sessionEnvironment(root, binds, SessionPrefs.guestHostname(context))) + } + } + + private const val RUNPATH_CONF = "etc/ld.so.conf.d/zz-droiddeck-native.conf" + private val SCAN_DIRS = listOf("usr/lib", "usr/local/lib", "opt") + + /** + * ld.so opens the absolute RUNPATH directories of a library itself, with system calls the hook + * cannot translate, so every one of them is also listed in ld.so.conf (which the hook maps). + * Rescanned when dpkg's state or a library directory changes. + */ + fun syncLibraryDirs(root: File) { + val conf = File(root, RUNPATH_CONF) + val signature = (listOf("var/lib/dpkg/status", "usr/lib", "usr/local/lib", "usr/lib/aarch64-linux-gnu") + .joinToString(",") { File(root, it).lastModified().toString() }) + "," + conf.parentFile.list()?.size + val stamp = File(root, "etc/droiddeck/native-runpath-signature") + if (conf.isFile && stamp.isFile && stamp.readText() == signature) return + val dirs = sortedSetOf() + for (name in SCAN_DIRS) File(root, name).walkTopDown().maxDepth(6) + .onEnter { !Files.isSymbolicLink(it.toPath()) } + .filter { it.isFile && !Files.isSymbolicLink(it.toPath()) && ".so" in it.name } + .forEach { file -> runCatching { ElfRunpath.read(file) }.getOrNull()?.let { dirs.addAll(it) } } + val kept = dirs.filter { it.startsWith("/usr") || it.startsWith("/lib") || it.startsWith("/opt") } + conf.parentFile?.mkdirs() + conf.writeText(kept.joinToString("\n", postfix = "\n")) + stamp.parentFile?.mkdirs() + stamp.writeText(signature) + } + + /** What a rootfs needs before anything runs in it natively; cheap once done. */ + fun prepare(context: Context, root: File) { + val relinked = relinkAbsoluteSymlinks(context, root) + if (relinked > 0) android.util.Log.i(TAG, "native userland: $relinked absolute symlinks made relative") + val patched = patchForSeccomp(root) + if (patched > 0) android.util.Log.i(TAG, "native userland: $patched system call sites in ld.so and libc answer Android's blocked calls") + syncLibraryDirs(root) + } + + /** See [ElfSeccompPatch]: the linker and libc must survive Android's seccomp filter before the hook loads. */ + fun patchForSeccomp(root: File): Int = + listOf("usr/lib/ld-linux-aarch64.so.1", "usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1", + "usr/lib/aarch64-linux-gnu/libc.so.6", "usr/lib/libc.so.6") + .map { File(root, it) }.filter { it.exists() } + .distinctBy { it.canonicalPath }.sumOf { file -> + runCatching { ElfSeccompPatch.patchFile(file) }.getOrElse { + android.util.Log.e(TAG, "native userland: could not patch $file for Android's seccomp filter; programs will die with SIGSYS", it) + 0 + } + } + + private val HOST_LINK_TARGETS = listOf("/proc", "/dev", "/sys", "/sdcard", "/storage", "/data", "/system", "/vendor", "/apex", + "/system_ext", "/product", "/odm", "/linkerconfig", "/mnt") + + /** + * Symbolic links the kernel follows by itself (ld.so, Steam, anything that skips the hook) read + * their absolute target on Android's own filesystem. The image's links were made for proot, + * 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" + if (stamp.isFile && stamp.readText() == version) return 0 + val rootPath = root.toPath() + var rewritten = 0 + Files.walkFileTree(rootPath, object : java.nio.file.SimpleFileVisitor() { + override fun visitFile(file: java.nio.file.Path, attrs: java.nio.file.attribute.BasicFileAttributes): java.nio.file.FileVisitResult { + if (attrs.isSymbolicLink) { + try { + val target = Files.readSymbolicLink(file).toString() + if (target.startsWith("/") && HOST_LINK_TARGETS.none { target == it || target.startsWith("$it/") } + && !target.startsWith(root.path)) { + val relative = file.parent.relativize(rootPath.resolve(target.removePrefix("/"))) + val temp = file.resolveSibling(".relink-" + file.fileName) + Files.deleteIfExists(temp) + Files.createSymbolicLink(temp, relative) + Files.move(temp, file, java.nio.file.StandardCopyOption.REPLACE_EXISTING) + rewritten++ + } + } catch (_: java.io.IOException) { + } + } + return java.nio.file.FileVisitResult.CONTINUE + } + + override fun visitFileFailed(file: java.nio.file.Path, exc: java.io.IOException) = java.nio.file.FileVisitResult.CONTINUE + }) + stamp.parentFile?.mkdirs() + stamp.writeText(version) + return rewritten + } + + /** + * 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 + * such mount when several share a source). + */ + fun guestLinkTarget(target: String, root: String, binds: List): String? { + if (!target.startsWith("/")) return null + fun under(path: String, dir: String) = path == dir || path.startsWith("$dir/") + var best: Pair? = if (under(target, root)) root to "/" else null + for (spec in binds) { + val colon = spec.indexOf(':') + if (colon < 0) continue + val src = spec.substring(0, colon).trimEnd('/') + val dst = spec.substring(colon + 1).trimEnd('/').ifEmpty { "/" } + if (src.isEmpty() || src == dst || !under(target, src)) continue + if (best == null || src.length > best.first.length) best = src to dst + } + val (src, dst) = best ?: return null + val rest = target.removePrefix(src) + return if (rest.isEmpty()) dst else dst.trimEnd('/') + rest + } + + /** + * Wine prefixes' drive links (dosdevices) hold guest paths ("s:" -> /mnt/droiddeck-sd), which + * proot follows and the hook follows inside a mount. Earlier native sessions made them point at + * their real places, which proot cannot see: those are given their guest paths back. + */ + fun relinkPrefixDrives(root: File, binds: List): Int { + val compatdata = listOf(File(root, "root/.local/share/Steam/steamapps/compatdata")) + binds.mapNotNull { spec -> + val colon = spec.indexOf(':') + if (colon > 0 && spec.substring(colon + 1).trimEnd('/').endsWith("/steamapps/compatdata")) File(spec.substring(0, colon)) else null + } + var rewritten = 0 + for (prefix in compatdata.distinct().flatMap { it.listFiles()?.toList().orEmpty() }) { + val devices = File(prefix, "pfx/dosdevices").toPath() + val links = try { + Files.newDirectoryStream(devices).use { it.toList() } + } catch (e: java.nio.file.NoSuchFileException) { + continue + } catch (e: java.io.IOException) { + android.util.Log.w(TAG, "native userland: could not read the drive links of $prefix", e) + continue + } catch (e: java.nio.file.DirectoryIteratorException) { + android.util.Log.w(TAG, "native userland: could not read the drive links of $prefix", e) + continue + } + for (link in links) { + try { + if (!Files.isSymbolicLink(link)) continue + val guest = guestLinkTarget(Files.readSymbolicLink(link).toString(), root.path, binds) ?: continue + val temp = link.resolveSibling(".relink-" + link.fileName) + Files.deleteIfExists(temp) + Files.createSymbolicLink(temp, java.nio.file.Paths.get(guest)) + Files.move(temp, link, java.nio.file.StandardCopyOption.REPLACE_EXISTING) + rewritten++ + } catch (_: java.io.IOException) { + } + } + } + return rewritten + } + + fun builder(root: File, cwd: String, argv: List, env: Map, preload: List = emptyList()): ProcessBuilder { + val program = argv.first() + val cmd = command(root, program, argv.drop(1)) + val builder = ProcessBuilder(cmd).redirectErrorStream(false) + builder.directory(File(root, cwd.removePrefix("/")).takeIf { it.isDirectory } ?: root) + builder.environment().apply { + clear() + putAll(environment(root, preload)) + putAll(env) + } + return builder + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/ProotFastPath.kt b/app/src/main/java/com/droiddeck/launcher/runtime/ProotFastPath.kt index e9a9f4a72e..11265bd8ae 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/ProotFastPath.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/ProotFastPath.kt @@ -12,15 +12,16 @@ object ProotFastPath { const val LIBRARY = "/usr/local/lib/libblfastpath.so" const val MAX_BINDS = 256 - fun allows(preference: Boolean, noSeccomp: Boolean, source: String): Boolean = - preference && !noSeccomp && source == LinuxRuntime.PROOT_PACK + fun allows(preference: Boolean, noSeccomp: Boolean, source: String, nativeUserland: Boolean = false): Boolean = + preference && !noSeccomp && !nativeUserland && source == LinuxRuntime.PROOT_PACK fun enabled(context: Context): Boolean { val preference = SessionPrefs.prootFastPath(context) val noSeccomp = SessionPrefs.prootNoSeccomp(context) + val nativeUserland = SessionPrefs.nativeUserland(context) val source = LinuxRuntime.prootSource(context) - val on = allows(preference, noSeccomp, source) - if (preference && !noSeccomp && !on) + val on = allows(preference, noSeccomp, source, nativeUserland) + if (preference && !noSeccomp && !nativeUserland && !on) Log.i(TAG, "proot: fast path off - binary is $source, not the patched proot") return on } diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/RootSpawner.kt b/app/src/main/java/com/droiddeck/launcher/runtime/RootSpawner.kt index 1138bab0de..a96ff46f38 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/RootSpawner.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/RootSpawner.kt @@ -113,20 +113,26 @@ object RootSpawner { val argv = request.getJSONArray("argv").let { a -> (0 until a.length()).map { a.getString(it) } } if (argv.isEmpty()) throw IOException("no command") val cwd = request.optString("cwd", "/root").ifEmpty { "/root" } - val cmd = LinuxRuntime.prootPrefix(context, root, cwd, true) - cmd.add(1, "--link2symlink") - LinuxRuntime.lastBinds(context).filterNot { it.startsWith("/proc/self/fd/") }.forEach { cmd.add("-b"); cmd.add(it) } - cmd.add("/usr/bin/env") - cmd.add("-i") - request.optJSONObject("env")?.let { e -> e.keys().forEach { k -> cmd.add("$k=${e.getString(k)}") } } - cmd.addAll(argv) - val builder = ProcessBuilder(cmd).directory(root) - builder.environment().apply { - clear() - put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) - put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(context).path) - if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") - LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + val binds = LinuxRuntime.lastBinds(context).filterNot { it.startsWith("/proc/self/fd/") } + val guest = mutableListOf("/usr/bin/env", "-i") + request.optJSONObject("env")?.let { e -> e.keys().forEach { k -> guest.add("$k=${e.getString(k)}") } } + guest.addAll(argv) + val builder = if (NativeGuest.forSession(context)) { + NativeGuest.processBuilder(context, root, binds, guest, cwd) + } else { + val cmd = LinuxRuntime.prootPrefix(context, root, cwd, true) + cmd.add(1, "--link2symlink") + binds.forEach { cmd.add("-b"); cmd.add(it) } + cmd.addAll(guest) + ProcessBuilder(cmd).directory(root).also { b -> + b.environment().apply { + clear() + put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) + put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(context).path) + if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") + LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + } + } } val streams = listOf(stdio(fds.getOrNull(0), false), stdio(fds.getOrNull(1), true), stdio(fds.getOrNull(2), true)) try { diff --git a/app/src/main/java/com/droiddeck/launcher/session/BottomDesktop.kt b/app/src/main/java/com/droiddeck/launcher/session/BottomDesktop.kt index 36b9c3dfdc..10d031715b 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/BottomDesktop.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/BottomDesktop.kt @@ -39,6 +39,7 @@ object BottomDesktop { val sessionDir: File, val generation: Int, val installed: Boolean, + val nativeUserland: Boolean, ) @Volatile private var launch: Launch? = null @@ -89,10 +90,11 @@ object BottomDesktop { root: File, sessionDir: File, generation: Int, + nativeUserland: Boolean, stillCurrent: () -> Boolean, ) { val prepared = Launch(ArrayList(guest), ArrayList(binds), sessionRoot, hostEnvironment.copyOf(), root, sessionDir, generation, - DesktopCatalog.desktopInstalled(root)) + DesktopCatalog.desktopInstalled(root), nativeUserland) synchronized(launchLock) { if (stillCurrent()) launch = prepared } } @@ -320,9 +322,17 @@ object BottomDesktop { add("BL_IME_DIR=" + imeDir.path) add(SCRIPT) } - val command = LinuxRuntime.commandWithBinds(context, current.root, binds, guest) + val command = if (current.nativeUserland) { + com.droiddeck.launcher.runtime.NativeGuest.command(current.root, guest.first(), guest.drop(1)) + } else { + LinuxRuntime.commandWithBinds(context, current.root, binds, guest) + } + val environment = if (current.nativeUserland) { + com.droiddeck.launcher.runtime.NativeGuest.sessionEnvironment(current.root, binds, SessionPrefs.guestHostname(context)) + .map { (k, v) -> "$k=$v" }.toTypedArray() + } else current.hostEnvironment val line = command.joinToString(" ") { it.replace("\\", "\\\\").replace(" ", "\\ ") } - val pid = HostProcess.start(line, current.hostEnvironment, current.root, { status -> + val pid = HostProcess.start(line, environment, current.root, { status -> main.post { if (generation != guestGeneration) return@post Log.i(TAG, "the desktop ended ($status)") diff --git a/app/src/main/java/com/droiddeck/launcher/session/ProtonExtras.kt b/app/src/main/java/com/droiddeck/launcher/session/ProtonExtras.kt index d17d40df48..94334f4dc9 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/ProtonExtras.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/ProtonExtras.kt @@ -283,14 +283,20 @@ object ProtonExtras { "LANG=C.UTF-8", "XDG_DATA_HOME=/root/.local/share", "/usr/local/bin/droiddeck-proton-extra", "/root/.local/share/Steam", archive.absolutePath, ) - val command = LinuxRuntime.command(context, null, runtimeDir, null, guest) - val process = ProcessBuilder(command) - .directory(root) - .redirectErrorStream(true) - val hostEnv = process.environment() - hostEnv["PROOT_LOADER"] = LinuxRuntime.prootLoader(context).path - hostEnv["PROOT_TMP_DIR"] = LinuxRuntime.prootTempDir(context).path - LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { hostEnv["LD_LIBRARY_PATH"] = it } + val process = if (com.droiddeck.launcher.runtime.NativeGuest.enabled(context)) { + com.droiddeck.launcher.runtime.NativeGuest.processBuilder(context, root, + LinuxRuntime.binds(context, null, runtimeDir, null, null), guest).redirectErrorStream(true) + } else { + ProcessBuilder(LinuxRuntime.command(context, null, runtimeDir, null, guest)) + .directory(root) + .redirectErrorStream(true) + .also { builder -> + val hostEnv = builder.environment() + hostEnv["PROOT_LOADER"] = LinuxRuntime.prootLoader(context).path + hostEnv["PROOT_TMP_DIR"] = LinuxRuntime.prootTempDir(context).path + LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { hostEnv["LD_LIBRARY_PATH"] = it } + } + } val child = process.start() var placed: String? = null child.inputStream.bufferedReader().useLines { lines -> diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt index 3ade0b497e..8c5e7431b1 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt @@ -83,6 +83,7 @@ object SessionFiles { "libblsession.so" to "usr/local/lib/libblsession.so", "libfakeinput.so" to "usr/local/lib/libfakeinput.so", "libblfastpath.so" to "usr/local/lib/libblfastpath.so", + "libddnatv-hook.so" to "usr/lib/libddnatv-hook.so", "libblaudit.so" to "usr/local/lib/libblaudit.so", "libssbs.so" to "usr/local/lib/libssbs.so", "usr/local/bin/droiddeck-session" to "usr/local/bin/droiddeck-session", @@ -289,12 +290,16 @@ object SessionFiles { if (!File(Environment.getExternalStorageDirectory(), NO_PAD_SWITCH).exists()) { preload.append("/usr/local/lib/libfakeinput.so\n") } + val nativeUserland = SessionPrefs.nativeUserland(context) + if (nativeUserland) com.droiddeck.launcher.runtime.NativeGuest.prepare(context, root) if (ProotFastPath.enabled(context)) { val library = com.droiddeck.launcher.agent.AgentEnv.fastPathLibrary(context) preload.append(library + "\n") if (startsSession && library != ProotFastPath.LIBRARY) Log.i(TAG, "fast path preload: $library (debugging override)") } val etc = File(root, "etc").apply { mkdirs() } + File(etc, "droiddeck").mkdirs() + FileUtils.writeString(File(etc, "droiddeck/native-preload"), preload.toString()) val staged = File(etc, "ld.so.preload.staged") if (!FileUtils.writeString(staged, preload.toString()) || !staged.renameTo(File(etc, "ld.so.preload"))) { diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index c8e81550a5..4b0a94f7b6 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -752,6 +752,12 @@ object SessionPrefs { prefs(context).edit().putBoolean("prootNoSeccomp", on).apply() } + fun nativeUserland(context: Context): Boolean = prefs(context).getBoolean("nativeUserland", false) + + fun setNativeUserland(context: Context, on: Boolean) { + prefs(context).edit().putBoolean("nativeUserland", on).apply() + } + fun prootFastPath(context: Context): Boolean = prefs(context).getBoolean("prootFastPath", true) fun setProotFastPath(context: Context, on: Boolean) { diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index 5fb5e37f29..4faa870866 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -665,6 +665,10 @@ class SessionService : Service() { FileUtils.clear(File(LinuxRuntime.liveDir(this), "shm")) val binds = sessionBinds(controllersOn, fakeInputDir, sessionDir, guest, sessionRoot, runtimeDir) + val nativeUserland = com.droiddeck.launcher.runtime.NativeGuest.enabled(this) + synchronized(stopLock) { + if (gen == sessionGen && SessionState.running) com.droiddeck.launcher.runtime.NativeGuest.sessionMode = nativeUserland + } val fastPathKey = if (ProotFastPath.enabled(this)) { val prootBinds = LinuxRuntime.binds( this, sessionRoot, runtimeDir, Environment.getExternalStorageDirectory(), binds, @@ -681,27 +685,42 @@ class SessionService : Service() { } } else null - val command = LinuxRuntime.command( - this, sessionRoot, runtimeDir, Environment.getExternalStorageDirectory(), binds, guest, - ) - - val hostEnv = HostEnvironment() - hostEnv["PROOT_LOADER"] = LinuxRuntime.prootLoader(this).path - hostEnv["PROOT_TMP_DIR"] = LinuxRuntime.prootTempDir(this).path - if (SessionPrefs.prootNoSeccomp(this)) { - hostEnv["PROOT_NO_SECCOMP"] = "1" - Log.i(TAG, "proot: seccomp acceleration off by request") + val sessionBindSpecs = LinuxRuntime.binds(this, sessionRoot, runtimeDir, Environment.getExternalStorageDirectory(), binds) + val command = if (nativeUserland) { + LinuxRuntime.rememberSessionBinds(sessionBindSpecs) + com.droiddeck.launcher.runtime.NativeGuest.command(root, guest.first(), guest.drop(1)) + } else { + LinuxRuntime.command(this, sessionRoot, runtimeDir, Environment.getExternalStorageDirectory(), binds, guest) } - fastPathKey?.let { ProotFastPath.hostEnv(it).let { (k, v) -> hostEnv[k] = v } } + + val prootEnv = HostEnvironment() + prootEnv["PROOT_LOADER"] = LinuxRuntime.prootLoader(this).path + prootEnv["PROOT_TMP_DIR"] = LinuxRuntime.prootTempDir(this).path + if (SessionPrefs.prootNoSeccomp(this)) { + prootEnv["PROOT_NO_SECCOMP"] = "1" + if (!nativeUserland) Log.i(TAG, "proot: seccomp acceleration off by request") + } + fastPathKey?.let { ProotFastPath.hostEnv(it).let { (k, v) -> prootEnv[k] = v } } for (key in listOf("PROOT_PROFILE", "PROOT_PROFILE_SLOW_US")) { - guest.lastOrNull { it.startsWith("$key=") }?.substringAfter('=')?.let { hostEnv[key] = it } + guest.lastOrNull { it.startsWith("$key=") }?.substringAfter('=')?.let { prootEnv[key] = it } } val prootLibs = LinuxRuntime.prootLibraryPath(this) - if (prootLibs.isNotEmpty()) hostEnv["LD_LIBRARY_PATH"] = prootLibs + if (prootLibs.isNotEmpty()) prootEnv["LD_LIBRARY_PATH"] = prootLibs - val terminalCommand = LinuxRuntime.command( - this, sessionRoot, runtimeDir, Environment.getExternalStorageDirectory(), binds, shellGuest, - ) + val drives = com.droiddeck.launcher.runtime.NativeGuest.relinkPrefixDrives(root, sessionBindSpecs) + if (drives > 0) Log.i(TAG, "$drives Wine drive links given their guest paths back") + val hostEnv = if (nativeUserland) HostEnvironment().also { env -> + Log.i(TAG, "native userland: no proot (${sessionBindSpecs.size} mounts)") + com.droiddeck.launcher.runtime.NativeGuest.sessionEnvironment(root, sessionBindSpecs, SessionPrefs.guestHostname(this)) + .forEach { (k, v) -> env[k] = v } + } else prootEnv + + // The terminal and the bottom desktop run the way the session does: without proot when it does + val terminalCommand = if (nativeUserland) { + com.droiddeck.launcher.runtime.NativeGuest.command(root, shellGuest.first(), shellGuest.drop(1)) + } else { + LinuxRuntime.command(this, sessionRoot, runtimeDir, Environment.getExternalStorageDirectory(), binds, shellGuest) + } val terminalHostEnvironment = LinkedHashMap(System.getenv()) hostEnv.asArray().forEach { entry -> val separator = entry.indexOf('=') @@ -711,7 +730,7 @@ class SessionService : Service() { SessionTerminal.prepare(terminalCommand, terminalHostEnvironment.map { (key, value) -> "$key=$value" }.toTypedArray(), root, gen) val (fastPathName, _) = ProotFastPath.hostEnv("") BottomDesktop.prepare(desktopGuest, binds, sessionRoot, - hostEnv.asArray().filterNot { it.startsWith("$fastPathName=") }.toTypedArray(), root, sessionDir, gen, + prootEnv.asArray().filterNot { it.startsWith("$fastPathName=") }.toTypedArray(), root, sessionDir, gen, nativeUserland, ) { gen == sessionGen && SessionState.running } } @@ -2023,6 +2042,8 @@ class SessionService : Service() { if (gen != null && gen != sessionGen) return cancelQueuedSteamGame() gameExitWatchToken++ + // also when startup stopped before the guest ran + com.droiddeck.launcher.runtime.NativeGuest.sessionMode = null if (!SessionState.running) return if (relaunch) SessionState.confirmRelaunchStop() SessionEvents.record("guest.exited", mapOf("status" to status)) diff --git a/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt b/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt index 2146ac1942..67f013b78f 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt @@ -22,7 +22,7 @@ object SettingsReset { "gpuDriverMode", "androidDriver", "linuxDriver", "linuxDriver.steam", "linuxDriver.desktop", "tuSysmem", "glThread", "noGlError", "zinkLazy", "mangoapp", "gpuClockPin", "gamescopeRealtime", "ramCapMb", "vramCapMb", "zramClient", "directAudio", "mic", "wifiDiscovery", "clientCpusOverride", "clientCpus", "gameCpus", - "noXalia", "fastSync", "fsyncFirst", "syncFallback", "prootNoSeccomp", "prootFastPath", "cursorComposite", + "noXalia", "fastSync", "fsyncFirst", "syncFallback", "prootNoSeccomp", "prootFastPath", "nativeUserland", "cursorComposite", "guestHostname", "fexPreset", "forceSsbs", "logs", "gameDebugLogsEnabled", "enhancedDiagnosticsNext", "storageDiagnostics", "upscaler", "upscaleSharpness", "screenEffects", "textureAnisotropy", "textureLodBias", "steamDownloadsInBackground", "steam_offline", "bootMovie", "bootMovieSound", "bootMovieVolume", "bootMovieSeconds", diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogComet.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogComet.kt index dec376c456..8ad240e651 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogComet.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogComet.kt @@ -139,15 +139,21 @@ object GogComet { !File(LinuxRuntime.rootDir(context), BIN.removePrefix("/")).isFile) return reply(false, "not-installed") val data = File(context.filesDir, "gog-achievements").apply { mkdirs() } val ready = CountDownLatch(1) - val command = LinuxRuntime.command(context, null, null, Environment.getExternalStorageDirectory(), - listOf("/usr/bin/env", "HOME=/root", "XDG_DATA_HOME=${data.path}/data", "XDG_CONFIG_HOME=${data.path}/config", "COMET_LOG=off", "COMET_ALLOWED_CLIENTS=${policy.path}", BIN, "--credentials-stdin")) - val pb = ProcessBuilder(command).redirectErrorStream(true) - pb.environment().apply { - remove("LD_PRELOAD"); remove("LD_AUDIT"); remove("LD_LIBRARY_PATH") - put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) - put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(context).path) - if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") - LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + val guest = listOf("/usr/bin/env", "HOME=/root", "XDG_DATA_HOME=${data.path}/data", "XDG_CONFIG_HOME=${data.path}/config", "COMET_LOG=off", "COMET_ALLOWED_CLIENTS=${policy.path}", BIN, "--credentials-stdin") + val pb = if (com.droiddeck.launcher.runtime.NativeGuest.forSession(context)) { + val root = LinuxRuntime.rootDir(context) + com.droiddeck.launcher.runtime.NativeGuest.processBuilder(context, root, + LinuxRuntime.binds(context, null, null, Environment.getExternalStorageDirectory(), null), guest).redirectErrorStream(true) + } else { + ProcessBuilder(LinuxRuntime.command(context, null, null, Environment.getExternalStorageDirectory(), guest)).redirectErrorStream(true).also { + it.environment().apply { + remove("LD_PRELOAD"); remove("LD_AUDIT"); remove("LD_LIBRARY_PATH") + put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) + put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(context).path) + if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") + LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + } + } } try { val child = pb.start() diff --git a/app/src/main/java/com/droiddeck/launcher/stores/xbox/XboxCredentials.kt b/app/src/main/java/com/droiddeck/launcher/stores/xbox/XboxCredentials.kt index c8b126519f..59403dac0f 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/xbox/XboxCredentials.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/xbox/XboxCredentials.kt @@ -64,15 +64,32 @@ internal class XboxCredentials(private val app: Context, expectedGeneration: Str if (root <= 0 || pid <= 0) return null val status = java.io.File("/proc/$pid/status").readLines() val tracer = status.first { it.startsWith("TracerPid:") }.substringAfter(':').trim().toInt() - val tracerGroup = java.io.File("/proc/$tracer/status").readLines() - .first { it.startsWith("Tgid:") }.substringAfter(':').trim().toInt() - if (tracerGroup != root) return null + if (tracer != 0) { + // under proot, every guest process is traced by the proot the app started + val tracerGroup = java.io.File("/proc/$tracer/status").readLines() + .first { it.startsWith("Tgid:") }.substringAfter(':').trim().toInt() + if (tracerGroup != root) return null + } else if (!descendsFrom(pid, root)) { + // without proot, the process the app started is the backend itself (or its parent) + return null + } val executable = android.system.Os.stat(binary.path) if (!mapsBackend(java.io.File("/proc/$pid/maps").readText(), binary.path, executable.st_ino)) return null return java.io.File("/proc/$pid/stat").readText().substringAfterLast(") ").split(' ')[19] + ":${executable.st_dev}:${executable.st_ino}" } + private fun descendsFrom(pid: Int, root: Int): Boolean { + var current = pid + repeat(32) { + if (current == root) return true + if (current <= 1) return false + current = java.io.File("/proc/$current/status").readLines() + .firstOrNull { it.startsWith("PPid:") }?.substringAfter(':')?.trim()?.toIntOrNull() ?: return false + } + return false + } + fun signalBackend(signal: Int) { val process = backend ?: return if (!process.isAlive) return diff --git a/app/src/main/java/com/droiddeck/launcher/stores/xbox/Xodus.kt b/app/src/main/java/com/droiddeck/launcher/stores/xbox/Xodus.kt index f803d06416..f27794d1b5 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/xbox/Xodus.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/xbox/Xodus.kt @@ -88,7 +88,8 @@ internal object Xodus { val fastPathPreloaded = runCatching { File(root, "etc/ld.so.preload").useLines { lines -> lines.any { it.trim() == ProotFastPath.LIBRARY } } }.getOrDefault(false) - val fastPathKey = if (ProotFastPath.enabled(app) && fastPathPreloaded && + val native = com.droiddeck.launcher.runtime.NativeGuest.forSession(app) + val fastPathKey = if (!native && ProotFastPath.enabled(app) && fastPathPreloaded && File(root, ProotFastPath.LIBRARY.removePrefix("/")).isFile) ProotFastPath.key(root, binds) else null val guestCommand = mutableListOf( "/usr/bin/env", "HOME=/root", "XODUS_LOG=off", "RUST_LOG=off", @@ -98,12 +99,12 @@ internal object Xodus { guestCommand.addAll(guestCommand.lastIndex, ProotFastPath.guestEnv(root, binds, key)) } guestCommand.addAll(args) - val command = LinuxRuntime.commandWithBinds(app, root, binds, guestCommand) - val builder = ProcessBuilder(command).directory(LinuxRuntime.rootDir(app)) + val builder = if (native) com.droiddeck.launcher.runtime.NativeGuest.processBuilder(app, root, binds, guestCommand) + else ProcessBuilder(LinuxRuntime.commandWithBinds(app, root, binds, guestCommand)).directory(LinuxRuntime.rootDir(app)) if (launchDirectory != null) builder.environment()["TMPDIR"] = launchDirectory.path builder.environment()["DROIDDECK_XBOX_CREDENTIAL_PORT"] = credentials.port.toString() builder.environment()["DROIDDECK_XBOX_CREDENTIAL_KEY"] = credentials.key - builder.environment().apply { + if (!native) builder.environment().apply { remove("LD_PRELOAD"); remove("LD_AUDIT"); remove("LD_LIBRARY_PATH") put("PROOT_LOADER", LinuxRuntime.prootLoader(app).path) put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(app).path) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/PerformanceDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/PerformanceDialog.kt index 2c0927bc88..f8f44907a7 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/PerformanceDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/PerformanceDialog.kt @@ -53,6 +53,7 @@ fun PerformancePage( ramCapAvailable: Boolean, prootNoSeccomp: Boolean, prootFastPath: Boolean, + nativeUserland: Boolean, cursorComposite: Boolean, guestHostname: String, phantomWarning: String?, @@ -72,6 +73,7 @@ fun PerformancePage( onVramCapMb: (Int) -> Unit, onProotNoSeccomp: (Boolean) -> Unit, onProotFastPath: (Boolean) -> Unit, + onNativeUserland: (Boolean) -> Unit, onCursorComposite: (Boolean) -> Unit, onGuestHostname: (String) -> Unit, onClientCore: (Int, Boolean) -> Unit, @@ -187,9 +189,18 @@ fun PerformancePage( ) ToggleRow( host, "fastpath", stringResource(R.string.perf_fastpath), - if (prootNoSeccomp) stringResource(R.string.perf_fastpath_needs_seccomp) - else stringResource(R.string.perf_fastpath_hint), - prootFastPath && !prootNoSeccomp, enabled = !prootNoSeccomp, onChange = onProotFastPath, + when { + nativeUserland -> stringResource(R.string.perf_fastpath_needs_proot) + prootNoSeccomp -> stringResource(R.string.perf_fastpath_needs_seccomp) + else -> stringResource(R.string.perf_fastpath_hint) + }, + prootFastPath && !prootNoSeccomp && !nativeUserland, enabled = !prootNoSeccomp && !nativeUserland, + onChange = onProotFastPath, + ) + ToggleRow( + host, "nativeuserland", stringResource(R.string.perf_native_userland), + stringResource(R.string.perf_native_userland_hint), + nativeUserland, onChange = onNativeUserland, ) ToggleRow( host, "cursorcomposite", stringResource(R.string.perf_cursor_composite), diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6a7a221a0b..3351982981 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -648,7 +648,10 @@ Disables Proton\'s gamepad navigation helper, which costs CPU in every game. Run proot without seccomp May fix missing-syscall errors on some kernels. Can reduce performance. + Run without proot (experimental) + Starts the Linux side directly on Android. Applies to the next session. Turn off if something fails to start. proot fast path + Not used without proot. Needs proot\'s seccomp filter. Faster file lookups and less stutter. Turn off if a program can\'t find its files. Draw the cursor inside gamescope diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/BwrapSpawnerTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/BwrapSpawnerTest.kt index a71063bf35..23db657ed5 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/BwrapSpawnerTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/BwrapSpawnerTest.kt @@ -55,6 +55,26 @@ class BwrapSpawnerTest { assertTrue(Bind("/rootfs/etc/droiddeck/proc/stat", "/newproc/stat") in binds) } + @Test fun theSessionsRootfsSettingsStayOutOfTheSandbox() { + val rootfs = "/data/app/linuxfs" + val env = mutableMapOf( + "DDNATV_ROOT" to rootfs, "DDNATV_BINDS" to "/dev:/dev", "HOME" to "/root", + "SPA_PLUGIN_DIR" to "/usr/local/lib/droiddeck-pipewire/lib/spa-0.2", + "LD_PRELOAD" to "/run/droiddeck-preload/libblsession.so:$rootfs/usr/local/lib/libblsession.so:$rootfs/usr/lib/libddnatv-hook.so", + ) + BwrapSpawner.withoutRootfsSettings(env, rootfs) + assertEquals(mapOf("HOME" to "/root", "LD_PRELOAD" to "/run/droiddeck-preload/libblsession.so"), env) + val onlySession = mutableMapOf("LD_PRELOAD" to "$rootfs/usr/lib/libddnatv-hook.so") + BwrapSpawner.withoutRootfsSettings(onlySession, rootfs) + assertTrue(onlySession.isEmpty()) + val wayland = mutableMapOf("SDL_VIDEODRIVER" to "x11", "WAYLAND_DISPLAY" to "wayland-0") + BwrapSpawner.withoutRootfsSettings(wayland, rootfs) + assertEquals(mapOf("WAYLAND_DISPLAY" to "wayland-0"), wayland) + val x11 = mutableMapOf("SDL_VIDEODRIVER" to "x11", "WAYLAND_DISPLAY" to "wayland-0", "DISPLAY" to ":99") + BwrapSpawner.withoutRootfsSettings(x11, rootfs) + assertEquals("x11", x11["SDL_VIDEODRIVER"]) + } + @Test fun normalizeResolvesDotsLexically() { assertEquals("/usr/lib/x", BwrapSpawner.normalize("/usr/lib/GL/../x")) assertEquals("/", BwrapSpawner.normalize("/../..")) diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/ElfSeccompPatchTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/ElfSeccompPatchTest.kt new file mode 100644 index 0000000000..386017d154 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/runtime/ElfSeccompPatchTest.kt @@ -0,0 +1,53 @@ +package com.droiddeck.launcher.runtime + +import java.nio.ByteBuffer +import java.nio.ByteOrder +import org.junit.Assert.assertEquals +import org.junit.Test + +class ElfSeccompPatchTest { + private fun elfWith(vararg words: Int): ByteArray { + val data = ByteArray(0x200) + val b = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN) + b.put(0, 0x7f); b.put(1, 'E'.code.toByte()); b.put(2, 'L'.code.toByte()); b.put(3, 'F'.code.toByte()) + b.put(4, 2); b.put(5, 1) + b.putShort(18, 183) + b.putLong(32, 64) + b.putShort(54, 56); b.putShort(56, 1) + b.putInt(64, 1); b.putInt(68, 5) + b.putLong(72, 0x100); b.putLong(96, 0x100) + words.forEachIndexed { i, w -> b.putInt(0x100 + i * 4, w) } + return data + } + + @Test + fun theSvcAfterEachBlockedCallIsAnsweredInPlace() { + val svc = 0xd4000001.toInt() + val data = elfWith(0xd2800c68.toInt(), 0xaa0003e1.toInt(), svc, 0xd28024a8.toInt(), svc, 0xd2803668.toInt(), svc) + assertEquals(3, ElfSeccompPatch.patch(data)) + val b = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN) + assertEquals(0xd503201f.toInt(), b.getInt(0x108)) + assertEquals(0x928004a0.toInt(), b.getInt(0x110)) + assertEquals(0x928004a0.toInt(), b.getInt(0x118)) + assertEquals(0, ElfSeccompPatch.patch(data)) + } + + @Test + fun aSecondRunLeavesTheNextCallAlone() { + val svc = 0xd4000001.toInt() + val exit = 0xd2800ba8.toInt() + for (blocked in listOf(0xd2803668.toInt(), 0xd2800c68.toInt())) { + val data = elfWith(blocked, svc, 0xaa1303e0.toInt(), exit, svc) + assertEquals(1, ElfSeccompPatch.patch(data)) + assertEquals(0, ElfSeccompPatch.patch(data)) + assertEquals(svc, ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).getInt(0x110)) + } + } + + @Test + fun otherSystemCallsAreLeftAlone() { + val svc = 0xd4000001.toInt() + val data = elfWith(0xd2800ba8.toInt(), svc) + assertEquals(0, ElfSeccompPatch.patch(data)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt new file mode 100644 index 0000000000..3188eb4e62 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt @@ -0,0 +1,99 @@ +package com.droiddeck.launcher.runtime + +import java.io.File +import java.nio.file.Files +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test + +class NativeGuestTest { + private fun rootWithLoader(): File = Files.createTempDirectory("native-root").toFile().also { + File(it, "usr/lib").mkdirs() + File(it, "usr/lib/ld-linux-aarch64.so.1").writeText("") + } + + @Test + fun theSeccompSwitchNamesAHelperAndItsArgumentsInTheRootfs() { + val root = Files.createTempDirectory("rootfs").toFile() + assertEquals(null, NativeGuest.simulator(root)) + File(root, "usr/local/bin").mkdirs() + File(root, "usr/local/bin/seccomp-sim").writeText("") + File(root, "etc/droiddeck").mkdirs() + File(root, "etc/droiddeck/seccomp-sim").writeText("/usr/local/bin/seccomp-sim -f /etc/droiddeck/app.bpf\n") + assertEquals(listOf(File(root, "usr/local/bin/seccomp-sim").path, "-f", File(root, "etc/droiddeck/app.bpf").path), + NativeGuest.simulator(root)) + File(root, "etc/droiddeck/seccomp-sim").writeText("/usr/local/bin/missing") + assertEquals(null, NativeGuest.simulator(root)) + } + + @Test + fun aWorkingDirectoryInAMountIsItsSource() { + val root = Files.createTempDirectory("rootfs").toFile() + val storage = Files.createTempDirectory("storage").toFile() + File(root, "root").mkdirs() + // a mount spec is "src:dst"; Windows' drive letters cannot be written in one + org.junit.Assume.assumeFalse(storage.path.contains(':')) + val binds = listOf("${storage.path}:/root/Storage", "/proc") + assertEquals(storage, NativeGuest.workingDirectory(root, binds, "/root/Storage")) + assertEquals(File(root, "root"), NativeGuest.workingDirectory(root, binds, "/root")) + assertEquals(root, NativeGuest.workingDirectory(root, binds, "/nowhere")) + } + + @Test + fun commandRunsTheProgramThroughTheRootfsLoader() { + val root = rootWithLoader() + val command = NativeGuest.command(root, "/usr/bin/env", listOf("-i", "A=1")) + assertEquals(File(root, "usr/lib/ld-linux-aarch64.so.1").path, command[0]) + assertEquals(listOf("--argv0", "env", File(root, "usr/bin/env").path, "-i", "A=1"), command.drop(1)) + } + + @Test + fun mountsWithoutADestinationMapToThemselves() { + val root = rootWithLoader() + val env = NativeGuest.sessionEnvironment(root, listOf("/dev", "/a:/b")) + assertEquals("/dev:/dev|/a:/b", env["DDNATV_BINDS"]) + assertEquals(root.path, env["DDNATV_ROOT"]) + assertTrue(env.getValue("LD_PRELOAD").replace(File.separatorChar, '/').endsWith("usr/lib/libddnatv-hook.so")) + } + + private val linkRoot = "/data/user/0/app/files/linuxfs" + private val sdBinds = listOf( + "/mnt/media_rw/CARD/files/steam:/mnt/droiddeck-sd", + "/data/user/0/app/files/steam-libraries/x/steamapps/compatdata:/mnt/droiddeck-sd/steamapps/compatdata", + "/storage/emulated/0", + ) + + @Test + fun aDriveLinkToTheCardGetsItsGuestPathBack() { + assertEquals("/mnt/droiddeck-sd", NativeGuest.guestLinkTarget("/mnt/media_rw/CARD/files/steam", linkRoot, sdBinds)) + assertEquals("/mnt/droiddeck-sd/steamapps/compatdata/1", + NativeGuest.guestLinkTarget("/data/user/0/app/files/steam-libraries/x/steamapps/compatdata/1", linkRoot, sdBinds)) + } + + @Test + fun aDriveLinkIntoTheRootfsGetsItsGuestPathBack() { + assertEquals("/", NativeGuest.guestLinkTarget(linkRoot, linkRoot, sdBinds)) + assertEquals("/root/Games", NativeGuest.guestLinkTarget("$linkRoot/root/Games", linkRoot, sdBinds)) + } + + @Test + fun guestPathsStayAsTheyAre() { + assertEquals(null, NativeGuest.guestLinkTarget("/mnt/droiddeck-sd", linkRoot, sdBinds)) + assertEquals(null, NativeGuest.guestLinkTarget("/", linkRoot, sdBinds)) + assertEquals(null, NativeGuest.guestLinkTarget("../drive_c", linkRoot, sdBinds)) + assertEquals(null, NativeGuest.guestLinkTarget("/storage/emulated/0/Games", linkRoot, sdBinds)) + } + + @Test + fun aSiblingOfAMountSourceIsNotPartOfIt() { + assertEquals(null, NativeGuest.guestLinkTarget("/mnt/media_rw/CARD/files/steamx", linkRoot, sdBinds)) + assertEquals(null, NativeGuest.guestLinkTarget("${linkRoot}x/root", linkRoot, sdBinds)) + } + + @Test + fun theFirstOfMountsSharingASourceIsTheGuestPath() { + val binds = sdBinds + "/data/user/0/app/files/steam-libraries/x/steamapps/compatdata:/mnt/bannerlator-sd/steamapps/compatdata" + assertEquals("/mnt/droiddeck-sd/steamapps/compatdata/2", + NativeGuest.guestLinkTarget("/data/user/0/app/files/steam-libraries/x/steamapps/compatdata/2", linkRoot, binds)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/ProotFastPathTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/ProotFastPathTest.kt index 12fb45bf91..f413618942 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/ProotFastPathTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/ProotFastPathTest.kt @@ -33,4 +33,8 @@ class ProotFastPathTest { assertFalse(ProotFastPath.allows(preference = false, noSeccomp = false, source = LinuxRuntime.PROOT_PACK)) assertFalse(ProotFastPath.allows(preference = true, noSeccomp = true, source = LinuxRuntime.PROOT_PACK)) } + + @Test fun fastPathStaysOffWithoutProot() { + assertFalse(ProotFastPath.allows(preference = true, noSeccomp = false, source = LinuxRuntime.PROOT_PACK, nativeUserland = true)) + } } diff --git a/tools/build_local.sh b/tools/build_local.sh index d7f2ca2a21..d564f1e3c0 100755 --- a/tools/build_local.sh +++ b/tools/build_local.sh @@ -133,6 +133,7 @@ docker run --rm --platform "${docker_platform}" \ aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \ -o "$d/libblfastpath.so" tools/proot/fastpath/fastpath.c -ldl aarch64-linux-gnu-strip --strip-unneeded "$d/libblfastpath.so" + bash tools/nativeguest/build.sh "$d/libddnatv-hook.so" aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -nostdlib -ffreestanding -fno-stack-protector \ -fno-tree-loop-distribute-patterns -Wl,-z,defs -o "$d/libblaudit.so" tools/proot/fastpath/audit.c aarch64-linux-gnu-strip --strip-unneeded "$d/libblaudit.so" diff --git a/tools/components/build-arm64-preloads.sh b/tools/components/build-arm64-preloads.sh index a38a1edaaa..e06aa2afc1 100644 --- a/tools/components/build-arm64-preloads.sh +++ b/tools/components/build-arm64-preloads.sh @@ -12,6 +12,7 @@ aarch64-linux-gnu-strip --strip-unneeded "$D/libblsession.so" aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \ -o "$D/libblfastpath.so" tools/proot/fastpath/fastpath.c -ldl aarch64-linux-gnu-strip --strip-unneeded "$D/libblfastpath.so" +bash tools/nativeguest/build.sh "$D/libddnatv-hook.so" aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -nostdlib -ffreestanding -fno-stack-protector \ -fno-tree-loop-distribute-patterns -Wl,-z,defs -o "$D/libblaudit.so" tools/proot/fastpath/audit.c aarch64-linux-gnu-strip --strip-unneeded "$D/libblaudit.so" diff --git a/tools/components/preloads.py b/tools/components/preloads.py index 808d510463..36d0b3dcc8 100644 --- a/tools/components/preloads.py +++ b/tools/components/preloads.py @@ -51,6 +51,7 @@ def source_files(group, root=ROOT, commit=None, patterns=None): if not paths: raise ValueError('No preload inputs found') result = {} + listed = set(paths) pending = list(paths) while pending: path = pending.pop() @@ -58,7 +59,13 @@ def source_files(group, root=ROOT, commit=None, patterns=None): continue if path.startswith('../') or path.startswith('/'): raise ValueError('Preload include escapes repository') - content = read(path).replace(b'\r\n', b'\n') + try: + content = read(path).replace(b'\r\n', b'\n') + except (OSError, subprocess.CalledProcessError): + # a header the build generates (gosys/gosys_blob.h, included behind __has_include) + if path in listed: + raise + continue result[path] = sha(content) for include in re.findall(rb'^\s*#\s*include\s*"([^"\n]+)"', content, re.M): pending.append(posixpath.normpath(posixpath.join(posixpath.dirname(path), include.decode()))) @@ -126,13 +133,15 @@ def artifact_bytes(origin, cache): if len(names) != 1: raise ValueError('Expected one original APK') with ZipFile(io.BytesIO(archive.read(names[0]))) as apk: - return {name: apk.read('assets/linuxfs/' + name) for name in FILES} + present = set(apk.namelist()) + return {name: apk.read('assets/linuxfs/' + name) for name in FILES if 'assets/linuxfs/' + name in present} def validate_lock(data): if data.get('version') != 1 or set(data['groups']) != set(INPUTS): raise ValueError('Unsupported preload snapshot') - if set(data['outputs']) != FILES: + # A lock made before a recipe gained an output lacks it; that group is rebuilt (restore) + if not set(data['outputs']) <= FILES: raise ValueError('Unexpected preload output set') for digest in [data['archive_sha256'], *data['outputs'].values()]: if len(digest) != 64 or any(c not in '0123456789abcdef' for c in digest): @@ -160,7 +169,7 @@ def producer_sources(group, commit, root=ROOT): def prove_origin(data, cache, root=ROOT): content = artifact_bytes(data['origin'], cache) - if {name: sha(value) for name, value in content.items()} != data['outputs']: + if {name: sha(value) for name, value in content.items() if name in data['outputs']} != data['outputs']: raise ValueError('Snapshot binaries differ from the original main APK') commit = data['origin']['commit'] run('git', 'fetch', '--no-tags', 'origin', commit, root=root) @@ -183,7 +192,7 @@ def unpack(content, data): total = 0 with tarfile.open(fileobj=io.BytesIO(content), mode='r:gz') as archive: for entry in archive: - if entry.name not in FILES or entry.name in result or not entry.isfile() or entry.size > 32 * 1024 * 1024: + if entry.name not in data['outputs'] or entry.name in result or not entry.isfile() or entry.size > 32 * 1024 * 1024: raise ValueError('Unsafe or unexpected preload archive entry') total += entry.size if total > 64 * 1024 * 1024: @@ -192,7 +201,7 @@ def unpack(content, data): if sha(value) != data['outputs'][entry.name]: raise ValueError('Preload binary checksum mismatch') result[entry.name] = value - if set(result) != FILES: + if set(result) != set(data['outputs']): raise ValueError('Incomplete preload archive') return result @@ -203,6 +212,8 @@ def restore(root=ROOT): matching = {group: source_files(group, root) == data['groups'][group] for group in INPUTS} matching['arm64'] &= recipe(root) == data['arm64_recipe'] matching['x86'] &= x86_recipe(root) == data.get('x86_recipe') + for group in matching: + matching[group] &= all(name in data['outputs'] for name in OUTPUTS[group]) if os.environ.get('FORCE_PRELOAD_REBUILD') == 'true': matching = dict.fromkeys(INPUTS, False) if any(matching.values()): diff --git a/tools/components/recipes.json b/tools/components/recipes.json index b5789aa7ea..92fcc548a4 100644 --- a/tools/components/recipes.json +++ b/tools/components/recipes.json @@ -164,7 +164,8 @@ "tools/components/build-arm64-preloads.sh", "tools/linuxfs/preload", "app/src/main/cpp/fakeinput_steam.cpp", - "tools/components/recipes/arm64-preloads.sh" + "tools/components/recipes/arm64-preloads.sh", + "tools/nativeguest" ], "outputs": [ "app/src/main/assets/linuxfs/libfakeinput.so", @@ -172,7 +173,8 @@ "app/src/main/assets/linuxfs/libblfastpath.so", "app/src/main/assets/linuxfs/libblaudit.so", "app/src/main/assets/linuxfs/libssbs.so", - "app/src/main/assets/linuxfs/usr/local/bin/droiddeck-clipboard" + "app/src/main/assets/linuxfs/usr/local/bin/droiddeck-clipboard", + "app/src/main/assets/linuxfs/libddnatv-hook.so" ], "packages": [ "gcc-aarch64-linux-gnu", @@ -187,7 +189,8 @@ "tools/quest/kgsl_compat.h", "tools/proot/fastpath", "tools/linuxfs/ssbs", - "tools/linuxfs/clipboard" + "tools/linuxfs/clipboard", + "tools/nativeguest" ], "snapshot_outputs": [ "libfakeinput.so", @@ -195,7 +198,8 @@ "libblfastpath.so", "libblaudit.so", "libssbs.so", - "usr/local/bin/droiddeck-clipboard" + "usr/local/bin/droiddeck-clipboard", + "libddnatv-hook.so" ] }, "preload-snapshot": { diff --git a/tools/droiddeckctl b/tools/droiddeckctl index 8d08288d01..7c4116384b 100755 --- a/tools/droiddeckctl +++ b/tools/droiddeckctl @@ -101,6 +101,10 @@ def encode_request(request: dict) -> str: def receiver_call(adb: str, serial: str, method: str, arg: str | None, request: dict | None, timeout: float) -> dict: """The agent bridge through a broadcast, for shells without the `content` command.""" + # Its receiver is registered by the running app; a broadcast does not start the app the way `content call` does + running = run_adb(adb, serial, "shell", f"pidof {PACKAGE}", timeout=30) + if not running.stdout.decode(errors="replace").strip(): + raise ControlError(f"{PACKAGE} is not running and this shell has no `content` command to start it; open DroidDeck once", "APP_NOT_RUNNING") call_id = uuid.uuid4().hex[:16] remote = f"/sdcard/Android/data/{PACKAGE}/files/agent-calls/{call_id}.json" command = ["shell", "am", "broadcast", "-a", f"{PACKAGE}.AGENT_CALL", "-p", PACKAGE, @@ -132,7 +136,8 @@ def provider_call(adb: str, serial: str, method: str, arg: str | None = None, re command.extend(["--extra", f"request:s:{encoded}"]) result = run_adb(adb, serial, *command, timeout=timeout) output = (result.stdout + result.stderr).decode(errors="replace") - if "content: inaccessible or not found" in output: + # Only the shell's own answer for a missing `content` binary, never a command's output that quotes it + if result.returncode == 127 and "Result:" not in output and "content: inaccessible or not found" in result.stderr.decode(errors="replace") + output: return receiver_call(adb, serial, method, arg, request, timeout) response = _content_result(output) if result.returncode != 0 and response.get("ok") is not False: @@ -781,6 +786,28 @@ def execute(args: argparse.Namespace, adb: str, serial: str) -> dict: f"{(response.get('stderr') or {}).get('text', '')[-400:]}", "GUEST_EXIT") return add_serial(response, serial) + if command == "native": + argv = args.argv[1:] if args.argv[:1] == ["--"] else args.argv + if not argv: + raise ControlError("native needs a command after --", "INVALID_ARGUMENTS") + if not 0 < args.timeout <= 600: + raise ControlError("--timeout must be more than 0 and at most 600 seconds", "INVALID_ARGUMENTS") + env = {} + for item in args.env or []: + key, sep, value = item.partition("=") + if not sep: + raise ControlError(f"--env takes KEY=VALUE, not {item}", "INVALID_ARGUMENTS") + env[key] = value + request = {"argv": argv, "timeout": args.timeout, "env": env} + if args.cwd: + request["cwd"] = args.cwd + if args.stdin is not None: + request["stdin"] = args.stdin + if args.preload: + request["preload"] = args.preload + response = checked(provider_call(adb, serial, "native", request=request, timeout=args.timeout + 40), "NATIVE_FAILED") + return add_serial(response, serial) + if command == "cdp": if args.cdp_op == "targets": request = {"op": "targets"} @@ -1015,6 +1042,14 @@ def build_parser() -> argparse.ArgumentParser: guest.add_argument("--check", action="store_true", help="fail unless the command exits 0") guest.add_argument("argv", nargs=argparse.REMAINDER) + native = commands.add_parser("native", help="run a command in the rootfs without proot (debugging tools)") + native.add_argument("--timeout", type=float, default=30) + native.add_argument("--env", action="append", metavar="KEY=VALUE") + native.add_argument("--cwd") + native.add_argument("--stdin") + native.add_argument("--preload", action="append", help="guest path of another library to preload before the hook") + native.add_argument("argv", nargs=argparse.REMAINDER) + cdp = commands.add_parser("cdp", help="the Steam client's DevTools (debugging tools)") cdp_ops = cdp.add_subparsers(dest="cdp_op", required=True) cdp_ops.add_parser("targets", help="list DevTools targets") diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env index 16b12bfe3d..175b8d8d1c 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env @@ -1270,6 +1270,10 @@ def main(): if scripts and install_scripts_done(scripts, env): print("droiddeck-game-env: install scripts already run in this prefix; evaluator skipped", file=sys.stderr) return 0 + if scripts and not env.get("DBUS_SESSION_BUS_ADDRESS"): + # libdbus would autolaunch a session bus under the evaluator's reaper, which then waits for it + # forever: Steam keeps seeing the app as running and cannot move or update it + env["DBUS_SESSION_BUS_ADDRESS"] = "disabled:" try: digest = scripts_digest(scripts) if scripts else None except OSError: diff --git a/tools/linuxfs/preload/tracer.c b/tools/linuxfs/preload/tracer.c index 572cf13e09..7f5ac8e8cd 100644 --- a/tools/linuxfs/preload/tracer.c +++ b/tools/linuxfs/preload/tracer.c @@ -81,7 +81,10 @@ __attribute__((visibility("hidden"))) int bl_status_without_tracer(const char *p long pid; if ((flags & O_ACCMODE) != O_RDONLY || (pid = status_pid(path)) < 0 || is_web_helper(caller) || bl_from_ntdll(caller)) return -1; - int in = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC); + // through the next open(): the native userland's hook gives the app's ids as root's there + static int (*next_open)(const char *, int, ...) = NULL; + if (!next_open) next_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open"); + int in = next_open ? next_open(path, O_RDONLY | O_CLOEXEC) : (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC); if (in < 0) goto done; for (;;) { ssize_t n = read(in, text + len, sizeof(text) - 1 - len); diff --git a/tools/nativeguest/LICENSE b/tools/nativeguest/LICENSE new file mode 100644 index 0000000000..d645695673 --- /dev/null +++ b/tools/nativeguest/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/tools/nativeguest/README.md b/tools/nativeguest/README.md new file mode 100644 index 0000000000..19571de96c --- /dev/null +++ b/tools/nativeguest/README.md @@ -0,0 +1,34 @@ +# Native userland (no proot) + +`libddnatv-hook.so` is an `LD_PRELOAD` library that lets the glibc rootfs run directly on Android: +it maps the guest's absolute paths into the rootfs (plus the mounts proot used to provide, from +`DDNATV_BINDS`), starts every program through the rootfs's `ld.so`, and answers the system calls +Android's seccomp policy kills a process for (`SIGSYS` handler). There is no tracer. The app side +is `runtime/NativeGuest.kt`; the setting is Performance > "Run without proot (experimental)". + +The hook is derived from the Apache-2.0 `libcortex-hook` of +[ssfuisu/cortex](https://github.com/ssfuisu/cortex) (see `LICENSE`), renamed (`cortex` -> `ddnatv`, +same length: the code compares environment names by length) and extended with: + +- `DDNATV_BINDS` mounts, `DDNATV_PRELOAD` (what `/etc/ld.so.preload` does for proot), and mapping of + guest paths inside `LD_PRELOAD`; +- POSIX `shm_open`/`shm_unlink` through `/dev/shm`; +- starting Wine without its static preloader, which opens files with raw system calls. + +Build: `tools/nativeguest/build.sh out.so` with an aarch64 glibc cross compiler. + +## Testing under Android's seccomp filter + +Some devices (eng builds, `setenforce 0` ROMs) run apps with no seccomp filter, so a hook that +passes there can still die with SIGSYS on a stock phone (`GUEST_EXIT 159`). `seccomp-sim.c` applies +a filter that answers the calls Android blocks with SIGSYS, then runs the program: + +``` +aarch64-linux-gnu-gcc -static -O2 -o seccomp-sim tools/nativeguest/seccomp-sim.c +# copy it into the rootfs, then name it for every native launch: +echo /usr/local/bin/seccomp-sim > /etc/droiddeck/seccomp-sim +``` + +`grep Seccomp: /proc//status` should then show `2` for the session's processes. Empty the file +to turn it off. `seccomp-sim -a NR,NR PROGRAM` leaves the listed calls allowed, to find which one +kills a program. diff --git a/tools/nativeguest/app-seccomp-dump.c b/tools/nativeguest/app-seccomp-dump.c new file mode 100644 index 0000000000..1ec09ea256 --- /dev/null +++ b/tools/nativeguest/app-seccomp-dump.c @@ -0,0 +1,50 @@ +// Writes the seccomp filter Android's zygote gives an app (from the device's libandroid_runtime.so) +// to a file, as the raw sock_filter array, for seccomp-sim -f on devices whose zygote skips it +// (a permissive SELinux). A bionic program, built with the NDK and run from adb shell: +// aarch64-linux-android33-clang -O2 -rdynamic -o app-seccomp-dump app-seccomp-dump.c -ldl +// app-seccomp-dump OUT +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const char *g_out; +static int g_dumped; + +// the runtime's own prctl() call lands here first +__attribute__((visibility("default"))) int prctl(int option, ...) { + va_list ap; + va_start(ap, option); + unsigned long a2 = va_arg(ap, unsigned long), a3 = va_arg(ap, unsigned long); + unsigned long a4 = va_arg(ap, unsigned long), a5 = va_arg(ap, unsigned long); + va_end(ap); + if (option == PR_SET_SECCOMP && a2 == SECCOMP_MODE_FILTER && g_out) { + const struct sock_fprog *prog = (const struct sock_fprog *)a3; + FILE *f = fopen(g_out, "wb"); + if (f) { + fwrite(prog->filter, sizeof(prog->filter[0]), prog->len, f); + fclose(f); + fprintf(stderr, "app-seccomp-dump: %u instructions to %s\n", prog->len, g_out); + g_dumped = 1; + } + return 0; // not installed in this process + } + return (int)syscall(SYS_prctl, option, a2, a3, a4, a5); +} + +int main(int argc, char **argv) { + if (argc != 2) { fprintf(stderr, "usage: %s OUT\n", argv[0]); return 2; } + g_out = argv[1]; + void *rt = dlopen("libandroid_runtime.so", RTLD_NOW); + if (!rt) { fprintf(stderr, "%s\n", dlerror()); return 2; } + int (*set_app_filter)(void) = (int (*)(void))dlsym(rt, "_Z22set_app_seccomp_filterv"); + if (!set_app_filter) { fprintf(stderr, "no set_app_seccomp_filter\n"); return 2; } + set_app_filter(); + return g_dumped ? 0 : 1; +} diff --git a/tools/nativeguest/build-gosys.sh b/tools/nativeguest/build-gosys.sh new file mode 100755 index 0000000000..ce05326c1e --- /dev/null +++ b/tools/nativeguest/build-gosys.sh @@ -0,0 +1,82 @@ +#!/usr/bin/env bash +# Builds the code the native userland adds to static Go programs (gosys/gosys.c) into the C header +# the hook includes (gosys/gosys_blob.h, not kept in the repository; build.sh makes it each time). +# +# The code is AArch64 whatever the machine this runs on: it is compiled freestanding with an +# aarch64 cross compiler (gcc-aarch64-linux-gnu: the one that builds the hook; or clang with lld). +# +# usage: tools/nativeguest/build-gosys.sh [output header] +set -euo pipefail + +here=$(cd "$(dirname "$0")" && pwd) +src=$here/gosys +out=${1:-$src/gosys_blob.h} + +if command -v aarch64-linux-gnu-gcc >/dev/null 2>&1; then + CC=(aarch64-linux-gnu-gcc) + LD=(aarch64-linux-gnu-ld) + OBJCOPY=(aarch64-linux-gnu-objcopy) + NM=(aarch64-linux-gnu-nm) + OBJDUMP=(aarch64-linux-gnu-objdump) +elif command -v clang >/dev/null 2>&1 && command -v ld.lld >/dev/null 2>&1; then + CC=(clang --target=aarch64-linux-gnu) + LD=(ld.lld) + OBJCOPY=(llvm-objcopy) + NM=(llvm-nm) + OBJDUMP=(llvm-objdump) +else + echo "build-gosys.sh: needs aarch64-linux-gnu-gcc (apt install gcc-aarch64-linux-gnu) or clang with lld" >&2 + exit 1 +fi + +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT + +"${CC[@]}" -std=gnu11 -O2 -Wall -Wextra -Werror \ + -ffreestanding -fno-builtin -nostdlib -fPIE -fvisibility=hidden -fno-stack-protector \ + -fno-asynchronous-unwind-tables -fno-unwind-tables -fno-tree-loop-distribute-patterns \ + -mcmodel=tiny -mgeneral-regs-only -mno-outline-atomics -ffixed-x18 -ffixed-x28 \ + -c "$src/gosys.c" -o "$tmp/gosys.o" +"${LD[@]}" -T "$src/gosys.ld" -nostdlib -o "$tmp/gosys.elf" "$tmp/gosys.o" +"${OBJCOPY[@]}" -O binary "$tmp/gosys.elf" "$tmp/gosys.bin" + +# It must stand on its own: nothing undefined, no writable data, and nothing that depends on the +# address it is loaded at (adrp assumes the page offset of the link address; adr does not) +if [ -n "$("${NM[@]}" -u "$tmp/gosys.elf")" ]; then + echo "build-gosys.sh: gosys.c needs symbols it does not define:" >&2 + "${NM[@]}" -u "$tmp/gosys.elf" >&2 + exit 1 +fi +if "${OBJDUMP[@]}" -d "$tmp/gosys.elf" | grep -q -w adrp; then + echo "build-gosys.sh: gosys.c uses adrp, which is not position-independent at any address" >&2 + exit 1 +fi +sections=$("${OBJDUMP[@]}" -h "$tmp/gosys.elf" | awk '$1 ~ /^[0-9]+$/ {print $2}' | tr '\n' ' ') +for s in $sections; do + case $s in + .hdr|.thunks|.text|.rodata|.trailer|.symtab|.strtab|.shstrtab) ;; + *) echo "build-gosys.sh: unexpected section $s in gosys.elf (sections: $sections)" >&2; exit 1 ;; + esac +done + +size=$(stat -c %s "$tmp/gosys.bin") +entry=$("${NM[@]}" "$tmp/gosys.elf" | awk '$3 == "cxgo_entry" {print $1}') +thunks=$("${NM[@]}" "$tmp/gosys.elf" | awk '$3 == "g_thunks" {print $1}') +if [ -z "$entry" ] || [ -z "$thunks" ]; then + echo "build-gosys.sh: cxgo_entry or g_thunks not found" >&2 + exit 1 +fi +[ "$(head -c 8 "$tmp/gosys.bin")" = "CXGOSYS1" ] || { echo "build-gosys.sh: the header is not first" >&2; exit 1; } +[ "$(tail -c 64 "$tmp/gosys.bin" | head -c 8)" = "CXGOEND1" ] || { echo "build-gosys.sh: the trailer is not last" >&2; exit 1; } + +{ + echo "// Generated by tools/nativeguest/build-gosys.sh from gosys/gosys.c. Do not edit." + echo "#define GOSYS_BLOB_SIZE $size" + echo "#define GOSYS_ENTRY_OFF 0x$entry" + echo "#define GOSYS_THUNKS_OFF 0x$thunks" + echo "static const unsigned char gosys_blob[GOSYS_BLOB_SIZE] = {" + od -An -v -tx1 "$tmp/gosys.bin" | sed -e 's/^ *//' -e 's/ /, 0x/g' -e 's/^/0x/' -e 's/$/,/' + echo "};" +} > "$out.tmp" +mv "$out.tmp" "$out" +echo "build-gosys.sh: $out ($size bytes, entry 0x$entry)" diff --git a/tools/nativeguest/build.sh b/tools/nativeguest/build.sh new file mode 100755 index 0000000000..46002f248c --- /dev/null +++ b/tools/nativeguest/build.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# Builds libddnatv-hook.so, the library that lets the glibc rootfs run on Android without proot. +# usage: tools/nativeguest/build.sh [compiler, default aarch64-linux-gnu-gcc] +set -euo pipefail +here=$(cd "$(dirname "$0")" && pwd) +out=${1:?output path} +cc=${2:-aarch64-linux-gnu-gcc} +gen=$(mktemp -d) +trap 'rm -rf "$gen"' EXIT +mkdir -p "$gen/gosys" +bash "$here/build-gosys.sh" "$gen/gosys/gosys_blob.h" +"$cc" -std=gnu11 -shared -fPIC -O2 -fno-delete-null-pointer-checks -Wall -Wno-nonnull-compare \ + -Wno-format-truncation -Wno-stringop-truncation -Wl,-z,max-page-size=16384 -I "$gen" \ + -o "$out" "$here/ddnatv-hook.c" -ldl +"${cc%gcc}strip" --strip-unneeded "$out" diff --git a/tools/nativeguest/ddnatv-hook.c b/tools/nativeguest/ddnatv-hook.c new file mode 100644 index 0000000000..954991c261 --- /dev/null +++ b/tools/nativeguest/ddnatv-hook.c @@ -0,0 +1,87 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef RENAME_EXCHANGE +#define RENAME_EXCHANGE (1 << 1) +#endif + +// The code that is added to static Go programs (gosys/gosys.c), built by scripts/build-gosys.sh. A +// build without it works as before, only without Go support. It is AArch64 code: the 32-bit hook +// has none (a 32-bit phone cannot run 64-bit programs). +#if defined(__has_include) && defined(__LP64__) +#if __has_include("gosys/gosys_blob.h") +#include "gosys/gosys_abi.h" +#include "gosys/gosys_blob.h" +#define DDNATV_HAVE_GOSYS 1 +#endif +#endif + +// The hook is one translation unit split into parts by topic, in this order (later parts use +// the static functions of earlier ones). +#include "hook/syscalls.c" +#include "hook/paths.c" +#include "hook/hardlinks.c" +#include "hook/signals.c" +#include "hook/setup.c" +#include "hook/files.c" +#include "hook/fileops.c" +#include "hook/identity.c" +#include "hook/privileged.c" +#include "hook/env.c" +#include "hook/exec_probe.c" +#include "hook/static_programs.c" +#include "hook/go_programs.c" +#include "hook/exec.c" +#include "hook/net.c" +#include "hook/sockets.c" +#include "hook/sysv_shm.c" +#include "hook/posix_shm.c" diff --git a/tools/nativeguest/gosys/gosys.c b/tools/nativeguest/gosys/gosys.c new file mode 100644 index 0000000000..fb8230ce4a --- /dev/null +++ b/tools/nativeguest/gosys/gosys.c @@ -0,0 +1,510 @@ +// The code The native userland adds to a static Go program so that it sees the rootfs (see gosys_abi.h). +// +// Built freestanding (scripts/build-gosys.sh) as a flat, position-independent binary with no +// writable data, no libc and no relocations: the hook copies it into the program's file. It is +// entered from the one place where Go's system calls are made, in place of the "svc #0" of +// internal/runtime/syscall.Syscall6, with the registers of that call: arguments in x0 to x5, +// the number in x8 (passed again as the eighth argument). It returns what the kernel would, and it +// may use every register but x28 (the current goroutine), x29, x30 and sp, which it keeps. +// +// It does for Go what libddnatv-hook.so does for C programs: maps absolute Ubuntu paths into the +// rootfs (the same rules as hook/paths.c), shortens the Unix socket paths that do not fit, shows +// the program the paths it expects from getcwd and /proc/self/exe, and starts Ubuntu programs +// through the rootfs's dynamic linker and env, which then go through the hook. + +#include "gosys_abi.h" + +typedef int64_t i64; +typedef uint64_t u64; +typedef uint32_t u32; +typedef uint16_t u16; + +#define AT_FDCWD (-100) +#define SLOT_PATH_A 0u +#define SLOT_PATH_B 4096u +#define SLOT_ARGV 8192u +#define SLOT_ENV 16384u +#define SLOT_PATH_CAP 4096u +#define ARGV_CAP 1000u // entries of the argv array that starts a program +#define ENV_CAP 2000u + +// The numbers of AArch64 Linux (the same for every kernel) +enum { + NR_SETXATTR = 5, NR_LSETXATTR = 6, NR_GETXATTR = 8, NR_LGETXATTR = 9, NR_LISTXATTR = 11, + NR_LLISTXATTR = 12, NR_REMOVEXATTR = 14, NR_LREMOVEXATTR = 15, NR_GETCWD = 17, + NR_INOTIFY_ADD_WATCH = 27, NR_MKNODAT = 33, NR_MKDIRAT = 34, NR_UNLINKAT = 35, NR_SYMLINKAT = 36, + NR_LINKAT = 37, NR_RENAMEAT = 38, NR_STATFS = 43, NR_TRUNCATE = 45, NR_FACCESSAT = 48, + NR_CHDIR = 49, NR_CHROOT = 51, NR_FCHMODAT = 53, NR_FCHOWNAT = 54, NR_OPENAT = 56, + NR_READLINKAT = 78, NR_NEWFSTATAT = 79, NR_UTIMENSAT = 88, NR_BIND = 200, NR_CONNECT = 203, + NR_EXECVE = 221, NR_NAME_TO_HANDLE_AT = 264, NR_RENAMEAT2 = 276, NR_STATX = 291, + NR_OPENAT2 = 437, NR_FACCESSAT2 = 439, NR_FCHMODAT2 = 452, +}; + +__attribute__((section(".hdr"), used)) +static const volatile struct gosys_hdr g_hdr = { .magic = "CXGOSYS1", .version = GOSYS_VERSION }; + +// Filled by the patcher; the instructions that connect the call site to cxgo_entry +__attribute__((section(".thunks"), used)) +static const volatile u32 g_thunks[GOSYS_THUNK_BYTES / 4]; + +__attribute__((section(".trailer"), used)) +static const volatile struct gosys_trailer g_trailer = { .magic = "CXGOEND1", .version = GOSYS_VERSION }; + +static inline const struct gosys_hdr *hdr(void) { + const struct gosys_hdr *h = (const struct gosys_hdr *)&g_hdr; + __asm__("" : "+r"(h)); // the compiler must not assume it knows the contents + return h; +} + +static inline i64 sc(i64 nr, i64 a0, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5) { + register i64 x8 __asm__("x8") = nr; + register i64 x0 __asm__("x0") = a0; + register i64 x1 __asm__("x1") = a1; + register i64 x2 __asm__("x2") = a2; + register i64 x3 __asm__("x3") = a3; + register i64 x4 __asm__("x4") = a4; + register i64 x5 __asm__("x5") = a5; + __asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5) : "memory", "cc"); + return x0; +} + +static u64 slen(const char *s) { + u64 n = 0; + while (s[n]) n++; + return n; +} + +static void mcopy(char *d, const char *s, u64 n) { + for (u64 i = 0; i < n; i++) d[i] = s[i]; +} + +// Moves n bytes down (d < s) +static void mmove_down(char *d, const char *s, u64 n) { + for (u64 i = 0; i < n; i++) d[i] = s[i]; +} + +static int pre(const char *path, const char *lit, u64 n) { + for (u64 i = 0; i < n; i++) + if (path[i] != lit[i]) return 0; + return path[n] == '/' || path[n] == '\0'; +} + +// Whether path is the rootfs or in it +static int in_root(const struct gosys_hdr *h, const char *p) { + u64 n = h->root_len; + if (n == 0) return 0; + for (u64 i = 0; i < n; i++) + if (p[i] != h->root[i]) return 0; + return p[n] == '/' || p[n] == '\0'; +} + +// Android's own mounts and files, which must never be redirected into the rootfs (hook/paths.c) +static int is_host_path(const char *path) { + switch (path[1]) { + case 'a': return pre(path, "/apex", 5); + case 'c': return pre(path, "/config", 7); + case 'd': return pre(path, "/dev", 4) || pre(path, "/d", 2) || pre(path, "/data", 5); + case 'l': return pre(path, "/linkerconfig", 13); + case 'o': return pre(path, "/odm", 4) || pre(path, "/oem", 4); + case 'p': return pre(path, "/proc", 5) || pre(path, "/product", 8); + case 's': return pre(path, "/sbin/su", 8) || pre(path, "/su", 3) || pre(path, "/sys", 4) || + pre(path, "/system", 7) || pre(path, "/system_ext", 11) || + pre(path, "/sdcard", 7) || pre(path, "/storage", 8); + case 'v': return pre(path, "/vendor", 7); + default: return 0; + } +} + +// An absolute Ubuntu path mapped into the rootfs: `path` itself when it needs no change, else buf +// (hook/paths.c rewrite_path_in_root). A result that would not fit is the path unchanged: the +// kernel then answers as it would for a path that long. +static const char *rw_path(const struct gosys_hdr *h, const char *path, char *buf, u64 cap) { + if (path[0] != '/' || h->root_len == 0) return path; + while (path[0] == '/' && path[1] == '/') path++; + while (path[0] == '/' && path[1] == '.' && (path[2] == '/' || path[2] == '\0')) { + if (path[2] == '/') { + path += 2; + } else { + path = "/"; + break; + } + } + u64 rl = h->root_len; + if (path[1] == '\0') { + mcopy(buf, h->root, rl); + buf[rl] = '\0'; + return buf; + } + if (in_root(h, path)) return path; + if (pre(path, "/dev/shm", 8)) { + u64 rest = slen(path + 8); + if (rl + 8 + rest + 1 > cap) return path; + mcopy(buf, h->root, rl); + mcopy(buf + rl, "/tmp/shm", 8); + mcopy(buf + rl + 8, path + 8, rest + 1); + return buf; + } + if (is_host_path(path)) return path; + u64 pl = slen(path); + if (rl + pl + 1 > cap) return path; + mcopy(buf, h->root, rl); + mcopy(buf + rl, path, pl + 1); + return buf; +} + +// Whether the rootfs is where the code was told it is. In root mode the program runs in a chroot of +// that very rootfs, where the real paths are the Ubuntu ones and the rootfs's own path does not +// exist: then nothing is mapped and the program works as any other. Looked at once for each process +// (the scratch memory starts as zero, and a forked child keeps what its parent found). +static int rewriting(const struct gosys_hdr *h) { + volatile u32 *mode = (volatile u32 *)((char *)h + h->scratch_rel + 4); + u32 m = *mode; + if (m == 0) { + m = sc(NR_FACCESSAT, AT_FDCWD, (i64)h->root, 0, 0, 0, 0) == 0 ? 1u : 2u; + *mode = m; + } + return m == 1; +} + +#define NO_SLOT 0xffffffffu +#define NR_SCHED_YIELD 124 + +// Who is making the call: the goroutine (x28 holds it, and the code is built not to touch x28). A +// goroutine makes one call at a time, and the vfork child that a goroutine makes runs with the same +// x28 as the goroutine does. (TPIDR_EL0 does not tell the threads of a Go program apart: its runtime +// leaves it unset without cgo.) +static inline u64 thread_tag(void) { + u64 t; + __asm__ volatile("mov %0, x28" : "=r"(t)); + return t; +} + +// How many execve calls have not returned (state area, offset 8). One that works never does: it is +// made by a vfork child, and the slot its path was in stays marked as the goroutine's, which goes on +// when the child is gone (the same x28, a call of its own). Without this, goroutines that each +// started a program held a slot each for good, and after the sixty-fourth all the others waited. +static inline u32 *execs_on_their_way(const struct gosys_hdr *h) { + return (u32 *)((char *)h + h->scratch_rel + 8); +} + +static void count_down(u32 *count) { + u32 n = __atomic_load_n(count, __ATOMIC_RELAXED); + while (n != 0 && !__atomic_compare_exchange_n(count, &n, n - 1, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) { + } +} + +// Gives back what this goroutine's vfork child left (see above). Every call looks at one counter +// for it; the list of slots is only looked through while an execve is on its way. +static void reclaim_left_slots(const struct gosys_hdr *h) { + u32 *count = execs_on_their_way(h); + if (__atomic_load_n(count, __ATOMIC_RELAXED) == 0) return; + u64 *owner = (u64 *)((char *)h + h->scratch_rel + 64); + u64 me = thread_tag() | 1; + for (u32 i = 0; i < GOSYS_SLOTS; i++) { + if (__atomic_load_n(&owner[i], __ATOMIC_RELAXED) != me) continue; + __atomic_store_n(&owner[i], 0, __ATOMIC_RELEASE); + count_down(count); + } +} + +// A slot for the call this goroutine is making; *held is its number, to give back when the call is +// done. Each slot has a word of its own that holds who has it (0: nobody), so that taking one is one +// compare-and-swap, and no other goroutine can ever mistake it for its own. +static char *take_slot(const struct gosys_hdr *h, u32 *held) { + char *base = (char *)h + h->scratch_rel; + u64 *owner = (u64 *)(base + 64); + u64 me = thread_tag() | 1; // never 0 + u32 mine = NO_SLOT; + // A slot of this goroutine that is still taken now (cxgo_entry gives back those of its vfork + // children) is its own again + for (u32 i = 0; i < GOSYS_SLOTS; i++) { + if (__atomic_load_n(&owner[i], __ATOMIC_RELAXED) != me) continue; + if (mine == NO_SLOT) + mine = i; + else + __atomic_store_n(&owner[i], 0, __ATOMIC_RELEASE); + } + while (mine == NO_SLOT) { + for (u32 i = 0; i < GOSYS_SLOTS && mine == NO_SLOT; i++) { + u64 none = 0; + if (__atomic_load_n(&owner[i], __ATOMIC_RELAXED) == 0 && + __atomic_compare_exchange_n(&owner[i], &none, me, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) + mine = i; + } + if (mine == NO_SLOT) sc(NR_SCHED_YIELD, 0, 0, 0, 0, 0, 0); // all taken: the others finish soon + } + *held = mine; + return base + GOSYS_STATE_SIZE + (u64)mine * GOSYS_SLOT_SIZE; +} + +static void put_slot(const struct gosys_hdr *h, u32 held) { + if (held == NO_SLOT) return; + char *base = (char *)h + h->scratch_rel; + __atomic_store_n((u64 *)(base + 64) + held, 0, __ATOMIC_RELEASE); +} + +// A path as the program expects to see it, from one of the kernel's: the rootfs prefix removed. +// Returns the new length of the n bytes at buf (a path, not NUL-terminated unless `nul`). +static i64 unmap_prefix(const struct gosys_hdr *h, char *buf, i64 n, int nul) { + u64 rl = h->root_len; + if (rl == 0 || n <= 0 || (u64)n < rl + (nul ? 1 : 0)) return n; + for (u64 i = 0; i < rl; i++) + if (buf[i] != h->root[i]) return n; + u64 end = (u64)n - (nul ? 1 : 0); // the path's own length + if (end == rl) { // exactly the rootfs + buf[0] = '/'; + if (nul) { + buf[1] = '\0'; + return 2; + } + return 1; + } + if (buf[rl] != '/') return n; + mmove_down(buf, buf + rl, (u64)n - rl); + return n - (i64)rl; +} + +// ---- Unix sockets ------------------------------------------------------------------------ + +static void put_hex16(char *out, u64 v) { + static const char digits[] = "0123456789abcdef"; + for (int i = 15; i >= 0; i--) { + out[i] = digits[v & 15]; + v >>= 4; + } +} + +// bind() and connect() of a Unix socket with a path: mapped like hook/setup.c does, and hashed +// into /tmp when it does not fit the 108 bytes of sun_path (the same name, so a Go server and a +// program using the C library find each other) +static i64 with_sockaddr(const struct gosys_hdr *h, u32 *held, i64 nr, i64 a0, i64 a1, i64 a2) { + const unsigned char *sa = (const unsigned char *)a1; + if (!sa || a2 < 4 || a2 > 128 || sa[0] != 1 /* AF_UNIX */ || sa[1] != 0 || sa[2] != '/') + return sc(nr, a0, a1, a2, 0, 0, 0); + char *slot = take_slot(h, held); + char *orig = slot + SLOT_PATH_A; + u64 max = (u64)a2 - 2; + if (max > 108) max = 108; + mcopy(orig, (const char *)sa + 2, max); + orig[max] = '\0'; + const char *rw = rw_path(h, orig, slot + SLOT_PATH_B, SLOT_PATH_CAP); + u64 rl = slen(rw); + char *out = slot + SLOT_ARGV; + out[0] = 1; + out[1] = 0; + if (rl < 108) { + mcopy(out + 2, rw, rl + 1); + return sc(nr, a0, (i64)out, (i64)(2 + rl + 1), 0, 0, 0); + } + u64 hash = 14695981039346656037ULL; + for (const char *p = rw; *p; p++) { + hash ^= (unsigned char)*p; + hash *= 1099511628211ULL; + } + u64 n = h->root_len; + if (n + 5 + 11 + 16 + 1 > 108) return sc(nr, a0, a1, a2, 0, 0, 0); + mcopy(out + 2, h->root, n); + mcopy(out + 2 + n, "/tmp/.ctx_sock_", 15); + put_hex16(out + 2 + n + 15, hash); + out[2 + n + 15 + 16] = '\0'; + return sc(nr, a0, (i64)out, (i64)(2 + n + 15 + 16 + 1), 0, 0, 0); +} + +// ---- Starting programs ------------------------------------------------------------------- + +static int starts_with(const char *s, const char *lit) { + for (u64 i = 0; lit[i]; i++) + if (s[i] != lit[i]) return 0; + return 1; +} + +// A program of Android's gets the environment it expects, as from hook/env.c clean_env_for_system(): +// without the hook and the rootfs's libraries, and with Android's directories in its PATH +static i64 exec_host(const struct gosys_hdr *h, char *slot, const char *p, i64 a1, char *const *envp) { + (void)h; + static const char android_path[] = "PATH=/system/bin:/system/xbin:/sbin:/vendor/bin"; + u32 envc = 0; + while (envp && envp[envc]) envc++; + if (envc + 2 > ENV_CAP) return sc(NR_EXECVE, (i64)p, a1, (i64)envp, 0, 0, 0); + char **ne = (char **)(slot + SLOT_ENV); + char *path_str = slot + SLOT_PATH_B; + u32 n = 0; + int has_path = 0; + for (u32 i = 0; i < envc; i++) { + char *e = envp[i]; + if (starts_with(e, "LD_PRELOAD=") || starts_with(e, "LD_LIBRARY_PATH=") || starts_with(e, "GLIBC_TUNABLES=")) + continue; + if (starts_with(e, "PATH=")) { + u64 len = slen(e); + has_path = 1; + if (len + sizeof(android_path) + 1 < SLOT_PATH_CAP) { + mcopy(path_str, android_path, sizeof(android_path) - 1); + path_str[sizeof(android_path) - 1] = ':'; + mcopy(path_str + sizeof(android_path), e + 5, len - 5 + 1); + ne[n++] = path_str; + continue; + } + } + ne[n++] = e; + } + if (!has_path) ne[n++] = (char *)android_path; + ne[n] = 0; + return sc(NR_EXECVE, (i64)p, a1, (i64)ne, 0, 0, 0); +} + +// execve(): a program of the rootfs is started as +// --preload --library-path -c 'exec -a "$0" "$@"' ... +// so that bash, a program of the rootfs with the hook loaded, starts it the way every program of +// The native userland is started (Ubuntu's dynamic programs through ld.so, scripts through their interpreter, +// static programs patched first), with the argv[0] the caller chose. Without bash it is env, +// which cannot set argv[0]. The path is passed as the caller wrote it. +static i64 with_execve(const struct gosys_hdr *h, u32 *held, i64 a0, i64 a1, i64 a2) { + const char *path = (const char *)a0; + char *const *argv = (char *const *)a1; + char *const *envp = (char *const *)a2; + if (!path || path[0] == '\0') return sc(NR_EXECVE, a0, a1, a2, 0, 0, 0); + + char *slot = take_slot(h, held); + const char *p = path[0] == '/' ? rw_path(h, path, slot + SLOT_PATH_A, SLOT_PATH_CAP) : path; + int wrap = h->ldso_len != 0 && h->runner_len != 0 && (path[0] != '/' || in_root(h, p)); + if (!wrap) { + if (path[0] == '/' && !in_root(h, p)) return exec_host(h, slot, p, a1, envp); + return sc(NR_EXECVE, (i64)p, a1, a2, 0, 0, 0); + } + + // A program that is not there is reported to the caller, as execve() does + i64 r = sc(NR_FACCESSAT, AT_FDCWD, (i64)p, 0, 0, 0, 0); + if (r < 0) return r; + + u32 argc = 0; + while (argv && argv[argc]) argc++; + if (argc + 12 > ARGV_CAP) return sc(NR_EXECVE, (i64)p, a1, a2, 0, 0, 0); + + // DDNATV_ROOT must reach env even when the caller built an environment of its own + char *const *env = envp; + u32 envc = 0; + int has_root = 0; + while (envp && envp[envc]) { + const char *e = envp[envc]; + if (e[0] == 'D' && e[1] == 'D' && e[2] == 'N' && e[3] == 'A' && e[4] == 'T' && e[5] == 'V' && + e[6] == '_' && e[7] == 'R' && e[8] == 'O' && e[9] == 'O' && e[10] == 'T' && e[11] == '=') + has_root = 1; + envc++; + } + if (!has_root) { + if (envc + 2 > ENV_CAP) return sc(NR_EXECVE, (i64)p, a1, a2, 0, 0, 0); + char **ne = (char **)(slot + SLOT_ENV); + char *kv = slot + SLOT_PATH_B; + mcopy(kv, "DDNATV_ROOT=", 12); + mcopy(kv + 12, h->root, h->root_len + 1); + for (u32 i = 0; i < envc; i++) ne[i] = envp[i]; + ne[envc] = kv; + ne[envc + 1] = 0; + env = ne; + } + + char **v = (char **)(slot + SLOT_ARGV); + u32 n = 0; + v[n++] = (char *)h->ldso; + if (h->hook_len) { + v[n++] = (char *)"--preload"; + v[n++] = (char *)h->hook; + } + v[n++] = (char *)"--library-path"; + v[n++] = (char *)h->libs; + v[n++] = (char *)h->runner; + if (h->flags & GOSYS_FLAG_RUNNER_IS_BASH) { + v[n++] = (char *)"-c"; + v[n++] = (char *)"exec -a \"$0\" \"$@\""; + v[n++] = (argc > 0 && argv[0]) ? argv[0] : (char *)path; + } else { + v[n++] = (char *)"--"; + } + v[n++] = (char *)path; + for (u32 i = 1; i < argc; i++) v[n++] = argv[i]; + v[n] = 0; + return sc(NR_EXECVE, (i64)h->ldso, (i64)v, (i64)env, 0, 0, 0); +} + +// ---- The entry --------------------------------------------------------------------------- + +// Rewrites the path arguments `i1` and `i2` (an index into a[], -1: none) that are absolute, makes +// the call, and shows the program the paths it expects in what the call returns. +static i64 with_paths(const struct gosys_hdr *h, u32 *held, i64 nr, i64 *a, int i1, int i2) { + char *slot = 0; + int proc_link = 0; + int idx[2] = { i1, i2 }; + for (int k = 0; k < 2; k++) { + int i = idx[k]; + if (i < 0) continue; + const char *p = (const char *)a[i]; + if (!p || p[0] != '/') continue; + if (!slot) slot = take_slot(h, held); + if (nr == NR_READLINKAT && k == 0 && pre(p, "/proc", 5)) proc_link = 1; + a[i] = (i64)rw_path(h, p, slot + (k ? SLOT_PATH_B : SLOT_PATH_A), SLOT_PATH_CAP); + } + i64 r = sc(nr, a[0], a[1], a[2], a[3], a[4], a[5]); + if (proc_link && r > 0) r = unmap_prefix(h, (char *)a[2], r, 0); + return r; +} + +// The call itself; a slot that it takes is given back by cxgo_entry +static i64 handle(const struct gosys_hdr *h, u32 *held, i64 nr, i64 a0, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5) { + i64 a[6] = { a0, a1, a2, a3, a4, a5 }; + switch (nr) { + case NR_OPENAT: case NR_FACCESSAT: case NR_FACCESSAT2: case NR_FCHMODAT: case NR_FCHMODAT2: + case NR_FCHOWNAT: case NR_NEWFSTATAT: case NR_STATX: case NR_READLINKAT: case NR_MKDIRAT: + case NR_MKNODAT: case NR_UNLINKAT: case NR_UTIMENSAT: case NR_OPENAT2: case NR_NAME_TO_HANDLE_AT: + case NR_INOTIFY_ADD_WATCH: case NR_CHDIR: case NR_CHROOT: case NR_STATFS: case NR_TRUNCATE: + case NR_SETXATTR: case NR_LSETXATTR: case NR_GETXATTR: case NR_LGETXATTR: case NR_LISTXATTR: + case NR_LLISTXATTR: case NR_REMOVEXATTR: case NR_LREMOVEXATTR: case NR_RENAMEAT: case NR_RENAMEAT2: + case NR_LINKAT: case NR_SYMLINKAT: case NR_GETCWD: case NR_BIND: case NR_CONNECT: case NR_EXECVE: + // The calls that carry paths: not mapped where the rootfs is not in its own place (above) + if (!rewriting(h)) return sc(nr, a0, a1, a2, a3, a4, a5); + break; + default: + return sc(nr, a0, a1, a2, a3, a4, a5); + } + switch (nr) { + case NR_OPENAT: case NR_FACCESSAT: case NR_FACCESSAT2: case NR_FCHMODAT: case NR_FCHMODAT2: + case NR_FCHOWNAT: case NR_NEWFSTATAT: case NR_STATX: case NR_READLINKAT: case NR_MKDIRAT: + case NR_MKNODAT: case NR_UNLINKAT: case NR_UTIMENSAT: case NR_OPENAT2: case NR_NAME_TO_HANDLE_AT: + case NR_INOTIFY_ADD_WATCH: + return with_paths(h, held, nr, a, 1, -1); + case NR_CHDIR: case NR_CHROOT: case NR_STATFS: case NR_TRUNCATE: case NR_SETXATTR: + case NR_LSETXATTR: case NR_GETXATTR: case NR_LGETXATTR: case NR_LISTXATTR: case NR_LLISTXATTR: + case NR_REMOVEXATTR: case NR_LREMOVEXATTR: + return with_paths(h, held, nr, a, 0, -1); + case NR_RENAMEAT: case NR_RENAMEAT2: case NR_LINKAT: + return with_paths(h, held, nr, a, 1, 3); + case NR_SYMLINKAT: // the target is data, only the link is a path + return with_paths(h, held, nr, a, 2, -1); + case NR_GETCWD: { + i64 r = sc(nr, a0, a1, 0, 0, 0, 0); + return r > 0 ? unmap_prefix(h, (char *)a0, r, 1) : r; + } + case NR_BIND: case NR_CONNECT: + return with_sockaddr(h, held, nr, a0, a1, a2); + case NR_EXECVE: { + // Counted while it is on its way: when it works it does not return (see execs_on_their_way) + u32 *count = execs_on_their_way(h); + __atomic_fetch_add(count, 1, __ATOMIC_RELAXED); + i64 r = with_execve(h, held, a0, a1, a2); + count_down(count); // it did return: it failed, and cxgo_entry gives its slot back + return r; + } + default: + return sc(nr, a0, a1, a2, a3, a4, a5); + } +} + +__attribute__((section(".text.cxgo_entry"), used, noinline)) +i64 cxgo_entry(i64 a0, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5, i64 unused, i64 nr) { + (void)unused; + const struct gosys_hdr *h = hdr(); + u32 held = NO_SLOT; + reclaim_left_slots(h); + i64 r = handle(h, &held, nr, a0, a1, a2, a3, a4, a5); + put_slot(h, held); + return r; +} diff --git a/tools/nativeguest/gosys/gosys.ld b/tools/nativeguest/gosys/gosys.ld new file mode 100644 index 0000000000..ba5a6ba34f --- /dev/null +++ b/tools/nativeguest/gosys/gosys.ld @@ -0,0 +1,14 @@ +/* The layout of gosys.c's flat binary: the header at 0 (the patcher fills it in), the room for the + call site's connecting code, the code with its entry first, read-only data, the trailer last. */ +OUTPUT_FORMAT(elf64-littleaarch64) +ENTRY(cxgo_entry) +SECTIONS +{ + . = 0; + .hdr : { KEEP(*(.hdr)) } + .thunks : { . = ALIGN(16); KEEP(*(.thunks)) } + .text : { . = ALIGN(16); KEEP(*(.text.cxgo_entry)) *(.text .text.*) } + .rodata : { . = ALIGN(16); *(.rodata .rodata.*) } + .trailer : { . = ALIGN(64); KEEP(*(.trailer)) } + /DISCARD/ : { *(.comment) *(.note*) *(.eh_frame*) *(.gnu*) *(.interp) } +} diff --git a/tools/nativeguest/gosys/gosys_abi.h b/tools/nativeguest/gosys/gosys_abi.h new file mode 100644 index 0000000000..e0c4807d34 --- /dev/null +++ b/tools/nativeguest/gosys/gosys_abi.h @@ -0,0 +1,79 @@ +// What the hook and the code it adds to a static Go program (gosys.c) agree on. +// +// A Go program built without cgo makes its system calls itself, so the preloaded hook never sees +// them and Ubuntu's paths (/etc/resolv.conf, /usr/bin/git, ...) mean nothing to it. Before such a +// program starts, the hook adds a small piece of position-independent code to its file (see +// go_programs.c): the program's one system call function, internal/runtime/syscall.Syscall6, jumps +// to it, and it maps paths into the rootfs like the hook does, then makes the call. +// +// This header has no dependencies beyond , so that the freestanding code and the hook, +// which are built with different flags, can both include it. + +#ifndef DDNATV_GOSYS_ABI_H +#define DDNATV_GOSYS_ABI_H + +#include + +#ifndef GOSYS_VERSION +#define GOSYS_VERSION 3u // raised when the code or its header changes: patched programs are patched again +#endif + +// The scratch memory for rewritten paths: a small state area and 64 slots, in zero-filled memory the +// patcher appends to the program's data segment (so nothing is allocated per call). A slot belongs to +// the thread that took it until its call is over: a word in the state area for each slot says +// which goroutine (x28) has it. A slot that a vfork child kept by a successful execve is given back +// at the next call of the goroutine that made the child (the state area counts the execve calls +// that have not returned, [8]). +// (A ring of slots shared by every call was tried first: a thread that was descheduled for a +// moment between taking a slot and the call let sixteen others go round the ring and overwrite its +// path, so a Go program opened another file than it asked for.) +#define GOSYS_SLOTS 64u +#define GOSYS_SLOT_SIZE 32768u // two paths (4K each), an argv array (8K), an environment array (16K) +#define GOSYS_STATE_SIZE 1024u // [4] rewriting mode, [8] execve calls on their way, [64 + 8 * slot] who has the slot (0: nobody) +#define GOSYS_SCRATCH_SIZE (GOSYS_STATE_SIZE + GOSYS_SLOTS * GOSYS_SLOT_SIZE) + +#define GOSYS_THUNK_BYTES 256u // room for the small code that connects a call site to the code + +#define GOSYS_ROOT_MAX 448 +#define GOSYS_PATH_MAX 448 +#define GOSYS_LIBS_MAX 1024 + +struct gosys_hdr { + char magic[8]; // "CXGOSYS1" + uint32_t version; // GOSYS_VERSION + uint32_t flags; + int64_t scratch_rel; // scratch memory, relative to this header (the same in any load address) + uint32_t root_len; // the rootfs, as Android sees it + uint32_t ldso_len; // the rootfs's dynamic linker + uint32_t hook_len; // libddnatv-hook.so; 0: none + uint32_t libs_len; // the rootfs's library directories, ':' separated + uint32_t runner_len; // the rootfs's bash, or env without it; 0: Ubuntu programs cannot be started + uint32_t reserved; + char root[GOSYS_ROOT_MAX]; + char ldso[GOSYS_PATH_MAX]; + char hook[GOSYS_PATH_MAX]; + char libs[GOSYS_LIBS_MAX]; + char runner[GOSYS_PATH_MAX]; +}; + +#define GOSYS_FLAG_RUNNER_IS_BASH 1u // gosys_hdr.flags + +// The last 64 bytes of the added code: where it is, and what the patcher changed to add it. +struct gosys_trailer { + char magic[8]; // "CXGOEND1" + uint32_t version; + uint32_t total_size; // the added code, header to trailer + uint64_t text_orig_filesz; // the segment that holds it: its size before + uint64_t rw_orig_memsz; // the data segment: its size before + uint32_t text_index; // their numbers in the program header table + uint32_t rw_index; + uint32_t text_orig_flags; + uint32_t reserved; + uint64_t site_file_off; // the instruction that was replaced (a "svc #0") + uint64_t reserved2; +}; + +_Static_assert(sizeof(struct gosys_trailer) == 64, "the trailer is 64 bytes"); +_Static_assert(sizeof(struct gosys_hdr) % 16 == 0, "the header keeps the code after it aligned"); + +#endif diff --git a/tools/nativeguest/hook/env.c b/tools/nativeguest/hook/env.c new file mode 100644 index 0000000000..e95083c25e --- /dev/null +++ b/tools/nativeguest/hook/env.c @@ -0,0 +1,309 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// The environment of started programs: Android programs get it without the hook, rootfs +// programs with it. + +#ifndef DDNATV_HOST_TEST + +static const char *ddnatv_hook_library(char *buf, size_t bufsz); + +static char **clean_env_for_system(char *const envp[]) { + int count = 0; + while (envp && envp[count]) count++; + char **new_env = calloc(count + 3, sizeof(char *)); + int dst = 0; + int has_path = 0; + for (int i = 0; i < count; i++) { + char first = envp[i][0]; + if (!((first == 'L' && (strncmp(envp[i], "LD_PRELOAD=", 11) == 0 || + strncmp(envp[i], "LD_LIBRARY_PATH=", 16) == 0)) || + (first == 'G' && strncmp(envp[i], "GLIBC_TUNABLES=", 15) == 0))) { + if (first == 'P' && strncmp(envp[i], "PATH=", 5) == 0) { + has_path = 1; + size_t plen = strlen(envp[i]); + char *new_path = malloc(plen + 64); + if (new_path) { + snprintf(new_path, plen + 64, "PATH=/system/bin:/system/xbin:/sbin:/vendor/bin:%s", envp[i] + 5); + new_env[dst++] = new_path; + } else { + new_env[dst++] = envp[i]; + } + } else { + new_env[dst++] = envp[i]; + } + } + } + if (!has_path) { + new_env[dst++] = strdup("PATH=/system/bin:/system/xbin:/sbin:/vendor/bin"); + } + new_env[dst] = NULL; + return new_env; +} + +// LD_PRELOAD with every absolute path that is not in the rootfs yet mapped into it. Entries are +// separated by ':' or ' '. free() the result. +static char *map_preload_paths(const char *value) { + if (!value) return NULL; + size_t cap = strlen(value) + (g_ddnatv_root_len + 1) * 16 + 16; + char *out = malloc(cap); + if (!out) return NULL; + size_t used = 0; + out[0] = '\0'; + const char *p = value; + while (*p) { + size_t len = strcspn(p, ": "); + if (len > 0 && len < PATH_MAX) { + char entry[PATH_MAX]; + memcpy(entry, p, len); + entry[len] = '\0'; + char buf[PATH_MAX]; + const char *mapped = entry[0] == '/' ? rewrite_path_in_root(entry, buf, sizeof(buf)) : entry; + size_t mlen = strlen(mapped); + if (used + mlen + 2 >= cap) break; + if (used) out[used++] = ':'; + memcpy(out + used, mapped, mlen); + used += mlen; + out[used] = '\0'; + } + p += len; + if (*p) p++; + } + return out; +} + +static char **prepare_ddnatv_env(char *const envp[], const char *real_exe) { + init_ddnatv_hook(); + int count = 0; + int has_preload = 0; + int has_ld_library_path = 0; + int has_root = 0; + int has_tunables = 0; + int has_path = 0; + int has_tmp = 0; + int has_threads_max = 0; + int has_xz_opt = 0; + int has_xz_defaults = 0; + int has_frontend = 0; + int has_debconf_frontend = 0; + int has_debconf_seen = 0; + + const char *chosen_real_exe = (real_exe && real_exe[0] != '\0') ? real_exe : (g_real_exe[0] != '\0' ? g_real_exe : NULL); + + // Every variable is looked at on every exec, so only the names that start with its first letter + // are compared (this was 13 strncmp() calls for each of the 30 or so variables) + int has_nt = 0; + int has_binds = 0; + int has_hostname = 0; + int has_link_root = 0; + int has_preload_list = 0; + const char *preload_value = NULL; + while (envp && envp[count]) { + const char *e = envp[count]; + switch (e[0]) { + case 'L': + if (strncmp(e, "LD_PRELOAD=", 11) == 0) { has_preload = 1; preload_value = e + 11; } + else if (strncmp(e, "LD_LIBRARY_PATH=", 16) == 0) has_ld_library_path = 1; + break; + case 'G': + if (strncmp(e, "GLIBC_TUNABLES=", 15) == 0) has_tunables = 1; + break; + case 'P': + if (strncmp(e, "PATH=", 5) == 0) has_path = 1; + break; + case 'T': + if (strncmp(e, "TMPDIR=", 7) == 0) has_tmp = 1; + break; + case 'X': + if (strncmp(e, "XZ_OPT=", 7) == 0) has_xz_opt = 1; + else if (strncmp(e, "XZ_DEFAULTS=", 12) == 0) has_xz_defaults = 1; + break; + case 'D': + if (strncmp(e, "DDNATV_ROOT=", 12) == 0) has_root = 1; + else if (strncmp(e, "DDNATV_NT=", 10) == 0) has_nt = 1; + else if (strncmp(e, "DDNATV_BINDS=", 13) == 0) has_binds = 1; + else if (strncmp(e, "DDNATV_PRELOAD=", 15) == 0) has_preload_list = 1; + else if (strncmp(e, "DDNATV_HOSTNAME=", 16) == 0) has_hostname = 1; + else if (strncmp(e, "DDNATV_LINK_ROOT=", 17) == 0) has_link_root = 1; + else if (strncmp(e, "DPKG_DEB_THREADS_MAX=", 21) == 0) has_threads_max = 1; + else if (strncmp(e, "DEBIAN_FRONTEND=", 16) == 0) has_frontend = 1; + else if (strncmp(e, "DEBCONF_FRONTEND=", 17) == 0) has_debconf_frontend = 1; + else if (strncmp(e, "DEBCONF_NONINTERACTIVE_SEEN=", 28) == 0) has_debconf_seen = 1; + break; + default: + break; + } + count++; + } + + char **new_env = calloc(count + 24, sizeof(char *)); + int dst = 0; + for (int i = 0; i < count; i++) { + if (has_preload && envp[i][0] == 'L' && strncmp(envp[i], "LD_PRELOAD=", 11) == 0) { + // ld.so opens these itself: the guest's paths are mapped to the rootfs, and the libraries every + // program loads (DDNATV_PRELOAD) come first + char *mapped = map_preload_paths(preload_value); + if (mapped) { + size_t chain = g_preload_host ? strlen(g_preload_host) : 0; + int has_chain = chain > 0 && strstr(mapped, g_preload_host) != NULL; + size_t len = 11 + chain + 1 + strlen(mapped) + 1; + char *str = malloc(len); + if (str) { + if (chain > 0 && !has_chain) snprintf(str, len, "LD_PRELOAD=%s:%s", g_preload_host, mapped); + else snprintf(str, len, "LD_PRELOAD=%s", mapped); + new_env[dst++] = str; + free(mapped); + continue; + } + free(mapped); + } + } + // If we have an explicit real executable, drop the inherited entry to replace with the fresh target + if (chosen_real_exe && envp[i][0] == 'D' && strncmp(envp[i], "DDNATV_REAL_EXE=", 16) == 0) { + continue; + } + new_env[dst++] = envp[i]; + } + + // Exact-size strings: this runs before every exec, often in a freshly forked child where each + // newly touched page is a copy-on-write fault. + if (chosen_real_exe) { + size_t len = strlen(chosen_real_exe); + char *str = malloc(16 + len + 1); + if (str) { + memcpy(str, "DDNATV_REAL_EXE=", 16); + memcpy(str + 16, chosen_real_exe, len + 1); + new_env[dst++] = str; + } + } + + char hook_buf[PATH_MAX]; + const char *hook = (!has_preload && g_ddnatv_root[0] != '\0') ? ddnatv_hook_library(hook_buf, sizeof(hook_buf)) : NULL; + if (hook) { + size_t hook_len = strlen(hook); + size_t host_len = g_preload_host ? strlen(g_preload_host) + 1 : 0; + size_t size = 11 + host_len + hook_len + 1; + char *str = malloc(size); + if (str) { + size_t n = ddnatv_append(str, size, 0, "LD_PRELOAD=", 11); + if (host_len) { + n = ddnatv_append(str, size, n, g_preload_host, host_len - 1); + n = ddnatv_append(str, size, n, ":", 1); + } + ddnatv_append(str, size, n, hook, hook_len); + new_env[dst++] = str; + } + } + if (!has_preload_list && g_preload_guest) { + size_t len = strlen(g_preload_guest); + char *str = malloc(15 + len + 1); + if (str) { + memcpy(str, "DDNATV_PRELOAD=", 15); + memcpy(str + 15, g_preload_guest, len + 1); + new_env[dst++] = str; + } + } + if (!has_ld_library_path && g_ddnatv_root[0] != '\0') { + size_t size = 16 + 1; + for (size_t i = 0; i < sizeof(k_rootfs_lib_dirs) / sizeof(k_rootfs_lib_dirs[0]); i++) { + char dir[PATH_MAX]; + size += 1 + strlen(rewrite_path_in_root(k_rootfs_lib_dirs[i], dir, sizeof(dir))); + } + char *str = malloc(size); + if (str) { + size_t n = ddnatv_append(str, size, 0, "LD_LIBRARY_PATH=", 16); + append_rootfs_lib_dirs(str, size, n, 0); + new_env[dst++] = str; + } + } + if (!has_root && g_ddnatv_root[0] != '\0') { + char *str = malloc(PATH_MAX + 16); + if (str) { + snprintf(str, PATH_MAX + 16, "DDNATV_ROOT=%s", g_ddnatv_root); + new_env[dst++] = str; + } + } + if (!has_link_root && g_link_root_len) { + char *str = malloc(17 + g_link_root_len + 1); + if (str) { + memcpy(str, "DDNATV_LINK_ROOT=", 17); + memcpy(str + 17, g_link_root, g_link_root_len + 1); + new_env[dst++] = str; + } + } + if (!has_hostname && g_hostname[0]) { + char *str = malloc(16 + sizeof(g_hostname)); + if (str) { + snprintf(str, 16 + sizeof(g_hostname), "DDNATV_HOSTNAME=%s", g_hostname); + new_env[dst++] = str; + } + } + if (!has_binds && g_binds_raw) { + size_t len = strlen(g_binds_raw); + char *str = malloc(13 + len + 1); + if (str) { + memcpy(str, "DDNATV_BINDS=", 13); + memcpy(str + 13, g_binds_raw, len + 1); + new_env[dst++] = str; + } + } + // This process looked for the ARM translator's files and found none: the programs it starts + // need not look again (it takes a look at /proc/self/fd in each of them, see privileged.c) + if (!has_nt && g_no_translator) { + new_env[dst++] = strdup("DDNATV_NT=0"); + } + if (!has_tunables) { + new_env[dst++] = strdup("GLIBC_TUNABLES=glibc.pthread.rseq=0"); + } + if (!has_path) { + new_env[dst++] = strdup("PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"); + } + if (!has_tmp && g_ddnatv_root[0] != '\0') { + char *str = malloc(PATH_MAX + 16); + if (str) { + snprintf(str, PATH_MAX + 16, "TMPDIR=%s/tmp", g_ddnatv_root); + new_env[dst++] = str; + } + } + if (!has_threads_max) { + new_env[dst++] = strdup("DPKG_DEB_THREADS_MAX=1"); + } + if (!has_xz_opt) { + new_env[dst++] = strdup("XZ_OPT=-T1"); + } + if (!has_xz_defaults) { + new_env[dst++] = strdup("XZ_DEFAULTS=-T1"); + } + if (!has_frontend) { + new_env[dst++] = strdup("DEBIAN_FRONTEND=noninteractive"); + } + if (!has_debconf_frontend) { + new_env[dst++] = strdup("DEBCONF_FRONTEND=noninteractive"); + } + if (!has_debconf_seen) { + new_env[dst++] = strdup("DEBCONF_NONINTERACTIVE_SEEN=true"); + } + new_env[dst] = NULL; + return new_env; +} + +static void free_modified_env(char **modified_env, char *const orig_env[]) { + if (!modified_env) return; + for (int i = 0; modified_env[i] != NULL; i++) { + int is_orig = 0; + if (orig_env) { + for (int j = 0; orig_env[j] != NULL; j++) { + if (modified_env[i] == orig_env[j]) { + is_orig = 1; + break; + } + } + } + if (!is_orig) { + free(modified_env[i]); + } + } + free(modified_env); +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/exec.c b/tools/nativeguest/hook/exec.c new file mode 100644 index 0000000000..90df96f8fe --- /dev/null +++ b/tools/nativeguest/hook/exec.c @@ -0,0 +1,1127 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// execve() and posix_spawn(): rootfs programs through ld.so with the hook, scripts through their +// interpreter, Android programs with Android's environment. + +#ifndef DDNATV_HOST_TEST + +// Runs before every exec, mostly in a child a shell has just forked: the copies write only the +// string (ddnatv_copy_cstr), not the whole PATH_MAX buffer, since each page written there is a +// copy-on-write fault. +static void resolve_symlinks(const char *path, char *out, size_t out_size, int depth) { + if (!path || !out || out_size == 0) return; + ddnatv_copy_cstr(out, out_size, path); + if (depth > 8) return; + + char link_target[PATH_MAX]; + ssize_t len = readlink(path, link_target, sizeof(link_target) - 1); + if (len < 0) return; + link_target[len] = '\0'; + + char next_path[PATH_MAX]; + if (link_target[0] == '/') { + char rw_buf[PATH_MAX]; + const char *rewritten = rewrite_path(link_target, rw_buf, sizeof(rw_buf)); + ddnatv_copy_cstr(next_path, sizeof(next_path), rewritten); + } else { + const char *slash = strrchr(path, '/'); + if (slash) { + size_t dir_len = (size_t)(slash - path); + if (dir_len > sizeof(next_path) - 1) dir_len = sizeof(next_path) - 1; + memcpy(next_path, path, dir_len); + next_path[dir_len] = '\0'; + size_t n = ddnatv_append(next_path, sizeof(next_path), dir_len, "/", 1); + ddnatv_append(next_path, sizeof(next_path), n, link_target, (size_t)len); + } else { + ddnatv_copy_cstr(next_path, sizeof(next_path), link_target); + } + } + // proot's link2symlink leaves hard links as ".l2s.*" files that programs must not see by name + const char *next_base = strrchr(next_path, '/'); + next_base = next_base ? next_base + 1 : next_path; + if (strncmp(next_base, ".l2s.", 5) == 0) return; + resolve_symlinks(next_path, out, out_size, depth + 1); +} + +static char **ensure_hook_in_envp(char *const envp[], const char *real_exe) { + return prepare_ddnatv_env(envp, real_exe); +} + +// The file whose header decides how to start `target`: the symlink-resolved ELF (so ld.so and +// /proc/self/exe see the real program, as with the kernel), else `target` itself (scripts, +// links to ld.so). `probe` describes the returned file. +static const char *select_exec_target(const char *target, char *resolved, size_t resolved_size, struct exec_probe *probe) { + resolve_symlinks(target, resolved, resolved_size, 0); + probe_exec_file(resolved, probe); + if (probe->is_elf && !is_ld_linux(resolved)) return resolved; + if (strcmp(resolved, target) != 0) probe_exec_file(target, probe); + return target; +} + +// The library children get with --preload: the one this process runs from, so that a program +// always starts with the same build as its parent, else the first of usr/lib and lib that exists; +// NULL if neither does. +// The path dladdr() finds is kept (it does not change): set once, read by every later exec, and +// set before a fork by prepare_exec_in_parent() so that the children only read it. +static char g_hook_library[PATH_MAX]; +static int g_hook_library_known = 0; + +static const char *ddnatv_hook_library(char *buf, size_t bufsz) { + static const char usr_lib[] = "/usr/lib/libddnatv-hook.so"; + static const char lib[] = "/lib/libddnatv-hook.so"; + if (__atomic_load_n(&g_hook_library_known, __ATOMIC_ACQUIRE)) return g_hook_library; + Dl_info info; + if (dladdr((void *)ddnatv_hook_library, &info) && info.dli_fname && info.dli_fname[0] == '/' && + strlen(info.dli_fname) < sizeof(g_hook_library)) { + // loaded from there, so it exists + ddnatv_copy_cstr(g_hook_library, sizeof(g_hook_library), info.dli_fname); + __atomic_store_n(&g_hook_library_known, 1, __ATOMIC_RELEASE); + return g_hook_library; + } + size_t n = ddnatv_append(buf, bufsz, 0, g_ddnatv_root, g_ddnatv_root_len); + ddnatv_append(buf, bufsz, n, usr_lib, sizeof(usr_lib) - 1); + if (access(buf, F_OK) == 0) return buf; + n = ddnatv_append(buf, bufsz, 0, g_ddnatv_root, g_ddnatv_root_len); + ddnatv_append(buf, bufsz, n, lib, sizeof(lib) - 1); + return access(buf, F_OK) == 0 ? buf : NULL; +} + +// ld.so does not need the exec bit on the program it loads, but other tools look at it: set +// it as the old access(X_OK)/chmod() pair did, using the mode the probe already has. +static void ensure_exec_bit(const char *path, mode_t mode) { + if (S_ISDIR(mode) || (mode & 0111) != 0) return; + if (strstr(path, "/bin/") || strstr(path, "/sbin/")) return; + chmod(path, 0755); +} + +// Rootfs path of a "#!" interpreter; a missing /bin/sh falls back to Android's shell. +static const char *shebang_interpreter(const char *interp, char *buf, size_t bufsz) { + const char *rewritten = rewrite_path(interp, buf, bufsz); + if (strcmp(interp, "/bin/sh") == 0 && access(rewritten, F_OK) != 0 && access("/system/bin/sh", X_OK) == 0) { + return "/system/bin/sh"; + } + return rewritten; +} + +static const char *exec_prog_name(const char *target) { + const char *slash = strrchr(target, '/'); + return slash ? slash + 1 : target; +} + +// Hook execve +typedef int (*orig_execve_f_type)(const char *filename, char *const argv[], char *const envp[]); + +// glibc's execve(), looked up once (also by prepare_exec_in_parent() before a fork) +static orig_execve_f_type g_orig_execve = NULL; + +static orig_execve_f_type real_execve(void) { + orig_execve_f_type f = __atomic_load_n(&g_orig_execve, __ATOMIC_RELAXED); + if (!f) { + f = (orig_execve_f_type)dlsym(RTLD_NEXT, "execve"); + __atomic_store_n(&g_orig_execve, f, __ATOMIC_RELAXED); + } + return f; +} + +// prepare_exec_in_parent() rehearses an exec in the parent: everything up to the execve() itself, +// which this then skips. Only in the thread that rehearses: another thread's exec at that moment +// is a real one. +static int g_exec_rehearsal = 0; +static pthread_t g_exec_rehearsal_thread; + +static int in_exec_rehearsal(void) { + return __atomic_load_n(&g_exec_rehearsal, __ATOMIC_ACQUIRE) && + pthread_equal(pthread_self(), g_exec_rehearsal_thread); +} + +static int run_execve(orig_execve_f_type f, const char *path, char *const argv[], char *const envp[]) { + if (in_exec_rehearsal()) { + errno = 0; + return 0; + } + return f(path, argv, envp); +} + +// ldconfig does nothing in The native userland (its cache is for paths that ld.so opens without the hook), but the +// exec code reads /etc/ld.so.conf(.d) again when /etc/ld.so.cache changes: "sudo ldconfig" after an +// edit of a .conf file marks it changed, as the real ldconfig would by writing it. A cache asked +// for with -C is made (empty): Flatpak mounts the one it generates over /etc/ld.so.cache. +static void ldconfig_mark_cache(char *const argv[]) { + if (g_ddnatv_root[0] == '\0') return; + char path[PATH_MAX]; + const char *cache = NULL; + for (int i = 1; argv && argv[i]; i++) { + if (strcmp(argv[i], "-C") == 0 && argv[i + 1]) cache = argv[++i]; + else if (strncmp(argv[i], "-C", 2) == 0 && argv[i][2] != '\0') cache = argv[i] + 2; + } + const char *target = path; + if (cache) { + target = rewrite_path_in_root(cache, path, sizeof(path)); + } else { + size_t n = ddnatv_append(path, sizeof(path), 0, g_ddnatv_root, g_ddnatv_root_len); + ddnatv_append(path, sizeof(path), n, "/etc/ld.so.cache", 16); + } + int fd = (int)syscall(SYS_openat, AT_FDCWD, target, O_WRONLY | O_CREAT | O_CLOEXEC, 0644); + if (fd < 0) return; + syscall(SYS_utimensat, fd, NULL, NULL, 0); + syscall(SYS_close, fd); +} + +static int is_ldconfig_name(const char *name) { + return strcmp(name, "ldconfig") == 0 || strcmp(name, "ldconfig.real") == 0; +} + +static int ddnatv_execve(const char *filename, char *const argv[], char *const envp[], int depth) { + orig_execve_f_type orig_execve = real_execve(); + if (!orig_execve) { + errno = ENOSYS; + return -1; + } + + // Scripts get the path exactly as the caller wrote it ("./x.sh" stays "./x.sh" in $0), + // like the kernel would pass it; routing below uses the absolute form. + const char *caller_path = filename; + char abs_buf[PATH_MAX]; + filename = absolutize_exec_path(filename, abs_buf, sizeof(abs_buf)); + + char buf[PATH_MAX]; + const char *target = rewrite_path(filename, buf, sizeof(buf)); + + init_ddnatv_hook(); + + const char *f_base = (filename != NULL) ? strrchr(filename, '/') : NULL; + f_base = (f_base != NULL) ? f_base + 1 : (filename != NULL ? filename : ""); + + const char *prog_name = strrchr(target, '/'); + prog_name = (prog_name != NULL) ? prog_name + 1 : target; + + // Wine's preloader is a static program that opens the program and the dynamic linker it loads with + // system calls of its own, which no hook can map, to reserve address ranges before the linker runs. + // The program is started without it: its libc and ntdll reserve what Wine needs themselves. + if ((strcmp(prog_name, "wine-preloader") == 0 || strcmp(prog_name, "wine64-preloader") == 0) && + argv && argv[0] && argv[1] && depth < DDNATV_MAX_SHEBANG_DEPTH) { + return ddnatv_execve(argv[1], argv + 1, envp, depth + 1); + } + + // Intercept utilities that fail or cause issues in unprivileged Android environment + if (strcmp(f_base, "ldconfig") == 0 || + strcmp(f_base, "ldconfig.real") == 0 || + strcmp(f_base, "systemd-machine-id-setup") == 0 || + strcmp(f_base, "systemd-sysusers") == 0 || + strcmp(f_base, "systemd-tmpfiles") == 0 || + strcmp(f_base, "start-stop-daemon") == 0 || + strcmp(f_base, "mandb") == 0 || + strcmp(f_base, "mandb.real") == 0 || + strcmp(f_base, "update-mime-database") == 0 || + strcmp(f_base, "update-desktop-database") == 0 || + strcmp(f_base, "install-info") == 0 || + strcmp(f_base, "install-sgmlcatalog") == 0 || + strcmp(prog_name, "ldconfig") == 0 || + strcmp(prog_name, "ldconfig.real") == 0 || + strcmp(prog_name, "systemd-machine-id-setup") == 0 || + strcmp(prog_name, "systemd-sysusers") == 0 || + strcmp(prog_name, "systemd-tmpfiles") == 0 || + strcmp(prog_name, "start-stop-daemon") == 0 || + strcmp(prog_name, "mandb") == 0 || + strcmp(prog_name, "mandb.real") == 0 || + strcmp(prog_name, "update-mime-database") == 0 || + strcmp(prog_name, "update-desktop-database") == 0 || + strcmp(prog_name, "install-info") == 0 || + strcmp(prog_name, "install-sgmlcatalog") == 0) { + if (is_ldconfig_name(f_base) || is_ldconfig_name(prog_name)) ldconfig_mark_cache(argv); + _exit(0); + } + if (strcmp(f_base, "mountpoint") == 0 || strcmp(prog_name, "mountpoint") == 0) { + int is_virtual_fs = 0; + for (int i = 1; argv && argv[i]; i++) { + if (strcmp(argv[i], "/proc") == 0 || strcmp(argv[i], "/proc/") == 0 || + strcmp(argv[i], "/sys") == 0 || strcmp(argv[i], "/sys/") == 0 || + strcmp(argv[i], "/dev") == 0 || strcmp(argv[i], "/dev/") == 0 || + strcmp(argv[i], "/dev/pts") == 0) { + is_virtual_fs = 1; + break; + } + } + if (is_virtual_fs) { + _exit(0); + } + } + if (strcmp(f_base, "policy-rc.d") == 0 || strcmp(prog_name, "policy-rc.d") == 0) { + _exit(101); + } + + // A static program inside the rootfs, to be made safe to start (prepare_static_program) + const char *static_elf = NULL; + char resolved_target[PATH_MAX]; + + // Only intercept binaries/scripts within DDNATV_ROOT + if ((g_ddnatv_root[0] != '\0' && strncmp(target, g_ddnatv_root, g_ddnatv_root_len) == 0) || is_mapped_guest_path(filename)) { + struct exec_probe probe; + const char *elf_target = select_exec_target(target, resolved_target, sizeof(resolved_target), &probe); + if (probe.is_elf && !probe.is_dynamic) static_elf = elf_target; + // A Go program built for Android asks for Android's linker, so it is started like the dynamic + // ones, but it makes its own system calls: it gets the code of the static Go programs (#42) + if (probe.is_dynamic && probe.android_interp) prepare_static_program_ex(elf_target, 1); + + // 1. Transparently route dynamically linked glibc ELF binaries through ld.so + // Note: Statically linked binaries (PT_INTERP absent, e.g. Meta Muse Code) must execute directly! + if (probe.is_dynamic) { + char ld_so[PATH_MAX]; + ld_so[0] = '\0'; + if (find_dynamic_linker_for_machine(g_ddnatv_root, probe.e_machine, ld_so, sizeof(ld_so)) && strcmp(elf_target, ld_so) != 0) { + ensure_exec_bit(elf_target, probe.mode); + + if (!in_exec_rehearsal()) ddnatv_copy_cstr(g_real_exe, sizeof(g_real_exe), elf_target); + + char hook_buf[PATH_MAX]; + const char *hook_so = ddnatv_hook_library(hook_buf, sizeof(hook_buf)); + char ld_lib_path[PATH_MAX * 6]; + build_ld_library_path(envp, probe.runpath, probe.runpath_is_rpath, elf_target, ld_lib_path, sizeof(ld_lib_path)); + char **new_argv = build_ldso_argv(ld_so, hook_so, ld_lib_path, elf_target, exec_prog_name(target), argv); + if (!new_argv) { + errno = ENOMEM; + return -1; + } + char **new_env = ensure_hook_in_envp(envp, elf_target); + int ret = run_execve(orig_execve, ld_so, new_argv, new_env); + if (errno == EACCES && chmod(ld_so, 0755) == 0) { + // ld.so without its exec bit (an unusual restore): fix it once and retry + ret = run_execve(orig_execve, ld_so, new_argv, new_env); + } + int saved_errno = errno; + free(new_argv); + free_modified_env(new_env, envp); + errno = saved_errno; + return ret; + } + } + + // ld.so started directly (ldd, "ld.so --list PROGRAM", "ld.so PROGRAM"): the program it loads + // gets the --library-path a start of its own gets, or ldd reports its libraries as not found, + // and its path mapped into the rootfs, since ld.so opens it without the hook + if (probe.is_elf && !probe.is_dynamic && is_ld_linux(elf_target)) { + int caller_lib_path = 0; + int pi = ldso_program_index(argv, &caller_lib_path); + if (pi > 0 && caller_lib_path) { + // the caller's own --library-path stays as it is; only the program is mapped (when it is + // in the rootfs: a caller can also name a file by its real path) + char prog_buf[PATH_MAX]; + const char *prog = argv[pi]; + if (prog[0] == '/') { + const char *mapped = rewrite_path(prog, prog_buf, sizeof(prog_buf)); + int fd = mapped != prog ? (int)syscall(SYS_openat, AT_FDCWD, mapped, O_PATH | O_CLOEXEC) : -1; + if (fd >= 0) { + syscall(SYS_close, fd); + prog = mapped; + } + } + int argc = 0; + while (argv[argc]) argc++; + char **new_argv = (char **)malloc(sizeof(char *) * (size_t)(argc + 1)); + if (!new_argv) { + errno = ENOMEM; + return -1; + } + for (int i = 0; i <= argc; i++) new_argv[i] = argv[i]; + new_argv[pi] = (char *)prog; + prepare_static_program(static_elf); // ld.so is a static program itself + char **ddnatv_env = prepare_ddnatv_env(envp, target); + int ret = run_execve(orig_execve, target, new_argv, ddnatv_env); + int saved_errno = errno; + free_modified_env(ddnatv_env, envp); + free(new_argv); + errno = saved_errno; + return ret; + } + if (pi > 0) { + char prog_buf[PATH_MAX]; + const char *prog = argv[pi][0] == '/' ? rewrite_path(argv[pi], prog_buf, sizeof(prog_buf)) : argv[pi]; + struct exec_probe prog_probe; + probe_exec_file(prog, &prog_probe); + char ld_lib_path[PATH_MAX * 6]; + build_ld_library_path(envp, prog_probe.runpath, prog_probe.runpath_is_rpath, + prog[0] == '/' ? prog : NULL, ld_lib_path, sizeof(ld_lib_path)); + int argc = 0; + while (argv[argc]) argc++; + char **new_argv = (char **)malloc(sizeof(char *) * (size_t)(argc + 3)); + if (!new_argv) { + errno = ENOMEM; + return -1; + } + new_argv[0] = argv[0]; + new_argv[1] = (char *)"--library-path"; + new_argv[2] = ld_lib_path; + for (int i = 1; i <= argc; i++) new_argv[i + 2] = argv[i]; + new_argv[pi + 2] = (char *)prog; // ld.so opens it itself: "ldd /usr/bin/adb" as well + prepare_static_program(static_elf); // ld.so is a static program itself + char **ddnatv_env = prepare_ddnatv_env(envp, target); + int ret = run_execve(orig_execve, target, new_argv, ddnatv_env); + int saved_errno = errno; + free_modified_env(ddnatv_env, envp); + free(new_argv); + errno = saved_errno; + return ret; + } + } + + // 2. Handle scripts with shebang lines (e.g., #!/bin/sh, #!/usr/bin/perl) + if (!probe.is_elf && probe.n >= 2 && probe.hdr[0] == '#' && probe.hdr[1] == '!') { + char *interp; + char *interp_arg; + parse_shebang(probe.hdr, &interp, &interp_arg); + char interp_buf[PATH_MAX]; + const char *rewritten_interp = shebang_interpreter(interp, interp_buf, sizeof(interp_buf)); + const char *script = (caller_path && caller_path[0] != '/') ? caller_path : target; + char **new_argv = build_script_argv(rewritten_interp, interp_arg, script, argv); + if (!new_argv) { + errno = ENOMEM; + return -1; + } + int ret; + if (is_host_interpreter(rewritten_interp)) { + char **sys_env = clean_env_for_system(envp); + ret = run_execve(orig_execve, rewritten_interp, new_argv, sys_env); + int saved_errno = errno; + free_modified_env(sys_env, envp); + errno = saved_errno; + } else if (depth >= DDNATV_MAX_SHEBANG_DEPTH) { + errno = ELOOP; + ret = -1; + } else { + char **ddnatv_env = prepare_ddnatv_env(envp, NULL); + ret = ddnatv_execve(rewritten_interp, new_argv, ddnatv_env, depth + 1); + int saved_errno = errno; + free_modified_env(ddnatv_env, envp); + errno = saved_errno; + } + free(new_argv); + return ret; + } + } else { + // Any binary outside DDNATV_ROOT is an Android host binary (e.g. /system/bin/su, /sbin/su, /data/adb/...) + char **sys_env = clean_env_for_system(envp); + int ret = run_execve(orig_execve, target, argv, sys_env); + int saved_errno = errno; + free_modified_env(sys_env, envp); + errno = saved_errno; + return ret; + } + + if (static_elf) prepare_static_program(static_elf); + char **ddnatv_env = prepare_ddnatv_env(envp, target); + int ret = run_execve(orig_execve, target, argv, ddnatv_env); + int saved_errno = errno; + free_modified_env(ddnatv_env, envp); + errno = saved_errno; + return ret; +} + +int execve(const char *filename, char *const argv[], char *const envp[]) { + return ddnatv_execve(filename, argv, envp, 0); +} + +// ---- fork() --------------------------------------------------------------------------------- +// A shell (and make, dpkg, perl, ...) starts a program with fork() and then execve() in the child. +// What the exec code above needs for the first time in a process (glibc's execve, fstat and readlink +// behind the hooks, the rootfs' ld.so, the path of this library) it found in the child, where every +// page it writes is a copy of the parent's: some 40 copy-on-write faults before each program even +// started, where a shell's child without the hook has fewer than ten. Now the parent finds them at +// its first fork(), and the children inherit them and only read them. At its eighth fork() the +// parent also rehearses an exec (everything but the execve() itself), which binds the calls the +// exec code makes and touches the code it runs. That costs about as much as it saves in four +// children, so only a process that keeps forking (a shell running a loop or a long script, make, +// dpkg) does it; one that forks a few times (sh -c with a pipe) does not pay for it. +#define DDNATV_FORKS_BEFORE_REHEARSAL 8 +static int g_exec_prepared = 0; // 1 after the first fork(), 2 after the rehearsal +static pthread_mutex_t g_exec_prepare_lock = PTHREAD_MUTEX_INITIALIZER; + +static void rehearse_exec(void) { + static const char rehearsal_target[] = "/usr/bin/true"; + char target_buf[PATH_MAX]; + const char *target = rewrite_path(rehearsal_target, target_buf, sizeof(target_buf)); + struct exec_probe probe; + // A dynamic program only: it takes the one way through ddnatv_execve() that ends in run_execve() + probe_exec_file(target, &probe); + if (!probe.is_elf || !probe.is_dynamic) return; + extern char **environ; + char *rehearsal_argv[] = { (char *)"true", NULL }; + g_exec_rehearsal_thread = pthread_self(); + __atomic_store_n(&g_exec_rehearsal, 1, __ATOMIC_RELEASE); + ddnatv_execve(rehearsal_target, rehearsal_argv, environ, 0); + __atomic_store_n(&g_exec_rehearsal, 0, __ATOMIC_RELEASE); +} + +static void prepare_exec_in_parent(int level) { + // One thread prepares (two rehearsing at once would take each other for the other's thread). + // Not waiting for the lock: a signal handler that forks while its thread is preparing would wait + // for itself; a busy lock leaves the preparation to a later fork(). + if (pthread_mutex_trylock(&g_exec_prepare_lock) != 0) return; + int done = __atomic_load_n(&g_exec_prepared, __ATOMIC_ACQUIRE); + if (done >= level) { + pthread_mutex_unlock(&g_exec_prepare_lock); + return; + } + int saved_errno = errno; + init_ddnatv_hook(); + real_execve(); + if (g_ddnatv_root[0] != '\0') { + if (done < 1) { + char ld_so[PATH_MAX]; +#if defined(__aarch64__) + uint16_t machine = 183; +#elif defined(__arm__) + uint16_t machine = 40; +#elif defined(__x86_64__) + uint16_t machine = 62; +#else + uint16_t machine = 0; +#endif + if (machine && find_dynamic_linker_for_machine(g_ddnatv_root, machine, ld_so, sizeof(ld_so))) { + // readlink(), open(), read() and close() behind the hooks, as the probe calls them + char resolved[PATH_MAX]; + struct exec_probe probe; + resolve_symlinks(ld_so, resolved, sizeof(resolved), 0); + probe_exec_file(ld_so, &probe); + } + char hook_buf[PATH_MAX]; + ddnatv_hook_library(hook_buf, sizeof(hook_buf)); + struct stat st; + probe_fstat(-1, &st); // looks fstat() up; -1 only fails with EBADF + char conf[PATH_MAX * 2]; + ldconf_dirs(conf, sizeof(conf)); // ld.so.conf read here, so the children only compare times + } + if (level >= 2) rehearse_exec(); + } + __atomic_store_n(&g_exec_prepared, level, __ATOMIC_RELEASE); + pthread_mutex_unlock(&g_exec_prepare_lock); + errno = saved_errno; +} + +pid_t fork(void) { + static pid_t (*orig_fork)(void) = NULL; + if (!orig_fork) orig_fork = (pid_t (*)(void))dlsym(RTLD_NEXT, "fork"); + if (!orig_fork) { + errno = ENOSYS; + return -1; + } + int done = __atomic_load_n(&g_exec_prepared, __ATOMIC_ACQUIRE); + if (done < 2) { + static int forks = 0; + int n = __atomic_add_fetch(&forks, 1, __ATOMIC_RELAXED); + if (n == 1 || n >= DDNATV_FORKS_BEFORE_REHEARSAL) prepare_exec_in_parent(n >= DDNATV_FORKS_BEFORE_REHEARSAL ? 2 : 1); + } + pid_t pid = orig_fork(); + // A child forked from a signal handler that interrupted a rehearsal is not rehearsing: its + // execve() is a real one (only read in the usual case, so no page is copied for it) + if (pid == 0 && __atomic_load_n(&g_exec_rehearsal, __ATOMIC_RELAXED)) __atomic_store_n(&g_exec_rehearsal, 0, __ATOMIC_RELAXED); + return pid; +} + +extern char **environ; + +int execv(const char *path, char *const argv[]) { + return execve(path, argv, environ); +} + +int execl(const char *path, const char *arg0, ...) { + va_list args; + va_start(args, arg0); + int count = (arg0 != NULL) ? 1 : 0; + if (arg0 != NULL) { + while (va_arg(args, const char *) != NULL) { + count++; + } + } + va_end(args); + + char **argv = (char **)calloc(count + 1, sizeof(char *)); + if (!argv) { + errno = ENOMEM; + return -1; + } + if (count > 0) { + argv[0] = (char *)arg0; + va_start(args, arg0); + for (int i = 1; i < count; i++) { + argv[i] = va_arg(args, char *); + } + va_end(args); + } + argv[count] = NULL; + int ret = execv(path, argv); + free(argv); + return ret; +} + +int execlp(const char *file, const char *arg0, ...) { + va_list args; + va_start(args, arg0); + int count = (arg0 != NULL) ? 1 : 0; + if (arg0 != NULL) { + while (va_arg(args, const char *) != NULL) { + count++; + } + } + va_end(args); + + char **argv = (char **)calloc(count + 1, sizeof(char *)); + if (!argv) { + errno = ENOMEM; + return -1; + } + if (count > 0) { + argv[0] = (char *)arg0; + va_start(args, arg0); + for (int i = 1; i < count; i++) { + argv[i] = va_arg(args, char *); + } + va_end(args); + } + argv[count] = NULL; + int ret = execvp(file, argv); + free(argv); + return ret; +} + +int execle(const char *path, const char *arg0, ...) { + va_list args; + va_start(args, arg0); + int count = (arg0 != NULL) ? 1 : 0; + if (arg0 != NULL) { + while (va_arg(args, const char *) != NULL) { + count++; + } + } + char *const *envp = va_arg(args, char *const *); + va_end(args); + + char **argv = (char **)calloc(count + 1, sizeof(char *)); + if (!argv) { + errno = ENOMEM; + return -1; + } + if (count > 0) { + argv[0] = (char *)arg0; + va_start(args, arg0); + for (int i = 1; i < count; i++) { + argv[i] = va_arg(args, char *); + } + va_end(args); + } + argv[count] = NULL; + int ret = execve(path, argv, (char *const *)envp); + free(argv); + return ret; +} + +int execvp(const char *file, char *const argv[]) { + if (!file || !*file) { + errno = ENOENT; + return -1; + } + if (strchr(file, '/')) { + return execve(file, argv, environ); + } + const char *path_env = getenv("PATH"); + if (!path_env) path_env = "/usr/bin:/bin"; + char path_copy[4096]; + strncpy(path_copy, path_env, sizeof(path_copy) - 1); + path_copy[sizeof(path_copy) - 1] = '\0'; + char *saveptr = NULL; + char *token = strtok_r(path_copy, ":", &saveptr); + while (token) { + char candidate[PATH_MAX]; + snprintf(candidate, sizeof(candidate), "%s/%s", token, file); + if (access(candidate, F_OK) == 0) { + return execve(candidate, argv, environ); + } + token = strtok_r(NULL, ":", &saveptr); + } + const char *standard_paths[] = {"/usr/bin", "/bin", "/usr/sbin", "/sbin", "/usr/local/bin", NULL}; + for (int i = 0; standard_paths[i] != NULL; i++) { + char candidate[PATH_MAX]; + snprintf(candidate, sizeof(candidate), "%s/%s", standard_paths[i], file); + if (access(candidate, F_OK) == 0) { + return execve(candidate, argv, environ); + } + } + return execve(file, argv, environ); +} + +int execvpe(const char *file, char *const argv[], char *const envp[]) { + if (!file || !*file) { + errno = ENOENT; + return -1; + } + if (strchr(file, '/')) { + return execve(file, argv, envp); + } + const char *path_env = NULL; + if (envp) { + for (char *const *ep = envp; *ep; ep++) { + if (strncmp(*ep, "PATH=", 5) == 0) { + path_env = *ep + 5; + break; + } + } + } + if (!path_env) path_env = getenv("PATH"); + if (!path_env) path_env = "/usr/bin:/bin"; + char path_copy[4096]; + strncpy(path_copy, path_env, sizeof(path_copy) - 1); + path_copy[sizeof(path_copy) - 1] = '\0'; + char *saveptr = NULL; + char *token = strtok_r(path_copy, ":", &saveptr); + while (token) { + char candidate[PATH_MAX]; + snprintf(candidate, sizeof(candidate), "%s/%s", token, file); + if (access(candidate, F_OK) == 0) { + return execve(candidate, argv, envp); + } + token = strtok_r(NULL, ":", &saveptr); + } + const char *standard_paths[] = {"/usr/bin", "/bin", "/usr/sbin", "/sbin", "/usr/local/bin", NULL}; + for (int i = 0; standard_paths[i] != NULL; i++) { + char candidate[PATH_MAX]; + snprintf(candidate, sizeof(candidate), "%s/%s", standard_paths[i], file); + if (access(candidate, F_OK) == 0) { + return execve(candidate, argv, envp); + } + } + return execve(file, argv, envp); +} + +typedef int (*orig_posix_spawn_f_type)(pid_t *, const char *, const posix_spawn_file_actions_t *, + const posix_spawnattr_t *, char *const [], char *const []); + +static orig_posix_spawn_f_type real_posix_spawn(void) { + static orig_posix_spawn_f_type orig_posix_spawn = NULL; + if (!orig_posix_spawn) orig_posix_spawn = (orig_posix_spawn_f_type)dlsym(RTLD_NEXT, "posix_spawn"); + return orig_posix_spawn; +} + +static int ddnatv_posix_spawn(pid_t *pid, const char *path, + const posix_spawn_file_actions_t *file_actions, + const posix_spawnattr_t *attrp, + char *const argv[], char *const envp[], int depth); + +// A rootfs script: start its interpreter with the same file actions and attributes. Handing the +// script to the kernel resolves "#!/bin/sh" to Android's own shell, which crashed on the glibc +// hook in LD_PRELOAD (and "#!/usr/bin/env" etc. used to fall back to fork() + execve(), which +// dropped the file actions). +static int spawn_script(pid_t *pid, const char *script, struct exec_probe *probe, + const posix_spawn_file_actions_t *file_actions, const posix_spawnattr_t *attrp, + char *const argv[], char *const envp[], int depth) { + char *interp; + char *interp_arg; + parse_shebang(probe->hdr, &interp, &interp_arg); + char interp_buf[PATH_MAX]; + const char *rewritten_interp = shebang_interpreter(interp, interp_buf, sizeof(interp_buf)); + char **new_argv = build_script_argv(rewritten_interp, interp_arg, script, argv); + if (!new_argv) return ENOMEM; + int ret; + if (is_host_interpreter(rewritten_interp)) { + orig_posix_spawn_f_type orig_posix_spawn = real_posix_spawn(); + char **sys_env = clean_env_for_system(envp); + ret = orig_posix_spawn ? orig_posix_spawn(pid, rewritten_interp, file_actions, attrp, new_argv, sys_env) : ENOSYS; + free_modified_env(sys_env, envp); + } else { + ret = ddnatv_posix_spawn(pid, rewritten_interp, file_actions, attrp, new_argv, envp, depth + 1); + } + free(new_argv); + return ret; +} + +static int ddnatv_posix_spawn(pid_t *pid, const char *path, + const posix_spawn_file_actions_t *file_actions, + const posix_spawnattr_t *attrp, + char *const argv[], char *const envp[], int depth) { + orig_posix_spawn_f_type orig_posix_spawn = real_posix_spawn(); + + const char *caller_path = path; + char abs_buf[PATH_MAX]; + path = absolutize_exec_path(path, abs_buf, sizeof(abs_buf)); + + char buf[PATH_MAX]; + const char *target = rewrite_path(path, buf, sizeof(buf)); + + init_ddnatv_hook(); + + const char *f_base = (path != NULL) ? strrchr(path, '/') : NULL; + f_base = (f_base != NULL) ? f_base + 1 : (path != NULL ? path : ""); + + const char *prog_name = exec_prog_name(target); + + if (strcmp(f_base, "ldconfig") == 0 || + strcmp(f_base, "ldconfig.real") == 0 || + strcmp(f_base, "systemd-machine-id-setup") == 0 || + strcmp(f_base, "systemd-sysusers") == 0 || + strcmp(f_base, "systemd-tmpfiles") == 0 || + strcmp(f_base, "start-stop-daemon") == 0 || + strcmp(prog_name, "ldconfig") == 0 || + strcmp(prog_name, "ldconfig.real") == 0 || + strcmp(prog_name, "systemd-machine-id-setup") == 0 || + strcmp(prog_name, "systemd-sysusers") == 0 || + strcmp(prog_name, "systemd-tmpfiles") == 0 || + strcmp(prog_name, "start-stop-daemon") == 0) { + pid_t child = fork(); + if (child == 0) { + if (is_ldconfig_name(f_base) || is_ldconfig_name(prog_name)) ldconfig_mark_cache(argv); + _exit(0); + } else if (child > 0) { + if (pid) *pid = child; + return 0; + } + return errno; + } + if (strcmp(f_base, "mountpoint") == 0 || strcmp(prog_name, "mountpoint") == 0) { + int is_virtual_fs = 0; + for (int i = 1; argv && argv[i]; i++) { + if (strcmp(argv[i], "/proc") == 0 || strcmp(argv[i], "/proc/") == 0 || + strcmp(argv[i], "/sys") == 0 || strcmp(argv[i], "/sys/") == 0 || + strcmp(argv[i], "/dev") == 0 || strcmp(argv[i], "/dev/") == 0 || + strcmp(argv[i], "/dev/pts") == 0) { + is_virtual_fs = 1; + break; + } + } + if (is_virtual_fs) { + pid_t child = fork(); + if (child == 0) { + _exit(0); + } else if (child > 0) { + if (pid) *pid = child; + return 0; + } + return errno; + } + } + if (strcmp(f_base, "policy-rc.d") == 0 || strcmp(prog_name, "policy-rc.d") == 0) { + pid_t child = fork(); + if (child == 0) { + _exit(101); + } else if (child > 0) { + if (pid) *pid = child; + return 0; + } + return errno; + } + + char *const *orig_ep = envp ? envp : environ; + int in_root = (g_ddnatv_root[0] != '\0' && strncmp(target, g_ddnatv_root, g_ddnatv_root_len) == 0) || is_mapped_guest_path(path); + + struct exec_probe probe; + char resolved_target[PATH_MAX]; + const char *elf_target = target; + if (in_root) { + elf_target = select_exec_target(target, resolved_target, sizeof(resolved_target), &probe); + if (!probe.is_elf && probe.n >= 2 && probe.hdr[0] == '#' && probe.hdr[1] == '!') { + // Like the kernel, give up after a few nested "#!" interpreters. + if (depth >= DDNATV_MAX_SHEBANG_DEPTH) return ELOOP; + const char *script = (caller_path && caller_path[0] != '/') ? caller_path : target; + return spawn_script(pid, script, &probe, file_actions, attrp, argv, orig_ep, depth); + } + } else { + probe.hdr[0] = '\0'; + probe.n = -1; + probe.is_elf = 0; + probe.is_dynamic = 0; + probe.android_interp = 0; + probe.e_machine = 0; + probe.mode = 0; + probe.runpath[0] = '\0'; + probe.runpath_is_rpath = 0; + } + + char ld_so[PATH_MAX]; + ld_so[0] = '\0'; + int has_ld_so = probe.is_dynamic ? find_dynamic_linker_for_machine(g_ddnatv_root, probe.e_machine, ld_so, sizeof(ld_so)) : 0; + + // Android programs get Android's environment, as execve() gives them: the glibc hook in + // LD_PRELOAD crashes them. + char **new_envp = in_root ? ensure_hook_in_envp(orig_ep, elf_target) : clean_env_for_system(orig_ep); + + int ret = -1; + if (has_ld_so && strcmp(elf_target, ld_so) != 0) { + ensure_exec_bit(elf_target, probe.mode); + if (probe.android_interp) prepare_static_program_ex(elf_target, 1); // a Go program built for Android (#42) + + char hook_buf[PATH_MAX]; + const char *hook_so = ddnatv_hook_library(hook_buf, sizeof(hook_buf)); + char ld_lib_path[PATH_MAX * 6]; + build_ld_library_path(orig_ep, probe.runpath, probe.runpath_is_rpath, elf_target, ld_lib_path, sizeof(ld_lib_path)); + char **new_argv = build_ldso_argv(ld_so, hook_so, ld_lib_path, elf_target, prog_name, argv); + if (!new_argv) { + ret = ENOMEM; + } else { + if (orig_posix_spawn) { + ret = orig_posix_spawn(pid, ld_so, file_actions, attrp, new_argv, new_envp); + if (ret == EACCES && chmod(ld_so, 0755) == 0) { + // ld.so without its exec bit (an unusual restore): fix it once and retry + ret = orig_posix_spawn(pid, ld_so, file_actions, attrp, new_argv, new_envp); + } + } + free(new_argv); + } + } else { + if (in_root && probe.is_elf && !probe.is_dynamic) prepare_static_program(elf_target); + if (orig_posix_spawn) { + ret = orig_posix_spawn(pid, target, file_actions, attrp, argv, new_envp); + } + } + + if (!orig_posix_spawn) { + pid_t child = fork(); + if (child < 0) { + free_modified_env(new_envp, orig_ep); + return errno; + } else if (child == 0) { + execve(target, argv, new_envp); + _exit(127); + } else { + if (pid) *pid = child; + free_modified_env(new_envp, orig_ep); + return 0; + } + } + free_modified_env(new_envp, orig_ep); + return ret; +} + +static int apply_closefrom(posix_spawn_file_actions_t *actions); + +int posix_spawn(pid_t *pid, const char *path, + const posix_spawn_file_actions_t *file_actions, + const posix_spawnattr_t *attrp, + char *const argv[], char *const envp[]) { + if (file_actions) { + int bad = apply_closefrom((posix_spawn_file_actions_t *)file_actions); + if (bad) return bad; + } + posix_spawnattr_t sanitized; + return ddnatv_posix_spawn(pid, path, file_actions, spawn_attr_for_android(attrp, &sanitized), argv, envp, 0); +} + +int posix_spawnp(pid_t *pid, const char *file, + const posix_spawn_file_actions_t *file_actions, + const posix_spawnattr_t *attrp, + char *const argv[], char *const envp[]) { + if (!file || !*file) { + return ENOENT; + } + if (strchr(file, '/')) { + return posix_spawn(pid, file, file_actions, attrp, argv, envp); + } + const char *path_env = NULL; + if (envp) { + for (char *const *ep = envp; *ep; ep++) { + if (strncmp(*ep, "PATH=", 5) == 0) { + path_env = *ep + 5; + break; + } + } + } + if (!path_env) path_env = getenv("PATH"); + if (!path_env) path_env = "/usr/bin:/bin"; + char path_copy[4096]; + strncpy(path_copy, path_env, sizeof(path_copy) - 1); + path_copy[sizeof(path_copy) - 1] = '\0'; + char *saveptr = NULL; + char *token = strtok_r(path_copy, ":", &saveptr); + while (token) { + char candidate[PATH_MAX]; + snprintf(candidate, sizeof(candidate), "%s/%s", token, file); + if (access(candidate, F_OK) == 0) { + return posix_spawn(pid, candidate, file_actions, attrp, argv, envp); + } + token = strtok_r(NULL, ":", &saveptr); + } + const char *standard_paths[] = {"/usr/bin", "/bin", "/usr/sbin", "/sbin", "/usr/local/bin", NULL}; + for (int i = 0; standard_paths[i] != NULL; i++) { + char candidate[PATH_MAX]; + snprintf(candidate, sizeof(candidate), "%s/%s", standard_paths[i], file); + if (access(candidate, F_OK) == 0) { + return posix_spawn(pid, candidate, file_actions, attrp, argv, envp); + } + } + // If not found in PATH or standard paths, return ENOENT without spawning zombie + return ENOENT; +} + +// ---- system(), popen() and pclose() --------------------------------------------------------- +// glibc starts the shell of system() and popen() from inside the C library (its own posix_spawn and +// execve), where the hook's execve() is not called: the kernel ran the machine's /bin/sh, which on +// Android is the system's own shell, and that one failed to link against Ubuntu's libraries +// ("CANNOT LINK EXECUTABLE sh: ...libc.so has bad ELF magic", #49). They start Ubuntu's /bin/sh here. + +extern char **environ; + +static pid_t start_shell(const char *command, int child_fd, int target_fd, const sigset_t *mask, + const struct sigaction *intr, const struct sigaction *quit, int close_popen_streams); + +// While system() waits, SIGINT and SIGQUIT are ignored; with several threads in system() at once, the +// first one in saves the dispositions and the last one out puts them back (as glibc does) +static pthread_mutex_t g_system_lock = PTHREAD_MUTEX_INITIALIZER; +static int g_system_waiting; +static struct sigaction g_system_intr, g_system_quit; + +// system(3) as POSIX describes it: SIGINT and SIGQUIT ignored and SIGCHLD blocked while it waits +int system(const char *command) { + if (!command) return access("/bin/sh", X_OK) == 0; // whether there is a shell (Ubuntu's) + pthread_mutex_lock(&g_system_lock); + if (g_system_waiting++ == 0) { + struct sigaction ignore; + memset(&ignore, 0, sizeof(ignore)); + ignore.sa_handler = SIG_IGN; + sigemptyset(&ignore.sa_mask); + sigaction(SIGINT, &ignore, &g_system_intr); + sigaction(SIGQUIT, &ignore, &g_system_quit); + } + struct sigaction intr = g_system_intr, quit = g_system_quit; + pthread_mutex_unlock(&g_system_lock); + sigset_t block, old; + sigemptyset(&block); + sigaddset(&block, SIGCHLD); + sigprocmask(SIG_BLOCK, &block, &old); + + int status = -1; + pid_t pid = start_shell(command, -1, -1, &old, &intr, &quit, 0); + if (pid > 0) { + while (waitpid(pid, &status, 0) < 0) { + if (errno != EINTR) { + status = -1; + break; + } + } + } + int saved = errno; + pthread_mutex_lock(&g_system_lock); + if (--g_system_waiting == 0) { + sigaction(SIGINT, &g_system_intr, NULL); + sigaction(SIGQUIT, &g_system_quit, NULL); + } + pthread_mutex_unlock(&g_system_lock); + sigprocmask(SIG_SETMASK, &old, NULL); + errno = saved; + return status; +} + +// The streams popen() made, with their shells: pclose() waits for the one of its stream +struct ddnatv_popen { + FILE *fp; + pid_t pid; + struct ddnatv_popen *next; +}; +static struct ddnatv_popen *g_popen_streams; +static pthread_mutex_t g_popen_lock = PTHREAD_MUTEX_INITIALIZER; + +static pid_t start_shell(const char *command, int child_fd, int target_fd, const sigset_t *mask, + const struct sigaction *intr, const struct sigaction *quit, int close_popen_streams) { + pid_t pid = fork(); + if (pid != 0) return pid; + if (intr) sigaction(SIGINT, intr, NULL); + if (quit) sigaction(SIGQUIT, quit, NULL); + if (mask) sigprocmask(SIG_SETMASK, mask, NULL); + if (child_fd >= 0) { + if (child_fd == target_fd) { + fcntl(target_fd, F_SETFD, 0); + } else if (dup2(child_fd, target_fd) < 0) { + _exit(127); + } + } + // POSIX: the streams of earlier popen() calls are not open in the new shell + if (close_popen_streams) { + for (struct ddnatv_popen *p = g_popen_streams; p; p = p->next) { + int fd = fileno(p->fp); + if (fd >= 0 && fd != target_fd) close(fd); + } + } + char *const argv[] = { (char *)"sh", (char *)"-c", (char *)command, NULL }; + ddnatv_execve("/bin/sh", argv, environ, 0); + _exit(127); +} + +FILE *popen(const char *command, const char *type) { + if (!command || !type || (type[0] != 'r' && type[0] != 'w')) { + errno = EINVAL; + return NULL; + } + int reading = type[0] == 'r'; + int keep_on_exec = strchr(type, 'e') == NULL; + int fds[2]; + if (pipe2(fds, O_CLOEXEC) != 0) return NULL; + int parent_end = reading ? fds[0] : fds[1]; + int child_end = reading ? fds[1] : fds[0]; + struct ddnatv_popen *entry = (struct ddnatv_popen *)malloc(sizeof(*entry)); + if (!entry) { + close(fds[0]); + close(fds[1]); + errno = ENOMEM; + return NULL; + } + pthread_mutex_lock(&g_popen_lock); + pid_t pid = start_shell(command, child_end, reading ? STDOUT_FILENO : STDIN_FILENO, NULL, NULL, NULL, 1); + int saved = errno; + close(child_end); + FILE *fp = NULL; + if (pid > 0) { + if (keep_on_exec) fcntl(parent_end, F_SETFD, 0); + fp = fdopen(parent_end, reading ? "r" : "w"); + saved = errno; + } + if (!fp) { + close(parent_end); + pthread_mutex_unlock(&g_popen_lock); + free(entry); + if (pid > 0) { + // A shell nobody will read or write: it is not left to run (or waited for while it does) + kill(pid, SIGKILL); + while (waitpid(pid, NULL, 0) < 0 && errno == EINTR) {} + } + errno = saved; + return NULL; + } + entry->fp = fp; + entry->pid = pid; + entry->next = g_popen_streams; + g_popen_streams = entry; + pthread_mutex_unlock(&g_popen_lock); + return fp; +} + +int pclose(FILE *stream) { + struct ddnatv_popen *found = NULL; + pthread_mutex_lock(&g_popen_lock); + for (struct ddnatv_popen **p = &g_popen_streams; *p; p = &(*p)->next) { + if ((*p)->fp == stream) { + found = *p; + *p = found->next; + break; + } + } + // Closed before the lock is let go: a popen() in between would fork a shell that keeps this pipe open, + // and the shell of this stream would wait for its end of it + if (found) fclose(stream); + pthread_mutex_unlock(&g_popen_lock); + if (!found) { + // Not one of ours: glibc's own + static int (*orig_pclose)(FILE *) = NULL; + if (!orig_pclose) orig_pclose = (int (*)(FILE *))dlsym(RTLD_NEXT, "pclose"); + if (!orig_pclose) { + errno = ECHILD; + return -1; + } + return orig_pclose(stream); + } + pid_t pid = found->pid; + free(found); + int status = -1; + pid_t r; + do { + r = waitpid(pid, &status, 0); + } while (r < 0 && errno == EINTR); + return r < 0 ? -1 : status; +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/exec_probe.c b/tools/nativeguest/hook/exec_probe.c new file mode 100644 index 0000000000..f877175e58 --- /dev/null +++ b/tools/nativeguest/hook/exec_probe.c @@ -0,0 +1,1027 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// What execve() and posix_spawn() need to know about a program: its ld.so, whether it is a +// dynamic ELF, a script and its interpreter, and the argument lists built from that. + +// Relative exec paths ("./configure", "bin/tool", "../x.sh") must go through the same +// DDNATV_ROOT routing as absolute ones. Without this they were mistaken for Android host +// binaries and handed to the kernel unchanged, which then resolved "#!/bin/bash" (or a glibc +// PT_INTERP) literally and failed with "bad interpreter: No such file or directory". +// getcwd() is not virtualised, so cwd + path is a real path that starts with DDNATV_ROOT. +static const char *absolutize_exec_path(const char *path, char *buffer, size_t bufsize) { + if (!path || path[0] == '\0' || path[0] == '/') return path; + char cwd[PATH_MAX]; + static char *(*real_getcwd)(char *, size_t) = NULL; + if (!real_getcwd) real_getcwd = (char *(*)(char *, size_t))dlsym(RTLD_NEXT, "getcwd"); + if (!real_getcwd || !real_getcwd(cwd, sizeof(cwd))) return path; + + while (path[0] == '.' && path[1] == '/') { + path += 2; + while (path[0] == '/') path++; + } + if (path[0] == '\0' || strcmp(path, ".") == 0) return path; + + size_t cwd_len = strlen(cwd); + while (cwd_len > 1 && cwd[cwd_len - 1] == '/') cwd[--cwd_len] = '\0'; + int n = snprintf(buffer, bufsize, "%s%s%s", cwd, (cwd_len == 1 && cwd[0] == '/') ? "" : "/", path); + if (n < 0 || (size_t)n >= bufsize) return path; + return buffer; +} + +static char s_cached_ld_so[PATH_MAX] = {0}; +static char s_cached_ld_root[PATH_MAX] = {0}; +static uint16_t s_cached_e_machine = 0; + +// ld.so inside `ddnatv_root` for an ELF with machine type `e_machine` (0 when unknown). +static int find_dynamic_linker_for_machine(const char *ddnatv_root, uint16_t e_machine, char *out_ld_so, size_t max_len) { + if (!ddnatv_root || ddnatv_root[0] == '\0' || !out_ld_so || max_len == 0) return 0; + + if (s_cached_ld_so[0] != '\0' && strcmp(s_cached_ld_root, ddnatv_root) == 0 && + e_machine != 0 && e_machine == s_cached_e_machine) { + ddnatv_copy_cstr(out_ld_so, max_len, s_cached_ld_so); + return 1; + } + + const char *aarch64_cands[] = { + "/usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1", + "/usr/lib/aarch64-linux-gnu/ld-2.39.so", + "/lib/ld-linux-aarch64.so.1", + "/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1", + "/usr/lib/ld-linux-aarch64.so.1", + "/usr/lib64/ld-linux-aarch64.so.1", + "/lib64/ld-linux-aarch64.so.1", + "/lib/aarch64-linux-gnu/ld-2.39.so", + NULL + }; + + const char *armhf_cands[] = { + "/usr/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3", + "/usr/lib/arm-linux-gnueabihf/ld-2.39.so", + "/lib/ld-linux-armhf.so.3", + "/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3", + "/usr/lib/ld-linux-armhf.so.3", + "/lib/arm-linux-gnueabihf/ld-2.39.so", + "/usr/lib/arm-linux-gnueabi/ld-linux.so.3", + "/lib/arm-linux-gnueabi/ld-linux.so.3", + NULL + }; + + const char *x86_64_cands[] = { + "/lib64/ld-linux-x86-64.so.2", + "/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2", + "/usr/lib/x86_64-linux-gnu/ld-2.39.so", + "/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2", + NULL + }; + + const char **primary = NULL; + const char **secondary = NULL; + + if (e_machine == 183) { // EM_AARCH64 + primary = aarch64_cands; + secondary = NULL; + } else if (e_machine == 40) { // EM_ARM + primary = armhf_cands; + secondary = NULL; + } else if (e_machine == 62) { // EM_X86_64 + primary = x86_64_cands; + secondary = NULL; + } else if (e_machine == 3) { // EM_386 + primary = NULL; + secondary = NULL; + } else { + #if defined(__aarch64__) + primary = aarch64_cands; + secondary = armhf_cands; + #elif defined(__arm__) + primary = armhf_cands; + secondary = aarch64_cands; + #else + primary = x86_64_cands; + #endif + } + + if (primary) { + for (int i = 0; primary[i] != NULL; i++) { + snprintf(out_ld_so, max_len, "%s%s", ddnatv_root, primary[i]); + if (access(out_ld_so, F_OK) == 0) { + if (e_machine != 0) { + ddnatv_copy_cstr(s_cached_ld_so, sizeof(s_cached_ld_so), out_ld_so); + ddnatv_copy_cstr(s_cached_ld_root, sizeof(s_cached_ld_root), ddnatv_root); + s_cached_e_machine = e_machine; + } + return 1; + } + } + } + if (secondary) { + for (int i = 0; secondary[i] != NULL; i++) { + snprintf(out_ld_so, max_len, "%s%s", ddnatv_root, secondary[i]); + if (access(out_ld_so, F_OK) == 0) { + if (e_machine != 0) { + ddnatv_copy_cstr(s_cached_ld_so, sizeof(s_cached_ld_so), out_ld_so); + ddnatv_copy_cstr(s_cached_ld_root, sizeof(s_cached_ld_root), ddnatv_root); + s_cached_e_machine = e_machine; + } + return 1; + } + } + } + + return 0; +} + +#ifdef DDNATV_HOST_TEST +// Test entry point taking a program instead of its machine type (ddnatv-pty.c, which the tests +// include too, has a find_dynamic_linker of its own). +static int hook_find_dynamic_linker(const char *ddnatv_root, const char *cmd, char *out_ld_so, size_t max_len) { + if (!ddnatv_root || ddnatv_root[0] == '\0' || !out_ld_so || max_len == 0) return 0; + + uint16_t e_machine = 0; + if (cmd && cmd[0] != '\0') { + int fd = open(cmd, O_RDONLY); + if (fd >= 0) { + unsigned char ehdr[20]; + ssize_t n = read(fd, ehdr, sizeof(ehdr)); + close(fd); + if (n >= 20 && ehdr[0] == 0x7f && ehdr[1] == 'E' && ehdr[2] == 'L' && ehdr[3] == 'F') { + e_machine = (uint16_t)(ehdr[18] | (ehdr[19] << 8)); + } + } + } + return find_dynamic_linker_for_machine(ddnatv_root, e_machine, out_ld_so, max_len); +} +#endif + +static int check_elf_dynamic(int fd, const unsigned char *ehdr, ssize_t n) { + if (n < 52 || ehdr[0] != 0x7f || ehdr[1] != 'E' || ehdr[2] != 'L' || ehdr[3] != 'F') { + return 0; + } + if ((ehdr[4] != 1 && ehdr[4] != 2) || ehdr[5] != 1 /* ELFDATA2LSB */) { + return 0; + } + int is_64 = (ehdr[4] == 2); + uint64_t phoff = 0; + uint16_t phentsize = 0; + uint16_t phnum = 0; + if (is_64) { + if (n < 64) return 0; + memcpy(&phoff, ehdr + 32, sizeof(uint64_t)); + memcpy(&phentsize, ehdr + 54, sizeof(uint16_t)); + memcpy(&phnum, ehdr + 56, sizeof(uint16_t)); + } else { + uint32_t phoff32 = 0; + memcpy(&phoff32, ehdr + 28, sizeof(uint32_t)); + phoff = phoff32; + memcpy(&phentsize, ehdr + 42, sizeof(uint16_t)); + memcpy(&phnum, ehdr + 44, sizeof(uint16_t)); + } + uint16_t min_phentsize = is_64 ? 56 : 32; + if (phoff == 0 || phentsize < min_phentsize || phentsize > 4096 || phnum == 0) { + return 0; + } + if ((off_t)phoff < 0 || (uint64_t)(off_t)phoff != phoff) { + return 0; + } + if (lseek(fd, (off_t)phoff, SEEK_SET) < 0) { + return 0; + } + for (int i = 0; i < phnum && i < 128; i++) { + uint32_t p_type = 0; + if (read(fd, &p_type, sizeof(p_type)) != sizeof(p_type)) break; + if (p_type == 3 /* PT_INTERP */) { + return 1; + } + if (lseek(fd, (off_t)(phentsize - sizeof(p_type)), SEEK_CUR) < 0) break; + } + return 0; +} + +// check_elf_dynamic() on the bytes already read: 1 or 0, or -1 when a program header lies past +// the buffer and the file has to be read after all. Program headers normally follow the ELF +// header directly, so this saves the lseek()/read() pairs. +static int elf_interp_in_buffer(const unsigned char *ehdr, ssize_t n) { + if (n < 52 || ehdr[0] != 0x7f || ehdr[1] != 'E' || ehdr[2] != 'L' || ehdr[3] != 'F') { + return 0; + } + if ((ehdr[4] != 1 && ehdr[4] != 2) || ehdr[5] != 1 /* ELFDATA2LSB */) { + return 0; + } + int is_64 = (ehdr[4] == 2); + uint64_t phoff = 0; + uint16_t phentsize = 0; + uint16_t phnum = 0; + if (is_64) { + if (n < 64) return 0; + memcpy(&phoff, ehdr + 32, sizeof(uint64_t)); + memcpy(&phentsize, ehdr + 54, sizeof(uint16_t)); + memcpy(&phnum, ehdr + 56, sizeof(uint16_t)); + } else { + uint32_t phoff32 = 0; + memcpy(&phoff32, ehdr + 28, sizeof(uint32_t)); + phoff = phoff32; + memcpy(&phentsize, ehdr + 42, sizeof(uint16_t)); + memcpy(&phnum, ehdr + 44, sizeof(uint16_t)); + } + uint16_t min_phentsize = is_64 ? 56 : 32; + if (phoff == 0 || phentsize < min_phentsize || phentsize > 4096 || phnum == 0) { + return 0; + } + if ((off_t)phoff < 0 || (uint64_t)(off_t)phoff != phoff) { + return 0; + } + for (int i = 0; i < phnum && i < 128; i++) { + uint64_t off = phoff + (uint64_t)i * phentsize; + if (off > (uint64_t)n || (uint64_t)n - off < sizeof(uint32_t)) return -1; + uint32_t p_type = 0; + memcpy(&p_type, ehdr + off, sizeof(p_type)); + if (p_type == 3 /* PT_INTERP */) { + return 1; + } + } + return 0; +} + +// Whether the interpreter of a 64-bit ELF is Android's own linker (/system/bin/linker64). A Go +// program built with GOOS=android is a PIE that asks for it, so it has a PT_INTERP and looks like a +// dynamic program, but it has no C library and makes its own system calls like every Go program +// (#42). `h` holds the `n` first bytes of the file; the program headers usually are among them. +static int elf_interp_is_android(int fd, const unsigned char *h, ssize_t n) { + if (n < 64 || h[4] != 2) return 0; + uint64_t phoff = 0; + uint16_t phentsize = 0, phnum = 0; + memcpy(&phoff, h + 32, sizeof(phoff)); + memcpy(&phentsize, h + 54, sizeof(phentsize)); + memcpy(&phnum, h + 56, sizeof(phnum)); + if (phoff == 0 || phoff > (1ULL << 40) || phentsize < 56 || phentsize > 4096 || phnum == 0) return 0; + for (int i = 0; i < phnum && i < 128; i++) { + unsigned char e[56]; + uint64_t off = phoff + (uint64_t)i * phentsize; + if (off + sizeof(e) <= (uint64_t)n) { + memcpy(e, h + off, sizeof(e)); + } else if (pread(fd, e, sizeof(e), (off_t)off) != (ssize_t)sizeof(e)) { + return 0; + } + uint32_t p_type = 0; + memcpy(&p_type, e, sizeof(p_type)); + if (p_type != 3 /* PT_INTERP */) continue; + uint64_t p_offset = 0, p_filesz = 0; + memcpy(&p_offset, e + 8, sizeof(p_offset)); + memcpy(&p_filesz, e + 32, sizeof(p_filesz)); + char interp[48]; + size_t want = p_filesz < sizeof(interp) - 1 ? (size_t)p_filesz : sizeof(interp) - 1; + if (want == 0 || p_offset > (1ULL << 40)) return 0; + ssize_t got = pread(fd, interp, want, (off_t)p_offset); + if (got <= 0) return 0; + interp[got] = '\0'; + return strncmp(interp, "/system/bin/linker", 18) == 0; + } + return 0; +} + +// The kernel follows at most four nested "#!" interpreters, then fails with ELOOP. +#define DDNATV_MAX_SHEBANG_DEPTH 4 + +// What execve()/posix_spawn() need to know about the file they are about to start, from a +// single open()/read() (they used to open it up to three times). +struct exec_probe { + char hdr[256]; // first bytes of the file, NUL-terminated for "#!" parsing + ssize_t n; // bytes read; -1 when the file could not be opened + int is_elf; + int is_dynamic; // ELF with PT_INTERP: has to be started through ld.so + int android_interp; // AArch64 ELF whose PT_INTERP is Android's linker (a Go program for GOOS=android) + uint16_t e_machine; + mode_t mode; // st_mode, filled in for dynamic ELF files only + int runpath_is_rpath; // runpath holds a DT_RPATH (searched before LD_LIBRARY_PATH), not a DT_RUNPATH + char runpath[1024]; // DT_RUNPATH (else DT_RPATH) of a dynamic ELF as written in it, or "" +}; + +// The DT_RUNPATH of a dynamic ELF (its DT_RPATH when it has none, as ld.so does) into `out`, as +// written in the file. ld.so looks for the program's libraries there, but it opens those +// directories itself, without the hook: an absolute one such as /usr/lib/aarch64-linux-gnu/android +// (Ubuntu's adb and fastboot keep their own libbase, liblog, libssl... there) is looked for in +// Android's tree and the program does not start. The exec adds the rootfs copies to the +// --library-path it gives ld.so (build_ld_library_path). Returns 1 for a DT_RUNPATH, 2 for a +// DT_RPATH, 0 when there is neither. The headers are read in pieces, so that their number does +// not matter; a list longer than `out` keeps its first whole entries (ld.so still has the rest). +static int elf_read_dynamic(int fd, const unsigned char *h, ssize_t n, char *out, size_t outsz, + char *needed, size_t needed_size, int *needed_count); + +static int elf_read_runpath(int fd, const unsigned char *h, ssize_t n, char *out, size_t outsz) { + return elf_read_dynamic(fd, h, n, out, outsz, NULL, 0, NULL); +} + +// elf_read_runpath(), and with `needed` the DT_NEEDED names too: one after the other, each ended by a +// NUL, their number in *needed_count (as many as fit). +#define ELF_MAX_NEEDED 128 +static int elf_read_dynamic(int fd, const unsigned char *h, ssize_t n, char *out, size_t outsz, + char *needed, size_t needed_size, int *needed_count) { + if (needed_count) *needed_count = 0; + if (!out || outsz < 2) return 0; + out[0] = '\0'; + if (n < 52 || h[0] != 0x7f || h[1] != 'E' || h[2] != 'L' || h[3] != 'F' || h[5] != 1 /* LSB */) return 0; + int is_64 = (h[4] == 2); + if (!is_64 && h[4] != 1) return 0; + if (is_64 && n < 64) return 0; + uint64_t phoff = 0; + uint16_t phentsize = 0, phnum = 0; + if (is_64) { + memcpy(&phoff, h + 32, 8); + memcpy(&phentsize, h + 54, 2); + memcpy(&phnum, h + 56, 2); + } else { + uint32_t off32 = 0; + memcpy(&off32, h + 28, 4); + phoff = off32; + memcpy(&phentsize, h + 42, 2); + memcpy(&phnum, h + 44, 2); + } + size_t ph_size = is_64 ? 56 : 32; + if (phoff == 0 || phoff > (1ULL << 40) || phentsize < ph_size || phentsize > 1024 || phnum == 0) return 0; + + // PT_DYNAMIC, and the PT_LOAD segments that map an address of the file to its offset + unsigned char buf[2048]; + uint64_t dyn_off = 0, dyn_size = 0; + uint64_t load_vaddr[16], load_off[16], load_size[16]; + int loads = 0; + size_t per_read = sizeof(buf) / phentsize; + for (size_t first = 0; first < phnum; first += per_read) { + size_t count = phnum - first < per_read ? phnum - first : per_read; + size_t bytes = count * phentsize; + if (pread(fd, buf, bytes, (off_t)(phoff + first * phentsize)) != (ssize_t)bytes) return 0; + for (size_t i = 0; i < count; i++) { + const unsigned char *e = buf + i * phentsize; + uint32_t type = 0; + memcpy(&type, e, 4); + uint64_t off = 0, vaddr = 0, filesz = 0; + if (is_64) { + memcpy(&off, e + 8, 8); + memcpy(&vaddr, e + 16, 8); + memcpy(&filesz, e + 32, 8); + } else { + uint32_t v = 0; + memcpy(&v, e + 4, 4); off = v; + memcpy(&v, e + 8, 4); vaddr = v; + memcpy(&v, e + 16, 4); filesz = v; + } + if (type == 2 /* PT_DYNAMIC */) { + dyn_off = off; + dyn_size = filesz; + } else if (type == 1 /* PT_LOAD */ && loads < 16) { + load_vaddr[loads] = vaddr; + load_off[loads] = off; + load_size[loads] = filesz; + loads++; + } + } + } + if (dyn_size == 0 || dyn_off > (1ULL << 40)) return 0; + if (dyn_size > (1u << 20)) dyn_size = 1u << 20; + + size_t ent = is_64 ? 16 : 8; + uint64_t strtab = 0, runpath = 0, rpath = 0; + int has_strtab = 0, has_runpath = 0, has_rpath = 0, done = 0; + uint64_t needed_at[ELF_MAX_NEEDED]; + int nneeded = 0; + for (uint64_t at = 0; at + ent <= dyn_size && !done;) { + size_t want = dyn_size - at < sizeof(buf) ? (size_t)(dyn_size - at) : sizeof(buf); + want -= want % ent; + ssize_t got = pread(fd, buf, want, (off_t)(dyn_off + at)); + if (got < (ssize_t)ent) break; + for (size_t i = 0; i + ent <= (size_t)got; i += ent) { + int64_t tag = 0; + uint64_t val = 0; + if (is_64) { + memcpy(&tag, buf + i, 8); + memcpy(&val, buf + i + 8, 8); + } else { + int32_t tv = 0; + uint32_t vv = 0; + memcpy(&tv, buf + i, 4); + memcpy(&vv, buf + i + 4, 4); + tag = tv; + val = vv; + } + if (tag == 0 /* DT_NULL */) { done = 1; break; } + if (tag == 5 /* DT_STRTAB */) { strtab = val; has_strtab = 1; } + else if (tag == 29 /* DT_RUNPATH */) { runpath = val; has_runpath = 1; } + else if (tag == 15 /* DT_RPATH */) { rpath = val; has_rpath = 1; } + else if (tag == 1 /* DT_NEEDED */ && needed && nneeded < ELF_MAX_NEEDED) needed_at[nneeded++] = val; + } + at += (uint64_t)got - (uint64_t)got % ent; + } + if (!has_strtab || (!has_runpath && !has_rpath)) return 0; + uint64_t str_index = has_runpath ? runpath : rpath; + + // DT_STRTAB is an address: the file offset is that of the PT_LOAD segment holding it + uint64_t strtab_off = 0; + int mapped = 0; + for (int i = 0; i < loads; i++) { + if (strtab >= load_vaddr[i] && strtab - load_vaddr[i] < load_size[i]) { + strtab_off = load_off[i] + (strtab - load_vaddr[i]); + mapped = 1; + break; + } + } + if (!mapped || str_index > (1ULL << 32)) return 0; + if (needed && needed_count && needed_size > 0) { + size_t used = 0; + for (int i = 0; i < nneeded; i++) { + char name[256]; + ssize_t got = pread(fd, name, sizeof(name) - 1, (off_t)(strtab_off + needed_at[i])); + if (got <= 0) continue; + name[got] = '\0'; + size_t len = strnlen(name, (size_t)got); + if (len == 0 || len == (size_t)got || used + len + 1 > needed_size) continue; + memcpy(needed + used, name, len + 1); + used += len + 1; + (*needed_count)++; + } + } + ssize_t len = pread(fd, out, outsz - 1, (off_t)(strtab_off + str_index)); + if (len <= 0) { + out[0] = '\0'; + return 0; + } + out[len] = '\0'; + if (strnlen(out, (size_t)len) == (size_t)len) { + // Longer than `out`: its whole entries up to there + char *last = strrchr(out, ':'); + if (!last) { + out[0] = '\0'; + return 0; + } + *last = '\0'; + } + if (out[0] == '\0') return 0; + return has_runpath ? 1 : 2; +} + +// Appends dir[0..len) to the ':'-separated out_buf[0..used) unless it is there already. +static size_t append_lib_dir(char *out_buf, size_t out_size, size_t used, const char *dir, size_t len) { + if (len == 0 || lib_dir_list_has(out_buf, dir, len)) return used; + size_t need = (used > 0 ? 1 : 0) + len; + if (used + need >= out_size) return used; + if (used > 0) out_buf[used++] = ':'; + memcpy(out_buf + used, dir, len); + used += len; + out_buf[used] = '\0'; + return used; +} + +// The directories of `runpath` (a DT_RUNPATH or DT_RPATH), in their order, appended to +// out_buf[0..used): the absolute ones that lie in the rootfs mapped into it, and those that start +// with $ORIGIN with it replaced by the directory of `exe` (the program's real path, in the rootfs +// already), so that both keep their places. ld.so opens them itself, without the hook, so +// /usr/lib/aarch64-linux-gnu/android would be looked for in Android's tree. Others ($LIB, +// $PLATFORM, directories outside the rootfs) are left to ld.so. Returns the new length. +static size_t append_runpath_dirs(char *out_buf, size_t out_size, size_t used, const char *runpath, const char *exe) { + size_t origin_len = 0; + if (exe) { + const char *slash = strrchr(exe, '/'); + if (slash) origin_len = slash > exe ? (size_t)(slash - exe) : 1; + } + for (const char *p = runpath; p && *p;) { + const char *end = strchr(p, ':'); + size_t len = end ? (size_t)(end - p) : strlen(p); + if (len > 0 && len < PATH_MAX) { + char tok[PATH_MAX]; + memcpy(tok, p, len); + tok[len] = '\0'; + size_t skip = 0; + if (strncmp(tok, "$ORIGIN", 7) == 0) skip = 7; + else if (strncmp(tok, "${ORIGIN}", 9) == 0) skip = 9; + if (skip && (tok[skip] == '\0' || tok[skip] == '/') && origin_len > 0 && !strchr(tok + skip, '$')) { + char full[PATH_MAX]; + size_t rest = len - skip; + if (origin_len + rest < sizeof(full)) { + memcpy(full, exe, origin_len); + memcpy(full + origin_len, tok + skip, rest); + full[origin_len + rest] = '\0'; + used = append_lib_dir(out_buf, out_size, used, full, origin_len + rest); + } + } else if (tok[0] == '/' && len > 1 && !strchr(tok, '$')) { + char rw_buf[PATH_MAX]; + const char *rw = rewrite_path_in_root(tok, rw_buf, sizeof(rw_buf)); + if (strcmp(rw, tok) != 0) used = append_lib_dir(out_buf, out_size, used, rw, strlen(rw)); + } + } + if (!end) break; + p = end + 1; + } + return used; +} + +// Whether dir[0..len) is one of the rootfs library directories The native userland adds itself (also what its +// shells export in LD_LIBRARY_PATH). +static int is_rootfs_default_lib_dir(const char *dir, size_t len) { + if (len <= g_ddnatv_root_len || memcmp(dir, g_ddnatv_root, g_ddnatv_root_len) != 0) return 0; + const char *rel = dir + g_ddnatv_root_len; + size_t rel_len = len - g_ddnatv_root_len; + for (size_t i = 0; i < sizeof(k_rootfs_lib_dirs) / sizeof(k_rootfs_lib_dirs[0]); i++) { + if (strlen(k_rootfs_lib_dirs[i]) == rel_len && memcmp(k_rootfs_lib_dirs[i], rel, rel_len) == 0) return 1; + } + return 0; +} + +// Where the program is among the arguments of ld.so started directly (ldd runs "ld.so PROGRAM" with +// LD_TRACE_LOADED_OBJECTS set; "ld.so --list PROGRAM" and "ld.so --verify PROGRAM" are the same): its +// index, or -1 when there is none; *has_library_path says whether the caller gives --library-path itself. +static int ldso_program_index(char *const argv[], int *has_library_path) { + if (has_library_path) *has_library_path = 0; + if (!argv || !argv[0]) return -1; + static const char *const with_value[] = { "--library-path", "--preload", "--argv0", "--inhibit-rpath", "--audit", + "--glibc-hwcaps-prepend", "--glibc-hwcaps-mask" }; + for (int i = 1; argv[i]; i++) { + const char *a = argv[i]; + if (strcmp(a, "--library-path") == 0 && has_library_path) *has_library_path = 1; + if (a[0] != '-' || a[1] != '-') return i; + if (a[2] == '\0') return argv[i + 1] ? i + 1 : -1; + int takes_value = 0; + for (size_t k = 0; k < sizeof(with_value) / sizeof(with_value[0]); k++) { + if (strcmp(a, with_value[k]) == 0) takes_value = 1; + } + if (takes_value) { + if (!argv[i + 1]) return -1; + i++; + } + } + return -1; +} + +// ---- dlopen() of a library with a RUNPATH --------------------------------------------------- +// A library that a program loads with dlopen() looks for its own dependencies in its RUNPATH, which ld.so +// opens without the hook: PulseAudio's modules (RUNPATH /usr/lib/aarch64-linux-gnu/pulseaudio) and many +// plugins did not load. Before the real dlopen(), the dependencies found in the rootfs copies of those +// directories are loaded by their full path, depth first; ld.so then finds them loaded, by their name. +#define DLOPEN_PRELOAD_DEPTH 6 + +static int dlopen_preload_deps(const char *path, void *(*orig_dlopen)(const char *, int), int depth) { + if (depth > DLOPEN_PRELOAD_DEPTH || !path || path[0] != '/') return 0; + int fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC); + if (fd < 0) return 0; + unsigned char hdr[64]; + ssize_t n = pread(fd, hdr, sizeof(hdr), 0); + char runpath[1024]; + char needed[4096]; + int count = 0; + int kind = n > 0 ? elf_read_dynamic(fd, hdr, n, runpath, sizeof(runpath), needed, sizeof(needed), &count) : 0; + syscall(SYS_close, fd); + if (!kind || count == 0) return 0; + // the RUNPATH's directories, mapped into the rootfs ($ORIGIN from this library's own place) + char dirs[PATH_MAX * 2]; + dirs[0] = '\0'; + size_t used = append_runpath_dirs(dirs, sizeof(dirs), 0, runpath, path); + if (used == 0) return 0; + int loaded = 0; + const char *name = needed; + for (int i = 0; i < count; i++, name += strlen(name) + 1) { + if (strchr(name, '/')) continue; + void *already = orig_dlopen(name, RTLD_LAZY | RTLD_NOLOAD); + if (already) { + dlclose(already); + continue; + } + for (const char *p = dirs; *p;) { + const char *end = strchr(p, ':'); + size_t len = end ? (size_t)(end - p) : strlen(p); + char candidate[PATH_MAX]; + if (len + 1 + strlen(name) < sizeof(candidate)) { + memcpy(candidate, p, len); + candidate[len] = '/'; + strcpy(candidate + len + 1, name); + int probe_fd = (int)syscall(SYS_openat, AT_FDCWD, candidate, O_PATH | O_CLOEXEC); + if (probe_fd >= 0) { + syscall(SYS_close, probe_fd); + dlopen_preload_deps(candidate, orig_dlopen, depth + 1); + if (orig_dlopen(candidate, RTLD_LAZY | RTLD_LOCAL)) loaded++; + break; + } + } + if (!end) break; + p = end + 1; + } + } + return loaded; +} + +// ---- /etc/ld.so.conf ------------------------------------------------------------------------- +// The library directories of /etc/ld.so.conf and the files it includes (/etc/ld.so.conf.d/*.conf), +// mapped into the rootfs. On Linux, ldconfig turns them into /etc/ld.so.cache, which ld.so reads. +// Here ld.so would read Android's /etc/ld.so.cache, and the paths in the cache are not mapped into +// the rootfs either, so the exec hands the directories to ld.so in --library-path instead. They are +// read again only when /etc/ld.so.conf, /etc/ld.so.conf.d or /etc/ld.so.cache changes: a .conf file +// added or removed counts at once, an edit of one after ldconfig, as on Linux. +#define LDCONF_MAX_DEPTH 4 +#define LDCONF_MAX_INCLUDES 64 + +struct ldconf_sig { + int64_t t[6]; // mtimes (seconds, nanoseconds) of ld.so.conf, ld.so.conf.d and ld.so.cache; 0 if missing +}; + +static struct { + int valid; + struct ldconf_sig sig; + char root[PATH_MAX]; + char dirs[PATH_MAX * 2]; +} g_ldconf; +static pthread_mutex_t g_ldconf_lock = PTHREAD_MUTEX_INITIALIZER; + +// fstatat() behind the hook's stat emulation (which would map the hard link table in a child that is +// about to exec): only the times are wanted. +static int ldconf_stat_time(const char *real, int64_t *sec, int64_t *nsec) { + static int (*f)(int, const char *, struct stat64 *, int) = NULL; + int (*fn)(int, const char *, struct stat64 *, int) = __atomic_load_n(&f, __ATOMIC_RELAXED); + if (!fn) { + fn = (int (*)(int, const char *, struct stat64 *, int))dlsym(RTLD_NEXT, "fstatat64"); + if (!fn) return -1; + __atomic_store_n(&f, fn, __ATOMIC_RELAXED); + } + struct stat64 st; + if (fn(AT_FDCWD, real, &st, 0) != 0) return -1; + *sec = (int64_t)st.st_mtim.tv_sec; + *nsec = (int64_t)st.st_mtim.tv_nsec; + return 0; +} + +static void ldconf_signature(struct ldconf_sig *sig) { + static const char *const files[3] = { "/etc/ld.so.conf", "/etc/ld.so.conf.d", "/etc/ld.so.cache" }; + for (int i = 0; i < 3; i++) { + char path[PATH_MAX]; + size_t n = ddnatv_append(path, sizeof(path), 0, g_ddnatv_root, g_ddnatv_root_len); + ddnatv_append(path, sizeof(path), n, files[i], strlen(files[i])); + if (ldconf_stat_time(path, &sig->t[i * 2], &sig->t[i * 2 + 1]) != 0) { + sig->t[i * 2] = 0; + sig->t[i * 2 + 1] = 0; + } + } +} + +// The whole of the rootfs file `logical` (an absolute path inside Ubuntu) into buf, NUL-terminated. +static ssize_t ldconf_read(const char *logical, char *buf, size_t bufsz) { + char rw_buf[PATH_MAX]; + const char *real = rewrite_path_in_root(logical, rw_buf, sizeof(rw_buf)); + int fd = (int)syscall(SYS_openat, AT_FDCWD, real, O_RDONLY | O_CLOEXEC); + if (fd < 0) return -1; + size_t got = 0; + while (got + 1 < bufsz) { + ssize_t n = (ssize_t)syscall(SYS_read, fd, buf + got, bufsz - 1 - got); + if (n <= 0) break; + got += (size_t)n; + } + syscall(SYS_close, fd); + buf[got] = '\0'; + return (ssize_t)got; +} + +static int ldconf_name_cmp(const void *a, const void *b) { + return strcmp(*(const char *const *)a, *(const char *const *)b); +} + +static void ldconf_parse_file(const char *logical, char *out, size_t outsz, size_t *used, int depth, int *includes); + +// include PATTERN: the files of one directory that match a shell pattern, in name order (as glob()). +// Patterns in the directory part are not supported (Ubuntu uses /etc/ld.so.conf.d/*.conf). +static void ldconf_include(const char *pattern, char *out, size_t outsz, size_t *used, int depth, int *includes) { + const char *slash = strrchr(pattern, '/'); + if (!slash || slash == pattern) return; + size_t dir_len = (size_t)(slash - pattern); + if (dir_len >= PATH_MAX || memchr(pattern, '*', dir_len) || memchr(pattern, '?', dir_len) || memchr(pattern, '[', dir_len)) return; + char dir[PATH_MAX]; + memcpy(dir, pattern, dir_len); + dir[dir_len] = '\0'; + const char *name_pattern = slash + 1; + if (!strpbrk(name_pattern, "*?[")) { + ldconf_parse_file(pattern, out, outsz, used, depth + 1, includes); + return; + } + char rw_buf[PATH_MAX]; + const char *real = rewrite_path_in_root(dir, rw_buf, sizeof(rw_buf)); + int fd = (int)syscall(SYS_openat, AT_FDCWD, real, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if (fd < 0) return; + char names_buf[4096]; + char *names[LDCONF_MAX_INCLUDES]; + int count = 0; + size_t names_used = 0; + char dents[4096]; + for (;;) { + long n = syscall(SYS_getdents64, fd, dents, sizeof(dents)); + if (n <= 0) break; + for (long at = 0; at < n;) { + unsigned short reclen; + memcpy(&reclen, dents + at + 16, sizeof(reclen)); + const char *name = dents + at + 19; + if (reclen == 0) break; + size_t len = strnlen(name, (size_t)reclen - 19); + if (count < LDCONF_MAX_INCLUDES && names_used + len + 1 <= sizeof(names_buf) && + fnmatch(name_pattern, name, FNM_PERIOD) == 0) { + memcpy(names_buf + names_used, name, len + 1); + names[count++] = names_buf + names_used; + names_used += len + 1; + } + at += reclen; + } + } + syscall(SYS_close, fd); + qsort(names, (size_t)count, sizeof(names[0]), ldconf_name_cmp); + for (int i = 0; i < count; i++) { + char file[PATH_MAX]; + if (dir_len + 1 + strlen(names[i]) >= sizeof(file)) continue; + snprintf(file, sizeof(file), "%s/%s", dir, names[i]); + ldconf_parse_file(file, out, outsz, used, depth + 1, includes); + } +} + +// One ld.so.conf-style file: a directory per line, "include PATTERN..." (relative to the file's own +// directory), "#" comments, and the old "hwcap" lines, which are skipped as glibc does. +static void ldconf_parse_file(const char *logical, char *out, size_t outsz, size_t *used, int depth, int *includes) { + if (depth > LDCONF_MAX_DEPTH || ++*includes > LDCONF_MAX_INCLUDES) return; + char text[8192]; + if (ldconf_read(logical, text, sizeof(text)) < 0) return; + for (char *line = text; line && *line;) { + char *next = strchr(line, '\n'); + if (next) *next++ = '\0'; + char *hash = strchr(line, '#'); + if (hash) *hash = '\0'; + while (*line == ' ' || *line == '\t' || *line == '\r') line++; + size_t len = strlen(line); + while (len > 0 && (line[len - 1] == ' ' || line[len - 1] == '\t' || line[len - 1] == '\r')) line[--len] = '\0'; + if (len == 0) { + line = next; + continue; + } + if (strncmp(line, "include", 7) == 0 && (line[7] == ' ' || line[7] == '\t')) { + for (char *p = line + 8; *p;) { + while (*p == ' ' || *p == '\t') p++; + if (!*p) break; + char *end = p; + while (*end && *end != ' ' && *end != '\t') end++; + char saved = *end; + *end = '\0'; + char pattern[PATH_MAX]; + if (p[0] == '/') { + snprintf(pattern, sizeof(pattern), "%s", p); + } else { + const char *slash = strrchr(logical, '/'); + int base = slash ? (int)(slash - logical) : 0; + snprintf(pattern, sizeof(pattern), "%.*s/%s", base, logical, p); + } + ldconf_include(pattern, out, outsz, used, depth, includes); + *end = saved; + p = end; + } + } else if (strncmp(line, "hwcap", 5) == 0 && (line[5] == ' ' || line[5] == '\t')) { + // ignored by ldconfig since glibc 2.37 + } else if (line[0] == '/') { + // "DIR" or the old "DIR=TYPE" / "DIR TYPE" + size_t dlen = strcspn(line, "= \t"); + while (dlen > 1 && line[dlen - 1] == '/') dlen--; + if (dlen > 0 && dlen < PATH_MAX) { + char dir[PATH_MAX]; + memcpy(dir, line, dlen); + dir[dlen] = '\0'; + char rw_buf[PATH_MAX]; + const char *rw = rewrite_path_in_root(dir, rw_buf, sizeof(rw_buf)); + if (strcmp(rw, dir) != 0) *used = append_lib_dir(out, outsz, *used, rw, strlen(rw)); + } + } + line = next; + } +} + +static void ldconf_parse(char *out, size_t outsz) { + out[0] = '\0'; + size_t used = 0; + int includes = 0; + ldconf_parse_file("/etc/ld.so.conf", out, outsz, &used, 0, &includes); +} + +// The ld.so.conf directories into out (':'-separated, "" when there are none). Uses the copy of an +// earlier call while the files have not changed: the parent finds it when it first forks, and its +// children only compare the three times. Never waits for the lock: a child forked while another +// thread held it reads the files itself. +static void ldconf_dirs(char *out, size_t outsz) { + if (!out || outsz == 0) return; + out[0] = '\0'; + if (g_ddnatv_root[0] == '\0') return; + struct ldconf_sig sig; + ldconf_signature(&sig); + if (sig.t[0] == 0 && sig.t[1] == 0) return; // neither ld.so.conf nor ld.so.conf.d + if (pthread_mutex_trylock(&g_ldconf_lock) != 0) { + ldconf_parse(out, outsz); + return; + } + if (!g_ldconf.valid || memcmp(&g_ldconf.sig, &sig, sizeof(sig)) != 0 || strcmp(g_ldconf.root, g_ddnatv_root) != 0) { + ldconf_parse(g_ldconf.dirs, sizeof(g_ldconf.dirs)); + g_ldconf.sig = sig; + snprintf(g_ldconf.root, sizeof(g_ldconf.root), "%s", g_ddnatv_root); + g_ldconf.valid = 1; + } + snprintf(out, outsz, "%s", g_ldconf.dirs); + pthread_mutex_unlock(&g_ldconf_lock); +} + +// The --library-path ld.so is given for the rootfs program `exe`, in the order ld.so searches on +// Linux: the program's RPATH, the caller's LD_LIBRARY_PATH (the rootfs paths in it mapped), its +// RUNPATH, the directories of /etc/ld.so.conf (ld.so's cache), and the rootfs defaults last. The native userland's +// shells export the defaults in LD_LIBRARY_PATH too, where Linux has none; when there is anything to +// put before them they move to the end, so that a library the program keeps for itself wins over +// one of the same name in /usr/lib. Runs after the hook's setup, so the +// rewrites are those of rewrite_path(). +static void build_ld_library_path(char *const envp[], const char *runpath, int is_rpath, const char *exe, + char *out_buf, size_t out_size) { + if (!out_buf || out_size == 0) return; + out_buf[0] = '\0'; + + const char *caller_ld = NULL; + if (envp) { + for (int i = 0; envp[i] != NULL; i++) { + if (envp[i][0] == 'L' && strncmp(envp[i], "LD_LIBRARY_PATH=", 16) == 0) { + caller_ld = envp[i] + 16; + break; + } + } + } + if (!caller_ld) { + caller_ld = getenv("LD_LIBRARY_PATH"); + } + int has_runpath = runpath && runpath[0]; + char conf[PATH_MAX * 2]; + ldconf_dirs(conf, sizeof(conf)); + // The rootfs defaults that The native userland's shells export in LD_LIBRARY_PATH go to the end, after the + // program's and ld.so.conf's own directories, as on Linux (where LD_LIBRARY_PATH has none of them) + int move_defaults = has_runpath || conf[0] != '\0'; + + size_t used = 0; + if (has_runpath && is_rpath) used = append_runpath_dirs(out_buf, out_size, used, runpath, exe); + + // The caller's directories (those inside the rootfs rewritten), written straight into out_buf: + // this runs in nearly every child a shell forks, where two 8 KB copies of the list on the stack + // were pages more to copy before the exec. + size_t cap = used + PATH_MAX * 2 < out_size ? used + PATH_MAX * 2 : out_size; // the caller's part, as before + for (const char *p = caller_ld; p && *p;) { + const char *end = strchr(p, ':'); + size_t len = end ? (size_t)(end - p) : strlen(p); + if (len > 0 && len < PATH_MAX) { + char tok[PATH_MAX]; + memcpy(tok, p, len); + tok[len] = '\0'; + char rw_buf[PATH_MAX]; + const char *rw = (tok[0] == '/') ? rewrite_path_in_root(tok, rw_buf, sizeof(rw_buf)) : tok; + size_t rw_len = strlen(rw); + size_t need = (used > 0 ? 1 : 0) + rw_len; + if (move_defaults && is_rootfs_default_lib_dir(rw, rw_len)) { + // after the program's own directories, with the other defaults + } else if (used + need < cap) { + if (used > 0) out_buf[used++] = ':'; + memcpy(out_buf + used, rw, rw_len); + used += rw_len; + out_buf[used] = '\0'; + } + } + if (!end) break; + p = end + 1; + } + if (has_runpath && !is_rpath) used = append_runpath_dirs(out_buf, out_size, used, runpath, exe); + // ld.so.conf, as ld.so's cache: after LD_LIBRARY_PATH and RUNPATH, before the default directories + for (const char *p = conf; *p;) { + const char *end = strchr(p, ':'); + size_t len = end ? (size_t)(end - p) : strlen(p); + used = append_lib_dir(out_buf, out_size, used, p, len); + if (!end) break; + p = end + 1; + } + append_rootfs_lib_dirs_missing_from(out_buf, out_size, used, used > 0, out_buf); +} + +// fstat() without the hook's stat emulation: the probe wants the mode bits only, and the +// emulation would map the hard link table and look up the app's ids in a child that is about to +// exec (new pages there, each a copy-on-write fault). Resolved by prepare_exec_in_parent() when the +// process forks, so that its children find it set. +static int (*g_probe_fstat)(int, struct stat *) = NULL; + +static int probe_fstat(int fd, struct stat *st) { + int (*f)(int, struct stat *) = __atomic_load_n(&g_probe_fstat, __ATOMIC_RELAXED); + if (!f) { + f = (int (*)(int, struct stat *))dlsym(RTLD_NEXT, "fstat"); + if (!f) return fstat(fd, st); + __atomic_store_n(&g_probe_fstat, f, __ATOMIC_RELAXED); + } + return f(fd, st); +} + +static void probe_exec_file(const char *path, struct exec_probe *p) { + p->hdr[0] = '\0'; + p->n = -1; + p->is_elf = 0; + p->is_dynamic = 0; + p->android_interp = 0; + p->e_machine = 0; + p->mode = 0; + p->runpath[0] = '\0'; + p->runpath_is_rpath = 0; + if (!path || path[0] == '\0') return; + int fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd < 0) return; + ssize_t n = read(fd, p->hdr, sizeof(p->hdr) - 1); + p->n = n; + p->hdr[n > 0 ? n : 0] = '\0'; + const unsigned char *h = (const unsigned char *)p->hdr; + if (n >= 4 && h[0] == 0x7f && h[1] == 'E' && h[2] == 'L' && h[3] == 'F') { + p->is_elf = 1; + if (n >= 20) p->e_machine = (uint16_t)(h[18] | (h[19] << 8)); + int dynamic = elf_interp_in_buffer(h, n); + if (dynamic < 0) dynamic = check_elf_dynamic(fd, h, n); + p->is_dynamic = dynamic; + if (dynamic && p->e_machine == 183 /* EM_AARCH64 */) p->android_interp = elf_interp_is_android(fd, h, n); + if (dynamic && !p->android_interp) p->runpath_is_rpath = elf_read_runpath(fd, h, n, p->runpath, sizeof(p->runpath)) == 2; + struct stat st; + if (dynamic && probe_fstat(fd, &st) == 0) p->mode = st.st_mode; + } + close(fd); +} + +static int is_ld_linux(const char *path) { + if (!path) return 0; + const char *base = strrchr(path, '/'); + base = base ? base + 1 : path; + return (strncmp(base, "ld-linux", 8) == 0 || strncmp(base, "ld-2.", 5) == 0); +} + +// Splits a "#!" line (NUL-terminated `hdr`, modified in place) into the interpreter and its +// optional argument, the way execve() always has. +static void parse_shebang(char *hdr, char **interp, char **interp_arg) { + char *newline = strchr(hdr, '\n'); + if (newline) *newline = '\0'; + char *p = hdr + 2; + while (*p == ' ' || *p == '\t') p++; + char *arg = strchr(p, ' '); + if (arg) { + *arg = '\0'; + arg++; + while (*arg == ' ' || *arg == '\t') arg++; + } + *interp = p; + *interp_arg = arg; +} + +// Interpreters outside the rootfs (Android's /system/bin/sh) run with Android's environment. +static int is_host_interpreter(const char *interp) { + return strncmp(interp, "/system", 7) == 0 || + (g_ddnatv_root[0] != '\0' && strncmp(interp, g_ddnatv_root, g_ddnatv_root_len) != 0); +} + +// argv for running `script` with its interpreter: interp [arg] script args... (the script's own +// argv[0] is dropped, as the kernel does). free() the result. +static char **build_script_argv(const char *interp, const char *interp_arg, const char *script, char *const argv[]) { + int argc = 0; + while (argv && argv[argc]) argc++; + int has_arg = (interp_arg && *interp_arg) ? 1 : 0; + char **v = (char **)calloc(argc + has_arg + 3, sizeof(char *)); + if (!v) return NULL; + int n = 0; + v[n++] = (char *)interp; + if (has_arg) v[n++] = (char *)interp_arg; + v[n++] = (char *)script; + for (int i = 1; i < argc; i++) v[n++] = argv[i]; + v[n] = NULL; + return v; +} + +// argv for "ld.so [--preload hook] --library-path dirs --argv0 name program args...". +// free() the result; the strings stay owned by the caller. +static char **build_ldso_argv(const char *ld_so, const char *hook, const char *lib_path, + const char *program, const char *prog_name, char *const argv[]) { + int argc = 0; + while (argv && argv[argc]) argc++; + char **v = (char **)calloc(argc + 16, sizeof(char *)); + if (!v) return NULL; + int n = 0; + v[n++] = (char *)ld_so; + if (hook) { + v[n++] = (char *)"--preload"; + v[n++] = (char *)hook; + } + v[n++] = (char *)"--library-path"; + v[n++] = (char *)lib_path; + v[n++] = (char *)"--argv0"; + v[n++] = (char *)((argc > 0 && argv[0]) ? argv[0] : prog_name); + v[n++] = (char *)program; + for (int i = 1; i < argc; i++) v[n++] = argv[i]; + v[n] = NULL; + return v; +} + +// POSIX_SPAWN_RESETIDS makes glibc's spawn child call setresuid() and setresgid() after it has reset +// every signal handler to its default, which includes the SIGSYS handler that emulates the calls +// Android's seccomp filter blocks, so the child died of SIGSYS (GNU make asks for the flag on every +// command it starts). The native userland shows one fixed user id and never runs set-user-id programs: there is +// no id to reset, so the flag is left out and everything else is passed on. +static const posix_spawnattr_t *spawn_attr_for_android(const posix_spawnattr_t *attrp, posix_spawnattr_t *copy) { + short flags = 0; + if (!attrp || posix_spawnattr_getflags(attrp, &flags) != 0 || !(flags & POSIX_SPAWN_RESETIDS)) return attrp; + memcpy(copy, attrp, sizeof(*copy)); + posix_spawnattr_setflags(copy, (short)(flags & ~POSIX_SPAWN_RESETIDS)); + return copy; +} diff --git a/tools/nativeguest/hook/fileops.c b/tools/nativeguest/hook/fileops.c new file mode 100644 index 0000000000..9d0ff7cf9d --- /dev/null +++ b/tools/nativeguest/hook/fileops.c @@ -0,0 +1,805 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Other file operations: access, chmod, unlink, mkdir, rename, readlink, extended attributes, +// links, times, truncate, statfs, the working directory, realpath and scandir. + +#ifndef DDNATV_HOST_TEST + +// Hook faccessat +int faccessat(int dirfd, const char *pathname, int mode, int flags) { + init_ddnatv_hook(); + if (!pathname) { + errno = EFAULT; + return -1; + } + DDNATV_AT_REMAP(dirfd, pathname); + + struct stat st; + int stat_flags = (flags & AT_SYMLINK_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0; + if (fstatat(dirfd, pathname, &st, stat_flags) != 0) { + return -1; + } + + if (mode == F_OK) { + return 0; + } + + if (mode & X_OK) { + if (S_ISDIR(st.st_mode)) return 0; + if ((st.st_mode & 0111) != 0) return 0; + if (strstr(pathname, "/bin/") || strstr(pathname, "/sbin/")) return 0; + errno = EACCES; + return -1; + } + + return 0; +} + +int faccessat2(int dirfd, const char *pathname, int mode, int flags) { + return faccessat(dirfd, pathname, mode, flags); +} + +// Hook access +int access(const char *pathname, int mode) { + return faccessat(AT_FDCWD, pathname, mode, 0); +} + +// Hook euidaccess and eaccess +int euidaccess(const char *pathname, int mode) { + return faccessat(AT_FDCWD, pathname, mode, AT_EACCESS); +} + +int eaccess(const char *pathname, int mode) { + return faccessat(AT_FDCWD, pathname, mode, AT_EACCESS); +} + +// Hook chmod +int chmod(const char *pathname, mode_t mode) { + static int (*orig_chmod)(const char *, mode_t) = NULL; + if (!orig_chmod) orig_chmod = (int (*)(const char *, mode_t))dlsym(RTLD_NEXT, "chmod"); + if (!orig_chmod) return -1; + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(pathname, buf, sizeof(buf)); + int ret = orig_chmod(target, mode); + if (ret != 0 && ((mode & 06000) != 0 || errno == EPERM || errno == EACCES || errno == ENOSYS)) { + ret = orig_chmod(target, mode & 01777); + } + if (ret != 0 && (errno == EPERM || errno == EACCES)) { + errno = 0; + return 0; + } + return ret; +} + +// Hook fchmod +int fchmod(int fd, mode_t mode) { + static int (*orig_fchmod)(int, mode_t) = NULL; + if (!orig_fchmod) orig_fchmod = (int (*)(int, mode_t))dlsym(RTLD_NEXT, "fchmod"); + if (!orig_fchmod) return -1; + int ret = orig_fchmod(fd, mode); + if (ret != 0 && ((mode & 06000) != 0 || errno == EPERM || errno == EACCES || errno == ENOSYS)) { + ret = orig_fchmod(fd, mode & 01777); + } + if (ret != 0 && (errno == EPERM || errno == EACCES)) { + errno = 0; + return 0; + } + return ret; +} + +// Hook fchmodat +int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags) { + static int (*orig_fchmodat)(int, const char *, mode_t, int) = NULL; + if (!orig_fchmodat) orig_fchmodat = (int (*)(int, const char *, mode_t, int))dlsym(RTLD_NEXT, "fchmodat"); + if (!orig_fchmodat) return -1; + DDNATV_AT_REMAP(dirfd, pathname); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + int ret = orig_fchmodat(dirfd, target, mode, flags); + if (ret != 0 && ((mode & 06000) != 0 || errno == EPERM || errno == EACCES || errno == ENOSYS)) { + ret = orig_fchmodat(dirfd, target, mode & 01777, flags); + } + if (ret != 0 && (errno == EPERM || errno == EACCES)) { + errno = 0; + return 0; + } + return ret; +} + +// Hook unlink +int unlink(const char *pathname) { + static int (*orig_unlink)(const char *) = NULL; + if (!orig_unlink) orig_unlink = (int (*)(const char *))dlsym(RTLD_NEXT, "unlink"); + char buf[PATH_MAX]; + const char *target = rewrite_path(pathname, buf, sizeof(buf)); + unlink_mapped_unix_socket(target); + dev_t hl_dev = 0; + ino_t hl_ino = 0; + int hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, target, &hl_dev, &hl_ino); + int ret = orig_unlink(target); + if (ret != 0 && refused_in_repaired_dir(AT_FDCWD, target)) { + // The name could not be looked up while its directory was closed + hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, target, &hl_dev, &hl_ino); + ret = orig_unlink(target); + } + if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino); + return ret; +} + +// Hook unlinkat +int unlinkat(int dirfd, const char *pathname, int flags) { + static int (*orig_unlinkat)(int, const char *, int) = NULL; + if (!orig_unlinkat) orig_unlinkat = (int (*)(int, const char *, int))dlsym(RTLD_NEXT, "unlinkat"); + DDNATV_AT_REMAP(dirfd, pathname); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + dev_t hl_dev = 0; + ino_t hl_ino = 0; + int hardlinked = 0; + if (target) { + unlink_mapped_unix_socket(target); + hardlinked = lookup_emulated_hardlink_at(dirfd, target, &hl_dev, &hl_ino); + } + int ret = orig_unlinkat ? orig_unlinkat(dirfd, target, flags) : -1; + if (ret != 0 && orig_unlinkat && refused_in_repaired_dir(dirfd, target)) { + hardlinked = target ? lookup_emulated_hardlink_at(dirfd, target, &hl_dev, &hl_ino) : 0; + ret = orig_unlinkat(dirfd, target, flags); + } + if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino); + return ret; +} + +// Hook rmdir +int rmdir(const char *pathname) { + static int (*orig_rmdir)(const char *) = NULL; + if (!orig_rmdir) orig_rmdir = (int (*)(const char *))dlsym(RTLD_NEXT, "rmdir"); + char buf[PATH_MAX]; + const char *target = rewrite_path(pathname, buf, sizeof(buf)); + int ret = orig_rmdir(target); + if (ret != 0 && refused_in_repaired_dir(AT_FDCWD, target)) ret = orig_rmdir(target); + return ret; +} + +// glibc's remove() calls its internal unlinkat, which no preload can see: GLib's g_file_delete() +// got ENOENT for every guest path, and Flatpak could not replace an app's "current" link +int remove(const char *pathname) { + if (unlink(pathname) == 0) return 0; + if (errno != EISDIR) return -1; + return rmdir(pathname); +} + +// glob() and nftw() walk directories with glibc's internal calls too. glob() is given the hook's +// own through GLOB_ALTDIRFUNC; nftw() walks the mapped directory and its callback is given guest paths. +static int glob_alt_stat(const char *path, void *st) { return stat(path, (struct stat *)st); } +static int glob_alt_lstat(const char *path, void *st) { return lstat(path, (struct stat *)st); } +static void *glob_alt_opendir(const char *path) { return opendir(path); } +static void *glob_alt_readdir(void *dir) { return readdir((DIR *)dir); } +static void glob_alt_closedir(void *dir) { closedir((DIR *)dir); } + +int glob(const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob) { + static int (*orig_glob)(const char *, int, int (*)(const char *, int), glob_t *) = NULL; + if (!orig_glob) orig_glob = (int (*)(const char *, int, int (*)(const char *, int), glob_t *))dlsym(RTLD_NEXT, "glob"); + if (pglob && !(flags & GLOB_ALTDIRFUNC)) { + init_ddnatv_hook(); + pglob->gl_closedir = glob_alt_closedir; + pglob->gl_readdir = (struct dirent *(*)(void *))glob_alt_readdir; + pglob->gl_opendir = glob_alt_opendir; + pglob->gl_lstat = (int (*)(const char *, struct stat *))glob_alt_lstat; + pglob->gl_stat = (int (*)(const char *, struct stat *))glob_alt_stat; + flags |= GLOB_ALTDIRFUNC; + } + return orig_glob ? orig_glob(pattern, flags, errfunc, pglob) : GLOB_NOSPACE; +} + +int glob64(const char *pattern, int flags, int (*errfunc)(const char *, int), glob64_t *pglob) { + return glob(pattern, flags, errfunc, (glob_t *)pglob); +} + +static __thread struct { + const char *host, *guest; + size_t host_len, guest_len; + int (*nftw_fn)(const char *, const struct stat *, int, struct FTW *); + int (*ftw_fn)(const char *, const struct stat *, int); +} t_walk; + +static const char *walk_guest_path(const char *path, char *buf, size_t size, int *base) { + if (!t_walk.host || strncmp(path, t_walk.host, t_walk.host_len) != 0) return path; + const char *rest = path + t_walk.host_len; + if (*rest != '\0' && *rest != '/') return path; + // a walk of "/" gives "/etc", not "//etc" + size_t guest_len = (t_walk.guest_len == 1 && *rest == '/') ? 0 : t_walk.guest_len; + size_t n = ddnatv_append(buf, size, 0, t_walk.guest, guest_len); + ddnatv_append(buf, size, n, rest, strlen(rest)); + if (base) *base = *base - (int)t_walk.host_len + (int)guest_len; + return buf; +} + +static int walk_nftw_cb(const char *path, const struct stat *st, int type, struct FTW *ftwbuf) { + char buf[PATH_MAX]; + struct FTW guest_ftw = *ftwbuf; + return t_walk.nftw_fn(walk_guest_path(path, buf, sizeof(buf), &guest_ftw.base), st, type, &guest_ftw); +} + +static int walk_ftw_cb(const char *path, const struct stat *st, int type) { + char buf[PATH_MAX]; + return t_walk.ftw_fn(walk_guest_path(path, buf, sizeof(buf), NULL), st, type); +} + +int nftw(const char *dir, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fds, int flags) { + static int (*orig_nftw)(const char *, int (*)(const char *, const struct stat *, int, struct FTW *), int, int) = NULL; + if (!orig_nftw) orig_nftw = (int (*)(const char *, int (*)(const char *, const struct stat *, int, struct FTW *), int, int))dlsym(RTLD_NEXT, "nftw"); + if (!orig_nftw) { errno = ENOSYS; return -1; } + char buf[PATH_MAX]; + const char *target = rewrite_path(dir, buf, sizeof(buf)); + if (target == dir || !fn) return orig_nftw(dir, fn, fds, flags); + __typeof__(t_walk) saved = t_walk; + t_walk.host = target; t_walk.host_len = strlen(target); + while (t_walk.host_len > 1 && target[t_walk.host_len - 1] == '/') t_walk.host_len--; + t_walk.guest = dir; t_walk.guest_len = strlen(dir); + while (t_walk.guest_len > 1 && dir[t_walk.guest_len - 1] == '/') t_walk.guest_len--; + t_walk.nftw_fn = fn; + int ret = orig_nftw(target, walk_nftw_cb, fds, flags); + t_walk = saved; + return ret; +} + +int nftw64(const char *dir, int (*fn)(const char *, const struct stat64 *, int, struct FTW *), int fds, int flags) { + return nftw(dir, (int (*)(const char *, const struct stat *, int, struct FTW *))fn, fds, flags); +} + +int ftw(const char *dir, int (*fn)(const char *, const struct stat *, int), int fds) { + static int (*orig_ftw)(const char *, int (*)(const char *, const struct stat *, int), int) = NULL; + if (!orig_ftw) orig_ftw = (int (*)(const char *, int (*)(const char *, const struct stat *, int), int))dlsym(RTLD_NEXT, "ftw"); + if (!orig_ftw) { errno = ENOSYS; return -1; } + char buf[PATH_MAX]; + const char *target = rewrite_path(dir, buf, sizeof(buf)); + if (target == dir || !fn) return orig_ftw(dir, fn, fds); + __typeof__(t_walk) saved = t_walk; + t_walk.host = target; t_walk.host_len = strlen(target); + while (t_walk.host_len > 1 && target[t_walk.host_len - 1] == '/') t_walk.host_len--; + t_walk.guest = dir; t_walk.guest_len = strlen(dir); + while (t_walk.guest_len > 1 && dir[t_walk.guest_len - 1] == '/') t_walk.guest_len--; + t_walk.ftw_fn = fn; + int ret = orig_ftw(target, walk_ftw_cb, fds); + t_walk = saved; + return ret; +} + +int ftw64(const char *dir, int (*fn)(const char *, const struct stat64 *, int), int fds) { + return ftw(dir, (int (*)(const char *, const struct stat *, int))fn, fds); +} + +// The app is root for the programs it runs, and root enters and writes every directory whatever its +// mode; the app does not. dpkg makes each new directory with mode 0 and sets the mode a moment later, +// and a chmod that fails is reported as done (see chmod), which left directories nobody could put a +// file in: "Permission denied" in /usr/share/applications and others (#44). So a directory always +// keeps its owner's read, write and search rights. +#define NEW_DIR_OWNER_RIGHTS S_IRWXU + +// Hook mkdir +int mkdir(const char *pathname, mode_t mode) { + static int (*orig_mkdir)(const char *, mode_t) = NULL; + if (!orig_mkdir) orig_mkdir = (int (*)(const char *, mode_t))dlsym(RTLD_NEXT, "mkdir"); + char buf[PATH_MAX]; + const char *target = rewrite_path(pathname, buf, sizeof(buf)); + int ret = orig_mkdir(target, mode | NEW_DIR_OWNER_RIGHTS); + if (ret != 0 && refused_in_repaired_dir(AT_FDCWD, target)) ret = orig_mkdir(target, mode | NEW_DIR_OWNER_RIGHTS); + return ret; +} + +// Hook mkdirat +int mkdirat(int dirfd, const char *pathname, mode_t mode) { + static int (*orig_mkdirat)(int, const char *, mode_t) = NULL; + if (!orig_mkdirat) orig_mkdirat = (int (*)(int, const char *, mode_t))dlsym(RTLD_NEXT, "mkdirat"); + DDNATV_AT_REMAP(dirfd, pathname); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + int ret = orig_mkdirat(dirfd, target, mode | NEW_DIR_OWNER_RIGHTS); + if (ret != 0 && refused_in_repaired_dir(dirfd, target)) ret = orig_mkdirat(dirfd, target, mode | NEW_DIR_OWNER_RIGHTS); + return ret; +} + +static void ensure_dir_exists(const char *path) { + if (!path || path[0] == '\0') return; + char tmp[PATH_MAX]; + strncpy(tmp, path, sizeof(tmp) - 1); + tmp[sizeof(tmp) - 1] = '\0'; + for (char *p = tmp + 1; *p; p++) { + if (*p == '/') { + *p = '\0'; + mkdir(tmp, 0755); + *p = '/'; + } + } + mkdir(tmp, 0755); +} + +// Hook inotify_add_watch +int inotify_add_watch(int fd, const char *pathname, uint32_t mask) { + static int (*orig_inotify_add_watch)(int, const char *, uint32_t) = NULL; + if (!orig_inotify_add_watch) orig_inotify_add_watch = (int (*)(int, const char *, uint32_t))dlsym(RTLD_NEXT, "inotify_add_watch"); + if (!pathname) return -1; + char buf[PATH_MAX]; + const char *target = rewrite_path(pathname, buf, sizeof(buf)); + if (!orig_inotify_add_watch) return -1; + int wd = orig_inotify_add_watch(fd, target, mask); + if (wd < 0 && errno == ENOENT && (mask & IN_ONLYDIR)) { + ensure_dir_exists(target); + wd = orig_inotify_add_watch(fd, target, mask); + } + return wd; +} + +// Hook rename +int rename(const char *oldpath, const char *newpath) { + static int (*orig_rename)(const char *, const char *) = NULL; + if (!orig_rename) orig_rename = (int (*)(const char *, const char *))dlsym(RTLD_NEXT, "rename"); + char buf1[PATH_MAX]; + char buf2[PATH_MAX]; + const char *target_old = rewrite_path(oldpath, buf1, sizeof(buf1)); + const char *target_new = rewrite_path(newpath, buf2, sizeof(buf2)); + dev_t hl_dev = 0; + ino_t hl_ino = 0; + int hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, target_new, &hl_dev, &hl_ino); + int ret = orig_rename(target_old, target_new); + if (ret != 0 && errno == EACCES) { + // Both directories: the old name is taken out of one, the new put in the other + int refused = errno; + int fixed_old = refused_in_repaired_dir(AT_FDCWD, target_old); + errno = refused; + int fixed_new = refused_in_repaired_dir(AT_FDCWD, target_new); + if (fixed_old || fixed_new) { + hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, target_new, &hl_dev, &hl_ino); + ret = orig_rename(target_old, target_new); + } + } + if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino); + return ret; +} + +// Hook renameat +int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) { + static int (*orig_renameat)(int, const char *, int, const char *) = NULL; + if (!orig_renameat) orig_renameat = (int (*)(int, const char *, int, const char *))dlsym(RTLD_NEXT, "renameat"); + DDNATV_AT_REMAP_NAMED(olddirfd, oldpath, ddnatv_at_old); + DDNATV_AT_REMAP_NAMED(newdirfd, newpath, ddnatv_at_new); + char buf1[PATH_MAX]; + char buf2[PATH_MAX]; + const char *target_old = (oldpath && oldpath[0] == '/') ? rewrite_path(oldpath, buf1, sizeof(buf1)) : oldpath; + const char *target_new = (newpath && newpath[0] == '/') ? rewrite_path(newpath, buf2, sizeof(buf2)) : newpath; + dev_t hl_dev = 0; + ino_t hl_ino = 0; + int hardlinked = lookup_emulated_hardlink_at(newdirfd, target_new, &hl_dev, &hl_ino); + int ret = orig_renameat(olddirfd, target_old, newdirfd, target_new); + if (ret != 0 && errno == EACCES) { + int refused = errno; + int fixed_old = refused_in_repaired_dir(olddirfd, target_old); + errno = refused; + int fixed_new = refused_in_repaired_dir(newdirfd, target_new); + if (fixed_old || fixed_new) { + hardlinked = lookup_emulated_hardlink_at(newdirfd, target_new, &hl_dev, &hl_ino); + ret = orig_renameat(olddirfd, target_old, newdirfd, target_new); + } + } + if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino); + return ret; +} + +// Hook renameat2 +int renameat2(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, unsigned int flags) { + static int (*orig_renameat2)(int, const char *, int, const char *, unsigned int) = NULL; + if (!orig_renameat2) { + orig_renameat2 = (int (*)(int, const char *, int, const char *, unsigned int))dlsym(RTLD_NEXT, "renameat2"); + if (!orig_renameat2) return renameat(olddirfd, oldpath, newdirfd, newpath); + } + DDNATV_AT_REMAP_NAMED(olddirfd, oldpath, ddnatv_at_old); + DDNATV_AT_REMAP_NAMED(newdirfd, newpath, ddnatv_at_new); + char buf1[PATH_MAX]; + char buf2[PATH_MAX]; + const char *target_old = (oldpath && oldpath[0] == '/') ? rewrite_path(oldpath, buf1, sizeof(buf1)) : oldpath; + const char *target_new = (newpath && newpath[0] == '/') ? rewrite_path(newpath, buf2, sizeof(buf2)) : newpath; + dev_t hl_dev = 0; + ino_t hl_ino = 0; + // RENAME_EXCHANGE keeps both files, so nothing is replaced. + int hardlinked = !(flags & RENAME_EXCHANGE) && lookup_emulated_hardlink_at(newdirfd, target_new, &hl_dev, &hl_ino); + int ret = orig_renameat2(olddirfd, target_old, newdirfd, target_new, flags); + if (ret != 0 && errno == EACCES) { + int refused = errno; + int fixed_old = refused_in_repaired_dir(olddirfd, target_old); + errno = refused; + int fixed_new = refused_in_repaired_dir(newdirfd, target_new); + if (fixed_old || fixed_new) { + hardlinked = !(flags & RENAME_EXCHANGE) && lookup_emulated_hardlink_at(newdirfd, target_new, &hl_dev, &hl_ino); + ret = orig_renameat2(olddirfd, target_old, newdirfd, target_new, flags); + } + } + if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino); + return ret; +} + +// Hook readlink +ssize_t readlink(const char *pathname, char *buf, size_t bufsiz) { + if (is_proc_self_exe(pathname)) { + ssize_t exe_len = handle_proc_self_exe(buf, bufsiz); + if (exe_len >= 0) return exe_len; + } + static ssize_t (*orig_readlink)(const char *, char *, size_t) = NULL; + if (!orig_readlink) orig_readlink = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink"); + char pbuf[PATH_MAX]; + const char *target = rewrite_path(pathname, pbuf, sizeof(pbuf)); + ssize_t ret = orig_readlink ? orig_readlink(target, buf, bufsiz) : -1; + if (ret < 0 && errno == ENOENT) { + char alt[PATH_MAX]; + const char *retry = usrmerge_retry_path(target, alt, sizeof(alt)); + if (retry) ret = orig_readlink(retry, buf, bufsiz); + } + return postprocess_readlink(pathname, buf, ret, bufsiz); +} + +// Hook readlinkat +ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) { + static ssize_t (*orig_readlinkat)(int, const char *, char *, size_t) = NULL; + if (!orig_readlinkat) orig_readlinkat = (ssize_t (*)(int, const char *, char *, size_t))dlsym(RTLD_NEXT, "readlinkat"); + DDNATV_AT_REMAP(dirfd, pathname); + // after the remap: "proc/self/exe" relative to a descriptor for "/" is /proc/self/exe as well + if (is_proc_self_exe(pathname)) { + ssize_t exe_len = handle_proc_self_exe(buf, bufsiz); + if (exe_len >= 0) return exe_len; + } + char pbuf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, pbuf, sizeof(pbuf)) : pathname; + ssize_t ret = orig_readlinkat ? orig_readlinkat(dirfd, target, buf, bufsiz) : -1; + if (ret < 0 && errno == ENOENT && pathname && pathname[0] == '/') { + char alt[PATH_MAX]; + const char *retry = usrmerge_retry_path(target, alt, sizeof(alt)); + if (retry) ret = orig_readlinkat(dirfd, retry, buf, bufsiz); + } + return postprocess_readlink(pathname, buf, ret, bufsiz); +} + +// Fortified readlink variants used by dpkg / update-alternatives +ssize_t __readlink_chk(const char *pathname, char *buf, size_t bufsiz, size_t buflen) { + return readlink(pathname, buf, bufsiz); +} + +ssize_t __readlinkat_chk(int dirfd, const char *pathname, char *buf, size_t bufsiz, size_t buflen) { + return readlinkat(dirfd, pathname, buf, bufsiz); +} + +// Ubuntu's programs find Android's SELinux (enforcing) through libselinux and then ask its policy +// questions it has no answers for. The one that mattered: PAM's pam_rootok, which lets root +// change its own entries, asks "may this user be root" and was refused, so chfn, chsh and +// `adduser --gecos` (the installation of PostgreSQL) failed with "PAM: Authentication failure". +// Ubuntu does not use SELinux, so for its programs it is off, as its labels are hidden below. +int is_selinux_enabled(void) { return 0; } + +// Extended attributes take the same paths as stat(). Without these hooks coreutils read +// security.selinux with the Ubuntu path itself: `ls -l` printed an error line for every file +// Android lacks (/etc/os-release) and showed Android's own label for the ones it has +// (/etc/passwd). Ubuntu does not use SELinux, so its label is hidden altogether: `ls -l` looks as +// on Ubuntu and cp/tar/rsync do not try to copy Android's labels. +ssize_t getxattr(const char *path, const char *name, void *value, size_t size) { + static ssize_t (*orig_getxattr)(const char *, const char *, void *, size_t) = NULL; + if (!orig_getxattr) orig_getxattr = (ssize_t (*)(const char *, const char *, void *, size_t))dlsym(RTLD_NEXT, "getxattr"); + if (is_selinux_xattr(name)) { + errno = ENODATA; + return -1; + } + char pbuf[PATH_MAX]; + return orig_getxattr ? orig_getxattr(rewrite_path_follow(path, pbuf, sizeof(pbuf)), name, value, size) : -1; +} + +ssize_t lgetxattr(const char *path, const char *name, void *value, size_t size) { + static ssize_t (*orig_lgetxattr)(const char *, const char *, void *, size_t) = NULL; + if (!orig_lgetxattr) orig_lgetxattr = (ssize_t (*)(const char *, const char *, void *, size_t))dlsym(RTLD_NEXT, "lgetxattr"); + if (is_selinux_xattr(name)) { + errno = ENODATA; + return -1; + } + char pbuf[PATH_MAX]; + return orig_lgetxattr ? orig_lgetxattr(rewrite_path(path, pbuf, sizeof(pbuf)), name, value, size) : -1; +} + +ssize_t listxattr(const char *path, char *list, size_t size) { + static ssize_t (*orig_listxattr)(const char *, char *, size_t) = NULL; + if (!orig_listxattr) orig_listxattr = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "listxattr"); + char pbuf[PATH_MAX]; + ssize_t ret = orig_listxattr ? orig_listxattr(rewrite_path_follow(path, pbuf, sizeof(pbuf)), list, size) : -1; + return size > 0 ? drop_selinux_xattr(list, ret) : ret; +} + +ssize_t llistxattr(const char *path, char *list, size_t size) { + static ssize_t (*orig_llistxattr)(const char *, char *, size_t) = NULL; + if (!orig_llistxattr) orig_llistxattr = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "llistxattr"); + char pbuf[PATH_MAX]; + ssize_t ret = orig_llistxattr ? orig_llistxattr(rewrite_path(path, pbuf, sizeof(pbuf)), list, size) : -1; + return size > 0 ? drop_selinux_xattr(list, ret) : ret; +} + +ssize_t fgetxattr(int fd, const char *name, void *value, size_t size) { + static ssize_t (*orig_fgetxattr)(int, const char *, void *, size_t) = NULL; + if (!orig_fgetxattr) orig_fgetxattr = (ssize_t (*)(int, const char *, void *, size_t))dlsym(RTLD_NEXT, "fgetxattr"); + if (is_selinux_xattr(name)) { + errno = ENODATA; + return -1; + } + return orig_fgetxattr ? orig_fgetxattr(fd, name, value, size) : -1; +} + +ssize_t flistxattr(int fd, char *list, size_t size) { + static ssize_t (*orig_flistxattr)(int, char *, size_t) = NULL; + if (!orig_flistxattr) orig_flistxattr = (ssize_t (*)(int, char *, size_t))dlsym(RTLD_NEXT, "flistxattr"); + ssize_t ret = orig_flistxattr ? orig_flistxattr(fd, list, size) : -1; + return size > 0 ? drop_selinux_xattr(list, ret) : ret; +} + +int setxattr(const char *path, const char *name, const void *value, size_t size, int flags) { + static int (*orig_setxattr)(const char *, const char *, const void *, size_t, int) = NULL; + if (!orig_setxattr) orig_setxattr = (int (*)(const char *, const char *, const void *, size_t, int))dlsym(RTLD_NEXT, "setxattr"); + char pbuf[PATH_MAX]; + return orig_setxattr ? orig_setxattr(rewrite_path_follow(path, pbuf, sizeof(pbuf)), name, value, size, flags) : -1; +} + +int lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags) { + static int (*orig_lsetxattr)(const char *, const char *, const void *, size_t, int) = NULL; + if (!orig_lsetxattr) orig_lsetxattr = (int (*)(const char *, const char *, const void *, size_t, int))dlsym(RTLD_NEXT, "lsetxattr"); + char pbuf[PATH_MAX]; + return orig_lsetxattr ? orig_lsetxattr(rewrite_path(path, pbuf, sizeof(pbuf)), name, value, size, flags) : -1; +} + +int removexattr(const char *path, const char *name) { + static int (*orig_removexattr)(const char *, const char *) = NULL; + if (!orig_removexattr) orig_removexattr = (int (*)(const char *, const char *))dlsym(RTLD_NEXT, "removexattr"); + char pbuf[PATH_MAX]; + return orig_removexattr ? orig_removexattr(rewrite_path(path, pbuf, sizeof(pbuf)), name) : -1; +} + +int lremovexattr(const char *path, const char *name) { + static int (*orig_lremovexattr)(const char *, const char *) = NULL; + if (!orig_lremovexattr) orig_lremovexattr = (int (*)(const char *, const char *))dlsym(RTLD_NEXT, "lremovexattr"); + char pbuf[PATH_MAX]; + return orig_lremovexattr ? orig_lremovexattr(rewrite_path(path, pbuf, sizeof(pbuf)), name) : -1; +} + +// Hook symlink +int symlink(const char *target, const char *linkpath) { + static int (*orig_symlink)(const char *, const char *) = NULL; + if (!orig_symlink) orig_symlink = (int (*)(const char *, const char *))dlsym(RTLD_NEXT, "symlink"); + char pbuf[PATH_MAX], tbuf[PATH_MAX]; + const char *newlink = rewrite_path(linkpath, pbuf, sizeof(pbuf)); + const char *newtarget = (target && target[0] == '/') ? rewrite_path(target, tbuf, sizeof(tbuf)) : target; + int ret = orig_symlink ? orig_symlink(newtarget, newlink) : -1; + if (ret != 0 && orig_symlink && refused_in_repaired_dir(AT_FDCWD, newlink)) ret = orig_symlink(newtarget, newlink); + return ret; +} + +// Hook symlinkat +int symlinkat(const char *target, int newdirfd, const char *linkpath) { + static int (*orig_symlinkat)(const char *, int, const char *) = NULL; + if (!orig_symlinkat) orig_symlinkat = (int (*)(const char *, int, const char *))dlsym(RTLD_NEXT, "symlinkat"); + DDNATV_AT_REMAP(newdirfd, linkpath); + char pbuf[PATH_MAX], tbuf[PATH_MAX]; + const char *newlink = (linkpath && linkpath[0] == '/') ? rewrite_path(linkpath, pbuf, sizeof(pbuf)) : linkpath; + const char *newtarget = (target && target[0] == '/') ? rewrite_path(target, tbuf, sizeof(tbuf)) : target; + int ret = orig_symlinkat ? orig_symlinkat(newtarget, newdirfd, newlink) : -1; + if (ret != 0 && orig_symlinkat && refused_in_repaired_dir(newdirfd, newlink)) ret = orig_symlinkat(newtarget, newdirfd, newlink); + return ret; +} + +// Hook linkat +int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) { + static int (*orig_linkat)(int, const char *, int, const char *, int) = NULL; + if (!orig_linkat) orig_linkat = (int (*)(int, const char *, int, const char *, int))dlsym(RTLD_NEXT, "linkat"); + DDNATV_AT_REMAP_NAMED(olddirfd, oldpath, ddnatv_at_old); + DDNATV_AT_REMAP_NAMED(newdirfd, newpath, ddnatv_at_new); + char obuf[PATH_MAX], nbuf[PATH_MAX]; + const char *rold = (oldpath && oldpath[0] == '/') ? rewrite_path(oldpath, obuf, sizeof(obuf)) : oldpath; + const char *rnew = (newpath && newpath[0] == '/') ? rewrite_path(newpath, nbuf, sizeof(nbuf)) : newpath; + int ret = orig_linkat ? orig_linkat(olddirfd, rold, newdirfd, rnew, flags) : -1; + if (ret != 0 && orig_linkat && refused_in_repaired_dir(newdirfd, rnew)) ret = orig_linkat(olddirfd, rold, newdirfd, rnew, flags); + if (ret != 0 && (errno == EXDEV || errno == EPERM || errno == EACCES || errno == ENOTSUP || errno == ENOSYS)) { + ret = ddnatv_emulate_hardlinkat(olddirfd, rold, newdirfd, rnew); + } + return ret; +} + +// Hook link +int link(const char *oldpath, const char *newpath) { + return linkat(AT_FDCWD, oldpath, AT_FDCWD, newpath, 0); +} + +// Hook utime / utimes / lutimes / futimesat / utimensat +int utime(const char *filename, const struct utimbuf *times) { + static int (*orig_utime)(const char *, const struct utimbuf *) = NULL; + if (!orig_utime) orig_utime = (int (*)(const char *, const struct utimbuf *))dlsym(RTLD_NEXT, "utime"); + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(filename, buf, sizeof(buf)); + return orig_utime ? orig_utime(target, times) : -1; +} + +int utimes(const char *filename, const struct timeval times[2]) { + static int (*orig_utimes)(const char *, const struct timeval[2]) = NULL; + if (!orig_utimes) orig_utimes = (int (*)(const char *, const struct timeval[2]))dlsym(RTLD_NEXT, "utimes"); + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(filename, buf, sizeof(buf)); + return orig_utimes ? orig_utimes(target, times) : -1; +} + +int lutimes(const char *filename, const struct timeval times[2]) { + static int (*orig_lutimes)(const char *, const struct timeval[2]) = NULL; + if (!orig_lutimes) orig_lutimes = (int (*)(const char *, const struct timeval[2]))dlsym(RTLD_NEXT, "lutimes"); + char buf[PATH_MAX]; + const char *target = rewrite_path(filename, buf, sizeof(buf)); + return orig_lutimes ? orig_lutimes(target, times) : -1; +} + +int futimesat(int dirfd, const char *pathname, const struct timeval times[2]) { + static int (*orig_futimesat)(int, const char *, const struct timeval[2]) = NULL; + if (!orig_futimesat) orig_futimesat = (int (*)(int, const char *, const struct timeval[2]))dlsym(RTLD_NEXT, "futimesat"); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + return orig_futimesat ? orig_futimesat(dirfd, target, times) : -1; +} + +int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags) { + static int (*orig_utimensat)(int, const char *, const struct timespec[2], int) = NULL; + if (!orig_utimensat) orig_utimensat = (int (*)(int, const char *, const struct timespec[2], int))dlsym(RTLD_NEXT, "utimensat"); + DDNATV_AT_REMAP(dirfd, pathname); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + return orig_utimensat ? orig_utimensat(dirfd, target, times, flags) : -1; +} + +// Hook truncate +int truncate(const char *path, off_t length) { + static int (*orig_truncate)(const char *, off_t) = NULL; + if (!orig_truncate) orig_truncate = (int (*)(const char *, off_t))dlsym(RTLD_NEXT, "truncate"); + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(path, buf, sizeof(buf)); + return orig_truncate(target, length); +} + +// Hook statfs / statfs64 / statvfs / statvfs64 +int statfs(const char *path, struct statfs *buf) { + static int (*orig_statfs)(const char *, struct statfs *) = NULL; + if (!orig_statfs) orig_statfs = (int (*)(const char *, struct statfs *))dlsym(RTLD_NEXT, "statfs"); + char pbuf[PATH_MAX]; + const char *target = rewrite_path_follow(path, pbuf, sizeof(pbuf)); + return orig_statfs ? orig_statfs(target, buf) : -1; +} + +int statvfs(const char *path, struct statvfs *buf) { + static int (*orig_statvfs)(const char *, struct statvfs *) = NULL; + if (!orig_statvfs) orig_statvfs = (int (*)(const char *, struct statvfs *))dlsym(RTLD_NEXT, "statvfs"); + char pbuf[PATH_MAX]; + const char *target = rewrite_path_follow(path, pbuf, sizeof(pbuf)); + return orig_statvfs ? orig_statvfs(target, buf) : -1; +} + +// Hook chdir +int chdir(const char *path) { + static int (*orig_chdir)(const char *) = NULL; + if (!orig_chdir) orig_chdir = (int (*)(const char *))dlsym(RTLD_NEXT, "chdir"); + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(path, buf, sizeof(buf)); + return orig_chdir ? orig_chdir(target) : -1; +} + +// Hook fchdir +int fchdir(int fd) { + static int (*orig_fchdir)(int) = NULL; + if (!orig_fchdir) orig_fchdir = (int (*)(int))dlsym(RTLD_NEXT, "fchdir"); + return orig_fchdir ? orig_fchdir(fd) : -1; +} + +// Hook getcwd +char *getcwd(char *buf, size_t size) { + static char *(*orig_getcwd)(char *, size_t) = NULL; + if (!orig_getcwd) orig_getcwd = (char *(*)(char *, size_t))dlsym(RTLD_NEXT, "getcwd"); + char *res = orig_getcwd ? orig_getcwd(buf, size) : NULL; + if (res) { + size_t len = strlen(res); + guest_path_of_mount_source(res, len, buf ? size : len + 1); + } + return res; +} + +extern void __chk_fail(void) __attribute__((__noreturn__)); +char *__getcwd_chk(char *buf, size_t size, size_t buflen) { + if (size > buflen) __chk_fail(); + return getcwd(buf, size); +} + +// Hook realpath and canonicalize_file_name +char *realpath(const char *path, char *resolved_path) { + static char *(*orig_realpath)(const char *, char *) = NULL; + if (!orig_realpath) orig_realpath = (char *(*)(const char *, char *))dlsym(RTLD_NEXT, "realpath"); + if (!path) { + errno = EINVAL; + return NULL; + } + init_ddnatv_hook(); + if (is_proc_self_exe(path)) { + char tmp[PATH_MAX]; + ssize_t n = handle_proc_self_exe(tmp, sizeof(tmp) - 1); + if (n > 0) { + tmp[n] = '\0'; + if (resolved_path) { + strncpy(resolved_path, tmp, PATH_MAX - 1); + resolved_path[PATH_MAX - 1] = '\0'; + return resolved_path; + } else { + return strdup(tmp); + } + } + } + char pbuf[PATH_MAX]; + const char *target = rewrite_path(path, pbuf, sizeof(pbuf)); + char *res = orig_realpath ? orig_realpath(target, resolved_path) : NULL; + if (res && g_ddnatv_root[0] != '\0') { + size_t rlen = g_ddnatv_root_len; + if (strncmp(res, g_ddnatv_root, rlen) == 0 && (res[rlen] == '/' || res[rlen] == '\0')) { + size_t rem_len = strlen(res + rlen); + if (rem_len == 0) { + res[0] = '/'; + res[1] = '\0'; + } else { + memmove(res, res + rlen, rem_len + 1); + } + } + } + if (res) guest_path_of_mount_source(res, strlen(res), resolved_path ? PATH_MAX : strlen(res) + 1); + return res; +} + +char *__realpath_chk(const char *path, char *resolved_path, size_t resolved_len) { + return realpath(path, resolved_path); +} + +char *canonicalize_file_name(const char *path) { + return realpath(path, NULL); +} + +// Hook scandir and scandir64 +int scandir(const char *dirp, struct dirent ***namelist, + int (*filter)(const struct dirent *), + int (*compar)(const struct dirent **, const struct dirent **)) { + static int (*orig_scandir)(const char *, struct dirent ***, + int (*)(const struct dirent *), + int (*)(const struct dirent **, const struct dirent **)) = NULL; + if (!orig_scandir) orig_scandir = (int (*)(const char *, struct dirent ***, + int (*)(const struct dirent *), + int (*)(const struct dirent **, const struct dirent **)))dlsym(RTLD_NEXT, "scandir"); + char buf[PATH_MAX]; + const char *target = rewrite_path(dirp, buf, sizeof(buf)); + return orig_scandir ? orig_scandir(target, namelist, filter, compar) : -1; +} + +#if defined(__LP64__) +// Export 64-bit Large File Support (LFS) symbol aliases on 64-bit platforms so that +// calls from libraries compiled with LFS (such as libstdc++ calling fopen64) are intercepted. +__asm__( + ".globl open64\n" ".set open64, open\n" + ".globl openat64\n" ".set openat64, openat\n" + ".globl __open64_2\n" ".set __open64_2, __open_2\n" + ".globl __openat64_2\n" ".set __openat64_2, __openat_2\n" + ".globl creat64\n" ".set creat64, creat\n" + ".globl fopen64\n" ".set fopen64, fopen\n" + ".globl freopen64\n" ".set freopen64, freopen\n" + ".globl stat64\n" ".set stat64, stat\n" + ".globl lstat64\n" ".set lstat64, lstat\n" + ".globl fstatat64\n" ".set fstatat64, fstatat\n" + ".globl truncate64\n" ".set truncate64, truncate\n" + ".globl statfs64\n" ".set statfs64, statfs\n" + ".globl statvfs64\n" ".set statvfs64, statvfs\n" + ".globl scandir64\n" ".set scandir64, scandir\n" + ".globl mkstemp64\n" ".set mkstemp64, mkstemp\n" + ".globl mkostemp64\n" ".set mkostemp64, mkostemp\n" + ".globl mkstemps64\n" ".set mkstemps64, mkstemps\n" + ".globl mkostemps64\n" ".set mkostemps64, mkostemps\n" + ".globl tmpfile64\n" ".set tmpfile64, tmpfile\n" +); +#endif + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/files.c b/tools/nativeguest/hook/files.c new file mode 100644 index 0000000000..6c52f70adf --- /dev/null +++ b/tools/nativeguest/hook/files.c @@ -0,0 +1,813 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Opening, creating and inspecting files: open, fopen, temporary files, directories, the stat family. + +#ifndef DDNATV_HOST_TEST + +#ifndef O_TMPFILE +#define O_TMPFILE (020000000 | 00200000) +#endif + +static inline int open_needs_mode(int flags) { +#ifdef O_TMPFILE + return ((flags & O_CREAT) != 0) || ((flags & O_TMPFILE) == O_TMPFILE); +#else + return (flags & O_CREAT) != 0; +#endif +} + +// The app is root for the programs it runs, and root puts names in every directory and takes them out +// whatever the mode of the directory; the app does not. A directory that is the app's own can have lost +// its owner's rights: an earlier version left the directories dpkg makes (with mode 0) at mode 0 when the +// chmod that follows did not take effect, and a package can ship a read-only one (#44). When a call that +// makes or removes a name is refused in such a directory, the directory is given its owner's rights and the +// call is made once more. Returns 1 when the directory was repaired (the call can be made again). +static int give_dir_owner_rights(int dirfd, const char *name) { + static int (*orig_fchmodat)(int, const char *, mode_t, int) = NULL; + if (!name || !*name) return 0; + char parent[PATH_MAX]; + size_t len = strlen(name); + if (len >= sizeof(parent)) return 0; + memcpy(parent, name, len + 1); + while (len > 1 && parent[len - 1] == '/') parent[--len] = '\0'; + char *slash = strrchr(parent, '/'); + if (!slash) { + strcpy(parent, "."); + } else if (slash == parent) { + parent[1] = '\0'; + } else { + *slash = '\0'; + } + struct stat st; + if (real_fstatat(dirfd, parent, &st, 0) != 0 || !S_ISDIR(st.st_mode)) return 0; + if (__atomic_load_n(&g_app_uid, __ATOMIC_RELAXED) == (uid_t)-1) load_app_ids(); + if (st.st_uid != __atomic_load_n(&g_app_uid, __ATOMIC_RELAXED)) return 0; + if ((st.st_mode & 0700) == 0700) return 0; + if (!orig_fchmodat) orig_fchmodat = (int (*)(int, const char *, mode_t, int))dlsym(RTLD_NEXT, "fchmodat"); + return orig_fchmodat && orig_fchmodat(dirfd, parent, (st.st_mode | 0700) & 07777, 0) == 0; +} + +// For the calls that were refused (EACCES): whether the directory of `name` was repaired, so that the call +// can be made again. errno stays what the call left when it was not. +static int refused_in_repaired_dir(int dirfd, const char *name) { + if (errno != EACCES) return 0; + int saved = errno; + int fixed = give_dir_owner_rights(dirfd, name); + if (!fixed) errno = saved; + return fixed; +} + +static int dlopen_preload_deps(const char *path, void *(*orig_dlopen)(const char *, int), int depth); + +// Hook dlopen and dlmopen +void *dlopen(const char *filename, int flags) { + static void *(*orig_dlopen)(const char *, int) = NULL; + if (!orig_dlopen) orig_dlopen = (void *(*)(const char *, int))dlsym(RTLD_NEXT, "dlopen"); + if (!filename) return orig_dlopen ? orig_dlopen(NULL, flags) : NULL; + if (!orig_dlopen) return NULL; + + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(filename, buf, sizeof(buf)); + void *handle = orig_dlopen(target, flags); + if (!handle && target[0] == '/' && !(flags & RTLD_NOLOAD)) { + // Its dependencies may be in its RUNPATH, which ld.so looked for outside the rootfs + int saved = errno; + if (dlopen_preload_deps(target, orig_dlopen, 0) > 0) handle = orig_dlopen(target, flags); + if (!handle) errno = saved; + } + return handle; +} + +void *dlmopen(Lmid_t lmid, const char *filename, int flags) { + static void *(*orig_dlmopen)(Lmid_t, const char *, int) = NULL; + if (!orig_dlmopen) orig_dlmopen = (void *(*)(Lmid_t, const char *, int))dlsym(RTLD_NEXT, "dlmopen"); + if (!filename) return orig_dlmopen ? orig_dlmopen(lmid, NULL, flags) : NULL; + + char buf[PATH_MAX]; + const char *target = rewrite_path(filename, buf, sizeof(buf)); + return orig_dlmopen ? orig_dlmopen(lmid, target, flags) : NULL; +} + +// /proc//status (and a thread's) give the app's own ids, which every other call presents as +// root: process monitors comparing them with getuid() showed none of the session's processes. Only +// such tools are given root's there: Chromium (Steam's web helper) checks the ids of the processes it +// sandboxes in those files, and with them all 0 every helper exited and Steam never drew a frame. +static int reader_compares_process_ids(void) { + static const char *const k_readers[] = { + "ps", "top", "htop", "btop", "atop", "pgrep", "pkill", "pidof", "pstree", "w", "glances", + "ksystemstats", "plasma-systemmonitor", "ksysguard", "ksysguardd", "kinfocenter", "gnome-system-monitor", + }; + const char *me = program_invocation_short_name; + if (!me) return 0; + for (size_t i = 0; i < sizeof(k_readers) / sizeof(k_readers[0]); i++) { + if (strcmp(me, k_readers[i]) == 0) return 1; + } + return 0; +} + +static int is_proc_status_path(const char *p) { + if (!p || strncmp(p, "/proc/", 6) != 0) return 0; + p += 6; + if (strncmp(p, "self/", 5) == 0) p += 5; + else if (strncmp(p, "thread-self/", 12) == 0) p += 12; + else { + if (*p < '0' || *p > '9') return 0; + while (*p >= '0' && *p <= '9') p++; + if (*p++ != '/') return 0; + if (strncmp(p, "task/", 5) == 0) { + p += 5; + if (*p < '0' || *p > '9') return 0; + while (*p >= '0' && *p <= '9') p++; + if (*p++ != '/') return 0; + } + } + return strcmp(p, "status") == 0; +} + +static void proc_status_root_ids(char *text, size_t len, const char *field, unsigned long id) { + size_t flen = strlen(field); + for (size_t at = 0; at + flen <= len; at++) { + if ((at != 0 && text[at - 1] != '\n') || memcmp(text + at, field, flen) != 0) continue; + char *p = text + at + flen; + char *end = memchr(p, '\n', len - (size_t)(p - text)); + if (!end) end = text + len; + char out[128]; + size_t used = 0; + while (p < end && used + 24 < sizeof(out)) { + while (p < end && (*p == '\t' || *p == ' ')) out[used++] = *p++; + char *num = p; + while (p < end && *p >= '0' && *p <= '9') p++; + if (p == num) break; + unsigned long v = strtoul(num, NULL, 10); + used += (size_t)snprintf(out + used, sizeof(out) - used, "%lu", v == id ? 0UL : v); + } + if (p != end) return; + // the line shrinks or stays (an id becomes 0), so it is rewritten in place + size_t old_len = (size_t)(end - (text + at + flen)); + if (used > old_len) return; + memcpy(text + at + flen, out, used); + memmove(text + at + flen + used, end, len - (size_t)(end - text) + 1); + return; + } +} + +// The status file with the app's ids as root's, in a memory file; -2 when `path` is no such file +// or is opened for writing. +static int open_proc_status(const char *path, int flags) { + if ((flags & O_ACCMODE) != O_RDONLY || !is_proc_status_path(path) || !reader_compares_process_ids()) return -2; + int fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC); + if (fd < 0) return -1; + char text[16384]; + size_t len = 0; + for (;;) { + ssize_t n = (ssize_t)syscall(SYS_read, fd, text + len, sizeof(text) - 1 - len); + if (n <= 0) break; + len += (size_t)n; + if (len >= sizeof(text) - 1) break; + } + syscall(SYS_close, fd); + text[len] = '\0'; + if (__atomic_load_n(&g_app_uid, __ATOMIC_RELAXED) == (uid_t)-1) load_app_ids(); + proc_status_root_ids(text, len, "Uid:", (unsigned long)__atomic_load_n(&g_app_uid, __ATOMIC_RELAXED)); + len = strlen(text); + proc_status_root_ids(text, len, "Gid:", (unsigned long)__atomic_load_n(&g_app_gid, __ATOMIC_RELAXED)); + len = strlen(text); + int mfd = (int)syscall(SYS_memfd_create, "status", (flags & O_CLOEXEC) ? MFD_CLOEXEC : 0); + if (mfd < 0) return (int)syscall(SYS_openat, AT_FDCWD, path, flags, 0); + size_t written = 0; + while (written < len) { + ssize_t n = (ssize_t)syscall(SYS_write, mfd, text + written, len - written); + if (n <= 0) break; + written += (size_t)n; + } + syscall(SYS_lseek, mfd, 0, SEEK_SET); + return mfd; +} + +// openat(dirfd, "status") with dirfd on /proc/ or a thread of it +static int open_proc_status_at(int dirfd, const char *name, int flags) { + if (dirfd < 0 || !name || strcmp(name, "status") != 0 || (flags & O_ACCMODE) != O_RDONLY) return -2; + char link[48], dir[PATH_MAX]; + snprintf(link, sizeof(link), "/proc/self/fd/%d", dirfd); + long n = syscall(SYS_readlinkat, AT_FDCWD, link, dir, sizeof(dir) - 8); + if (n <= 0) return -2; + dir[n] = '\0'; + memcpy(dir + n, "/status", 8); + return open_proc_status(dir, flags); +} + +// Hook open +int open(const char *pathname, int flags, ...) { + static int (*orig_open)(const char *, int, ...) = NULL; + if (!orig_open) orig_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open"); + init_ddnatv_hook(); + char exe_buf[PATH_MAX]; + if (is_proc_self_exe(pathname)) { + if (g_real_exe[0] != '\0') { + pathname = g_real_exe; + } else if (handle_proc_self_exe(exe_buf, sizeof(exe_buf)) > 0) { + pathname = exe_buf; + } + } + int status_fd = open_proc_status(pathname, flags); + if (status_fd != -2) return status_fd; + char buf[PATH_MAX]; + const char *target = ((flags & O_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)); + if (open_needs_mode(flags)) { + va_list args; + va_start(args, flags); + mode_t mode = va_arg(args, mode_t); + va_end(args); + int fd = orig_open(target, flags, mode); + if (fd < 0 && (flags & O_CREAT) && refused_in_repaired_dir(AT_FDCWD, target)) fd = orig_open(target, flags, mode); + return fd; + } + return orig_open(target, flags); +} + +// Hook openat +int openat(int dirfd, const char *pathname, int flags, ...) { + static int (*orig_openat)(int, const char *, int, ...) = NULL; + if (!orig_openat) orig_openat = (int (*)(int, const char *, int, ...))dlsym(RTLD_NEXT, "openat"); + init_ddnatv_hook(); + DDNATV_AT_REMAP(dirfd, pathname); + char exe_buf[PATH_MAX]; + if (is_proc_self_exe(pathname)) { + if (g_real_exe[0] != '\0') { + pathname = g_real_exe; + } else if (handle_proc_self_exe(exe_buf, sizeof(exe_buf)) > 0) { + pathname = exe_buf; + } + } + int status_fd = (pathname && pathname[0] == '/') ? open_proc_status(pathname, flags) : open_proc_status_at(dirfd, pathname, flags); + if (status_fd != -2) return status_fd; + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? ((flags & O_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname; + if (open_needs_mode(flags)) { + va_list args; + va_start(args, flags); + mode_t mode = va_arg(args, mode_t); + va_end(args); + int fd = orig_openat(dirfd, target, flags, mode); + if (fd < 0 && (flags & O_CREAT) && refused_in_repaired_dir(dirfd, target)) fd = orig_openat(dirfd, target, flags, mode); + return fd; + } + return orig_openat(dirfd, target, flags); +} + +// Hook creat +int creat(const char *pathname, mode_t mode) { + return open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode); +} + +// Fortified open variants used by GNU tar and other coreutils compiled with _FORTIFY_SOURCE=2/3 +int __open_2(const char *pathname, int flags) { + return open(pathname, flags); +} + +int __openat_2(int dirfd, const char *pathname, int flags) { + return openat(dirfd, pathname, flags); +} + +// openat2/open_how local definition: avoids depending on , +// which old NDK/cross headers may lack. Layout matches UAPI ver0 +// (flags, mode, resolve = 3 x __u64); extra fields are never read here. +struct ddnatv_open_how { + uint64_t flags; + uint64_t mode; + uint64_t resolve; +}; +#ifndef RESOLVE_BENEATH +#define RESOLVE_BENEATH 0x08 +#endif +#ifndef __NR_openat2 +#define __NR_openat2 437 +#endif +#ifndef __NR_fchmodat2 +#define __NR_fchmodat2 452 +#endif + +static long call_orig_syscall(long number, unsigned long a1, unsigned long a2, unsigned long a3, unsigned long a4, unsigned long a5, unsigned long a6) { + static long (*orig_syscall)(long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long) = NULL; + if (!orig_syscall) { + orig_syscall = (long (*)(long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long))dlsym(RTLD_NEXT, "syscall"); + } + return orig_syscall ? orig_syscall(number, a1, a2, a3, a4, a5, a6) : syscall(number, a1, a2, a3, a4, a5, a6); +} + +// Hook openat2 (used by GNU tar 1.35+/gnulib for RESOLVE_BENEATH traversal). +// Rewrites absolute paths like openat, then delegates; on ENOSYS (kernels +// without openat2, e.g. Android < 5.6) emulates with plain openat instead of +// trusting every caller to implement the fallback itself. +int openat2(int dirfd, const char *pathname, struct ddnatv_open_how *how, size_t usize) { + static int (*orig_openat2)(int, const char *, struct ddnatv_open_how *, size_t) = NULL; + if (!orig_openat2) orig_openat2 = (int (*)(int, const char *, struct ddnatv_open_how *, size_t))dlsym(RTLD_NEXT, "openat2"); + if (!pathname) { + errno = EFAULT; + return -1; + } + const char *given = pathname; + DDNATV_AT_REMAP(dirfd, pathname); + // a remapped path is absolute, which RESOLVE_BENEATH and the like refuse: the caller walks it itself + if (pathname != given && how && how->resolve != 0) { + errno = ENOSYS; + return -1; + } + char exe_buf[PATH_MAX]; + if (is_proc_self_exe(pathname)) { + if (g_real_exe[0] != '\0') { + pathname = g_real_exe; + } else if (handle_proc_self_exe(exe_buf, sizeof(exe_buf)) > 0) { + pathname = exe_buf; + } + } + char buf[PATH_MAX]; + const char *target = (pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + if (orig_openat2) { + int ret = orig_openat2(dirfd, target, how, usize); + if (ret >= 0 || errno != ENOSYS) return ret; + } +#ifdef __NR_openat2 + long sret = call_orig_syscall(__NR_openat2, (unsigned long)dirfd, (unsigned long)target, (unsigned long)how, (unsigned long)usize, 0, 0); + if (sret >= 0 || errno != ENOSYS) { + return (int)sret; + } +#endif + if (!how || usize < 16) { + errno = EINVAL; + return -1; + } + int flags = (int)(how->flags & 0xffffffffu); + mode_t mode = (mode_t)(how->mode & 07777u); + + // RESOLVE_BENEATH, RESOLVE_NO_SYMLINKS and the other resolve flags restrict every component of + // the path, which a plain openat() cannot do (a symlink in the middle would still be followed). + // Say so: callers such as gnulib's openat2 (GNU tar) then walk the path safely themselves. + if (how->resolve != 0) { + errno = ENOSYS; + return -1; + } + + if (open_needs_mode(flags)) { + return openat(dirfd, target, flags, mode); + } + return openat(dirfd, target, flags); +} + +// Hook fchmodat2 (used by GNU tar for lchmod-style symlink chmod). +// Delegates when the libc symbol exists, otherwise emulates: symlinks cannot +// be chmod'ed without kernel support, so skip them (return 0) like gnulib, +// and route everything else through fchmodat. +int fchmodat2(int dirfd, const char *pathname, mode_t mode, int flags) { + static int (*orig_fchmodat2)(int, const char *, mode_t, int) = NULL; + if (!orig_fchmodat2) orig_fchmodat2 = (int (*)(int, const char *, mode_t, int))dlsym(RTLD_NEXT, "fchmodat2"); + if (!pathname) { + errno = EFAULT; + return -1; + } + char buf[PATH_MAX]; + const char *target = (pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + if (orig_fchmodat2) { + int ret = orig_fchmodat2(dirfd, target, mode, flags); + if (ret == 0 || errno != ENOSYS) return ret; + } + if ((flags & AT_SYMLINK_NOFOLLOW) != 0) { + struct stat st; + if (fstatat(dirfd, target, &st, AT_SYMLINK_NOFOLLOW) == 0 && S_ISLNK(st.st_mode)) { + return 0; + } + } + return fchmodat(dirfd, target, mode, 0); +} + + +// Hook fopen +FILE *fopen(const char *pathname, const char *mode) { + static FILE *(*orig_fopen)(const char *, const char *) = NULL; + if (!orig_fopen) orig_fopen = (FILE *(*)(const char *, const char *))dlsym(RTLD_NEXT, "fopen"); + if (mode && mode[0] == 'r' && !strchr(mode, '+')) { + int status_fd = open_proc_status(pathname, O_RDONLY | (strchr(mode, 'e') ? O_CLOEXEC : 0)); + if (status_fd >= 0) return fdopen(status_fd, mode); + if (status_fd == -1) return NULL; + } + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(pathname, buf, sizeof(buf)); + return orig_fopen(target, mode); +} + +// Hook freopen +FILE *freopen(const char *pathname, const char *mode, FILE *stream) { + static FILE *(*orig_freopen)(const char *, const char *, FILE *) = NULL; + if (!orig_freopen) orig_freopen = (FILE *(*)(const char *, const char *, FILE *))dlsym(RTLD_NEXT, "freopen"); + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(pathname, buf, sizeof(buf)); + return orig_freopen(target, mode, stream); +} + +// Hook write +ssize_t write(int fd, const void *buf, size_t count) { + static ssize_t (*orig_write)(int, const void *, size_t) = NULL; + if (!orig_write) orig_write = (ssize_t (*)(int, const void *, size_t))dlsym(RTLD_NEXT, "write"); + return orig_write ? orig_write(fd, buf, count) : -1; +} + +// Hook mkstemp +int mkstemp(char *template) { + static int (*orig_mkstemp)(char *) = NULL; + if (!orig_mkstemp) orig_mkstemp = (int (*)(char *))dlsym(RTLD_NEXT, "mkstemp"); + if (!template) { errno = EINVAL; return -1; } + + char buf[PATH_MAX]; + const char *target = rewrite_path(template, buf, sizeof(buf)); + if (target == template) { + return orig_mkstemp ? orig_mkstemp(template) : -1; + } + + char tmp_buf[PATH_MAX]; + strncpy(tmp_buf, target, sizeof(tmp_buf) - 1); + tmp_buf[sizeof(tmp_buf) - 1] = '\0'; + + int fd = orig_mkstemp ? orig_mkstemp(tmp_buf) : -1; + if (fd >= 0) { + size_t orig_len = strlen(template); + size_t tmp_len = strlen(tmp_buf); + if (orig_len >= 6 && tmp_len >= 6) { + memcpy(template + orig_len - 6, tmp_buf + tmp_len - 6, 6); + } + } + return fd; +} + +// Hook mkostemp +int mkostemp(char *template, int flags) { + static int (*orig_mkostemp)(char *, int) = NULL; + if (!orig_mkostemp) orig_mkostemp = (int (*)(char *, int))dlsym(RTLD_NEXT, "mkostemp"); + if (!template) { errno = EINVAL; return -1; } + + char buf[PATH_MAX]; + const char *target = rewrite_path(template, buf, sizeof(buf)); + if (target == template) { + return orig_mkostemp ? orig_mkostemp(template, flags) : -1; + } + + char tmp_buf[PATH_MAX]; + strncpy(tmp_buf, target, sizeof(tmp_buf) - 1); + tmp_buf[sizeof(tmp_buf) - 1] = '\0'; + + int fd = orig_mkostemp ? orig_mkostemp(tmp_buf, flags) : -1; + if (fd >= 0) { + size_t orig_len = strlen(template); + size_t tmp_len = strlen(tmp_buf); + if (orig_len >= 6 && tmp_len >= 6) { + memcpy(template + orig_len - 6, tmp_buf + tmp_len - 6, 6); + } + } + return fd; +} + +// Hook mkstemps +int mkstemps(char *template, int suffixlen) { + static int (*orig_mkstemps)(char *, int) = NULL; + if (!orig_mkstemps) orig_mkstemps = (int (*)(char *, int))dlsym(RTLD_NEXT, "mkstemps"); + if (!template) { errno = EINVAL; return -1; } + + char buf[PATH_MAX]; + const char *target = rewrite_path(template, buf, sizeof(buf)); + if (target == template) { + return orig_mkstemps ? orig_mkstemps(template, suffixlen) : -1; + } + + char tmp_buf[PATH_MAX]; + strncpy(tmp_buf, target, sizeof(tmp_buf) - 1); + tmp_buf[sizeof(tmp_buf) - 1] = '\0'; + + int fd = orig_mkstemps ? orig_mkstemps(tmp_buf, suffixlen) : -1; + if (fd >= 0) { + size_t orig_len = strlen(template); + size_t tmp_len = strlen(tmp_buf); + if (orig_len >= (size_t)(suffixlen + 6) && tmp_len >= (size_t)(suffixlen + 6)) { + memcpy(template + orig_len - suffixlen - 6, tmp_buf + tmp_len - suffixlen - 6, 6); + } + } + return fd; +} + +// Hook mkostemps +int mkostemps(char *template, int suffixlen, int flags) { + static int (*orig_mkostemps)(char *, int, int) = NULL; + if (!orig_mkostemps) orig_mkostemps = (int (*)(char *, int, int))dlsym(RTLD_NEXT, "mkostemps"); + if (!template) { errno = EINVAL; return -1; } + + char buf[PATH_MAX]; + const char *target = rewrite_path(template, buf, sizeof(buf)); + if (target == template) { + return orig_mkostemps ? orig_mkostemps(template, suffixlen, flags) : -1; + } + + char tmp_buf[PATH_MAX]; + strncpy(tmp_buf, target, sizeof(tmp_buf) - 1); + tmp_buf[sizeof(tmp_buf) - 1] = '\0'; + + int fd = orig_mkostemps ? orig_mkostemps(tmp_buf, suffixlen, flags) : -1; + if (fd >= 0) { + size_t orig_len = strlen(template); + size_t tmp_len = strlen(tmp_buf); + if (orig_len >= (size_t)(suffixlen + 6) && tmp_len >= (size_t)(suffixlen + 6)) { + memcpy(template + orig_len - suffixlen - 6, tmp_buf + tmp_len - suffixlen - 6, 6); + } + } + return fd; +} + +// Hook mkdtemp +char *mkdtemp(char *template) { + static char *(*orig_mkdtemp)(char *) = NULL; + if (!orig_mkdtemp) orig_mkdtemp = (char *(*)(char *))dlsym(RTLD_NEXT, "mkdtemp"); + if (!template) { errno = EINVAL; return NULL; } + + char buf[PATH_MAX]; + const char *target = rewrite_path(template, buf, sizeof(buf)); + if (target == template) { + return orig_mkdtemp ? orig_mkdtemp(template) : NULL; + } + + char tmp_buf[PATH_MAX]; + strncpy(tmp_buf, target, sizeof(tmp_buf) - 1); + tmp_buf[sizeof(tmp_buf) - 1] = '\0'; + + char *res = orig_mkdtemp ? orig_mkdtemp(tmp_buf) : NULL; + if (res) { + size_t orig_len = strlen(template); + size_t tmp_len = strlen(tmp_buf); + if (orig_len >= 6 && tmp_len >= 6) { + memcpy(template + orig_len - 6, tmp_buf + tmp_len - 6, 6); + } + return template; + } + return NULL; +} + +// Hook tmpfile +FILE *tmpfile(void) { + init_ddnatv_hook(); + char template_buf[PATH_MAX]; + if (g_ddnatv_root[0] != '\0') { + snprintf(template_buf, sizeof(template_buf), "%s/tmp/tmpfile.XXXXXX", g_ddnatv_root); + } else { + snprintf(template_buf, sizeof(template_buf), "/tmp/tmpfile.XXXXXX"); + } + int fd = mkstemp(template_buf); + if (fd < 0) return NULL; + unlink(template_buf); + FILE *f = fdopen(fd, "w+b"); + if (!f) { + int saved_errno = errno; + close(fd); + errno = saved_errno; + } + return f; +} + +#ifndef __USE_FILE_OFFSET64 +// Programs built with _LARGEFILE64_SOURCE call this name. With _FILE_OFFSET_BITS=64 (the 32-bit ARM +// build) the headers already export the definition above as tmpfile64. +FILE *tmpfile64(void) { + return tmpfile(); +} +#endif + +// Hook opendir +DIR *opendir(const char *name) { + static DIR *(*orig_opendir)(const char *) = NULL; + if (!orig_opendir) orig_opendir = (DIR *(*)(const char *))dlsym(RTLD_NEXT, "opendir"); + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(name, buf, sizeof(buf)); + return orig_opendir(target); +} + +// Hook stat +int stat(const char *pathname, struct stat *statbuf) { + static int (*orig_stat)(const char *, struct stat *) = NULL; + if (!orig_stat) orig_stat = (int (*)(const char *, struct stat *))dlsym(RTLD_NEXT, "stat"); + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(pathname, buf, sizeof(buf)); + int ret = orig_stat ? orig_stat(target, statbuf) : -1; + if (ret != 0 && errno == ENOENT) { + char alt[PATH_MAX]; + const char *retry = usrmerge_retry_path(target, alt, sizeof(alt)); + if (retry) ret = orig_stat(retry, statbuf); + } + if (ret == 0) apply_stat_emulation(statbuf); + return ret; +} + +// Hook lstat +int lstat(const char *pathname, struct stat *statbuf) { + static int (*orig_lstat)(const char *, struct stat *) = NULL; + if (!orig_lstat) orig_lstat = (int (*)(const char *, struct stat *))dlsym(RTLD_NEXT, "lstat"); + char buf[PATH_MAX]; + const char *target = rewrite_path(pathname, buf, sizeof(buf)); + int ret = orig_lstat ? orig_lstat(target, statbuf) : -1; + char alt[PATH_MAX]; + if (ret != 0 && errno == ENOENT) { + const char *retry = usrmerge_retry_path(target, alt, sizeof(alt)); + if (retry) { + ret = orig_lstat(retry, statbuf); + if (ret == 0) target = retry; + } + } + if (ret == 0 && statbuf && S_ISLNK(statbuf->st_mode) && g_ddnatv_root[0] != '\0' && target && + readlink_result_may_change(pathname, (long long)statbuf->st_size)) { + char link_buf[PATH_MAX]; + static ssize_t (*orig_readlink)(const char *, char *, size_t) = NULL; + if (!orig_readlink) orig_readlink = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink"); + ssize_t llen = orig_readlink ? orig_readlink(target, link_buf, sizeof(link_buf) - 1) : -1; + if (llen > 0) { + ssize_t post_len = postprocess_readlink(pathname, link_buf, llen, sizeof(link_buf)); + if (post_len > 0) { + statbuf->st_size = post_len; + } + } + } + if (ret == 0) apply_stat_emulation(statbuf); + return ret; +} + +// Hook fstatat +int fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags) { + static int (*orig_fstatat)(int, const char *, struct stat *, int) = NULL; + if (!orig_fstatat) orig_fstatat = (int (*)(int, const char *, struct stat *, int))dlsym(RTLD_NEXT, "fstatat"); + DDNATV_AT_REMAP(dirfd, pathname); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname; + int ret = orig_fstatat ? orig_fstatat(dirfd, target, statbuf, flags) : -1; + char alt[PATH_MAX]; + if (ret != 0 && errno == ENOENT && pathname && pathname[0] == '/') { + const char *retry = usrmerge_retry_path(target, alt, sizeof(alt)); + if (retry) { + ret = orig_fstatat(dirfd, retry, statbuf, flags); + if (ret == 0) target = retry; + } + } + if (ret == 0 && statbuf && (flags & AT_SYMLINK_NOFOLLOW) && S_ISLNK(statbuf->st_mode) && g_ddnatv_root[0] != '\0' && target && + readlink_result_may_change(pathname, (long long)statbuf->st_size)) { + char link_buf[PATH_MAX]; + static ssize_t (*orig_readlinkat)(int, const char *, char *, size_t) = NULL; + if (!orig_readlinkat) orig_readlinkat = (ssize_t (*)(int, const char *, char *, size_t))dlsym(RTLD_NEXT, "readlinkat"); + ssize_t llen = orig_readlinkat ? orig_readlinkat(dirfd, target, link_buf, sizeof(link_buf) - 1) : -1; + if (llen > 0) { + ssize_t post_len = postprocess_readlink(pathname, link_buf, llen, sizeof(link_buf)); + if (post_len > 0) { + statbuf->st_size = post_len; + } + } + } + if (ret == 0) apply_stat_emulation(statbuf); + return ret; +} + +// Hook fstat +int fstat(int fd, struct stat *statbuf) { + static int (*orig_fstat)(int, struct stat *) = NULL; + if (!orig_fstat) orig_fstat = (int (*)(int, struct stat *))dlsym(RTLD_NEXT, "fstat"); + int ret = orig_fstat ? orig_fstat(fd, statbuf) : -1; + if (ret == 0) apply_stat_emulation(statbuf); + return ret; +} + +// Glibc __xstat compatibility hooks, what programs built against glibc before 2.33 call (Firefox): +// they present owners like stat() does, or Firefox saw $HOME owned by the app's uid while geteuid() +// said 0 and refused to start ("as root in a regular user's session"). +int __xstat(int ver, const char *pathname, struct stat *statbuf) { + static int (*orig___xstat)(int, const char *, struct stat *) = NULL; + if (!orig___xstat) orig___xstat = (int (*)(int, const char *, struct stat *))dlsym(RTLD_NEXT, "__xstat"); + if (!orig___xstat) return stat(pathname, statbuf); + char buf[PATH_MAX]; + int ret = orig___xstat(ver, rewrite_path_follow(pathname, buf, sizeof(buf)), statbuf); + if (ret == 0) apply_stat_emulation(statbuf); + return ret; +} + +int __xstat64(int ver, const char *pathname, struct stat64 *statbuf) { + static int (*orig___xstat64)(int, const char *, struct stat64 *) = NULL; + if (!orig___xstat64) orig___xstat64 = (int (*)(int, const char *, struct stat64 *))dlsym(RTLD_NEXT, "__xstat64"); + if (!orig___xstat64) return stat(pathname, (struct stat *)statbuf); + char buf[PATH_MAX]; + int ret = orig___xstat64(ver, rewrite_path_follow(pathname, buf, sizeof(buf)), statbuf); + if (ret == 0) apply_stat_emulation((struct stat *)statbuf); + return ret; +} + +int __lxstat(int ver, const char *pathname, struct stat *statbuf) { + static int (*orig___lxstat)(int, const char *, struct stat *) = NULL; + if (!orig___lxstat) orig___lxstat = (int (*)(int, const char *, struct stat *))dlsym(RTLD_NEXT, "__lxstat"); + if (!orig___lxstat) return lstat(pathname, statbuf); + char buf[PATH_MAX]; + int ret = orig___lxstat(ver, rewrite_path(pathname, buf, sizeof(buf)), statbuf); + if (ret == 0) apply_stat_emulation(statbuf); + return ret; +} + +int __lxstat64(int ver, const char *pathname, struct stat64 *statbuf) { + static int (*orig___lxstat64)(int, const char *, struct stat64 *) = NULL; + if (!orig___lxstat64) orig___lxstat64 = (int (*)(int, const char *, struct stat64 *))dlsym(RTLD_NEXT, "__lxstat64"); + if (!orig___lxstat64) return lstat(pathname, (struct stat *)statbuf); + char buf[PATH_MAX]; + int ret = orig___lxstat64(ver, rewrite_path(pathname, buf, sizeof(buf)), statbuf); + if (ret == 0) apply_stat_emulation((struct stat *)statbuf); + return ret; +} + +int __fxstat(int ver, int fd, struct stat *statbuf) { + static int (*orig___fxstat)(int, int, struct stat *) = NULL; + if (!orig___fxstat) orig___fxstat = (int (*)(int, int, struct stat *))dlsym(RTLD_NEXT, "__fxstat"); + if (!orig___fxstat) return fstat(fd, statbuf); + int ret = orig___fxstat(ver, fd, statbuf); + if (ret == 0) apply_stat_emulation(statbuf); + return ret; +} + +int __fxstat64(int ver, int fd, struct stat64 *statbuf) { + static int (*orig___fxstat64)(int, int, struct stat64 *) = NULL; + if (!orig___fxstat64) orig___fxstat64 = (int (*)(int, int, struct stat64 *))dlsym(RTLD_NEXT, "__fxstat64"); + if (!orig___fxstat64) return fstat(fd, (struct stat *)statbuf); + int ret = orig___fxstat64(ver, fd, statbuf); + if (ret == 0) apply_stat_emulation((struct stat *)statbuf); + return ret; +} + +int __fxstatat(int ver, int dirfd, const char *pathname, struct stat *statbuf, int flags) { + static int (*orig___fxstatat)(int, int, const char *, struct stat *, int) = NULL; + if (!orig___fxstatat) orig___fxstatat = (int (*)(int, int, const char *, struct stat *, int))dlsym(RTLD_NEXT, "__fxstatat"); + if (!orig___fxstatat) return fstatat(dirfd, pathname, statbuf, flags); + DDNATV_AT_REMAP(dirfd, pathname); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname; + int ret = orig___fxstatat(ver, dirfd, target, statbuf, flags); + if (ret == 0) apply_stat_emulation(statbuf); + return ret; +} + +int __fxstatat64(int ver, int dirfd, const char *pathname, struct stat64 *statbuf, int flags) { + static int (*orig___fxstatat64)(int, int, const char *, struct stat64 *, int) = NULL; + if (!orig___fxstatat64) orig___fxstatat64 = (int (*)(int, int, const char *, struct stat64 *, int))dlsym(RTLD_NEXT, "__fxstatat64"); + if (!orig___fxstatat64) return fstatat(dirfd, pathname, (struct stat *)statbuf, flags); + DDNATV_AT_REMAP(dirfd, pathname); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname; + int ret = orig___fxstatat64(ver, dirfd, target, statbuf, flags); + if (ret == 0) apply_stat_emulation((struct stat *)statbuf); + return ret; +} + +// Hook statx +int statx(int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf) { + // Rust's standard library asks whether statx exists by calling it with a NULL path and buffer + // (EFAULT means it does). glibc's header declares both as nonnull, so the compiler would drop + // the NULL check below and read the path: every Rust program (rg, rustc, cargo...) crashed. + __asm__ volatile("" : "+r"(pathname), "+r"(statxbuf)); + static int (*orig_statx)(int, const char *, int, unsigned int, struct statx *) = NULL; + if (!orig_statx) orig_statx = (int (*)(int, const char *, int, unsigned int, struct statx *))dlsym(RTLD_NEXT, "statx"); + DDNATV_AT_REMAP(dirfd, pathname); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname; + int ret = orig_statx ? orig_statx(dirfd, target, flags, mask, statxbuf) : -1; + if (ret == 0 && statxbuf && hardlinks_maybe_any()) { + const unsigned int needed = STATX_INO | STATX_NLINK | STATX_MTIME; + struct statx full; + const struct statx *sx = statxbuf; + if ((statxbuf->stx_mask & needed) != needed) { + // The caller asked for less ("stat -c %h" for the link count only): what an emulated + // link is recognized by + int saved_errno = errno; + sx = orig_statx(dirfd, target, flags, needed, &full) == 0 && (full.stx_mask & needed) == needed ? &full : NULL; + errno = saved_errno; + } + if (sx) { + struct stat st; + memset(&st, 0, sizeof(st)); + st.st_dev = makedev(sx->stx_dev_major, sx->stx_dev_minor); + st.st_ino = sx->stx_ino; + st.st_nlink = sx->stx_nlink; + st.st_mtim.tv_sec = sx->stx_mtime.tv_sec; + st.st_mtim.tv_nsec = sx->stx_mtime.tv_nsec; + apply_emulated_hardlink(&st); + if (st.st_ino != sx->stx_ino || st.st_nlink != sx->stx_nlink) { + statxbuf->stx_ino = st.st_ino; + statxbuf->stx_nlink = st.st_nlink; + statxbuf->stx_mask |= STATX_INO | STATX_NLINK; + } + } + } + if (ret == 0 && statxbuf) { + uid_t uid = (uid_t)statxbuf->stx_uid; + gid_t gid = (gid_t)statxbuf->stx_gid; + present_owner_as_root((statxbuf->stx_mask & STATX_UID) ? &uid : NULL, (statxbuf->stx_mask & STATX_GID) ? &gid : NULL); + statxbuf->stx_uid = (uint32_t)uid; + statxbuf->stx_gid = (uint32_t)gid; + } + return ret; +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/go_programs.c b/tools/nativeguest/hook/go_programs.c new file mode 100644 index 0000000000..987d0132ee --- /dev/null +++ b/tools/nativeguest/hook/go_programs.c @@ -0,0 +1,465 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Static Go programs: they make their system calls themselves, so Ubuntu's paths mean nothing to +// them. The code of gosys/gosys.c is added to their file. + +// ---- Static Go programs ------------------------------------------------------------------- +// A Go program built without cgo (all of Ubuntu's, and the single files downloaded from a project's +// releases) has no C library: the preloaded hook is never loaded into it, so "/etc/resolv.conf" is +// a file of Android's that does not exist, and so is every program it starts. Go makes all of its +// system calls but a few of its runtime's in one assembly function, internal/runtime/syscall. +// Syscall6, which loads the number and the arguments from the stack and executes "svc #0". +// +// Before such a program (AArch64, Go 1.19 or newer) starts, The native userland changes its file in place, once: +// - the code of gosys/gosys.c is put after the program's code, in the zero padding that the Go +// linker leaves between its segments (the segment is made longer, and executable if it only +// was readable), +// - zero-filled memory for it is added to the end of the program's data segment, +// - the "svc #0" of Syscall6 becomes a branch to a few instructions in that code, which call it +// and go back to the instruction after the "svc". +// The code maps the paths of the calls into the rootfs, as the hook does for C programs, and starts +// the programs of the rootfs through the hook. The file keeps its size, its times and its mode; +// only the program headers, the padding and one instruction differ. The file is changed back and +// patched again when the code of a newer The native userland differs (the trailer says what to undo). + +#ifdef DDNATV_HAVE_GOSYS + +#define GO_MAX_PH 32 +#define GO_PF_X 1u +#define GO_PF_W 2u +#define GO_ET_EXEC 2 +#define GO_ET_DYN 3 + +struct go_ph { + uint32_t type, flags; + uint64_t offset, vaddr, filesz, memsz; + uint64_t pos; // where the entry is in the file +}; + +static uint64_t go_rd64(const unsigned char *p) { return (uint64_t)rd32(p) | ((uint64_t)rd32(p + 4) << 32); } +static void go_wr64(unsigned char *p, uint64_t v) { wr32(p, (uint32_t)v); wr32(p + 4, (uint32_t)(v >> 32)); } + +// The program headers of a 64-bit little-endian ELF; their number, or -1 +static int go_read_phdrs(int fd, struct go_ph *ph, uint16_t *etype, uint16_t *machine) { + unsigned char eh[64]; + if (pread(fd, eh, sizeof(eh), 0) != (ssize_t)sizeof(eh) || memcmp(eh, "\x7f" "ELF", 4) != 0 || eh[4] != 2 || eh[5] != 1) + return -1; + *etype = rd16(eh + 16); + *machine = rd16(eh + 18); + uint64_t phoff = go_rd64(eh + 32); + uint16_t phentsize = rd16(eh + 54), phnum = rd16(eh + 56); + if (phentsize != 56 || phnum == 0 || phnum > GO_MAX_PH) return -1; + for (int i = 0; i < phnum; i++) { + unsigned char e[56]; + uint64_t pos = phoff + (uint64_t)i * 56; + if (pread(fd, e, sizeof(e), (off_t)pos) != (ssize_t)sizeof(e)) return -1; + ph[i].type = rd32(e); + ph[i].flags = rd32(e + 4); + ph[i].offset = go_rd64(e + 8); + ph[i].vaddr = go_rd64(e + 16); + ph[i].filesz = go_rd64(e + 32); + ph[i].memsz = go_rd64(e + 40); + ph[i].pos = pos; + } + return phnum; +} + +// The instructions of Syscall6 up to its system call: x8 and x0 to x5 loaded from the stack +// (ldr x8, [sp, #8]; ldr x0, [sp, #16] ... ldr x5, [sp, #56]), then "svc #0" and "cmn x0, #4095" +static const uint32_t k_go_syscall6[7] = { + 0xf94007e8u, 0xf9400be0u, 0xf9400fe1u, 0xf94013e2u, 0xf94017e3u, 0xf9401be4u, 0xf9401fe5u, +}; +#define GO_SVC 0xd4000001u +#define GO_CMN_X0_4095 0xb13ffc1fu + +// Where Syscall6 makes its call: the file offset of the instruction after the seven loads (the +// "svc #0", or the branch that replaced it). *found counts the places; the first is returned. +static int64_t go_find_syscall6(int fd, const struct go_ph *ph, int n, int *found) { + int64_t first = -1; + *found = 0; + enum { CHUNK = 1 << 20, KEEP = 36 }; + unsigned char *buf = (unsigned char *)malloc(CHUNK + KEEP); + if (!buf) return -1; + for (int i = 0; i < n; i++) { + if (ph[i].type != 1 /* PT_LOAD */ || !(ph[i].flags & GO_PF_X) || ph[i].filesz == 0) continue; + uint64_t pos = 0; + while (pos < ph[i].filesz) { + uint64_t want = ph[i].filesz - pos; + if (want > CHUNK + KEEP) want = CHUNK + KEEP; + ssize_t got = pread(fd, buf, (size_t)want, (off_t)(ph[i].offset + pos)); + if (got <= KEEP) break; + for (ssize_t o = 0; o + 36 <= got; o += 4) { + if (rd32(buf + o) != k_go_syscall6[0]) continue; + int same = 1; + for (int k = 1; k < 7 && same; k++) same = rd32(buf + o + 4 * k) == k_go_syscall6[k]; + if (!same || rd32(buf + o + 32) != GO_CMN_X0_4095) continue; + if (*found == 0) first = (int64_t)(ph[i].offset + pos + (uint64_t)o + 28); + (*found)++; + } + if (pos + (uint64_t)got >= ph[i].filesz) break; + pos += (uint64_t)got - (KEEP - 4); // the next chunk starts where the last place of this could begin, no place is split or counted twice + } + } + free(buf); + return first; +} + +// "b " / "bl " for a distance in bytes, or 0 when it does not fit +static uint32_t go_branch(uint32_t base, int64_t delta) { + if ((delta & 3) != 0 || delta >= (1LL << 27) || delta < -(1LL << 27)) return 0; + return base | (uint32_t)((delta >> 2) & 0x3ffffff); +} + +static uint64_t go_align_up(uint64_t v, uint64_t a) { return (v + a - 1) & ~(a - 1); } + +// The strings the added code needs, as they are now +static int go_make_header(struct gosys_hdr *h) { + if (g_ddnatv_root_len == 0 || g_ddnatv_root_len >= GOSYS_ROOT_MAX) return -1; + memcpy(h->root, g_ddnatv_root, g_ddnatv_root_len); + h->root_len = (uint32_t)g_ddnatv_root_len; + + char buf[PATH_MAX]; + if (find_dynamic_linker_for_machine(g_ddnatv_root, EM_AARCH64, buf, sizeof(buf)) && strlen(buf) < GOSYS_PATH_MAX) { + h->ldso_len = (uint32_t)strlen(buf); + memcpy(h->ldso, buf, h->ldso_len); + } + static const char *const hooks[] = { "/usr/lib/libddnatv-hook.so", "/lib/libddnatv-hook.so" }; + for (size_t i = 0; i < sizeof(hooks) / sizeof(hooks[0]); i++) { + size_t n = ddnatv_append(buf, sizeof(buf), 0, g_ddnatv_root, g_ddnatv_root_len); + n = ddnatv_append(buf, sizeof(buf), n, hooks[i], strlen(hooks[i])); + if (n < GOSYS_PATH_MAX && access(buf, F_OK) == 0) { + h->hook_len = (uint32_t)n; + memcpy(h->hook, buf, n); + break; + } + } + // The program that starts the others: bash, which can give them the argv[0] they are asked for + static const char *const runners[] = { "/usr/bin/bash", "/bin/bash", "/usr/bin/env", "/bin/env" }; + for (size_t i = 0; i < sizeof(runners) / sizeof(runners[0]); i++) { + size_t n = ddnatv_append(buf, sizeof(buf), 0, g_ddnatv_root, g_ddnatv_root_len); + n = ddnatv_append(buf, sizeof(buf), n, runners[i], strlen(runners[i])); + if (n < GOSYS_PATH_MAX && access(buf, F_OK) == 0) { + h->runner_len = (uint32_t)n; + memcpy(h->runner, buf, n); + if (i < 2) h->flags |= GOSYS_FLAG_RUNNER_IS_BASH; + break; + } + } + // The directories the programs of an AArch64 rootfs load their libraries from + static const int dirs[] = { 0, 1, 4, 5, 6 }; + size_t used = 0; + for (size_t i = 0; i < sizeof(dirs) / sizeof(dirs[0]); i++) { + const char *d = k_rootfs_lib_dirs[dirs[i]]; + if (i > 0) used = ddnatv_append(buf, sizeof(buf), used, ":", 1); + used = ddnatv_append(buf, sizeof(buf), used, g_ddnatv_root, g_ddnatv_root_len); + used = ddnatv_append(buf, sizeof(buf), used, d, strlen(d)); + } + if (used < GOSYS_LIBS_MAX) { + h->libs_len = (uint32_t)used; + memcpy(h->libs, buf, used); + } + if (h->ldso_len == 0 || h->runner_len == 0) h->runner_len = 0; // Ubuntu programs cannot be started + return 0; +} + +static int go_pwrite_all(int fd, const void *buf, size_t len, uint64_t off) { + return pwrite(fd, buf, len, (off_t)off) == (ssize_t)len ? 0 : -1; +} + +static int go_open_for_write(int fd, const struct stat *st, const char *path, int *made_writable) { + int wfd = open(path, O_WRONLY | O_CLOEXEC); + if (wfd < 0 && errno == EACCES && fchmod(fd, (st->st_mode | S_IWUSR) & 07777) == 0) { + *made_writable = 1; + wfd = open(path, O_WRONLY | O_CLOEXEC); + } + return wfd; // ETXTBSY: the program is running right now, patched at its next start +} + +// The bytes of the added code for a given place: the code with its header (the strings, and where +// the scratch memory is), the instructions that connect the call site to it, and the trailer that +// says what was changed to put it there. +static void go_build_image(unsigned char *img, const struct gosys_hdr *strings, uint64_t cave_v, uint64_t scratch_v, + uint32_t to_entry, uint32_t back, const struct gosys_trailer *info) { + memcpy(img, gosys_blob, GOSYS_BLOB_SIZE); + struct gosys_hdr h = *strings; + h.scratch_rel = (int64_t)scratch_v - (int64_t)cave_v; + memcpy(img, &h, sizeof(h)); + unsigned char *th = img + GOSYS_THUNKS_OFF; + wr32(th + 0, 0xd10043ffu); // sub sp, sp, #16 + wr32(th + 4, 0xf90003feu); // str x30, [sp] + wr32(th + 8, 0xaa0803e7u); // mov x7, x8 (the number is the eighth argument) + wr32(th + 12, to_entry); // bl entry + wr32(th + 16, 0xf94003feu); // ldr x30, [sp] + wr32(th + 20, 0x910043ffu); // add sp, sp, #16 + wr32(th + 24, back); // b + struct gosys_trailer t; + memcpy(&t, img + GOSYS_BLOB_SIZE - sizeof(t), sizeof(t)); + t.version = GOSYS_VERSION; + t.total_size = GOSYS_BLOB_SIZE; + t.text_orig_filesz = info->text_orig_filesz; + t.text_orig_flags = info->text_orig_flags; + t.rw_orig_memsz = info->rw_orig_memsz; + t.text_index = info->text_index; + t.rw_index = info->rw_index; + t.site_file_off = info->site_file_off; + memcpy(img + GOSYS_BLOB_SIZE - sizeof(t), &t, sizeof(t)); +} + +// A short fingerprint of the added code, so that a program patched by an older The native userland is looked at +// again (the marker of a prepared program has it) +static uint32_t go_blob_fingerprint(void) { + static uint32_t fp; + if (fp == 0) { + uint32_t h = 2166136261u; + for (size_t i = 0; i < GOSYS_BLOB_SIZE; i++) h = (h ^ gosys_blob[i]) * 16777619u; + fp = h ? h : 1; + } + return fp; +} + +// Gives the program its old segments and call back: the added code is zeroed, the data segment +// is shortened, the instruction is a "svc #0" again. +static int go_undo(int wfd, struct go_ph *ph, int cave_seg, const struct gosys_trailer *tr, int64_t site_off) { + uint64_t cave_off = ph[cave_seg].offset + ph[cave_seg].filesz - tr->total_size; + unsigned char word[4]; + wr32(word, GO_SVC); + if (go_pwrite_all(wfd, word, 4, (uint64_t)site_off) != 0) return -1; + unsigned char sz[16]; + go_wr64(sz, tr->text_orig_filesz); + go_wr64(sz + 8, tr->text_orig_filesz); + if (go_pwrite_all(wfd, sz, 16, ph[cave_seg].pos + 32) != 0) return -1; + wr32(word, tr->text_orig_flags); + if (go_pwrite_all(wfd, word, 4, ph[cave_seg].pos + 4) != 0) return -1; + if (tr->rw_index < GO_MAX_PH) { + unsigned char mem[8]; + go_wr64(mem, tr->rw_orig_memsz); + if (go_pwrite_all(wfd, mem, 8, ph[tr->rw_index].pos + 40) != 0) return -1; + } + unsigned char *zero = (unsigned char *)calloc(1, tr->total_size); + if (!zero) return -1; + int rc = go_pwrite_all(wfd, zero, tr->total_size, cave_off); + free(zero); + return rc; +} + +// DDNATV_NO_GOSYS=1 (any value but empty or 0): no code is added to Go programs, and the code that is +// in one is taken out again, so that a program can be run as it was built, to see whether a fault +// is the program's or the code's. The marker of a prepared program says it (static_marker). +static int go_disabled(void) { + const char *v = getenv("DDNATV_NO_GOSYS"); + return v && v[0] != '\0' && strcmp(v, "0") != 0; +} + +// The three branches that connect the call site and the code: site -> thunk, thunk -> entry (bl), +// thunk -> the instruction after the site. 0 when one of them is too far. +static int go_branches(uint64_t cave_v, uint64_t site_v, uint32_t *to_thunk, uint32_t *to_entry, uint32_t *back) { + uint64_t thunk_v = cave_v + GOSYS_THUNKS_OFF; + *to_thunk = go_branch(0x14000000u, (int64_t)thunk_v - (int64_t)site_v); + *to_entry = go_branch(0x94000000u, (int64_t)(cave_v + GOSYS_ENTRY_OFF) - (int64_t)(thunk_v + 12)); + *back = go_branch(0x14000000u, (int64_t)(site_v + 4) - (int64_t)(thunk_v + 24)); + return *to_thunk && *to_entry && *back; +} + +// Adds the code to the program, or brings it up to date. Returns 1 when it changed the file, 0 when +// there was nothing to do (not a Go program of a kind the code can serve, or already done), -1 when +// the file could not be changed (it is started as it is). +static int prepare_go_program(int fd, const char *path, const struct stat *st) { + struct go_ph ph[GO_MAX_PH]; + uint16_t etype = 0, machine = 0; + int n = go_read_phdrs(fd, ph, &etype, &machine); + if (n < 0) return -1; + if (machine != EM_AARCH64 || (etype != GO_ET_EXEC && etype != GO_ET_DYN)) return 0; + + int found = 0; + int64_t site_off = go_find_syscall6(fd, ph, n, &found); + if (found != 1) return 0; // not a Go of 1.19 or newer (or its function is not there once) + + // The loadable segments, by address + int order[GO_MAX_PH], nl = 0; + for (int i = 0; i < n; i++) { + if (ph[i].type != 1 /* PT_LOAD */) continue; + int k = nl++; + while (k > 0 && ph[order[k - 1]].vaddr > ph[i].vaddr) { + order[k] = order[k - 1]; + k--; + } + order[k] = i; + } + if (nl < 2) return 0; + + // The call site must be in a segment that is mapped as code + uint64_t site_v = 0; + for (int i = 0; i < nl; i++) { + const struct go_ph *s = &ph[order[i]]; + if ((s->flags & GO_PF_X) && (uint64_t)site_off >= s->offset && (uint64_t)site_off < s->offset + s->filesz) + site_v = s->vaddr + ((uint64_t)site_off - s->offset); + } + if (site_v == 0) return 0; + + struct gosys_hdr strings; + memset(&strings, 0, sizeof(strings)); + memcpy(strings.magic, "CXGOSYS1", 8); + strings.version = GOSYS_VERSION; + if (go_make_header(&strings) != 0) return 0; + + unsigned char *img = (unsigned char *)malloc(GOSYS_BLOB_SIZE); + unsigned char *have = (unsigned char *)malloc(GOSYS_BLOB_SIZE); + if (!img || !have) { + free(img); + free(have); + return -1; + } + int wfd = -1, made_writable = 0, changed = 0, rc = 0; + unsigned char word[4]; + + // An earlier run's code: it ends the segment it is in, and says what it changed + int cave_seg = -1; + struct gosys_trailer tr; + memset(&tr, 0, sizeof(tr)); + for (int i = 0; i < nl && cave_seg < 0; i++) { + const struct go_ph *s = &ph[order[i]]; + if (s->filesz < sizeof(tr) + sizeof(struct gosys_hdr)) continue; + struct gosys_trailer t; + if (pread(fd, &t, sizeof(t), (off_t)(s->offset + s->filesz - sizeof(t))) != (ssize_t)sizeof(t)) continue; + if (memcmp(t.magic, "CXGOEND1", 8) != 0 || t.total_size > s->filesz || t.total_size < sizeof(t) + sizeof(struct gosys_hdr)) continue; + tr = t; + cave_seg = order[i]; + } + + if (go_disabled()) { + if (cave_seg >= 0) { + wfd = go_open_for_write(fd, st, path, &made_writable); + if (wfd < 0) { + rc = -1; + } else { + rc = go_undo(wfd, ph, cave_seg, &tr, site_off); + changed = rc == 0; + } + } + goto done; + } + + if (cave_seg >= 0) { + uint64_t cave_off = ph[cave_seg].offset + ph[cave_seg].filesz - tr.total_size; + uint64_t cave_v = ph[cave_seg].vaddr + (cave_off - ph[cave_seg].offset); + int same = 0, site_ok = 0; + uint32_t to_thunk = 0, to_entry = 0, back = 0; + // Is it what would be put there now? (the code of an older The native userland differs) + if (tr.total_size == GOSYS_BLOB_SIZE && tr.rw_index < (uint32_t)n && ph[tr.rw_index].type == 1 && + go_branches(cave_v, site_v, &to_thunk, &to_entry, &back)) { + uint64_t scratch_v = go_align_up(ph[tr.rw_index].vaddr + tr.rw_orig_memsz, 64); + go_build_image(img, &strings, cave_v, scratch_v, to_entry, back, &tr); + same = pread(fd, have, GOSYS_BLOB_SIZE, (off_t)cave_off) == (ssize_t)GOSYS_BLOB_SIZE && + memcmp(have, img, GOSYS_BLOB_SIZE) == 0 && + ph[tr.rw_index].memsz == scratch_v - ph[tr.rw_index].vaddr + GOSYS_SCRATCH_SIZE; + } + unsigned char cur[4]; + site_ok = same && pread(fd, cur, 4, (off_t)site_off) == 4 && rd32(cur) == to_thunk; + if (same && site_ok) goto done; + + wfd = go_open_for_write(fd, st, path, &made_writable); + if (wfd < 0) { + rc = -1; + goto done; + } + if (same) { + // The code is there, only the branch is not (a patch that was cut short) + wr32(word, to_thunk); + rc = go_pwrite_all(wfd, word, 4, (uint64_t)site_off); + changed = rc == 0; + goto done; + } + // An older code, or other strings: back to the plain program, then patch again + if (go_undo(wfd, ph, cave_seg, &tr, site_off) != 0) { + rc = -1; + goto done; + } + close(wfd); + wfd = -1; + if (made_writable) fchmod(fd, st->st_mode & 07777); + free(img); + free(have); + return prepare_go_program(fd, path, st); + } + + // A place for the code: the end of a segment that is followed by padding up to the next one + uint64_t total = GOSYS_BLOB_SIZE; + int text = -1; + uint64_t cave_v = 0, cave_off = 0; + for (int pass = 0; pass < 2 && text < 0; pass++) { + for (int k = 0; k + 1 < nl && text < 0; k++) { + const struct go_ph *s = &ph[order[k]], *nx = &ph[order[k + 1]]; + if (pass == 0 ? !(s->flags & GO_PF_X) : (s->flags & GO_PF_W)) continue; + if (s->memsz != s->filesz || s->filesz == 0) continue; + uint64_t v = go_align_up(s->vaddr + s->filesz, 64); + uint64_t off = s->offset + (v - s->vaddr); + if (v + total > nx->vaddr || (nx->vaddr & 0x3fff) != 0) continue; + if (off + total > nx->offset || off + total > (uint64_t)st->st_size) continue; + // Only padding may be written over + uint64_t pad_from = s->offset + s->filesz, pad_len = off + total - pad_from; + unsigned char *pad = (unsigned char *)malloc(pad_len); + if (!pad) continue; + int zero = pread(fd, pad, pad_len, (off_t)pad_from) == (ssize_t)pad_len; + for (uint64_t i = 0; zero && i < pad_len; i++) zero = pad[i] == 0; + free(pad); + if (!zero) continue; + text = order[k]; + cave_v = v; + cave_off = off; + } + } + int rw = order[nl - 1]; + uint32_t to_thunk = 0, to_entry = 0, back = 0; + if (text < 0 || !(ph[rw].flags & GO_PF_W) || rw == text || !go_branches(cave_v, site_v, &to_thunk, &to_entry, &back)) goto done; + + uint64_t scratch_v = go_align_up(ph[rw].vaddr + ph[rw].memsz, 64); + struct gosys_trailer info; + memset(&info, 0, sizeof(info)); + info.text_orig_filesz = ph[text].filesz; + info.text_orig_flags = ph[text].flags; + info.rw_orig_memsz = ph[rw].memsz; + info.text_index = (uint32_t)text; + info.rw_index = (uint32_t)rw; + info.site_file_off = (uint64_t)site_off; + go_build_image(img, &strings, cave_v, scratch_v, to_entry, back, &info); + + wfd = go_open_for_write(fd, st, path, &made_writable); + if (wfd < 0) { + rc = -1; + goto done; + } + // The code first and the call site last: whatever is cut short leaves a program that runs + unsigned char sz[16]; + unsigned char mem[8]; + go_wr64(mem, scratch_v - ph[rw].vaddr + GOSYS_SCRATCH_SIZE); + go_wr64(sz, cave_v + GOSYS_BLOB_SIZE - ph[text].vaddr); + go_wr64(sz + 8, cave_v + GOSYS_BLOB_SIZE - ph[text].vaddr); + rc = go_pwrite_all(wfd, img, GOSYS_BLOB_SIZE, cave_off); + if (rc == 0) rc = go_pwrite_all(wfd, mem, 8, ph[rw].pos + 40); + if (rc == 0) rc = go_pwrite_all(wfd, sz, 16, ph[text].pos + 32); + if (rc == 0 && !(ph[text].flags & GO_PF_X)) { + wr32(word, ph[text].flags | GO_PF_X); + rc = go_pwrite_all(wfd, word, 4, ph[text].pos + 4); + } + if (rc == 0) { + wr32(word, to_thunk); + rc = go_pwrite_all(wfd, word, 4, (uint64_t)site_off); + } + changed = rc == 0; + +done: + free(img); + free(have); + if (wfd >= 0) { + // Keep the times: make and friends must not see a rebuilt program. + const struct timespec times[2] = { st->st_atim, st->st_mtim }; + futimens(wfd, times); + close(wfd); + } + if (made_writable) fchmod(fd, st->st_mode & 07777); + if (rc != 0) return -1; + return changed ? 1 : 0; +} + +#endif /* DDNATV_HAVE_GOSYS */ diff --git a/tools/nativeguest/hook/hardlinks.c b/tools/nativeguest/hook/hardlinks.c new file mode 100644 index 0000000000..ed8a32f7b2 --- /dev/null +++ b/tools/nativeguest/hook/hardlinks.c @@ -0,0 +1,628 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Hard links: link() falls back to a copy that reports the link count of a real link, in every +// process of the rootfs. + +// Emulated hard links. Android does not let apps create hard links, so link() falls back to a copy +// (ddnatv_emulate_hardlinkat). Programs that check the result through the link count, such as the +// lock files of shadow (link(file.PID, file.lock), then stat(file.PID).st_nlink == 2), and those +// that look for files linked to each other (tar, rsync -H, du, find -samefile), are shown what a +// real link would give: every copy of one emulated link reports the inode number of the original +// and the number of copies as its link count. +// +// The copies are recorded in a table that every process of the rootfs maps +// ($DDNATV_ROOT/var/lib/ddnatv/hardlinks), so "ln a b" in one process and "stat a" in the next +// agree, also after the app restarts. Copies are recorded by device and inode, so a relative name, +// an absolute name and an open descriptor of the same file all match, and with their modification +// time: a copy written to since (it no longer shares the original's content) or a new file that got +// the inode number of a removed copy (removed outside the hook, e.g. in root mode) do not match. +// +// Readers (every stat) take no lock: a sequence number that writers make odd while they change the +// table tells them to read again. Writers (link, unlink, rename) take an fcntl lock on the file. +// Without a rootfs (or when the file cannot be made) the table is private to the process and its +// children, as it was before. + +#define DDNATV_HL_MAGIC 0x6b6e6c43u // "Clnk" +#define DDNATV_HL_VERSION 1u +#define DDNATV_HL_SLOTS 8192u // a power of two +#define DDNATV_HL_MAX_USED 6144u // at most 3/4 full, so that lookups stay short +#define DDNATV_HL_EMPTY 0u +#define DDNATV_HL_USED 1u +#define DDNATV_HL_DELETED 2u + +typedef struct { + uint64_t dev, ino; // this copy + int64_t mtime_sec, mtime_nsec; // its modification time when it was linked (compared in whole + // seconds: stat() and statx() need not agree on the fraction) + uint64_t group_dev, group_ino; // the original, whose inode number every copy reports + uint32_t state; + uint32_t nlink; // copies in the group + uint32_t pad[2]; +} ddnatv_hl_slot_t; + +typedef struct { + uint32_t magic, version; + uint32_t seq; // odd while a writer changes the table + uint32_t used, deleted; + uint32_t pad[11]; + ddnatv_hl_slot_t slots[DDNATV_HL_SLOTS]; +} ddnatv_hl_table_t; + +// The file and the mapping, in whole 64 KB: a size that is a multiple of every page size +#define DDNATV_HL_MAP_SIZE ((sizeof(ddnatv_hl_table_t) + 0xffff) & ~(size_t)0xffff) + +static ddnatv_hl_table_t *g_hl_table = NULL; // set once, then never changed (until a test resets it) +static int g_hl_shared = 0; // mapped from the rootfs' file +static long g_hl_next_try = 0; // when to look for the file again (monotonic seconds) +static pthread_mutex_t g_hardlinks_lock = PTHREAD_MUTEX_INITIALIZER; + +#ifndef DDNATV_HOST_TEST +static void init_ddnatv_hook(void); +#endif + +static size_t hl_table_path(char *buf, size_t size) { +#ifndef DDNATV_HOST_TEST + // The rootfs is known once a path was rewritten; "ln a b" or "stat a" name only relative ones + init_ddnatv_hook(); +#endif + if (g_ddnatv_root_len == 0) return 0; + size_t n = ddnatv_append(buf, size, 0, g_ddnatv_root, g_ddnatv_root_len); + static const char rel[] = "/var/lib/ddnatv/hardlinks"; + n = ddnatv_append(buf, size, n, rel, sizeof(rel) - 1); + return n == g_ddnatv_root_len + sizeof(rel) - 1 ? n : 0; +} + +static long hl_now_seconds(void) { + struct timespec ts; +#ifdef CLOCK_MONOTONIC_COARSE + if (clock_gettime(CLOCK_MONOTONIC_COARSE, &ts) == 0) return (long)ts.tv_sec; +#endif + if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) return (long)ts.tv_sec; + return 0; +} + +static int hl_open_file(const char *path, int create) { + return (int)syscall(SYS_openat, AT_FDCWD, path, O_RDWR | O_CLOEXEC | (create ? O_CREAT : 0), 0600); +} + +// Takes the writers' lock on the table's file (blocking); -1 when there is no file to lock. +static int hl_lock_file(void) { + if (!g_hl_shared) return -1; + char path[PATH_MAX]; + if (!hl_table_path(path, sizeof(path))) return -1; + int fd = hl_open_file(path, 0); + if (fd < 0) return -1; + struct flock fl; + memset(&fl, 0, sizeof(fl)); + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + while (fcntl(fd, F_SETLKW, &fl) != 0 && errno == EINTR) { + } + return fd; +} + +static void hl_unlock_file(int fd) { + if (fd >= 0) syscall(SYS_close, fd); // closing the file drops the lock +} + +static void hl_init_header(ddnatv_hl_table_t *t) { + memset(t, 0, sizeof(*t)); + t->version = DDNATV_HL_VERSION; + __atomic_store_n(&t->magic, DDNATV_HL_MAGIC, __ATOMIC_RELEASE); +} + +// The size of an open file. Not fstat(): this library's own fstat() looks into the table, which +// would wait for the lock that the caller holds. +static off_t hl_file_size(int fd) { + return lseek(fd, 0, SEEK_END); +} + +// Maps the table. Readers (create = 0) get NULL while no process has made the file yet, and look +// for it again a second later; writers make it, or fall back to a table of this process. Readers +// never wait for the lock (a stat() while it is held, or in a child forked while another thread +// held it, would wait forever): they find no table then. +static ddnatv_hl_table_t *hl_table(int create) { + ddnatv_hl_table_t *t = __atomic_load_n(&g_hl_table, __ATOMIC_ACQUIRE); + if (t) return t; + if (!create && __atomic_load_n(&g_hl_next_try, __ATOMIC_RELAXED) > hl_now_seconds()) return NULL; + int saved_errno = errno; + if (create) { + pthread_mutex_lock(&g_hardlinks_lock); + } else if (pthread_mutex_trylock(&g_hardlinks_lock) != 0) { + errno = saved_errno; + return NULL; + } + t = g_hl_table; + if (!t) { + char path[PATH_MAX]; + path[0] = '\0'; + int have_path = hl_table_path(path, sizeof(path)) != 0; + int fd = have_path ? hl_open_file(path, 0) : -1; + if (fd < 0 && create && path[0]) { + // var/lib/ddnatv may not exist yet + char dir[PATH_MAX]; + size_t n = ddnatv_append(dir, sizeof(dir), 0, path, strlen(path) - strlen("/hardlinks")); + if (n > 0) syscall(SYS_mkdirat, AT_FDCWD, dir, 0700); + fd = hl_open_file(path, 1); + } + if (fd >= 0) { + off_t size = hl_file_size(fd); + int ok = size >= 0; + if (ok && (size_t)size < DDNATV_HL_MAP_SIZE) { + // New (or an older, shorter format): made by a writer, under the lock + ok = 0; + if (create) { + struct flock fl; + memset(&fl, 0, sizeof(fl)); + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + while (fcntl(fd, F_SETLKW, &fl) != 0 && errno == EINTR) { + } + size = hl_file_size(fd); + ok = size >= 0 && + ((size_t)size >= DDNATV_HL_MAP_SIZE || ftruncate(fd, DDNATV_HL_MAP_SIZE) == 0); + } + } + if (ok) { + void *m = mmap(NULL, DDNATV_HL_MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (m != MAP_FAILED) { + t = (ddnatv_hl_table_t *)m; + if (t->magic != DDNATV_HL_MAGIC || t->version != DDNATV_HL_VERSION) { + if (create) { + // Still under the lock taken above when the file was new; otherwise take it + struct flock fl; + memset(&fl, 0, sizeof(fl)); + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + while (fcntl(fd, F_SETLKW, &fl) != 0 && errno == EINTR) { + } + if (t->magic != DDNATV_HL_MAGIC || t->version != DDNATV_HL_VERSION) hl_init_header(t); + } else { + munmap(m, DDNATV_HL_MAP_SIZE); + t = NULL; + } + } + if (t) g_hl_shared = 1; + } + } + syscall(SYS_close, fd); // the mapping stays; also drops the lock + } + if (!t && create) { + // No rootfs file: this process and its children share a table + void *m = mmap(NULL, DDNATV_HL_MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); + if (m != MAP_FAILED) { + t = (ddnatv_hl_table_t *)m; + hl_init_header(t); + g_hl_shared = 0; + } + } + if (t) { + __atomic_store_n(&g_hl_table, t, __ATOMIC_RELEASE); + } else if (have_path) { + // No table yet: looked for again a second later (without a rootfs there is none to wait for) + __atomic_store_n(&g_hl_next_try, hl_now_seconds() + 1, __ATOMIC_RELAXED); + } + } + pthread_mutex_unlock(&g_hardlinks_lock); + errno = saved_errno; + return t; +} + +// Whether any emulated link may exist; what the stat() hooks check first. +static inline int hardlinks_maybe_any(void) { + ddnatv_hl_table_t *t = hl_table(0); + return t && __atomic_load_n(&t->used, __ATOMIC_RELAXED) > 0; +} + +static inline uint32_t hl_hash(uint64_t dev, uint64_t ino) { + uint64_t h = ino * 0x9E3779B97F4A7C15ull ^ (dev + 0x632BE59BD9B4E019ull) * 0xC2B2AE3D27D4EB4Full; + return (uint32_t)(h >> 40) & (DDNATV_HL_SLOTS - 1); +} + +// The slot of a copy, or -1. Writers only (under the locks). +static long hl_find_locked(ddnatv_hl_table_t *t, uint64_t dev, uint64_t ino) { + uint32_t i = hl_hash(dev, ino); + for (uint32_t n = 0; n < DDNATV_HL_SLOTS; n++, i = (i + 1) & (DDNATV_HL_SLOTS - 1)) { + ddnatv_hl_slot_t *s = &t->slots[i]; + if (s->state == DDNATV_HL_EMPTY) return -1; + if (s->state == DDNATV_HL_USED && s->ino == ino && s->dev == dev) return (long)i; + } + return -1; +} + +// Writers: the process' mutex, the file's lock, then the sequence number made odd. +static int hl_write_begin(ddnatv_hl_table_t *t) { + pthread_mutex_lock(&g_hardlinks_lock); + int fd = hl_lock_file(); + uint32_t seq = __atomic_load_n(&t->seq, __ATOMIC_RELAXED); + // An odd number left by a writer that died: the lock was given up with it + if (!(seq & 1)) seq++; + __atomic_store_n(&t->seq, seq, __ATOMIC_RELAXED); + __atomic_thread_fence(__ATOMIC_RELEASE); + return fd; +} + +static void hl_write_end(ddnatv_hl_table_t *t, int fd) { + __atomic_store_n(&t->seq, __atomic_load_n(&t->seq, __ATOMIC_RELAXED) + 1, __ATOMIC_RELEASE); + hl_unlock_file(fd); + pthread_mutex_unlock(&g_hardlinks_lock); +} + +// Rebuilds the table without the slots of removed copies, which make every lookup longer. +static void hl_compact_locked(ddnatv_hl_table_t *t) { + ddnatv_hl_slot_t *keep = (ddnatv_hl_slot_t *)malloc(sizeof(ddnatv_hl_slot_t) * DDNATV_HL_MAX_USED); + if (!keep) return; + uint32_t n = 0; + for (uint32_t i = 0; i < DDNATV_HL_SLOTS && n < DDNATV_HL_MAX_USED; i++) { + if (t->slots[i].state == DDNATV_HL_USED) keep[n++] = t->slots[i]; + } + memset(t->slots, 0, sizeof(t->slots)); + for (uint32_t k = 0; k < n; k++) { + uint32_t i = hl_hash(keep[k].dev, keep[k].ino); + while (t->slots[i].state != DDNATV_HL_EMPTY) i = (i + 1) & (DDNATV_HL_SLOTS - 1); + t->slots[i] = keep[k]; + } + t->used = n; + t->deleted = 0; + free(keep); +} + +static int hl_put_locked(ddnatv_hl_table_t *t, const struct stat *st, uint64_t group_dev, uint64_t group_ino) { + long i = hl_find_locked(t, st->st_dev, st->st_ino); + if (i < 0) { + if (t->used >= DDNATV_HL_MAX_USED) return -1; + if (t->used + t->deleted >= DDNATV_HL_SLOTS - DDNATV_HL_SLOTS / 8) hl_compact_locked(t); + uint32_t j = hl_hash(st->st_dev, st->st_ino); + while (t->slots[j].state == DDNATV_HL_USED) j = (j + 1) & (DDNATV_HL_SLOTS - 1); + if (t->slots[j].state == DDNATV_HL_DELETED) t->deleted--; + i = (long)j; + t->used++; + } + ddnatv_hl_slot_t *s = &t->slots[i]; + s->dev = st->st_dev; + s->ino = st->st_ino; + s->mtime_sec = st->st_mtim.tv_sec; + s->mtime_nsec = st->st_mtim.tv_nsec; + s->group_dev = group_dev; + s->group_ino = group_ino; + s->nlink = 1; + s->state = DDNATV_HL_USED; + return 0; +} + +static void hl_remove_locked(ddnatv_hl_table_t *t, long i) { + t->slots[i].state = DDNATV_HL_DELETED; + t->used--; + t->deleted++; +} + +// Gives every copy of a group its size; a group of one is a plain file again. +static void hl_update_group_locked(ddnatv_hl_table_t *t, uint64_t group_dev, uint64_t group_ino) { + uint32_t n = 0; + long last = -1; + for (uint32_t i = 0; i < DDNATV_HL_SLOTS; i++) { + ddnatv_hl_slot_t *s = &t->slots[i]; + if (s->state == DDNATV_HL_USED && s->group_ino == group_ino && s->group_dev == group_dev) { + n++; + last = (long)i; + } + } + if (n == 1) { + hl_remove_locked(t, last); + return; + } + for (uint32_t i = 0; i < DDNATV_HL_SLOTS && n > 1; i++) { + ddnatv_hl_slot_t *s = &t->slots[i]; + if (s->state == DDNATV_HL_USED && s->group_ino == group_ino && s->group_dev == group_dev) s->nlink = n; + } +} + +// Removes the copy in slot i. A group reports the inode number of one of its copies (the original); +// when that copy goes, the group reports another one's: the number of a removed file can be given to +// a new file, which must not show up as linked to the copies that are left. +static void hl_remove_copy_locked(ddnatv_hl_table_t *t, long i) { + uint64_t group_dev = t->slots[i].group_dev, group_ino = t->slots[i].group_ino; + int reported = t->slots[i].dev == group_dev && t->slots[i].ino == group_ino; + hl_remove_locked(t, i); + if (reported) { + uint64_t new_dev = 0, new_ino = 0; + int found = 0; + for (uint32_t j = 0; j < DDNATV_HL_SLOTS; j++) { + ddnatv_hl_slot_t *s = &t->slots[j]; + if (s->state != DDNATV_HL_USED || s->group_ino != group_ino || s->group_dev != group_dev) continue; + if (!found) { + new_dev = s->dev; + new_ino = s->ino; + found = 1; + } + s->group_dev = new_dev; + s->group_ino = new_ino; + } + if (found) { + group_dev = new_dev; + group_ino = new_ino; + } + } + hl_update_group_locked(t, group_dev, group_ino); +} + +// Records that the file [copy] is an emulated hard link of [old]. +static void register_emulated_hardlink(const struct stat *old, const struct stat *copy) { + ddnatv_hl_table_t *t = hl_table(1); + if (!t) return; + int saved_errno = errno; + int fd = hl_write_begin(t); + uint64_t group_dev = old->st_dev, group_ino = old->st_ino; + long i = hl_find_locked(t, old->st_dev, old->st_ino); + if (i >= 0 && t->slots[i].mtime_sec == old->st_mtim.tv_sec) { + // Linking a copy again: the new copy joins the same group + group_dev = t->slots[i].group_dev; + group_ino = t->slots[i].group_ino; + } else { + // A stale slot of an earlier file with this inode number + if (i >= 0) hl_remove_copy_locked(t, i); + hl_put_locked(t, old, group_dev, group_ino); + } + if (hl_put_locked(t, copy, group_dev, group_ino) == 0 || hl_find_locked(t, old->st_dev, old->st_ino) >= 0) { + hl_update_group_locked(t, group_dev, group_ino); + } + hl_write_end(t, fd); + errno = saved_errno; +} + +// Forgets a copy whose name was removed; the last copy of a group goes back to a plain file. +static void unregister_emulated_hardlink(dev_t dev, ino_t ino) { + ddnatv_hl_table_t *t = hl_table(0); + if (!t || __atomic_load_n(&t->used, __ATOMIC_RELAXED) == 0) return; + int saved_errno = errno; + int fd = hl_write_begin(t); + long i = hl_find_locked(t, dev, ino); + if (i >= 0) hl_remove_copy_locked(t, i); + hl_write_end(t, fd); + errno = saved_errno; +} + +// Reads the slot of a copy without a lock: 1 and [out] when there is one. +static int hl_read(ddnatv_hl_table_t *t, uint64_t dev, uint64_t ino, ddnatv_hl_slot_t *out) { + for (int tries = 0; tries < 64; tries++) { + uint32_t seq = __atomic_load_n(&t->seq, __ATOMIC_ACQUIRE); + if (seq & 1) { + sched_yield(); + continue; + } + int found = 0; + uint32_t i = hl_hash(dev, ino); + for (uint32_t n = 0; n < DDNATV_HL_SLOTS; n++, i = (i + 1) & (DDNATV_HL_SLOTS - 1)) { + const volatile ddnatv_hl_slot_t *s = &t->slots[i]; + uint32_t state = s->state; + if (state == DDNATV_HL_EMPTY) break; + if (state == DDNATV_HL_USED && s->ino == ino && s->dev == dev) { + memcpy(out, (const void *)s, sizeof(*out)); + found = 1; + break; + } + } + __atomic_thread_fence(__ATOMIC_ACQUIRE); + if (__atomic_load_n(&t->seq, __ATOMIC_RELAXED) == seq) return found; + } + return 0; // a writer that does not finish: as if there were no links +} + +// Makes a stat() result of an emulated hard link look like a real one: the inode number of the +// original and the number of copies as the link count. +static void apply_emulated_hardlink(struct stat *st) { + if (!st || !hardlinks_maybe_any()) return; + ddnatv_hl_slot_t s; + if (!hl_read(g_hl_table, st->st_dev, st->st_ino, &s)) return; + if (s.mtime_sec != (int64_t)st->st_mtim.tv_sec) return; + if (s.nlink > (uint32_t)st->st_nlink) st->st_nlink = (nlink_t)s.nlink; + st->st_ino = (ino_t)s.group_ino; +} + +// The app's own user and group, which the hook presents as root: getuid() and getpwuid() say so, +// and stat() does as well, so that a program that compares a file's owner with getuid() sees the +// files it made as its own (git's "dubious ownership", sshd's /run/sshd, tmux's socket directory, +// ssh's ~/.ssh/config). -1 until known; a test sets them. +static uid_t g_app_uid = (uid_t)-1; +static gid_t g_app_gid = (gid_t)-1; + +static void load_app_ids(void) { +#ifdef DDNATV_HOST_TEST + uid_t uid = getuid(); + gid_t gid = getgid(); +#else + // The real ones (getuid() and syscall() are the hook's own, which answer 0) + static uid_t (*orig_getuid)(void) = NULL; + static gid_t (*orig_getgid)(void) = NULL; + if (!orig_getuid) orig_getuid = (uid_t (*)(void))dlsym(RTLD_NEXT, "getuid"); + if (!orig_getgid) orig_getgid = (gid_t (*)(void))dlsym(RTLD_NEXT, "getgid"); + if (!orig_getuid || !orig_getgid) return; + uid_t uid = orig_getuid(); + gid_t gid = orig_getgid(); +#endif + __atomic_store_n(&g_app_gid, gid, __ATOMIC_RELAXED); + __atomic_store_n(&g_app_uid, uid, __ATOMIC_RELAXED); +} + +static void present_owner_as_root(uid_t *uid, gid_t *gid) { + if (__atomic_load_n(&g_app_uid, __ATOMIC_RELAXED) == (uid_t)-1) load_app_ids(); + uid_t app_uid = __atomic_load_n(&g_app_uid, __ATOMIC_RELAXED); + gid_t app_gid = __atomic_load_n(&g_app_gid, __ATOMIC_RELAXED); + if (app_uid == (uid_t)-1) return; + if (uid && *uid == app_uid) *uid = 0; + if (gid && *gid == app_gid) *gid = 0; +} + +// What the stat() family returns: emulated hard links as real ones, the app's files as root's. +static void apply_stat_emulation(struct stat *st) { + if (!st) return; + apply_emulated_hardlink(st); + present_owner_as_root(&st->st_uid, &st->st_gid); +} + +static int real_fstatat(int dirfd, const char *path, struct stat *st, int flags) { +#ifdef DDNATV_HOST_TEST + return fstatat(dirfd, path, st, flags); +#else + static int (*orig_fstatat)(int, const char *, struct stat *, int) = NULL; + if (!orig_fstatat) orig_fstatat = (int (*)(int, const char *, struct stat *, int))dlsym(RTLD_NEXT, "fstatat"); + return orig_fstatat ? orig_fstatat(dirfd, path, st, flags) : fstatat(dirfd, path, st, flags); +#endif +} + +// Before a name is removed or replaced (unlink, rename over it): whether it is an emulated hard +// link, and which. Costs nothing until the process has made one. +static int lookup_emulated_hardlink_at(int dirfd, const char *path, dev_t *dev, ino_t *ino) { + if (!path || !hardlinks_maybe_any()) return 0; + int saved_errno = errno; + struct stat st; + int found = 0; + if (real_fstatat(dirfd, path, &st, AT_SYMLINK_NOFOLLOW) == 0) { + ddnatv_hl_slot_t s; + found = hl_read(g_hl_table, st.st_dev, st.st_ino, &s); + *dev = st.st_dev; + *ino = st.st_ino; + } + errno = saved_errno; + return found; +} + +static int ddnatv_emulate_hardlinkat(int olddirfd, const char *rold, int newdirfd, const char *rnew) { + if (!rold || !rnew) { + errno = EINVAL; + return -1; + } + struct stat st; +#ifdef DDNATV_HOST_TEST + if (fstatat(olddirfd, rold, &st, 0) != 0) { + return -1; + } +#else + static int (*orig_fstatat)(int, const char *, struct stat *, int) = NULL; + if (!orig_fstatat) orig_fstatat = (int (*)(int, const char *, struct stat *, int))dlsym(RTLD_NEXT, "fstatat"); + if (orig_fstatat) { + if (orig_fstatat(olddirfd, rold, &st, 0) != 0) return -1; + } else { + if (fstatat(olddirfd, rold, &st, 0) != 0) return -1; + } +#endif + if (S_ISDIR(st.st_mode)) { + errno = EPERM; + return -1; + } + + // Clean up stale dangling symlink or stale dead-process lock file at rnew +#ifndef DDNATV_HOST_TEST + struct stat lst; + int have_lst = 0; + if (orig_fstatat) { + if (orig_fstatat(newdirfd, rnew, &lst, AT_SYMLINK_NOFOLLOW) == 0) have_lst = 1; + } else { + if (fstatat(newdirfd, rnew, &lst, AT_SYMLINK_NOFOLLOW) == 0) have_lst = 1; + } + if (have_lst) { + if (S_ISLNK(lst.st_mode)) { + unlinkat(newdirfd, rnew, 0); + } else if (strstr(rnew, ".lock")) { + static int (*orig_openat)(int, const char *, int, ...) = NULL; + if (!orig_openat) orig_openat = (int (*)(int, const char *, int, ...))dlsym(RTLD_NEXT, "openat"); + int rfd = orig_openat ? orig_openat(newdirfd, rnew, O_RDONLY | O_CLOEXEC) + : openat(newdirfd, rnew, O_RDONLY | O_CLOEXEC); + if (rfd >= 0) { + char pbuf[32] = {0}; + ssize_t pr = read(rfd, pbuf, sizeof(pbuf) - 1); + close(rfd); + if (pr > 0) { + pid_t lpid = (pid_t)atoi(pbuf); + if (lpid > 0 && kill(lpid, 0) == -1 && errno == ESRCH) { + unlinkat(newdirfd, rnew, 0); + } + } else { + unlinkat(newdirfd, rnew, 0); + } + } + } + } +#endif + +#ifdef DDNATV_HOST_TEST + int fd_new = openat(newdirfd, rnew, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, st.st_mode & 0777); + if (fd_new < 0) return -1; + int fd_old = openat(olddirfd, rold, O_RDONLY | O_CLOEXEC); +#else + static int (*orig_openat)(int, const char *, int, ...) = NULL; + if (!orig_openat) orig_openat = (int (*)(int, const char *, int, ...))dlsym(RTLD_NEXT, "openat"); + int fd_new = orig_openat ? orig_openat(newdirfd, rnew, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, st.st_mode & 0777) + : openat(newdirfd, rnew, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, st.st_mode & 0777); + if (fd_new < 0) return -1; + int fd_old = orig_openat ? orig_openat(olddirfd, rold, O_RDONLY | O_CLOEXEC) + : openat(olddirfd, rold, O_RDONLY | O_CLOEXEC); +#endif + if (fd_old < 0) { + int err = errno; + close(fd_new); + unlinkat(newdirfd, rnew, 0); + errno = err; + return -1; + } + + ssize_t sent = 0; + int copy_failed = 0; + while (sent < st.st_size) { + ssize_t s = sendfile(fd_new, fd_old, NULL, (size_t)(st.st_size - sent)); + if (s > 0) { + sent += s; + } else { + break; + } + } + if (sent < st.st_size) { + char cbuf[16384]; + ssize_t nread; + while ((nread = read(fd_old, cbuf, sizeof(cbuf))) > 0) { + ssize_t nwritten = 0; + while (nwritten < nread) { + ssize_t w = write(fd_new, cbuf + nwritten, nread - nwritten); + if (w < 0) { + copy_failed = 1; + break; + } + nwritten += w; + } + if (copy_failed) break; + } + if (nread < 0) copy_failed = 1; + } + + fchmod(fd_new, st.st_mode & 0777); + // A hard link shares the times of the original (tar, dpkg and make compare them). + const struct timespec times[2] = { st.st_atim, st.st_mtim }; + futimens(fd_new, times); + struct stat st_new; + memset(&st_new, 0, sizeof(st_new)); +#ifndef DDNATV_HOST_TEST + static int (*orig_fstat)(int, struct stat *) = NULL; + if (!orig_fstat) orig_fstat = (int (*)(int, struct stat *))dlsym(RTLD_NEXT, "fstat"); + if (orig_fstat) orig_fstat(fd_new, &st_new); + else fstat(fd_new, &st_new); +#else + fstat(fd_new, &st_new); +#endif + + close(fd_old); + close(fd_new); + + if (copy_failed) { + unlinkat(newdirfd, rnew, 0); + errno = EIO; + return -1; + } + + register_emulated_hardlink(&st, &st_new); + return 0; +} + +static inline int ddnatv_emulate_hardlink(const char *rold, const char *rnew) { + return ddnatv_emulate_hardlinkat(AT_FDCWD, rold, AT_FDCWD, rnew); +} diff --git a/tools/nativeguest/hook/identity.c b/tools/nativeguest/hook/identity.c new file mode 100644 index 0000000000..9599d7e0b2 --- /dev/null +++ b/tools/nativeguest/hook/identity.c @@ -0,0 +1,838 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Fake root: user and group ids, the shadow lock, and passwd/group lookups from the rootfs. + +#ifndef DDNATV_HOST_TEST + +// Fakeroot identity hooks for APT and DPKG to operate without superuser restrictions +uid_t getuid(void) { return 0; } +uid_t geteuid(void) { return 0; } +gid_t getgid(void) { return 0; } +gid_t getegid(void) { return 0; } + +int setuid(uid_t uid) { (void)uid; return 0; } +int seteuid(uid_t uid) { (void)uid; return 0; } +int setgid(gid_t gid) { (void)gid; return 0; } +int setegid(gid_t gid) { (void)gid; return 0; } +int setreuid(uid_t ruid, uid_t euid) { (void)ruid; (void)euid; return 0; } +int setregid(gid_t rgid, gid_t egid) { (void)rgid; (void)egid; return 0; } +int setresuid(uid_t ruid, uid_t euid, uid_t suid) { (void)ruid; (void)euid; (void)suid; return 0; } +int setresgid(gid_t rgid, gid_t egid, gid_t sgid) { (void)rgid; (void)egid; (void)sgid; return 0; } + +int getgroups(int size, gid_t list[]) { + if (size > 0 && list != NULL) list[0] = 0; + return 1; +} +// What programs built with _FORTIFY_SOURCE call when the buffer's size is known (the check is +// glibc's): without it the count came from the hook (1) and the list from the kernel (the app's +// groups, more than one), and sshd ended the login on "getgroups: Invalid argument" +extern void __chk_fail(void) __attribute__((__noreturn__)); +int __getgroups_chk(int size, gid_t list[], size_t listlen) { + if (size > 0 && (size_t)size * sizeof(gid_t) > listlen) __chk_fail(); + return getgroups(size, list); +} +int setgroups(size_t size, const gid_t *list) { (void)size; (void)list; return 0; } +int initgroups(const char *user, gid_t group) { (void)user; (void)group; return 0; } + +int getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups) { + (void)user; + if (!ngroups) { + errno = EINVAL; + return -1; + } + if (*ngroups > 0 && groups) { + groups[0] = group; + *ngroups = 1; + return 1; + } + // Too small (a caller asking for the size first): -1 and the number needed, as glibc does. + *ngroups = 1; + return -1; +} + +// The session's host name, as proot's --kernel-release gave it: Steam names the device after it +int uname(struct utsname *buf) { + static int (*orig_uname)(struct utsname *) = NULL; + if (!orig_uname) orig_uname = (int (*)(struct utsname *))dlsym(RTLD_NEXT, "uname"); + int ret = orig_uname ? orig_uname(buf) : -1; + if (ret == 0 && buf && g_hostname[0]) { + ddnatv_copy_cstr(buf->nodename, sizeof(buf->nodename), g_hostname); + ddnatv_copy_cstr(buf->domainname, sizeof(buf->domainname), "localdomain"); + } + return ret; +} + +int gethostname(char *name, size_t len) { + static int (*orig_gethostname)(char *, size_t) = NULL; + if (!orig_gethostname) orig_gethostname = (int (*)(char *, size_t))dlsym(RTLD_NEXT, "gethostname"); + if (!g_hostname[0]) return orig_gethostname ? orig_gethostname(name, len) : -1; + size_t n = strlen(g_hostname) + 1; + if (!name || n > len) { + if (name && len) memcpy(name, g_hostname, len); + errno = ENAMETOOLONG; + return -1; + } + memcpy(name, g_hostname, n); + return 0; +} + +#endif /* !DDNATV_HOST_TEST */ + +static uid_t real_uid(void) { +#ifdef DDNATV_HOST_TEST + return 10000; +#else + static uid_t (*orig_getuid)(void) = NULL; + if (!orig_getuid) orig_getuid = (uid_t (*)(void))dlsym(RTLD_NEXT, "getuid"); + return orig_getuid ? orig_getuid() : 0; +#endif +} + +static gid_t real_gid(void) { +#ifdef DDNATV_HOST_TEST + return 10000; +#else + static gid_t (*orig_getgid)(void) = NULL; + if (!orig_getgid) orig_getgid = (gid_t (*)(void))dlsym(RTLD_NEXT, "getgid"); + return orig_getgid ? orig_getgid() : 0; +#endif +} + +#ifdef DDNATV_HOST_TEST +#define DDNATV_NSS_SYM(name) hook_##name +#define DDNATV_SHADOW_SYM(name) hook_##name +static int g_test_orig_nss_r_ret = 0; +static int g_test_orig_nss_r_found = 0; + +int hook_link(const char *oldpath, const char *newpath) { + int ret = link(oldpath, newpath); + if (ret != 0 && (errno == EXDEV || errno == EPERM || errno == EACCES || errno == ENOTSUP || errno == ENOSYS)) { + ret = ddnatv_emulate_hardlink(oldpath, newpath); + } + return ret; +} + +int hook_linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) { + int ret = linkat(olddirfd, oldpath, newdirfd, newpath, flags); + if (ret != 0 && (errno == EXDEV || errno == EPERM || errno == EACCES || errno == ENOTSUP || errno == ENOSYS)) { + ret = ddnatv_emulate_hardlinkat(olddirfd, oldpath, newdirfd, newpath); + } + return ret; +} + +int hook_stat(const char *path, struct stat *st) { + int ret = stat(path, st); + if (ret == 0) apply_stat_emulation(st); + return ret; +} + +int hook_unlink(const char *path) { + dev_t hl_dev = 0; + ino_t hl_ino = 0; + int hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, path, &hl_dev, &hl_ino); + int ret = unlink(path); + if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino); + return ret; +} +#else +#define DDNATV_NSS_SYM(name) name +#define DDNATV_SHADOW_SYM(name) name +#endif + +// Hook lckpwdf and ulckpwdf (shadow password database locking) +// In glibc, lckpwdf() attempts to open /etc/.pwd.lock directly using internal syscalls +// which fails with EROFS on Android because host /etc is mounted read-only. +static int g_pwd_lock_fd = -1; +static pthread_mutex_t g_pwd_lock_mutex = PTHREAD_MUTEX_INITIALIZER; + +int DDNATV_SHADOW_SYM(lckpwdf)(void) { +#ifndef DDNATV_HOST_TEST + init_ddnatv_hook(); + pthread_mutex_lock(&g_pwd_lock_mutex); + if (g_pwd_lock_fd != -1) { + pthread_mutex_unlock(&g_pwd_lock_mutex); + return 0; // Already locked + } + char buf[PATH_MAX]; + const char *target = rewrite_path("/etc/.pwd.lock", buf, sizeof(buf)); + static int (*orig_open)(const char *, int, ...) = NULL; + if (!orig_open) orig_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open"); + if (orig_open && target) { + g_pwd_lock_fd = orig_open(target, O_WRONLY | O_CREAT | O_CLOEXEC, 0600); + if (g_pwd_lock_fd >= 0) { + struct flock fl; + memset(&fl, 0, sizeof(fl)); + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + if (fcntl(g_pwd_lock_fd, F_SETLK, &fl) != 0) { + int saved = errno; + close(g_pwd_lock_fd); + g_pwd_lock_fd = -1; + pthread_mutex_unlock(&g_pwd_lock_mutex); + errno = saved; + return -1; + } + } + } + if (g_pwd_lock_fd < 0) { + g_pwd_lock_fd = -2; // In-memory lock held + } + pthread_mutex_unlock(&g_pwd_lock_mutex); +#endif + return 0; +} + +int DDNATV_SHADOW_SYM(ulckpwdf)(void) { +#ifndef DDNATV_HOST_TEST + pthread_mutex_lock(&g_pwd_lock_mutex); + if (g_pwd_lock_fd >= 0) { + close(g_pwd_lock_fd); + } + g_pwd_lock_fd = -1; + pthread_mutex_unlock(&g_pwd_lock_mutex); +#endif + return 0; +} + + +// Group lookup helper: reads $DDNATV_ROOT/etc/group or falls back to known entries +static int ddnatv_lookup_group(const char *search_name, gid_t search_gid, int by_name, + struct group *grp, char *buf, size_t buflen) { + if (!grp || !buf || buflen < 64) { + return ERANGE; + } + + int found = 0; + char found_name[64] = {0}; + char found_pass[32] = {0}; + gid_t found_gid = 0; + char found_members[256] = {0}; + + // 1. Try reading /etc/group from The native userland rootfs +#ifndef DDNATV_HOST_TEST + char path_buf[PATH_MAX]; + const char *grp_path = rewrite_path("/etc/group", path_buf, sizeof(path_buf)); + static int (*orig_open)(const char *, int, ...) = NULL; + if (!orig_open) orig_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open"); + int fd = (orig_open && grp_path) ? orig_open(grp_path, O_RDONLY | O_CLOEXEC) : -1; + if (fd >= 0) { + char file_buf[8192]; + ssize_t n = read(fd, file_buf, sizeof(file_buf) - 1); + close(fd); + if (n > 0) { + file_buf[n] = '\0'; + char *line = file_buf; + while (*line && !found) { + char *next_line = strchr(line, '\n'); + if (next_line) { + *next_line = '\0'; + } + size_t llen = strlen(line); + if (llen > 0 && line[llen - 1] == '\r') line[llen - 1] = '\0'; + + // format: name:pass:gid:members + char *p1 = strchr(line, ':'); + if (p1) { + *p1 = '\0'; + char *p2 = strchr(p1 + 1, ':'); + if (p2) { + *p2 = '\0'; + char *p3 = strchr(p2 + 1, ':'); + if (p3) { + *p3 = '\0'; + const char *gname = line; + const char *gpass = p1 + 1; + gid_t ggid = (gid_t)strtoul(p2 + 1, NULL, 10); + const char *gmem = p3 + 1; + + if (by_name && search_name && strcmp(gname, search_name) == 0) { + snprintf(found_name, sizeof(found_name), "%s", gname); + snprintf(found_pass, sizeof(found_pass), "%s", gpass); + found_gid = ggid; + snprintf(found_members, sizeof(found_members), "%s", gmem); + found = 1; + } else if (!by_name && ggid == search_gid) { + snprintf(found_name, sizeof(found_name), "%s", gname); + snprintf(found_pass, sizeof(found_pass), "%s", gpass); + found_gid = ggid; + snprintf(found_members, sizeof(found_members), "%s", gmem); + found = 1; + } + } + } + } + if (!next_line) break; + line = next_line + 1; + } + } + } +#endif + + // 2. Fallbacks for well-known groups if absent from file + if (!found) { + if (by_name) { + if (!search_name) return ENOENT; + if (strcmp(search_name, "root") == 0 || strcmp(search_name, "ddnatv") == 0) { + snprintf(found_name, sizeof(found_name), "%s", search_name); + snprintf(found_pass, sizeof(found_pass), "x"); + found_gid = 0; + snprintf(found_members, sizeof(found_members), "%s", search_name); + found = 1; + } else if (strcmp(search_name, "_ssh") == 0) { + snprintf(found_name, sizeof(found_name), "_ssh"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_gid = 100; + found = 1; + } else if (strcmp(search_name, "ssh") == 0) { + snprintf(found_name, sizeof(found_name), "ssh"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_gid = 101; + found = 1; + } else if (strcmp(search_name, "nogroup") == 0 || strcmp(search_name, "nobody") == 0) { + snprintf(found_name, sizeof(found_name), "%s", search_name); + snprintf(found_pass, sizeof(found_pass), "x"); + found_gid = 65534; + found = 1; + } + } else { + if (search_gid == 0 || search_gid == real_gid()) { + snprintf(found_name, sizeof(found_name), "root"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_gid = search_gid; + snprintf(found_members, sizeof(found_members), "root"); + found = 1; + } + } + } + + if (!found) { + return ENOENT; + } + + // 3. Assemble struct group into caller's buf + size_t mem_ptr_count = 1; + char *m_copy = found_members; + while (*m_copy) { + if (*m_copy == ',') mem_ptr_count++; + m_copy++; + } + if (found_members[0] != '\0') mem_ptr_count++; + + size_t mem_array_bytes = (mem_ptr_count + 1) * sizeof(char *); + mem_array_bytes = (mem_array_bytes + sizeof(void *) - 1) & ~(sizeof(void *) - 1); + + size_t name_len = strlen(found_name) + 1; + size_t pass_len = strlen(found_pass) + 1; + size_t mem_str_len = strlen(found_members) + 1; + + if (mem_array_bytes + name_len + pass_len + mem_str_len > buflen) { + return ERANGE; + } + + char **mem_array = (char **)buf; + char *str_cursor = buf + mem_array_bytes; + + grp->gr_name = str_cursor; + memcpy(str_cursor, found_name, name_len); + str_cursor += name_len; + + grp->gr_passwd = str_cursor; + memcpy(str_cursor, found_pass, pass_len); + str_cursor += pass_len; + + grp->gr_gid = found_gid; + + size_t m_idx = 0; + if (found_members[0] != '\0') { + char *m_tok = str_cursor; + memcpy(str_cursor, found_members, mem_str_len); + str_cursor += mem_str_len; + + char *curr = m_tok; + while (curr && *curr) { + char *comma = strchr(curr, ','); + if (comma) { + *comma = '\0'; + } + if (*curr) { + mem_array[m_idx++] = curr; + } + curr = comma ? comma + 1 : NULL; + } + } + mem_array[m_idx] = NULL; + grp->gr_mem = mem_array; + + return 0; +} + +// Passwd lookup helper: reads $DDNATV_ROOT/etc/passwd or falls back to known entries +static int ddnatv_lookup_passwd(const char *search_name, uid_t search_uid, int by_name, + struct passwd *pwd, char *buf, size_t buflen) { + if (!pwd || !buf || buflen < 128) { + return ERANGE; + } + + int found = 0; + char found_name[64] = {0}; + char found_pass[32] = {0}; + uid_t found_uid = 0; + gid_t found_gid = 0; + char found_gecos[64] = {0}; + char found_dir[128] = {0}; + char found_shell[64] = {0}; + + // 1. Try reading /etc/passwd from The native userland rootfs +#ifndef DDNATV_HOST_TEST + char path_buf[PATH_MAX]; + const char *pwd_path = rewrite_path("/etc/passwd", path_buf, sizeof(path_buf)); + static int (*orig_open)(const char *, int, ...) = NULL; + if (!orig_open) orig_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open"); + int fd = (orig_open && pwd_path) ? orig_open(pwd_path, O_RDONLY | O_CLOEXEC) : -1; + if (fd >= 0) { + char file_buf[8192]; + ssize_t n = read(fd, file_buf, sizeof(file_buf) - 1); + close(fd); + if (n > 0) { + file_buf[n] = '\0'; + char *line = file_buf; + while (*line && !found) { + char *next_line = strchr(line, '\n'); + if (next_line) { + *next_line = '\0'; + } + size_t llen = strlen(line); + if (llen > 0 && line[llen - 1] == '\r') line[llen - 1] = '\0'; + + // format: name:pass:uid:gid:gecos:dir:shell + char *f[7] = {0}; + char *curr = line; + int f_idx = 0; + while (curr && f_idx < 7) { + f[f_idx++] = curr; + char *col = strchr(curr, ':'); + if (col && f_idx < 7) { + *col = '\0'; + curr = col + 1; + } else { + break; + } + } + if (f_idx >= 7) { + const char *uname = f[0]; + const char *upass = f[1]; + uid_t uuid = (uid_t)strtoul(f[2], NULL, 10); + gid_t ugid = (gid_t)strtoul(f[3], NULL, 10); + const char *ugecos = f[4]; + const char *udir = f[5]; + const char *ushell = f[6]; + + if (by_name && search_name && strcmp(uname, search_name) == 0) { + snprintf(found_name, sizeof(found_name), "%s", uname); + snprintf(found_pass, sizeof(found_pass), "%s", upass); + found_uid = uuid; + found_gid = ugid; + snprintf(found_gecos, sizeof(found_gecos), "%s", ugecos); + snprintf(found_dir, sizeof(found_dir), "%s", udir); + snprintf(found_shell, sizeof(found_shell), "%s", ushell); + found = 1; + } else if (!by_name && uuid == search_uid) { + snprintf(found_name, sizeof(found_name), "%s", uname); + snprintf(found_pass, sizeof(found_pass), "%s", upass); + found_uid = uuid; + found_gid = ugid; + snprintf(found_gecos, sizeof(found_gecos), "%s", ugecos); + snprintf(found_dir, sizeof(found_dir), "%s", udir); + snprintf(found_shell, sizeof(found_shell), "%s", ushell); + found = 1; + } + } + if (!next_line) break; + line = next_line + 1; + } + } + } +#endif + + // 2. Fallbacks for well-known users if absent from file + if (!found) { + if (by_name) { + if (!search_name) return ENOENT; + if (strcmp(search_name, "root") == 0) { + snprintf(found_name, sizeof(found_name), "root"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_uid = 0; + found_gid = 0; + snprintf(found_gecos, sizeof(found_gecos), "root"); + snprintf(found_dir, sizeof(found_dir), "/root"); + snprintf(found_shell, sizeof(found_shell), "/bin/bash"); + found = 1; + } else if (strcmp(search_name, "ddnatv") == 0) { + snprintf(found_name, sizeof(found_name), "ddnatv"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_uid = 0; + found_gid = 0; + snprintf(found_gecos, sizeof(found_gecos), "The native userland"); + snprintf(found_dir, sizeof(found_dir), "/home/ddnatv"); + snprintf(found_shell, sizeof(found_shell), "/bin/bash"); + found = 1; + } else if (strcmp(search_name, "_ssh") == 0) { + snprintf(found_name, sizeof(found_name), "_ssh"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_uid = 100; + found_gid = 100; + snprintf(found_gecos, sizeof(found_gecos), "OpenSSH daemon"); + snprintf(found_dir, sizeof(found_dir), "/run/sshd"); + snprintf(found_shell, sizeof(found_shell), "/usr/sbin/nologin"); + found = 1; + } else if (strcmp(search_name, "sshd") == 0) { + snprintf(found_name, sizeof(found_name), "sshd"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_uid = 101; + found_gid = 101; + snprintf(found_gecos, sizeof(found_gecos), "Privilege-separated SSH"); + snprintf(found_dir, sizeof(found_dir), "/run/sshd"); + snprintf(found_shell, sizeof(found_shell), "/usr/sbin/nologin"); + found = 1; + } else if (strcmp(search_name, "nobody") == 0) { + snprintf(found_name, sizeof(found_name), "nobody"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_uid = 65534; + found_gid = 65534; + snprintf(found_gecos, sizeof(found_gecos), "nobody"); + snprintf(found_dir, sizeof(found_dir), "/nonexistent"); + snprintf(found_shell, sizeof(found_shell), "/usr/sbin/nologin"); + found = 1; + } + } else { + if (search_uid == 0 || search_uid == real_uid()) { + snprintf(found_name, sizeof(found_name), "root"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_uid = search_uid; + found_gid = 0; + snprintf(found_gecos, sizeof(found_gecos), "root"); + snprintf(found_dir, sizeof(found_dir), "/home"); + snprintf(found_shell, sizeof(found_shell), "/bin/bash"); + found = 1; + } + } + } + + if (!found) { + return ENOENT; + } + + // 3. Assemble struct passwd into caller's buf + size_t name_len = strlen(found_name) + 1; + size_t pass_len = strlen(found_pass) + 1; + size_t gecos_len = strlen(found_gecos) + 1; + size_t dir_len = strlen(found_dir) + 1; + size_t shell_len = strlen(found_shell) + 1; + + size_t total_str_bytes = name_len + pass_len + gecos_len + dir_len + shell_len; + if (total_str_bytes > buflen) { + return ERANGE; + } + + char *str_cursor = buf; + + pwd->pw_name = str_cursor; + memcpy(str_cursor, found_name, name_len); + str_cursor += name_len; + + pwd->pw_passwd = str_cursor; + memcpy(str_cursor, found_pass, pass_len); + str_cursor += pass_len; + + pwd->pw_uid = found_uid; + pwd->pw_gid = found_gid; + + pwd->pw_gecos = str_cursor; + memcpy(str_cursor, found_gecos, gecos_len); + str_cursor += gecos_len; + + pwd->pw_dir = str_cursor; + memcpy(str_cursor, found_dir, dir_len); + str_cursor += dir_len; + + pwd->pw_shell = str_cursor; + memcpy(str_cursor, found_shell, shell_len); + str_cursor += shell_len; + + return 0; +} + +#ifndef DDNATV_HOST_TEST +// The shadow database (/etc/shadow) of the rootfs. libc reads the real /etc/shadow of the machine, +// which Android does not have: pam_unix's account check for root found no entry and answered +// "Authentication failure", so chfn, chsh and `adduser --gecos` (the installation of PostgreSQL) +// failed. The fields are "name:hash:lastchange:min:max:warn:inactive:expire:flag", numbers empty +// when unset (libc stores -1 then). +static int ddnatv_lookup_shadow(const char *name, struct spwd *sp, char *buf, size_t buflen) { + if (!name || !sp || !buf) return EINVAL; + char path_buf[PATH_MAX]; + const char *path = rewrite_path("/etc/shadow", path_buf, sizeof(path_buf)); + static int (*orig_open)(const char *, int, ...) = NULL; + if (!orig_open) orig_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open"); + int fd = (orig_open && path) ? orig_open(path, O_RDONLY | O_CLOEXEC) : -1; + if (fd < 0) return ENOENT; + FILE *f = fdopen(fd, "r"); + if (!f) { + close(fd); + return ENOENT; + } + size_t name_len = strlen(name); + char line[1024]; + int err = ENOENT; + while (fgets(line, sizeof(line), f)) { + if (strncmp(line, name, name_len) != 0 || line[name_len] != ':') continue; + line[strcspn(line, "\r\n")] = '\0'; + char *fields[9] = {0}; + int n = 0; + char *cursor = line; + while (n < 9) { + fields[n++] = cursor; + char *colon = strchr(cursor, ':'); + if (!colon) break; + *colon = '\0'; + cursor = colon + 1; + } + size_t need = strlen(fields[0]) + 1 + strlen(fields[1] ? fields[1] : "") + 1; + if (n < 2 || need > buflen) { + err = n < 2 ? ENOENT : ERANGE; + break; + } + char *out = buf; + sp->sp_namp = out; + memcpy(out, fields[0], strlen(fields[0]) + 1); + out += strlen(fields[0]) + 1; + sp->sp_pwdp = out; + memcpy(out, fields[1], strlen(fields[1]) + 1); + long number[6]; + for (int i = 0; i < 6; i++) { + const char *v = (2 + i < n) ? fields[2 + i] : NULL; + number[i] = (v && v[0] != '\0') ? strtol(v, NULL, 10) : -1; + } + sp->sp_lstchg = number[0]; + sp->sp_min = number[1]; + sp->sp_max = number[2]; + sp->sp_warn = number[3]; + sp->sp_inact = number[4]; + sp->sp_expire = number[5]; + sp->sp_flag = (8 < n && fields[8][0] != '\0') ? strtoul(fields[8], NULL, 10) : (unsigned long)-1; + err = 0; + break; + } + fclose(f); + return err; +} + +int getspnam_r(const char *name, struct spwd *spbuf, char *buf, size_t buflen, struct spwd **result) { + static int (*orig_getspnam_r)(const char *, struct spwd *, char *, size_t, struct spwd **) = NULL; + if (!orig_getspnam_r) orig_getspnam_r = (int (*)(const char *, struct spwd *, char *, size_t, struct spwd **))dlsym(RTLD_NEXT, "getspnam_r"); + int ret = ENOENT; + if (orig_getspnam_r) { + ret = orig_getspnam_r(name, spbuf, buf, buflen, result); + if ((ret == 0 && result && *result) || ret == ERANGE) return ret; + } + int err = ddnatv_lookup_shadow(name, spbuf, buf, buflen); + if (err == 0) { + if (result) *result = spbuf; + return 0; + } + if (result) *result = NULL; + return err == ENOENT ? (ret != 0 ? ret : ENOENT) : err; +} + +struct spwd *getspnam(const char *name) { + static struct spwd entry; + static char entry_buf[1024]; + struct spwd *found = NULL; + int err = getspnam_r(name, &entry, entry_buf, sizeof(entry_buf), &found); + if (err != 0) { + errno = err; + return NULL; + } + return found; +} +#endif /* !DDNATV_HOST_TEST */ + +static struct group s_grp_storage; +static char s_grp_buf[2048]; +static struct passwd s_pwd_storage; +static char s_pwd_buf[2048]; + +struct group *DDNATV_NSS_SYM(getgrgid)(gid_t gid) { +#ifndef DDNATV_HOST_TEST + static struct group *(*orig_getgrgid)(gid_t) = NULL; + if (!orig_getgrgid) orig_getgrgid = (struct group *(*)(gid_t))dlsym(RTLD_NEXT, "getgrgid"); + struct group *res = orig_getgrgid ? orig_getgrgid(gid) : NULL; + if (res) return res; +#endif + + int err = ddnatv_lookup_group(NULL, gid, 0, &s_grp_storage, s_grp_buf, sizeof(s_grp_buf)); + if (err == 0) return &s_grp_storage; + return NULL; +} + +struct group *DDNATV_NSS_SYM(getgrnam)(const char *name) { +#ifndef DDNATV_HOST_TEST + static struct group *(*orig_getgrnam)(const char *) = NULL; + if (!orig_getgrnam) orig_getgrnam = (struct group *(*)(const char *))dlsym(RTLD_NEXT, "getgrnam"); + struct group *res = orig_getgrnam ? orig_getgrnam(name) : NULL; + if (res) return res; +#endif + + int err = ddnatv_lookup_group(name, 0, 1, &s_grp_storage, s_grp_buf, sizeof(s_grp_buf)); + if (err == 0) return &s_grp_storage; + return NULL; +} + +int DDNATV_NSS_SYM(getgrgid_r)(gid_t gid, struct group *grp, char *buf, size_t buflen, struct group **result) { +#ifndef DDNATV_HOST_TEST + static int (*orig_getgrgid_r)(gid_t, struct group *, char *, size_t, struct group **) = NULL; + if (!orig_getgrgid_r) orig_getgrgid_r = (int (*)(gid_t, struct group *, char *, size_t, struct group **))dlsym(RTLD_NEXT, "getgrgid_r"); + int ret = orig_getgrgid_r ? orig_getgrgid_r(gid, grp, buf, buflen, result) : ENOENT; +#else + int ret = g_test_orig_nss_r_ret; + if (result) *result = (ret == 0 && g_test_orig_nss_r_found) ? grp : NULL; +#endif + if (ret == 0 && result && *result != NULL) return 0; + if (ret != 0 && ret != ENOENT) { + if (result) *result = NULL; + return ret; + } + + int err = ddnatv_lookup_group(NULL, gid, 0, grp, buf, buflen); + if (err == 0) { + if (result) *result = grp; + return 0; + } + if (result) *result = NULL; + return (err == ERANGE) ? ERANGE : 0; +} + +int DDNATV_NSS_SYM(getgrnam_r)(const char *name, struct group *grp, char *buf, size_t buflen, struct group **result) { +#ifndef DDNATV_HOST_TEST + static int (*orig_getgrnam_r)(const char *, struct group *, char *, size_t, struct group **) = NULL; + if (!orig_getgrnam_r) orig_getgrnam_r = (int (*)(const char *, struct group *, char *, size_t, struct group **))dlsym(RTLD_NEXT, "getgrnam_r"); + int ret = orig_getgrnam_r ? orig_getgrnam_r(name, grp, buf, buflen, result) : ENOENT; +#else + int ret = g_test_orig_nss_r_ret; + if (result) *result = (ret == 0 && g_test_orig_nss_r_found) ? grp : NULL; +#endif + if (ret == 0 && result && *result != NULL) return 0; + if (ret != 0 && ret != ENOENT) { + if (result) *result = NULL; + return ret; + } + + int err = ddnatv_lookup_group(name, 0, 1, grp, buf, buflen); + if (err == 0) { + if (result) *result = grp; + return 0; + } + if (result) *result = NULL; + return (err == ERANGE) ? ERANGE : 0; +} + +// sshd's privilege-separated user is root's ids: The native userland cannot drop privileges (setuid() and +// setgid() only pretend to), and sshd's unprivileged part checks after dropping them that it cannot +// take them back, which ended it ("permanently_set_uid: was able to restore old [e]gid"). Like apt's +// sandbox user (APT::Sandbox::User "root"), it then runs as the rest does. +// +// The app's own ids are root's as well: the rootfs lists root with them for proot, and a server +// that allows "localuser:root" compares getpwnam("root") with a peer's credentials, which the hook +// reports as 0. Xwayland refused gamescope's own connection when the two disagreed. +static struct passwd *present_privsep_user_as_root(struct passwd *pwd) { + if (pwd && pwd->pw_name && strcmp(pwd->pw_name, "sshd") == 0) { + pwd->pw_uid = 0; + pwd->pw_gid = 0; + } + if (pwd && pwd->pw_uid == real_uid()) pwd->pw_uid = 0; + if (pwd && pwd->pw_gid == real_gid()) pwd->pw_gid = 0; + return pwd; +} + +struct passwd *DDNATV_NSS_SYM(getpwuid)(uid_t uid) { +#ifndef DDNATV_HOST_TEST + static struct passwd *(*orig_getpwuid)(uid_t) = NULL; + if (!orig_getpwuid) orig_getpwuid = (struct passwd *(*)(uid_t))dlsym(RTLD_NEXT, "getpwuid"); + struct passwd *res = orig_getpwuid ? orig_getpwuid(uid) : NULL; + if (res) return present_privsep_user_as_root(res); +#endif + + int err = ddnatv_lookup_passwd(NULL, uid, 0, &s_pwd_storage, s_pwd_buf, sizeof(s_pwd_buf)); + if (err == 0) return present_privsep_user_as_root(&s_pwd_storage); + return NULL; +} + +struct passwd *DDNATV_NSS_SYM(getpwnam)(const char *name) { +#ifndef DDNATV_HOST_TEST + static struct passwd *(*orig_getpwnam)(const char *) = NULL; + if (!orig_getpwnam) orig_getpwnam = (struct passwd *(*)(const char *))dlsym(RTLD_NEXT, "getpwnam"); + struct passwd *res = orig_getpwnam ? orig_getpwnam(name) : NULL; + if (res) return present_privsep_user_as_root(res); +#endif + + int err = ddnatv_lookup_passwd(name, 0, 1, &s_pwd_storage, s_pwd_buf, sizeof(s_pwd_buf)); + if (err == 0) return present_privsep_user_as_root(&s_pwd_storage); + return NULL; +} + +int DDNATV_NSS_SYM(getpwuid_r)(uid_t uid, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result) { +#ifndef DDNATV_HOST_TEST + static int (*orig_getpwuid_r)(uid_t, struct passwd *, char *, size_t, struct passwd **) = NULL; + if (!orig_getpwuid_r) orig_getpwuid_r = (int (*)(uid_t, struct passwd *, char *, size_t, struct passwd **))dlsym(RTLD_NEXT, "getpwuid_r"); + int ret = orig_getpwuid_r ? orig_getpwuid_r(uid, pwd, buf, buflen, result) : ENOENT; +#else + int ret = g_test_orig_nss_r_ret; + if (result) *result = (ret == 0 && g_test_orig_nss_r_found) ? pwd : NULL; +#endif + if (ret == 0 && result && *result != NULL) { + present_privsep_user_as_root(*result); + return 0; + } + if (ret != 0 && ret != ENOENT) { + if (result) *result = NULL; + return ret; + } + + int err = ddnatv_lookup_passwd(NULL, uid, 0, pwd, buf, buflen); + if (err == 0) { + present_privsep_user_as_root(pwd); + if (result) *result = pwd; + return 0; + } + if (result) *result = NULL; + return (err == ERANGE) ? ERANGE : 0; +} + +int DDNATV_NSS_SYM(getpwnam_r)(const char *name, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result) { +#ifndef DDNATV_HOST_TEST + static int (*orig_getpwnam_r)(const char *, struct passwd *, char *, size_t, struct passwd **) = NULL; + if (!orig_getpwnam_r) orig_getpwnam_r = (int (*)(const char *, struct passwd *, char *, size_t, struct passwd **))dlsym(RTLD_NEXT, "getpwnam_r"); + int ret = orig_getpwnam_r ? orig_getpwnam_r(name, pwd, buf, buflen, result) : ENOENT; +#else + int ret = g_test_orig_nss_r_ret; + if (result) *result = (ret == 0 && g_test_orig_nss_r_found) ? pwd : NULL; +#endif + if (ret == 0 && result && *result != NULL) { + present_privsep_user_as_root(*result); + return 0; + } + if (ret != 0 && ret != ENOENT) { + if (result) *result = NULL; + return ret; + } + + int err = ddnatv_lookup_passwd(name, 0, 1, pwd, buf, buflen); + if (err == 0) { + present_privsep_user_as_root(pwd); + if (result) *result = pwd; + return 0; + } + if (result) *result = NULL; + return (err == ERANGE) ? ERANGE : 0; +} diff --git a/tools/nativeguest/hook/net.c b/tools/nativeguest/hook/net.c new file mode 100644 index 0000000000..19424c5796 --- /dev/null +++ b/tools/nativeguest/hook/net.c @@ -0,0 +1,1054 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Name resolution: DNS packets, the network's DNS servers, the fallback resolver, getaddrinfo() +// and the resolver state, and DNS queries to loopback redirected to a real server. + +// ---- DNS packets and servers ------------------------------------------------------------- +// Pure functions (no syscalls apart from if_nametoindex), so tests/native can check them directly. + +#define DDNATV_DNS_TYPE_A 1 +#define DDNATV_DNS_TYPE_AAAA 28 +#define DDNATV_DNS_MAX_SERVERS 8 + +typedef struct { + struct sockaddr_storage addr; + socklen_t len; +} ddnatv_dns_server_t; + +// Fills `server` with the DNS server `text` (an IPv4 or IPv6 address, IPv6 optionally with a +// "%interface" scope) on port 53. Returns 0 for loopback and unspecified addresses (nothing answers +// there on Android), for a link-local IPv6 address without a scope, and for anything invalid. +static int dns_server_from_string(const char *text, ddnatv_dns_server_t *server) { + memset(server, 0, sizeof(*server)); + if (!text) return 0; + struct in_addr a4; + if (inet_pton(AF_INET, text, &a4) == 1) { + if ((ntohl(a4.s_addr) >> 24) == 127 || a4.s_addr == 0) return 0; + struct sockaddr_in *sin = (struct sockaddr_in *)&server->addr; + sin->sin_family = AF_INET; + sin->sin_port = htons(53); + sin->sin_addr = a4; + server->len = sizeof(*sin); + return 1; + } + char host[INET6_ADDRSTRLEN + IF_NAMESIZE + 2]; + if (strlen(text) >= sizeof(host)) return 0; + strcpy(host, text); + unsigned int scope = 0; + char *pct = strchr(host, '%'); + if (pct) { + *pct++ = '\0'; + if (*pct >= '0' && *pct <= '9') { + scope = (unsigned int)strtoul(pct, NULL, 10); + } else { + scope = if_nametoindex(pct); + } + } + struct in6_addr a6; + if (inet_pton(AF_INET6, host, &a6) != 1) return 0; + if (IN6_IS_ADDR_LOOPBACK(&a6) || IN6_IS_ADDR_UNSPECIFIED(&a6)) return 0; + if (IN6_IS_ADDR_V4MAPPED(&a6) && (a6.s6_addr[12] == 127 || (a6.s6_addr[12] | a6.s6_addr[13] | a6.s6_addr[14] | a6.s6_addr[15]) == 0)) return 0; + if (IN6_IS_ADDR_LINKLOCAL(&a6) && scope == 0) return 0; + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&server->addr; + sin6->sin6_family = AF_INET6; + sin6->sin6_port = htons(53); + sin6->sin6_addr = a6; + sin6->sin6_scope_id = scope; + server->len = sizeof(*sin6); + return 1; +} + +// The "nameserver" lines of a resolv.conf, in order: up to `max` usable servers, IPv4 and IPv6. +static int parse_nameservers(const char *text, ddnatv_dns_server_t *out, int max) { + int count = 0; + const char *line = text; + while (line && *line && count < max) { + const char *next = strchr(line, '\n'); + size_t len = next ? (size_t)(next - line) : strlen(line); + char buf[256]; + if (len >= sizeof(buf)) len = sizeof(buf) - 1; + memcpy(buf, line, len); + buf[len] = '\0'; + char *p = buf; + while (*p == ' ' || *p == '\t') p++; + if (strncmp(p, "nameserver", 10) == 0 && (p[10] == ' ' || p[10] == '\t')) { + p += 10; + while (*p == ' ' || *p == '\t') p++; + char *end = p; + while (*end && *end != ' ' && *end != '\t' && *end != '\r' && *end != '#' && *end != ';') end++; + *end = '\0'; + if (dns_server_from_string(p, &out[count])) count++; + } + line = next ? next + 1 : NULL; + } + return count; +} + +static int dns_server_equal(const ddnatv_dns_server_t *a, const ddnatv_dns_server_t *b) { + return a->len == b->len && memcmp(&a->addr, &b->addr, a->len) == 0; +} + +// Builds a recursive query for one name and record type; returns its length, or -1 when the name +// is not a valid DNS name (an empty label, a label over 63 bytes, over 253 bytes in all). +static int dns_build_query(const char *name, int qtype, uint16_t txid, unsigned char *packet, size_t size) { + if (!name || !packet || size < 12) return -1; + size_t name_len = strlen(name); + if (name_len > 0 && name[name_len - 1] == '.') name_len--; // "example.com." is absolute + if (name_len == 0 || name_len > 253) return -1; + memset(packet, 0, 12); + packet[0] = (unsigned char)(txid >> 8); + packet[1] = (unsigned char)(txid & 0xff); + packet[2] = 0x01; // recursion desired + packet[5] = 0x01; // one question + size_t pos = 12; + size_t start = 0; + for (;;) { + size_t end = start; + while (end < name_len && name[end] != '.') end++; + size_t len = end - start; + if (len == 0 || len > 63 || pos + 1 + len + 1 + 4 > size) return -1; + packet[pos++] = (unsigned char)len; + memcpy(packet + pos, name + start, len); + pos += len; + if (end == name_len) break; + start = end + 1; + } + packet[pos++] = 0; + packet[pos++] = (unsigned char)(qtype >> 8); + packet[pos++] = (unsigned char)(qtype & 0xff); + packet[pos++] = 0x00; + packet[pos++] = 0x01; // class IN + return (int)pos; +} + +// Offset after the (possibly compressed) name at `off`, or -1 when it is malformed. +static int dns_skip_name(const unsigned char *msg, int n, int off) { + for (int labels = 0; labels < 128; labels++) { + if (off >= n) return -1; + unsigned char len = msg[off]; + if (len == 0) return off + 1; + if ((len & 0xc0) == 0xc0) return off + 2 <= n ? off + 2 : -1; + if (len & 0xc0) return -1; + off += 1 + len; + } + return -1; +} + +// Copies the first address of type `qtype` (4 bytes for A, 16 for AAAA) from the reply `msg` to the +// query `txid` and returns 0. Returns 1 when the reply says there is no such address (the name does +// not exist, or has no record of that type): asking other servers would only send the name to more +// of them. Returns -1 for anything that is worth asking another server about: a failure (SERVFAIL, +// REFUSED), a truncated, malformed or foreign reply. +static int dns_parse_answer(const unsigned char *msg, int n, uint16_t txid, int qtype, unsigned char *out) { + if (!msg || n < 12) return -1; + if (((msg[0] << 8) | msg[1]) != txid) return -1; + if (!(msg[2] & 0x80)) return -1; // not a reply + int rcode = msg[3] & 0x0f; + if (rcode == 3) return 1; // NXDOMAIN + if (rcode != 0) return -1; + int qdcount = (msg[4] << 8) | msg[5]; + int ancount = (msg[6] << 8) | msg[7]; + int off = 12; + for (int q = 0; q < qdcount; q++) { + off = dns_skip_name(msg, n, off); + if (off < 0 || off + 4 > n) return -1; + off += 4; + } + int want = qtype == DDNATV_DNS_TYPE_AAAA ? 16 : 4; + for (int a = 0; a < ancount; a++) { + off = dns_skip_name(msg, n, off); + if (off < 0 || off + 10 > n) return -1; + int rtype = (msg[off] << 8) | msg[off + 1]; + int rclass = (msg[off + 2] << 8) | msg[off + 3]; + int rdlen = (msg[off + 8] << 8) | msg[off + 9]; + off += 10; + if (off + rdlen > n) return -1; + if (rtype == qtype && rclass == 1 && rdlen == want) { + memcpy(out, msg + off, (size_t)want); + return 0; + } + off += rdlen; + } + return (msg[2] & 0x02) ? -1 : 1; // no such record, unless the reply was truncated +} + +// ---- Sockets bound to a port the program chose ---------------------------------------------- +// Which descriptors are TCP/UDP sockets that bind() gave a port the program asked for (not 0, +// where the kernel picks one). listen() reports only those to the app, which connects to a +// reported port to see whether it serves web pages (sockets.c). Descriptors past the bitmap +// are taken as not chosen: they are not reported. +#define DDNATV_CHOSEN_PORT_FDS 65536 +static unsigned char g_chosen_port_fds[DDNATV_CHOSEN_PORT_FDS / 8]; + +static void set_chosen_port_fd(int fd, int chosen) { + if (fd < 0 || fd >= DDNATV_CHOSEN_PORT_FDS) return; + unsigned char bit = (unsigned char)(1u << (fd & 7)); + // Written only when it changes: a page of the bitmap is copied for a forked child only then + if (chosen) { + if (!(__atomic_load_n(&g_chosen_port_fds[fd >> 3], __ATOMIC_RELAXED) & bit)) + __atomic_fetch_or(&g_chosen_port_fds[fd >> 3], bit, __ATOMIC_RELAXED); + } else if (__atomic_load_n(&g_chosen_port_fds[fd >> 3], __ATOMIC_RELAXED) & bit) { + __atomic_fetch_and(&g_chosen_port_fds[fd >> 3], (unsigned char)~bit, __ATOMIC_RELAXED); + } +} + +static int is_chosen_port_fd(int fd) { + if (fd < 0 || fd >= DDNATV_CHOSEN_PORT_FDS) return 0; + return (__atomic_load_n(&g_chosen_port_fds[fd >> 3], __ATOMIC_RELAXED) >> (fd & 7)) & 1; +} + +#ifndef DDNATV_HOST_TEST + +// DNS resolution hooking and localhost DNS redirect +#define DNS_REDIRECT_MAX 64 +static struct { + int fd; + struct sockaddr_storage orig; + socklen_t orig_len; + uint32_t active; +} g_dns_redirects[DNS_REDIRECT_MAX]; +static pthread_mutex_t g_dns_redirect_mutex = PTHREAD_MUTEX_INITIALIZER; + +static void record_dns_redirect(int fd, const struct sockaddr *orig, socklen_t orig_len) { + if (fd < 0 || !orig) return; + if (orig_len > sizeof(struct sockaddr_storage)) orig_len = sizeof(struct sockaddr_storage); + pthread_mutex_lock(&g_dns_redirect_mutex); + int target_idx = (fd >= 0 ? fd : -fd) % DNS_REDIRECT_MAX; + g_dns_redirects[target_idx].fd = fd; + memcpy(&g_dns_redirects[target_idx].orig, orig, orig_len); + g_dns_redirects[target_idx].orig_len = orig_len; + g_dns_redirects[target_idx].active = 1; + pthread_mutex_unlock(&g_dns_redirect_mutex); +} + +static void clear_dns_redirect(int fd) { + if (fd < 0) return; + int target_idx = (fd >= 0 ? fd : -fd) % DNS_REDIRECT_MAX; + if (!g_dns_redirects[target_idx].active || g_dns_redirects[target_idx].fd != fd) { + return; + } + pthread_mutex_lock(&g_dns_redirect_mutex); + if (g_dns_redirects[target_idx].active && g_dns_redirects[target_idx].fd == fd) { + g_dns_redirects[target_idx].active = 0; + g_dns_redirects[target_idx].fd = -1; + g_dns_redirects[target_idx].orig_len = 0; + } + pthread_mutex_unlock(&g_dns_redirect_mutex); +} + +int close(int fd) { + static int (*orig_close)(int) = NULL; + if (!orig_close) orig_close = (int (*)(int))dlsym(RTLD_NEXT, "close"); + // The ARM translator's memory file (privileged.c): left open, as if it were closed + if (g_kept_fd_count > 0 && is_kept_fd(fd)) return 0; + clear_dns_redirect(fd); + set_chosen_port_fd(fd, 0); + return orig_close ? orig_close(fd) : -1; +} + +int dup2(int oldfd, int newfd) { + static int (*orig_dup2)(int, int) = NULL; + if (!orig_dup2) orig_dup2 = (int (*)(int, int))dlsym(RTLD_NEXT, "dup2"); + if (oldfd != newfd) { + clear_dns_redirect(newfd); + set_chosen_port_fd(newfd, is_chosen_port_fd(oldfd)); + } + return orig_dup2 ? orig_dup2(oldfd, newfd) : -1; +} + +int dup3(int oldfd, int newfd, int flags) { + static int (*orig_dup3)(int, int, int) = NULL; + if (!orig_dup3) orig_dup3 = (int (*)(int, int, int))dlsym(RTLD_NEXT, "dup3"); + if (oldfd != newfd) { + clear_dns_redirect(newfd); + set_chosen_port_fd(newfd, is_chosen_port_fd(oldfd)); + } + return orig_dup3 ? orig_dup3(oldfd, newfd, flags) : -1; +} + +static int get_dns_redirect(int fd, struct sockaddr_storage *orig, socklen_t *orig_len) { + if (fd < 0) return 0; + int found = 0; + pthread_mutex_lock(&g_dns_redirect_mutex); + int target_idx = (fd >= 0 ? fd : -fd) % DNS_REDIRECT_MAX; + if (g_dns_redirects[target_idx].active && g_dns_redirects[target_idx].fd == fd) { + if (orig) *orig = g_dns_redirects[target_idx].orig; + if (orig_len) *orig_len = g_dns_redirects[target_idx].orig_len; + found = 1; + } + pthread_mutex_unlock(&g_dns_redirect_mutex); + return found; +} + +// A DNS query to 127.0.0.0/8 or ::1 (or ::ffff:127.x), where nothing listens on Android. +static inline int is_loopback_dns(const struct sockaddr *addr, socklen_t addrlen) { + if (!addr || addrlen < sizeof(sa_family_t)) return 0; + if (addr->sa_family == AF_INET && addrlen >= sizeof(struct sockaddr_in)) { + const struct sockaddr_in *sin = (const struct sockaddr_in *)addr; + return (sin->sin_port == htons(53) && ((ntohl(sin->sin_addr.s_addr) >> 24) == 127)); + } + if (addr->sa_family == AF_INET6 && addrlen >= sizeof(struct sockaddr_in6)) { + const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)addr; + if (sin6->sin6_port != htons(53)) return 0; + return IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) || + (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) && sin6->sin6_addr.s6_addr[12] == 127); + } + return 0; +} + +static in_addr_t get_primary_dns(void) { + static in_addr_t cached_dns = 0; + static time_t last_check = 0; + static pthread_mutex_t dns_mutex = PTHREAD_MUTEX_INITIALIZER; + + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + time_t now = ts.tv_sec; + + // Fast path: cached value valid within 5s TTL + if (cached_dns != 0 && (now - last_check < 5)) { + return cached_dns; + } + + pthread_mutex_lock(&dns_mutex); + if (cached_dns != 0 && (now - last_check < 5)) { + in_addr_t val = cached_dns; + pthread_mutex_unlock(&dns_mutex); + return val; + } + last_check = now; + + in_addr_t new_dns = 0; + init_ddnatv_hook(); + + // 1. Check Android system property net.dns1 via Bionic libc + static int (*orig_sys_prop_get)(const char *, char *) = NULL; + static int prop_lookup_done = 0; + if (!prop_lookup_done) { + orig_sys_prop_get = (int (*)(const char *, char *))dlsym(RTLD_DEFAULT, "__system_property_get"); + prop_lookup_done = 1; + } + if (orig_sys_prop_get) { + char prop_val[256]; + if (orig_sys_prop_get("net.dns1", prop_val) > 0 && prop_val[0] != '\0') { + struct in_addr a; + if (inet_aton(prop_val, &a)) { + if ((ntohl(a.s_addr) >> 24) != 127 && a.s_addr != 0) { + new_dns = a.s_addr; + } + } + } + } + + // 2. Parse /etc/resolv.conf in The native userland rootfs + char resolv_path[PATH_MAX]; + if (g_ddnatv_root[0] != '\0') { + snprintf(resolv_path, sizeof(resolv_path), "%s/etc/resolv.conf", g_ddnatv_root); + } else { + snprintf(resolv_path, sizeof(resolv_path), "/etc/resolv.conf"); + } + + struct stat st; + if (stat(resolv_path, &st) == 0) { + if (new_dns == 0) { + FILE *f = fopen(resolv_path, "r"); + if (f) { + char line[256]; + while (fgets(line, sizeof(line), f)) { + char *p = line; + while (*p == ' ' || *p == '\t') p++; + if (strncmp(p, "nameserver", 10) == 0) { + p += 10; + while (*p == ' ' || *p == '\t') p++; + char *end = p; + while (*end && *end != ' ' && *end != '\t' && *end != '\r' && *end != '\n') end++; + *end = '\0'; + struct in_addr a; + if (inet_aton(p, &a)) { + // Only accept valid, non-loopback DNS servers + if ((ntohl(a.s_addr) >> 24) != 127 && a.s_addr != 0) { + new_dns = a.s_addr; + break; + } + } + } + } + fclose(f); + } + } + } + + if (new_dns == 0) { + new_dns = inet_addr("8.8.8.8"); + } + + cached_dns = new_dns; + pthread_mutex_unlock(&dns_mutex); + return cached_dns; +} + + +// The DNS servers in the rootfs resolv.conf (which the app writes from Android's network settings), +// read again at most every 5 seconds. +static int get_dns_servers(ddnatv_dns_server_t *out, int max) { + static ddnatv_dns_server_t cached[DDNATV_DNS_MAX_SERVERS]; + static int cached_count = 0; + static time_t last_check = 0; + static int checked = 0; + static pthread_mutex_t servers_mutex = PTHREAD_MUTEX_INITIALIZER; + + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + pthread_mutex_lock(&servers_mutex); + if (!checked || ts.tv_sec - last_check >= 5) { + checked = 1; + last_check = ts.tv_sec; + init_ddnatv_hook(); + char path[PATH_MAX]; + snprintf(path, sizeof(path), "%s/etc/resolv.conf", g_ddnatv_root[0] ? g_ddnatv_root : ""); + char text[4096]; + ssize_t n = -1; + int fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd >= 0) { + n = read(fd, text, sizeof(text) - 1); + close(fd); + } + text[n > 0 ? n : 0] = '\0'; + cached_count = parse_nameservers(text, cached, DDNATV_DNS_MAX_SERVERS); + } + int count = cached_count < max ? cached_count : max; + memcpy(out, cached, (size_t)count * sizeof(cached[0])); + pthread_mutex_unlock(&servers_mutex); + return count; +} + +// Where a DNS query to a loopback address goes instead: the network's DNS server, in the family of +// the socket (an IPv6 socket gets the first IPv6 server, else the IPv4 one as ::ffff:a.b.c.d). +// Returns the length of the address written to `out`. +static socklen_t dns_redirect_target(const struct sockaddr *orig, struct sockaddr_storage *out) { + memset(out, 0, sizeof(*out)); + if (orig->sa_family == AF_INET) { + memcpy(out, orig, sizeof(struct sockaddr_in)); + ((struct sockaddr_in *)out)->sin_addr.s_addr = get_primary_dns(); + return sizeof(struct sockaddr_in); + } + struct sockaddr_in6 *dst = (struct sockaddr_in6 *)out; + memcpy(dst, orig, sizeof(*dst)); + ddnatv_dns_server_t servers[DDNATV_DNS_MAX_SERVERS]; + int n = get_dns_servers(servers, DDNATV_DNS_MAX_SERVERS); + for (int i = 0; i < n; i++) { + if (servers[i].addr.ss_family == AF_INET6) { + const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *)&servers[i].addr; + dst->sin6_addr = s6->sin6_addr; + dst->sin6_scope_id = s6->sin6_scope_id; + return sizeof(*dst); + } + } + in_addr_t v4 = get_primary_dns(); + memset(&dst->sin6_addr, 0, sizeof(dst->sin6_addr)); + dst->sin6_addr.s6_addr[10] = 0xff; + dst->sin6_addr.s6_addr[11] = 0xff; + memcpy(&dst->sin6_addr.s6_addr[12], &v4, 4); + dst->sin6_scope_id = 0; + return sizeof(*dst); +} + +// Puts the loopback address that a DNS socket was redirected from back into the source address of a +// reply (`cap` is the size of the caller's buffer), so resolvers that check the sender accept it. +static void restore_dns_source(int fd, struct sockaddr *addr, socklen_t cap, socklen_t *len) { + if (!addr || cap < sizeof(sa_family_t)) return; + in_port_t port; + if (addr->sa_family == AF_INET && cap >= sizeof(struct sockaddr_in)) { + port = ((const struct sockaddr_in *)addr)->sin_port; + } else if (addr->sa_family == AF_INET6 && cap >= sizeof(struct sockaddr_in6)) { + port = ((const struct sockaddr_in6 *)addr)->sin6_port; + } else { + return; + } + if (port != htons(53)) return; + struct sockaddr_storage orig; + socklen_t orig_len = 0; + if (!get_dns_redirect(fd, &orig, &orig_len) || orig.ss_family != addr->sa_family) return; + memcpy(addr, &orig, orig_len < cap ? orig_len : cap); + if (len) *len = orig_len; +} + +#define DDNATV_ADDRINFO_MAGIC 0x434f5254 + +// One addrinfo for an IPv4 (4 bytes) or IPv6 (16 bytes) address; freed by the freeaddrinfo hook. +static struct addrinfo *alloc_addrinfo(const char *node, int family, const void *addr, int port, int socktype, int protocol) { + struct addrinfo *ai = (struct addrinfo *)calloc(1, sizeof(struct addrinfo)); + if (!ai) return NULL; + socklen_t len = family == AF_INET6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); + void *sa = calloc(1, len); + if (!sa) { + free(ai); + return NULL; + } + if (family == AF_INET6) { + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa; + sin6->sin6_family = AF_INET6; + sin6->sin6_port = htons((uint16_t)port); + memcpy(&sin6->sin6_addr, addr, 16); + } else { + struct sockaddr_in *sin = (struct sockaddr_in *)sa; + sin->sin_family = AF_INET; + sin->sin_port = htons((uint16_t)port); + memcpy(&sin->sin_addr, addr, 4); + } + + ai->ai_flags = DDNATV_ADDRINFO_MAGIC; + ai->ai_family = family; + ai->ai_socktype = socktype ? socktype : SOCK_STREAM; + ai->ai_protocol = protocol; + ai->ai_addrlen = len; + ai->ai_addr = (struct sockaddr *)sa; + ai->ai_canonname = node ? strdup(node) : NULL; + ai->ai_next = NULL; + return ai; +} + +static uint16_t generate_dns_txid(void) { + uint16_t txid = 0; + static ssize_t (*libc_getrandom)(void *, size_t, unsigned int) = NULL; + static int getrandom_checked = 0; + if (!getrandom_checked) { + libc_getrandom = (ssize_t (*)(void *, size_t, unsigned int))dlsym(RTLD_DEFAULT, "getrandom"); + getrandom_checked = 1; + } + if (libc_getrandom) { + if (libc_getrandom(&txid, sizeof(txid), 1 /* GRND_NONBLOCK */) == sizeof(txid) && txid != 0) { + return txid; + } + } +#if defined(SYS_getrandom) || defined(__NR_getrandom) +#ifndef __NR_getrandom +#define __NR_getrandom SYS_getrandom +#endif + long r = syscall(__NR_getrandom, &txid, sizeof(txid), 1 /* GRND_NONBLOCK */); + if (r == (long)sizeof(txid) && txid != 0) { + return txid; + } +#endif + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + uint32_t val = (uint32_t)(ts.tv_nsec ^ (ts.tv_sec << 16) ^ (uintptr_t)&txid); + txid = (uint16_t)((val & 0xffff) ^ (val >> 16)); + if (txid == 0) txid = 0x5a6b; + return txid; +} + +static void add_dns_server(ddnatv_dns_server_t *list, int *count, int max, const ddnatv_dns_server_t *server) { + if (*count >= max) return; + for (int i = 0; i < *count; i++) { + if (dns_server_equal(&list[i], server)) return; + } + list[(*count)++] = *server; +} + +// Looks one address of `name` up itself, for when glibc found none: asks the network's DNS servers, +// then public ones (IPv4 first, then IPv6, for IPv6-only networks without IPv4 translation), and +// stops at the first that answers. `qtype` is DDNATV_DNS_TYPE_A (4 bytes into `out`) or +// DDNATV_DNS_TYPE_AAAA (16 bytes). `*server` is -1 to ask them in turn, or the index of the only +// server to ask; on return it holds the server that answered (-1 when none did). +static int dns_lookup(const char *name, int qtype, unsigned char *out, int *server) { + if (!name || !out) return -1; + int family = qtype == DDNATV_DNS_TYPE_AAAA ? AF_INET6 : AF_INET; + if (inet_pton(family, name, out) == 1) return 0; + + static const char *const k_public_dns[] = { + "8.8.8.8", "1.1.1.1", "9.9.9.9", "2001:4860:4860::8888", "2606:4700:4700::1111", + }; + ddnatv_dns_server_t servers[DDNATV_DNS_MAX_SERVERS + 6]; + int max = (int)(sizeof(servers) / sizeof(servers[0])); + int count = 0; + ddnatv_dns_server_t s; + // the network's own servers in resolv.conf's order first, its IPv6 ones too: the first IPv4 one + // (get_primary_dns) can be the app's public fallback, which must not see private names first + ddnatv_dns_server_t network[DDNATV_DNS_MAX_SERVERS]; + int network_count = get_dns_servers(network, DDNATV_DNS_MAX_SERVERS); + for (int i = 0; i < network_count; i++) add_dns_server(servers, &count, max, &network[i]); + char primary[INET_ADDRSTRLEN]; + in_addr_t p_dns = get_primary_dns(); // also covers net.dns1 of older Android versions + if (inet_ntop(AF_INET, &p_dns, primary, sizeof(primary)) && dns_server_from_string(primary, &s)) { + add_dns_server(servers, &count, max, &s); + } + for (size_t i = 0; i < sizeof(k_public_dns) / sizeof(k_public_dns[0]); i++) { + if (dns_server_from_string(k_public_dns[i], &s)) add_dns_server(servers, &count, max, &s); + } + + unsigned char packet[512]; + uint16_t txid = generate_dns_txid(); + int packet_len = dns_build_query(name, qtype, txid, packet, sizeof(packet)); + if (packet_len < 0) return -1; + + int only = *server; + *server = -1; + for (int i = (only >= 0 ? only : 0); i < count && (only < 0 || i == only); i++) { + int sock = socket(servers[i].addr.ss_family, SOCK_DGRAM | SOCK_CLOEXEC, 0); + if (sock < 0) continue; + + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 600000; // 600ms fast failover + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + + // A connected socket only receives datagrams from that server. + if (connect(sock, (const struct sockaddr *)&servers[i].addr, servers[i].len) != 0 || + send(sock, packet, (size_t)packet_len, 0) != packet_len) { + close(sock); + continue; + } + unsigned char resp[1500]; + ssize_t n = recv(sock, resp, sizeof(resp), 0); + close(sock); + int result = n > 0 ? dns_parse_answer(resp, (int)n, txid, qtype, out) : -1; + if (result >= 0) { + *server = i; + return result == 0 ? 0 : -1; + } + } + return -1; +} + +// Whether the network's DNS servers are all IPv6 ones, as on an IPv6-only mobile network. +static int network_dns_is_ipv6_only(void) { + ddnatv_dns_server_t servers[DDNATV_DNS_MAX_SERVERS]; + int n = get_dns_servers(servers, DDNATV_DNS_MAX_SERVERS); + for (int i = 0; i < n; i++) { + if (servers[i].addr.ss_family != AF_INET6) return 0; + } + return n > 0; +} + +// glibc reads /etc/services with an open() of its own, which the path hooks never see, so a service +// name was looked up where Android has no such file: getaddrinfo(host, "https") failed with +// EAI_SERVICE. The rootfs's file is read here when glibc finds nothing. 0, ENOENT or ERANGE. +static int ddnatv_lookup_service(const char *name, int port, const char *proto, + struct servent *out, char *buf, size_t buflen) { + char path_buf[PATH_MAX]; + const char *path = rewrite_path("/etc/services", path_buf, sizeof(path_buf)); + FILE *f = path ? fopen(path, "re") : NULL; + if (!f) return ENOENT; + char line[512]; + int result = ENOENT; + while (result == ENOENT && fgets(line, sizeof(line), f)) { + char *hash = strchr(line, '#'); + if (hash) *hash = '\0'; + char *save = NULL; + char *svc = strtok_r(line, " \t\r\n", &save); + char *pp = svc ? strtok_r(NULL, " \t\r\n", &save) : NULL; + char *slash = pp ? strchr(pp, '/') : NULL; + if (!slash) continue; + *slash = '\0'; + const char *svc_proto = slash + 1; + int svc_port = atoi(pp); + if (proto && strcmp(proto, svc_proto) != 0) continue; + char *aliases[16]; + int naliases = 0; + int matched = name ? strcmp(svc, name) == 0 : svc_port == port; + for (char *a = strtok_r(NULL, " \t\r\n", &save); a && naliases < 15; a = strtok_r(NULL, " \t\r\n", &save)) { + aliases[naliases++] = a; + if (name && strcmp(a, name) == 0) matched = 1; + } + if (!matched) continue; + // buf holds the alias pointers, then the strings + size_t need = sizeof(char *) * (size_t)(naliases + 1) + strlen(svc) + 1 + strlen(svc_proto) + 1; + for (int i = 0; i < naliases; i++) need += strlen(aliases[i]) + 1; + if (need > buflen) { result = ERANGE; break; } + char **list = (char **)(void *)buf; + char *p = buf + sizeof(char *) * (size_t)(naliases + 1); + out->s_name = strcpy(p, svc); p += strlen(svc) + 1; + out->s_proto = strcpy(p, svc_proto); p += strlen(svc_proto) + 1; + for (int i = 0; i < naliases; i++) { list[i] = strcpy(p, aliases[i]); p += strlen(aliases[i]) + 1; } + list[naliases] = NULL; + out->s_aliases = list; + out->s_port = (int)htons((uint16_t)svc_port); + result = 0; + } + fclose(f); + return result; +} + +int getservbyname_r(const char *name, const char *proto, struct servent *result_buf, char *buf, size_t buflen, + struct servent **result) { + static int (*orig)(const char *, const char *, struct servent *, char *, size_t, struct servent **) = NULL; + if (!orig) orig = (int (*)(const char *, const char *, struct servent *, char *, size_t, struct servent **))dlsym(RTLD_NEXT, "getservbyname_r"); + int ret = orig ? orig(name, proto, result_buf, buf, buflen, result) : ENOENT; + if (ret == 0 && result && *result) return 0; + if (!name || !result_buf || !buf) return ret; + int err = ddnatv_lookup_service(name, 0, proto, result_buf, buf, buflen); + if (result) *result = err == 0 ? result_buf : NULL; + return err == ERANGE ? ERANGE : 0; +} + +int getservbyport_r(int port, const char *proto, struct servent *result_buf, char *buf, size_t buflen, + struct servent **result) { + static int (*orig)(int, const char *, struct servent *, char *, size_t, struct servent **) = NULL; + if (!orig) orig = (int (*)(int, const char *, struct servent *, char *, size_t, struct servent **))dlsym(RTLD_NEXT, "getservbyport_r"); + int ret = orig ? orig(port, proto, result_buf, buf, buflen, result) : ENOENT; + if (ret == 0 && result && *result) return 0; + if (!result_buf || !buf) return ret; + int err = ddnatv_lookup_service(NULL, ntohs((uint16_t)port), proto, result_buf, buf, buflen); + if (result) *result = err == 0 ? result_buf : NULL; + return err == ERANGE ? ERANGE : 0; +} + +struct servent *getservbyname(const char *name, const char *proto) { + static __thread struct servent entry; + static __thread char buf[1024]; + struct servent *found = NULL; + return getservbyname_r(name, proto, &entry, buf, sizeof(buf), &found) == 0 ? found : NULL; +} + +struct servent *getservbyport(int port, const char *proto) { + static __thread struct servent entry; + static __thread char buf[1024]; + struct servent *found = NULL; + return getservbyport_r(port, proto, &entry, buf, sizeof(buf), &found) == 0 ? found : NULL; +} + +static int synthesize_fallback_addrinfo(const char *node, const char *service, + const struct addrinfo *hints, + struct addrinfo **res) { + if (!node || !res) return EAI_NONAME; + + int socktype = (hints && hints->ai_socktype) ? hints->ai_socktype : SOCK_STREAM; + int protocol = (hints && hints->ai_protocol) ? hints->ai_protocol + : socktype == SOCK_DGRAM ? IPPROTO_UDP : socktype == SOCK_STREAM ? IPPROTO_TCP : 0; + int family = hints ? hints->ai_family : AF_UNSPEC; + if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6) return EAI_FAMILY; + + // As glibc: no service is port 0, a number is that port, a name comes from /etc/services + int port = 0; + if (service && *service) { + char *end = NULL; + errno = 0; + long p = strtol(service, &end, 10); + if (end && *end == '\0' && errno == 0) { + if (p < 0 || p > 65535) return EAI_SERVICE; + port = (int)p; + } else { + struct servent se, *found = NULL; + char se_buf[1024]; + if (getservbyname_r(service, socktype == SOCK_DGRAM ? "udp" : "tcp", &se, se_buf, sizeof(se_buf), &found) != 0 || !found) + return EAI_SERVICE; + port = ntohs((uint16_t)found->s_port); + } + } + + // The address families the caller asked for: an IPv6-only caller must not get an IPv4 address, + // and AF_UNSPEC gets both, IPv6 first on a network whose DNS servers are all IPv6 ones. The second + // lookup only asks the server that answered the first, and is skipped when none answered. + int want4 = family == AF_UNSPEC || family == AF_INET; + int want6 = family == AF_UNSPEC || family == AF_INET6; + int v6_first = want6 && (!want4 || network_dns_is_ipv6_only()); + unsigned char resolved[16]; + int server = -1; + struct addrinfo *first = NULL; + struct addrinfo *second = NULL; + if (dns_lookup(node, v6_first ? DDNATV_DNS_TYPE_AAAA : DDNATV_DNS_TYPE_A, resolved, &server) == 0) { + first = alloc_addrinfo(node, v6_first ? AF_INET6 : AF_INET, resolved, port, socktype, protocol); + } + if (want4 && want6 && server >= 0 && + dns_lookup(node, v6_first ? DDNATV_DNS_TYPE_A : DDNATV_DNS_TYPE_AAAA, resolved, &server) == 0) { + second = alloc_addrinfo(node, v6_first ? AF_INET : AF_INET6, resolved, port, socktype, protocol); + } + if (first && second) { + first->ai_next = second; + } + struct addrinfo *ai = first ? first : second; + if (ai) { + *res = ai; + return 0; + } + + return EAI_NONAME; +} + +int getaddrinfo(const char *node, const char *service, + const struct addrinfo *hints, + struct addrinfo **res) { + static int (*orig_getaddrinfo)(const char *, const char *, const struct addrinfo *, struct addrinfo **) = NULL; + if (!orig_getaddrinfo) orig_getaddrinfo = (int (*)(const char *, const char *, const struct addrinfo *, struct addrinfo **))dlsym(RTLD_NEXT, "getaddrinfo"); + + struct addrinfo mod_hints; + if (hints) { + mod_hints = *hints; + // On Android, unprivileged apps are blocked by SELinux from querying netlink routing interfaces. + // Glibc's AI_ADDRCONFIG flag causes getaddrinfo to fail or return no addresses when netlink fails. + // Clearing AI_ADDRCONFIG ensures normal, robust DNS resolution. + mod_hints.ai_flags &= ~AI_ADDRCONFIG; + } else { + memset(&mod_hints, 0, sizeof(mod_hints)); + mod_hints.ai_family = AF_UNSPEC; + mod_hints.ai_flags = 0; + } + + int ret = orig_getaddrinfo ? orig_getaddrinfo(node, service, &mod_hints, res) : EAI_FAIL; + // A numeric-only lookup must never turn into a DNS query. + if ((ret != 0 || !res || !*res) && node && !(hints && (hints->ai_flags & AI_NUMERICHOST))) { + int fallback = synthesize_fallback_addrinfo(node, service, hints, res); + // When the fallback finds nothing either, glibc's own error (EAI_AGAIN: try again later, + // EAI_NONAME: no such name) is the more precise one. + // A request the fallback rejects outright (no such service, an unsupported family) is that + // error whatever glibc's was + if (fallback == 0 || ret == 0 || fallback == EAI_SERVICE || fallback == EAI_FAMILY) ret = fallback; + } + return ret; +} + +void freeaddrinfo(struct addrinfo *res) { + static void (*orig_freeaddrinfo)(struct addrinfo *) = NULL; + if (!orig_freeaddrinfo) orig_freeaddrinfo = (void (*)(struct addrinfo *))dlsym(RTLD_NEXT, "freeaddrinfo"); + + if (!res) return; + if (res->ai_flags == DDNATV_ADDRINFO_MAGIC) { + struct addrinfo *curr = res; + while (curr) { + struct addrinfo *next = curr->ai_next; + if (curr->ai_canonname) free(curr->ai_canonname); + if (curr->ai_addr) free(curr->ai_addr); + free(curr); + curr = next; + } + return; + } + if (orig_freeaddrinfo) orig_freeaddrinfo(res); +} + +// gethostbyname() and gethostbyname2() read /etc/resolv.conf with glibc's own open(), on Android's +// /etc, so they asked no DNS server and only /etc/hosts answered. When glibc finds nothing, the +// name is looked up with getaddrinfo() above, which knows the network's servers; a name DNS does +// not have still gets no address (HOST_NOT_FOUND). +static int hostent_from_getaddrinfo(const char *name, int af, struct hostent *ret, char *buf, size_t buflen, + struct hostent **result, int *herr) { + *result = NULL; + if (!name || (af != AF_INET && af != AF_INET6)) { *herr = NO_RECOVERY; return EINVAL; } + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = af; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_CANONNAME; + struct addrinfo *res = NULL; + int rc = getaddrinfo(name, NULL, &hints, &res); + if (rc != 0 || !res) { + *herr = (rc == EAI_NONAME || rc == EAI_NODATA || rc == EAI_ADDRFAMILY) ? HOST_NOT_FOUND + : rc == EAI_AGAIN ? TRY_AGAIN : NO_RECOVERY; + if (res) freeaddrinfo(res); + return rc == EAI_AGAIN ? EAGAIN : ENOENT; + } + size_t addr_len = af == AF_INET ? sizeof(struct in_addr) : sizeof(struct in6_addr); + int count = 0; + for (struct addrinfo *a = res; a && count < 16; a = a->ai_next) if (a->ai_family == af) count++; + const char *canon = res->ai_canonname ? res->ai_canonname : name; + size_t canon_len = strlen(canon) + 1; + size_t need = sizeof(char *) * (size_t)(count + 1) + sizeof(char *) + addr_len * (size_t)count + canon_len + sizeof(void *); + if (count == 0 || need > buflen) { + freeaddrinfo(res); + if (count == 0) { *herr = NO_DATA; return ENOENT; } + *herr = NETDB_INTERNAL; + return ERANGE; + } + // pointers first, aligned, then the addresses and the name + char *p = buf + ((sizeof(void *) - ((uintptr_t)buf % sizeof(void *))) % sizeof(void *)); + char **addr_list = (char **)p; + p += sizeof(char *) * (size_t)(count + 1); + char **aliases = (char **)p; + p += sizeof(char *); + aliases[0] = NULL; + int i = 0; + for (struct addrinfo *a = res; a && i < count; a = a->ai_next) { + if (a->ai_family != af) continue; + const void *src = af == AF_INET ? (const void *)&((struct sockaddr_in *)a->ai_addr)->sin_addr + : (const void *)&((struct sockaddr_in6 *)a->ai_addr)->sin6_addr; + memcpy(p, src, addr_len); + addr_list[i++] = p; + p += addr_len; + } + addr_list[i] = NULL; + memcpy(p, canon, canon_len); + ret->h_name = p; + ret->h_aliases = aliases; + ret->h_addrtype = af; + ret->h_length = (int)addr_len; + ret->h_addr_list = addr_list; + freeaddrinfo(res); + *result = ret; + *herr = 0; + return 0; +} + +int gethostbyname2_r(const char *name, int af, struct hostent *ret, char *buf, size_t buflen, + struct hostent **result, int *h_errnop) { + static int (*orig)(const char *, int, struct hostent *, char *, size_t, struct hostent **, int *) = NULL; + if (!orig) orig = (int (*)(const char *, int, struct hostent *, char *, size_t, struct hostent **, int *))dlsym(RTLD_NEXT, "gethostbyname2_r"); + int herr = 0; + int rc = orig ? orig(name, af, ret, buf, buflen, result, &herr) : ENOSYS; + if (rc == 0 && result && *result) { if (h_errnop) *h_errnop = 0; return 0; } + if (rc == ERANGE) { if (h_errnop) *h_errnop = herr; return rc; } + int fallback_herr = 0; + struct hostent *found = NULL; + rc = hostent_from_getaddrinfo(name, af, ret, buf, buflen, &found, &fallback_herr); + if (result) *result = found; + if (h_errnop) *h_errnop = fallback_herr; + return rc; +} + +int gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, + struct hostent **result, int *h_errnop) { + return gethostbyname2_r(name, AF_INET, ret, buf, buflen, result, h_errnop); +} + +static __thread struct hostent t_hostent; +static __thread char t_hostent_buf[4096]; + +struct hostent *gethostbyname2(const char *name, int af) { + struct hostent *result = NULL; + int herr = 0; + gethostbyname2_r(name, af, &t_hostent, t_hostent_buf, sizeof(t_hostent_buf), &result, &herr); + h_errno = result ? 0 : herr; + return result; +} + +struct hostent *gethostbyname(const char *name) { + return gethostbyname2(name, AF_INET); +} + +// Whether a resolver state names a server other than loopback. glibc keeps IPv6 servers in the +// extension (their nsaddr_list entry has family 0), so an IPv6-only resolv.conf counts too. +// The network's own servers, IPv6 ones too (an IPv6-only network has no IPv4 server to ask), for a +// state glibc left with none it can use: it read no /etc/resolv.conf. glibc frees the IPv6 entries. +static void fill_fallback_servers(res_state statp) { + ddnatv_dns_server_t servers[DDNATV_DNS_MAX_SERVERS]; + int n = get_dns_servers(servers, DDNATV_DNS_MAX_SERVERS); + for (int i = 0; i < MAXNS; i++) { + free(statp->_u._ext.nsaddrs[i]); + statp->_u._ext.nsaddrs[i] = NULL; + } + int count = 0; + for (int i = 0; i < n && count < MAXNS; i++) { + if (servers[i].addr.ss_family == AF_INET) { + memcpy(&statp->nsaddr_list[count], &servers[i].addr, sizeof(struct sockaddr_in)); + statp->nsaddr_list[count].sin_port = htons(53); + statp->_u._ext.nsaddrs[count] = NULL; + count++; + } else if (servers[i].addr.ss_family == AF_INET6) { + struct sockaddr_in6 *six = malloc(sizeof(*six)); + if (!six) continue; + memcpy(six, &servers[i].addr, sizeof(*six)); + six->sin6_port = htons(53); + memset(&statp->nsaddr_list[count], 0, sizeof(statp->nsaddr_list[count])); + statp->_u._ext.nsaddrs[count] = six; + count++; + } + } + if (count == 0) { + statp->nsaddr_list[0].sin_family = AF_INET; + statp->nsaddr_list[0].sin_port = htons(53); + statp->nsaddr_list[0].sin_addr.s_addr = get_primary_dns(); + statp->nsaddr_list[1].sin_family = AF_INET; + statp->nsaddr_list[1].sin_port = htons(53); + inet_pton(AF_INET, "8.8.8.8", &statp->nsaddr_list[1].sin_addr); + count = 2; + } + statp->nscount = count; +} + +static int res_state_has_usable_server(res_state statp) { + for (int i = 0; i < statp->nscount && i < MAXNS; i++) { + if (statp->nsaddr_list[i].sin_family == AF_INET) { + in_addr_t a = statp->nsaddr_list[i].sin_addr.s_addr; + if ((ntohl(a) >> 24) != 127 && a != 0) return 1; + } else if (statp->_u._ext.nsaddrs[i] != NULL) { + const struct in6_addr *a6 = &statp->_u._ext.nsaddrs[i]->sin6_addr; + if (!IN6_IS_ADDR_LOOPBACK(a6) && !IN6_IS_ADDR_UNSPECIFIED(a6)) return 1; + } + } + return 0; +} + +int res_init(void) { + static int (*orig_res_init)(void) = NULL; + // glibc's symbols are __res_init and __res_ninit (resolv.h makes the names macros): looked up as + // res_init and res_ninit they were never found, the state stayed without RES_INIT, and Chromium + // (Steam's store) failed to read a DNS configuration + if (!orig_res_init) orig_res_init = (int (*)(void))dlsym(RTLD_NEXT, "__res_init"); + if (!orig_res_init) orig_res_init = (int (*)(void))dlsym(RTLD_NEXT, "res_init"); + int ret = orig_res_init ? orig_res_init() : 0; + res_state statp = __res_state(); + if (statp) { + if (!res_state_has_usable_server(statp)) fill_fallback_servers(statp); + statp->retrans = 1; + statp->retry = 2; + } + return ret; +} + +int res_ninit(res_state statp) { + static int (*orig_res_ninit)(res_state) = NULL; + if (!orig_res_ninit) orig_res_ninit = (int (*)(res_state))dlsym(RTLD_NEXT, "__res_ninit"); + if (!orig_res_ninit) orig_res_ninit = (int (*)(res_state))dlsym(RTLD_NEXT, "res_ninit"); + if (statp) { + memset(statp, 0, sizeof(*statp)); + statp->_vcsock = -1; + for (int i = 0; i < MAXNS; i++) { + statp->_u._ext.nssocks[i] = -1; + } + } + int ret = orig_res_ninit ? orig_res_ninit(statp) : 0; + if (statp) { + if (!res_state_has_usable_server(statp)) fill_fallback_servers(statp); + statp->retrans = 1; + statp->retry = 2; + } + return ret; +} + +int res_nquery(res_state statp, const char *dname, int class, int type, + unsigned char *answer, int anslen) { + static int (*orig_res_nquery)(res_state, const char *, int, int, unsigned char *, int) = NULL; + if (!orig_res_nquery) orig_res_nquery = (int (*)(res_state, const char *, int, int, unsigned char *, int))dlsym(RTLD_NEXT, "res_nquery"); + // DNS SRV queries are optional for APT mirrors and frequently cause delays or issues + if (type == 33 /* T_SRV */) { + h_errno = NO_DATA; + return -1; + } + return orig_res_nquery ? orig_res_nquery(statp, dname, class, type, answer, anslen) : -1; +} + +int res_query(const char *dname, int class, int type, + unsigned char *answer, int anslen) { + static int (*orig_res_query)(const char *, int, int, unsigned char *, int) = NULL; + if (!orig_res_query) orig_res_query = (int (*)(const char *, int, int, unsigned char *, int))dlsym(RTLD_NEXT, "res_query"); + if (type == 33 /* T_SRV */) { + h_errno = NO_DATA; + return -1; + } + return orig_res_query ? orig_res_query(dname, class, type, answer, anslen) : -1; +} + +int res_nsearch(res_state statp, const char *dname, int class, int type, + unsigned char *answer, int anslen) { + static int (*orig_res_nsearch)(res_state, const char *, int, int, unsigned char *, int) = NULL; + if (!orig_res_nsearch) orig_res_nsearch = (int (*)(res_state, const char *, int, int, unsigned char *, int))dlsym(RTLD_NEXT, "res_nsearch"); + if (type == 33 /* T_SRV */) { + h_errno = NO_DATA; + return -1; + } + return orig_res_nsearch ? orig_res_nsearch(statp, dname, class, type, answer, anslen) : -1; +} + +int res_search(const char *dname, int class, int type, + unsigned char *answer, int anslen) { + static int (*orig_res_search)(const char *, int, int, unsigned char *, int) = NULL; + if (!orig_res_search) orig_res_search = (int (*)(const char *, int, int, unsigned char *, int))dlsym(RTLD_NEXT, "res_search"); + if (type == 33 /* T_SRV */) { + h_errno = NO_DATA; + return -1; + } + return orig_res_search ? orig_res_search(dname, class, type, answer, anslen) : -1; +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/paths.c b/tools/nativeguest/hook/paths.c new file mode 100644 index 0000000000..4c1340b339 --- /dev/null +++ b/tools/nativeguest/hook/paths.c @@ -0,0 +1,697 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Path translation: Ubuntu paths (/usr, /etc, ...) mapped into the rootfs, Android's own mounts +// left alone, and the library directories of the rootfs. + +// ---- Path translation ------------------------------------------------------------------- +// Pure string functions (no syscalls), so tests/native can check them directly. They run on +// every file-system call, so they avoid snprintf and compute the rootfs prefix length once. + +static char g_ddnatv_root[PATH_MAX] = {0}; +static size_t g_ddnatv_root_len = 0; + +static void ddnatv_set_root(const char *root, size_t len) { + if (!root) len = 0; + if (len > sizeof(g_ddnatv_root) - 1) len = sizeof(g_ddnatv_root) - 1; + if (len > 0) memcpy(g_ddnatv_root, root, len); + g_ddnatv_root[len] = '\0'; + g_ddnatv_root_len = len; +} + +// Mounts that proot used to provide: "src:dst" pairs from DDNATV_BINDS ('|' separated), longest dst first. +// A path under dst is read from src; src equal to dst keeps a host path that would otherwise be +// looked up in the rootfs. +struct ddnatv_bind { + char *src, *dst; + size_t src_len, dst_len; + int identity; + int order; // position in DDNATV_BINDS: the earlier of two mounts of one folder is its guest path +}; +// as many as the session has: a fixed table dropped what came after its 128th entry +static struct ddnatv_bind *g_binds = NULL; +static int g_nbinds = 0; +static char *g_binds_raw = NULL; +// The session's host name (DDNATV_HOSTNAME), what proot put in uname(): Android's kernel says "localhost" +static char g_hostname[65]; +// A Flatpak sandbox's: the session's rootfs (DDNATV_LINK_ROOT), whose hook stored absolute guest +// link targets as paths in it, as the kernel follows them there; in the sandbox they are guest paths +static char g_link_root[PATH_MAX]; +static size_t g_link_root_len = 0; + +static int ddnatv_bind_longer_first(const void *a, const void *b) { + const struct ddnatv_bind *x = a, *y = b; + return (y->dst_len > x->dst_len) - (y->dst_len < x->dst_len); +} + +static void ddnatv_load_binds(const char *spec) { + if (!spec || !spec[0]) return; + char *raw = strdup(spec); + char *copy = strdup(spec); + if (!raw || !copy) { free(raw); free(copy); return; } + char *save = NULL; + size_t count = 1; + for (const char *c = spec; *c; c++) count += *c == '|'; + g_binds = calloc(count, sizeof(*g_binds)); + if (!g_binds) { free(raw); free(copy); return; } + for (char *entry = strtok_r(copy, "|", &save); entry && (size_t)g_nbinds < count; entry = strtok_r(NULL, "|", &save)) { + char *colon = strchr(entry, ':'); + const char *src = entry; + const char *dst = colon ? colon + 1 : entry; + if (colon) *colon = '\0'; + if (src[0] != '/' || dst[0] != '/' || strcmp(dst, "/") == 0) continue; + struct ddnatv_bind *b = &g_binds[g_nbinds++]; + b->src = strdup(src); + b->dst = strdup(dst); + b->src_len = strlen(src); + b->dst_len = strlen(dst); + while (b->dst_len > 1 && b->dst[b->dst_len - 1] == '/') b->dst[--b->dst_len] = '\0'; + while (b->src_len > 1 && b->src[b->src_len - 1] == '/') b->src[--b->src_len] = '\0'; + b->identity = strcmp(b->src, b->dst) == 0; + b->order = g_nbinds - 1; + } + free(copy); + qsort(g_binds, (size_t)g_nbinds, sizeof(g_binds[0]), ddnatv_bind_longer_first); + g_binds_raw = raw; +} + +// Appends s[0..len) to out[0..used) and NUL-terminates, truncating like snprintf. +// Returns the new length. +static size_t ddnatv_append(char *out, size_t outsz, size_t used, const char *s, size_t len) { + if (outsz == 0) return 0; + size_t room = used < outsz - 1 ? outsz - 1 - used : 0; + if (len > room) len = room; + if (len > 0) memcpy(out + used, s, len); + out[used + len] = '\0'; + return used + len; +} + +// Copies the string src into out[0..outsz), truncated and NUL-terminated as strncpy() followed by +// out[outsz - 1] = '\0' leaves it, but writes only the bytes of the string: strncpy() fills the rest +// of a PATH_MAX buffer with zeros, 4 KB of writes for a path of 30 characters, and in a child that a +// shell has just forked every page written is a copy-on-write fault. +static size_t ddnatv_copy_cstr(char *out, size_t outsz, const char *src) { + if (outsz == 0) return 0; + size_t len = strnlen(src, outsz - 1); + memcpy(out, src, len); + out[len] = '\0'; + return len; +} + +static inline int is_path_prefix(const char *path, const char *prefix, size_t prefix_len) { + if (strncmp(path, prefix, prefix_len) != 0) return 0; + return path[prefix_len] == '/' || path[prefix_len] == '\0'; +} + +static inline const char *get_rootfs_subpath(const char *target) { + if (!target || g_ddnatv_root_len == 0) return NULL; + if (strncmp(target, g_ddnatv_root, g_ddnatv_root_len) == 0 && + (target[g_ddnatv_root_len] == '/' || target[g_ddnatv_root_len] == '\0')) { + return target + g_ddnatv_root_len; + } + return NULL; +} + +// A guest path that a session mount puts outside the rootfs (/run/droiddeck, a game library): a +// program or script there is the guest's, and a script's "#!/bin/sh" is the rootfs shell. The +// kernel ran them with Android's /system/bin/sh, which found no /usr/bin/kwin_wayland. +static int is_mapped_guest_path(const char *path) { + if (!path || path[0] != '/') return 0; + for (int i = 0; i < g_nbinds; i++) { + const struct ddnatv_bind *b = &g_binds[i]; + if (b->identity) continue; + if (is_path_prefix(path, b->dst, b->dst_len)) return 1; + // its real place too: a relative program is made absolute from the real working directory + if (is_path_prefix(path, b->src, b->src_len) && get_rootfs_subpath(b->src) == NULL) return 1; + } + return 0; +} + +// Android's own mounts and files, which must never be redirected into the rootfs. +// `path` starts with '/'; a prefix can only match when its second character does. +static int is_android_host_path(const char *path) { + switch (path[1]) { + case 'a': return is_path_prefix(path, "/apex", 5); + case 'c': return is_path_prefix(path, "/config", 7); + case 'd': return is_path_prefix(path, "/dev", 4) || is_path_prefix(path, "/d", 2) || + is_path_prefix(path, "/data", 5); + case 'l': return is_path_prefix(path, "/linkerconfig", 13); + case 'o': return is_path_prefix(path, "/odm", 4) || is_path_prefix(path, "/oem", 4); + case 'p': return is_path_prefix(path, "/proc", 5) || is_path_prefix(path, "/product", 8); + case 's': return strcmp(path, "/sbin/su") == 0 || strcmp(path, "/su") == 0 || + is_path_prefix(path, "/sys", 4) || is_path_prefix(path, "/system", 7) || + is_path_prefix(path, "/system_ext", 11) || is_path_prefix(path, "/sdcard", 7) || + is_path_prefix(path, "/storage", 8); + case 'v': return is_path_prefix(path, "/vendor", 7); + default: return 0; + } +} + +static const char *rewrite_path_in_root(const char *path, char *buffer, size_t bufsize); + +#if defined(__aarch64__) +static long raw_readlinkat(int dirfd, const char *path, char *buf, size_t size) { + register long x8 __asm__("x8") = 78; // readlinkat, made directly: the hook's own readlink would map the path again + register long x0 __asm__("x0") = dirfd; + register long x1 __asm__("x1") = (long)path; + register long x2 __asm__("x2") = (long)buf; + register long x3 __asm__("x3") = (long)size; + __asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3) : "memory"); + return x0; +} +#endif + +static __thread int t_bind_link_depth = 0; + +// A mount stand-in can hold symbolic links with absolute guest targets (/sys/class/hidraw/hidraw16 -> +// /sys/devices/droiddeck/...): proot follows them inside the guest, the kernel on Android's own /sys. +// A link met on the way to a deeper path is replaced by its target, mapped again; the last component is +// left as it is (lstat() and readlink() must see the link). +static const char *resolve_bind_links(const char *host, size_t src_len, char *buffer, size_t bufsize) { +#if defined(__aarch64__) + size_t len = strlen(host); + if (len >= PATH_MAX || t_bind_link_depth >= 8) return host; + for (size_t i = src_len + 1; i < len; i++) { + if (host[i] != '/') continue; + char prefix[PATH_MAX]; + memcpy(prefix, host, i); + prefix[i] = '\0'; + char target[PATH_MAX]; + long n = raw_readlinkat(AT_FDCWD, prefix, target, sizeof(target) - 1); + if (n <= 0) continue; + target[n] = '\0'; + if (target[0] != '/') continue; + char guest[PATH_MAX]; + size_t used = ddnatv_append(guest, sizeof(guest), 0, target, (size_t)n); + ddnatv_append(guest, sizeof(guest), used, host + i, len - i); + t_bind_link_depth++; + const char *mapped = rewrite_path_in_root(guest, buffer, bufsize); + if (mapped != buffer) ddnatv_copy_cstr(buffer, bufsize, mapped); + t_bind_link_depth--; + return buffer; + } +#else + (void)src_len; (void)bufsize; +#endif + return host == buffer ? buffer : host; +} + +// Wine opens every file through its prefix's drive link (dosdevices/z: -> /), so the link must be +// followed in the guest's view even where the prefix is plain rootfs: the kernel would look for the +// rest of the path on Android's own tree. Only that component is checked, not each one of every path. +static const char *resolve_drive_links(const char *host, char *buffer, size_t bufsize) { + const char *devices = strstr(host, "/dosdevices/"); + if (!devices) return host; + return resolve_bind_links(host, (size_t)(devices - host) + 11, buffer, bufsize); +} + +// Maps an absolute Ubuntu path into the rootfs. Returns `path` (possibly advanced past +// redundant leading "/" and "/." components) when it needs no change, else `buffer`. +static const char *rewrite_path_in_root(const char *path, char *buffer, size_t bufsize) { + if (!path) return NULL; + if (path[0] != '/' || g_ddnatv_root_len == 0) return path; + + // Strip multiple redundant leading slashes: //foo -> /foo + while (path[0] == '/' && path[1] == '/') { + path++; + } + + // Strip leading /./: /./boot -> /boot + while (path[0] == '/' && path[1] == '.' && (path[2] == '/' || path[2] == '\0')) { + if (path[2] == '/') { + path += 2; + } else { + path = "/"; + break; + } + } + + // Root directory + if (path[1] == '\0') { + ddnatv_append(buffer, bufsize, 0, g_ddnatv_root, g_ddnatv_root_len); + return buffer; + } + + for (int i = 0; i < g_nbinds; i++) { + const struct ddnatv_bind *b = &g_binds[i]; + if (path[1] == b->dst[1] && is_path_prefix(path, b->dst, b->dst_len)) { + if (b->identity) return path; + size_t n = ddnatv_append(buffer, bufsize, 0, b->src, b->src_len); + ddnatv_append(buffer, bufsize, n, path + b->dst_len, strlen(path + b->dst_len)); + return resolve_bind_links(buffer, b->src_len, buffer, bufsize); + } + } + + // Already inside The native userland rootfs + if (get_rootfs_subpath(path) != NULL) { + return resolve_drive_links(path, buffer, bufsize); + } + + // The real place of a mounted folder, which getcwd() and /proc/self/fd links answer for it: Steam + // made a game's path from one on the SD card library and found no sekiro.exe in the rootfs + for (int i = 0; i < g_nbinds; i++) { + const struct ddnatv_bind *b = &g_binds[i]; + if (!b->identity && path[1] == b->src[1] && is_path_prefix(path, b->src, b->src_len)) + return resolve_drive_links(path, buffer, bufsize); + } + + // POSIX shared memory (/dev/shm) redirection to The native userland rootfs tmp/shm + if (strncmp(path, "/dev/shm", 8) == 0 && (path[8] == '/' || path[8] == '\0')) { + size_t n = ddnatv_append(buffer, bufsize, 0, g_ddnatv_root, g_ddnatv_root_len); + n = ddnatv_append(buffer, bufsize, n, "/tmp/shm", 8); + ddnatv_append(buffer, bufsize, n, path + 8, strlen(path + 8)); + return buffer; + } + + // Real host Android kernel & system mounts + if (is_android_host_path(path)) { + return path; + } + + // Any other absolute path belongs to The native userland rootfs (/boot, /media, /snap, /etc, /usr, ...) + size_t n = ddnatv_append(buffer, bufsize, 0, g_ddnatv_root, g_ddnatv_root_len); + ddnatv_append(buffer, bufsize, n, path, strlen(path)); + return resolve_drive_links(buffer, buffer, bufsize); +} + +// A link target the session's hook stored as a path in its rootfs, given back as the guest path it +// stands for (see g_link_root). Rewrites target[0..len) in place; returns the new length. +static size_t link_root_guest_target(char *target, size_t len) { + if (!g_link_root_len || len < g_link_root_len || memcmp(target, g_link_root, g_link_root_len) != 0) return len; + if (len == g_link_root_len) { target[0] = '/'; target[1] = '\0'; return 1; } + if (target[g_link_root_len] != '/') return len; + memmove(target, target + g_link_root_len, len - g_link_root_len + 1); + return len - g_link_root_len; +} + +// The guest path a symbolic link at absolute guest path `path` leads to, followed in the guest's +// view: the kernel resolves a relative target next to the link's real place, and in a mount that is +// elsewhere (Flatpak's /run/user/0/wayland-0 -> ../../flatpak/wayland-0, where /run/flatpak/wayland-0 +// is another mount). Returns `path` when it is no link. +static const char *follow_guest_link(const char *path, char *out, size_t outsz) { +#if defined(__aarch64__) + const char *cur = path; + for (int depth = 0; depth < 8; depth++) { + char host_buf[PATH_MAX]; + const char *host = rewrite_path_in_root(cur, host_buf, sizeof(host_buf)); + // /dev/stdin and /dev/fd are mounts of /proc/self/fd, whose links ("pipe:[123]") only the kernel opens + if (strncmp(host, "/proc/", 6) == 0) return cur; + char target[PATH_MAX]; + long n = raw_readlinkat(AT_FDCWD, host, target, sizeof(target) - 1); + if (n <= 0) return cur; + target[n] = '\0'; + n = (long)link_root_guest_target(target, (size_t)n); + char joined[PATH_MAX]; + if (target[0] == '/') { + ddnatv_copy_cstr(joined, sizeof(joined), target); + } else { + const char *slash = strrchr(cur, '/'); + size_t dir_len = slash ? (size_t)(slash - cur) : 0; + size_t used = ddnatv_append(joined, sizeof(joined), 0, cur, dir_len); + used = ddnatv_append(joined, sizeof(joined), used, "/", 1); + ddnatv_append(joined, sizeof(joined), used, target, strlen(target)); + } + // lexically, as the guest sees it + size_t len = 0; + out[0] = '\0'; + for (const char *p = joined; *p;) { + while (*p == '/') p++; + const char *end = strchr(p, '/'); + size_t part = end ? (size_t)(end - p) : strlen(p); + if (part == 0) break; + if (part == 1 && p[0] == '.') { + } else if (part == 2 && p[0] == '.' && p[1] == '.') { + while (len > 0 && out[len - 1] != '/') len--; + if (len > 0) len--; + out[len] = '\0'; + } else if (len + 1 + part < outsz) { + out[len++] = '/'; + memcpy(out + len, p, part); + len += part; + out[len] = '\0'; + } + p += part; + } + if (len == 0) { out[0] = '/'; out[1] = '\0'; } + cur = out; + } + return cur; +#else + (void)out; (void)outsz; + return path; +#endif +} + +// On ENOENT, stat()/lstat()/readlink() retry /usr/bin <-> /bin and /usr/sbin <-> /sbin, for +// rootfs trees where bin and sbin are real directories. Returns 1 for the bin pair, 2 for the +// sbin pair (with the other spelling in `alt`), 0 when `sub` is in neither. +#define DDNATV_USRMERGE_BIN 1 +#define DDNATV_USRMERGE_SBIN 2 +static int usrmerge_alt_path(const char *sub, char *alt, size_t altsz) { + const char *dir; + const char *rest; + int pair; + if (strncmp(sub, "/usr/bin/", 9) == 0) { + dir = "/bin/"; rest = sub + 9; pair = DDNATV_USRMERGE_BIN; + } else if (strncmp(sub, "/bin/", 5) == 0) { + dir = "/usr/bin/"; rest = sub + 5; pair = DDNATV_USRMERGE_BIN; + } else if (strncmp(sub, "/usr/sbin/", 10) == 0) { + dir = "/sbin/"; rest = sub + 10; pair = DDNATV_USRMERGE_SBIN; + } else if (strncmp(sub, "/sbin/", 6) == 0) { + dir = "/usr/sbin/"; rest = sub + 6; pair = DDNATV_USRMERGE_SBIN; + } else { + return 0; + } + size_t n = ddnatv_append(alt, altsz, 0, g_ddnatv_root, g_ddnatv_root_len); + n = ddnatv_append(alt, altsz, n, dir, strlen(dir)); + ddnatv_append(alt, altsz, n, rest, strlen(rest)); + return pair; +} + +// Whether postprocess_readlink() below can change a link's target of length target_len, read +// through linkpath: only a target that starts with the rootfs path (at least that long) or a relative +// one into /etc/alternatives (at least "../alternatives", 15 characters, from a directory next to it; +// any "../" under /etc/alternatives itself). lstat() of a link reads it only then, to report its +// rewritten length. +static int readlink_result_may_change(const char *linkpath, long long target_len) { + if (g_ddnatv_root_len > 0 && target_len >= (long long)g_ddnatv_root_len) return 1; + if (!linkpath || linkpath[0] != '/') return 0; + if (strncmp(linkpath, "/etc/alternatives", 17) == 0) return target_len >= 2; + return target_len >= 15; +} + +// Helper for path normalization +static void normalize_path(char *path) { + if (!path || !*path) return; + int is_abs = (path[0] == '/'); + char *stack[128]; + int top = 0; + + char tmp[4096]; + strncpy(tmp, path, sizeof(tmp) - 1); + tmp[sizeof(tmp) - 1] = '\0'; + + char *saveptr = NULL; + char *tok = strtok_r(tmp, "/", &saveptr); + while (tok) { + if (strcmp(tok, ".") == 0) { + // ignore + } else if (strcmp(tok, "..") == 0) { + if (top > 0) top--; + } else { + if (top < 128) stack[top++] = tok; + } + tok = strtok_r(NULL, "/", &saveptr); + } + + char *dst = path; + if (is_abs) *dst++ = '/'; + for (int i = 0; i < top; i++) { + size_t len = strlen(stack[i]); + memcpy(dst, stack[i], len); + dst += len; + if (i + 1 < top) *dst++ = '/'; + } + if (dst == path && is_abs) *dst++ = '/'; + *dst = '\0'; +} + +// /proc/self/fd/N, /proc/thread-self/fd/N and /proc/PID/fd/N: the real path of an open file. A program +// that finds a path this way (Bun and Zig programs do: their realpath() is an open and a readlink of +// this) goes on with system calls of its own, which the hook does not see, so a path inside the rootfs +// has to keep its real beginning there: "/home" for $DDNATV_ROOT/home is a path that does not exist +// for them ("ENOENT, realpath '/home'" in Kilo). It is what getcwd() answers, too. +static int is_proc_fd_link(const char *path) { + if (!path || strncmp(path, "/proc/", 6) != 0) return 0; + const char *p = path + 6; + if (strncmp(p, "self/", 5) == 0) { + p += 5; + } else if (strncmp(p, "thread-self/", 12) == 0) { + p += 12; + } else { + if (*p < '0' || *p > '9') return 0; + while (*p >= '0' && *p <= '9') p++; + if (*p != '/') return 0; + p++; + } + // fd/N and nothing after it: a path beneath the link goes through it to somewhere else + if (strncmp(p, "fd/", 3) != 0) return 0; + p += 3; + if (*p < '0' || *p > '9') return 0; + while (*p >= '0' && *p <= '9') p++; + return *p == '\0'; +} + +// The guest path of a folder that a session mount puts outside the rootfs, for what the kernel +// reports as its real place (getcwd(), /proc/self/cwd and fd links). Wine found no DOS drive for +// Sekiro's folder on the SD card library ("starting in the Windows directory"), and the game quit. +// Rewrites buf[0..len) in place when the guest path fits in cap; returns the new length. +static size_t guest_path_of_mount_source(char *buf, size_t len, size_t cap) { + const struct ddnatv_bind *best = NULL; + for (int i = 0; i < g_nbinds; i++) { + const struct ddnatv_bind *b = &g_binds[i]; + if (b->identity || get_rootfs_subpath(b->src) != NULL || len < b->src_len) continue; + if (memcmp(buf, b->src, b->src_len) != 0 || (len > b->src_len && buf[b->src_len] != '/')) continue; + if (!best || b->src_len > best->src_len || (b->src_len == best->src_len && b->order < best->order)) best = b; + } + if (!best) return len; + size_t rest = len - best->src_len; + if (best->dst_len + rest + 1 > cap) return len; + memmove(buf + best->dst_len, buf + best->src_len, rest); + memcpy(buf, best->dst, best->dst_len); + buf[best->dst_len + rest] = '\0'; + return best->dst_len + rest; +} + +static ssize_t postprocess_readlink(const char *linkpath, char *buf, ssize_t ret, size_t bufsiz) { + if (ret <= 0) return ret; + if ((size_t)ret < bufsiz) { + buf[ret] = '\0'; + ret = (ssize_t)link_root_guest_target(buf, (size_t)ret); + } + ret = (ssize_t)guest_path_of_mount_source(buf, (size_t)ret, bufsiz); + + // 1. Strip g_ddnatv_root if target starts with it + if (g_ddnatv_root_len > 0 && !is_proc_fd_link(linkpath)) { + size_t rlen = g_ddnatv_root_len; + if ((size_t)ret >= rlen && memcmp(buf, g_ddnatv_root, rlen) == 0 && + ((size_t)ret == rlen || buf[rlen] == '/')) { + size_t new_len = ret - rlen; + if (new_len == 0) { + buf[0] = '/'; + return 1; + } else { + memmove(buf, buf + rlen, new_len); + return (ssize_t)new_len; + } + } + } + + // 2. Normalize relative alternatives links. The result can only start with /etc/alternatives + // (or the link be in it) when "alternatives" is in the target or in the link's own path: the + // normalized path is made of their components. Most relative links have neither (the ones of + // /usr/lib and /usr/share/doc), and these skip the copies and the normalization, which were a + // tenth of what "cp -a" of a tree of them cost. + if (linkpath && linkpath[0] == '/' && (size_t)ret < PATH_MAX && ret >= 2 && buf[0] == '.' && buf[1] == '.' && + (memmem(buf, (size_t)ret, "alternatives", 12) != NULL || strstr(linkpath, "alternatives") != NULL)) { + char link_copy[PATH_MAX]; + memcpy(link_copy, buf, ret); + link_copy[ret] = '\0'; + + if (strncmp(link_copy, "..", 2) == 0) { + char parent[PATH_MAX]; + snprintf(parent, sizeof(parent), "%s", linkpath); + char *last_slash = strrchr(parent, '/'); + if (last_slash) { + if (last_slash == parent) { + *(last_slash + 1) = '\0'; + } else { + *last_slash = '\0'; + } + } + char combined[PATH_MAX * 2]; + snprintf(combined, sizeof(combined), "%s/%s", parent, link_copy); + normalize_path(combined); + + if (strncmp(combined, "/etc/alternatives", 17) == 0 || + strncmp(linkpath, "/etc/alternatives", 17) == 0) { + size_t clen = strlen(combined); + if (clen <= bufsiz) { + memcpy(buf, combined, clen); + return (ssize_t)clen; + } + } + } + } + + return ret; +} + +// Android's SELinux label, which the xattr hooks keep away from Ubuntu programs. +static const char k_selinux_xattr[] = "security.selinux"; + +static int is_selinux_xattr(const char *name) { + return name != NULL && strcmp(name, k_selinux_xattr) == 0; +} + +// Removes "security.selinux" from a listxattr() result (NUL-separated names); returns the new length. +static ssize_t drop_selinux_xattr(char *list, ssize_t len) { + if (!list || len <= 0) return len; + ssize_t out = 0; + ssize_t i = 0; + while (i < len) { + size_t name_len = strnlen(list + i, (size_t)(len - i)); + ssize_t entry = (ssize_t)name_len + 1; + if (entry > len - i) entry = len - i; + if (name_len != sizeof(k_selinux_xattr) - 1 || memcmp(list + i, k_selinux_xattr, name_len) != 0) { + memmove(list + out, list + i, (size_t)entry); + out += entry; + } + i += entry; + } + return out; +} + +// Library directories of the rootfs, in the order ld.so searches them. ld.so tries every +// directory for every library, so the multiarch ones that hold nearly all libraries come first +// (as in glibc's own default order). Keep in sync with Environment.kt, 00-env.sh and su.sh. +static const char *const k_rootfs_lib_dirs[] = { + "/lib/aarch64-linux-gnu", "/usr/lib/aarch64-linux-gnu", + "/lib/arm-linux-gnueabihf", "/usr/lib/arm-linux-gnueabihf", + "/lib", "/usr/lib", "/usr/local/lib", + "/usr/lib/systemd", "/lib/systemd", + "/usr/lib/aarch64-linux-gnu/systemd", "/lib/aarch64-linux-gnu/systemd", + "/usr/lib/arm-linux-gnueabihf/systemd", "/lib/arm-linux-gnueabihf/systemd", +}; + +// Whether the ':'-separated list holds the directory dir[0..dir_len) as one of its entries. +static int lib_dir_list_has(const char *list, const char *dir, size_t dir_len) { + if (!list) return 0; + for (const char *p = list; *p;) { + const char *end = strchr(p, ':'); + size_t len = end ? (size_t)(end - p) : strlen(p); + if (len == dir_len && memcmp(p, dir, len) == 0) return 1; + if (!end) break; + p = end + 1; + } + return 0; +} + +// Appends the rootfs library directories as a ':'-separated list (with a leading ':' when +// `separate` is set), leaving out those that `have` (a list too, or NULL) names already: the +// caller's LD_LIBRARY_PATH is usually this very list, and ld.so spends time on every directory of +// the list it is given, in every program that is started, and tries it again for each library that +// is not found. Returns the new length. +static size_t append_rootfs_lib_dirs_missing_from(char *out, size_t outsz, size_t used, int separate, const char *have) { + for (size_t i = 0; i < sizeof(k_rootfs_lib_dirs) / sizeof(k_rootfs_lib_dirs[0]); i++) { + // a Flatpak sandbox mounts /usr from its runtime + char buf[PATH_MAX]; + const char *full = rewrite_path_in_root(k_rootfs_lib_dirs[i], buf, sizeof(buf)); + size_t n = strlen(full); + if (have && have[0] != '\0' && lib_dir_list_has(have, full, n)) continue; + // whole directories only: a cut-off one would be searched in the wrong place + if (used + (separate ? 1 : 0) + n >= outsz) continue; + if (separate) used = ddnatv_append(out, outsz, used, ":", 1); + separate = 1; + used = ddnatv_append(out, outsz, used, full, n); + } + return used; +} + +// All of them. +static size_t append_rootfs_lib_dirs(char *out, size_t outsz, size_t used, int separate) { + return append_rootfs_lib_dirs_missing_from(out, outsz, used, separate, NULL); +} + +#ifndef DDNATV_HOST_TEST +// Relative *at() calls. A program that walks a path one component at a time (systemd's chase(), used +// by libudev; fts) opens "/" and then "sys", "class", ... relative to the last descriptor, and those +// names never pass through rewrite_path(): "/" is the rootfs, whose own sys/ is empty, so the stand-ins +// mounted at /sys/class/hidraw and the host's /sys were out of reach. The directories on the way to a +// mount (every proper ancestor of a mount's guest path) are recorded by device and inode; a name given +// relative to one of them is joined to its guest path and mapped like an absolute one. +struct ddnatv_at_dir { + unsigned long dev, ino; + char *guest; +}; +static struct ddnatv_at_dir *g_at_dirs = NULL; +static int g_n_at_dirs = 0; +static pthread_once_t g_at_once = PTHREAD_ONCE_INIT; + +#if defined(__aarch64__) +static long raw_fstatat(int dirfd, const char *path, struct stat *st, int flags) { + register long x8 __asm__("x8") = 79; // newfstatat + register long x0 __asm__("x0") = dirfd; + register long x1 __asm__("x1") = (long)path; + register long x2 __asm__("x2") = (long)st; + register long x3 __asm__("x3") = flags; + __asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3) : "memory"); + return x0; +} +#endif + +static void ddnatv_add_at_dir(const char *guest) { +#if defined(__aarch64__) + for (int i = 0; i < g_n_at_dirs; i++) if (strcmp(g_at_dirs[i].guest, guest) == 0) return; + char buf[PATH_MAX]; + const char *host = rewrite_path_in_root(guest, buf, sizeof(buf)); + struct stat st; + if (raw_fstatat(AT_FDCWD, host, &st, 0) != 0 || !S_ISDIR(st.st_mode)) return; + struct ddnatv_at_dir *grown = realloc(g_at_dirs, sizeof(*grown) * (size_t)(g_n_at_dirs + 1)); + if (!grown) return; + g_at_dirs = grown; + g_at_dirs[g_n_at_dirs].dev = (unsigned long)st.st_dev; + g_at_dirs[g_n_at_dirs].ino = (unsigned long)st.st_ino; + g_at_dirs[g_n_at_dirs].guest = strdup(guest); + if (g_at_dirs[g_n_at_dirs].guest) g_n_at_dirs++; +#else + (void)guest; +#endif +} + +static void ddnatv_build_at_dirs(void) { + if (g_nbinds == 0) return; + ddnatv_add_at_dir("/"); + for (int i = 0; i < g_nbinds; i++) { + char guest[PATH_MAX]; + ddnatv_copy_cstr(guest, sizeof(guest), g_binds[i].dst); + for (char *slash = strrchr(guest, '/'); slash && slash > guest; slash = strrchr(guest, '/')) { + *slash = '\0'; + ddnatv_add_at_dir(guest); + } + } +} + +// The absolute host path for `rel` given relative to `dirfd`, or NULL when the call needs no change. +static const char *map_at(int dirfd, const char *rel, char *buf, size_t size) { +#if defined(__aarch64__) + if (dirfd == AT_FDCWD || !rel || rel[0] == '\0' || rel[0] == '/' || g_nbinds == 0) return NULL; + pthread_once(&g_at_once, ddnatv_build_at_dirs); + if (g_n_at_dirs == 0) return NULL; + struct stat st; + if (raw_fstatat(dirfd, "", &st, AT_EMPTY_PATH) != 0) return NULL; + for (int i = 0; i < g_n_at_dirs; i++) { + if (g_at_dirs[i].ino != (unsigned long)st.st_ino || g_at_dirs[i].dev != (unsigned long)st.st_dev) continue; + char guest[PATH_MAX]; + const char *base = g_at_dirs[i].guest; + size_t used = ddnatv_append(guest, sizeof(guest), 0, base, strlen(base)); + if (used > 1) used = ddnatv_append(guest, sizeof(guest), used, "/", 1); + ddnatv_append(guest, sizeof(guest), used, rel, strlen(rel)); + const char *mapped = rewrite_path_in_root(guest, buf, size); + if (mapped != buf) ddnatv_copy_cstr(buf, size, mapped); + return buf; + } +#else + (void)dirfd; (void)rel; (void)buf; (void)size; +#endif + return NULL; +} + +#define DDNATV_AT_REMAP_NAMED(dirfd, pathname, buf) \ + char buf[PATH_MAX]; \ + if (map_at((dirfd), (pathname), buf, sizeof(buf))) { \ + (dirfd) = AT_FDCWD; \ + (pathname) = buf; \ + } +#define DDNATV_AT_REMAP(dirfd, pathname) DDNATV_AT_REMAP_NAMED(dirfd, pathname, ddnatv_at_buf) +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/posix_shm.c b/tools/nativeguest/hook/posix_shm.c new file mode 100644 index 0000000000..afd8a6f655 --- /dev/null +++ b/tools/nativeguest/hook/posix_shm.c @@ -0,0 +1,174 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c. +// +// shm_open() and shm_unlink() of glibc open /dev/shm/NAME with a system call of their own, which +// the path hooks never see and Android has no /dev/shm for. They go through open() and unlink() +// here, which map /dev/shm. + +#ifndef DDNATV_HOST_TEST + +static int posix_shm_path(const char *name, char *path, size_t size) { + if (!name) { errno = EINVAL; return -1; } + while (*name == '/') name++; + size_t len = strlen(name); + if (len == 0 || len > NAME_MAX || strchr(name, '/') || strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { + errno = len > NAME_MAX ? ENAMETOOLONG : EINVAL; + return -1; + } + if (snprintf(path, size, "/dev/shm/%s", name) >= (int)size) { errno = ENAMETOOLONG; return -1; } + return 0; +} + +int shm_open(const char *name, int oflag, mode_t mode) { + char path[PATH_MAX]; + if (posix_shm_path(name, path, sizeof(path)) < 0) return -1; + return open(path, (oflag & (O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) | O_NOFOLLOW | O_CLOEXEC, mode); +} + +int shm_unlink(const char *name) { + char path[PATH_MAX]; + if (posix_shm_path(name, path, sizeof(path)) < 0) return -1; + return unlink(path); +} + +#endif /* !DDNATV_HOST_TEST */ + +#ifndef DDNATV_HOST_TEST +// glibc's posix_spawn child resets every signal handler to its default, the hook's SIGSYS handler +// included, before it runs the file actions, and "close everything from N" is close_range(), which +// Android's seccomp filter answers with SIGSYS: the child died of it (Python's subprocess with +// close_fds does this). It becomes one close per descriptor, added when the program is spawned so +// that descriptors opened in between and the ones earlier actions make are closed too; the targets +// of actions added after it stay open, as they would. A close of a descriptor that is not open is +// no error to glibc. +#define CLOSEFROM_SLOTS 64 +#define CLOSEFROM_KEEP 32 +static struct closefrom_entry { + posix_spawn_file_actions_t *actions; + int from; // -1: no close-from yet, only targets of actions seen + int highest; // the highest descriptor an action before close-from makes + int nkeep; + int keep[CLOSEFROM_KEEP]; + int closed_to; // the descriptors up to this one are closed by actions already added + int bad_source; // a dup2 after close-from duplicates a descriptor it closed: glibc's EBADF +} g_closefrom[CLOSEFROM_SLOTS]; +static pthread_mutex_t g_closefrom_lock = PTHREAD_MUTEX_INITIALIZER; + +static struct closefrom_entry *closefrom_entry(posix_spawn_file_actions_t *actions, int create) { + struct closefrom_entry *free_slot = NULL; + for (int i = 0; i < CLOSEFROM_SLOTS; i++) { + if (g_closefrom[i].actions == actions) return &g_closefrom[i]; + if (!g_closefrom[i].actions && !free_slot) free_slot = &g_closefrom[i]; + } + if (!create || !free_slot) return NULL; + memset(free_slot, 0, sizeof(*free_slot)); + free_slot->actions = actions; + free_slot->from = -1; + free_slot->highest = -1; + free_slot->closed_to = -1; + return free_slot; +} + +static void closefrom_note_target(posix_spawn_file_actions_t *actions, int fd) { + pthread_mutex_lock(&g_closefrom_lock); + struct closefrom_entry *e = closefrom_entry(actions, 1); + if (e) { + if (e->from < 0) { + if (fd > e->highest) e->highest = fd; + } else if (e->nkeep < CLOSEFROM_KEEP) { + e->keep[e->nkeep++] = fd; + } + } + pthread_mutex_unlock(&g_closefrom_lock); +} + +int posix_spawn_file_actions_addclosefrom_np(posix_spawn_file_actions_t *actions, int from) { + if (!actions || from < 0) return EINVAL; + pthread_mutex_lock(&g_closefrom_lock); + struct closefrom_entry *e = closefrom_entry(actions, 1); + if (e && (e->from < 0 || from < e->from)) e->from = from; + pthread_mutex_unlock(&g_closefrom_lock); + return e ? 0 : ENOMEM; +} + +int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t *actions, int fd, const char *path, int oflag, mode_t mode) { + static int (*orig)(posix_spawn_file_actions_t *, int, const char *, int, mode_t) = NULL; + if (!orig) orig = (int (*)(posix_spawn_file_actions_t *, int, const char *, int, mode_t))dlsym(RTLD_NEXT, "posix_spawn_file_actions_addopen"); + int ret = orig ? orig(actions, fd, path, oflag, mode) : ENOSYS; + if (ret == 0) closefrom_note_target(actions, fd); + return ret; +} + +int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *actions, int fd, int newfd) { + static int (*orig)(posix_spawn_file_actions_t *, int, int) = NULL; + if (!orig) orig = (int (*)(posix_spawn_file_actions_t *, int, int))dlsym(RTLD_NEXT, "posix_spawn_file_actions_adddup2"); + int ret = orig ? orig(actions, fd, newfd) : ENOSYS; + if (ret == 0) { + pthread_mutex_lock(&g_closefrom_lock); + struct closefrom_entry *e = closefrom_entry(actions, 0); + if (e && e->from >= 0 && fd >= e->from) { + int reopened = 0; + for (int k = 0; k < e->nkeep; k++) reopened |= e->keep[k] == fd; + if (!reopened) e->bad_source = 1; + } + pthread_mutex_unlock(&g_closefrom_lock); + closefrom_note_target(actions, newfd); + } + return ret; +} + +int posix_spawn_file_actions_init(posix_spawn_file_actions_t *actions) { + static int (*orig)(posix_spawn_file_actions_t *) = NULL; + if (!orig) orig = (int (*)(posix_spawn_file_actions_t *))dlsym(RTLD_NEXT, "posix_spawn_file_actions_init"); + pthread_mutex_lock(&g_closefrom_lock); + struct closefrom_entry *e = closefrom_entry(actions, 0); + if (e) e->actions = NULL; + pthread_mutex_unlock(&g_closefrom_lock); + return orig ? orig(actions) : ENOSYS; +} + +int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *actions) { + static int (*orig)(posix_spawn_file_actions_t *) = NULL; + if (!orig) orig = (int (*)(posix_spawn_file_actions_t *))dlsym(RTLD_NEXT, "posix_spawn_file_actions_destroy"); + pthread_mutex_lock(&g_closefrom_lock); + struct closefrom_entry *e = closefrom_entry(actions, 0); + if (e) e->actions = NULL; + pthread_mutex_unlock(&g_closefrom_lock); + return orig ? orig(actions) : ENOSYS; +} + +static int apply_closefrom(posix_spawn_file_actions_t *actions) { + pthread_mutex_lock(&g_closefrom_lock); + struct closefrom_entry *e = closefrom_entry(actions, 0); + if (!e || e->from < 0) { + pthread_mutex_unlock(&g_closefrom_lock); + return 0; + } + if (e->bad_source) { + pthread_mutex_unlock(&g_closefrom_lock); + return EBADF; + } + int highest = e->highest; + DIR *dir = opendir("/proc/self/fd"); + if (dir) { + int self = dirfd(dir); + struct dirent *entry; + while ((entry = readdir(dir)) != NULL) { + if (entry->d_name[0] < '0' || entry->d_name[0] > '9') continue; + int fd = atoi(entry->d_name); + if (fd != self && fd > highest) highest = fd; + } + closedir(dir); + } + int start = e->from > e->closed_to + 1 ? e->from : e->closed_to + 1; + for (int fd = start; fd <= highest; fd++) { + int kept = 0; + for (int k = 0; k < e->nkeep; k++) kept |= e->keep[k] == fd; + if (!kept && posix_spawn_file_actions_addclose(actions, fd) != 0) break; + e->closed_to = fd; + } + pthread_mutex_unlock(&g_closefrom_lock); + return 0; +} +#else +static int apply_closefrom(posix_spawn_file_actions_t *actions) { (void)actions; return 0; } +#endif diff --git a/tools/nativeguest/hook/privileged.c b/tools/nativeguest/hook/privileged.c new file mode 100644 index 0000000000..6b527ae4f3 --- /dev/null +++ b/tools/nativeguest/hook/privileged.c @@ -0,0 +1,815 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Calls an app may not make (chown, mknod, capabilities, prctl, landlock, seccomp, namespaces, +// close_range) and the generic syscall() entry point. + +// The ARM translator of x86 devices and emulators (ndk_translation, houdini) keeps memory files of +// its own open in the process (ndk_translation at descriptor 3) and needs them whenever it +// translates more code: a program that closes every descriptor it did not open (sshd at start, +// closefrom(3)) made it abort later ("CHECK failed" in mmap_posix.cc). Its memory files open when +// the hook starts survive close(), close_range() and closefrom(); phones that run ARM code +// themselves have none. +#define DDNATV_MAX_KEPT_FDS 4 +static int g_kept_fds[DDNATV_MAX_KEPT_FDS]; // ascending +static int g_kept_fd_count = 0; + +static int is_kept_fd(int fd) { + for (int i = 0; i < g_kept_fd_count; i++) { + if (g_kept_fds[i] == fd) return 1; + } + return 0; +} + +// Calls fn for each part of [first, last] that leaves out the descriptors in kept (ascending); +// the first nonzero answer of fn is returned, else 0. +static int for_each_range_without(unsigned int first, unsigned int last, const int *kept, int nkept, + int (*fn)(unsigned int, unsigned int, void *), void *arg) { + unsigned int from = first; + for (int i = 0; i < nkept && from <= last; i++) { + if (kept[i] < 0 || (unsigned int)kept[i] < from || (unsigned int)kept[i] > last) continue; + unsigned int k = (unsigned int)kept[i]; + if (k > from) { + int r = fn(from, k - 1, arg); + if (r != 0) return r; + } + if (k == last) return 0; + from = k + 1; + } + return from <= last ? fn(from, last, arg) : 0; +} + +#ifndef DDNATV_HOST_TEST + +// ndk_translation's file is "/memfd:child"; any memory file counts in a process whose executable is +// a translator's runner (houdini). ndk_translation shows a program its own path as /proc/self/exe, +// so the name is what tells there. +// +// This runs at the start of every program, so it costs three system calls on a phone that runs ARM +// code itself: the descriptors that are open (/proc/self/fd, read at once) are all that are asked +// about, where asking about each of the numbers from 3 to 63 was 61 readlink() calls in every +// process that was started. +// Set once this process knows that it has none (it looked, or was told by DDNATV_NT=0 in its +// environment, which prepare_ddnatv_env hands on to the programs it starts) +static int g_no_translator = 0; + +__attribute__((constructor(102))) static void find_translator_files(void) { + // The first program of a session looks; the ones it starts are told (a translator is there for + // all the programs of a machine or for none, and where it is not known they look as before) + const char *known = getenv("DDNATV_NT"); + if (known && known[0] == '0' && known[1] == '\0') { + g_no_translator = 1; + return; + } + ssize_t (*real_readlink)(const char *, char *, size_t) = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink"); + int (*real_open)(const char *, int, ...) = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open"); + int (*real_close)(int) = (int (*)(int))dlsym(RTLD_NEXT, "close"); + ssize_t (*real_getdents64)(int, void *, size_t) = (ssize_t (*)(int, void *, size_t))dlsym(RTLD_NEXT, "getdents64"); + if (!real_readlink || !real_open || !real_close) return; + int dir = real_getdents64 ? real_open("/proc/self/fd", O_RDONLY | O_DIRECTORY | O_CLOEXEC) : -1; + if (dir < 0) return; + // The numbers of the descriptors open now, those from 3 to 63 (the directory's own left out) + char entries[2048]; + unsigned char open_fds[64]; + int nopen = 0; + for (;;) { + long n = real_getdents64(dir, entries, sizeof(entries)); + if (n <= 0) break; + for (long off = 0; off < n;) { + // struct linux_dirent64: d_ino (8), d_off (8), d_reclen (2), d_type (1), then d_name + unsigned short reclen; + memcpy(&reclen, entries + off + 16, sizeof(reclen)); + const char *name = entries + off + 19; + off += reclen ? reclen : n; // a record without a length ends the buffer + if (name[0] < '0' || name[0] > '9') continue; // "." and ".." + int fd = atoi(name); + if (fd >= 3 && fd < 64 && fd != dir) open_fds[nopen++] = (unsigned char)fd; + } + // A short answer is the whole directory; asking again would only be told "no more" + if ((size_t)n + 64 <= sizeof(entries)) break; + } + real_close(dir); + if (nopen == 0) { + g_no_translator = 1; + return; + } + + char path[PATH_MAX]; + int runner = 0; + ssize_t n = real_readlink("/proc/self/exe", path, sizeof(path) - 1); + if (n > 0) { + path[n] = '\0'; + const char *base = strrchr(path, '/'); + base = base ? base + 1 : path; + runner = strncmp(base, "ndk_translation", 15) == 0 || strncmp(base, "houdini", 7) == 0; + } + // In ascending order, as the list is kept + for (int i = 0; i < nopen - 1; i++) { + for (int j = i + 1; j < nopen; j++) { + if (open_fds[j] < open_fds[i]) { + unsigned char t = open_fds[i]; + open_fds[i] = open_fds[j]; + open_fds[j] = t; + } + } + } + for (int i = 0; i < nopen && g_kept_fd_count < DDNATV_MAX_KEPT_FDS; i++) { + char link[32], target[64]; + snprintf(link, sizeof(link), "/proc/self/fd/%d", open_fds[i]); + ssize_t m = real_readlink(link, target, sizeof(target) - 1); + if (m <= 0) continue; + target[m] = '\0'; + if (strncmp(target, "/memfd:child", 12) == 0 || (runner && strncmp(target, "/memfd:", 7) == 0)) { + g_kept_fds[g_kept_fd_count++] = open_fds[i]; + } + } + if (g_kept_fd_count == 0) g_no_translator = 1; +} + +int chown(const char *pathname, uid_t owner, gid_t group) { (void)pathname; (void)owner; (void)group; return 0; } +int fchown(int fd, uid_t owner, gid_t group) { (void)fd; (void)owner; (void)group; return 0; } +int lchown(const char *pathname, uid_t owner, gid_t group) { (void)pathname; (void)owner; (void)group; return 0; } +int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags) { + DDNATV_AT_REMAP(dirfd, pathname); + (void)dirfd; (void)pathname; (void)owner; (void)group; (void)flags; return 0; +} +int chroot(const char *path) { (void)path; return 0; } + +// Hook mkfifo / mkfifoat / mknod / mknodat +int mkfifo(const char *pathname, mode_t mode); +int mkfifoat(int dirfd, const char *pathname, mode_t mode); + +int mknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev) { + DDNATV_AT_REMAP(dirfd, pathname); + (void)dev; + if (!pathname) { + errno = EFAULT; + return -1; + } + char buf[PATH_MAX]; + const char *target = (pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + // A file type of 0 means a regular file, as in mknod(2), which also fails with EEXIST instead + // of touching an existing file. + if (S_ISREG(mode) || (mode & S_IFMT) == 0) { + // mknod(2) returns 0 on success, never a file descriptor: close the + // transient fd instead of leaking one per call (tar/dpkg extract + // thousands of files; leaked fds exhaust the table -> EBADF noise). + int fd = openat(dirfd, target, O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, mode & 07777); + if (fd >= 0) { + close(fd); + return 0; + } + return -1; + } + if (S_ISFIFO(mode)) { + return mkfifoat(dirfd, target, mode); + } + return 0; +} + +int mknod(const char *pathname, mode_t mode, dev_t dev) { + return mknodat(AT_FDCWD, pathname, mode, dev); +} + +int mkfifoat(int dirfd, const char *pathname, mode_t mode) { + static int (*orig_mkfifoat)(int, const char *, mode_t) = NULL; + if (!orig_mkfifoat) orig_mkfifoat = (int (*)(int, const char *, mode_t))dlsym(RTLD_NEXT, "mkfifoat"); + if (!pathname) { + errno = EFAULT; + return -1; + } + char buf[PATH_MAX]; + const char *target = (pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + if (orig_mkfifoat) { + return orig_mkfifoat(dirfd, target, mode); + } + static int (*orig_mkfifo)(const char *, mode_t) = NULL; + if (!orig_mkfifo) orig_mkfifo = (int (*)(const char *, mode_t))dlsym(RTLD_NEXT, "mkfifo"); + if (orig_mkfifo && dirfd == AT_FDCWD) { + return orig_mkfifo(target, mode); + } + return -1; +} + +int mkfifo(const char *pathname, mode_t mode) { + return mkfifoat(AT_FDCWD, pathname, mode); +} + +// Hook sync / syncfs to prevent Android flash storage stalls during dpkg operations +void sync(void) { + // No-op +} + +int syncfs(int fd) { + (void)fd; + return 0; +} + +int capget(void *hdrp, void *datap) { + static int (*orig_capget)(void *, void *) = NULL; + if (!orig_capget) orig_capget = (int (*)(void *, void *))dlsym(RTLD_NEXT, "capget"); + if (orig_capget && orig_capget(hdrp, datap) == 0) { + return 0; + } + if (datap) { + uint32_t ver = hdrp ? *(const uint32_t *)hdrp : 0; + size_t nbytes = (ver == 0x20071026U || ver == 0x20080522U) ? 24 : 12; + memset(datap, 0, nbytes); + } + errno = 0; + return 0; +} +int capset(void *hdrp, const void *datap) { (void)hdrp; (void)datap; return 0; } + +int prctl(int option, ...) { + va_list ap; + va_start(ap, option); + unsigned long arg2 = va_arg(ap, unsigned long); + unsigned long arg3 = va_arg(ap, unsigned long); + unsigned long arg4 = va_arg(ap, unsigned long); + unsigned long arg5 = va_arg(ap, unsigned long); + va_end(ap); + + // PR_SET_NO_NEW_PRIVS = 38, PR_GET_NO_NEW_PRIVS = 39 +#ifdef PR_SET_NO_NEW_PRIVS + if (option == PR_SET_NO_NEW_PRIVS) return 0; +#endif + if (option == 38) return 0; +#ifdef PR_GET_NO_NEW_PRIVS + if (option == PR_GET_NO_NEW_PRIVS) return 1; +#endif + if (option == 39) return 1; + + // PR_SET_SECCOMP = 22, PR_GET_SECCOMP = 21 +#ifdef PR_SET_SECCOMP + if (option == PR_SET_SECCOMP) { errno = EINVAL; return -1; } +#endif + if (option == 22) { errno = EINVAL; return -1; } +#ifdef PR_GET_SECCOMP + if (option == PR_GET_SECCOMP) return 0; +#endif + if (option == 21) return 0; + + // PR_SET_SPECULATION_CTRL = 47 + if (option == 47) return 0; + + static int (*orig_prctl)(int, unsigned long, unsigned long, unsigned long, unsigned long) = NULL; + if (!orig_prctl) orig_prctl = (int (*)(int, unsigned long, unsigned long, unsigned long, unsigned long))dlsym(RTLD_NEXT, "prctl"); + return orig_prctl ? orig_prctl(option, arg2, arg3, arg4, arg5) : 0; +} + +// Limits that leave a process no new descriptors (RLIMIT_NOFILE below 16) or no file writes +// (RLIMIT_FSIZE 0) are not applied. Sandboxes give them to their unprivileged part (sshd's before +// login sets 1 and 0, with the seccomp filter above), and Android's ARM translator (emulators, x86 +// devices) needs new descriptors and memory files in the process: sshd's died in it ("CHECK failed" +// in its mmap) and the client's connection was reset. Other limits are set as asked. +static int sandbox_only_limit(int resource, const struct rlimit *rlim) { + if (!rlim) return 0; + if (resource == RLIMIT_NOFILE) return rlim->rlim_cur < 16; + if (resource == RLIMIT_FSIZE) return rlim->rlim_cur == 0; + return 0; +} + +int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlim) { + if (sandbox_only_limit((int)resource, rlim)) return 0; + static int (*orig_setrlimit)(__rlimit_resource_t, const struct rlimit *) = NULL; +#if defined(__USE_FILE_OFFSET64) && !defined(__LP64__) + // With 64-bit file offsets on a 32-bit system this function is setrlimit64 + if (!orig_setrlimit) orig_setrlimit = (int (*)(__rlimit_resource_t, const struct rlimit *))dlsym(RTLD_NEXT, "setrlimit64"); +#else + if (!orig_setrlimit) orig_setrlimit = (int (*)(__rlimit_resource_t, const struct rlimit *))dlsym(RTLD_NEXT, "setrlimit"); +#endif + if (!orig_setrlimit) { + errno = ENOSYS; + return -1; + } + return orig_setrlimit(resource, rlim); +} +#if defined(__LP64__) +__asm__(".globl setrlimit64\n" ".set setrlimit64, setrlimit\n"); +#endif + +#ifndef LANDLOCK_CREATE_RULESET_VERSION +#define LANDLOCK_CREATE_RULESET_VERSION (1U << 0) +#endif + +#ifndef __NR_landlock_create_ruleset +#define __NR_landlock_create_ruleset 444 +#endif +#ifndef __NR_landlock_add_rule +#define __NR_landlock_add_rule 445 +#endif +#ifndef __NR_landlock_restrict_self +#define __NR_landlock_restrict_self 446 +#endif + +#ifndef __NR_seccomp +#if defined(__aarch64__) +#define __NR_seccomp 277 +#elif defined(__x86_64__) +#define __NR_seccomp 317 +#elif defined(__arm__) +#define __NR_seccomp 383 +#else +#define __NR_seccomp 277 +#endif +#endif + +#ifndef __NR_unshare +#if defined(__aarch64__) +#define __NR_unshare 97 +#elif defined(__x86_64__) +#define __NR_unshare 272 +#elif defined(__arm__) +#define __NR_unshare 337 +#else +#define __NR_unshare 97 +#endif +#endif + +#ifndef __NR_setns +#if defined(__aarch64__) +#define __NR_setns 268 +#elif defined(__x86_64__) +#define __NR_setns 308 +#elif defined(__arm__) +#define __NR_setns 375 +#else +#define __NR_setns 268 +#endif +#endif + +#ifndef __NR_clone3 +#define __NR_clone3 435 +#endif +#ifndef __NR_pidfd_open +#define __NR_pidfd_open 434 +#endif +#ifndef __NR_pidfd_send_signal +#define __NR_pidfd_send_signal 424 +#endif +#ifndef __NR_pidfd_getfd +#define __NR_pidfd_getfd 438 +#endif +#ifndef __NR_close_range +#define __NR_close_range 436 +#endif +#ifndef __NR_faccessat +#if defined(__aarch64__) +#define __NR_faccessat 48 +#elif defined(__arm__) +#define __NR_faccessat 334 +#elif defined(__x86_64__) +#define __NR_faccessat 269 +#endif +#endif +#ifndef __NR_faccessat2 +#define __NR_faccessat2 439 +#endif +#ifndef __NR_statx +#if defined(__aarch64__) || defined(__arm__) +#define __NR_statx 291 +#elif defined(__x86_64__) +#define __NR_statx 332 +#endif +#endif + +#ifndef SECCOMP_SET_MODE_STRICT +#define SECCOMP_SET_MODE_STRICT 0 +#endif +#ifndef SECCOMP_SET_MODE_FILTER +#define SECCOMP_SET_MODE_FILTER 1 +#endif +#ifndef SECCOMP_GET_ACTION_AVAIL +#define SECCOMP_GET_ACTION_AVAIL 2 +#endif + +int landlock_create_ruleset(const void *attr, size_t size, uint32_t flags) { + (void)attr; + (void)size; + (void)flags; + errno = ENOSYS; + return -1; +} + +int landlock_add_rule(int ruleset_fd, int rule_type, const void *rule_attr, uint32_t flags) { + (void)ruleset_fd; + (void)rule_type; + (void)rule_attr; + (void)flags; + errno = ENOSYS; + return -1; +} + +int landlock_restrict_self(int ruleset_fd, uint32_t flags) { + (void)ruleset_fd; + (void)flags; + errno = ENOSYS; + return -1; +} + +int seccomp(unsigned int operation, unsigned int flags, void *args) { + (void)flags; + (void)args; + if (operation != SECCOMP_SET_MODE_STRICT && + operation != SECCOMP_SET_MODE_FILTER && + operation != SECCOMP_GET_ACTION_AVAIL && + operation != 3 /* SECCOMP_GET_NOTIF_SIZES */) { + errno = EINVAL; + return -1; + } + errno = ENOSYS; + return -1; +} + +int unshare(int flags) { + (void)flags; + errno = ENOSYS; + return -1; +} + +int setns(int fd, int nstype) { + (void)fd; + (void)nstype; + errno = ENOSYS; + return -1; +} + +#ifndef CLOSE_RANGE_CLOEXEC +#define CLOSE_RANGE_CLOEXEC (1U << 2) +#endif + +static int close_range_all(unsigned int first, unsigned int last, int flags); + +static int close_range_part(unsigned int first, unsigned int last, void *flags) { + return close_range_all(first, last, *(int *)flags); +} + +int close_range(unsigned int first, unsigned int last, int flags) { + // Close-on-exec only (CLOSE_RANGE_CLOEXEC) leaves the translator's files open + if (g_kept_fd_count > 0 && !(flags & CLOSE_RANGE_CLOEXEC)) { + return for_each_range_without(first, last, g_kept_fds, g_kept_fd_count, close_range_part, &flags); + } + return close_range_all(first, last, flags); +} + +// glibc's closefrom() makes the close_range system call itself, past the function above +void closefrom(int lowfd) { + close_range(lowfd < 0 ? 0U : (unsigned int)lowfd, ~0U, 0); +} + +static int close_range_all(unsigned int first, unsigned int last, int flags) { + static int (*orig_close_range)(unsigned int, unsigned int, int) = NULL; + if (!orig_close_range) { + orig_close_range = (int (*)(unsigned int, unsigned int, int))dlsym(RTLD_NEXT, "close_range"); + } +#ifdef __NR_close_range + static long (*orig_raw_syscall)(long, ...) = NULL; + if (!orig_raw_syscall) orig_raw_syscall = (long (*)(long, ...))dlsym(RTLD_NEXT, "syscall"); + if (orig_raw_syscall) { + long sret = orig_raw_syscall(__NR_close_range, first, last, flags); + if (sret == 0) return 0; + if (sret < 0 && errno != ENOSYS) return -1; + } +#endif + if (orig_close_range) { + int cret = orig_close_range(first, last, flags); + if (cret == 0) return 0; + if (cret < 0 && errno != ENOSYS) return -1; + } + + int set_cloexec = (flags & CLOSE_RANGE_CLOEXEC) != 0; + + DIR *d = opendir("/proc/self/fd"); + if (d) { + int dfd = dirfd(d); + struct dirent *de; + while ((de = readdir(d)) != NULL) { + if (de->d_name[0] == '.') continue; + int fd = atoi(de->d_name); + if (fd >= (int)first && (last == ~0U || (unsigned int)fd <= last) && fd != dfd) { + if (set_cloexec) { + int fl = fcntl(fd, F_GETFD); + if (fl >= 0) { + fcntl(fd, F_SETFD, fl | FD_CLOEXEC); + } + } else { + close(fd); + } + } + } + closedir(d); + return 0; + } + int max_fd = (int)sysconf(_SC_OPEN_MAX); + if (max_fd <= 0 || max_fd > 1024) max_fd = 1024; + int end = (last < (unsigned int)max_fd) ? (int)last : max_fd; + for (int fd = (int)first; fd <= end; fd++) { + if (set_cloexec) { + int fl = fcntl(fd, F_GETFD); + if (fl >= 0) { + fcntl(fd, F_SETFD, fl | FD_CLOEXEC); + } + } else { + close(fd); + } + } + return 0; +} + +static __thread int t_syscall_redirect = 0; + +long syscall(long number, ...) { + va_list ap; + va_start(ap, number); + unsigned long arg1 = va_arg(ap, unsigned long); + unsigned long arg2 = va_arg(ap, unsigned long); + unsigned long arg3 = va_arg(ap, unsigned long); + unsigned long arg4 = va_arg(ap, unsigned long); + unsigned long arg5 = va_arg(ap, unsigned long); + unsigned long arg6 = va_arg(ap, unsigned long); + va_end(ap); + +#if defined(__NR_readlinkat) + if (number == __NR_readlinkat) { + return (long)readlinkat((int)arg1, (const char *)arg2, (char *)arg3, (size_t)arg4); + } +#endif +#if defined(__NR_readlink) + if (number == __NR_readlink) { + return (long)readlink((const char *)arg1, (char *)arg2, (size_t)arg3); + } +#endif +#if defined(__NR_linkat) + if (number == __NR_linkat) { + return (long)linkat((int)arg1, (const char *)arg2, (int)arg3, (const char *)arg4, (int)arg5); + } +#endif +#if defined(__NR_link) + if (number == __NR_link) { + return (long)link((const char *)arg1, (const char *)arg2); + } +#endif + +#ifdef __NR_landlock_create_ruleset + if (number == __NR_landlock_create_ruleset) { + return (long)landlock_create_ruleset((const void *)arg1, (size_t)arg2, (uint32_t)arg3); + } + if (number == __NR_landlock_add_rule) { + return (long)landlock_add_rule((int)arg1, (int)arg2, (const void *)arg3, (uint32_t)arg4); + } + if (number == __NR_landlock_restrict_self) { + return (long)landlock_restrict_self((int)arg1, (uint32_t)arg2); + } +#endif + +#ifdef __NR_seccomp + if (number == __NR_seccomp) { + return (long)seccomp((unsigned int)arg1, (unsigned int)arg2, (void *)arg3); + } +#endif + +#ifdef __NR_unshare + if (number == __NR_unshare) { + return (long)unshare((int)arg1); + } +#endif + +#ifdef __NR_setns + if (number == __NR_setns) { + return (long)setns((int)arg1, (int)arg2); + } +#endif + +#ifdef __NR_clone3 + if (number == __NR_clone3) { + errno = ENOSYS; + return -1; + } +#endif + +#ifdef __NR_pidfd_open + if (number == __NR_pidfd_open) { + errno = ENOSYS; + return -1; + } +#endif +#ifdef __NR_pidfd_send_signal + if (number == __NR_pidfd_send_signal) { + errno = ENOSYS; + return -1; + } +#endif +#ifdef __NR_pidfd_getfd + if (number == __NR_pidfd_getfd) { + errno = ENOSYS; + return -1; + } +#endif + +#ifdef __NR_close_range + if (number == __NR_close_range) { + // libblsession's close_range() calls syscall() again: the nested call goes straight to the kernel + if (t_syscall_redirect) return call_orig_syscall(number, arg1, arg2, arg3, 0, 0, 0); + t_syscall_redirect++; + long ret = (long)close_range((unsigned int)arg1, (unsigned int)arg2, (int)arg3); + t_syscall_redirect--; + return ret; + } +#endif + +#ifdef __NR_openat2 + if (number == __NR_openat2) { + // libblsession's openat2() calls syscall() again (an endless loop until the stack ran out): the nested + // call maps the path and goes straight to the kernel + if (t_syscall_redirect) { + char nested_buf[PATH_MAX]; + const char *path = (const char *)arg2; + if (path && path[0] == '/') path = rewrite_path(path, nested_buf, sizeof(nested_buf)); + return call_orig_syscall(number, arg1, (unsigned long)path, arg3, arg4, 0, 0); + } + t_syscall_redirect++; + long ret = (long)openat2((int)arg1, (const char *)arg2, (struct ddnatv_open_how *)arg3, (size_t)arg4); + t_syscall_redirect--; + return ret; + } +#endif + +#ifdef __NR_fchmodat2 + if (number == __NR_fchmodat2) { + return (long)fchmodat2((int)arg1, (const char *)arg2, (mode_t)arg3, (int)arg4); + } +#endif + +#if defined(__NR_statx) + if (number == __NR_statx) { + return (long)statx((int)arg1, (const char *)arg2, (int)arg3, (unsigned int)arg4, (struct statx *)arg5); + } +#endif + +#if defined(__NR_faccessat) + if (number == __NR_faccessat) { + if (t_syscall_redirect) { + char nested_buf[PATH_MAX]; + const char *path = (const char *)arg2; + if (path && path[0] == '/') path = rewrite_path(path, nested_buf, sizeof(nested_buf)); + return call_orig_syscall(number, arg1, (unsigned long)path, arg3, 0, 0, 0); + } + t_syscall_redirect++; + long ret = (long)faccessat((int)arg1, (const char *)arg2, (int)arg3, 0); + t_syscall_redirect--; + return ret; + } +#endif + +#if defined(__NR_faccessat2) + if (number == __NR_faccessat2) { + return (long)faccessat2((int)arg1, (const char *)arg2, (int)arg3, (int)arg4); + } +#endif + +#if defined(__NR_mknodat) + if (number == __NR_mknodat) { + return (long)mknodat((int)arg1, (const char *)arg2, (mode_t)arg3, (dev_t)arg4); + } +#endif + +#if defined(__NR_mknod) + if (number == __NR_mknod) { + return (long)mknod((const char *)arg1, (mode_t)arg2, (dev_t)arg3); + } +#endif + +#if defined(__NR_mkfifoat) + if (number == __NR_mkfifoat) { + return (long)mkfifoat((int)arg1, (const char *)arg2, (mode_t)arg3); + } +#endif + +#if defined(__NR_setresuid) + if (number == __NR_setresuid) return 0; +#endif +#if defined(__NR_setresuid32) + if (number == __NR_setresuid32) return 0; +#endif +#if defined(__NR_setresgid) + if (number == __NR_setresgid) return 0; +#endif +#if defined(__NR_setresgid32) + if (number == __NR_setresgid32) return 0; +#endif +#if defined(__NR_setuid) + if (number == __NR_setuid) return 0; +#endif +#if defined(__NR_setuid32) + if (number == __NR_setuid32) return 0; +#endif +#if defined(__NR_setgid) + if (number == __NR_setgid) return 0; +#endif +#if defined(__NR_setgid32) + if (number == __NR_setgid32) return 0; +#endif +#if defined(__NR_setreuid) + if (number == __NR_setreuid) return 0; +#endif +#if defined(__NR_setreuid32) + if (number == __NR_setreuid32) return 0; +#endif +#if defined(__NR_setregid) + if (number == __NR_setregid) return 0; +#endif +#if defined(__NR_setregid32) + if (number == __NR_setregid32) return 0; +#endif +#if defined(__NR_setgroups) + if (number == __NR_setgroups) return 0; +#endif +#if defined(__NR_setgroups32) + if (number == __NR_setgroups32) return 0; +#endif +#if defined(__NR_setfsuid) + if (number == __NR_setfsuid) return 0; +#endif +#if defined(__NR_setfsuid32) + if (number == __NR_setfsuid32) return 0; +#endif +#if defined(__NR_setfsgid) + if (number == __NR_setfsgid) return 0; +#endif +#if defined(__NR_setfsgid32) + if (number == __NR_setfsgid32) return 0; +#endif +#if defined(__NR_getuid) + if (number == __NR_getuid) return 0; +#endif +#if defined(__NR_getuid32) + if (number == __NR_getuid32) return 0; +#endif +#if defined(__NR_geteuid) + if (number == __NR_geteuid) return 0; +#endif +#if defined(__NR_geteuid32) + if (number == __NR_geteuid32) return 0; +#endif +#if defined(__NR_getgid) + if (number == __NR_getgid) return 0; +#endif +#if defined(__NR_getgid32) + if (number == __NR_getgid32) return 0; +#endif +#if defined(__NR_getegid) + if (number == __NR_getegid) return 0; +#endif +#if defined(__NR_getegid32) + if (number == __NR_getegid32) return 0; +#endif +#if defined(__NR_getresuid) + if (number == __NR_getresuid) { + uid_t *ruid = (uid_t *)arg1; + uid_t *euid = (uid_t *)arg2; + uid_t *suid = (uid_t *)arg3; + if (ruid) *ruid = 0; + if (euid) *euid = 0; + if (suid) *suid = 0; + return 0; + } +#endif +#if defined(__NR_getresuid32) + if (number == __NR_getresuid32) { + uid_t *ruid = (uid_t *)arg1; + uid_t *euid = (uid_t *)arg2; + uid_t *suid = (uid_t *)arg3; + if (ruid) *ruid = 0; + if (euid) *euid = 0; + if (suid) *suid = 0; + return 0; + } +#endif +#if defined(__NR_getresgid) + if (number == __NR_getresgid) { + gid_t *rgid = (gid_t *)arg1; + gid_t *egid = (gid_t *)arg2; + gid_t *sgid = (gid_t *)arg3; + if (rgid) *rgid = 0; + if (egid) *egid = 0; + if (sgid) *sgid = 0; + return 0; + } +#endif +#if defined(__NR_getresgid32) + if (number == __NR_getresgid32) { + gid_t *rgid = (gid_t *)arg1; + gid_t *egid = (gid_t *)arg2; + gid_t *sgid = (gid_t *)arg3; + if (rgid) *rgid = 0; + if (egid) *egid = 0; + if (sgid) *sgid = 0; + return 0; + } +#endif + + return call_orig_syscall(number, arg1, arg2, arg3, arg4, arg5, arg6); +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/setup.c b/tools/nativeguest/hook/setup.c new file mode 100644 index 0000000000..90c5d87afe --- /dev/null +++ b/tools/nativeguest/hook/setup.c @@ -0,0 +1,384 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Start-up (DDNATV_ROOT, certificate and time zone defaults), /proc/self/exe, rewrite_path() and +// the short names of Unix sockets. + +#ifndef DDNATV_HOST_TEST + +static char g_real_exe[PATH_MAX] = {0}; +// DDNATV_PRELOAD: guest paths of libraries every program loads before the hook (what +// /etc/ld.so.preload does for programs run by the rootfs's own ld.so), and the same as host paths. +static char *g_preload_guest = NULL; +static char *g_preload_host = NULL; +static int g_initialized = 0; + +static void init_ddnatv_hook(void) { + if (g_initialized) return; + const char *root = getenv("DDNATV_ROOT"); + if (root && strlen(root) > 0) { + size_t len = strlen(root); + if (len > sizeof(g_ddnatv_root) - 1) len = sizeof(g_ddnatv_root) - 1; + if (root[len - 1] == '/') len--; + ddnatv_set_root(root, len); + } else { + Dl_info info; + if (dladdr((void *)init_ddnatv_hook, &info) && info.dli_fname) { + const char *p = strstr(info.dli_fname, "/usr/lib/libddnatv-hook"); + if (p && p > info.dli_fname) { + size_t len = p - info.dli_fname; + if (len < sizeof(g_ddnatv_root)) { + ddnatv_set_root(info.dli_fname, len); + } + } + } + } + ddnatv_load_binds(getenv("DDNATV_BINDS")); + const char *hostname = getenv("DDNATV_HOSTNAME"); + if (hostname && hostname[0] && strlen(hostname) < sizeof(g_hostname)) ddnatv_copy_cstr(g_hostname, sizeof(g_hostname), hostname); + const char *link_root = getenv("DDNATV_LINK_ROOT"); + if (link_root && link_root[0] == '/' && strlen(link_root) < sizeof(g_link_root)) { + ddnatv_copy_cstr(g_link_root, sizeof(g_link_root), link_root); + g_link_root_len = strlen(g_link_root); + while (g_link_root_len > 1 && g_link_root[g_link_root_len - 1] == '/') g_link_root[--g_link_root_len] = 0; + } + const char *preload = getenv("DDNATV_PRELOAD"); + if (preload && preload[0] && g_ddnatv_root_len > 0) { + size_t libs = 1; + for (const char *p = preload; *p; p++) libs += *p == ':'; + size_t cap = libs * (PATH_MAX + 1); + char *host = malloc(cap); + if (host) { + size_t used = 0; + host[0] = '\0'; + char *copy = strdup(preload); + char *save = NULL; + for (char *lib = copy ? strtok_r(copy, ":", &save) : NULL; lib; lib = strtok_r(NULL, ":", &save)) { + if (lib[0] != '/') continue; + char mapped_buf[PATH_MAX]; + const char *mapped = rewrite_path_in_root(lib, mapped_buf, sizeof(mapped_buf)); + size_t mapped_len = strlen(mapped); + if (used + mapped_len + 2 > cap) break; + if (used) host[used++] = ':'; + used = ddnatv_append(host, cap, used, mapped, mapped_len); + } + free(copy); + g_preload_host = host; + g_preload_guest = strdup(preload); + } + } + const char *curr_real_exe = getenv("DDNATV_REAL_EXE"); + if (curr_real_exe && curr_real_exe[0] != '\0') { + ddnatv_copy_cstr(g_real_exe, sizeof(g_real_exe), curr_real_exe); + } + if (g_ddnatv_root[0] != '\0') { + const char *curr_tzdir = getenv("TZDIR"); + if (!curr_tzdir || curr_tzdir[0] == '\0') { + char tzdir_buf[PATH_MAX]; + snprintf(tzdir_buf, sizeof(tzdir_buf), "%s/usr/share/zoneinfo", g_ddnatv_root); + setenv("TZDIR", tzdir_buf, 0); + } + + const char *curr_cert = getenv("SSL_CERT_FILE"); + if (!curr_cert || curr_cert[0] == '\0' || strcmp(curr_cert, "/etc/ssl/certs/ca-certificates.crt") == 0) { + char cert_buf[PATH_MAX]; + snprintf(cert_buf, sizeof(cert_buf), "%s/etc/ssl/certs/ca-certificates.crt", g_ddnatv_root); + setenv("SSL_CERT_FILE", cert_buf, 1); + } + + const char *curr_cert_dir = getenv("SSL_CERT_DIR"); + if (!curr_cert_dir || curr_cert_dir[0] == '\0' || strcmp(curr_cert_dir, "/etc/ssl/certs") == 0) { + char cert_dir_buf[PATH_MAX]; + snprintf(cert_dir_buf, sizeof(cert_dir_buf), "%s/etc/ssl/certs:/system/etc/security/cacerts", g_ddnatv_root); + setenv("SSL_CERT_DIR", cert_dir_buf, 1); + } + + const char *curr_curl = getenv("CURL_CA_BUNDLE"); + if (!curr_curl || curr_curl[0] == '\0' || strcmp(curr_curl, "/etc/ssl/certs/ca-certificates.crt") == 0) { + char curl_buf[PATH_MAX]; + snprintf(curl_buf, sizeof(curl_buf), "%s/etc/ssl/certs/ca-certificates.crt", g_ddnatv_root); + setenv("CURL_CA_BUNDLE", curl_buf, 1); + } + + const char *curr_godebug = getenv("GODEBUG"); + if (!curr_godebug || curr_godebug[0] == '\0') { + setenv("GODEBUG", "netdns=cgo", 0); + } else if (strstr(curr_godebug, "netdns") == NULL) { + char godebug_buf[512]; + snprintf(godebug_buf, sizeof(godebug_buf), "%s,netdns=cgo", curr_godebug); + setenv("GODEBUG", godebug_buf, 1); + } + } + g_initialized = 1; +} + +// Before the program runs: init_ddnatv_hook() sets variables, and setenv() may move the block the +// environment is kept in. A program that had changed its environment (putenv, setenv) and passed it +// to execv() before its first call into the hook handed the exec code a block that was freed by the +// time it was read. +__attribute__((constructor(103))) static void init_ddnatv_hook_early(void) { + init_ddnatv_hook(); + // The kernel names the process after what it executed, the rootfs ld.so: process monitors, and + // the app's own checks of /proc//comm (steam, wineserver), saw only "ld-linux-aarch64.so.1" + const char *name = program_invocation_short_name; + if (g_ddnatv_root[0] != '\0' && name && name[0] && strncmp(name, "ld-linux", 8) != 0) { + prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0); + } +} + +void tzset(void) { + static void (*orig_tzset)(void) = NULL; + if (!orig_tzset) orig_tzset = (void (*)(void))dlsym(RTLD_NEXT, "tzset"); + init_ddnatv_hook(); + if (g_ddnatv_root[0] != '\0') { + const char *curr_tzdir = getenv("TZDIR"); + if (!curr_tzdir || curr_tzdir[0] == '\0') { + char tzdir_buf[PATH_MAX]; + snprintf(tzdir_buf, sizeof(tzdir_buf), "%s/usr/share/zoneinfo", g_ddnatv_root); + setenv("TZDIR", tzdir_buf, 0); + } + } + if (orig_tzset) orig_tzset(); +} + +static int is_proc_self_exe(const char *path) { + if (!path) return 0; + if (strcmp(path, "/proc/self/exe") == 0 || + strcmp(path, "/proc/thread-self/exe") == 0) { + return 1; + } + if (strncmp(path, "/proc/", 6) == 0) { + const char *p = path + 6; + while (*p >= '0' && *p <= '9') p++; + if (strcmp(p, "/exe") == 0) { + pid_t pid = (pid_t)atoi(path + 6); + if (pid == getpid()) { + return 1; + } + } + } + return 0; +} + +static ssize_t handle_proc_self_exe(char *buf, size_t bufsiz) { + if (!buf || bufsiz == 0) { + errno = EINVAL; + return -1; + } + init_ddnatv_hook(); + if (g_real_exe[0] == '\0') { + int fd = open("/proc/self/cmdline", O_RDONLY); + if (fd >= 0) { + char cmdline[8192]; + ssize_t n = read(fd, cmdline, sizeof(cmdline) - 1); + close(fd); + if (n > 0) { + cmdline[n] = '\0'; + char *args[64] = {0}; + int ac = 0; + char *p = cmdline; + while (p < cmdline + n && ac < 64) { + args[ac++] = p; + p += strlen(p) + 1; + } + for (int i = 0; i < ac; i++) { + if (strcmp(args[i], "--argv0") == 0 && i + 2 < ac) { + strncpy(g_real_exe, args[i + 2], sizeof(g_real_exe) - 1); + g_real_exe[sizeof(g_real_exe) - 1] = '\0'; + break; + } + } + if (g_real_exe[0] == '\0' && ac > 0 && args[0] && args[0][0] != '\0') { + strncpy(g_real_exe, args[0], sizeof(g_real_exe) - 1); + g_real_exe[sizeof(g_real_exe) - 1] = '\0'; + } + } + } + } + + if (g_real_exe[0] != '\0') { + char stripped[PATH_MAX]; + const char *out_path = g_real_exe; + if (g_ddnatv_root[0] != '\0') { + size_t rlen = g_ddnatv_root_len; + if (strncmp(g_real_exe, g_ddnatv_root, rlen) == 0 && + (g_real_exe[rlen] == '/' || g_real_exe[rlen] == '\0')) { + out_path = g_real_exe + rlen; + if (out_path[0] == '\0') out_path = "/"; + } + } + strncpy(stripped, out_path, sizeof(stripped) - 1); + stripped[sizeof(stripped) - 1] = '\0'; + + size_t len = strlen(stripped); + size_t copy_len = (len < bufsiz) ? len : bufsiz; + memcpy(buf, stripped, copy_len); + return (ssize_t)copy_len; + } + return -1; +} + +static const char *rewrite_path(const char *path, char *buffer, size_t bufsize) { + if (!path) return NULL; + // Relative paths must NEVER be rewritten to rootfs root + if (path[0] != '/') return path; + init_ddnatv_hook(); + return rewrite_path_in_root(path, buffer, bufsize); +} + +// rewrite_path() for calls that follow a symbolic link in the last component. In a mount whose +// source is elsewhere the kernel would follow the link's absolute guest target on Android's own +// tree (a Flatpak app's /app/bin/PINBALL.DAT -> /app/extra/Pinball/PINBALL.DAT), so the link is +// followed in the guest's view first. The rootfs's own links were made relative already. +static const char *rewrite_path_follow(const char *path, char *buffer, size_t bufsize) { + if (!path) return NULL; + if (path[0] != '/') return path; + init_ddnatv_hook(); + int moved = 0; + for (int i = 0; i < g_nbinds; i++) { + const struct ddnatv_bind *b = &g_binds[i]; + if (path[1] == b->dst[1] && is_path_prefix(path, b->dst, b->dst_len)) { moved = !b->identity; break; } + } + char from_source[PATH_MAX]; + if (!moved) { + // or a mount's real place, as /proc/self/exe and getcwd() give it (a Flatpak app's checkout) + const struct ddnatv_bind *best = NULL; + for (int i = 0; i < g_nbinds; i++) { + const struct ddnatv_bind *b = &g_binds[i]; + if (b->identity || !is_path_prefix(path, b->src, b->src_len)) continue; + if (!best || b->src_len > best->src_len) best = b; + } + if (!best) { + const char *host = rewrite_path_in_root(path, buffer, bufsize); +#if defined(__aarch64__) + // A link in the rootfs with an absolute guest target that the hook did not write (one made + // under proot, or copied from Proton's template prefix, as Wine's "z:" -> "/") is followed + // by the kernel on Android's own tree: Wine found no drive for any path and gave games + // \\?\unix\ names, which an NW.js game could not load its DLLs from. + if (is_android_host_path(path)) return host; + char target[PATH_MAX]; + long n = raw_readlinkat(AT_FDCWD, host, target, sizeof(target) - 1); + if (n <= 0 || target[0] != '/') return host; + target[n] = '\0'; + if (is_android_host_path(target) || + (g_ddnatv_root_len && strncmp(target, g_ddnatv_root, g_ddnatv_root_len) == 0)) return host; + char guest_path[PATH_MAX]; + ddnatv_copy_cstr(guest_path, sizeof(guest_path), path); + char guest_target[PATH_MAX]; + const char *followed = follow_guest_link(guest_path, guest_target, sizeof(guest_target)); + char mapped[PATH_MAX]; + ddnatv_copy_cstr(buffer, bufsize, rewrite_path_in_root(followed, mapped, sizeof(mapped))); + return buffer; +#else + return host; +#endif + } + size_t n = ddnatv_append(from_source, sizeof(from_source), 0, best->dst, best->dst_len); + ddnatv_append(from_source, sizeof(from_source), n, path + best->src_len, strlen(path + best->src_len)); + path = from_source; + } + char guest[PATH_MAX]; + const char *followed = follow_guest_link(path, guest, sizeof(guest)); + char mapped[PATH_MAX]; + const char *host = rewrite_path_in_root(followed, mapped, sizeof(mapped)); + // the result may be `followed` or `path` itself, which can live here + ddnatv_copy_cstr(buffer, bufsize, host); + return buffer; +} + +// Whether the rootfs keeps bin (pair DDNATV_USRMERGE_BIN) or sbin as a symlink into /usr, as +// Ubuntu 24.04 does. Checked once per process with one readlink(). +static int rootfs_pair_is_merged(int pair) { + static int known[3] = { 0, -1, -1 }; + if (known[pair] >= 0) return known[pair]; + static ssize_t (*orig_readlink)(const char *, char *, size_t) = NULL; + if (!orig_readlink) orig_readlink = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink"); + const char *dir = (pair == DDNATV_USRMERGE_BIN) ? "/bin" : "/sbin"; + const char *usr_dir = (pair == DDNATV_USRMERGE_BIN) ? "usr/bin" : "usr/sbin"; + char path[PATH_MAX]; + char link[16]; + size_t n = ddnatv_append(path, sizeof(path), 0, g_ddnatv_root, g_ddnatv_root_len); + ddnatv_append(path, sizeof(path), n, dir, strlen(dir)); + int saved_errno = errno; + ssize_t len = orig_readlink ? orig_readlink(path, link, sizeof(link) - 1) : -1; + errno = saved_errno; + int merged = 0; + if (len > 0) { + link[len] = '\0'; + size_t ulen = strlen(usr_dir); + merged = strncmp(link, usr_dir, ulen) == 0 && (link[ulen] == '\0' || (link[ulen] == '/' && link[ulen + 1] == '\0')); + } + known[pair] = merged; + return merged; +} + +// The other spelling of a missing rootfs path under bin/sbin, or NULL when there is none or both +// spellings reach the same directory (the retry could only fail again). +static const char *usrmerge_retry_path(const char *target, char *alt, size_t altsz) { + const char *sub = get_rootfs_subpath(target); + if (!sub) return NULL; + int pair = usrmerge_alt_path(sub, alt, altsz); + if (pair == 0 || rootfs_pair_is_merged(pair)) return NULL; + return alt; +} + +static const char *rewrite_unix_socket_path(const char *sun_path, char *out_buf, size_t out_size) { + if (!sun_path || out_size == 0) return sun_path; + // Abstract sockets start with null byte (\0); leave untouched + if (sun_path[0] == '\0') { + return sun_path; + } + + char full[PATH_MAX]; + const char *rw = (sun_path[0] == '/') ? rewrite_path(sun_path, full, sizeof(full)) : sun_path; + size_t rw_len = strlen(rw); + + // If rewritten path fits safely in sockaddr_un.sun_path (usually 108 bytes) + if (rw_len < out_size) { + strncpy(out_buf, rw, out_size - 1); + out_buf[out_size - 1] = '\0'; + return out_buf; + } + + // Path exceeds sockaddr_un 108-byte limit! + // Deterministically hash the path into /tmp to guarantee it fits safely (< 80 bytes) + // while remaining 100% deterministic between server and client. + unsigned long long hash = 14695981039346656037ULL; + for (const char *p = rw; *p; p++) { + hash ^= (unsigned char)(*p); + hash *= 1099511628211ULL; + } + + init_ddnatv_hook(); + if (g_ddnatv_root[0] != '\0') { + snprintf(out_buf, out_size, "%s/tmp/.ctx_sock_%016llx", g_ddnatv_root, hash); + } else { + snprintf(out_buf, out_size, "/tmp/.ctx_sock_%016llx", hash); + } + out_buf[out_size - 1] = '\0'; + return out_buf; +} + +static void unlink_mapped_unix_socket(const char *target) { + if (!target) return; + size_t t_len = strlen(target); + if (t_len >= 107) { + unsigned long long hash = 14695981039346656037ULL; + for (const char *p = target; *p; p++) { + hash ^= (unsigned char)(*p); + hash *= 1099511628211ULL; + } + char short_path[PATH_MAX]; + init_ddnatv_hook(); + if (g_ddnatv_root[0] != '\0') { + snprintf(short_path, sizeof(short_path), "%s/tmp/.ctx_sock_%016llx", g_ddnatv_root, hash); + } else { + snprintf(short_path, sizeof(short_path), "/tmp/.ctx_sock_%016llx", hash); + } + static int (*orig_unlink)(const char *) = NULL; + if (!orig_unlink) orig_unlink = (int (*)(const char *))dlsym(RTLD_NEXT, "unlink"); + if (orig_unlink) orig_unlink(short_path); + } +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/signals.c b/tools/nativeguest/hook/signals.c new file mode 100644 index 0000000000..0b5765070d --- /dev/null +++ b/tools/nativeguest/hook/signals.c @@ -0,0 +1,233 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// The SIGSYS handler for system calls Android's seccomp filter blocks, and the signal hooks that +// keep programs from replacing or blocking it. + +#ifndef DDNATV_HOST_TEST + +#ifndef SYS_SECCOMP +#define SYS_SECCOMP 1 +#endif + +static int (*get_real_sigaction(void))(int, const struct sigaction *, struct sigaction *); + +// A program's own SIGSYS handler, for the traps of a seccomp filter it installed itself (Firefox's +// and Chromium's sandboxes emulate the calls they trap there). Android's app filter traps with data +// 0; such a sandbox's traps carry their own nonzero data, which the kernel gives as si_errno. +static struct sigaction g_app_sigsys; +static int g_app_sigsys_set = 0; + +static void remember_app_sigsys(const struct sigaction *act, struct sigaction *oldact) { + if (oldact) { + if (g_app_sigsys_set) { + *oldact = g_app_sigsys; + } else { + memset(oldact, 0, sizeof(*oldact)); + oldact->sa_handler = SIG_DFL; + } + } + if (act) { + g_app_sigsys = *act; + g_app_sigsys_set = act->sa_handler != SIG_DFL && act->sa_handler != SIG_IGN; + } +} + +// Intercept SECCOMP blocked syscalls (SIGSYS), resolve sandbox/landlock gracefully, and advance PC +static void ddnatv_sigsys_handler(int sig, siginfo_t *info, void *ctx) { + if (!ctx || !info) return; + if (info->si_code != SYS_SECCOMP) { + int (*real_sig)(int, const struct sigaction *, struct sigaction *) = get_real_sigaction(); + if (real_sig) { + struct sigaction sa_dfl; + memset(&sa_dfl, 0, sizeof(sa_dfl)); + sa_dfl.sa_handler = SIG_DFL; + sigemptyset(&sa_dfl.sa_mask); + real_sig(sig, &sa_dfl, NULL); + } + raise(sig); + return; + } + if (info->si_errno != 0 && g_app_sigsys_set) { + if (g_app_sigsys.sa_flags & SA_SIGINFO) g_app_sigsys.sa_sigaction(sig, info, ctx); + else g_app_sigsys.sa_handler(sig); + return; + } + ucontext_t *uctx = (ucontext_t *)ctx; + + int sys_nr = info->si_syscall; + long ret_val = is_sigsys_synthetic_success_syscall(sys_nr) ? 0 : -ENOSYS; + +#if defined(__aarch64__) + uctx->uc_mcontext.regs[0] = (uint64_t)ret_val; + if (uctx->uc_mcontext.pc >= 4) { + uint32_t insn_at_pc = 0; + uint32_t insn_before_pc = 0; + memcpy(&insn_at_pc, (const void *)uctx->uc_mcontext.pc, sizeof(insn_at_pc)); + memcpy(&insn_before_pc, (const void *)(uctx->uc_mcontext.pc - 4), sizeof(insn_before_pc)); + // On SECCOMP_RET_TRAP, the kernel already advanced PC past svc #0 (*(pc - 4) is svc #0). + // Only advance PC if PC itself points to svc #0 and *(pc - 4) does not. + int at_is_svc = ((insn_at_pc & 0xffe0001fU) == 0xd4000001U); + int prev_is_svc = ((insn_before_pc & 0xffe0001fU) == 0xd4000001U); + if (at_is_svc && !prev_is_svc) { + uctx->uc_mcontext.pc += 4; + } + } +#elif defined(__arm__) + uctx->uc_mcontext.arm_r0 = (unsigned long)ret_val; + if (uctx->uc_mcontext.arm_pc >= 4) { + if (uctx->uc_mcontext.arm_cpsr & 0x20) { + uint16_t insn_at_pc = 0; + uint16_t insn_before_pc = 0; + memcpy(&insn_at_pc, (const void *)uctx->uc_mcontext.arm_pc, sizeof(insn_at_pc)); + memcpy(&insn_before_pc, (const void *)(uctx->uc_mcontext.arm_pc - 2), sizeof(insn_before_pc)); + int at_is_svc = ((insn_at_pc & 0xff00U) == 0xdf00U); + int prev_is_svc = ((insn_before_pc & 0xff00U) == 0xdf00U); + if (at_is_svc && !prev_is_svc) { + uctx->uc_mcontext.arm_pc += 2; + } + } else { + uint32_t insn_at_pc = 0; + uint32_t insn_before_pc = 0; + memcpy(&insn_at_pc, (const void *)uctx->uc_mcontext.arm_pc, sizeof(insn_at_pc)); + memcpy(&insn_before_pc, (const void *)(uctx->uc_mcontext.arm_pc - 4), sizeof(insn_before_pc)); + int at_is_svc = ((insn_at_pc & 0x0f000000U) == 0x0f000000U); + int prev_is_svc = ((insn_before_pc & 0x0f000000U) == 0x0f000000U); + if (at_is_svc && !prev_is_svc) { + uctx->uc_mcontext.arm_pc += 4; + } + } + } +#elif defined(__x86_64__) && defined(REG_RAX) + uctx->uc_mcontext.gregs[REG_RAX] = (greg_t)ret_val; + if (uctx->uc_mcontext.gregs[REG_RIP] >= 2) { + unsigned char insn_at[2] = {0}; + unsigned char insn_prev[2] = {0}; + memcpy(insn_at, (const void *)uctx->uc_mcontext.gregs[REG_RIP], sizeof(insn_at)); + memcpy(insn_prev, (const void *)(uctx->uc_mcontext.gregs[REG_RIP] - 2), sizeof(insn_prev)); + int at_is_sys = (insn_at[0] == 0x0f && insn_at[1] == 0x05); + int prev_is_sys = (insn_prev[0] == 0x0f && insn_prev[1] == 0x05); + if (at_is_sys && !prev_is_sys) { + uctx->uc_mcontext.gregs[REG_RIP] += 2; + } + } +#elif defined(__i386__) && defined(REG_EAX) + uctx->uc_mcontext.gregs[REG_EAX] = (greg_t)ret_val; + if (uctx->uc_mcontext.gregs[REG_EIP] >= 2) { + unsigned char insn_at[2] = {0}; + unsigned char insn_prev[2] = {0}; + memcpy(insn_at, (const void *)uctx->uc_mcontext.gregs[REG_EIP], sizeof(insn_at)); + memcpy(insn_prev, (const void *)(uctx->uc_mcontext.gregs[REG_EIP] - 2), sizeof(insn_prev)); + int at_is_sys = (insn_at[0] == 0xcd && insn_at[1] == 0x80); + int prev_is_sys = (insn_prev[0] == 0xcd && insn_prev[1] == 0x80); + if (at_is_sys && !prev_is_sys) { + uctx->uc_mcontext.gregs[REG_EIP] += 2; + } + } +#endif +} + +static int (*get_real_sigaction(void))(int, const struct sigaction *, struct sigaction *) { + static int (*real_sigaction)(int, const struct sigaction *, struct sigaction *) = NULL; + if (!real_sigaction) { + real_sigaction = (int (*)(int, const struct sigaction *, struct sigaction *))dlsym(RTLD_NEXT, "sigaction"); + if (!real_sigaction) { + real_sigaction = (int (*)(int, const struct sigaction *, struct sigaction *))dlsym(RTLD_DEFAULT, "sigaction"); + } + } + return real_sigaction; +} + +__attribute__((constructor(101))) static void install_sigsys_handler(void) { + int (*real_sig)(int, const struct sigaction *, struct sigaction *) = get_real_sigaction(); + if (!real_sig) return; + + struct sigaction sa; + memset(&sa, 0, sizeof(sa)); + sa.sa_sigaction = ddnatv_sigsys_handler; + sa.sa_flags = SA_SIGINFO | SA_RESTART; + sigemptyset(&sa.sa_mask); + real_sig(SIGSYS, &sa, NULL); +} + +int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) { + int (*real_sig)(int, const struct sigaction *, struct sigaction *) = get_real_sigaction(); + if (!real_sig) return -1; + + if (signum == SIGSYS) { + if (act && act->sa_sigaction == ddnatv_sigsys_handler) { + return real_sig(signum, act, oldact); + } + remember_app_sigsys(act, oldact); + return 0; + } + return real_sig(signum, act, oldact); +} + +int rt_sigaction(int signum, const struct sigaction *act, struct sigaction *oldact, size_t sigsetsize) { + static int (*orig_rt_sigaction)(int, const struct sigaction *, struct sigaction *, size_t) = NULL; + if (!orig_rt_sigaction) orig_rt_sigaction = (int (*)(int, const struct sigaction *, struct sigaction *, size_t))dlsym(RTLD_NEXT, "rt_sigaction"); + + if (signum == SIGSYS) { + if (act && act->sa_sigaction == ddnatv_sigsys_handler) { + return orig_rt_sigaction ? orig_rt_sigaction(signum, act, oldact, sigsetsize) : 0; + } + remember_app_sigsys(act, oldact); + return 0; + } + return orig_rt_sigaction ? orig_rt_sigaction(signum, act, oldact, sigsetsize) : -1; +} + +int __libc_sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) { + return sigaction(signum, act, oldact); +} + +typedef void (*sighandler_t)(int); +sighandler_t signal(int signum, sighandler_t handler) { + static sighandler_t (*orig_signal)(int, sighandler_t) = NULL; + if (!orig_signal) orig_signal = (sighandler_t (*)(int, sighandler_t))dlsym(RTLD_NEXT, "signal"); + if (signum == SIGSYS) { + struct sigaction act, old; + memset(&act, 0, sizeof(act)); + act.sa_handler = handler; + remember_app_sigsys(&act, &old); + return old.sa_handler; + } + return orig_signal ? orig_signal(signum, handler) : NULL; +} + +int sigprocmask(int how, const sigset_t *set, sigset_t *oldset) { + static int (*orig_sigprocmask)(int, const sigset_t *, sigset_t *) = NULL; + if (!orig_sigprocmask) orig_sigprocmask = (int (*)(int, const sigset_t *, sigset_t *))dlsym(RTLD_NEXT, "sigprocmask"); + if (set && (how == SIG_BLOCK || how == SIG_SETMASK)) { + sigset_t mod_set = *set; + sigdelset(&mod_set, SIGSYS); + return orig_sigprocmask ? orig_sigprocmask(how, &mod_set, oldset) : 0; + } + return orig_sigprocmask ? orig_sigprocmask(how, set, oldset) : 0; +} + +int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset) { + static int (*orig_pthread_sigmask)(int, const sigset_t *, sigset_t *) = NULL; + if (!orig_pthread_sigmask) orig_pthread_sigmask = (int (*)(int, const sigset_t *, sigset_t *))dlsym(RTLD_NEXT, "pthread_sigmask"); + if (set && (how == SIG_BLOCK || how == SIG_SETMASK)) { + sigset_t mod_set = *set; + sigdelset(&mod_set, SIGSYS); + return orig_pthread_sigmask ? orig_pthread_sigmask(how, &mod_set, oldset) : 0; + } + return orig_pthread_sigmask ? orig_pthread_sigmask(how, set, oldset) : 0; +} + +int rt_sigprocmask(int how, const sigset_t *set, sigset_t *oldset, size_t sigsetsize) { + static int (*orig_rt_sigprocmask)(int, const sigset_t *, sigset_t *, size_t) = NULL; + if (!orig_rt_sigprocmask) orig_rt_sigprocmask = (int (*)(int, const sigset_t *, sigset_t *, size_t))dlsym(RTLD_NEXT, "rt_sigprocmask"); + if (set && (how == SIG_BLOCK || how == SIG_SETMASK)) { + sigset_t mod_set; + memcpy(&mod_set, set, sizeof(mod_set)); + sigdelset(&mod_set, SIGSYS); + return orig_rt_sigprocmask ? orig_rt_sigprocmask(how, &mod_set, oldset, sigsetsize) : 0; + } + return orig_rt_sigprocmask ? orig_rt_sigprocmask(how, set, oldset, sigsetsize) : 0; +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/sockets.c b/tools/nativeguest/hook/sockets.c new file mode 100644 index 0000000000..2903505f9b --- /dev/null +++ b/tools/nativeguest/hook/sockets.c @@ -0,0 +1,351 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Sockets: Unix socket paths in the rootfs, the redirected DNS queries of loopback DNS, and the +// TCP servers programs start (the app offers to open them in the browser). + +// The TCP port a listening socket was bound to, 0 for other sockets. +static int listening_tcp_port(int fd) { + struct sockaddr_storage ss; + socklen_t len = sizeof(ss); + memset(&ss, 0, sizeof(ss)); + if (syscall(SYS_getsockname, fd, (struct sockaddr *)&ss, &len) != 0) return 0; + if (ss.ss_family == AF_INET && len >= (socklen_t)sizeof(struct sockaddr_in)) + return ntohs(((struct sockaddr_in *)&ss)->sin_port); + if (ss.ss_family == AF_INET6 && len >= (socklen_t)sizeof(struct sockaddr_in6)) + return ntohs(((struct sockaddr_in6 *)&ss)->sin6_port); + return 0; +} + +// Tells the app that this program listens on [port]: $DDNATV_ROOT/run/ddnatv/listening/PORT with +// "PID NAME" (ListeningPorts). Raw system calls: nothing of this library runs again. +static void report_listening_port(const char *root, size_t root_len, int port, const char *name) { + if (root_len == 0 || port <= 0 || port > 65535) return; + char path[PATH_MAX]; + int n = snprintf(path, sizeof(path), "%.*s/run/ddnatv/listening/%d", (int)root_len, root, port); + if (n <= 0 || n >= (int)sizeof(path)) return; + int out = (int)syscall(SYS_openat, AT_FDCWD, path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); + if (out < 0) return; // no app watching (the directory is the app's) + char line[160]; + int m = snprintf(line, sizeof(line), "%d %.100s\n", (int)getpid(), name && name[0] ? name : "?"); + if (m > 0) syscall(SYS_write, out, line, (size_t)(m < (int)sizeof(line) ? m : (int)sizeof(line) - 1)); + syscall(SYS_close, out); +} + +// The first port of the kernel's range for ports it picks itself, from the text of +// /proc/sys/net/ipv4/ip_local_port_range ("32768\t60999"); 32768, Linux's default, when there is none. +static int ephemeral_port_low(const char *range) { + if (!range) return 32768; + while (*range == ' ' || *range == '\t') range++; + int low = 0, digits = 0; + while (*range >= '0' && *range <= '9' && digits < 6) low = low * 10 + (*range++ - '0'), digits++; + return (digits > 0 && low > 0 && low <= 65535) ? low : 32768; +} + +// Whether listen() tells the app about a server on `port`, which it then connects to and sends an +// HTTP request (LocalServers). Only for a port the program chose (`chosen`) below the kernel's own +// range: a server whose port the kernel picked, or one in that range, is how programs talk to +// each other (Gradle's daemon, the Kotlin daemon, a JVM's debugger, test runners). Such a server +// often accepts one connection, so the app's own took the place of the program it waited for: +// that one read EOF ("Did not receive connection preamble" from Gradle, #51). Web servers people +// open (npm run dev, vite, python3 -m http.server, jupyter) are on ports such as 3000, 5173, 8000. +static int listening_port_is_reported(int chosen, int port, int ephemeral_low) { + return chosen && port > 0 && port < ephemeral_low; +} + +#ifndef DDNATV_HOST_TEST + +// The kernel's range, read once (raw system calls: nothing of this library runs again) +static int kernel_ephemeral_port_low(void) { + static int low = 0; + int v = __atomic_load_n(&low, __ATOMIC_RELAXED); + if (v) return v; + char text[64]; + ssize_t n = -1; + int fd = (int)syscall(SYS_openat, AT_FDCWD, "/proc/sys/net/ipv4/ip_local_port_range", O_RDONLY | O_CLOEXEC); + if (fd >= 0) { + n = syscall(SYS_read, fd, text, sizeof(text) - 1); + syscall(SYS_close, fd); + } + if (n > 0) text[n] = '\0'; + v = ephemeral_port_low(n > 0 ? text : NULL); + __atomic_store_n(&low, v, __ATOMIC_RELAXED); + return v; +} + +int listen(int sockfd, int backlog) { + static int (*orig_listen)(int, int) = NULL; + if (!orig_listen) orig_listen = (int (*)(int, int))dlsym(RTLD_NEXT, "listen"); + if (!orig_listen) { + errno = ENOSYS; + return -1; + } + int ret = orig_listen(sockfd, backlog); + if (ret == 0) { + int saved_errno = errno; + int port = is_chosen_port_fd(sockfd) ? listening_tcp_port(sockfd) : 0; + if (listening_port_is_reported(port > 0, port, kernel_ephemeral_port_low())) { + init_ddnatv_hook(); + extern char *program_invocation_short_name; + report_listening_port(g_ddnatv_root, g_ddnatv_root_len, port, program_invocation_short_name); + } + errno = saved_errno; + } + return ret; +} + +int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { + static int (*orig_bind)(int, const struct sockaddr *, socklen_t) = NULL; + if (!orig_bind) orig_bind = (int (*)(int, const struct sockaddr *, socklen_t))dlsym(RTLD_NEXT, "bind"); + + if (addr && addrlen > offsetof(struct sockaddr_un, sun_path) && addr->sa_family == AF_UNIX) { + const struct sockaddr_un *sun = (const struct sockaddr_un *)addr; + if (sun->sun_path[0] != '\0') { + struct sockaddr_un mod_sun; + memset(&mod_sun, 0, sizeof(mod_sun)); + mod_sun.sun_family = AF_UNIX; + rewrite_unix_socket_path(sun->sun_path, mod_sun.sun_path, sizeof(mod_sun.sun_path)); + return orig_bind ? orig_bind(sockfd, (struct sockaddr *)&mod_sun, sizeof(mod_sun)) : -1; + } + } + int ret = orig_bind ? orig_bind(sockfd, addr, addrlen) : -1; + if (ret == 0 && addr) { + int saved_errno = errno; + int chosen = 0; + if (addr->sa_family == AF_INET && addrlen >= (socklen_t)sizeof(struct sockaddr_in)) + chosen = ((const struct sockaddr_in *)addr)->sin_port != 0; + else if (addr->sa_family == AF_INET6 && addrlen >= (socklen_t)sizeof(struct sockaddr_in6)) + chosen = ((const struct sockaddr_in6 *)addr)->sin6_port != 0; + set_chosen_port_fd(sockfd, chosen); + errno = saved_errno; + } + return ret; +} + +int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { + static int (*orig_connect)(int, const struct sockaddr *, socklen_t) = NULL; + if (!orig_connect) orig_connect = (int (*)(int, const struct sockaddr *, socklen_t))dlsym(RTLD_NEXT, "connect"); + + if (addr && addrlen > offsetof(struct sockaddr_un, sun_path) && addr->sa_family == AF_UNIX) { + const struct sockaddr_un *sun = (const struct sockaddr_un *)addr; + if (sun->sun_path[0] != '\0') { + struct sockaddr_un mod_sun; + memset(&mod_sun, 0, sizeof(mod_sun)); + mod_sun.sun_family = AF_UNIX; + char guest[sizeof(sun->sun_path) + 1]; + size_t max = addrlen - offsetof(struct sockaddr_un, sun_path); + if (max > sizeof(sun->sun_path)) max = sizeof(sun->sun_path); + memcpy(guest, sun->sun_path, max); + guest[max] = '\0'; + char followed[PATH_MAX]; + const char *path = guest[0] == '/' ? follow_guest_link(guest, followed, sizeof(followed)) : guest; + rewrite_unix_socket_path(path, mod_sun.sun_path, sizeof(mod_sun.sun_path)); + return orig_connect ? orig_connect(sockfd, (struct sockaddr *)&mod_sun, sizeof(mod_sun)) : -1; + } + } + + if (addr && is_loopback_dns(addr, addrlen)) { + struct sockaddr_storage redirected; + socklen_t redirected_len = dns_redirect_target(addr, &redirected); + int ret = orig_connect ? orig_connect(sockfd, (struct sockaddr *)&redirected, redirected_len) : -1; + if (ret == 0 || errno == EINPROGRESS) { + int saved_errno = errno; + record_dns_redirect(sockfd, addr, addrlen); + errno = saved_errno; + } + return ret; + } + return orig_connect ? orig_connect(sockfd, addr, addrlen) : -1; +} + +ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, + const struct sockaddr *dest_addr, socklen_t addrlen) { + static ssize_t (*orig_sendto)(int, const void *, size_t, int, const struct sockaddr *, socklen_t) = NULL; + if (!orig_sendto) orig_sendto = (ssize_t (*)(int, const void *, size_t, int, const struct sockaddr *, socklen_t))dlsym(RTLD_NEXT, "sendto"); + + if (dest_addr && is_loopback_dns(dest_addr, addrlen)) { + struct sockaddr_storage redirected; + socklen_t redirected_len = dns_redirect_target(dest_addr, &redirected); + record_dns_redirect(sockfd, dest_addr, addrlen); + return orig_sendto ? orig_sendto(sockfd, buf, len, flags, (struct sockaddr *)&redirected, redirected_len) : -1; + } + return orig_sendto ? orig_sendto(sockfd, buf, len, flags, dest_addr, addrlen) : -1; +} + +#if defined(__NR_getuid32) +#define DDNATV_NR_REAL_GETUID __NR_getuid32 +#define DDNATV_NR_REAL_GETGID __NR_getgid32 +#else +#define DDNATV_NR_REAL_GETUID __NR_getuid +#define DDNATV_NR_REAL_GETGID __NR_getgid +#endif + +// Credentials sent over a Unix socket (SCM_CREDENTIALS) have to be the process's own: the kernel +// refuses others with EPERM, and programs fill them in with getuid(), which is root's 0 here. +// PulseAudio's clients send them on every connection, which then failed ("Connection terminated"). +// Returns 1 with the message copied into mod (its control data into ctrl) and the ids made real. +static int real_scm_credentials(const struct msghdr *msg, struct msghdr *mod, char *ctrl, size_t ctrl_size) { + if (!msg || !msg->msg_control || msg->msg_controllen < sizeof(struct cmsghdr) || msg->msg_controllen > ctrl_size) return 0; + int found = 0; + for (struct cmsghdr *c = CMSG_FIRSTHDR(msg); c; c = CMSG_NXTHDR((struct msghdr *)msg, c)) { + if (c->cmsg_level == SOL_SOCKET && c->cmsg_type == SCM_CREDENTIALS) found = 1; + } + if (!found) return 0; + memcpy(mod, msg, sizeof(*mod)); + memcpy(ctrl, msg->msg_control, msg->msg_controllen); + mod->msg_control = ctrl; + for (struct cmsghdr *c = CMSG_FIRSTHDR(mod); c; c = CMSG_NXTHDR(mod, c)) { + if (c->cmsg_level != SOL_SOCKET || c->cmsg_type != SCM_CREDENTIALS || c->cmsg_len < CMSG_LEN(sizeof(struct ucred))) continue; + // a record that claims more than the buffer holds is the kernel's to refuse, not ours to read + if (c->cmsg_len > mod->msg_controllen - (size_t)((char *)c - ctrl)) break; + struct ucred cred; + memcpy(&cred, CMSG_DATA(c), sizeof(cred)); + cred.pid = getpid(); + cred.uid = (uid_t)call_orig_syscall(DDNATV_NR_REAL_GETUID, 0, 0, 0, 0, 0, 0); + cred.gid = (gid_t)call_orig_syscall(DDNATV_NR_REAL_GETGID, 0, 0, 0, 0, 0, 0); + memcpy(CMSG_DATA(c), &cred, sizeof(cred)); + } + return 1; +} + +ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags) { + static ssize_t (*orig_sendmsg)(int, const struct msghdr *, int) = NULL; + if (!orig_sendmsg) orig_sendmsg = (ssize_t (*)(int, const struct msghdr *, int))dlsym(RTLD_NEXT, "sendmsg"); + struct msghdr cred_msg; + char cred_ctrl[512] __attribute__((aligned(8))); + if (real_scm_credentials(msg, &cred_msg, cred_ctrl, sizeof(cred_ctrl))) msg = &cred_msg; + + if (msg && msg->msg_name && is_loopback_dns((const struct sockaddr *)msg->msg_name, msg->msg_namelen)) { + struct sockaddr_storage redirected; + socklen_t redirected_len = dns_redirect_target((const struct sockaddr *)msg->msg_name, &redirected); + struct msghdr mod_msg; + memcpy(&mod_msg, msg, sizeof(mod_msg)); + mod_msg.msg_name = &redirected; + mod_msg.msg_namelen = redirected_len; + record_dns_redirect(sockfd, (const struct sockaddr *)msg->msg_name, msg->msg_namelen); + return orig_sendmsg ? orig_sendmsg(sockfd, &mod_msg, flags) : -1; + } + return orig_sendmsg ? orig_sendmsg(sockfd, msg, flags) : -1; +} + +static void present_peer_as_root(struct ucred *cred) { + if (cred->uid == (uid_t)call_orig_syscall(DDNATV_NR_REAL_GETUID, 0, 0, 0, 0, 0, 0)) cred->uid = 0; + if (cred->gid == (gid_t)call_orig_syscall(DDNATV_NR_REAL_GETGID, 0, 0, 0, 0, 0, 0)) cred->gid = 0; +} + +// A server compares a peer's credentials with the user the peer claims, which is getuid()'s 0 +// here, while the kernel reports the app's ids: dbus-daemon refused every EXTERNAL login ("Did not +// receive a reply"), so the Plasma desktop never started. +int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen) { + static int (*orig_getsockopt)(int, int, int, void *, socklen_t *) = NULL; + if (!orig_getsockopt) orig_getsockopt = (int (*)(int, int, int, void *, socklen_t *))dlsym(RTLD_NEXT, "getsockopt"); + if (!orig_getsockopt) { + errno = ENOSYS; + return -1; + } + int ret = orig_getsockopt(sockfd, level, optname, optval, optlen); + if (ret == 0 && level == SOL_SOCKET && optname == SO_PEERCRED && optval && optlen && *optlen >= sizeof(struct ucred)) { + struct ucred cred; + memcpy(&cred, optval, sizeof(cred)); + present_peer_as_root(&cred); + memcpy(optval, &cred, sizeof(cred)); + } + return ret; +} + +static void received_credentials_as_root(struct msghdr *msg) { + if (!msg || !msg->msg_control || msg->msg_controllen < sizeof(struct cmsghdr)) return; + for (struct cmsghdr *c = CMSG_FIRSTHDR(msg); c; c = CMSG_NXTHDR(msg, c)) { + if (c->cmsg_level != SOL_SOCKET || c->cmsg_type != SCM_CREDENTIALS || c->cmsg_len < CMSG_LEN(sizeof(struct ucred))) continue; + struct ucred cred; + memcpy(&cred, CMSG_DATA(c), sizeof(cred)); + present_peer_as_root(&cred); + memcpy(CMSG_DATA(c), &cred, sizeof(cred)); + } +} + +ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, + struct sockaddr *src_addr, socklen_t *addrlen) { + static ssize_t (*orig_recvfrom)(int, void *, size_t, int, struct sockaddr *, socklen_t *) = NULL; + if (!orig_recvfrom) orig_recvfrom = (ssize_t (*)(int, void *, size_t, int, struct sockaddr *, socklen_t *))dlsym(RTLD_NEXT, "recvfrom"); + + socklen_t cap = addrlen ? *addrlen : 0; + ssize_t ret = orig_recvfrom ? orig_recvfrom(sockfd, buf, len, flags, src_addr, addrlen) : -1; + if (ret > 0 && src_addr && addrlen) { + int saved_errno = errno; + restore_dns_source(sockfd, src_addr, cap, addrlen); + errno = saved_errno; + } + return ret; +} + +// What programs built with _FORTIFY_SOURCE call when the buffer's size is known; the check is +// glibc's +extern void __chk_fail(void) __attribute__((__noreturn__)); +ssize_t __recvfrom_chk(int sockfd, void *buf, size_t len, size_t buflen, int flags, + struct sockaddr *src_addr, socklen_t *addrlen) { + if (len > buflen) __chk_fail(); + return recvfrom(sockfd, buf, len, flags, src_addr, addrlen); +} + +ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags) { + static ssize_t (*orig_recvmsg)(int, struct msghdr *, int) = NULL; + if (!orig_recvmsg) orig_recvmsg = (ssize_t (*)(int, struct msghdr *, int))dlsym(RTLD_NEXT, "recvmsg"); + + socklen_t cap = msg ? msg->msg_namelen : 0; + ssize_t ret = orig_recvmsg ? orig_recvmsg(sockfd, msg, flags) : -1; + if (ret >= 0) received_credentials_as_root(msg); + if (ret > 0 && msg && msg->msg_name) { + int saved_errno = errno; + restore_dns_source(sockfd, (struct sockaddr *)msg->msg_name, cap, &msg->msg_namelen); + errno = saved_errno; + } + return ret; +} + +int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { + static int (*orig_getpeername)(int, struct sockaddr *, socklen_t *) = NULL; + if (!orig_getpeername) orig_getpeername = (int (*)(int, struct sockaddr *, socklen_t *))dlsym(RTLD_NEXT, "getpeername"); + + socklen_t cap = addrlen ? *addrlen : 0; + int ret = orig_getpeername ? orig_getpeername(sockfd, addr, addrlen) : -1; + if (ret == 0 && addr && addrlen) restore_dns_source(sockfd, addr, cap, addrlen); + return ret; +} + +// accept() as accept4() without flags, the call bionic makes for both: Android's seccomp filter +// for apps is made from what bionic calls. On the x86_64 emulator a translated accept() answered +// with its system call number (43) as the new descriptor, and sshd failed every connection with +// "fcntl(43, F_GETFL): Bad file descriptor" (Perl, Python and nginx use accept4 themselves). +int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { + static int (*orig_accept4)(int, struct sockaddr *, socklen_t *, int) = NULL; + if (!orig_accept4) orig_accept4 = (int (*)(int, struct sockaddr *, socklen_t *, int))dlsym(RTLD_NEXT, "accept4"); + if (!orig_accept4) { + errno = ENOSYS; + return -1; + } + return orig_accept4(sockfd, addr, addrlen, 0); +} + +#ifndef AF_NETLINK +#define AF_NETLINK 16 +#endif +#ifndef NETLINK_AUDIT +#define NETLINK_AUDIT 9 +#endif + +int socket(int domain, int type, int protocol) { + static int (*orig_socket)(int, int, int) = NULL; + if (!orig_socket) orig_socket = (int (*)(int, int, int))dlsym(RTLD_NEXT, "socket"); + if (domain == AF_NETLINK && protocol == NETLINK_AUDIT) { + errno = EPROTONOSUPPORT; + return -1; + } + int fd = orig_socket ? orig_socket(domain, type, protocol) : -1; + // A new socket has not chosen a port, whatever the descriptor's last one had (close_range() and + // raw close system calls do not pass the hook's close()) + if (fd >= 0) set_chosen_port_fd(fd, 0); + return fd; +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/static_programs.c b/tools/nativeguest/hook/static_programs.c new file mode 100644 index 0000000000..406fca0da9 --- /dev/null +++ b/tools/nativeguest/hook/static_programs.c @@ -0,0 +1,345 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Statically linked programs: their seccomp-blocked system calls are patched before they run. + +// ---- Static programs ---------------------------------------------------------------------- +// A statically linked program (gcc -static) carries its own copy of glibc, whose system calls the +// preloaded hook never sees. The ones Android's seccomp filter answers with SIGSYS therefore kill it: +// set_robust_list at start-up, clone3 for every thread, rseq, and the others the hook's SIGSYS +// handler catches for dynamic programs. Before such a program runs, The native userland patches those call sites +// in its file to what that handler answers (0 for set_robust_list and the set*id family, -ENOSYS, +// which glibc falls back from, for the rest), as scripts/patch-bootstrap-ld.py does to the rootfs +// ld.so and libc at build time. The scanning is a pure function over a buffer, so tests/native can +// check it against the script. + +#define DDNATV_STATIC_WINDOW 16 // instructions between "mov x8, #nr" and its "svc #0" + +// AArch64: what the SIGSYS handler answers for syscall `nr`: 1 for 0, 2 for -ENOSYS, 0 to leave it. +// set_robust_list keeps the script's no-op (x0 then holds its non-error first argument). +static int a64_blocked_syscall(unsigned int nr) { + switch (nr) { + case 99: // set_robust_list + return 3; + case 100: // get_robust_list + case 143: case 144: case 145: case 146: case 147: case 149: // setregid setgid setreuid setuid setresuid setresgid + case 151: case 152: case 159: // setfsuid setfsgid setgroups + return 1; + case 97: case 217: case 218: case 219: // unshare add_key request_key keyctl + case 268: case 277: case 280: case 293: // setns seccomp bpf rseq + return 2; + default: + return (nr >= 424 && nr < 512) ? 2 : 0; // newer than Android's filter (clone3, close_range, ...) + } +} + +static uint32_t rd32(const unsigned char *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); } +static uint16_t rd16(const unsigned char *p) { return (uint16_t)(p[0] | (p[1] << 8)); } +static void wr32(unsigned char *p, uint32_t v) { p[0] = v & 0xff; p[1] = (v >> 8) & 0xff; p[2] = (v >> 16) & 0xff; p[3] = (v >> 24) & 0xff; } +static void wr16(unsigned char *p, uint16_t v) { p[0] = v & 0xff; p[1] = (v >> 8) & 0xff; } + +// An AArch64 instruction that ends the search for the "svc #0" of a "mov x8, #nr": a branch, another +// svc, or anything with x8 in its first register field (another write to x8, most likely). +static int a64_ends_syscall_window(uint32_t insn) { + if ((insn & 0x1f) == 8) return 1; + if ((insn & 0xffe0001f) == 0xd4000001u) return 1; // svc #imm + if ((insn & 0x7c000000u) == 0x14000000u) return 1; // b, bl + if ((insn & 0xff000010u) == 0x54000000u) return 1; // b.cond + if ((insn & 0x7e000000u) == 0x34000000u) return 1; // cbz, cbnz + if ((insn & 0x7e000000u) == 0x36000000u) return 1; // tbz, tbnz + if ((insn & 0xfe000000u) == 0xd6000000u) return 1; // br, blr, ret + return 0; +} + +static size_t a64_patch_seccomp_syscalls(unsigned char *data, size_t start, size_t end) { + size_t patched = 0; + start = (start + 3) & ~(size_t)3; + for (size_t pos = start; pos + 4 <= end; pos += 4) { + uint32_t insn = rd32(data + pos); + // movz x8, #nr / movz w8, #nr + if ((insn & 0x7fe0001fu) != 0x52800008u) continue; + int result = a64_blocked_syscall((insn >> 5) & 0xffff); + if (!result) continue; + for (size_t s = pos + 4, i = 0; s + 4 <= end && i < DDNATV_STATIC_WINDOW; s += 4, i++) { + uint32_t next = rd32(data + s); + if (next == 0xd4000001u) { // svc #0 + wr32(data + s, result == 3 ? 0xd503201fu /* nop */ + : result == 1 ? 0xd2800000u /* mov x0, #0 */ + : 0x928004a0u); /* mov x0, #-38 */ + patched++; + break; + } + if (a64_ends_syscall_window(next)) break; + } + } + return patched; +} + +// 32-bit ARM: the three calls glibc makes on its own, with the exact patterns of the script +// (ARM mode, Thumb-2 with r7, and Thumb-2 through __libc_do_syscall with r12). +static long thumb_bl_target(const unsigned char *data, size_t size, size_t off) { + if (off + 4 > size) return -1; + uint16_t hw1 = rd16(data + off), hw2 = rd16(data + off + 2); + if ((hw1 & 0xf800) != 0xf000 || (hw2 & 0xd000) != 0xd000) return -1; + long s = (hw1 >> 10) & 1; + long imm10 = hw1 & 0x3ff; + long j1 = (hw2 >> 13) & 1, j2 = (hw2 >> 11) & 1; + long imm11 = hw2 & 0x7ff; + long i1 = (j1 ^ s) ^ 1, i2 = (j2 ^ s) ^ 1; + long imm25 = (s << 24) | (i1 << 23) | (i2 << 22) | (imm10 << 12) | (imm11 << 1); + if (s) imm25 -= (1L << 25); + return (long)off + 4 + imm25; +} + +static size_t arm_patch_seccomp_syscalls(unsigned char *data, size_t start, size_t end) { + static const struct { uint32_t movw_r7; uint32_t repl; } arm[] = { + { 0xe3007152u, 0xe320f000u }, // movw r7, #338 (set_robust_list) -> nop + { 0xe30071b3u, 0xe3e00025u }, // movw r7, #435 (clone3) -> mvn r0, #37 + { 0xe300718eu, 0xe3e00025u }, // movw r7, #398 (rseq) -> mvn r0, #37 + }; + static const struct { uint16_t hw1, hw2; int nop_only; } thumb_r7[] = { + { 0xf240, 0x1752, 1 }, // movw r7, #338 + { 0xf240, 0x17b3, 0 }, // movw r7, #435 + { 0xf240, 0x178e, 0 }, // movw r7, #398 + }; + static const struct { uint16_t hw1, hw2; uint16_t repl1, repl2; } thumb_r12[] = { + { 0xf44f, 0x7ca9, 0xf04f, 0x0000 }, // mov.w r12, #338 -> mov.w r0, #0 + { 0xf44f, 0x7cc7, 0xf06f, 0x0025 }, // mov.w r12, #398 -> mvn.w r0, #37 + { 0xf240, 0x1cb3, 0xf06f, 0x0025 }, // movw r12, #435 -> mvn.w r0, #37 + }; + static const unsigned char do_syscall_stub[6] = { 0x80, 0xb5, 0x67, 0x46, 0x00, 0xdf }; + size_t patched = 0; + + size_t a4 = (start + 3) & ~(size_t)3, e4 = end & ~(size_t)3; + for (size_t pos = a4; pos + 4 < e4; pos += 4) { + uint32_t insn = rd32(data + pos); + for (size_t k = 0; k < sizeof(arm) / sizeof(arm[0]); k++) { + if (insn != arm[k].movw_r7) continue; + for (size_t s = pos + 4; s < e4 && s < pos + 68; s += 4) { + if (rd32(data + s) == 0xef000000u) { // svc #0 + wr32(data + s, arm[k].repl); + patched++; + break; + } + } + } + } + + size_t a2 = (start + 1) & ~(size_t)1, e2 = end & ~(size_t)1; + for (size_t t = a2; t + 4 < e2; t += 2) { + uint16_t hw1 = rd16(data + t), hw2 = rd16(data + t + 2); + int matched_r7 = 0; + for (size_t k = 0; k < sizeof(thumb_r7) / sizeof(thumb_r7[0]); k++) { + if (hw1 != thumb_r7[k].hw1 || hw2 != thumb_r7[k].hw2) continue; + matched_r7 = 1; + size_t limit = (e2 - 2 < t + 64 ? e2 - 2 : t + 64) + 2; + for (size_t s = t + 4; s < limit; s += 2) { + if (rd16(data + s) == 0xdf00) { // svc #0 + if (!thumb_r7[k].nop_only) { + wr16(data + t, 0xf06f); // mvn.w r0, #37 in place of the movw + wr16(data + t + 2, 0x0025); + } + wr16(data + s, 0xbf00); // nop + patched++; + break; + } + } + break; + } + if (matched_r7) continue; + for (size_t k = 0; k < sizeof(thumb_r12) / sizeof(thumb_r12[0]); k++) { + if (hw1 != thumb_r12[k].hw1 || hw2 != thumb_r12[k].hw2) continue; + size_t limit = (e2 - 4 < t + 48 ? e2 - 4 : t + 48) + 2; + for (size_t s = t + 4; s < limit; s += 2) { + long target = thumb_bl_target(data, end, s); + if (target >= (long)start && target + 6 <= (long)end && memcmp(data + target, do_syscall_stub, 6) == 0) { + wr16(data + s, thumb_r12[k].repl1); + wr16(data + s + 2, thumb_r12[k].repl2); + patched++; + break; + } + } + break; + } + } + return patched; +} + +#ifndef EM_ARM +#define EM_ARM 40 +#endif +#ifndef EM_AARCH64 +#define EM_AARCH64 183 +#endif + +// Patches the blocked syscalls in data[start, end), code of an ELF for `e_machine`; returns how many. +static size_t patch_seccomp_syscalls(unsigned char *data, size_t start, size_t end, uint16_t e_machine) { + if (!data || end <= start) return 0; + if (e_machine == EM_AARCH64) return a64_patch_seccomp_syscalls(data, start, end); + if (e_machine == EM_ARM) return arm_patch_seccomp_syscalls(data, start, end); + return 0; +} + +// Reads the program headers of an ELF file into `ph` (as 64-bit fields); returns their number, or -1. +struct ddnatv_phdr { uint32_t type, flags; uint64_t offset, filesz; }; +static int read_elf_phdrs(int fd, uint16_t *machine, struct ddnatv_phdr *ph, int max) { + unsigned char eh[64]; + if (pread(fd, eh, sizeof(eh), 0) < 52 || memcmp(eh, "\x7f" "ELF", 4) != 0 || eh[5] != 1) return -1; + int is64 = eh[4] == 2; + uint64_t phoff = is64 ? ((uint64_t)rd32(eh + 32) | ((uint64_t)rd32(eh + 36) << 32)) : rd32(eh + 28); + uint16_t phentsize = rd16(eh + (is64 ? 54 : 42)); + uint16_t phnum = rd16(eh + (is64 ? 56 : 44)); + *machine = rd16(eh + 18); + if (phentsize < (is64 ? 56 : 32) || phnum == 0 || phnum > max) return -1; + for (int i = 0; i < phnum; i++) { + unsigned char e[64]; + if (pread(fd, e, phentsize < sizeof(e) ? phentsize : sizeof(e), (off_t)(phoff + (uint64_t)i * phentsize)) < (is64 ? 56 : 32)) return -1; + ph[i].type = rd32(e); + if (is64) { + ph[i].flags = rd32(e + 4); + ph[i].offset = (uint64_t)rd32(e + 8) | ((uint64_t)rd32(e + 12) << 32); + ph[i].filesz = (uint64_t)rd32(e + 32) | ((uint64_t)rd32(e + 36) << 32); + } else { + ph[i].offset = rd32(e + 4); + ph[i].filesz = rd32(e + 16); + ph[i].flags = rd32(e + 24); + } + } + return phnum; +} + +// Whether the ELF has a Go build-ID note: Go programs make their own system calls and never these. +static int elf_is_go(int fd, const struct ddnatv_phdr *ph, int n) { + for (int i = 0; i < n; i++) { + if (ph[i].type != 4 /* PT_NOTE */ || ph[i].filesz < 16) continue; + unsigned char note[16]; + if (pread(fd, note, sizeof(note), (off_t)ph[i].offset) != sizeof(note)) continue; + if (rd32(note) == 4 && memcmp(note + 12, "Go\0\0", 4) == 0) return 1; + } + return 0; +} + +#define DDNATV_STATIC_XATTR "user.ddnatv.seccomp" +#define DDNATV_STATIC_MAX_CODE (256u * 1024 * 1024) + +#ifdef DDNATV_HAVE_GOSYS +static int prepare_go_program(int fd, const char *path, const struct stat *st); +static uint32_t go_blob_fingerprint(void); +static int go_disabled(void); +#endif + +// What was done to a file is recorded with its size and time, with the rootfs (a Go program carries +// the paths of it) and with the code Go programs are given (go_programs.c): a newer The native userland looks at +// the programs again. +static void static_marker(const struct stat *st, char *buf, size_t size) { + unsigned fingerprint = 0; + const char *off = ""; +#ifdef DDNATV_HAVE_GOSYS + fingerprint = go_blob_fingerprint(); + if (go_disabled()) off = " off"; +#endif + snprintf(buf, size, "3 %lld %lld.%09ld %08x %s%s", (long long)st->st_size, (long long)st->st_mtim.tv_sec, + (long)st->st_mtim.tv_nsec, fingerprint, g_ddnatv_root, off); +} + +// Makes a static program safe to start (see above). Runs before every start of one, so a program +// already handled (marked by an extended attribute that holds its size and modification time) costs +// one fstat and one getxattr; without extended attributes it is scanned again each time. Returns the +// number of patched calls, 0 when there was nothing to do, -1 when the file could not be handled +// (it is then started as it is). +// go_only: leave the file alone unless it is an AArch64 Go program. That is for a program that asks for +// Android's linker (GOOS=android): it is started like the dynamic ones, but it makes its own system +// calls, so it gets the code of the static Go programs (#42). +static int prepare_static_program_ex(const char *path, int go_only) { + int fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd < 0) return -1; + struct stat st; + char marker[PATH_MAX + 96], seen[PATH_MAX + 96]; + if (fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) { + close(fd); + return -1; + } + static_marker(&st, marker, sizeof(marker)); + ssize_t seen_len = fgetxattr(fd, DDNATV_STATIC_XATTR, seen, sizeof(seen) - 1); + if (seen_len > 0) { + seen[seen_len] = '\0'; + if (strcmp(seen, marker) == 0) { + close(fd); + return 0; + } + } + + struct ddnatv_phdr ph[64]; + uint16_t machine = 0; + int n = read_elf_phdrs(fd, &machine, ph, 64); + if (go_only && !(n >= 0 && machine == EM_AARCH64 && elf_is_go(fd, ph, n))) { + close(fd); + return 0; + } + if (n < 0 || (machine != EM_AARCH64 && machine != EM_ARM) || elf_is_go(fd, ph, n)) { + // A Go program has no C library for the hook to be loaded into: it gets code of its own + int go_rc = 0; +#ifdef DDNATV_HAVE_GOSYS + if (n >= 0 && machine == EM_AARCH64 && elf_is_go(fd, ph, n)) go_rc = prepare_go_program(fd, path, &st); +#endif + if (n >= 0 && go_rc >= 0) { + struct stat after; + if (fstat(fd, &after) == 0) static_marker(&after, marker, sizeof(marker)); + fsetxattr(fd, DDNATV_STATIC_XATTR, marker, strlen(marker), 0); + } + close(fd); + return n < 0 || go_rc < 0 ? -1 : 0; + } + + int total = 0; + int wfd = -1; + int made_writable = 0; + for (int i = 0; i < n; i++) { + if (ph[i].type != 1 /* PT_LOAD */ || !(ph[i].flags & 1 /* PF_X */) || ph[i].filesz == 0) continue; + if (ph[i].filesz > DDNATV_STATIC_MAX_CODE || ph[i].offset > (uint64_t)st.st_size || + ph[i].filesz > (uint64_t)st.st_size - ph[i].offset) continue; + size_t len = (size_t)ph[i].filesz; + unsigned char *code = (unsigned char *)malloc(len); + if (!code) continue; + if (pread(fd, code, len, (off_t)ph[i].offset) != (ssize_t)len) { + free(code); + continue; + } + size_t patched = patch_seccomp_syscalls(code, 0, len, machine); + if (patched > 0) { + if (wfd < 0) { + wfd = open(path, O_WRONLY | O_CLOEXEC); + if (wfd < 0 && errno == EACCES && fchmod(fd, (st.st_mode | S_IWUSR) & 07777) == 0) { + made_writable = 1; + wfd = open(path, O_WRONLY | O_CLOEXEC); + } + } + // ETXTBSY: the program is running right now, so it was handled when it started. + if (wfd < 0 || pwrite(wfd, code, len, (off_t)ph[i].offset) != (ssize_t)len) { + free(code); + total = -1; + break; + } + total += (int)patched; + } + free(code); + } + if (wfd >= 0) { + // Keep the times: make and friends must not see a rebuilt program. + const struct timespec times[2] = { st.st_atim, st.st_mtim }; + futimens(wfd, times); + close(wfd); + } + if (made_writable) fchmod(fd, st.st_mode & 07777); + if (total >= 0) { + struct stat after; + if (fstat(fd, &after) == 0) { + static_marker(&after, marker, sizeof(marker)); + fsetxattr(fd, DDNATV_STATIC_XATTR, marker, strlen(marker), 0); + } + } + close(fd); + return total; +} + +static int prepare_static_program(const char *path) { return prepare_static_program_ex(path, 0); } diff --git a/tools/nativeguest/hook/syscalls.c b/tools/nativeguest/hook/syscalls.c new file mode 100644 index 0000000000..06cacd8646 --- /dev/null +++ b/tools/nativeguest/hook/syscalls.c @@ -0,0 +1,128 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// The system calls that Android's seccomp filter answers with SIGSYS, and which of them the +// SIGSYS handler (signals.c) turns into a fake success. + +static int is_sigsys_synthetic_success_syscall(int sys_nr) { +#if defined(__NR_setuid) + if (sys_nr == __NR_setuid) return 1; +#endif +#if defined(__NR_setuid32) + if (sys_nr == __NR_setuid32) return 1; +#endif +#if defined(__NR_setgid) + if (sys_nr == __NR_setgid) return 1; +#endif +#if defined(__NR_setgid32) + if (sys_nr == __NR_setgid32) return 1; +#endif +#if defined(__NR_setreuid) + if (sys_nr == __NR_setreuid) return 1; +#endif +#if defined(__NR_setreuid32) + if (sys_nr == __NR_setreuid32) return 1; +#endif +#if defined(__NR_setregid) + if (sys_nr == __NR_setregid) return 1; +#endif +#if defined(__NR_setregid32) + if (sys_nr == __NR_setregid32) return 1; +#endif +#if defined(__NR_setresuid) + if (sys_nr == __NR_setresuid) return 1; +#endif +#if defined(__NR_setresuid32) + if (sys_nr == __NR_setresuid32) return 1; +#endif +#if defined(__NR_setresgid) + if (sys_nr == __NR_setresgid) return 1; +#endif +#if defined(__NR_setresgid32) + if (sys_nr == __NR_setresgid32) return 1; +#endif +#if defined(__NR_setfsuid) + if (sys_nr == __NR_setfsuid) return 1; +#endif +#if defined(__NR_setfsuid32) + if (sys_nr == __NR_setfsuid32) return 1; +#endif +#if defined(__NR_setfsgid) + if (sys_nr == __NR_setfsgid) return 1; +#endif +#if defined(__NR_setfsgid32) + if (sys_nr == __NR_setfsgid32) return 1; +#endif +#if defined(__NR_setgroups) + if (sys_nr == __NR_setgroups) return 1; +#endif +#if defined(__NR_setgroups32) + if (sys_nr == __NR_setgroups32) return 1; +#endif +#if defined(__NR_set_robust_list) + if (sys_nr == __NR_set_robust_list) return 1; +#endif +#if defined(__NR_get_robust_list) + if (sys_nr == __NR_get_robust_list) return 1; +#endif + return 0; +} + +static int is_handled_sigsys_syscall(int sys_nr) { + if (is_sigsys_synthetic_success_syscall(sys_nr)) { + return 1; + } + + // Unified Linux 5.1+ syscall numbers across architectures + if (sys_nr >= 424 && sys_nr < 512) { + return 1; + } + + // Architecture-specific syscall numbers guarded per target ABI +#if defined(__aarch64__) + if (sys_nr == 97 /* __NR_unshare */ || + sys_nr == 217 /* __NR_add_key */ || + sys_nr == 218 /* __NR_request_key */ || + sys_nr == 219 /* __NR_keyctl */ || + sys_nr == 268 /* __NR_setns */ || + sys_nr == 277 /* __NR_seccomp */ || + sys_nr == 280 /* __NR_bpf */ || + sys_nr == 293 /* __NR_rseq */) { + return 1; + } +#elif defined(__arm__) + if (sys_nr == 309 /* __NR_add_key */ || + sys_nr == 310 /* __NR_request_key */ || + sys_nr == 311 /* __NR_keyctl */ || + sys_nr == 337 /* __NR_unshare */ || + sys_nr == 375 /* __NR_setns */ || + sys_nr == 383 /* __NR_seccomp */ || + sys_nr == 386 /* __NR_bpf */ || + sys_nr == 398 /* __NR_rseq */) { + return 1; + } +#elif defined(__x86_64__) + if (sys_nr == 248 /* __NR_add_key */ || + sys_nr == 249 /* __NR_request_key */ || + sys_nr == 250 /* __NR_keyctl */ || + sys_nr == 272 /* __NR_unshare */ || + sys_nr == 308 /* __NR_setns */ || + sys_nr == 317 /* __NR_seccomp */ || + sys_nr == 321 /* __NR_bpf */ || + sys_nr == 334 /* __NR_rseq */) { + return 1; + } +#elif defined(__i386__) + if (sys_nr == 286 /* __NR_add_key */ || + sys_nr == 287 /* __NR_request_key */ || + sys_nr == 288 /* __NR_keyctl */ || + sys_nr == 310 /* __NR_unshare */ || + sys_nr == 346 /* __NR_setns */ || + sys_nr == 354 /* __NR_seccomp */ || + sys_nr == 357 /* __NR_bpf */ || + sys_nr == 386 /* __NR_rseq */) { + return 1; + } +#endif + return 0; +} diff --git a/tools/nativeguest/hook/sysv_shm.c b/tools/nativeguest/hook/sysv_shm.c new file mode 100644 index 0000000000..0b43e1fe2c --- /dev/null +++ b/tools/nativeguest/hook/sysv_shm.c @@ -0,0 +1,561 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// System V shared memory (shmget, shmat, shmdt, shmctl). Android's seccomp filter blocks these system +// calls for apps (the SIGSYS handler turns them into ENOSYS), but X uses them: a program draws an image +// into a segment and the X server reads it from there (the MIT-SHM extension), and Qt, GTK and SDL +// programs expect it. The segments are files of their own in /tmp/.ddnatv-sysv-shm of the rootfs, +// which every process of the app can open and map: +// +// id-N the segment's description: "PID KEY MODE SIZE" (who made it, its key, its rights, its size) +// seg-N its memory (a file of SIZE bytes, mapped MAP_SHARED) +// key-K for a segment with a key: the N it belongs to (K in hex) +// att-N-P process P has the segment attached (made by shmat(), removed by its last shmdt()) +// +// N is chosen at random and claimed with O_EXCL, so processes never pick the same one. IPC_RMID +// removes the files at once: programs that attached the segment keep their mapping (as on Linux), +// later attaches fail, which is what X programs expect (they remove a segment as soon as the X +// server has attached it). A segment nobody removed stays until the session's directory is cleared. + +#ifndef DDNATV_HOST_TEST + +#ifndef MAP_FIXED_NOREPLACE +#define MAP_FIXED_NOREPLACE 0x100000 +#endif +#ifndef SHM_REMAP +#define SHM_REMAP 040000 +#endif + +#define SHM_DIR "/tmp/.ddnatv-sysv-shm" +#define SHM_MAX_ATTACH 512 + +struct shm_meta { + int pid; + long key; + unsigned mode; + size_t size; +}; + +struct shm_attach { + void *addr; + size_t size; + int id; +}; + +static struct shm_attach g_shm_attach[SHM_MAX_ATTACH]; +static pthread_mutex_t g_shm_lock = PTHREAD_MUTEX_INITIALIZER; + +// root + /tmp/.ddnatv-sysv-shm[/name] +static int shm_path(char *out, size_t outsz, const char *name) { + int n = snprintf(out, outsz, "%s%s%s%s", g_ddnatv_root, SHM_DIR, name ? "/" : "", name ? name : ""); + return n > 0 && (size_t)n < outsz ? 0 : -1; +} + +static int shm_open_file(const char *name, int flags, unsigned mode) { + char path[PATH_MAX]; + if (shm_path(path, sizeof(path), name) != 0) return -1; + return (int)syscall(SYS_openat, AT_FDCWD, path, flags | O_CLOEXEC, mode); +} + +static void shm_unlink_file(const char *name) { + char path[PATH_MAX]; + if (shm_path(path, sizeof(path), name) == 0) syscall(SYS_unlinkat, AT_FDCWD, path, 0); +} + +static int shm_ensure_dir(void) { + char path[PATH_MAX]; + if (shm_path(path, sizeof(path), NULL) != 0) return -1; + if (syscall(SYS_mkdirat, AT_FDCWD, path, 0700) == 0 || errno == EEXIST) return 0; + // /tmp of the rootfs missing too (a rootfs being set up) + char tmp[PATH_MAX]; + snprintf(tmp, sizeof(tmp), "%s/tmp", g_ddnatv_root); + syscall(SYS_mkdirat, AT_FDCWD, tmp, 01777); + return (syscall(SYS_mkdirat, AT_FDCWD, path, 0700) == 0 || errno == EEXIST) ? 0 : -1; +} + +static int shm_read_small(const char *name, char *buf, size_t bufsz) { + int fd = shm_open_file(name, O_RDONLY, 0); + if (fd < 0) return -1; + ssize_t n = (ssize_t)syscall(SYS_read, fd, buf, bufsz - 1); + syscall(SYS_close, fd); + if (n < 0) return -1; + buf[n] = '\0'; + return (int)n; +} + +// The description of segment `id`; waits a moment for one being written. 0, or -1 (errno EINVAL). +static int shm_read_meta(int id, struct shm_meta *meta) { + char name[32], text[128]; + snprintf(name, sizeof(name), "id-%d", id); + for (int attempt = 0; attempt < 50; attempt++) { + int n = shm_read_small(name, text, sizeof(text)); + if (n < 0) break; + unsigned long size = 0; + if (n > 0 && sscanf(text, "%d %lx %o %lu", &meta->pid, &meta->key, &meta->mode, &size) == 4) { + meta->size = (size_t)size; + return 0; + } + struct timespec pause = { 0, 1000000 }; + nanosleep(&pause, NULL); + } + errno = EINVAL; + return -1; +} + +static int shm_process_alive(int pid) { + return pid > 0 && (kill(pid, 0) == 0 || errno == EPERM); +} + +static void shm_remove(int id, long key) { + char name[32]; + if (key != IPC_PRIVATE) { + // only when the key still names this segment + char text[32]; + snprintf(name, sizeof(name), "key-%lx", key); + if (shm_read_small(name, text, sizeof(text)) > 0 && atoi(text) == id) shm_unlink_file(name); + } + snprintf(name, sizeof(name), "seg-%d", id); + shm_unlink_file(name); + snprintf(name, sizeof(name), "id-%d", id); + shm_unlink_file(name); +} + +static int shm_mark_attached(int id, int attached) { + char name[48]; + snprintf(name, sizeof(name), "att-%d-%d", id, (int)getpid()); + if (!attached) { + shm_unlink_file(name); + return 0; + } + int fd = shm_open_file(name, O_WRONLY | O_CREAT, 0600); + if (fd < 0) return -1; + syscall(SYS_close, fd); + return 0; +} + +// The collector holds this exclusively while it scans and removes, shmat() shared while it checks a +// segment, maps it and marks itself, so that no segment is collected while it is being attached. +static int shm_lock_dir(int op) { + int fd = shm_open_file(".lock", O_RDWR | O_CREAT, 0600); + if (fd < 0) return -1; + long rc; + do { + rc = syscall(SYS_flock, fd, op); + } while (rc != 0 && errno == EINTR); + if (rc != 0) { + syscall(SYS_close, fd); + return -1; + } + return fd; +} + +static void shm_unlock_dir(int fd) { + if (fd >= 0) syscall(SYS_close, fd); +} + +// Deletes the marks of attached processes that have ended. Every segment stays until IPC_RMID, as on +// Linux: a private one's maker can hand its id to a process that attaches after the maker has ended. +static void shm_collect(void) { + char path[PATH_MAX]; + if (shm_path(path, sizeof(path), NULL) != 0) return; + int lock = shm_lock_dir(LOCK_EX); + if (lock < 0) return; + int dfd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if (dfd < 0) { + shm_unlock_dir(lock); + return; + } + char dents[4096]; + for (;;) { + long n = syscall(SYS_getdents64, dfd, dents, sizeof(dents)); + if (n <= 0) break; + for (long at = 0; at < n;) { + unsigned short reclen; + memcpy(&reclen, dents + at + 16, sizeof(reclen)); + if (reclen == 0) break; + const char *entry = dents + at + 19; + int id = 0, pid = 0; + if (strncmp(entry, "att-", 4) == 0 && sscanf(entry + 4, "%d-%d", &id, &pid) == 2 && !shm_process_alive(pid)) + shm_unlink_file(entry); + at += reclen; + } + } + syscall(SYS_close, dfd); + shm_unlock_dir(lock); +} + +static int shm_create(long key, size_t size, unsigned mode) { + // a different sequence of ids for every call, also within one second and one process + static unsigned calls = 0; + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + unsigned seed = (unsigned)getpid() * 2654435761u ^ (unsigned)now.tv_nsec ^ (unsigned)now.tv_sec * 40503u ^ + __atomic_add_fetch(&calls, 1, __ATOMIC_RELAXED) * 0x9e3779b9u; + for (int attempt = 0; attempt < 64; attempt++) { + seed = seed * 1103515245u + 12345u; + int id = (int)((seed >> 1) & 0x3fffffff); + if (id == 0) continue; + char name[32]; + snprintf(name, sizeof(name), "id-%d", id); + int meta_fd = shm_open_file(name, O_WRONLY | O_CREAT | O_EXCL, 0600); + if (meta_fd < 0) { + if (errno == EEXIST) continue; + return -1; + } + snprintf(name, sizeof(name), "seg-%d", id); + int seg_fd = shm_open_file(name, O_RDWR | O_CREAT | O_TRUNC, 0600); + long page = sysconf(_SC_PAGESIZE); + if (page <= 0) page = 4096; + if (size > (size_t)INTPTR_MAX - (size_t)page) { + // no file (and no mapping) can be that large + if (seg_fd >= 0) syscall(SYS_close, seg_fd); + syscall(SYS_close, meta_fd); + shm_remove(id, IPC_PRIVATE); + errno = EINVAL; + return -1; + } + size_t rounded = (size + (size_t)page - 1) / (size_t)page * (size_t)page; + if (seg_fd < 0 || ftruncate(seg_fd, (off_t)rounded) != 0) { + int saved = errno; + if (seg_fd >= 0) syscall(SYS_close, seg_fd); + syscall(SYS_close, meta_fd); + shm_remove(id, IPC_PRIVATE); + errno = saved == EFBIG ? EINVAL : (saved ? saved : ENOMEM); + return -1; + } + syscall(SYS_close, seg_fd); + char text[128]; + int len = snprintf(text, sizeof(text), "%d %lx %o %lu\n", getpid(), key, mode & 0777, (unsigned long)size); + ssize_t written = (ssize_t)syscall(SYS_write, meta_fd, text, (size_t)len); + syscall(SYS_close, meta_fd); + if (written != len) { + shm_remove(id, IPC_PRIVATE); + errno = ENOSPC; + return -1; + } + return id; + } + errno = ENOSPC; + return -1; +} + +// Writes `text` to the file `name` at once: into "new-ID" first, then renamed over it (`exclusive`: only +// when `name` does not exist yet, else -1 with EEXIST). Readers see the old file or the whole new one. +static int shm_publish(const char *name, int id, const char *text, size_t len, int exclusive) { + char tmp_name[32], tmp[PATH_MAX], path[PATH_MAX]; + snprintf(tmp_name, sizeof(tmp_name), "new-%d", id); + if (shm_path(tmp, sizeof(tmp), tmp_name) != 0 || shm_path(path, sizeof(path), name) != 0) { + errno = ENAMETOOLONG; + return -1; + } + int fd = (int)syscall(SYS_openat, AT_FDCWD, tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); + if (fd < 0) return -1; + ssize_t written = (ssize_t)syscall(SYS_write, fd, text, len); + syscall(SYS_close, fd); + if (written != (ssize_t)len) { + syscall(SYS_unlinkat, AT_FDCWD, tmp, 0); + errno = ENOSPC; + return -1; + } + long rc; + if (exclusive) { + rc = syscall(SYS_renameat2, AT_FDCWD, tmp, AT_FDCWD, path, 1 /* RENAME_NOREPLACE */); + if (rc != 0 && (errno == ENOSYS || errno == EINVAL)) { + // a kernel or file system without RENAME_NOREPLACE: claim the name, then write it + int kfd = (int)syscall(SYS_openat, AT_FDCWD, path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600); + syscall(SYS_unlinkat, AT_FDCWD, tmp, 0); + if (kfd < 0) return -1; + written = (ssize_t)syscall(SYS_write, kfd, text, len); + syscall(SYS_close, kfd); + if (written == (ssize_t)len) return 0; + syscall(SYS_unlinkat, AT_FDCWD, path, 0); + errno = ENOSPC; + return -1; + } + } else { + rc = syscall(SYS_renameat2, AT_FDCWD, tmp, AT_FDCWD, path, 0); +#ifdef SYS_renameat + if (rc != 0 && errno == ENOSYS) rc = syscall(SYS_renameat, AT_FDCWD, tmp, AT_FDCWD, path); +#endif + } + if (rc != 0) { + int saved = errno; + syscall(SYS_unlinkat, AT_FDCWD, tmp, 0); + errno = saved; + return -1; + } + return 0; +} + +int shmget(key_t key, size_t size, int shmflg) { + init_ddnatv_hook(); + if (g_ddnatv_root[0] == '\0') { + errno = ENOSYS; + return -1; + } + if (shm_ensure_dir() != 0) return -1; + if ((shmflg & IPC_CREAT) || key == IPC_PRIVATE) shm_collect(); + if (key == IPC_PRIVATE) { + if (size == 0) { + errno = EINVAL; + return -1; + } + return shm_create(IPC_PRIVATE, size, (unsigned)shmflg); + } + char key_name[32]; + snprintf(key_name, sizeof(key_name), "key-%lx", (long)key); + for (int attempt = 0; attempt < 8; attempt++) { + char text[32]; + if (shm_read_small(key_name, text, sizeof(text)) > 0) { + int id = atoi(text); + struct shm_meta meta; + if (id > 0 && shm_read_meta(id, &meta) == 0) { + if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) { + errno = EEXIST; + return -1; + } + if (size > meta.size) { + errno = EINVAL; + return -1; + } + return id; + } + shm_unlink_file(key_name); // names a segment that is gone + } + if (!(shmflg & IPC_CREAT)) { + errno = ENOENT; + return -1; + } + if (size == 0) { + errno = EINVAL; + return -1; + } + int id = shm_create((long)key, size, (unsigned)shmflg); + if (id < 0) return -1; + // The key file appears with its id in it, never empty: written under a name of its own, then + // renamed to the key's name unless another process made the key's segment meanwhile + int len = snprintf(text, sizeof(text), "%d\n", id); + int published = shm_publish(key_name, id, text, (size_t)len, 1); + if (published == 0) return id; + int saved = errno; + shm_remove(id, IPC_PRIVATE); + if (saved != EEXIST) { + errno = saved; + return -1; + } + // another process made the key's segment at the same time: use that one + } + errno = EAGAIN; + return -1; +} + +static void *shmat_locked(int shmid, const void *shmaddr, int shmflg); + +void *shmat(int shmid, const void *shmaddr, int shmflg) { + init_ddnatv_hook(); + // without the lock the collector could remove the segment mid-attach + int lock = g_ddnatv_root[0] != '\0' ? shm_lock_dir(LOCK_SH) : -1; + if (lock < 0) { + errno = EINVAL; + return (void *)-1; + } + void *addr = shmat_locked(shmid, shmaddr, shmflg); + int saved = errno; + shm_unlock_dir(lock); + errno = saved; + return addr; +} + +static int shm_attached_here_locked(int id); + +static void *shmat_locked(int shmid, const void *shmaddr, int shmflg) { + struct shm_meta meta; + if (g_ddnatv_root[0] == '\0' || shm_read_meta(shmid, &meta) != 0) { + errno = EINVAL; + return (void *)-1; + } + int readonly = (shmflg & SHM_RDONLY) != 0; + char name[32]; + snprintf(name, sizeof(name), "seg-%d", shmid); + int fd = shm_open_file(name, readonly ? O_RDONLY : O_RDWR, 0); + if (fd < 0) { + errno = EINVAL; + return (void *)-1; + } + void *want = (void *)shmaddr; + int flags = MAP_SHARED; + if (want) { + if (shmflg & SHM_RND) want = (void *)((uintptr_t)want & ~((uintptr_t)SHMLBA - 1)); + else if ((uintptr_t)want & ((uintptr_t)sysconf(_SC_PAGESIZE) - 1)) { + syscall(SYS_close, fd); + errno = EINVAL; + return (void *)-1; + } + // an address in use is replaced only with SHM_REMAP, as on Linux (MAP_FIXED_NOREPLACE: a kernel + // older than 4.17 takes it as a hint, which the check below catches) + flags |= (shmflg & SHM_REMAP) ? MAP_FIXED : MAP_FIXED_NOREPLACE; + } + // a place in the table first, so that every attach can be detached + int slot = -1; + pthread_mutex_lock(&g_shm_lock); + for (int i = 0; i < SHM_MAX_ATTACH; i++) { + if (!g_shm_attach[i].addr && !g_shm_attach[i].size) { + g_shm_attach[i].size = 1; // reserved + slot = i; + break; + } + } + pthread_mutex_unlock(&g_shm_lock); + if (slot < 0) { + syscall(SYS_close, fd); + errno = EMFILE; + return (void *)-1; + } + void *addr = mmap(want, meta.size, readonly ? PROT_READ : (PROT_READ | PROT_WRITE), flags, fd, 0); + int saved = errno; + syscall(SYS_close, fd); + if (addr != MAP_FAILED && want && addr != want) { + munmap(addr, meta.size); + addr = MAP_FAILED; + saved = EINVAL; + } + pthread_mutex_lock(&g_shm_lock); + // the mark keeps the segment from being collected; without one the attach is undone + if (addr != MAP_FAILED && shm_mark_attached(shmid, 1) != 0) { + saved = errno; + if (shm_attached_here_locked(shmid) == 0) shm_mark_attached(shmid, 0); + munmap(addr, meta.size); + addr = MAP_FAILED; + if (saved != ENOMEM) saved = EINVAL; + } + if (addr == MAP_FAILED) { + g_shm_attach[slot].size = 0; + } else { + g_shm_attach[slot].addr = addr; + g_shm_attach[slot].size = meta.size; + g_shm_attach[slot].id = shmid; + } + pthread_mutex_unlock(&g_shm_lock); + if (addr == MAP_FAILED) { + errno = saved == ENOMEM ? ENOMEM : EINVAL; + return (void *)-1; + } + return addr; +} + +int shmdt(const void *shmaddr) { + pthread_mutex_lock(&g_shm_lock); + for (int i = 0; i < SHM_MAX_ATTACH; i++) { + if (g_shm_attach[i].addr && g_shm_attach[i].addr == shmaddr) { + void *addr = g_shm_attach[i].addr; + size_t size = g_shm_attach[i].size; + int id = g_shm_attach[i].id; + g_shm_attach[i].addr = NULL; + g_shm_attach[i].size = 0; + // counted and unmarked under the lock, so that an attach of this process meanwhile keeps its mark + if (shm_attached_here_locked(id) == 0) shm_mark_attached(id, 0); + pthread_mutex_unlock(&g_shm_lock); + munmap(addr, size); + return 0; + } + } + pthread_mutex_unlock(&g_shm_lock); + errno = EINVAL; + return -1; +} + +static int shm_attached_here_locked(int id) { + int count = 0; + for (int i = 0; i < SHM_MAX_ATTACH; i++) { + if (g_shm_attach[i].addr && g_shm_attach[i].id == id) count++; + } + return count; +} + +static int shm_attached_here(int id) { + int count = 0; + pthread_mutex_lock(&g_shm_lock); + for (int i = 0; i < SHM_MAX_ATTACH; i++) { + if (g_shm_attach[i].addr && g_shm_attach[i].id == id) count++; + } + pthread_mutex_unlock(&g_shm_lock); + return count; +} + +int shmctl(int shmid, int cmd, struct shmid_ds *buf) { + init_ddnatv_hook(); + if (g_ddnatv_root[0] == '\0') { + errno = ENOSYS; + return -1; + } + switch (cmd) { + case IPC_INFO: + case SHM_INFO: + if (buf) { + // the caller's buffer is the structure of the command (struct shminfo or struct shm_info) + memset(buf, 0, cmd == IPC_INFO ? sizeof(struct shminfo) : sizeof(struct shm_info)); + if (cmd == IPC_INFO) { + struct shminfo *info = (struct shminfo *)buf; + info->shmmax = sizeof(unsigned long) > 4 ? (unsigned long)1 << 40 : 0x7fffffffUL; + info->shmmin = 1; + info->shmmni = 4096; + info->shmseg = 4096; + info->shmall = (unsigned long)1 << 30; + } + } + return 0; + case SHM_LOCK: + case SHM_UNLOCK: + return 0; + default: + break; + } + struct shm_meta meta; + if (shm_read_meta(shmid, &meta) != 0) { + errno = EINVAL; + return -1; + } + switch (cmd) { + case IPC_STAT: + case SHM_STAT: + case SHM_STAT_ANY: + if (!buf) { + errno = EFAULT; + return -1; + } + memset(buf, 0, sizeof(*buf)); + buf->shm_perm.__key = (key_t)meta.key; + // The owner's rights for everyone of the app: X compares the uid of its client with the owner's, + // and the two can differ in The native userland (root emulated for one, not the other) + unsigned owner = meta.mode & 0600; + buf->shm_perm.mode = (unsigned short)(owner | (owner >> 3) | (owner >> 6)); + buf->shm_perm.uid = buf->shm_perm.cuid = geteuid(); + buf->shm_perm.gid = buf->shm_perm.cgid = getegid(); + buf->shm_segsz = meta.size; + buf->shm_cpid = meta.pid; + buf->shm_lpid = getpid(); + buf->shm_nattch = (shmatt_t)(shm_attached_here(shmid) + (meta.pid != getpid() ? 1 : 0)); + buf->shm_ctime = time(NULL); + return cmd == IPC_STAT ? 0 : shmid; + case IPC_SET: { + if (!buf) { + errno = EFAULT; + return -1; + } + // the new rights are kept (owner and group stay the app's: there is no other user to give it to) + char name[32], text[128]; + snprintf(name, sizeof(name), "id-%d", shmid); + int len = snprintf(text, sizeof(text), "%d %lx %o %lu\n", meta.pid, meta.key, buf->shm_perm.mode & 0777u, + (unsigned long)meta.size); + return shm_publish(name, shmid, text, (size_t)len, 0) == 0 ? 0 : -1; + } + case IPC_RMID: + shm_remove(shmid, meta.key); + return 0; + default: + errno = EINVAL; + return -1; + } +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/seccomp-sim.c b/tools/nativeguest/seccomp-sim.c new file mode 100644 index 0000000000..e1b50a09c6 --- /dev/null +++ b/tools/nativeguest/seccomp-sim.c @@ -0,0 +1,92 @@ +// Runs a program under a seccomp filter that answers the system calls Android's app policy blocks +// with SIGSYS, the way a stock app process does, for testing on builds that have no filter: +// seccomp-sim [-a NR,NR...] PROGRAM [ARGS...] +// seccomp-sim -f FILTER PROGRAM [ARGS...] +// -a leaves the listed system calls allowed (to find which one kills a program). -f installs a +// filter as it is (raw struct sock_filter entries), such as the app filter a device's zygote makes. +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const int blocked[] = {99, 100, 97, 217, 218, 219, 268, 277, 280, 293, 143, 144, 145, 146, 147, 149, 151, 152, 159}; + +#define MAX_ALLOWED 64 +#define NBLOCKED (sizeof(blocked) / sizeof(blocked[0])) + +static int run_filter_file(const char *path, char **argv) { + static struct sock_filter filter[BPF_MAXINSNS]; + FILE *f = fopen(path, "rb"); + if (!f) { + perror("seccomp-sim: -f"); + return 2; + } + size_t n = fread(filter, sizeof(filter[0]), BPF_MAXINSNS, f); + int extra = fgetc(f) != EOF; + fclose(f); + if (n == 0 || extra) { + fprintf(stderr, "seccomp-sim: %s holds no filter of at most %d instructions\n", path, BPF_MAXINSNS); + return 2; + } + struct sock_fprog prog = { .len = (unsigned short)n, .filter = filter }; + if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) || prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) { + perror("seccomp-sim"); + return 1; + } + execv(argv[0], argv); + perror("seccomp-sim: exec"); + return 127; +} + +int main(int argc, char **argv) { + if (argc > 3 && strcmp(argv[1], "-f") == 0) return run_filter_file(argv[2], argv + 3); + int skip[MAX_ALLOWED], nskip = 0, arg = 1; + if (argc > 3 && strcmp(argv[1], "-a") == 0) { + for (char *p = argv[2];; p++) { + char *end = NULL; + long nr = strtol(p, &end, 10); + if (end == p || (*end != '\0' && *end != ',') || *p < '0' || *p > '9' || + nr < 0 || nr > 1023 || nskip == MAX_ALLOWED) { + fprintf(stderr, "seccomp-sim: -a takes up to %d comma-separated system call numbers, not \"%s\"\n", MAX_ALLOWED, argv[2]); + return 2; + } + skip[nskip++] = (int)nr; + if (*end == '\0') break; + p = end; + } + arg = 3; + } + if (arg >= argc) return 2; + struct sock_filter filter[4 + 2 * MAX_ALLOWED + 2 + 2 * NBLOCKED + 1]; + int n = 0; + filter[n++] = (struct sock_filter)BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, arch)); + filter[n++] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AUDIT_ARCH_AARCH64, 1, 0); + filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW); + filter[n++] = (struct sock_filter)BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)); + for (int i = 0; i < nskip; i++) { + filter[n++] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, skip[i], 0, 1); + filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW); + } + filter[n++] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, 424, 0, 1); + filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRAP); + for (size_t i = 0; i < NBLOCKED; i++) { + filter[n++] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, blocked[i], 0, 1); + filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRAP); + } + filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW); + struct sock_fprog prog = { .len = (unsigned short)n, .filter = filter }; + if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) || prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) { + perror("seccomp-sim"); + return 1; + } + execv(argv[arg], argv + arg); + perror("seccomp-sim: exec"); + return 127; +} diff --git a/tools/release/runtime_inputs.py b/tools/release/runtime_inputs.py index 5afaae5f61..57fe792465 100644 --- a/tools/release/runtime_inputs.py +++ b/tools/release/runtime_inputs.py @@ -77,7 +77,7 @@ def required(): paths += [f'assets/graphics_driver/adrenotools-{name}.tzst' for name in ['turnip26.3.0-a6xx', 'turnip26.3.0-quest2']] paths += ['assets/linuxfs/' + name for name in ['libfakeinput.so', 'libblsession.so', - 'libblfastpath.so', 'libssbs.so', 'usr/local/bin/droiddeck-clipboard', + 'libblfastpath.so', 'libddnatv-hook.so', 'libssbs.so', 'usr/local/bin/droiddeck-clipboard', 'usr/local/bin/droiddeck-gog-maintenance', 'usr/local/bin/gamescope', 'usr/local/lib/droiddeck/uruntime', 'usr/local/lib/droiddeck-pipewire.tzst']] diff --git a/tools/tests/test_droiddeckctl.py b/tools/tests/test_droiddeckctl.py index c0fb5742b8..c2fc95d30c 100644 --- a/tools/tests/test_droiddeckctl.py +++ b/tools/tests/test_droiddeckctl.py @@ -417,12 +417,14 @@ class DroiddeckctlTest(FakeAdbTest): return ctl.subprocess.CompletedProcess(args, 127, b"", b"/system/bin/sh: content: inaccessible or not found") if args[:2] == ("shell", "am"): return ctl.subprocess.CompletedProcess(args, 0, b"Broadcast completed: result=0", b"") + if args[:2] == ("shell", "pidof " + ctl.PACKAGE): + return ctl.subprocess.CompletedProcess(args, 0, b"4940\n", b"") return ctl.subprocess.CompletedProcess(args, 0, next(answers), b"") with unittest.mock.patch.object(ctl, "run_adb", adb), unittest.mock.patch.object(ctl.time, "sleep"): answer = ctl.provider_call("adb", "fake-1", "state", arg="a b", request={"x": 1}) self.assertEqual({"ok": True, "command": "state"}, answer) - broadcast = calls[1] + broadcast = next(c for c in calls if c[:2] == ("shell", "am")) self.assertEqual(("shell", "am", "broadcast", "-a", ctl.PACKAGE + ".AGENT_CALL", "-p", ctl.PACKAGE), broadcast[:7]) self.assertIn("'a b'", broadcast) self.assertEqual(ctl.encode_request({"x": 1}), broadcast[broadcast.index("request") + 1]) @@ -823,5 +825,48 @@ class ScenarioTest(FakeAdbTest): self.assertEqual([{"appId": 7, "timeout": 15.0}] * 2, [c["request"] for c in self.calls("quit")]) +class ProviderFallbackTest(unittest.TestCase): + MISSING = "/system/bin/sh: content: inaccessible or not found\n" + + def adb(self, answers): + calls = [] + + def run(adb, serial, *args, timeout=None): + calls.append(args) + for prefix, (code, out, err) in answers: + if " ".join(args).startswith(" ".join(prefix)): + return unittest.mock.Mock(returncode=code, stdout=out.encode(), stderr=err.encode()) + raise AssertionError(f"unexpected adb call {args}") + return calls, unittest.mock.patch.object(ctl, "run_adb", run) + + def test_a_command_that_prints_the_shell_error_is_not_run_again(self): + result = 'Result: Bundle[{json={"ok":true,"stdout":{"text":"' + self.MISSING.strip() + '"}}}]' + calls, patch = self.adb([(["shell", "content"], (0, result, ""))]) + with patch: + self.assertTrue(ctl.provider_call("adb", "s", "native", request={"argv": ["x"]})["ok"]) + self.assertEqual(1, len(calls)) + + def test_a_shell_without_content_uses_the_broadcast(self): + calls, patch = self.adb([ + (["shell", "content"], (127, "", self.MISSING)), + (["shell", "pidof"], (0, "4940\n", "")), + (["shell", "am", "broadcast"], (0, "Broadcast completed", "")), + (["shell"], (0, '{"ok": true}', "")), + ]) + with patch: + self.assertTrue(ctl.provider_call("adb", "s", "state")["ok"]) + self.assertEqual(["shell", "am", "broadcast"], list(calls[2][:3])) + + def test_the_broadcast_needs_the_app_running(self): + calls, patch = self.adb([ + (["shell", "content"], (127, "", self.MISSING)), + (["shell", "pidof"], (1, "", "")), + ]) + with patch, self.assertRaises(ctl.ControlError) as raised: + ctl.provider_call("adb", "s", "state") + self.assertEqual("APP_NOT_RUNNING", raised.exception.code) + self.assertFalse(any(c[:3] == ("shell", "am", "broadcast") for c in calls)) + + if __name__ == "__main__": unittest.main() diff --git a/tools/tests/test_preload_snapshots.py b/tools/tests/test_preload_snapshots.py index 6629d1ab22..2d0ee01642 100644 --- a/tools/tests/test_preload_snapshots.py +++ b/tools/tests/test_preload_snapshots.py @@ -150,6 +150,30 @@ class PreloadSnapshotTest(unittest.TestCase): with self.assertRaisesRegex(ValueError, 'successful main'): snapshots.artifact_bytes(self.data['origin'], self.root / 'cache') + def test_origin_apk_without_a_newer_output_reads_the_rest(self): + from zipfile import ZipFile + newest = 'libddnatv-hook.so' + apk = io.BytesIO() + with ZipFile(apk, 'w') as z: + for name, value in self.binaries.items(): + if name != newest: + z.writestr('assets/linuxfs/' + name, value) + outer = io.BytesIO() + with ZipFile(outer, 'w') as z: + z.writestr('app-release.apk', apk.getvalue()) + digest = snapshots.sha(outer.getvalue()) + origin = dict(self.data['origin'], artifact_sha256=digest) + build = dict(event='push', head_branch='main', conclusion='success', head_sha=origin['commit'], + path='.github/workflows/build.yml', repository=dict(full_name=snapshots.REPO)) + artifact = dict(name='droiddeck-apk', workflow_run=dict(id=origin['run']), digest='sha256:' + digest) + cache = self.root / 'cache' + cache.mkdir() + (cache / (digest + '.zip')).write_bytes(outer.getvalue()) + with patch.object(snapshots, 'api', side_effect=[build, artifact]): + content = snapshots.artifact_bytes(origin, cache) + self.assertNotIn(newest, content) + self.assertEqual(set(content), snapshots.FILES - {newest}) + def test_corrupt_archive_is_rejected(self): with self.assertRaisesRegex(ValueError, 'checksum'): snapshots.unpack(self.content + b'tampered', self.data)