Skip to content

Commit 2099cee

Browse files
committed
fix(sandbox): fall back to /proc/net/tcp for peer identity resolution (NVIDIA#681)
On Docker Desktop with WSL2 (amd64), iptables REDIRECT/DNAT connections from the sandbox network namespace do not appear in per-PID /proc/<pid>/net/tcp — they only appear in the global /proc/net/tcp. This caused parse_proc_net_tcp() to fail, preventing the proxy from identifying the calling binary. With no binary identity, no network policy matched, and OPA denied all CONNECT and forward proxy requests. Add /proc/net/tcp{,6} as a fallback when the per-PID table does not contain the peer port. The per-PID path is tried first (most accurate), preserving existing behavior on platforms where it works. Signed-off-by: David Peden <[email protected]>
1 parent c1dd81e commit 2099cee

1 file changed

Lines changed: 45 additions & 31 deletions

File tree

‎crates/openshell-sandbox/src/procfs.rs‎

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ pub fn collect_cmdline_paths(pid: u32, stop_pid: u32, exclude: &[PathBuf]) -> Ve
163163
/// Checks both IPv4 and IPv6 tables because some clients (notably gRPC C-core)
164164
/// use `AF_INET6` sockets with IPv4-mapped addresses even for IPv4 connections.
165165
///
166+
/// Falls back to `/proc/net/tcp{,6}` (the init-namespace global view) when the
167+
/// per-PID tables do not contain the connection. This handles container runtimes
168+
/// (notably Docker Desktop on WSL2) where iptables REDIRECT/DNAT from the
169+
/// sandbox network namespace creates connections that are visible in the global
170+
/// table but not in `/proc/<pid>/net/tcp`.
171+
///
166172
/// Format of `/proc/net/tcp`:
167173
/// ```text
168174
/// sl local_address rem_address st tx_queue:rx_queue ... inode
@@ -173,48 +179,56 @@ pub fn collect_cmdline_paths(pid: u32, stop_pid: u32, exclude: &[PathBuf]) -> Ve
173179
/// - Inode is field index 9 (0-indexed)
174180
#[cfg(target_os = "linux")]
175181
fn parse_proc_net_tcp(pid: u32, peer_port: u16) -> Result<u64> {
176-
// Check IPv4 first (most common), then IPv6.
177-
for suffix in &["tcp", "tcp6"] {
178-
let path = format!("/proc/{pid}/net/{suffix}");
179-
let Ok(content) = std::fs::read_to_string(&path) else {
180-
continue;
181-
};
182-
183-
for line in content.lines().skip(1) {
184-
let fields: Vec<&str> = line.split_whitespace().collect();
185-
if fields.len() < 10 {
182+
// Try per-PID first (most accurate), then fall back to global.
183+
let pid_prefixes: &[String] = &[
184+
format!("/proc/{pid}"),
185+
"/proc".to_string(),
186+
];
187+
188+
for prefix in pid_prefixes {
189+
for suffix in &["net/tcp", "net/tcp6"] {
190+
let path = format!("{prefix}/{suffix}");
191+
let Ok(content) = std::fs::read_to_string(&path) else {
186192
continue;
187-
}
188-
189-
// Parse local_address to extract port.
190-
// IPv4 format: AABBCCDD:PORT
191-
// IPv6 format: 00000000000000000000000000000000:PORT
192-
let local_addr = fields[1];
193-
let local_port = match local_addr.rsplit_once(':') {
194-
Some((_, port_hex)) => u16::from_str_radix(port_hex, 16).unwrap_or(0),
195-
None => continue,
196193
};
197194

198-
// Check state is ESTABLISHED (01)
199-
let state = fields[3];
200-
if state != "01" {
201-
continue;
202-
}
195+
for line in content.lines().skip(1) {
196+
let fields: Vec<&str> = line.split_whitespace().collect();
197+
if fields.len() < 10 {
198+
continue;
199+
}
203200

204-
if local_port == peer_port {
205-
let inode: u64 = fields[9]
206-
.parse()
207-
.map_err(|_| miette::miette!("Failed to parse inode from {}", fields[9]))?;
208-
if inode == 0 {
201+
// Parse local_address to extract port.
202+
// IPv4 format: AABBCCDD:PORT
203+
// IPv6 format: 00000000000000000000000000000000:PORT
204+
let local_addr = fields[1];
205+
let local_port = match local_addr.rsplit_once(':') {
206+
Some((_, port_hex)) => u16::from_str_radix(port_hex, 16).unwrap_or(0),
207+
None => continue,
208+
};
209+
210+
// Check state is ESTABLISHED (01)
211+
let state = fields[3];
212+
if state != "01" {
209213
continue;
210214
}
211-
return Ok(inode);
215+
216+
if local_port == peer_port {
217+
let inode: u64 = fields[9]
218+
.parse()
219+
.map_err(|_| miette::miette!("Failed to parse inode from {}", fields[9]))?;
220+
if inode == 0 {
221+
continue;
222+
}
223+
return Ok(inode);
224+
}
212225
}
213226
}
214227
}
215228

216229
Err(miette::miette!(
217-
"No ESTABLISHED TCP connection found for port {} in /proc/{}/net/tcp{{,6}}",
230+
"No ESTABLISHED TCP connection found for port {} in /proc/{}/net/tcp{{,6}} \
231+
or /proc/net/tcp{{,6}}",
218232
peer_port,
219233
pid
220234
))

0 commit comments

Comments
 (0)