Skip to content

Commit bff230a

Browse files
ammar-agentClaude
andcommitted
feat: transparent DNS interception via in-namespace server
Implements transparent DNS interception in Linux strong jails to prevent DNS exfiltration via subdomain encoding attacks. ## Core Implementation - **ForkedDnsProcess**: Manages child process running DummyDnsServer inside network namespace with proper privilege dropping and cleanup - **DummyDnsServer**: Returns 6.6.6.6 for all A record queries using simple-dns library - **DNS Redirection**: nftables rules redirect all UDP port 53 traffic to in-namespace DNS server (6.6.6.6) - **IPv6 Support**: Separate ip6 table with DNS DNAT to ::1 and blocks other IPv6 egress ## Security Features - DNS server drops privileges to nobody:nogroup (UID/GID 65534) after binding port 53 - Uses PR_SET_PDEATHSIG(SIGTERM) to ensure DNS server terminates with parent - Closes all inherited file descriptors (3-1024) to avoid tokio runtime issues - Prevents DNS exfiltration - external DNS servers (1.1.1.1, 8.8.8.8) are unreachable from jail ## Dependencies - Added simple-dns = "0.7" for DNS packet parsing - Added nix features: process, fs, sched, user, signal (Linux-only) - Added command_utils module for command execution with timeout handling ## Testing - Fixed #![allow(deprecated)] placement in test files (must be line 1) - Fixed test_weak_mode_allows_localhost to accept exit code 28 (timeout) in addition to 7 (connection refused) and 52 (empty reply) since the proxy may timeout when trying to connect to localhost:80 ## Platform Support - Linux: Full DNS interception via network namespaces (strong jail) - macOS: Weak mode only (no DNS changes needed) Co-authored-by: Claude <assistant@anthropic.com>
1 parent 574ca02 commit bff230a

18 files changed

Lines changed: 1354 additions & 890 deletions

CLAUDE.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,51 @@ on both targets.
132132

133133
After modifying code, run `cargo fmt` to ensure consistent formatting before committing changes.
134134

135+
## System Resource Cleanup
136+
137+
**CRITICAL: All global system resources MUST be properly cleaned up to prevent resource leaks.**
138+
139+
### Linux System Resources
140+
141+
The following system resources are created for each jail and MUST be cleaned up:
142+
143+
1. **Network Namespace** (`NetworkNamespace`) - `/var/run/netns/httpjail_<jail_id>`
144+
2. **Virtual Ethernet Pairs** (`VethPair`) - `veth_host_<jail_id>` and `veth_ns_<jail_id>`
145+
3. **NFTables Rules** (`NFTable`) - iptables/nftables rules for traffic redirection
146+
4. **DNS Server Process** (`ForkedDnsProcess`) - Child process running in namespace
147+
5. **Any namespace-specific configuration** - e.g., `/etc/netns/<namespace>` if created
148+
149+
### Cleanup Mechanisms
150+
151+
1. **Normal Exit**: Resources implement `Drop` trait for automatic cleanup
152+
2. **Orphan Cleanup**: `cleanup_orphaned()` handles resources from crashed instances
153+
3. **Process Cleanup**: Must kill ALL processes in namespace before deletion
154+
4. **Order Matters**: Clean processes first, then network resources, then namespace
155+
156+
### Implementation Requirements
157+
158+
When adding new system resources:
159+
- Implement `SystemResource` trait with proper `cleanup()` method
160+
- Add to `cleanup_orphaned()` for crash recovery
161+
- Ensure `Drop` implementation for normal cleanup
162+
- Test with `--no-jail-cleanup` flag to verify cleanup works
163+
- Use `ManagedResource<T>` wrapper for automatic cleanup on drop
164+
165+
### Testing Cleanup
166+
167+
```bash
168+
# Test orphan cleanup
169+
sudo ./target/debug/httpjail --js "true" -- sleep 100 &
170+
PID=$!
171+
sudo kill -9 $PID # Simulate crash
172+
sudo ./target/debug/httpjail --cleanup # Should clean up orphaned resources
173+
174+
# Verify no resources left
175+
ip netns list | grep httpjail # Should be empty
176+
ip link show | grep veth_ # Should show no jail veths
177+
sudo iptables -L -t nat | grep httpjail # Should show no jail rules
178+
```
179+
135180
## Logging
136181

137182
In regular operation of the CLI-only jail (non-server mode), info and warn logs are not permitted as they would interfere with the underlying process output. Only use debug level logs for normal operation and error logs for actual errors. The server mode (`--server`) may use info/warn logs as appropriate since it has no underlying process.

0 commit comments

Comments
 (0)