Image runs with every capability dropped; INBUXA_LISTENER_PORTS #443

Merged
jcoffey-dev merged 1 commits from feat/no-file-caps into main 2026-11-18 15:55:44 +00:00
7 changed files with 219 additions and 20 deletions

No files matched your search

+6 -2
View File
@@ -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"]
+12 -4
View File
@@ -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"]
+7 -4
View File
@@ -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"]
+16 -9
View File
@@ -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 {
+165
View File
@@ -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<Vec<(&'static str, u16, bool)>, String> {
let mut listeners = DEFAULT_LISTENERS
.iter()
.map(|&(name, port, tls)| (name, Some(port), tls))
.collect::<Vec<_>>();
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::<u16>() {
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::<Vec<_>>();
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::<Vec<_>>(),
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());
}
}
+1
View File
@@ -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")]
+12 -1
View File
@@ -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),
);
}
}