Skip to content

Commit 3873fa4

Browse files
committed
fix: accept unimplemented socket options instead of erroring
The 0.2.0 setsockopt/getsockopt hardening changed silent no-ops into errors for unrecognised options. This broke DNS resolution because musl's resolver sets IP_RECVERR (level=0, opt=11) on its UDP socket and aborts when the setsockopt fails. Revert to accepting unknown options silently — for a socket-proxy pattern the guest OS manages its own socket semantics, so unknown options are harmless configuration hints, not security boundaries. Signed-off-by: danbugs <danilochiarlone@gmail.com>
1 parent cb048d7 commit 3873fa4

1 file changed

Lines changed: 19 additions & 28 deletions

File tree

host/src/lib.rs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,21 +1176,17 @@ fn register_net_tools(
11761176
let value = args["value"].as_i64().unwrap_or(0) as i32;
11771177
let tbl = t.lock().unwrap();
11781178
let sock = tbl.get_socket(fd)?;
1179-
// SOL_SOCKET=1, SO_REUSEADDR=2
1180-
if level == 1 && optname == 2 {
1181-
sock.set_reuse_address(value != 0)?;
1182-
// SOL_SOCKET=1, SO_KEEPALIVE=9
1183-
} else if level == 1 && optname == 9 {
1184-
sock.set_keepalive(value != 0)?;
1185-
// IPPROTO_TCP=6, TCP_NODELAY=1
1186-
} else if level == 6 && optname == 1 {
1187-
sock.set_nodelay(value != 0)?;
1188-
} else {
1189-
return Err(anyhow!(
1190-
"unsupported socket option: level={}, optname={}",
1191-
level,
1192-
optname
1193-
));
1179+
match (level, optname) {
1180+
// SOL_SOCKET(1), SO_REUSEADDR(2)
1181+
(1, 2) => sock.set_reuse_address(value != 0)?,
1182+
// SOL_SOCKET(1), SO_KEEPALIVE(9)
1183+
(1, 9) => sock.set_keepalive(value != 0)?,
1184+
// IPPROTO_TCP(6), TCP_NODELAY(1)
1185+
(6, 1) => sock.set_nodelay(value != 0)?,
1186+
// Silently accepted — the dispatch round-trip makes
1187+
// guest-side timeouts and error-reporting opts
1188+
// counterproductive; the guest's own retry logic suffices.
1189+
_ => {}
11941190
}
11951191
Ok(json!({}))
11961192
});
@@ -1203,19 +1199,14 @@ fn register_net_tools(
12031199
let optname = args["optname"].as_i64().unwrap_or(0) as i32;
12041200
let tbl = t.lock().unwrap();
12051201
let sock = tbl.get_socket(fd)?;
1206-
let val: i32 = if level == 1 && optname == 3 {
1207-
// SOL_SOCKET + SO_TYPE — return the actual socket type
1208-
tbl.get_sock_type(fd)?
1209-
} else if level == 1 && optname == 2 {
1210-
sock.reuse_address()? as i32
1211-
} else if level == 6 && optname == 1 {
1212-
sock.nodelay()? as i32
1213-
} else {
1214-
return Err(anyhow!(
1215-
"unsupported socket option: level={}, optname={}",
1216-
level,
1217-
optname
1218-
));
1202+
let val: i32 = match (level, optname) {
1203+
// SOL_SOCKET(1), SO_TYPE(3)
1204+
(1, 3) => tbl.get_sock_type(fd)?,
1205+
// SOL_SOCKET(1), SO_REUSEADDR(2)
1206+
(1, 2) => sock.reuse_address()? as i32,
1207+
// IPPROTO_TCP(6), TCP_NODELAY(1)
1208+
(6, 1) => sock.nodelay()? as i32,
1209+
_ => 0,
12191210
};
12201211
Ok(json!({ "value": val }))
12211212
});

0 commit comments

Comments
 (0)