eBPF Socket-Level Egress Auditing for Autonomous AI Agents: Enforcing Zero-Bypass Network Policies Outside the Python Process
Why Python-Level Network Guardrails Are Easily Bypassed
Most AI agent frameworks enforce network restrictions inside Python (if url.host in ALLOWED_HOSTS: httpx.get(url)).
If that same agent has access to a bash or run_python tool, an indirect prompt injection inside a cloned GitHub repo can bypass Python HTTP wrappers trivially:
- Calling
curlorwgetdirectly in a subshell. - Opening raw
socket.create_connection(("169.254.169.254", 80))to steal AWS IMDSv1/v2 metadata credentials! - Clearing
HTTP_PROXYenvironment variables inside the child process.
To make egress controls unbypassable, enforcement must live below the process boundary—inside the Linux kernel via eBPF.
Architecture: cgroup/connect4 Kernel Gatekeeper
By launching each agent sandbox inside its own cgroup v2 slice (/sys/fs/cgroup/agent_sandbox_<id>) and attaching an eBPF BPF_PROG_TYPE_CGROUP_SOCK_ADDR hook on connect4, the kernel intercepts every IPv4 connect() syscall—whether from Python, Node, curl, or compiled C binaries:
text[Agent Sandbox (cgroup v2: /agent_42)] ├── python -> httpx.get(...) ├── bash -> curl https://evil.io └── bin -> raw socket(169.254.169.254:80) │ ▼ (Linux Kernel Syscall Boundary) [eBPF Program: cgroup/connect4] ├── Block 169.254.169.254 (Cloud Metadata) -> EPERM (-1) ├── Check BPF_MAP_TYPE_HASH (Allowed IPs) └── Emit RingBuffer Audit Event -> Userspace Telemetry Daemon
Minimal eBPF C Program (agent_egress_guard.bpf.c)
c#include <linux/bpf.h> #include <bpf/bpf_helpers.h> #include <bpf/bpf_endian.h> // Map populated by control plane with sanctioned registry/API IPv4s struct { __uint(type, BPF_MAP_TYPE_HASH); __uint(max_entries, 1024); __type(key, __u32); // IPv4 in network byte order __type(value, __u8); // 1 = allowed } allowed_egress_ips SEC(".maps"); SEC("cgroup/connect4") int enforce_agent_egress(struct bpf_sock_addr *ctx) { __u32 dst_ip = ctx->user_ip4; // 1. Hard-block Cloud Metadata IP (169.254.169.254 -> 0xFEA9FEA9 LE) if (dst_ip == bpf_htonl(0xA9FEA9FE)) { return 0; // Kernel returns -EPERM immediately! } // 2. Allow loopback (127.0.0.1) for local MCP IPC if (dst_ip == bpf_htonl(0x7F000001)) { return 1; } // 3. Enforce explicit IP allowlist __u8 *allowed = bpf_map_lookup_elem(&allowed_egress_ips, &dst_ip); if (!allowed) { return 0; // Block unauthorized outbound connection } return 1; } char _license[] SEC("license") = "GPL";
Why Kernel-Level eBPF Beats Proxy Sidecars
- <200 nanoseconds overhead per
connect()syscall (vs milliseconds of TCP proxy hopping). - Zero escape surface: Subshells, static Go binaries, and
LD_PRELOADtricks cannot bypasscgroup/connect4. - Full Auditability: Every blocked or permitted destination IP emits directly to a lock-free
BPF_MAP_TYPE_RINGBUFfor SOC 2 security logs.