use super::LinuxJail;
use crate::jail::{Jail, JailConfig};
use crate::sys_resource::{ManagedResource, SystemResource};
use anyhow::{Context, Result};
use std::process::{Command, ExitStatus};
use tracing::{debug, info, warn};
struct DockerNetwork {
network_name: String,
}
impl DockerNetwork {
const NETWORK_PREFIX: &'static str = "httpjail_";
fn network_name_from_jail_id(jail_id: &str) -> String {
format!("{}{}", Self::NETWORK_PREFIX, jail_id)
}
fn is_not_found_error(stderr: &str) -> bool {
stderr.contains("not found")
|| stderr.contains("No such")
|| stderr.contains("does not exist")
}
fn is_already_exists_error(stderr: &str) -> bool {
stderr.contains("already exists")
}
}
struct DockerRoutingTable {
#[allow(dead_code)]
jail_id: String,
table_name: String,
}
impl DockerRoutingTable {
fn table_name_from_jail_id(jail_id: &str) -> String {
format!("httpjail_docker_{}", jail_id)
}
}
impl SystemResource for DockerRoutingTable {
fn create(jail_id: &str) -> Result<Self> {
Ok(Self {
jail_id: jail_id.to_string(),
table_name: Self::table_name_from_jail_id(jail_id),
})
}
fn cleanup(&mut self) -> Result<()> {
debug!("Cleaning up Docker routing table: {}", self.table_name);
for family in ["inet", "ip"] {
let output = Command::new("/usr/sbin/nft")
.args(["delete", "table", family, &self.table_name])
.output()
.context("Failed to delete Docker routing table")?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
if !DockerNetwork::is_not_found_error(&stderr) {
anyhow::bail!("Failed to delete Docker routing table: {}", stderr);
}
} else {
info!(
"Removed Docker {} routing table {}",
family, self.table_name
);
}
}
Ok(())
}
fn for_existing(jail_id: &str) -> Self {
Self {
jail_id: jail_id.to_string(),
table_name: Self::table_name_from_jail_id(jail_id),
}
}
}
fn local_docker_command() -> Command {
let mut cmd = Command::new("/usr/bin/docker");
cmd.env_remove("DOCKER_HOST")
.env_remove("DOCKER_CONTEXT")
.env_remove("DOCKER_CONFIG")
.arg("--host=unix:///var/run/docker.sock");
cmd
}
impl SystemResource for DockerNetwork {
fn create(jail_id: &str) -> Result<Self> {
let network_name = Self::network_name_from_jail_id(jail_id);
let subnet = Self::compute_docker_subnet(jail_id);
let output = local_docker_command()
.args([
"network",
"create",
"--driver",
"bridge",
"--subnet",
&subnet,
"--opt",
"com.docker.network.bridge.enable_ip_masquerade=false",
&network_name,
])
.output()
.context("Failed to create Docker network")?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
if Self::is_already_exists_error(&stderr) {
info!("Docker network {} already exists", network_name);
} else {
anyhow::bail!("Failed to create Docker network: {}", stderr);
}
} else {
info!(
"Created Docker network {} with subnet {}",
network_name, subnet
);
}
Ok(Self { network_name })
}
fn cleanup(&mut self) -> Result<()> {
debug!("Cleaning up Docker network: {}", self.network_name);
let output = local_docker_command()
.args(["network", "rm", &self.network_name])
.output()
.context("Failed to remove Docker network")?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
if Self::is_not_found_error(&stderr) {
debug!("Docker network {} already removed", self.network_name);
} else {
anyhow::bail!("Failed to remove Docker network: {}", stderr);
}
} else {
info!("Removed Docker network {}", self.network_name);
}
Ok(())
}
fn for_existing(jail_id: &str) -> Self {
Self {
network_name: Self::network_name_from_jail_id(jail_id),
}
}
}
impl DockerNetwork {
fn compute_docker_subnet(jail_id: &str) -> String {
use std::hash::{Hash, Hasher};
let mut hasher = std::collections::hash_map::DefaultHasher::new();
jail_id.hash(&mut hasher);
let h = hasher.finish();
let third_octet = ((h % 256) as u8).max(1); format!("172.20.{}.0/24", third_octet)
}
fn get_bridge_name(&self) -> Result<String> {
let output = local_docker_command()
.args(["network", "inspect", &self.network_name, "-f", "{{.Id}}"])
.output()
.context("Failed to inspect Docker network")?;
if !output.status.success() {
anyhow::bail!("Failed to get Docker network ID");
}
let network_id = String::from_utf8_lossy(&output.stdout)
.trim()
.chars()
.take(12)
.collect::<String>();
let bridge = format!("br-{}", network_id);
anyhow::ensure!(
std::path::Path::new("/sys/class/net")
.join(&bridge)
.exists(),
"Docker bridge {bridge} is not present on the local host"
);
Ok(bridge)
}
}
pub struct DockerLinux {
inner_jail: LinuxJail,
config: JailConfig,
docker_network: Option<ManagedResource<DockerNetwork>>,
docker_routing: Option<ManagedResource<DockerRoutingTable>>,
}
impl DockerLinux {
pub fn new(config: JailConfig) -> Result<Self> {
let inner_jail = LinuxJail::new(config.clone())?;
Ok(Self {
inner_jail,
config,
docker_network: None,
docker_routing: None,
})
}
fn container_name(&self) -> String {
format!("httpjail_{}_run", self.config.jail_id)
}
const SAFE_VALUE_FLAGS: &'static [&'static str] = &[
"-e",
"--env",
"--entrypoint",
"-w",
"--workdir",
"-u",
"--user",
"--pull",
"--memory",
"--cpus",
"--label",
];
const SAFE_BOOL_FLAGS: &'static [&'static str] = &[
"--rm",
"--read-only",
"--init",
"--no-healthcheck",
"-i",
"-t",
];
#[allow(clippy::collapsible_if)]
fn build_docker_command(
&self,
docker_args: &[String],
extra_env: &[(String, String)],
) -> Result<(Command, Option<tempfile::TempDir>)> {
let network_name = DockerNetwork::network_name_from_jail_id(&self.config.jail_id);
let image_idx = Self::find_image_index(docker_args)?;
let docker_opts = &docker_args[..image_idx];
let image = &docker_args[image_idx];
let user_command = &docker_args[image_idx + 1..];
let mut cmd = local_docker_command();
cmd.arg("run");
let host_ip =
super::format_ip(LinuxJail::compute_host_ip_for_jail_id(&self.config.jail_id));
let container_name = self.container_name();
cmd.args([
"--network",
&network_name,
"--dns",
&host_ip,
"--name",
&container_name,
"--cap-drop=NET_RAW",
"--rm",
]);
let mut ca_cert_path = None;
for (key, value) in extra_env {
if key == "SSL_CERT_DIR" {
continue; }
cmd.arg("-e").arg(format!("{}={}", key, value));
if key == "SSL_CERT_FILE" && ca_cert_path.is_none() {
ca_cert_path = Some(value.clone());
}
}
let mut cert_dir = None;
if let Some(cert_path) = ca_cert_path {
use std::io::Read;
use std::os::unix::fs::OpenOptionsExt;
let cert = std::fs::OpenOptions::new()
.read(true)
.custom_flags(libc::O_NOFOLLOW)
.open(&cert_path)
.context("Failed to open public CA certificate")?;
let metadata = cert.metadata()?;
anyhow::ensure!(
metadata.is_file() && metadata.len() < 1024 * 1024,
"Invalid public CA certificate"
);
let mut pem = String::new();
cert.take(1024 * 1024).read_to_string(&mut pem)?;
anyhow::ensure!(
pem.trim_start().starts_with("-----BEGIN CERTIFICATE-----")
&& pem.contains("-----END CERTIFICATE-----")
&& !pem.contains("PRIVATE KEY"),
"Invalid public CA certificate"
);
let dir = if unsafe { libc::geteuid() == 0 } {
let root_dir = std::path::Path::new("/var/lib/httpjail");
crate::jail::ensure_trusted_root_dir(root_dir)?;
tempfile::Builder::new()
.prefix("cert-")
.tempdir_in(root_dir)
} else {
tempfile::tempdir()
}
.context("Failed to create protected CA mount directory")?;
let snapshot = dir.path().join("ca-cert.pem");
std::fs::write(&snapshot, pem).context("Failed to write public CA snapshot")?;
cmd.arg("-v")
.arg(format!("{}:{}:ro", snapshot.display(), cert_path));
cert_dir = Some(dir);
cmd.env_remove("SSL_CERT_DIR");
}
for opt in docker_opts {
cmd.arg(opt);
}
cmd.arg(image);
for arg in user_command {
cmd.arg(arg);
}
Ok((cmd, cert_dir))
}
fn find_image_index(args: &[String]) -> Result<usize> {
let mut i = 0;
while let Some(arg) = args.get(i) {
if !arg.starts_with('-') {
anyhow::ensure!(!arg.is_empty(), "Docker image must not be empty");
return Ok(i);
}
if Self::SAFE_BOOL_FLAGS.contains(&arg.as_str()) {
i += 1;
continue;
}
let (flag, inline_value) = arg
.split_once('=')
.map_or((arg.as_str(), None), |(name, value)| (name, Some(value)));
anyhow::ensure!(
Self::SAFE_VALUE_FLAGS.contains(&flag),
"Docker option not permitted in an isolated jail: {}",
arg
);
if let Some(value) = inline_value {
anyhow::ensure!(
!value.is_empty(),
"Missing value for Docker option {}",
flag
);
i += 1;
} else {
let value = args.get(i + 1).context("Missing Docker option value")?;
anyhow::ensure!(
!value.starts_with('-'),
"Invalid value for Docker option {}",
flag
);
i += 2;
}
}
anyhow::bail!("Could not find Docker image in arguments")
}
fn setup_docker_routing(&mut self) -> Result<()> {
let docker_network = self
.docker_network
.as_ref()
.context("Docker network not created")?;
if let Some(network) = docker_network.inner() {
let bridge_name = network.get_bridge_name()?;
let host_ip = LinuxJail::compute_host_ip_for_jail_id(&self.config.jail_id);
let host_ip_str = super::format_ip(host_ip);
info!(
"Setting up routing from Docker bridge {} to jail at {}",
bridge_name, host_ip_str
);
let table_name = DockerRoutingTable::table_name_from_jail_id(&self.config.jail_id);
let nft_rules = format!(
r#"table ip {table_name} {{
chain prerouting {{
type nat hook prerouting priority -100;
iifname "{bridge_name}" tcp dport 80 dnat to {host_ip_str}:{http_port};
iifname "{bridge_name}" tcp dport 443 dnat to {host_ip_str}:{https_port};
}}
}}"#,
http_port = self.config.http_proxy_port,
https_port = self.config.https_proxy_port,
);
let guard = format!(
r#"table inet {table_name} {{
chain input {{
type filter hook input priority -5; policy accept;
iifname "{bridge_name}" ip saddr {docker_subnet} ip daddr {host_ip_str} tcp dport {{ {http_port}, {https_port} }} accept
iifname "{bridge_name}" ip saddr {docker_subnet} ip daddr {host_ip_str} udp dport 53 accept
iifname "{bridge_name}" drop
}}
chain forward {{
type filter hook forward priority -5; policy accept;
iifname "{bridge_name}" drop
}}
}}"#,
table_name = table_name,
bridge_name = bridge_name,
docker_subnet = DockerNetwork::compute_docker_subnet(&self.config.jail_id),
host_ip_str = host_ip_str,
http_port = self.config.http_proxy_port,
https_port = self.config.https_proxy_port,
);
let nft_rules = format!("{nft_rules}\n{guard}");
let mut nft_cmd = Command::new("/usr/sbin/nft");
nft_cmd.arg("-f").arg("-");
nft_cmd.stdin(std::process::Stdio::piped());
let mut child = nft_cmd.spawn().context("Failed to spawn nft command")?;
if let Some(mut stdin) = child.stdin.take() {
use std::io::Write;
stdin
.write_all(nft_rules.as_bytes())
.context("Failed to write nftables rules")?;
}
let status = child.wait().context("Failed to wait for nft command")?;
if !status.success() {
anyhow::bail!("Failed to apply nftables rules for Docker routing");
}
info!("Docker routing rules applied successfully");
self.docker_routing = Some(ManagedResource::<DockerRoutingTable>::create(
&self.config.jail_id,
)?);
}
Ok(())
}
}
impl Jail for DockerLinux {
fn setup(&mut self, proxy_port: u16) -> Result<()> {
self.inner_jail.setup(proxy_port)?;
self.docker_network = Some(ManagedResource::<DockerNetwork>::create(
&self.config.jail_id,
)?);
self.setup_docker_routing()?;
info!("DockerLinux jail setup complete with Docker network isolation");
Ok(())
}
fn execute(&self, command: &[String], extra_env: &[(String, String)]) -> Result<ExitStatus> {
info!("Executing Docker container in isolated network");
let (cmd, _cert_dir) = self.build_docker_command(command, extra_env)?;
debug!("Docker command: {:?}", cmd);
crate::jail::run_command(cmd, None).context("Failed to execute docker run command")
}
fn execute_with_timeout(
&self,
command: &[String],
extra_env: &[(String, String)],
timeout: std::time::Duration,
) -> Result<ExitStatus> {
let (cmd, _cert_dir) = self.build_docker_command(command, extra_env)?;
let status = crate::jail::run_command(cmd, Some(timeout))
.context("Failed to execute docker run command")?;
if status.code() == Some(124) {
let output = local_docker_command()
.args(["rm", "-f", &self.container_name()])
.output()
.context("Failed to stop timed-out Docker container")?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
anyhow::ensure!(
DockerNetwork::is_not_found_error(&stderr),
"Failed to stop timed-out Docker container: {}",
stderr
);
}
}
Ok(status)
}
fn cleanup(&self) -> Result<()> {
Self::cleanup_orphaned(&self.config.jail_id)
}
fn jail_id(&self) -> &str {
self.inner_jail.jail_id()
}
fn cleanup_orphaned(jail_id: &str) -> Result<()>
where
Self: Sized,
{
DockerNetwork::for_existing(jail_id).cleanup()?;
DockerRoutingTable::for_existing(jail_id).cleanup()?;
LinuxJail::cleanup_orphaned(jail_id)
}
}
impl Drop for DockerLinux {
fn drop(&mut self) {
if let Some(mut network) = self.docker_network.take() {
let result = network.inner_mut().map_or(Ok(()), SystemResource::cleanup);
if let Err(error) = result {
warn!(
"Retaining Docker bridge firewall after network cleanup failure: {}",
error
);
if let Some(guard) = self.docker_routing.take() {
std::mem::forget(guard);
}
}
}
}
}
impl Clone for DockerLinux {
fn clone(&self) -> Self {
Self {
inner_jail: self.inner_jail.clone(),
config: self.config.clone(),
docker_network: None,
docker_routing: None,
}
}
}
#[cfg(test)]
mod tests {
use super::DockerLinux;
#[test]
fn docker_mounts_only_public_ca_file() {
let dir = tempfile::tempdir().unwrap();
let cert = dir.path().join("ca-cert.pem");
std::fs::write(
&cert,
b"-----BEGIN CERTIFICATE-----\npublic\n-----END CERTIFICATE-----\n",
)
.unwrap();
std::fs::write(dir.path().join("ca-key.pem"), b"dummy private key").unwrap();
let cert_path = cert.to_string_lossy().to_string();
let parent = dir.path().to_string_lossy().to_string();
let jail = DockerLinux::new(crate::jail::JailConfig::new()).unwrap();
let (cmd, _cert_dir) = jail
.build_docker_command(
&["alpine:latest".to_string()],
&[
("SSL_CERT_FILE".to_string(), cert_path.clone()),
("SSL_CERT_DIR".to_string(), parent.clone()),
],
)
.unwrap();
let args = cmd
.get_args()
.map(|arg| arg.to_string_lossy().into_owned())
.collect::<Vec<_>>();
let mount = args
.iter()
.find(|arg| arg.ends_with(&format!(":{cert_path}:ro")))
.unwrap();
let source = mount.strip_suffix(&format!(":{cert_path}:ro")).unwrap();
assert_ne!(source, cert_path);
assert!(
std::fs::read_to_string(source)
.unwrap()
.contains("BEGIN CERTIFICATE")
);
assert!(!args.contains(&format!("{parent}:{parent}:ro")));
assert!(!args.iter().any(|arg| arg.starts_with("SSL_CERT_DIR=")));
std::fs::remove_file(&cert).unwrap();
std::os::unix::fs::symlink("ca-key.pem", &cert).unwrap();
assert!(
std::fs::read_to_string(source)
.unwrap()
.contains("BEGIN CERTIFICATE")
);
assert!(
jail.build_docker_command(
&["alpine:latest".to_string()],
&[("SSL_CERT_FILE".to_string(), cert_path)]
)
.is_err()
);
}
#[test]
fn docker_options_cannot_override_isolation() {
let args = |parts: &[&str]| parts.iter().map(|s| (*s).to_string()).collect::<Vec<_>>();
assert_eq!(
DockerLinux::find_image_index(&args(&[
"--rm",
"-e",
"FOO=bar",
"alpine",
"--net=other"
]))
.unwrap(),
3
);
for option in [
"--net=other",
"--network=host",
"--privileged",
"--dns=8.8.8.8",
"-v",
] {
assert!(
DockerLinux::find_image_index(&args(&[option, "alpine"])).is_err(),
"unsafe Docker option {option} was accepted"
);
}
}
}