Skip to content

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

fix(x11): bind implicit display/audio forwards to loopback; harden the guest bridge

ExTvf092f08b9beb
diff --git a/app/src/main/java/com/excp/podroid/data/repository/PortForwardRepository.kt b/app/src/main/java/com/excp/podroid/data/repository/PortForwardRepository.kt
index 210b838..f9db368 100644
--- a/app/src/main/java/com/excp/podroid/data/repository/PortForwardRepository.kt
+++ b/app/src/main/java/com/excp/podroid/data/repository/PortForwardRepository.kt
@@ -25,12 +25,20 @@ import javax.inject.Singleton
* @param hostPort Port on the Android device
* @param guestPort Port inside the VM
* @param protocol "tcp" or "udp"
+ * @param loopbackOnly Bind the host listener to 127.0.0.1 instead of 0.0.0.0.
+ * Set for the implicit VNC/audio forwards (the in-app viewer dials loopback),
+ * so an unauthenticated X session + raw PCM aren't exposed to the whole LAN.
+ * NOT serialized — implicit rules are never persisted; user-created rules keep
+ * the default (0.0.0.0) so they remain reachable from a PC.
*/
data class PortForwardRule(
val hostPort: Int,
val guestPort: Int,
val protocol: String = "tcp",
+ val loopbackOnly: Boolean = false,
) {
+ // serialize/deserialize intentionally omit loopbackOnly: persistence format
+ // is unchanged (sacred) and only user rules (loopbackOnly=false) persist.
fun serialize(): String = "$protocol:$hostPort:$guestPort"
companion object {
diff --git a/app/src/main/java/com/excp/podroid/engine/avf/VsockPortForwarder.kt b/app/src/main/java/com/excp/podroid/engine/avf/VsockPortForwarder.kt
index df19c3b..8a858b0 100644
--- a/app/src/main/java/com/excp/podroid/engine/avf/VsockPortForwarder.kt
+++ b/app/src/main/java/com/excp/podroid/engine/avf/VsockPortForwarder.kt
@@ -40,6 +40,9 @@ class VsockPortForwarder(
private val guestVsockPort: Int,
private val vm: Any,
private val scope: CoroutineScope,
+ // 127.0.0.1 for the implicit VNC/audio forwards (off the network);
+ // 0.0.0.0 for user rules so a PC on the LAN can reach them.
+ private val bindAddress: String = "0.0.0.0",
) : Forwarder {
companion object {
private const val TAG = "VsockPortForwarder"
@@ -73,9 +76,9 @@ class VsockPortForwarder(
@Volatile private var closed = false
override fun start() {
- val s = ServerSocket(hostPort, /* backlog */ 16, InetAddress.getByName("0.0.0.0"))
+ val s = ServerSocket(hostPort, /* backlog */ 16, InetAddress.getByName(bindAddress))
server = s
- Log.d(TAG, "listening on 0.0.0.0:$hostPort → vsock:$guestVsockPort")
+ Log.d(TAG, "listening on $bindAddress:$hostPort → vsock:$guestVsockPort")
acceptJob = scope.launch(Dispatchers.IO) {
while (!closed) {
val client = try { s.accept() } catch (_: SocketException) { break }
diff --git a/app/src/main/java/com/excp/podroid/engine/avf/VsockUdpForwarder.kt b/app/src/main/java/com/excp/podroid/engine/avf/VsockUdpForwarder.kt
index 67dd408..28f2f6c 100644
--- a/app/src/main/java/com/excp/podroid/engine/avf/VsockUdpForwarder.kt
+++ b/app/src/main/java/com/excp/podroid/engine/avf/VsockUdpForwarder.kt
@@ -41,6 +41,8 @@ class VsockUdpForwarder(
private val guestVsockPort: Int,
private val vm: Any,
private val scope: CoroutineScope,
+ // 127.0.0.1 for loopback-only rules; 0.0.0.0 for user rules (see VsockPortForwarder).
+ private val bindAddress: String = "0.0.0.0",
) : Forwarder {
companion object {
private const val TAG = "VsockUdpForwarder"
@@ -73,13 +75,13 @@ class VsockUdpForwarder(
val s = DatagramSocket(null)
s.reuseAddress = true
try {
- s.bind(InetSocketAddress(InetAddress.getByName("0.0.0.0"), hostPort))
+ s.bind(InetSocketAddress(InetAddress.getByName(bindAddress), hostPort))
} catch (e: Throwable) {
runCatching { s.close() }
throw e
}
socket = s
- Log.d(TAG, "listening udp 0.0.0.0:$hostPort → vsock:$guestVsockPort")
+ Log.d(TAG, "listening udp $bindAddress:$hostPort → vsock:$guestVsockPort")
recvJob = scope.launch(Dispatchers.IO + children) { receiveLoop(s) }
reaperJob = scope.launch(Dispatchers.IO + children) { reapLoop() }
}
diff --git a/app/src/main/java/com/excp/podroid/engine/hostbridge/HostRequestDispatcher.kt b/app/src/main/java/com/excp/podroid/engine/hostbridge/HostRequestDispatcher.kt
index c349aa9..36e937f 100644
--- a/app/src/main/java/com/excp/podroid/engine/hostbridge/HostRequestDispatcher.kt
+++ b/app/src/main/java/com/excp/podroid/engine/hostbridge/HostRequestDispatcher.kt
@@ -22,7 +22,11 @@ class HostRequestDispatcher(
private val validProtocols = setOf("tcp", "udp")
suspend fun handle(line: String): String {
- val parts = line.trim().split(" ")
+ // Split on runs of whitespace and drop empties so a double space (or a
+ // tab) between tokens doesn't shift the field count and fail an otherwise
+ // valid request.
+ val parts = line.trim().split(Regex("\\s+")).filter { it.isNotEmpty() }
+ if (parts.isEmpty()) return HostProtocol.err("bad request")
return try {
when (parts[0]) {
"NOTIFY" -> handleNotify(parts)
@@ -60,6 +64,10 @@ class HostRequestDispatcher(
val guest = p[2].toIntOrNull() ?: return HostProtocol.err("bad guest port")
val proto = p[3]
if (host !in 1..65535 || guest !in 1..65535) return HostProtocol.err("port out of range")
+ // An unprivileged Android app lacks CAP_NET_BIND_SERVICE, so a host port
+ // below 1024 can never bind — accepting it would persist a rule that
+ // fails to apply on every diff and every boot while the guest CLI saw OK.
+ if (host < 1024) return HostProtocol.err("host port must be >= 1024 (Android apps can't bind privileged ports)")
if (proto !in validProtocols) return HostProtocol.err("bad protocol")
addForward(PortForwardRule(host, guest, proto))
return HostProtocol.ok()
diff --git a/app/src/main/java/com/excp/podroid/ui/screens/x11/X11ViewModel.kt b/app/src/main/java/com/excp/podroid/ui/screens/x11/X11ViewModel.kt
index fe5a827..5b71adf 100644
--- a/app/src/main/java/com/excp/podroid/ui/screens/x11/X11ViewModel.kt
+++ b/app/src/main/java/com/excp/podroid/ui/screens/x11/X11ViewModel.kt
@@ -125,24 +125,29 @@ class X11ViewModel @Inject constructor(
audio.start(viewModelScope)
while (isActive) {
val upd = VncClient.readFramebufferUpdate(inp, scratch, fbW, zrle)
- upd.newSize?.let { ns ->
- if (ns.w != fbW || ns.h != fbH) {
- fbW = ns.w; fbH = ns.h
- val fresh = IntArray(fbW * fbH)
- // Clear damage in the same critical section that swaps the
- // framebuffer so a recomposition between resize and the next
- // full frame can't blit stale damage rects against the new size.
- synchronized(fbLock) { framebuffer = fresh; lastDamage = emptyList() }
- scratch = IntArray(fbW * fbH)
- _fbSize.value = ns
- cursor.value = android.graphics.Point(fbW / 2, fbH / 2)
- // Route through the serialized writer so this full-update
- // request can't byte-interleave with a concurrent input
- // write. Capture the just-resized dimensions explicitly.
- val rw = fbW; val rh = fbH
- submitRfb { VncClient.requestFramebufferUpdate(it, w = rw, h = rh, incremental = false) }
- return@let
- }
+ val ns = upd.newSize
+ if (ns != null && (ns.w != fbW || ns.h != fbH)) {
+ fbW = ns.w; fbH = ns.h
+ val fresh = IntArray(fbW * fbH)
+ // Clear damage in the same critical section that swaps the
+ // framebuffer so a recomposition between resize and the next
+ // full frame can't blit stale damage rects against the new size.
+ synchronized(fbLock) { framebuffer = fresh; lastDamage = emptyList() }
+ scratch = IntArray(fbW * fbH)
+ _fbSize.value = ns
+ cursor.value = android.graphics.Point(fbW / 2, fbH / 2)
+ // Route through the serialized writer so this full-update
+ // request can't byte-interleave with a concurrent input
+ // write. Capture the just-resized dimensions explicitly.
+ val rw = fbW; val rh = fbH
+ submitRfb { VncClient.requestFramebufferUpdate(it, w = rw, h = rh, incremental = false) }
+ // Skip the rest of this iteration: the old code used
+ // return@let here, which only exited the let lambda and
+ // then fell through to overwrite lastDamage with rects
+ // measured against the OLD geometry (the exact race the
+ // synchronized block above prevents) and fire a spurious
+ // incremental request.
+ continue
}
synchronized(fbLock) {
System.arraycopy(scratch, 0, framebuffer, 0, framebuffer.size)
diff --git a/app/src/main/java/com/excp/podroid/x11/AudioStreamer.kt b/app/src/main/java/com/excp/podroid/x11/AudioStreamer.kt
index ddeca83..68fdb7b 100644
--- a/app/src/main/java/com/excp/podroid/x11/AudioStreamer.kt
+++ b/app/src/main/java/com/excp/podroid/x11/AudioStreamer.kt
@@ -35,6 +35,10 @@ class AudioStreamer(private val host: String = "127.0.0.1") {
// shifted by 1–3 bytes — the audible result was the clicking the
// user reported in Firefox video playback.
private const val BUF_BYTES = 4096
+ // 16-bit stereo = 4 bytes/frame. Any partial-frame write desyncs L/R
+ // for the rest of the stream, so the write loop must never abandon a
+ // chunk mid-frame.
+ private const val FRAME_BYTES = 4
}
private var job: Job? = null
@@ -117,7 +121,20 @@ class AudioStreamer(private val host: String = "127.0.0.1") {
throw java.io.IOException("AudioTrack.write error $n")
}
n == 0 -> {
- if (++zeroRetries > 8) break // give up this chunk
+ if (++zeroRetries > 8) {
+ // Give up this chunk — but only at a frame
+ // boundary. Breaking mid-frame would shift
+ // every later sample by 1-3 bytes (L/R swap /
+ // clicking). If we're mid-frame, rebuild the
+ // track instead so the stream realigns cleanly.
+ if (off % FRAME_BYTES != 0) {
+ Log.v(TAG, "AudioTrack stalled mid-frame; rebuilding to keep alignment")
+ track = null
+ activeTrack.stop(); activeTrack.release()
+ throw java.io.IOException("AudioTrack stalled mid-frame")
+ }
+ break
+ }
delay(5)
}
else -> { off += n; zeroRetries = 0 }
diff --git a/build-rootfs/host-bridge/podroid-hostd.c b/build-rootfs/host-bridge/podroid-hostd.c
index f4e29e0..cdb5528 100644
--- a/build-rootfs/host-bridge/podroid-hostd.c
+++ b/build-rootfs/host-bridge/podroid-hostd.c
@@ -218,9 +218,15 @@ static int cli_notify(int argc, char **argv) {
char *b64title = title ? b64encode((const unsigned char *)title, strlen(title)) : NULL;
char *b64body = b64encode((const unsigned char *)body, strlen(body));
char req[8192];
- snprintf(req, sizeof(req), "NOTIFY %s %s %s %s",
+ int reqlen = snprintf(req, sizeof(req), "NOTIFY %s %s %s %s",
prio, id, b64title ? b64title : "-", b64body);
free(body); free(b64title); free(b64body);
+ /* A body over ~6 KB truncates the base64 mid-string, which the host decodes
+ * as a confusing "bad body". Report a clear error instead. */
+ if (reqlen < 0 || (size_t)reqlen >= sizeof(req)) {
+ fprintf(stderr, "podroid: notification body too long\n");
+ return 2;
+ }
char resp[8192];
if (cli_roundtrip(req, resp, sizeof(resp)) < 0) {
@@ -325,7 +331,11 @@ static int make_unix_listener(void) {
sa.sun_family = AF_UNIX;
strncpy(sa.sun_path, SOCK_PATH, sizeof(sa.sun_path) - 1);
if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) { close(fd); return -1; }
- chmod(SOCK_PATH, 0666);
+ /* 0660, not 0666: the socket exposes powerful verbs (POWER stop/restart,
+ * HEADLESS, NOTIFY, OPEN). The daemon and the podroid-* CLIs run as root in
+ * the guest, so owner/group rw is sufficient; world-writable let any guest
+ * UID (incl. processes in containers that bind-mount /run) drive them. */
+ chmod(SOCK_PATH, 0660);
if (listen(fd, 16) < 0) { close(fd); return -1; }
return fd;
}