Skip to content

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

fix(portforward): reserve the X11 display and audio host ports

ExTvb854da4575de
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 f9db368..08ba4b3 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
@@ -10,6 +10,7 @@ import android.content.Context
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.emptyPreferences
import androidx.datastore.preferences.core.stringSetPreferencesKey
+import com.excp.podroid.x11.X11Constants
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
@@ -86,6 +87,18 @@ class PortForwardRepository @Inject constructor(
) {
companion object {
private val KEY_PORT_FORWARDS = stringSetPreferencesKey("port_forwards")
+
+ /**
+ * Host ports owned by the implicit, loopback-bound X11 display/audio
+ * forwards (injected at launch, never persisted). A user rule on one of
+ * these would shadow the implicit rule and bind 0.0.0.0, exposing the
+ * no-auth X session + raw PCM to the whole LAN. They are rejected on add
+ * and filtered from reads here — the single chokepoint feeding the launch
+ * snapshot, the EngineHolder live-diff, and the Settings UI — so a stale
+ * rule persisted by an older build can never be surfaced or applied.
+ * SSH (9922) is intentionally LAN-reachable and is NOT reserved.
+ */
+ val RESERVED_HOST_PORTS = setOf(X11Constants.VNC_PORT, X11Constants.AUDIO_PORT)
}
val rules: Flow<List<PortForwardRule>> = context.dataStore.data
@@ -93,12 +106,14 @@ class PortForwardRepository @Inject constructor(
.map { prefs ->
prefs[KEY_PORT_FORWARDS]
?.mapNotNull { PortForwardRule.deserialize(it) }
+ ?.filter { it.hostPort !in RESERVED_HOST_PORTS }
?.sortedWith(compareBy({ it.hostPort }, { it.protocol }))
?: emptyList()
}
.distinctUntilChanged()
suspend fun addRule(rule: PortForwardRule) {
+ if (rule.hostPort in RESERVED_HOST_PORTS) return
context.dataStore.edit { prefs ->
val current = prefs[KEY_PORT_FORWARDS] ?: emptySet()
prefs[KEY_PORT_FORWARDS] = deduplicatePortForwards(current, rule)
@@ -119,6 +134,7 @@ class PortForwardRepository @Inject constructor(
.map { prefs ->
prefs[KEY_PORT_FORWARDS]
?.mapNotNull { PortForwardRule.deserialize(it) }
+ ?.filter { it.hostPort !in RESERVED_HOST_PORTS }
?: emptyList()
}.first()
}
diff --git a/app/src/main/java/com/excp/podroid/ui/screens/settings/SettingsScreen.kt b/app/src/main/java/com/excp/podroid/ui/screens/settings/SettingsScreen.kt
index f52d733..924185a 100644
--- a/app/src/main/java/com/excp/podroid/ui/screens/settings/SettingsScreen.kt
+++ b/app/src/main/java/com/excp/podroid/ui/screens/settings/SettingsScreen.kt
@@ -70,6 +70,7 @@ import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.excp.podroid.BuildConfig
import com.excp.podroid.R
+import com.excp.podroid.data.repository.PortForwardRepository
import com.excp.podroid.data.repository.PortForwardRule
import com.excp.podroid.engine.EngineSelection
import com.excp.podroid.engine.VmState
@@ -837,6 +838,10 @@ private fun AddPortForwardDialog(
error = invalidPortsMsg
return@TextButton
}
+ if (hp in PortForwardRepository.RESERVED_HOST_PORTS) {
+ error = context.getString(R.string.port_reserved, hp)
+ return@TextButton
+ }
val added = onAdd(hp, gp, protocol)
if (!added) error = context.getString(R.string.port_already_forwarded, hp, protocol.uppercase())
}) {
diff --git a/app/src/main/java/com/excp/podroid/ui/screens/settings/SettingsViewModel.kt b/app/src/main/java/com/excp/podroid/ui/screens/settings/SettingsViewModel.kt
index 05ed9f9..7d56a0f 100644
--- a/app/src/main/java/com/excp/podroid/ui/screens/settings/SettingsViewModel.kt
+++ b/app/src/main/java/com/excp/podroid/ui/screens/settings/SettingsViewModel.kt
@@ -217,6 +217,10 @@ class SettingsViewModel @Inject constructor(
// The caller uses the return value to show feedback instead of silently
// closing the dialog on a duplicate.
fun addPortForward(hostPort: Int, guestPort: Int, protocol: String = "tcp"): Boolean {
+ // Backstop the dialog's reserved-port check: these host ports back the
+ // implicit loopback-bound X11 forwards and must never be user-bound to
+ // 0.0.0.0 (the dialog rejects them with a dedicated message first).
+ if (hostPort in PortForwardRepository.RESERVED_HOST_PORTS) return false
val protos = if (protocol == "both") listOf("tcp", "udp") else listOf(protocol)
val existing = portForwardRules.value.toSet()
val toAdd = protos.filter { proto ->
diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml
index a10d352..8e9b947 100644
--- a/app/src/main/res/values-zh/strings.xml
+++ b/app/src/main/res/values-zh/strings.xml
@@ -101,6 +101,7 @@
<string name="both">两者</string>
<string name="enter_valid_ports">输入有效的端口号 (1-65535)</string>
<string name="port_already_forwarded">端口 %1$d (%2$s) 已转发</string>
+ <string name="port_reserved">端口 %1$d 已保留给应用内桌面</string>
<string name="reset">重置</string>
<string name="remove">删除</string>
<string name="port_forwards_count">端口转发 (%1$d)</string>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index fee5587..82ba0a4 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -101,6 +101,7 @@
<string name="both">Both</string>
<string name="enter_valid_ports">Enter valid port numbers (1–65535)</string>
<string name="port_already_forwarded">Port %1$d (%2$s) is already forwarded</string>
+ <string name="port_reserved">Port %1$d is reserved for the in-app desktop</string>
<string name="reset">Reset</string>
<string name="remove">Remove</string>
<string name="port_forwards_count">Port forwards (%1$d)</string>