Skip to content

A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.

fix: preserve both device DNS resolvers in QEMU guest

Commiemusclemommie1af330035812
diff --git a/CLAUDE.md b/CLAUDE.md
index 4520333..7053fa8 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'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.
+- **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 up to two distinct device IPv4 resolvers in order plus deduplicated public fallbacks (or the original `10.0.2.3`/public list when no valid device resolver is available).
- **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/build-rootfs/files/etc/init.d/podroid-network b/build-rootfs/files/etc/init.d/podroid-network
index fbcdf0a..ac592ad 100644
--- a/build-rootfs/files/etc/init.d/podroid-network
+++ b/build-rootfs/files/etc/init.d/podroid-network
@@ -1,6 +1,83 @@
#!/sbin/openrc-run
+# shellcheck shell=sh
+# shellcheck disable=SC2034
description="Podroid VM networking (10.0.2.15 via SLIRP)"
+# Validate only plain dotted-quad IPv4 literals before they reach resolv.conf.
+# Keep this separate from qemu_resolv_conf so the resolver ordering can be
+# regression-tested without starting OpenRC or touching the host's /etc.
+is_valid_ipv4() {
+ case "$1" in
+ ''|*[!0-9.]*|.*|*.|*..*) return 1 ;;
+ esac
+ _old_ifs=$IFS
+ IFS=.
+ # Intentional splitting on dots after the character allowlist above.
+ # shellcheck disable=SC2086
+ set -- $1
+ IFS=$_old_ifs
+ [ "$#" -eq 4 ] || return 1
+ for _octet in "$@"; do
+ case "$_octet" in ''|*[!0-9]*|0[0-9]*) return 1 ;; esac
+ [ "$_octet" -le 255 ] 2>/dev/null || return 1
+ done
+}
+
+# Print the QEMU resolver file. Device resolvers occupy up to two slots in
+# their original order; the remaining slots use the existing public fallbacks,
+# without duplicates. With no valid device resolver, retain the original
+# 10.0.2.3 / public fallback behavior.
+qemu_resolv_conf() {
+ _dns_raw=$1
+ DEVICE_DNS_1=""
+ DEVICE_DNS_2=""
+
+ # Reject the whole cmdline value if it contains anything outside the
+ # comma-separated dotted-quad grammar. This also prevents field splitting
+ # or pathname expansion from interpreting attacker-controlled characters.
+ case "$_dns_raw" in *[!0-9.,]*) _dns_raw="" ;; esac
+ if [ -n "$_dns_raw" ]; then
+ _old_ifs=$IFS
+ IFS=,
+ # The character allowlist makes this expansion safe and glob-free.
+ # shellcheck disable=SC2086
+ set -- $_dns_raw
+ IFS=$_old_ifs
+ for _cand in "$@"; do
+ is_valid_ipv4 "$_cand" || continue
+ case "$_cand" in 127.*|0.0.0.0) continue ;; esac
+ [ "$_cand" = "$DEVICE_DNS_1" ] && continue
+ [ "$_cand" = "$DEVICE_DNS_2" ] && continue
+ if [ -z "$DEVICE_DNS_1" ]; then
+ DEVICE_DNS_1=$_cand
+ elif [ -z "$DEVICE_DNS_2" ]; then
+ DEVICE_DNS_2=$_cand
+ fi
+ done
+ fi
+
+ if [ -z "$DEVICE_DNS_1" ]; then
+ printf 'nameserver 10.0.2.3\nnameserver 8.8.8.8\nnameserver 1.1.1.1\n'
+ return
+ fi
+
+ printf 'nameserver %s\n' "$DEVICE_DNS_1"
+ _resolver_count=1
+ if [ -n "$DEVICE_DNS_2" ]; then
+ printf 'nameserver %s\n' "$DEVICE_DNS_2"
+ _resolver_count=2
+ fi
+ if [ "$_resolver_count" -lt 3 ] &&
+ [ "$DEVICE_DNS_1" != "8.8.8.8" ] && [ "$DEVICE_DNS_2" != "8.8.8.8" ]; then
+ printf 'nameserver 8.8.8.8\n'
+ _resolver_count=$((_resolver_count + 1))
+ fi
+ if [ "$_resolver_count" -lt 3 ] &&
+ [ "$DEVICE_DNS_1" != "1.1.1.1" ] && [ "$DEVICE_DNS_2" != "1.1.1.1" ]; then
+ printf 'nameserver 1.1.1.1\n'
+ fi
+}
+
depend() {
need podroid-bootstrap
before podroid-ready dropbear
@@ -54,44 +131,13 @@ start() {
# 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.
+ # re-validate them and put up to two distinct entries first, followed
+ # by deduplicated public fallbacks. No valid device resolver retains
+ # the original 10.0.2.3 / public fallback list.
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
+ qemu_resolv_conf "$DNS_RAW" > /etc/resolv.conf
+ DNS_SERVERS=$(awk '$1 == "nameserver" { if (dns != "") dns = dns " "; dns = dns $2 } END { print dns }' /etc/resolv.conf)
+ echo "DNS: $DNS_SERVERS" > /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
diff --git a/build-rootfs/tests/test_podroid_network_dns.sh b/build-rootfs/tests/test_podroid_network_dns.sh
new file mode 100755
index 0000000..75d5ce4
--- /dev/null
+++ b/build-rootfs/tests/test_podroid_network_dns.sh
@@ -0,0 +1,75 @@
+#!/bin/sh
+# Regression tests for QEMU guest DNS resolver ordering.
+set -eu
+
+ROOT=$(cd "$(dirname "$0")/.." && pwd)
+# shellcheck disable=SC1091
+. "$ROOT/files/etc/init.d/podroid-network"
+
+assert_resolvers() {
+ actual=$(qemu_resolv_conf "$1")
+ if [ "$actual" != "$2" ]; then
+ echo "FAIL: DNS input: $1" >&2
+ printf 'expected:\n%s' "$2" >&2
+ printf 'actual:\n%s\n' "$actual" >&2
+ exit 1
+ fi
+}
+
+# Invalid dotted quads are rejected without discarding later valid entries.
+assert_resolvers '999.1.1.1,1.2.3,127.0.0.1,0.0.0.0,192.168.1.1' \
+ 'nameserver 192.168.1.1
+nameserver 8.8.8.8
+nameserver 1.1.1.1'
+
+# Trailing dots and leading-zero octets are not accepted as IPv4 literals.
+assert_resolvers '192.168.1.1.,10.0.0.1' \
+ 'nameserver 10.0.0.1
+nameserver 8.8.8.8
+nameserver 1.1.1.1'
+assert_resolvers '192.168.001.1,10.0.0.2' \
+ 'nameserver 10.0.0.2
+nameserver 8.8.8.8
+nameserver 1.1.1.1'
+
+# IPv6 and shell metacharacters are rejected without being evaluated.
+assert_resolvers '2001:db8::1,1.2.3.4;touch /tmp/podroid-dns' \
+ 'nameserver 10.0.2.3
+nameserver 8.8.8.8
+nameserver 1.1.1.1'
+
+# Valid entries retain order and duplicates do not consume a resolver slot.
+assert_resolvers '192.168.1.2,192.168.1.2,192.168.1.1' \
+ 'nameserver 192.168.1.2
+nameserver 192.168.1.1
+nameserver 8.8.8.8'
+
+# One device resolver is followed by both public fallbacks.
+assert_resolvers '192.168.1.1' \
+ 'nameserver 192.168.1.1
+nameserver 8.8.8.8
+nameserver 1.1.1.1'
+
+# Two device resolvers precede the first non-duplicate fallback.
+assert_resolvers '192.168.1.1,192.168.1.2' \
+ 'nameserver 192.168.1.1
+nameserver 192.168.1.2
+nameserver 8.8.8.8'
+
+# No device DNS preserves the original SLIRP/public fallback behavior.
+assert_resolvers '' \
+ 'nameserver 10.0.2.3
+nameserver 8.8.8.8
+nameserver 1.1.1.1'
+
+# Public fallback duplicates are removed and the result never exceeds musl's
+# three nameserver slots, even when more device values are supplied.
+assert_resolvers '8.8.8.8,8.8.8.8,1.1.1.1' \
+ 'nameserver 8.8.8.8
+nameserver 1.1.1.1'
+assert_resolvers '10.0.0.1,10.0.0.2,10.0.0.3,8.8.8.8' \
+ 'nameserver 10.0.0.1
+nameserver 10.0.0.2
+nameserver 8.8.8.8'
+
+echo "PASS"
diff --git a/docs/guide/how-it-works.html b/docs/guide/how-it-works.html
index aa064c8..db11f12 100644
--- a/docs/guide/how-it-works.html
+++ b/docs/guide/how-it-works.html
@@ -78,7 +78,7 @@
<p>OpenRC is PID 1 inside the guest. Several app-specific services on the squashfs handle bring-up; the three that drive the boot log are:</p>
<ul>
<li><strong>podroid-bootstrap:</strong> kernel modules, cgroup v2, ZRAM swap, devpts/shm/mqueue mounts, sysctl, hostname.</li>
- <li><strong>podroid-network:</strong> brings up eth0, assigns 10.0.2.15/24, sets the default route and /etc/resolv.conf (SLIRP provides NAT). DNS goes to the device's own resolver first (passed in via <code>podroid.dns=</code> on the kernel command line), then 8.8.8.8 and 1.1.1.1 - SLIRP's own 10.0.2.3 forwarder is dead on Android, which has no host /etc/resolv.conf for it to read.</li>
+ <li><strong>podroid-network:</strong> brings up eth0, assigns 10.0.2.15/24, sets the default route and /etc/resolv.conf (SLIRP provides NAT). DNS uses up to two distinct device resolvers in order (passed via <code>podroid.dns=</code>), then deduplicated 8.8.8.8 / 1.1.1.1 fallbacks - SLIRP's own 10.0.2.3 forwarder is dead on Android, which has no host /etc/resolv.conf for it to read.</li>
<li><strong>podroid-ready:</strong> emits the "Starting SSH...", "Almost ready...", and "Ready!" markers that the app tracks to update the notification and unlock the terminal.</li>
</ul>
diff --git a/docs/guide/networking.html b/docs/guide/networking.html
index 6ede0cd..fa479bd 100644
--- a/docs/guide/networking.html
+++ b/docs/guide/networking.html
@@ -66,7 +66,7 @@
</tbody>
</table>
- <p>IPv6 is disabled on the QEMU netdev (<code>ipv6=off</code>). Outbound TCP and UDP both work. The <code>podroid-network</code> OpenRC service brings up eth0 with these addresses at boot. Because SLIRP's own forwarder is dead, the guest's <code>/etc/resolv.conf</code> lists the device's own resolver first (passed to the guest via <code>podroid.dns=</code> on the kernel command line), then <code>8.8.8.8</code> and <code>1.1.1.1</code>. In airplane mode there is no device resolver, so the list falls back to <code>10.0.2.3</code>, <code>8.8.8.8</code>, <code>1.1.1.1</code>.</p>
+ <p>IPv6 is disabled on the QEMU netdev (<code>ipv6=off</code>). Outbound TCP and UDP both work. The <code>podroid-network</code> OpenRC service brings up eth0 with these addresses at boot. Because SLIRP's own forwarder is dead, the guest's <code>/etc/resolv.conf</code> lists up to two distinct device resolvers in their original order (passed via <code>podroid.dns=</code>), then deduplicated <code>8.8.8.8</code> / <code>1.1.1.1</code> fallbacks. In airplane mode, or when no valid device resolver is available, the list preserves the fallback <code>10.0.2.3</code>, <code>8.8.8.8</code>, <code>1.1.1.1</code>.</p>
<div class="callout note">
<span class="label">AVF backend</span>
diff --git a/docs/guide/packages.html b/docs/guide/packages.html
index a8a8609..43f321e 100644
--- a/docs/guide/packages.html
+++ b/docs/guide/packages.html
@@ -154,7 +154,7 @@
</tr>
<tr>
<td><code>podroid-network</code></td>
- <td>Brings up eth0 (10.0.2.15/24 on QEMU, DHCP on AVF), adds the default route, and writes <code>/etc/resolv.conf</code> with the device's own resolver first, then <code>8.8.8.8</code> / <code>1.1.1.1</code>.</td>
+ <td>Brings up eth0 (10.0.2.15/24 on QEMU, DHCP on AVF), adds the default route, and writes <code>/etc/resolv.conf</code> with up to two distinct device resolvers first, then deduplicated <code>8.8.8.8</code> / <code>1.1.1.1</code> fallbacks.</td>
</tr>
<tr>
<td><code>podroid-resize</code></td>