Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/hive-priv/src/egress_ech.rs

1116 lines
39 KiB
Rust

//! `egress-ech-relay <listen-address:port> <mark>`: the TLS `ClientHello` check in
//! front of squid's HTTPS intercept port (`nix/host-modules/hive-egress.nix`).
//!
//! Squid 7.6 cannot see the `encrypted_client_hello` extension, so the nft
//! redirect for TCP port 443 ends here. The relay reads the first TLS records of
//! each connection and closes it when the `ClientHello` carries the extension
//! or cannot be parsed. Otherwise it connects to the destination the agent
//! dialed and sends the bytes on. That second connection has to look like the
//! agent's to squid, which reads the destination from conntrack and names the
//! agent from the source address:
//!
//! - it goes to the original destination (`SO_ORIGINAL_DST` of the accepted
//! socket) and carries the packet mark; an nft `output` rule DNATs marked
//! packets to squid's HTTPS intercept port;
//! - it is bound to a source address in [`POOL`], derived from the agent's
//! ([`pool_source`]). The pool is a `local` route on `lo`, so squid's replies
//! come back over the loopback; the agent's own address would send them out
//! over the bridge. [`agent_of`] maps a pool address back to the agent.
//!
//! A connection the relay cannot place (no original destination, no complete
//! hello within [`HELLO_TIMEOUT`], too many connections open) is closed.
//!
//! GREASE: a client with no ECH configuration (Chrome and Firefox by default)
//! sends the extension anyway with random contents, and the wire cannot tell
//! that from a real one. Every `encrypted_client_hello` extension counts as ECH.
//! A `log` agent is observed, not refused: its ECH hello goes on, with a
//! `hive-egress-ech-would-refuse` line.
//!
//! [`agent_of`]: crate::egress::agent_of
use std::collections::HashMap;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::os::fd::AsRawFd as _;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use anyhow::{Context as _, Result, bail};
use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
use tokio::net::{TcpListener, TcpSocket, TcpStream};
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
/// The sub-command that runs the relay.
pub(crate) const RELAY: &str = "egress-ech-relay";
/// The pool of source addresses the relay connects from: `198.18.0.0/15`, which
/// RFC 2544 sets aside for benchmarking, so no network routes it. The hive-egress
/// unit adds it to the `local` table of `lo`.
const POOL: u32 = 0xC612_0000;
/// The part of a pool address that is the agent's: the low 17 bits. The bridge
/// subnet must be no larger than a /15, which the module asserts.
const HOST_BITS: u32 = 0x0001_FFFF;
/// The source address the relay connects from for an agent at `agent`.
pub(crate) fn pool_source(agent: Ipv4Addr) -> Ipv4Addr {
Ipv4Addr::from_bits(POOL | (agent.to_bits() & HOST_BITS))
}
/// Whether `ip` is in the pool.
pub(crate) fn in_pool(ip: Ipv4Addr) -> bool {
ip.to_bits() & !HOST_BITS == POOL
}
/// Whether `lease` is the address a connection from `source` was made for:
/// the same address, or a pool address built from it.
pub(crate) fn same_host(source: Ipv4Addr, lease: Ipv4Addr) -> bool {
if in_pool(source) {
!in_pool(lease) && pool_source(lease) == source
} else {
source == lease
}
}
/// Most TLS records a `ClientHello` may be spread over.
const MAX_RECORDS: usize = 4;
/// Largest TLS record a peer may send (RFC 8446, 5.1).
const MAX_RECORD: usize = 16384;
/// Largest `ClientHello` the relay reads. Real ones are a few kilobytes.
const MAX_HELLO: usize = MAX_RECORD;
/// Time a peer has to send its whole `ClientHello`.
const HELLO_TIMEOUT: Duration = Duration::from_secs(10);
/// Time the relay waits for the destination to accept.
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
/// Connections open at once; further ones are closed.
const MAX_CONNECTIONS: usize = 2048;
/// Connections open at once from one source address, so that one agent cannot
/// use up [`MAX_CONNECTIONS`]. A browser keeps a few dozen open; 256 leaves an
/// agent room for a busy crawler and lets eight sources fill the relay.
const MAX_PER_SOURCE: usize = 256;
/// Where the squid include that `hive-c0re` renders names the `log` agents, set
/// by the unit.
const AGENT_ALLOW_ENV: &str = "HIVE_EGRESS_AGENT_ALLOW";
/// Least time between two log lines for one source address.
const LOG_EVERY: Duration = Duration::from_secs(1);
/// Source addresses the log limiter remembers.
const LOG_TRACKED: usize = 1024;
/// `SO_ORIGINAL_DST` of `<linux/netfilter_ipv4.h>`.
const SO_ORIGINAL_DST: libc::c_int = 80;
/// The extension types that carry an encrypted `ClientHello`: RFC 9849's
/// `encrypted_client_hello`, and the codepoints earlier drafts of it used
/// (`encrypted_server_name` of the ESNI drafts, the draft codepoints
/// `0xfe08`-`0xfe0c` and `0xff02`).
fn is_ech(extension: u16) -> bool {
matches!(extension, 0xfe0d | 0xfe08..=0xfe0c | 0xff02 | 0xffce)
}
/// What the first bytes of a connection say.
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum Verdict {
/// The `ClientHello` is not complete yet.
Incomplete,
/// A well-formed `ClientHello` with no ECH extension.
Clean,
/// A well-formed `ClientHello` with an ECH extension.
Ech,
/// Not a `ClientHello` the relay accepts, and why.
Malformed(&'static str),
}
fn u16_at(b: &[u8], at: usize) -> Option<usize> {
Some(usize::from(u16::from_be_bytes([
*b.get(at)?,
*b.get(at + 1)?,
])))
}
fn u24_at(b: &[u8], at: usize) -> Option<usize> {
Some((usize::from(*b.get(at)?) << 16) | u16_at(b, at + 1)?)
}
/// Look at the bytes a connection has sent so far, from its first byte.
///
/// The handshake message is reassembled from the records; there may be at most
/// [`MAX_RECORDS`] of them. Everything the parse cannot account for is
/// [`Verdict::Malformed`]: a record that is not a handshake record, a first
/// message that is not a `ClientHello`, bytes after the hello, a length that
/// overruns its container or leaves bytes over.
pub(crate) fn inspect(stream: &[u8]) -> Verdict {
let mut message = Vec::new();
let mut at = 0;
for _ in 0..MAX_RECORDS {
let Some(header) = stream.get(at..at + 5) else {
return Verdict::Incomplete;
};
if header[0] != 22 || header[1] != 3 || header[2] > 4 {
return Verdict::Malformed("not a TLS handshake record");
}
let len = usize::from(u16::from_be_bytes([header[3], header[4]]));
if len == 0 || len > MAX_RECORD {
return Verdict::Malformed("record length out of range");
}
let Some(body) = stream.get(at + 5..at + 5 + len) else {
return Verdict::Incomplete;
};
message.extend_from_slice(body);
at += 5 + len;
if message.len() >= 4 {
if message[0] != 1 {
return Verdict::Malformed("first handshake message is not a ClientHello");
}
let want = 4 + u24_at(&message, 1).unwrap_or(0);
if want > MAX_HELLO {
return Verdict::Malformed("ClientHello too large");
}
if message.len() == want {
return hello(&message[4..]);
}
if message.len() > want {
return Verdict::Malformed("bytes after the ClientHello");
}
}
}
Verdict::Malformed("ClientHello spans too many records")
}
/// The body of a `ClientHello` (RFC 8446, 4.1.2).
fn hello(body: &[u8]) -> Verdict {
const BAD: Verdict = Verdict::Malformed("ClientHello is malformed");
if body.first() != Some(&3) {
return BAD;
}
// legacy_version (2), random (32), then the session id.
let Some(&session) = body.get(34) else {
return BAD;
};
if session > 32 {
return BAD;
}
let mut at = 35 + usize::from(session);
let Some(suites) = u16_at(body, at) else {
return BAD;
};
if suites < 2 || suites % 2 != 0 {
return BAD;
}
at += 2 + suites;
let Some(&compression) = body.get(at) else {
return BAD;
};
if compression < 1 {
return BAD;
}
at += 1 + usize::from(compression);
if at == body.len() {
// TLS 1.2 and older allow a hello with no extensions.
return Verdict::Clean;
}
let Some(total) = u16_at(body, at) else {
return BAD;
};
at += 2;
if body.len() - at != total {
return BAD;
}
let mut ech = false;
while at < body.len() {
let (Some(kind), Some(len)) = (u16_at(body, at), u16_at(body, at + 2)) else {
return BAD;
};
at += 4;
if at + len > body.len() {
return BAD;
}
at += len;
ech |= u16::try_from(kind).is_ok_and(is_ech);
}
if ech { Verdict::Ech } else { Verdict::Clean }
}
/// Lets a source address log once per [`LOG_EVERY`]. An address the limiter
/// forgets (it tracks [`LOG_TRACKED`]) logs again, so the memory stays bounded.
#[derive(Default)]
struct LogLimit {
last: HashMap<Ipv4Addr, Instant>,
}
impl LogLimit {
fn allow(&mut self, who: Ipv4Addr, now: Instant) -> bool {
if self.last.len() >= LOG_TRACKED && !self.last.contains_key(&who) {
self.last
.retain(|_, at| now.duration_since(*at) < LOG_EVERY);
if self.last.len() >= LOG_TRACKED {
self.last.clear();
}
}
match self.last.get(&who) {
Some(at) if now.duration_since(*at) < LOG_EVERY => false,
_ => {
self.last.insert(who, now);
true
}
}
}
}
/// The line a refusal logs: the agent that made the connection, as `egress-whois`
/// names it, and the address it dialed.
fn refusal_line(what: &str, agent: Option<&str>, dst: Ipv4Addr, reason: Option<&str>) -> String {
let agent = agent.unwrap_or("-");
match reason {
Some(reason) => format!("{what} agent={agent} dst={dst} reason={reason}"),
None => format!("{what} agent={agent} dst={dst}"),
}
}
/// What the relay shares between connections.
struct Shared {
mark: u32,
per_source: usize,
agent_allow: Option<String>,
limit: Mutex<LogLimit>,
open: Mutex<HashMap<Ipv4Addr, usize>>,
}
/// What the relay does with a connection whose hello is not clean.
#[derive(Debug, PartialEq, Eq)]
enum Action {
/// Send it on to squid.
Forward,
/// Send it on to squid, and log what a restricted agent would have got.
ForwardLogged,
Refuse,
}
/// A `log` agent is observed, not refused (it is allowed what it is not
/// listed for, and recorded as `would_deny`): its ECH hello goes on. A hello
/// that does not parse is refused for every agent.
fn decide(verdict: &Verdict, log_agent: bool) -> Action {
match verdict {
Verdict::Clean => Action::Forward,
Verdict::Ech if log_agent => Action::ForwardLogged,
Verdict::Ech | Verdict::Malformed(_) | Verdict::Incomplete => Action::Refuse,
}
}
/// Whether `allow` (the squid include `hive-c0re` renders) puts `agent` in `log`
/// mode. Squid reads the same file to decide the same thing, so the relay and
/// squid cannot disagree about a mode; a missing or unreadable file lists no
/// `log` agent.
fn is_log_agent(allow: &str, agent: &str) -> bool {
let line = format!("acl agent_log_{agent} proxy_auth {agent}");
allow.lines().any(|l| l.trim() == line)
}
impl Shared {
fn new(mark: u32, per_source: usize, agent_allow: Option<String>) -> Self {
Self {
mark,
per_source,
agent_allow,
limit: Mutex::default(),
open: Mutex::default(),
}
}
/// The agent behind `peer` as `egress-whois` names it, and whether it is in
/// `log` mode. Reads files; called off the runtime's threads.
fn who(&self, peer: Ipv4Addr) -> (Option<String>, bool) {
let agent = crate::egress::agent_of_now(&peer.to_string());
let log = match (&agent, &self.agent_allow) {
(Some(agent), Some(path)) => {
is_log_agent(&std::fs::read_to_string(path).unwrap_or_default(), agent)
}
_ => false,
};
(agent, log)
}
/// Log what happened to a connection whose hello was not clean.
fn report(
&self,
peer: Ipv4Addr,
dst: Ipv4Addr,
verdict: &Verdict,
action: &Action,
agent: Option<&str>,
) {
let allowed = self
.limit
.lock()
.is_ok_and(|mut limit| limit.allow(peer, Instant::now()));
if !allowed {
return;
}
let line = match (verdict, action) {
(Verdict::Ech, Action::ForwardLogged) => {
refusal_line("hive-egress-ech-would-refuse", agent, dst, None)
}
(Verdict::Ech, Action::Refuse) => {
refusal_line("hive-egress-ech-refused", agent, dst, None)
}
(Verdict::Malformed(why), Action::Refuse) => {
refusal_line("hive-egress-hello-refused", agent, dst, Some(why))
}
_ => return,
};
eprintln!("{line}");
}
}
/// One slot of the connections a source has open, released when dropped.
struct SourceSlot {
shared: Arc<Shared>,
source: Ipv4Addr,
}
impl SourceSlot {
/// `None` when `source` already has its share open.
fn take(shared: &Arc<Shared>, source: Ipv4Addr) -> Option<Self> {
let mut open = shared.open.lock().ok()?;
let count = open.entry(source).or_insert(0);
if *count >= shared.per_source {
return None;
}
*count += 1;
Some(Self {
shared: Arc::clone(shared),
source,
})
}
}
impl Drop for SourceSlot {
fn drop(&mut self) {
let Ok(mut open) = self.shared.open.lock() else {
return;
};
if let Some(count) = open.get_mut(&self.source) {
*count -= 1;
if *count == 0 {
open.remove(&self.source);
}
}
}
}
/// The room a connection holds for as long as it is open.
struct Admission {
_global: OwnedSemaphorePermit,
_source: SourceSlot,
}
/// Room for a connection from `source`, if both the relay and the source have
/// some left.
fn admit(shared: &Arc<Shared>, open: &Arc<Semaphore>, source: Ipv4Addr) -> Option<Admission> {
let global = Arc::clone(open).try_acquire_owned().ok()?;
let slot = SourceSlot::take(shared, source)?;
Some(Admission {
_global: global,
_source: slot,
})
}
/// The address the peer of `stream` dialed, from the conntrack entry of the
/// redirect. Fails for a connection that was not redirected.
fn original_dst(stream: &TcpStream) -> std::io::Result<SocketAddrV4> {
// SAFETY: `sockaddr_in` is plain data for which all zeroes is valid, and
// `getsockopt` writes at most `len` bytes into it.
let (rc, addr) = unsafe {
let mut addr: libc::sockaddr_in = std::mem::zeroed();
let mut len = libc::socklen_t::try_from(size_of::<libc::sockaddr_in>()).unwrap_or(16);
let rc = libc::getsockopt(
stream.as_raw_fd(),
libc::SOL_IP,
SO_ORIGINAL_DST,
(&raw mut addr).cast(),
&raw mut len,
);
(rc, addr)
};
if rc != 0 {
return Err(std::io::Error::last_os_error());
}
Ok(SocketAddrV4::new(
Ipv4Addr::from_bits(u32::from_be(addr.sin_addr.s_addr)),
u16::from_be(addr.sin_port),
))
}
fn set_mark(socket: &TcpSocket, mark: u32) -> std::io::Result<()> {
// SAFETY: `setsockopt` reads `size_of::<u32>()` bytes from a live `u32`.
let rc = unsafe {
libc::setsockopt(
socket.as_raw_fd(),
libc::SOL_SOCKET,
libc::SO_MARK,
(&raw const mark).cast(),
libc::socklen_t::try_from(size_of::<u32>()).unwrap_or(4),
)
};
if rc == 0 {
Ok(())
} else {
Err(std::io::Error::last_os_error())
}
}
/// Read until [`inspect`] has an answer. `None` is a peer that closed, errored
/// or was too slow before it had one.
async fn read_hello(client: &mut TcpStream, limit: Duration) -> Option<(Vec<u8>, Verdict)> {
let read = async {
let mut seen = Vec::new();
let mut chunk = [0_u8; 4096];
loop {
let verdict = inspect(&seen);
if verdict != Verdict::Incomplete {
return Some((seen, verdict));
}
if seen.len() > MAX_RECORDS * (MAX_RECORD + 5) {
return None;
}
match client.read(&mut chunk).await {
Ok(0) | Err(_) => return None,
Ok(n) => seen.extend_from_slice(&chunk[..n]),
}
}
};
tokio::time::timeout(limit, read).await.ok().flatten()
}
async fn upstream(shared: &Shared, peer: Ipv4Addr, dst: SocketAddrV4) -> Result<TcpStream> {
let socket = TcpSocket::new_v4().context("socket")?;
set_mark(&socket, shared.mark).context("set the packet mark")?;
socket
.bind(SocketAddr::V4(SocketAddrV4::new(pool_source(peer), 0)))
.context("bind the pool address")?;
tokio::time::timeout(CONNECT_TIMEOUT, socket.connect(SocketAddr::V4(dst)))
.await
.context("connect timed out")?
.context("connect")
}
async fn handle(shared: Arc<Shared>, mut client: TcpStream, peer: SocketAddr) {
let SocketAddr::V4(peer) = peer else {
return;
};
let Ok(dst) = original_dst(&client) else {
return;
};
let Some((seen, verdict)) = read_hello(&mut client, HELLO_TIMEOUT).await else {
return;
};
let action = if verdict == Verdict::Clean {
Action::Forward
} else {
let looked = {
let shared = Arc::clone(&shared);
let source = *peer.ip();
tokio::task::spawn_blocking(move || shared.who(source)).await
};
let (agent, log) = looked.unwrap_or((None, false));
let action = decide(&verdict, log);
shared.report(*peer.ip(), *dst.ip(), &verdict, &action, agent.as_deref());
action
};
if action == Action::Refuse {
return;
}
let Ok(mut server) = upstream(&shared, *peer.ip(), dst).await else {
return;
};
if server.write_all(&seen).await.is_err() {
return;
}
let _ = tokio::io::copy_bidirectional(&mut client, &mut server).await;
}
async fn serve(listen: SocketAddr, mark: u32, agent_allow: Option<String>) -> Result<()> {
let listener = TcpListener::bind(listen)
.await
.with_context(|| format!("listen on {listen}"))?;
let shared = Arc::new(Shared::new(mark, MAX_PER_SOURCE, agent_allow));
let open = Arc::new(Semaphore::new(MAX_CONNECTIONS));
loop {
let (client, peer) = match listener.accept().await {
Ok(accepted) => accepted,
Err(e) => {
eprintln!("{RELAY}: accept: {e}");
tokio::time::sleep(Duration::from_millis(100)).await;
continue;
}
};
let SocketAddr::V4(source) = peer else {
continue;
};
let Some(admission) = admit(&shared, &open, *source.ip()) else {
continue;
};
let shared = Arc::clone(&shared);
tokio::spawn(async move {
handle(shared, client, peer).await;
drop(admission);
});
}
}
/// `0x…` or decimal.
fn parse_mark(text: &str) -> Option<u32> {
match text.strip_prefix("0x") {
Some(hex) => u32::from_str_radix(hex, 16).ok(),
None => text.parse().ok(),
}
}
async fn start(args: &[String]) -> Result<()> {
let [listen, mark] = args else {
bail!("usage: {RELAY} <listen-address:port> <mark>");
};
let listen: SocketAddr = listen.parse().context("listen address")?;
let mark = parse_mark(mark).context("mark is a number")?;
serve(listen, mark, std::env::var(AGENT_ALLOW_ENV).ok()).await
}
/// Run `egress-ech-relay` when `args` name it; `None` otherwise. Returns the
/// exit code.
pub(crate) async fn run(args: &[String]) -> Option<i32> {
let (name, rest) = args.split_first()?;
if name != RELAY {
return None;
}
Some(match start(rest).await {
Ok(()) => 0,
Err(e) => {
eprintln!("{RELAY}: {e:#}");
1
}
})
}
#[cfg(test)]
mod tests {
use super::*;
/// The pieces of a `ClientHello`, with the values a real client sends.
struct Hello {
session: Vec<u8>,
extensions: Vec<(u16, Vec<u8>)>,
}
fn sni(name: &str) -> (u16, Vec<u8>) {
let mut data = Vec::new();
data.extend_from_slice(&u16::try_from(name.len() + 3).unwrap().to_be_bytes());
data.push(0);
data.extend_from_slice(&u16::try_from(name.len()).unwrap().to_be_bytes());
data.extend_from_slice(name.as_bytes());
(0, data)
}
/// The extensions of a hello from OpenSSL 3: SNI, groups, signature
/// algorithms, supported versions, a key share.
fn plain() -> Hello {
Hello {
session: vec![7; 32],
extensions: vec![
sni("allowed.example"),
(10, vec![0, 4, 0, 29, 0, 23]),
(13, vec![0, 4, 8, 4, 4, 3]),
(43, vec![2, 3, 4]),
(51, [&[0_u8, 36, 0, 29, 0, 32][..], &[9; 32][..]].concat()),
],
}
}
/// An ECH extension as a client sends it: `outer`, a KDF and AEAD, a config
/// id, a 32 byte `enc` and a payload. GREASE has the same layout.
fn ech_extension(kind: u16) -> (u16, Vec<u8>) {
let mut data = vec![0, 0, 1, 0, 1, 0x42, 0, 32];
data.extend_from_slice(&[5; 32]);
data.extend_from_slice(&[0, 144]);
data.extend_from_slice(&[6; 144]);
(kind, data)
}
impl Hello {
fn body(&self) -> Vec<u8> {
let mut body = vec![3, 3];
body.extend_from_slice(&[1; 32]);
body.push(u8::try_from(self.session.len()).unwrap());
body.extend_from_slice(&self.session);
body.extend_from_slice(&[0, 4, 0x13, 0x01, 0x13, 0x02]);
body.extend_from_slice(&[1, 0]);
let mut extensions = Vec::new();
for (kind, data) in &self.extensions {
extensions.extend_from_slice(&kind.to_be_bytes());
extensions.extend_from_slice(&u16::try_from(data.len()).unwrap().to_be_bytes());
extensions.extend_from_slice(data);
}
body.extend_from_slice(&u16::try_from(extensions.len()).unwrap().to_be_bytes());
body.extend_from_slice(&extensions);
body
}
/// The handshake message: type, 24 bit length, body.
fn message(&self) -> Vec<u8> {
let body = self.body();
let mut message = vec![1, 0];
message.extend_from_slice(&u16::try_from(body.len()).unwrap().to_be_bytes());
message.extend_from_slice(&body);
message
}
/// One record holding the whole message.
fn record(&self) -> Vec<u8> {
records(&self.message(), usize::MAX)
}
}
/// `message` in records of at most `size` bytes.
fn records(message: &[u8], size: usize) -> Vec<u8> {
let mut out = Vec::new();
for part in message.chunks(size.min(message.len().max(1))) {
out.extend_from_slice(&[22, 3, 1]);
out.extend_from_slice(&u16::try_from(part.len()).unwrap().to_be_bytes());
out.extend_from_slice(part);
}
out
}
#[test]
fn a_hello_without_ech_is_clean() {
assert_eq!(inspect(&plain().record()), Verdict::Clean);
}
#[test]
fn a_hello_with_the_ech_extension_is_ech_wherever_it_sits() {
for at in [0, 2, 5] {
let mut hello = plain();
hello.extensions.insert(at, ech_extension(0xfe0d));
assert_eq!(inspect(&hello.record()), Verdict::Ech, "at {at}");
}
}
#[test]
fn the_draft_codepoints_are_ech_and_their_neighbours_are_not() {
for (kind, expected) in [
(0xfe0d, Verdict::Ech),
(0xff02, Verdict::Ech),
(0xffce, Verdict::Ech),
(0xfe08, Verdict::Ech),
(0xfe0c, Verdict::Ech),
(0xfe07, Verdict::Clean),
(0xfe0e, Verdict::Clean),
(0xff01, Verdict::Clean),
(0xfd00, Verdict::Clean),
] {
let mut hello = plain();
hello.extensions.push(ech_extension(kind));
assert_eq!(inspect(&hello.record()), expected, "{kind:#x}");
}
}
#[test]
fn grease_ech_is_ech() {
// What a client with no configuration sends: the same extension, a
// random config id and random contents, and an empty session id.
let mut hello = plain();
hello.session = Vec::new();
let mut grease = vec![0, 0, 1, 0, 1, 0xf3, 0, 32];
grease.extend_from_slice(&[0xa5; 32]);
grease.extend_from_slice(&[0, 176]);
grease.extend_from_slice(&[0x5a; 176]);
hello.extensions.push((0xfe0d, grease));
assert_eq!(inspect(&hello.record()), Verdict::Ech);
}
#[test]
fn a_hello_with_no_extensions_is_clean() {
let mut hello = plain();
hello.extensions.clear();
let mut body = hello.body();
body.truncate(body.len() - 2);
let mut message = vec![1, 0];
message.extend_from_slice(&u16::try_from(body.len()).unwrap().to_be_bytes());
message.extend_from_slice(&body);
assert_eq!(inspect(&records(&message, usize::MAX)), Verdict::Clean);
}
#[test]
fn a_hello_is_incomplete_until_its_last_byte() {
let mut hello = plain();
hello.extensions.push(ech_extension(0xfe0d));
let record = hello.record();
for cut in 0..record.len() {
assert_eq!(inspect(&record[..cut]), Verdict::Incomplete, "cut at {cut}");
}
assert_eq!(inspect(&record), Verdict::Ech);
}
#[test]
fn a_hello_in_several_records_is_read_whole() {
let mut hello = plain();
hello.extensions.push(ech_extension(0xfe0d));
let message = hello.message();
// Three records; the extension is in the last.
let split = records(&message, message.len().div_ceil(3));
assert_eq!(inspect(&split), Verdict::Ech);
let mut clean = plain();
clean.extensions.push((0x15, vec![0; 300]));
let message = clean.message();
assert_eq!(
inspect(&records(&message, message.len().div_ceil(3))),
Verdict::Clean
);
// A record that ends inside the handshake header.
assert_eq!(
inspect(&records(&message, 2)),
Verdict::Malformed("ClientHello spans too many records")
);
}
#[test]
fn a_hello_over_too_many_records_is_refused() {
let hello = plain();
let message = hello.message();
assert_eq!(
inspect(&records(&message, message.len().div_ceil(MAX_RECORDS))),
Verdict::Clean
);
assert_eq!(
inspect(&records(&message, message.len().div_ceil(MAX_RECORDS + 1))),
Verdict::Malformed("ClientHello spans too many records")
);
}
#[test]
fn what_is_not_a_hello_is_refused() {
let cases: [(&str, Vec<u8>); 6] = [
("http", b"GET / HTTP/1.1\r\n\r\n".to_vec()),
("application data", vec![23, 3, 3, 0, 4, 1, 2, 3, 4]),
("alert", vec![21, 3, 3, 0, 2, 1, 0]),
("sslv2", vec![0x80, 0x2e, 1, 0, 2, 0, 0, 0, 0]),
("empty record", vec![22, 3, 1, 0, 0]),
("server hello", vec![22, 3, 3, 0, 4, 2, 0, 0, 0]),
];
for (name, bytes) in cases {
assert!(matches!(inspect(&bytes), Verdict::Malformed(_)), "{name}");
}
}
#[test]
fn a_record_header_of_another_protocol_is_refused() {
// A well-formed hello behind a header that is not TLS 1.x handshake.
let good = plain().record();
assert_eq!(inspect(&good), Verdict::Clean);
for (at, value) in [(0, 23_u8), (1, 2), (1, 4), (2, 5)] {
let mut bent = good.clone();
bent[at] = value;
assert_eq!(
inspect(&bent),
Verdict::Malformed("not a TLS handshake record"),
"byte {at} = {value}"
);
}
// The record versions real clients send.
for minor in [1_u8, 2, 3] {
let mut ok = good.clone();
ok[2] = minor;
assert_eq!(inspect(&ok), Verdict::Clean, "TLS 1.{}", minor - 1);
}
}
#[test]
fn a_record_over_the_largest_is_refused() {
let mut bytes = vec![22, 3, 1];
bytes.extend_from_slice(&u16::try_from(MAX_RECORD + 1).unwrap().to_be_bytes());
assert_eq!(
inspect(&bytes),
Verdict::Malformed("record length out of range")
);
}
#[test]
fn a_hello_over_the_largest_is_refused() {
let mut hello = plain();
hello.extensions.push((0x15, vec![0; MAX_HELLO]));
let message = hello.message();
assert_eq!(
inspect(&records(&message, MAX_RECORD)),
Verdict::Malformed("ClientHello too large")
);
}
#[test]
fn a_hello_whose_lengths_do_not_add_up_is_refused() {
let hello = plain();
let extensions: usize = hello.extensions.iter().map(|(_, d)| 4 + d.len()).sum();
// The two bytes before the extensions are their total length.
let total_at = hello.message().len() - extensions - 2;
for delta in [1_u8, 255] {
let mut bent = hello.message();
bent[total_at + 1] = bent[total_at + 1].wrapping_add(delta);
assert_eq!(
inspect(&records(&bent, usize::MAX)),
Verdict::Malformed("ClientHello is malformed"),
"total off by {delta}"
);
}
// The session id claims more than 32 bytes.
let mut bent = hello.message();
bent[4 + 34] = 33;
assert_eq!(
inspect(&records(&bent, usize::MAX)),
Verdict::Malformed("ClientHello is malformed")
);
// The cipher suites list is odd.
let mut bent = hello.message();
bent[4 + 35 + 32 + 1] = 3;
assert!(matches!(
inspect(&records(&bent, usize::MAX)),
Verdict::Malformed(_)
));
}
#[test]
fn an_extension_that_overruns_the_hello_is_refused_even_before_an_ech_one() {
let mut hello = plain();
hello.extensions.insert(0, (0x15, vec![0; 4]));
hello.extensions.push(ech_extension(0xfe0d));
let mut message = hello.message();
// The first extension's length now claims more than the rest holds.
let first = message.len()
- hello
.extensions
.iter()
.map(|(_, d)| 4 + d.len())
.sum::<usize>();
message[first + 3] = 0xff;
assert!(matches!(
inspect(&records(&message, usize::MAX)),
Verdict::Malformed(_)
));
}
#[test]
fn bytes_after_the_hello_are_refused() {
let mut message = plain().message();
message.extend_from_slice(&[20, 0, 0, 0]);
assert_eq!(
inspect(&records(&message, usize::MAX)),
Verdict::Malformed("bytes after the ClientHello")
);
}
#[test]
fn pool_addresses_map_back_to_the_agent_that_made_the_flow() {
let agent = Ipv4Addr::new(10, 42, 0, 37);
let source = pool_source(agent);
assert_eq!(source, Ipv4Addr::new(198, 18, 0, 37));
assert!(in_pool(source));
assert!(!in_pool(agent));
assert!(same_host(source, agent));
// A different host of the subnet, and a lease that is itself in the pool.
assert!(!same_host(source, Ipv4Addr::new(10, 42, 0, 38)));
assert!(!same_host(source, source));
// An address outside the pool is only itself.
assert!(same_host(agent, agent));
assert!(!same_host(agent, Ipv4Addr::new(10, 42, 0, 38)));
// A /16 bridge keeps its two host bytes; the /15 pool has room for 17 bits.
assert_eq!(
pool_source(Ipv4Addr::new(10, 42, 1, 200)),
Ipv4Addr::new(198, 18, 1, 200)
);
assert_eq!(
pool_source(Ipv4Addr::new(10, 43, 255, 254)),
Ipv4Addr::new(198, 19, 255, 254)
);
assert!(!in_pool(Ipv4Addr::new(198, 20, 0, 1)));
assert!(!in_pool(Ipv4Addr::new(198, 17, 255, 255)));
}
#[test]
fn a_source_logs_once_a_second() {
let t0 = Instant::now();
let a = Ipv4Addr::new(10, 42, 0, 3);
let b = Ipv4Addr::new(10, 42, 0, 4);
let mut limit = LogLimit::default();
assert!(limit.allow(a, t0));
assert!(!limit.allow(a, t0 + Duration::from_millis(900)));
assert!(limit.allow(b, t0 + Duration::from_millis(900)));
assert!(limit.allow(a, t0 + Duration::from_millis(1001)));
}
#[test]
fn the_log_limiter_stays_bounded() {
let t0 = Instant::now();
let mut limit = LogLimit::default();
for n in 0..u32::try_from(LOG_TRACKED * 3).unwrap() {
limit.allow(Ipv4Addr::from_bits(0x0A00_0000 + n), t0);
}
assert!(limit.last.len() <= LOG_TRACKED);
}
#[test]
fn refusal_lines_name_the_agent_and_the_address() {
let dst = Ipv4Addr::new(203, 0, 113, 2);
assert_eq!(
refusal_line("hive-egress-ech-refused", Some("atlas"), dst, None),
"hive-egress-ech-refused agent=atlas dst=203.0.113.2"
);
assert_eq!(
refusal_line("hive-egress-hello-refused", None, dst, Some("x")),
"hive-egress-hello-refused agent=- dst=203.0.113.2 reason=x"
);
}
#[test]
fn marks_are_decimal_or_hex() {
assert_eq!(parse_mark("0x6563"), Some(0x6563));
assert_eq!(parse_mark("25955"), Some(25955));
assert_eq!(parse_mark("x"), None);
}
/// Both ends of a loopback connection.
async fn pair() -> (TcpStream, TcpStream) {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let near = TcpStream::connect(listener.local_addr().unwrap());
let (far, accepted) = tokio::join!(near, listener.accept());
(far.unwrap(), accepted.unwrap().0)
}
#[tokio::test]
async fn a_hello_that_arrives_in_pieces_is_read_whole() {
let mut hello = plain();
hello.extensions.push(ech_extension(0xfe0d));
let bytes = hello.record();
let (mut peer, mut server) = pair().await;
let send = tokio::spawn(async move {
for piece in bytes.chunks(37) {
peer.write_all(piece).await.unwrap();
tokio::time::sleep(Duration::from_millis(2)).await;
}
peer
});
let (seen, verdict) = read_hello(&mut server, Duration::from_secs(5))
.await
.unwrap();
assert_eq!(verdict, Verdict::Ech);
assert_eq!(seen, hello.record());
send.await.unwrap();
}
#[tokio::test]
async fn a_peer_that_stops_or_closes_early_gets_no_verdict() {
let record = plain().record();
let (mut peer, mut server) = pair().await;
peer.write_all(&record[..40]).await.unwrap();
// Still open and silent: the limit ends the wait.
let waited = tokio::time::timeout(
Duration::from_secs(5),
read_hello(&mut server, Duration::from_millis(200)),
)
.await;
assert_eq!(waited.ok(), Some(None), "the limit did not end the read");
// Closed with the hello cut off.
drop(peer);
assert!(
read_hello(&mut server, Duration::from_secs(5))
.await
.is_none()
);
// Control: the whole hello, sent at once, is read.
let (mut peer, mut server) = pair().await;
peer.write_all(&record).await.unwrap();
let (_, verdict) = read_hello(&mut server, Duration::from_secs(5))
.await
.unwrap();
assert_eq!(verdict, Verdict::Clean);
}
#[test]
fn a_log_agent_is_observed_and_every_other_agent_refused() {
let ech = Verdict::Ech;
assert_eq!(decide(&Verdict::Clean, false), Action::Forward);
assert_eq!(decide(&Verdict::Clean, true), Action::Forward);
assert_eq!(decide(&ech, false), Action::Refuse);
assert_eq!(decide(&ech, true), Action::ForwardLogged);
// A hello that does not parse is refused for a log agent too.
let bad = Verdict::Malformed("x");
assert_eq!(decide(&bad, true), Action::Refuse);
assert_eq!(decide(&bad, false), Action::Refuse);
}
#[test]
fn the_log_mode_is_what_the_squid_include_says() {
let allow = "# source: swarm\n\
acl agent_allow_atlas proxy_auth atlas\n\
acl agent_log_logan proxy_auth logan\n\
acl agent_log_logan_ix ext_user logan\n\
http_access allow !icpt agent_log_logan !allowed_dst log_mark\n";
assert!(is_log_agent(allow, "logan"));
// Control for the refusals: the same text names a restricted agent
// without a log rule, an agent whose name extends a log agent's, and
// the other ACL of the log agent.
assert!(!is_log_agent(allow, "atlas"));
assert!(!is_log_agent(allow, "log"));
assert!(!is_log_agent(allow, "logan2"));
assert!(!is_log_agent(allow, "logan_ix"));
assert!(!is_log_agent("", "logan"));
// The ACL must name the agent and nobody besides.
assert!(!is_log_agent(
"acl agent_log_logan proxy_auth logan other\n",
"logan"
));
}
fn shared(per_source: usize) -> Arc<Shared> {
Arc::new(Shared::new(1, per_source, None))
}
#[test]
fn a_source_at_its_cap_is_refused_while_another_still_gets_through() {
let relay = shared(2);
let open = Arc::new(Semaphore::new(100));
let (a, b) = (Ipv4Addr::new(10, 42, 0, 3), Ipv4Addr::new(10, 42, 0, 4));
let first = admit(&relay, &open, a);
let second = admit(&relay, &open, a);
assert!(first.is_some() && second.is_some(), "a is under its cap");
assert!(admit(&relay, &open, a).is_none(), "a is at its cap");
assert!(admit(&relay, &open, b).is_some(), "b is not");
// The slot is released when the connection ends, and not before.
drop(first);
let third = admit(&relay, &open, a);
assert!(third.is_some(), "a slot of a came free");
assert!(admit(&relay, &open, a).is_none(), "a is at its cap again");
drop((second, third));
assert!(
relay.open.lock().unwrap().get(&a).is_none(),
"nothing is kept for a closed source"
);
}
#[test]
fn the_relay_as_a_whole_is_capped_whatever_the_source() {
let relay = shared(10);
let open = Arc::new(Semaphore::new(2));
let (a, b, c) = (
Ipv4Addr::new(10, 42, 0, 3),
Ipv4Addr::new(10, 42, 0, 4),
Ipv4Addr::new(10, 42, 0, 5),
);
let held = (admit(&relay, &open, a), admit(&relay, &open, b));
assert!(held.0.is_some() && held.1.is_some());
assert!(admit(&relay, &open, c).is_none(), "the relay is full");
// A refusal for the relay's cap does not leave a slot taken for c.
assert!(relay.open.lock().unwrap().get(&c).is_none());
drop(held);
assert!(admit(&relay, &open, c).is_some());
}
#[tokio::test]
async fn other_sub_commands_are_not_ours() {
assert_eq!(run(&["egress-whois".to_owned()]).await, None);
assert_eq!(run(&[]).await, None);
}
}