Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions context/interfaces/src/proxy/mod.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
- Resolving mesh names from trusted derivation state.
- Intercepting local traffic.
- Proxying connections over Intermesh mTLS.
- Owning `/etc/resolv.conf` while running: snapshotting the original,
installing a managed file (`ndots:0`) so libc does not search-expand short
mesh names, and restoring on shutdown.

## Public interface
```rust
Expand Down
42 changes: 41 additions & 1 deletion src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use std::convert::Infallible;
use std::net::{IpAddr, Ipv4Addr};
use std::path::PathBuf;
use std::sync::Arc;

use anyhow::{Context, Result};
Expand Down Expand Up @@ -38,12 +39,19 @@ use crate::verifier::IntermeshVerifier;

mod dns;
mod intercept;
mod resolv_conf;

use dns::{create_resolver, Dns};
use intercept::{
bind_dns, bind_tcp, get_original_dst, nftables_clean, nftables_inject, EXTERNAL_PORT,
PROXY_PORT,
};
use resolv_conf::ResolvConf;

/// Default location of the host's resolver config.
const RESOLV_CONF_PATH: &str = "/etc/resolv.conf";
/// Filename of the on-disk snapshot, placed alongside the daemon state file.
const RESOLV_CONF_SNAPSHOT: &str = "resolv.conf.orig";

// ============================================================================
// Proxy
Expand Down Expand Up @@ -83,8 +91,15 @@ impl Handle {
/// enabled, creates local service for host namespace interception and
/// spawns HTTP CONNECT listener for incoming mTLS from remote peers.
pub(crate) async fn run(self, cancel: CancellationToken) -> Result<()> {
// Clean up any stale rules from a previous run.
// Clean up any stale state from a previous run.
nftables_clean();
let resolv_conf = make_resolv_conf(&self.state)?;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-generated, human-unreviewed, non-binding: make_resolv_conf(&self.state)? now runs before the !self.state.intercept early-return path, so a non-intercepting proxy can fail due to resolv.conf snapshot path setup even though it will never manage /etc/resolv.conf. Moving this below the intercept check keeps the non-intercept mode behavior unchanged.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-generated -- a later change on this branch added resolv_conf.recover() immediately
after make_resolv_conf and before the intercept gate, so it's no longer dead weight
recover() restores a managed /etc/resolv.conf left by a crashed prior run and intentionally runs
cross-mode: if a previous intercept run crashed (leaving nameserver 127.0.0.1) and the operator
restarts without intercept, moving this below the gate would strand the host's DNS on a dead
127.0.0.1 until a future intercept run. The cited failure only occurs if state_file has no
parent (path /), which would break the daemon everywhere else anyway.

// A prior run may have crashed with our managed file still installed.
// Restore the real upstream config before create_resolver() reads it,
// otherwise the resolver would forward queries to our own loopback
// nameserver. Runs regardless of intercept so a stale managed file
// never outlives the proxy.
resolv_conf.recover().context("recover resolv.conf")?;

if !self.state.intercept {
cancel.cancelled().await;
Expand All @@ -96,7 +111,21 @@ impl Handle {
let tcp_sock = bind_tcp(Ipv4Addr::LOCALHOST, PROXY_PORT).context("bind proxy socket")?;
let ext_sock =
bind_tcp(Ipv4Addr::UNSPECIFIED, EXTERNAL_PORT).context("bind external socket")?;
// Read upstream nameservers from /etc/resolv.conf *before*
// resolv_conf.install() overwrites the file. Otherwise we'd read back
// our own `nameserver 127.0.0.1` and forward queries to ourselves.
let resolver = create_resolver();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-generated, human-unreviewed, non-binding: The normal-path ordering avoids reading the managed nameserver 127.0.0.1, but crash recovery still has that failure mode. If a prior run crashed after installing the managed file, /etc/resolv.conf still has the sentinel and loopback nameserver; on restart this create_resolver() runs before install() restores the snapshot, so the upstream resolver can be configured to forward back to the proxy itself. Consider splitting “recover prior managed file” out so it runs before resolver construction, then install the managed file after the real upstream config has been captured.

// Defer order matters: scopeguard fires LIFO. Register the resolv.conf
// restore first so nftables_clean (registered second) fires first on
// shutdown — global system rules drop ASAP, local file restore after.
resolv_conf

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-generated, human-unreviewed, non-binding: The restore guard is only registered after install() returns successfully. If install() snapshots the original and then fails while/after mutating /etc/resolv.conf (for example fs::write succeeds but the later chmod fails), Handle::run returns an error without restoring the global resolver file. Since this touches host-global state, install() should either roll back internally on post-snapshot failures or the guard should be active before the mutation begins.

.install()
.context("install managed resolv.conf")?;
defer!({
if let Err(e) = resolv_conf.restore() {
error!("restore resolv.conf failed: {e:#}");
}
});
nftables_inject().context("inject nftables rules")?;
defer!(nftables_clean());

Expand Down Expand Up @@ -322,6 +351,17 @@ impl Handle {
// Utilities
// ============================================================================

/// Construct a `ResolvConf` for production use, with the snapshot placed in
/// the same directory as the daemon state file.
fn make_resolv_conf(state: &State) -> Result<ResolvConf> {
let snapshot = state
.state_file
.parent()
.context("state file has no parent directory")?
.join(RESOLV_CONF_SNAPSHOT);
Ok(ResolvConf::new(PathBuf::from(RESOLV_CONF_PATH), snapshot))
}

/// Parse the target from an HTTP CONNECT request URI authority.
fn parse_connect_target(uri: &Uri) -> Result<(Name, u16)> {
let authority = uri.authority().context("missing authority")?.as_str();
Expand Down
Loading
Loading