From 57eee17522d9593d9b8241722b056ac0e8711865 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Wed, 18 Nov 2026 07:08:14 -0700 Subject: [PATCH] Image runs with every capability dropped; INBUXA_LISTENER_PORTS The binary carried cap_net_bind_service as a file capability, so exec failed outright wherever the capability isn't in the bounding set, as in a Kubernetes pod with every capability dropped. The images no longer set it (or install libcap to set it). Docker users are unaffected: Docker sets ip_unprivileged_port_start=0 in a container's own network namespace, so the server still listens on 25, 443, 465 and 993 unprivileged. Under podman or --network host, grant the sysctl or CAP_NET_BIND_SERVICE. INBUXA_LISTENER_PORTS moves the default listeners a new store gets, or leaves some out (smtp=2525,pop3s=off,...). It is checked on every start and a mistake stops the server, but only used when the store has no listeners yet; after that the registry holds them. A bind refused on a port below 1024 now says what would allow it. Closes #424 --- Dockerfile | 8 +- Dockerfile.build | 16 +- Dockerfile.fdb | 11 +- crates/common/src/manager/defaults.rs | 25 +-- crates/common/src/manager/listener_ports.rs | 165 ++++++++++++++++++++ crates/common/src/manager/mod.rs | 1 + crates/common/src/network/listen.rs | 13 +- 7 files changed, 219 insertions(+), 20 deletions(-) create mode 100644 crates/common/src/manager/listener_ports.rs diff --git a/Dockerfile b/Dockerfile index 1fb4edd8..8c752a8d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -58,7 +58,7 @@ RUN export DEBIAN_FRONTEND=noninteractive && \ FROM mirror.gcr.io/library/debian:trixie-slim RUN export DEBIAN_FRONTEND=noninteractive && \ apt-get update && \ - apt-get install -yq --no-install-recommends ca-certificates curl libcap2-bin && \ + apt-get install -yq --no-install-recommends ca-certificates curl && \ rm -rf /var/lib/apt/lists/* && \ groupadd -r -g 2000 inbuxa && \ useradd -r -u 2000 -g 2000 -s /usr/sbin/nologin -M inbuxa && \ @@ -68,7 +68,11 @@ COPY --from=builder --chmod=0755 /output/inbuxa /usr/local/bin/inbuxa COPY --from=migrate --chmod=0755 /migrate/inbuxa-migrate /usr/local/bin/inbuxa-migrate COPY --from=migrate /migrate/LICENSES /usr/share/doc/inbuxa-migrate/LICENSES COPY --from=migrate /migrate/README.md /usr/share/doc/inbuxa-migrate/README.md -RUN setcap 'cap_net_bind_service=+ep' /usr/local/bin/inbuxa +# inbuxa: K-S1: no file capability on the binary. One would make exec fail +# where every capability is dropped (Kubernetes' restricted pods). Docker +# lets an unprivileged user bind ports below 1024 on its own networks +# (net.ipv4.ip_unprivileged_port_start=0); elsewhere, grant that sysctl or +# CAP_NET_BIND_SERVICE, or move the listeners with INBUXA_LISTENER_PORTS. USER inbuxa WORKDIR /var/lib/inbuxa VOLUME ["/etc/inbuxa", "/var/lib/inbuxa"] diff --git a/Dockerfile.build b/Dockerfile.build index d9b38347..7bc22e28 100644 --- a/Dockerfile.build +++ b/Dockerfile.build @@ -168,14 +168,18 @@ COPY --from=builder /app/artifact / FROM --platform=$TARGETPLATFORM docker.io/library/debian:trixie-slim AS gnu RUN export DEBIAN_FRONTEND=noninteractive && \ apt-get update && \ - apt-get install -yq --no-install-recommends ca-certificates curl tzdata libcap2-bin && \ + apt-get install -yq --no-install-recommends ca-certificates curl tzdata && \ rm -rf /var/lib/apt/lists/* && \ groupadd -r -g 2000 inbuxa && \ useradd -r -u 2000 -g 2000 -s /usr/sbin/nologin -M inbuxa && \ mkdir -p /etc/inbuxa /var/lib/inbuxa && \ chown inbuxa:inbuxa /etc/inbuxa /var/lib/inbuxa COPY --from=builder --chmod=0755 /app/artifact/inbuxa /usr/local/bin/inbuxa -RUN setcap 'cap_net_bind_service=+ep' /usr/local/bin/inbuxa +# inbuxa: K-S1: no file capability on the binary. One would make exec fail +# where every capability is dropped (Kubernetes' restricted pods). Docker +# lets an unprivileged user bind ports below 1024 on its own networks +# (net.ipv4.ip_unprivileged_port_start=0); elsewhere, grant that sysctl or +# CAP_NET_BIND_SERVICE, or move the listeners with INBUXA_LISTENER_PORTS. USER inbuxa WORKDIR /var/lib/inbuxa VOLUME ["/etc/inbuxa", "/var/lib/inbuxa"] @@ -190,14 +194,18 @@ CMD ["--config", "/etc/inbuxa/config.json"] # Runtime image for musl targets # ***************** FROM --platform=$TARGETPLATFORM alpine AS musl -RUN apk add --update --no-cache ca-certificates curl tzdata libcap && \ +RUN apk add --update --no-cache ca-certificates curl tzdata && \ rm -rf /var/cache/apk/* && \ addgroup -S -g 2000 inbuxa && \ adduser -S -D -H -u 2000 -G inbuxa -s /sbin/nologin inbuxa && \ mkdir -p /etc/inbuxa /var/lib/inbuxa && \ chown inbuxa:inbuxa /etc/inbuxa /var/lib/inbuxa COPY --from=builder --chmod=0755 /app/artifact/inbuxa /usr/local/bin/inbuxa -RUN setcap 'cap_net_bind_service=+ep' /usr/local/bin/inbuxa +# inbuxa: K-S1: no file capability on the binary. One would make exec fail +# where every capability is dropped (Kubernetes' restricted pods). Docker +# lets an unprivileged user bind ports below 1024 on its own networks +# (net.ipv4.ip_unprivileged_port_start=0); elsewhere, grant that sysctl or +# CAP_NET_BIND_SERVICE, or move the listeners with INBUXA_LISTENER_PORTS. USER inbuxa WORKDIR /var/lib/inbuxa VOLUME ["/etc/inbuxa", "/var/lib/inbuxa"] diff --git a/Dockerfile.fdb b/Dockerfile.fdb index f7c309e6..135320c2 100644 --- a/Dockerfile.fdb +++ b/Dockerfile.fdb @@ -61,14 +61,17 @@ COPY --from=builder --chmod=0755 /app/target/release/inbuxa /usr/local/bin/inbux COPY --from=builder /usr/lib/libfdb_c.so /usr/lib/libfdb_c.so RUN export DEBIAN_FRONTEND=noninteractive && \ apt-get update && \ - apt-get install -yq --no-install-recommends ca-certificates curl libcap2-bin && \ + apt-get install -yq --no-install-recommends ca-certificates curl && \ rm -rf /var/lib/apt/lists/* && \ groupadd -r -g 2000 inbuxa && \ useradd -r -u 2000 -g 2000 -s /usr/sbin/nologin -M inbuxa && \ mkdir -p /etc/inbuxa /var/lib/inbuxa && \ - chown inbuxa:inbuxa /etc/inbuxa /var/lib/inbuxa && \ - setcap 'cap_net_bind_service=+ep' /usr/local/bin/inbuxa - + chown inbuxa:inbuxa /etc/inbuxa /var/lib/inbuxa +# inbuxa: K-S1: no file capability on the binary. One would make exec fail +# where every capability is dropped (Kubernetes' restricted pods). Docker +# lets an unprivileged user bind ports below 1024 on its own networks +# (net.ipv4.ip_unprivileged_port_start=0); elsewhere, grant that sysctl or +# CAP_NET_BIND_SERVICE, or move the listeners with INBUXA_LISTENER_PORTS. USER inbuxa WORKDIR /var/lib/inbuxa VOLUME ["/etc/inbuxa", "/var/lib/inbuxa"] diff --git a/crates/common/src/manager/defaults.rs b/crates/common/src/manager/defaults.rs index 24a1024a..20cafec6 100644 --- a/crates/common/src/manager/defaults.rs +++ b/crates/common/src/manager/defaults.rs @@ -512,21 +512,28 @@ async fn insert_safe_defaults(bp: &mut Bootstrap) -> trc::Result<()> { // inbuxa: personal-data catalog: the compliance roles, once per server super::compliance_roles::ensure_compliance_roles(&bp.registry, &bp.data_store).await?; + // inbuxa: K-S1: INBUXA_LISTENER_PORTS moves the default listeners, so a + // pod without the privilege to bind ports below 1024 can start. Checked on + // every start, so a mistake in it is found before it matters. Once the + // listeners exist it changes nothing: the registry holds them + let listener_ports = std::env::var(super::listener_ports::ENV_VAR).ok(); + let default_listeners = super::listener_ports::default_listeners(listener_ports.as_deref()) + .unwrap_or_else(|err| utils::failed(&err)); + if bp .registry .count_object(ObjectType::NetworkListener) .await? == 0 { - for (protocol, name, port, tls_implicit) in [ - (NetworkListenerProtocol::Smtp, "smtp", 25, false), - (NetworkListenerProtocol::Smtp, "submissions", 465, true), - (NetworkListenerProtocol::Imap, "imaps", 993, true), - (NetworkListenerProtocol::Pop3, "pop3s", 995, true), - (NetworkListenerProtocol::ManageSieve, "sieve", 4190, false), - (NetworkListenerProtocol::Http, "https", 443, true), - (NetworkListenerProtocol::Http, "http", 8080, false), - ] { + for (name, port, tls_implicit) in default_listeners { + let protocol = match name { + "smtp" | "submissions" => NetworkListenerProtocol::Smtp, + "imaps" => NetworkListenerProtocol::Imap, + "pop3s" => NetworkListenerProtocol::Pop3, + "sieve" => NetworkListenerProtocol::ManageSieve, + _ => NetworkListenerProtocol::Http, + }; bp.registry .write(RegistryWrite::insert( &NetworkListener { diff --git a/crates/common/src/manager/listener_ports.rs b/crates/common/src/manager/listener_ports.rs new file mode 100644 index 00000000..7e4b6e77 --- /dev/null +++ b/crates/common/src/manager/listener_ports.rs @@ -0,0 +1,165 @@ +/* + * SPDX-FileCopyrightText: 2026 Coffey Labs LLC + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +//! inbuxa: K-S1: the ports of the default listeners, from the environment. +//! +//! A new store gets one listener per protocol on its standard port (25, 465, +//! 993, ...). Ports below 1024 need a privilege that a Kubernetes pod with +//! every capability dropped doesn't have, so `INBUXA_LISTENER_PORTS` moves +//! them, or leaves some out: +//! +//! ```text +//! INBUXA_LISTENER_PORTS=smtp=2525,submissions=4465,imaps=9993,pop3s=off,https=8443 +//! ``` +//! +//! It is read only when those listeners are created, on a store that has no +//! listeners yet. After that the registry holds them and they are changed +//! there, like any other setting. + +/// The listeners a new store gets: name, port and whether TLS is implicit. +/// The names are what `INBUXA_LISTENER_PORTS` uses. +pub const DEFAULT_LISTENERS: [(&str, u16, bool); 7] = [ + ("smtp", 25, false), + ("submissions", 465, true), + ("imaps", 993, true), + ("pop3s", 995, true), + ("sieve", 4190, false), + ("https", 443, true), + ("http", 8080, false), +]; + +pub const ENV_VAR: &str = "INBUXA_LISTENER_PORTS"; + +/// The default listeners after the environment's changes: name, port and +/// whether TLS is implicit, in [`DEFAULT_LISTENERS`] order. A listener +/// turned off is left out. +pub fn default_listeners(spec: Option<&str>) -> Result, String> { + let mut listeners = DEFAULT_LISTENERS + .iter() + .map(|&(name, port, tls)| (name, Some(port), tls)) + .collect::>(); + let mut seen = Vec::new(); + + for item in spec + .unwrap_or_default() + .split(',') + .map(str::trim) + .filter(|item| !item.is_empty()) + { + let Some((name, port)) = item.split_once('=') else { + return Err(format!( + "{ENV_VAR}: '{item}' is not name=port (for example smtp=2525)" + )); + }; + let (name, port) = (name.trim(), port.trim()); + let Some(entry) = listeners.iter_mut().find(|entry| entry.0 == name) else { + return Err(format!( + "{ENV_VAR}: there is no default listener '{name}'; they are {}", + DEFAULT_LISTENERS.map(|entry| entry.0).join(", ") + )); + }; + if seen.contains(&name) { + return Err(format!("{ENV_VAR}: '{name}' is given twice")); + } + seen.push(entry.0); + + entry.1 = if port.eq_ignore_ascii_case("off") { + None + } else { + match port.parse::() { + Ok(port) if port > 0 => Some(port), + _ => { + return Err(format!( + "{ENV_VAR}: '{port}' is not a port for '{name}' (1 to 65535, or off)" + )); + } + } + }; + } + + let listeners = listeners + .into_iter() + .filter_map(|(name, port, tls)| port.map(|port| (name, port, tls))) + .collect::>(); + for (i, (name, port, _)) in listeners.iter().enumerate() { + if let Some((other, _, _)) = listeners[..i].iter().find(|entry| entry.1 == *port) { + return Err(format!( + "{ENV_VAR}: '{other}' and '{name}' would both listen on port {port}" + )); + } + } + + Ok(listeners) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn ports(spec: &str) -> Vec<(&'static str, u16)> { + default_listeners(Some(spec)) + .unwrap() + .into_iter() + .map(|(name, port, _)| (name, port)) + .collect() + } + + #[test] + fn unset_is_the_standard_ports() { + let standard = DEFAULT_LISTENERS + .map(|(name, port, _)| (name, port)) + .to_vec(); + assert_eq!( + default_listeners(None) + .unwrap() + .into_iter() + .map(|(name, port, _)| (name, port)) + .collect::>(), + standard + ); + assert_eq!(ports(""), standard); + assert_eq!(ports(" , "), standard); + } + + #[test] + fn moves_and_turns_off() { + assert_eq!( + ports("smtp=2525, submissions = 4465,imaps=9993,pop3s=OFF,https=8443"), + vec![ + ("smtp", 2525), + ("submissions", 4465), + ("imaps", 9993), + ("sieve", 4190), + ("https", 8443), + ("http", 8080), + ] + ); + // TLS stays with the listener, not the position + let listeners = default_listeners(Some("smtp=off")).unwrap(); + assert_eq!(listeners[0], ("submissions", 465, true)); + } + + #[test] + fn refuses_what_it_cannot_mean() { + for (spec, said) in [ + ("smtp", "not name=port"), + ("imap=143", "no default listener 'imap'"), + ("smtp=0", "not a port"), + ("smtp=70000", "not a port"), + ("smtp=twentyfive", "not a port"), + ("smtp=2525,smtp=2526", "given twice"), + ( + "https=8080", + "'https' and 'http' would both listen on port 8080", + ), + ] { + let err = default_listeners(Some(spec)).unwrap_err(); + assert!(err.contains(said), "{spec}: {err}"); + } + // A port freed by turning a listener off can be taken + assert!(default_listeners(Some("http=off,https=8080")).is_ok()); + } +} diff --git a/crates/common/src/manager/mod.rs b/crates/common/src/manager/mod.rs index 6c125a9b..73d50e41 100644 --- a/crates/common/src/manager/mod.rs +++ b/crates/common/src/manager/mod.rs @@ -24,6 +24,7 @@ pub mod console; pub mod defaults; pub mod first_party; pub mod granted_permissions; // inbuxa: permissions added after roles were stored +pub mod listener_ports; // inbuxa: K-S1, default listener ports from the environment pub mod restore; pub mod spam_rules; // inbuxa: rules bundled with the server #[cfg(target_os = "linux")] diff --git a/crates/common/src/network/listen.rs b/crates/common/src/network/listen.rs index 68ba4338..513788be 100644 --- a/crates/common/src/network/listen.rs +++ b/crates/common/src/network/listen.rs @@ -339,9 +339,20 @@ impl Listeners { for server in &self.servers { for listener in &server.listeners { if let Err(err) = listener.socket.bind(listener.addr) { + // inbuxa: K-S1: the image no longer carries the capability + // to bind low ports, so say what grants it + let hint = if err.kind() == std::io::ErrorKind::PermissionDenied + && listener.addr.port() < 1024 + { + " (ports below 1024 need CAP_NET_BIND_SERVICE, or the sysctl \ + net.ipv4.ip_unprivileged_port_start=0; or move the listener to \ + a higher port)" + } else { + "" + }; bp.build_error( server.registry_id, - format!("Failed to bind to {}: {}", listener.addr, err), + format!("Failed to bind to {}: {}{hint}", listener.addr, err), ); } } -- 2.54.0