A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.
fix(network): give the guest the device's own DNS resolvers
ExTvd0b6a7e429b0
diff --git a/CLAUDE.md b/CLAUDE.mdindex 4be4c52..1aba883 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -226,7 +226,7 @@ In `QemuEngine.buildCommand()`: `tcg,thread=multi`, larger `tb-size` for ≥2GB- **Boot detection** scans a rolling buffer, not per-read chunks (fast devices split markers).- **Bridge stderr is silenced** (`dup2(/dev/null, STDERR)`): it runs as a `TerminalSession` subprocess whose stderr IS the PTY. Never add `fprintf(stderr, ...)` to `podroid-bridge.c`.- **`forceUpdateSizeFromView` must not multiply by scaledDensity** - `TerminalView` passes the raw int textSize to `Paint`; mismatched math renders TUI apps in the wrong grid.-- **SLIRP has no ICMP** (`ping` fails in the guest) and its DNS forwarder is unreliable on Android (resolv.conf uses public DNS).+- **SLIRP's ICMP works here** - `ping 8.8.8.8` from the guest gets replies (libslirp proxies echo through an unprivileged ICMP socket, which Android grants app UIDs; the `ttl=255` on replies is SLIRP synthesizing them). A `ping: bad address` is therefore a DNS failure, not an ICMP one. SLIRP's DNS forwarder *is* unreliable on Android: `10.0.2.3` resolves upstream by reading the host's `/etc/resolv.conf`, which Android does not have, so the guest is given the device's own resolver plus public fallbacks instead.- **Privileged ports**: the app can't bind host ports < 1024 (no `CAP_NET_BIND_SERVICE`), so SSH is on 9922, not 22. Forward to a high host port.- **Reflection into the vendored Termux fork** (`mTermSession`, `mEmulator`, `mCurrentDecSetFlags`) is kept by `app/proguard-rules.pro`.- **Persistence is sacred**: DataStore keys and `filesDir` paths survive every release; renames need migration. Updates must install in place (same `applicationId` + signing key, monotonic `versionCode`).diff --git a/app/src/main/java/com/excp/podroid/engine/QemuEngine.kt b/app/src/main/java/com/excp/podroid/engine/QemuEngine.ktindex 7ec58a1..f6274cd 100644--- a/app/src/main/java/com/excp/podroid/engine/QemuEngine.kt+++ b/app/src/main/java/com/excp/podroid/engine/QemuEngine.kt@@ -574,6 +574,7 @@ class QemuEngine @Inject constructor(if (config.sshEnabled) append(" ssh=1")append(" podroid.x11.dpi=").append(config.x11Dpi)if (config.bandwidthMbps > 0) append(" podroid.bandwidth=").append(config.bandwidthMbps)+ if (config.dnsServers.isNotEmpty()) append(" podroid.dns=").append(config.dnsServers.joinToString(","))}args += "-append"; args += cmdline} else {diff --git a/app/src/main/java/com/excp/podroid/engine/VmEngine.kt b/app/src/main/java/com/excp/podroid/engine/VmEngine.ktindex fc286fc..fd089d0 100644--- a/app/src/main/java/com/excp/podroid/engine/VmEngine.kt+++ b/app/src/main/java/com/excp/podroid/engine/VmEngine.kt@@ -113,6 +113,7 @@ data class VmConfig(val cpus: Int = 1,val sshEnabled: Boolean = false,val androidIp: String = "unknown",+ val dnsServers: List<String> = emptyList(),val storageSizeGb: Int = 2,val storageAccessEnabled: Boolean = false,val qemuExtraArgs: String = "",diff --git a/app/src/main/java/com/excp/podroid/service/PodroidService.kt b/app/src/main/java/com/excp/podroid/service/PodroidService.ktindex fa37b3d..60e6b0c 100644--- a/app/src/main/java/com/excp/podroid/service/PodroidService.kt+++ b/app/src/main/java/com/excp/podroid/service/PodroidService.kt@@ -348,6 +348,7 @@ class PodroidService : Service() {cpus = settingsRepository.getVmCpusSnapshot(),sshEnabled = sshEnabled,androidIp = NetworkUtils.localIpv4(this@PodroidService),+ dnsServers = NetworkUtils.dnsServers(this@PodroidService),storageSizeGb = settingsRepository.getStorageSizeGbSnapshot(),storageAccessEnabled = settingsRepository.getStorageAccessEnabledSnapshot(),qemuExtraArgs = settingsRepository.getQemuExtraArgsSnapshot(),diff --git a/app/src/main/java/com/excp/podroid/util/NetworkUtils.kt b/app/src/main/java/com/excp/podroid/util/NetworkUtils.ktindex 9078d0c..13dd55d 100644--- a/app/src/main/java/com/excp/podroid/util/NetworkUtils.kt+++ b/app/src/main/java/com/excp/podroid/util/NetworkUtils.kt@@ -94,6 +94,52 @@ object NetworkUtils {addr.startsWith("172.") && addr.substringAfter('.').substringBefore('.').toIntOrNull()?.let { it in 16..31 } == true+ /**+ * The DNS resolvers the active network handed the device over DHCP/RA, most useful+ * first, capped and sanitized for injection into the guest's `/etc/resolv.conf`+ * (see [sanitizeDnsList]).+ *+ * IPv4-only here on purpose: musl's resolver in the Alpine guest reads a plain+ * `nameserver <addr>` line per line, and the guest network stack this feeds (see+ * `podroid-network`) is IPv4-only, so a literal we can't reach is worse than none.+ */+ fun dnsServers(context: Context): List<String> = try {+ val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager+ val servers = cm?.let { it.getLinkProperties(it.activeNetwork) }+ ?.dnsServers+ ?.filterIsInstance<Inet4Address>()+ ?.map { it.hostAddress }+ .orEmpty()+ sanitizeDnsList(servers)+ } catch (_: Exception) {+ emptyList()+ }++ /**+ * Pure validation/cleanup so [dnsServers]'s output is safe to write into a guest+ * config file: reject anything that isn't a plain dotted-quad IPv4 literal (kills+ * IPv6 literals, whitespace, and shell metacharacters in one check), drop loopback+ * and the unspecified address, de-duplicate, and cap at 2 - musl reads at most 3+ * `nameserver` lines and one slot is reserved for a public fallback the guest writes.+ */+ internal fun sanitizeDnsList(addresses: List<String?>): List<String> =+ addresses+ .mapNotNull { it?.trim() }+ .filter { it.isNotEmpty() }+ .filter { it.all { c -> c.isDigit() || c == '.' } }+ .filter { isDottedQuad(it) }+ .filterNot { it.startsWith("127.") || it == "0.0.0.0" }+ .distinct()+ .take(2)++ private fun isDottedQuad(addr: String): Boolean {+ val octets = addr.split(".")+ if (octets.size != 4) return false+ return octets.all { octet ->+ octet.isNotEmpty() && octet.toIntOrNull()?.let { it in 0..255 } == true+ }+ }+private val TRANSPORT_PREFERENCE = intArrayOf(NetworkCapabilities.TRANSPORT_WIFI,NetworkCapabilities.TRANSPORT_ETHERNET,diff --git a/app/src/test/java/com/excp/podroid/util/SanitizeDnsListTest.kt b/app/src/test/java/com/excp/podroid/util/SanitizeDnsListTest.ktnew file mode 100644index 0000000..dac6808--- /dev/null+++ b/app/src/test/java/com/excp/podroid/util/SanitizeDnsListTest.kt@@ -0,0 +1,83 @@+/*+ * Podroid - Rootless Podman for Android+ * Copyright (C) 2024-2026 Podroid contributors+ *+ * Validation rules for DNS resolver strings before they reach the guest's resolv.conf.+ */+package com.excp.podroid.util++import org.junit.Assert.assertEquals+import org.junit.Test++class SanitizeDnsListTest {++ @Test+ fun `empty input yields empty output`() {+ assertEquals(emptyList<String>(), NetworkUtils.sanitizeDnsList(emptyList()))+ }++ @Test+ fun `a typical two-resolver LAN case is preserved in order`() {+ assertEquals(+ listOf("192.168.1.1", "192.168.1.2"),+ NetworkUtils.sanitizeDnsList(listOf("192.168.1.1", "192.168.1.2")),+ )+ }++ @Test+ fun `an ipv6 literal is dropped`() {+ assertEquals(+ emptyList<String>(),+ NetworkUtils.sanitizeDnsList(listOf("2001:4860:4860::8888")),+ )+ }++ @Test+ fun `loopback is dropped`() {+ assertEquals(emptyList<String>(), NetworkUtils.sanitizeDnsList(listOf("127.0.0.1")))+ }++ @Test+ fun `the unspecified address is dropped`() {+ assertEquals(emptyList<String>(), NetworkUtils.sanitizeDnsList(listOf("0.0.0.0")))+ }++ @Test+ fun `a blank string is dropped`() {+ assertEquals(emptyList<String>(), NetworkUtils.sanitizeDnsList(listOf(" ")))+ }++ @Test+ fun `a string with shell metacharacters is dropped`() {+ assertEquals(+ emptyList<String>(),+ NetworkUtils.sanitizeDnsList(listOf("1.2.3.4 ; rm -rf /")),+ )+ }++ @Test+ fun `an out of range octet is dropped`() {+ assertEquals(emptyList<String>(), NetworkUtils.sanitizeDnsList(listOf("999.1.1.1")))+ }++ @Test+ fun `too few octets is dropped`() {+ assertEquals(emptyList<String>(), NetworkUtils.sanitizeDnsList(listOf("1.2.3")))+ }++ @Test+ fun `duplicates collapse with order preserved`() {+ assertEquals(+ listOf("8.8.8.8", "8.8.4.4"),+ NetworkUtils.sanitizeDnsList(listOf("8.8.8.8", "8.8.4.4", "8.8.8.8")),+ )+ }++ @Test+ fun `four valid entries are capped at two, keeping the first two`() {+ assertEquals(+ listOf("1.1.1.1", "1.0.0.1"),+ NetworkUtils.sanitizeDnsList(listOf("1.1.1.1", "1.0.0.1", "8.8.8.8", "8.8.4.4")),+ )+ }+}diff --git a/build-rootfs/files/etc/init.d/podroid-network b/build-rootfs/files/etc/init.d/podroid-networkindex b4ed01d..fbcdf0a 100644--- a/build-rootfs/files/etc/init.d/podroid-network+++ b/build-rootfs/files/etc/init.d/podroid-network@@ -51,8 +51,47 @@ start() {# is faster + matches every existing user's setup. Original behavior.ip addr add 10.0.2.15/24 dev "$NETIF" 2>/dev/nullip route add default via 10.0.2.2 dev "$NETIF" 2>/dev/null- printf 'nameserver 10.0.2.3\nnameserver 8.8.8.8\nnameserver 1.1.1.1\n' \- > /etc/resolv.conf+ # SLIRP's 10.0.2.3 DNS forwarder is dead on Android (it reads the+ # host's /etc/resolv.conf, which doesn't exist here). QemuEngine+ # passes the device's own resolvers via podroid.dns= when it has any;+ # re-validate rather than trust the cmdline, and use only the first+ # one that parses as a dotted quad. It replaces just the 10.0.2.3+ # slot - 8.8.8.8 and 1.1.1.1 stay in place either way.+ DNS_RAW=$(sed -n 's/.*podroid\.dns=\([0-9.,]*\).*/\1/p' /proc/cmdline 2>/dev/null)+ DEVICE_DNS=""+ if [ -n "$DNS_RAW" ]; then+ _ifs=$IFS+ IFS=,+ set -- $DNS_RAW+ IFS=$_ifs+ for _cand in "$@"; do+ _dots=$IFS+ IFS=.+ set -- $_cand+ IFS=$_dots+ if [ $# -eq 4 ]; then+ _ok=1+ for _o in "$1" "$2" "$3" "$4"; do+ case "$_o" in ''|*[!0-9]*) _ok=0 ;; esac+ if [ "$_ok" = 1 ] && [ "$_o" -gt 255 ] 2>/dev/null; then+ _ok=0+ fi+ done+ if [ "$_ok" = 1 ]; then+ DEVICE_DNS="$_cand"+ break+ fi+ fi+ done+ fi+ if [ -n "$DEVICE_DNS" ]; then+ printf 'nameserver %s\nnameserver 8.8.8.8\nnameserver 1.1.1.1\n' "$DEVICE_DNS" \+ > /etc/resolv.conf+ else+ printf 'nameserver 10.0.2.3\nnameserver 8.8.8.8\nnameserver 1.1.1.1\n' \+ > /etc/resolv.conf+ fi+ echo "DNS: ${DEVICE_DNS:-10.0.2.3} 8.8.8.8" > /dev/console# Mirror the AVF post-check: if the static assignment didn't stick# (wrong NIC, link not up), don't report success. Dependents that# `need` us would assume the link is up and then SSH/apk fail with no