Skip to content

armgit / kiri

Public Imported from GitHub
0

A small, fast container runtime for macOS, Windows and Linux

Rust

Security: Build steps keep the Mac's DNS; tighter limits on the DNS relay (#12)

Precious Okwuc1da681b2cf1
diff --git a/crates/kiri-init/src/linux/container.rs b/crates/kiri-init/src/linux/container.rs
index cc93bd2..4ff58b6 100644
--- a/crates/kiri-init/src/linux/container.rs
+++ b/crates/kiri-init/src/linux/container.rs
@@ -258,7 +258,8 @@ pub fn spawn(spec: &RunSpec<'_>, paths: &Paths, net: NetCtx<'_>) -> io::Result<P
// Small, short-lived files: kept in memory (/run is tmpfs), not on the data disk.
let etc_dir = PathBuf::from("/run/kiri").join(spec.id);
let embedded_dns = embedded_dns(spec, net.upstream_dns, net.dns.relay);
- prepared.etc_files = write_etc_files(spec, &etc_dir, net.upstream_dns, embedded_dns).map_err(cleanup)?;
+ let nameserver = embedded_dns.then(|| embedded_address(spec, net.dns.relay));
+ prepared.etc_files = write_etc_files(spec, &etc_dir, net.upstream_dns, nameserver).map_err(cleanup)?;
prepared.mounts = parse_mounts(spec.mounts).map_err(cleanup)?;
let bridged = !spec.net.bridge.is_empty() && spec.flags & (run_flags::NET_HOST | run_flags::NET_NONE) == 0 && prepared.net_join.is_none();
let mut go = None;
@@ -323,10 +324,15 @@ pub fn spawn(spec: &RunSpec<'_>, paths: &Paths, net: NetCtx<'_>) -> io::Result<P
net.dns.serve(spec.net.bridge, spec.net.gateway).map_err(|e| kill_child(io::Error::new(e.kind(), format!("dns: {e}"))))?;
net.dns.register(spec.net.bridge, spec.net.dns_names.iter(), spec.net.ip);
}
- // 127.0.0.11 in the container, what its resolv.conf names.
- std::fs::File::open(format!("/proc/{pid}/ns/net"))
- .and_then(|ns| net.dns.serve_in(&ns, pid, spec.net.ip, spec.net.bridge))
- .map_err(|e| kill_child(io::Error::new(e.kind(), format!("dns: {e}"))))?;
+ // 127.0.0.11 in the container, when its resolv.conf names it. Not
+ // when it names the gateway (the default bridge in the VM): the
+ // listener would only hold port 53, and a DNS server in the
+ // container could not bind it.
+ if nameserver == Some(super::dns::EMBEDDED) {
+ std::fs::File::open(format!("/proc/{pid}/ns/net"))
+ .and_then(|ns| net.dns.serve_in(&ns, pid, spec.net.ip, spec.net.bridge))
+ .map_err(|e| kill_child(io::Error::new(e.kind(), format!("dns: {e}"))))?;
+ }
if let Some(v) = v6 {
net.dns.register6(spec.net.bridge, spec.net.dns_names.iter(), spec.net.ip, v.ip);
}
@@ -672,7 +678,7 @@ fn prepare(argv: StrList<'_>, env: StrList<'_>, cwd: &str, user: &str, hostname:
/// Writes the container's /etc/hosts, /etc/resolv.conf and /etc/hostname
/// beside its writable layer; the child binds them over the image's files.
-fn write_etc_files(spec: &RunSpec<'_>, base: &std::path::Path, upstream: &[[u8; 4]], embedded_dns: bool) -> io::Result<Vec<(CString, CString)>> {
+fn write_etc_files(spec: &RunSpec<'_>, base: &std::path::Path, upstream: &[[u8; 4]], embedded: Option<[u8; 4]>) -> io::Result<Vec<(CString, CString)>> {
fs::create_dir_all(base)?;
// `--network container:<id>` shares that container's files, as Docker does.
if let Some(target) = net_opts::values(spec.net.opts, net_opts::NETNS).next() {
@@ -719,7 +725,7 @@ fn write_etc_files(spec: &RunSpec<'_>, base: &std::path::Path, upstream: &[[u8;
}
hosts.push('\n');
}
- let resolv = resolv_conf(spec, upstream, embedded_dns);
+ let resolv = resolv_conf(spec, upstream, embedded);
let files = [("hosts", hosts), ("resolv.conf", resolv), ("hostname", format!("{}\n", spec.hostname))];
let mut out = Vec::with_capacity(files.len());
for (name, text) in files {
@@ -750,11 +756,23 @@ fn embedded_dns(spec: &RunSpec<'_>, upstream: &[[u8; 4]], relay: bool) -> bool {
bridged && net_opts::values(spec.net.opts, net_opts::NAMESERVER).next().is_none() && (relay || net::local_resolver(upstream))
}
-fn resolv_conf(spec: &RunSpec<'_>, upstream: &[[u8; 4]], embedded_dns: bool) -> String {
+/// The address a container that uses the agent's resolver names in its
+/// resolv.conf: 127.0.0.11 on a user network, as Docker's embedded DNS;
+/// on the default bridge in the VM, the bridge's gateway, where the agent
+/// answers too. Not a loopback address there, because what runs builds
+/// in a container takes loopback nameservers out of the copy it gives
+/// each step and puts Google's in their place (BuildKit does, for every
+/// step that isn't `--network=host`): those steps would ask 8.8.8.8, not
+/// the Mac's resolver, and VPN and company names would stop resolving.
+fn embedded_address(spec: &RunSpec<'_>, relay: bool) -> [u8; 4] {
+ if relay && spec.flags & run_flags::DNS_GATEWAY == 0 && spec.net.gateway != [0; 4] { spec.net.gateway } else { super::dns::EMBEDDED }
+}
+
+fn resolv_conf(spec: &RunSpec<'_>, upstream: &[[u8; 4]], embedded: Option<[u8; 4]>) -> String {
let mut resolv = String::new();
let mut servers = net_opts::values(spec.net.opts, net_opts::NAMESERVER).peekable();
- if embedded_dns {
- resolv.push_str(&format!("nameserver {}\n", net::ip_str(super::dns::EMBEDDED)));
+ if let Some(ns) = embedded {
+ resolv.push_str(&format!("nameserver {}\n", net::ip_str(ns)));
} else if servers.peek().is_some() {
for d in servers {
resolv.push_str(&format!("nameserver {d}\n"));
@@ -1362,7 +1380,7 @@ mod tests {
let net = Net { bridge: "kiri1", ip: [10, 5, 0, 2], prefix: 24, gateway: [10, 5, 0, 1], extra_hosts: &[], dns_names: &[], opts };
encode_run(&mut buf, "c1", &[], &["sh"], &[], "/", "", "c1", flags, net, &[], &Limits::default(), 0, &[] as &[&str]);
let Ok(Msg::Run(spec)) = decode(&buf) else { panic!() };
- super::resolv_conf(&spec, upstream, flags & run_flags::DNS_GATEWAY != 0)
+ super::resolv_conf(&spec, upstream, (flags & run_flags::DNS_GATEWAY != 0).then_some(super::super::dns::EMBEDDED))
}
#[test]
@@ -1399,6 +1417,13 @@ mod tests {
spec_with(0, &[], &|s| assert!(super::embedded_dns(s, &gateway, true), "so a refused gateway can fall back to the host"));
spec_with(0, &["nameserver=8.8.8.8"], &|s| assert!(!super::embedded_dns(s, &gateway, true), "--dns servers are asked directly"));
spec_with(run_flags::NET_HOST, &[], &|s| assert!(!super::embedded_dns(s, &gateway, true), "host networking keeps the VM's resolv.conf"));
+ // Named by the bridge's gateway, not 127.0.0.11: builders keep it
+ // for their steps rather than swapping in 8.8.8.8.
+ spec_with(0, &[], &|s| assert_eq!(super::embedded_address(s, true), [172, 17, 0, 1]));
+ spec_with(0, &[], &|s| assert_eq!(super::resolv_conf(s, &gateway, Some(super::embedded_address(s, true))), "nameserver 172.17.0.1\n"));
+ // A user network keeps Docker's 127.0.0.11, as does a Linux host.
+ spec_with(run_flags::DNS_GATEWAY, &[], &|s| assert_eq!(super::embedded_address(s, true), super::super::dns::EMBEDDED));
+ spec_with(0, &[], &|s| assert_eq!(super::embedded_address(s, false), super::super::dns::EMBEDDED));
}
#[test]
diff --git a/crates/kiri-init/src/linux/dns.rs b/crates/kiri-init/src/linux/dns.rs
index ddf507a..6cca4cb 100644
--- a/crates/kiri-init/src/linux/dns.rs
+++ b/crates/kiri-init/src/linux/dns.rs
@@ -33,6 +33,15 @@ const FORWARD_TIMEOUT: Duration = Duration::from_secs(5);
/// At most this many forwarded queries in flight (each has its own socket);
/// more are dropped, and the client asks again.
const MAX_PENDING: usize = 256;
+/// At most this many of them from one container (or one address on a
+/// bridge), so a container flooding lookups can't take DNS from the rest.
+const MAX_PER_CLIENT: usize = 64;
+/// How long the agent's loop may wait for the host to take a relayed
+/// lookup's connection.
+const RELAY_CONNECT: Duration = Duration::from_millis(250);
+/// After the host failed to take one, lookups aren't relayed for this long:
+/// each would stall the loop for `RELAY_CONNECT` again.
+const RELAY_BACKOFF: Duration = Duration::from_secs(5);
/// A lookup the VM's resolver hasn't answered within this goes through the
/// host (VM only). Gateway answers take milliseconds; a slow recursive one
/// that loses the race is simply replaced by the host's.
@@ -62,6 +71,8 @@ struct Pending {
sock: OwnedFd,
/// The ID it went out with.
our: u16,
+ /// Who asked, for `MAX_PER_CLIENT`: the container's address.
+ key: [u8; 4],
/// The server's socket the query came in on.
server: RawFd,
client: libc::sockaddr_in,
@@ -94,6 +105,8 @@ pub struct Dns {
/// Lookups the VM's resolver fails go through the host (in the VM; a
/// Linux host's agent asks the host's resolvers itself).
pub relay: bool,
+ /// The host didn't take a relayed lookup: none are tried until then.
+ relay_down_until: Option<Instant>,
}
fn udp_socket() -> io::Result<OwnedFd> {
@@ -342,7 +355,7 @@ impl Dns {
self.pending.retain(|p| now.duration_since(p.sent) < FORWARD_TIMEOUT);
let key = self.servers[server].container.map_or(client.sin_addr.s_addr.to_ne_bytes(), |(_, owner)| owner);
let own = self.clients.get(&key).and_then(|v| v.first()).copied();
- if self.pending.len() >= MAX_PENDING {
+ if self.pending.len() >= MAX_PENDING || self.pending.iter().filter(|p| p.key == key).count() >= MAX_PER_CLIENT {
return;
}
let Some((_, _, end)) = parse_question(q) else {
@@ -357,7 +370,7 @@ impl Dns {
// No resolver yet, or it can't be reached: the host, if allowed.
let (sock, relayed) = match dest.and_then(|d| ask(d, &msg)) {
Some(s) => (s, false),
- None if may_relay => match relay_stream(&msg) {
+ None if may_relay => match self.relay_stream(&msg) {
Some(s) => (s, true),
None => return,
},
@@ -366,6 +379,7 @@ impl Dns {
let p = Pending {
sock,
our,
+ key,
server: self.servers[server].sock.as_raw_fd(),
client,
id,
@@ -389,12 +403,15 @@ impl Dns {
/// Sends pending query `at` again, through the host, once.
fn relay(&mut self, at: usize) {
- let p = &mut self.pending[at];
- if !p.may_relay {
+ if !self.pending[at].may_relay {
return;
}
- p.may_relay = false;
- if let Some(stream) = relay_stream(&p.query) {
+ self.pending[at].may_relay = false;
+ let query = std::mem::take(&mut self.pending[at].query);
+ let stream = self.relay_stream(&query);
+ let p = &mut self.pending[at];
+ p.query = query;
+ if let Some(stream) = stream {
p.sock = stream;
p.relayed = Some(Vec::new());
p.sent = Instant::now();
@@ -443,6 +460,29 @@ impl Dns {
}
}
+ /// A stream to the host's DNS relay with `msg` sent on it, non-blocking
+ /// for the reply; None while the host is taken for down.
+ fn relay_stream(&mut self, msg: &[u8]) -> Option<OwnedFd> {
+ if self.relay_down_until.is_some_and(|t| Instant::now() < t) {
+ return None;
+ }
+ let Ok(stream) = super::transport::dial_within(kiri_proto::DNS_RELAY_PORT, RELAY_CONNECT) else {
+ self.relay_down_until = Some(Instant::now() + RELAY_BACKOFF);
+ return None;
+ };
+ self.relay_down_until = None;
+ let mut frame = Vec::with_capacity(2 + msg.len());
+ frame.extend_from_slice(&u16::try_from(msg.len()).ok()?.to_be_bytes());
+ frame.extend_from_slice(msg);
+ // A few hundred bytes on a fresh stream: one write, never short.
+ // SAFETY: writing a buffer we own to a socket we own.
+ if unsafe { libc::write(stream.as_raw_fd(), frame.as_ptr().cast(), frame.len()) } != frame.len() as isize {
+ return None;
+ }
+ sys::set_nonblocking(stream.as_raw_fd()).ok()?;
+ Some(stream)
+ }
+
/// Sends lookups the VM's resolver has left unanswered too long
/// through the host. Called on every turn of the agent's loop.
pub fn on_timer(&mut self) {
@@ -484,22 +524,6 @@ fn ask(dest: [u8; 4], msg: &[u8]) -> Option<OwnedFd> {
Some(sock)
}
-/// A stream to the host's DNS relay with `msg` sent on it, non-blocking
-/// for the reply.
-fn relay_stream(msg: &[u8]) -> Option<OwnedFd> {
- let stream = super::transport::dial(kiri_proto::DNS_RELAY_PORT).ok()?;
- let mut frame = Vec::with_capacity(2 + msg.len());
- frame.extend_from_slice(&u16::try_from(msg.len()).ok()?.to_be_bytes());
- frame.extend_from_slice(msg);
- // A few hundred bytes on a fresh stream: one write, never short.
- // SAFETY: writing a buffer we own to a socket we own.
- if unsafe { libc::write(stream.as_raw_fd(), frame.as_ptr().cast(), frame.len()) } != frame.len() as isize {
- return None;
- }
- sys::set_nonblocking(stream.as_raw_fd()).ok()?;
- Some(stream)
-}
-
/// The reply in `u16 len | reply`, once all of it is there.
fn framed_reply(got: &[u8]) -> Option<&[u8]> {
let len = u16::from_be_bytes([*got.first()?, *got.get(1)?]) as usize;
@@ -725,6 +749,7 @@ mod tests {
Pending {
sock: udp_socket().unwrap(),
our: 1,
+ key: [0; 4],
server: -1,
client: unsafe { std::mem::zeroed() },
id: 7,
@@ -736,6 +761,35 @@ mod tests {
}
}
+ #[test]
+ fn one_client_cant_take_every_forwarded_slot() {
+ let mut dns = Dns::default();
+ dns.servers.push(Server { bridges: vec!["kiri0".into()], sock: udp_socket().unwrap(), container: None });
+ let client = super::sockaddr([172, 17, 0, 2], 4000);
+ for _ in 0..super::MAX_PER_CLIENT {
+ let mut p = pending();
+ p.key = [172, 17, 0, 2];
+ dns.pending.push(p);
+ }
+ let q = query("example.com", 1);
+ // Loopback as the resolver: sending always works.
+ dns.forward(0, &q, client, &[[127, 0, 0, 1]]);
+ assert_eq!(dns.pending.len(), super::MAX_PER_CLIENT, "that container's next lookup waits");
+ dns.forward(0, &q, super::sockaddr([172, 17, 0, 3], 4000), &[[127, 0, 0, 1]]);
+ assert_eq!(dns.pending.len(), super::MAX_PER_CLIENT + 1, "another container's goes out");
+ }
+
+ #[test]
+ fn a_host_that_didnt_answer_isnt_dialled_again_at_once() {
+ let mut dns = Dns { relay: true, ..Default::default() };
+ // No host to dial in a test: the first try fails and backs off.
+ assert!(dns.relay_stream(b"query").is_none());
+ let until = dns.relay_down_until.expect("backing off");
+ assert!(until > Instant::now());
+ assert!(dns.relay_stream(b"query").is_none());
+ assert_eq!(dns.relay_down_until, Some(until), "not tried again while backing off");
+ }
+
#[test]
fn a_relayed_reply_is_whole_only_once_its_length_is_there() {
use super::framed_reply;
diff --git a/crates/kiri-init/src/linux/transport.rs b/crates/kiri-init/src/linux/transport.rs
index f9d5059..9b73b04 100644
--- a/crates/kiri-init/src/linux/transport.rs
+++ b/crates/kiri-init/src/linux/transport.rs
@@ -85,6 +85,35 @@ pub fn dial(port: u32) -> io::Result<OwnedFd> {
}
}
+/// `SO_VM_SOCKETS_CONNECT_TIMEOUT` (linux/vm_sockets.h, the `_OLD` value,
+/// which takes a native `timeval`; libc doesn't name it).
+const SO_VM_SOCKETS_CONNECT_TIMEOUT: libc::c_int = 6;
+
+/// Like `dial`, but a connection the host doesn't take within `timeout`
+/// fails (vsock waits two seconds by default), for callers in the agent's
+/// loop, which nothing else runs in while it waits.
+pub fn dial_within(port: u32, timeout: std::time::Duration) -> io::Result<OwnedFd> {
+ if host_dir().is_some() {
+ return dial(port); // a unix socket: connect never waits
+ }
+ // SAFETY: plain socket setup and connect; the fd is owned immediately.
+ unsafe {
+ let fd = sys::check(libc::socket(libc::AF_VSOCK, libc::SOCK_STREAM | libc::SOCK_CLOEXEC, 0) as _, "vsock socket")?;
+ let sock = OwnedFd::from_raw_fd(fd as RawFd);
+ let tv = libc::timeval { tv_sec: timeout.as_secs() as _, tv_usec: timeout.subsec_micros() as _ };
+ sys::check(
+ libc::setsockopt(sock.as_raw_fd(), libc::AF_VSOCK, SO_VM_SOCKETS_CONNECT_TIMEOUT, (&tv as *const libc::timeval).cast(), std::mem::size_of::<libc::timeval>() as u32) as _,
+ "vsock connect timeout",
+ )?;
+ let mut addr: libc::sockaddr_vm = std::mem::zeroed();
+ addr.svm_family = libc::AF_VSOCK as libc::sa_family_t;
+ addr.svm_cid = VMADDR_CID_HOST;
+ addr.svm_port = port;
+ sys::check(libc::connect(sock.as_raw_fd(), (&addr as *const libc::sockaddr_vm).cast(), std::mem::size_of::<libc::sockaddr_vm>() as u32) as _, "vsock connect")?;
+ Ok(sock)
+ }
+}
+
/// The host's own DNS servers, for a host process (no DHCP lease to ask).
/// systemd-resolved's stub (127.0.0.53) is skipped for its upstreams.
pub fn host_dns() -> Vec<[u8; 4]> {
diff --git a/crates/kiri/src/daemon/dns.rs b/crates/kiri/src/daemon/dns.rs
index 28dbb6d..afb5ceb 100644
--- a/crates/kiri/src/daemon/dns.rs
+++ b/crates/kiri/src/daemon/dns.rs
@@ -10,8 +10,9 @@
use std::io::{self, Read, Write};
use std::net::{IpAddr, SocketAddr, UdpSocket};
+use std::os::fd::AsRawFd;
use std::path::Path;
-use std::sync::atomic::{AtomicBool, Ordering};
+use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::time::{Duration, Instant};
/// The resolver file's port; unprivileged, so the daemon needs no root.
@@ -84,36 +85,82 @@ fn answer(q: &[u8]) -> Option<Vec<u8>> {
const RELAY_TRY: Duration = Duration::from_millis(1500);
/// All the resolvers together: the agent stops waiting at five seconds.
const RELAY_BUDGET: Duration = Duration::from_millis(3500);
+/// How long the agent gets to send its query, and to take the reply.
+const RELAY_IO: Duration = Duration::from_secs(5);
+/// Relayed lookups at once, as many as the agent itself keeps in flight;
+/// more are dropped (the client asks again).
+const MAX_RELAYS: usize = 256;
+static RELAYS: AtomicUsize = AtomicUsize::new(0);
/// One lookup from the guest agent: `u16 len | query` in, `u16 len | reply`
/// out (kiri_proto::DNS_RELAY_PORT). The agent sends here only what the
/// VM's gateway resolver refused or left unanswered, so this is the path
/// while macOS's resolver for the VM network is down.
pub fn serve_relay(stream: kiri_vmm::VsockStream) {
- let _ = std::thread::Builder::new().name("kiri-dns-relay".into()).spawn(move || {
- let Ok(fd) = stream.fd.try_clone() else { return };
- let mut conn = std::fs::File::from(fd);
- let mut len = [0u8; 2];
- if conn.read_exact(&mut len).is_err() {
- return;
- }
- let mut query = vec![0u8; u16::from_be_bytes(len) as usize];
- if query.len() < 12 || conn.read_exact(&mut query).is_err() {
- return;
- }
- static SAID: AtomicBool = AtomicBool::new(false);
- if !SAID.swap(true, Ordering::Relaxed) {
- crate::diag::warn!("the VM's DNS at its gateway did not answer; container lookups go through the Mac");
- }
- let name = question_name(&query).unwrap_or_default();
- let servers = mac_resolvers(&name, Path::new("/etc/resolv.conf"), Path::new("/etc/resolver"));
- let reply = relay_query(&query, &servers).unwrap_or_else(|| servfail(&query));
- let mut out = Vec::with_capacity(2 + reply.len());
- out.extend_from_slice(&(reply.len().min(u16::MAX as usize) as u16).to_be_bytes());
- out.extend_from_slice(&reply[..reply.len().min(u16::MAX as usize)]);
- let _ = conn.write_all(&out);
- drop(stream);
+ if RELAYS.fetch_add(1, Ordering::Relaxed) >= MAX_RELAYS {
+ RELAYS.fetch_sub(1, Ordering::Relaxed);
+ return;
+ }
+ let spawned = std::thread::Builder::new().name("kiri-dns-relay".into()).spawn(move || {
+ relay_one(&stream);
+ RELAYS.fetch_sub(1, Ordering::Relaxed);
});
+ if spawned.is_err() {
+ RELAYS.fetch_sub(1, Ordering::Relaxed);
+ }
+}
+
+fn relay_one(stream: &kiri_vmm::VsockStream) {
+ let Ok(fd) = stream.fd.try_clone() else { return };
+ // A peer that connects and then says nothing doesn't keep a thread.
+ // Best effort: without the timeouts the lookup still works.
+ if let Err(e) = set_timeouts(fd.as_raw_fd(), RELAY_IO) {
+ crate::diag::debug!("DNS relay: no socket timeouts", error = e);
+ }
+ let mut conn = std::fs::File::from(fd);
+ let mut len = [0u8; 2];
+ if conn.read_exact(&mut len).is_err() {
+ return;
+ }
+ let mut query = vec![0u8; u16::from_be_bytes(len) as usize];
+ if conn.read_exact(&mut query).is_err() || !is_query(&query) {
+ return;
+ }
+ static SAID: AtomicBool = AtomicBool::new(false);
+ if !SAID.swap(true, Ordering::Relaxed) {
+ crate::diag::warn!("the VM's DNS at its gateway did not answer; container lookups go through the Mac");
+ }
+ let name = question_name(&query).unwrap_or_default();
+ let servers = mac_resolvers(&name, Path::new("/etc/resolv.conf"), Path::new("/etc/resolver"));
+ let reply = relay_query(&query, &servers).unwrap_or_else(|| servfail(&query));
+ let mut out = Vec::with_capacity(2 + reply.len());
+ out.extend_from_slice(&(reply.len().min(u16::MAX as usize) as u16).to_be_bytes());
+ out.extend_from_slice(&reply[..reply.len().min(u16::MAX as usize)]);
+ let _ = conn.write_all(&out);
+}
+
+/// Whether `q` is a standard DNS query with one well-formed question: the
+/// only thing the agent sends, and so all that's passed to the Mac's
+/// resolvers (some of which listen on the Mac's loopback).
+fn is_query(q: &[u8]) -> bool {
+ q.len() >= 12
+ && q[2] & 0x80 == 0 // QR: a query
+ && (q[2] >> 3) & 0x0f == 0 // OPCODE: QUERY
+ && u16::from_be_bytes([q[4], q[5]]) == 1
+ && q[6..8] == [0, 0] // no answers
+ && question_end(q).is_some()
+}
+
+fn set_timeouts(fd: std::os::fd::RawFd, t: Duration) -> io::Result<()> {
+ let tv = libc::timeval { tv_sec: t.as_secs() as _, tv_usec: t.subsec_micros() as _ };
+ let len = std::mem::size_of::<libc::timeval>() as libc::socklen_t;
+ for opt in [libc::SO_RCVTIMEO, libc::SO_SNDTIMEO] {
+ // SAFETY: a valid timeval for a socket option on a descriptor we hold.
+ if unsafe { libc::setsockopt(fd, libc::SOL_SOCKET, opt, (&tv as *const libc::timeval).cast(), len) } != 0 {
+ return Err(io::Error::last_os_error());
+ }
+ }
+ Ok(())
}
/// The first answer to `query` from `servers`, tried in order.
@@ -305,6 +352,24 @@ mod tests {
assert_eq!(super::question_name(&q).as_deref(), Some("registry-1.docker.io"));
}
+ #[test]
+ fn only_standard_queries_are_relayed() {
+ let q = query("registry-1.docker.io", 1);
+ assert!(super::is_query(&q));
+ let mut reply = q.clone();
+ reply[2] |= 0x80;
+ assert!(!super::is_query(&reply), "a response");
+ let mut update = q.clone();
+ update[2] = 5 << 3;
+ assert!(!super::is_query(&update), "an UPDATE");
+ let mut two = q.clone();
+ two[5] = 2;
+ assert!(!super::is_query(&two), "two questions");
+ assert!(!super::is_query(&q[..q.len() - 1]), "cut short");
+ assert!(!super::is_query(&[0x12, 0x34, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0xc0, 12, 0, 1, 0, 1]), "a compression pointer");
+ assert!(!super::is_query(b"GET / HTTP/1.1\r\nHost: x\r\n\r\n"), "not DNS at all");
+ }
+
#[test]
fn servfail_keeps_the_id_and_question() {
let q = query("example.com", 1);
diff --git a/tests/e2e.sh b/tests/e2e.sh
index 01485a9..95380ad 100755
--- a/tests/e2e.sh
+++ b/tests/e2e.sh
@@ -180,6 +180,10 @@ t default-route "default via 172.17.0.1" $D run --rm alpine ip route
t lo "127.0.0.1" $D run --rm alpine sh -c 'ip -4 addr show lo'
t internet-http "Example Domain" $D run --rm alpine wget -qO- http://example.com
t internet-dns "Address" $D run --rm alpine nslookup example.com
+# The default bridge in the VM names its gateway, not 127.0.0.11: BuildKit
+# drops loopback nameservers from build steps and puts 8.8.8.8 in their place.
+t dns-default-bridge-gateway "nameserver 172.17.0.1" $D run --rm alpine cat /etc/resolv.conf
+code dns-default-bridge-no-loopback 1 $D run --rm alpine grep -q 127.0.0.11 /etc/resolv.conf
t internet-https "200" $D run --rm curlimages/curl:latest -s -o /dev/null -w '%{http_code}' https://www.google.com
t none-net "lo" $D run --rm --network none alpine ip link
code none-no-internet 1 $D run --rm --network none alpine wget -qO- -T 2 http://example.com
@@ -540,6 +544,10 @@ t build-runs "from context" $D run --rm kiri-test/built
t build-arg "hello" $D run --rm kiri-test/built
t build-cached "CACHED" bash -c "cd $BD && BUILDX_BUILDER=$BX $D build --progress plain --build-arg MSG=hello -t kiri-test/built . 2>&1"
t build-network "curl" bash -c "cd $BD && printf 'FROM alpine\nRUN apk add --no-cache curl\n' > Dockerfile.net && BUILDX_BUILDER=$BX perl -e 'alarm 300; exec @ARGV' $D build -f Dockerfile.net -t kiri-test/net . >/dev/null 2>&1 && $D run --rm kiri-test/net curl --version"
+# Build steps keep Kiri's resolver (and with it the Mac's): no 8.8.8.8.
+t build-resolvconf "nameserver 172.17.0.1" bash -c "cd $BD && printf 'FROM alpine\nRUN cat /etc/resolv.conf\n' > Dockerfile.dns && BUILDX_BUILDER=$BX perl -e 'alarm 300; exec @ARGV' $D build --no-cache --progress plain -f Dockerfile.dns . 2>&1"
+code build-no-public-resolver 1 bash -c "cd $BD && BUILDX_BUILDER=$BX perl -e 'alarm 300; exec @ARGV' $D build --no-cache --progress plain -f Dockerfile.dns . 2>&1 | grep -q 'nameserver 8.8.8.8'"
+t build-dns "Address" bash -c "cd $BD && printf 'FROM alpine\nRUN nslookup example.com\n' > Dockerfile.lookup && BUILDX_BUILDER=$BX perl -e 'alarm 300; exec @ARGV' $D build --no-cache --progress plain -f Dockerfile.lookup . 2>&1"
t build-fails "exit code: 3" bash -c "cd $BD && printf 'FROM alpine\nRUN exit 3\n' > Dockerfile.bad && BUILDX_BUILDER=$BX $D build -f Dockerfile.bad . 2>&1"
$D rmi kiri-test/built kiri-test/net >/dev/null 2>&1; rm -rf "$BD"
@@ -683,7 +691,8 @@ $D rm -f dns >/dev/null
code udp-port-free-after 0 bash -c "python3 -c 'import socket; s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM); s.bind((\"0.0.0.0\", 15354))'"
section "Linux machines"
-KIRI=${KIRI:-./target/release/kiri}
+# Absolute: machine-cwd runs it from another folder.
+KIRI=${KIRI:-$(cd "$(dirname "$0")/.." && pwd)/target/release/kiri}
# The binary as one shell word, for commands passed to bash -c (app paths have spaces).
KQ=$(printf %q "$KIRI")
t machine-create "is running" "$KIRI" machine create e2e-m --distro alpine