From e3cd65cb79884ae3c246ca807f214779ffe1611b Mon Sep 17 00:00:00 2001 From: Adeeb Rahman Date: Tue, 24 Nov 2026 08:34:40 -0500 Subject: [PATCH] Build guest UDP endpoints through Winsock address conversion Signed-off-by: Adeeb Rahman --- crates/loader/src/services/winsock.rs | 57 +++++++++++++++++++++++++ crates/loader/tests/runtime_services.rs | 26 +++++++++-- 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/crates/loader/src/services/winsock.rs b/crates/loader/src/services/winsock.rs index 3662755..030e530 100644 --- a/crates/loader/src/services/winsock.rs +++ b/crates/loader/src/services/winsock.rs @@ -175,6 +175,7 @@ pub(super) fn register(registry: &mut ImportRegistry, state: &Arc Ok(0) }); + register_address_conversion(registry, state); register_loopback(registry, state); let get = Arc::clone(state); @@ -191,6 +192,62 @@ pub(super) fn register(registry: &mut ImportRegistry, state: &Arc } } +fn register_address_conversion(registry: &mut ImportRegistry, state: &Arc) { + for symbol in ["htons", "ntohs"] { + registry.register(MODULE, symbol, |args, _, _| { + Ok(u64::from((args.rcx as u16).swap_bytes())) + }); + } + for symbol in ["htonl", "ntohl"] { + registry.register(MODULE, symbol, |args, _, _| { + Ok(u64::from((args.rcx as u32).swap_bytes())) + }); + } + let state = Arc::clone(state); + registry.register_with_status( + MODULE, + "inet_pton", + ServiceStatus::Partial, + move |args, cpu, memory| { + let fail = |error| state.fail(cpu.gs_base, error, SOCKET_ERROR); + if args.rcx as u32 != 2 { + return fail(WSAEAFNOSUPPORT); + } + if args.rdx == 0 || args.r8 == 0 { + return fail(WSAEFAULT); + } + // The longest dotted decimal IPv4 spelling is 15 bytes plus NUL. + let mut text = [0; 15]; + for len in 0..=text.len() { + let Some(address) = args.rdx.checked_add(len as u64) else { + return fail(WSAEFAULT); + }; + let Ok(byte) = memory.read_u8(address) else { + return fail(WSAEFAULT); + }; + if byte == 0 { + let Some(ip) = std::str::from_utf8(&text[..len]) + .ok() + .and_then(|text| text.parse::().ok()) + else { + return Ok(0); + }; + return if memory.write_bytes(args.r8, &ip.octets()).is_ok() { + Ok(1) + } else { + fail(WSAEFAULT) + }; + } + if len == text.len() { + break; + } + text[len] = byte; + } + Ok(0) + }, + ); +} + fn register_loopback(registry: &mut ImportRegistry, state: &Arc) { for (symbol, connect) in [("bind", false), ("connect", true)] { let state = Arc::clone(state); diff --git a/crates/loader/tests/runtime_services.rs b/crates/loader/tests/runtime_services.rs index 7855e52..f262e77 100644 --- a/crates/loader/tests/runtime_services.rs +++ b/crates/loader/tests/runtime_services.rs @@ -337,6 +337,8 @@ fn guest_udp_loopback_delivers_datagrams_through_bound_imports() { "recv", "WSAGetLastError", "WSACleanup", + "inet_pton", + "htons", ]; let imports = symbols.map(|symbol| ("ws2_32.dll", Symbol::Name(symbol))); let image = build(&imports); @@ -373,15 +375,33 @@ fn guest_udp_loopback_delivers_datagrams_through_bound_imports() { let address = SCRATCH + 0x200; let outgoing = SCRATCH + 0x220; let incoming = SCRATCH + 0x240; + let text = SCRATCH + 0x260; + memory.write_u16(address, 2).unwrap(); + let port = call(&mut memory, "htons", &[5000]); + memory.write_u16(address + 2, port as u16).unwrap(); + memory.write_bytes(text, b"127.0.0.1\0").unwrap(); + assert_eq!(call(&mut memory, "inet_pton", &[2, text, address + 4]), 1); + assert_eq!( + memory.read_u32(address + 4).unwrap().to_le_bytes(), + [127, 0, 0, 1] + ); + memory.write_bytes(text, b"127.0.0.999\0").unwrap(); + assert_eq!(call(&mut memory, "inet_pton", &[2, text, address + 4]), 0); + assert_eq!( + memory.read_u32(address + 4).unwrap().to_le_bytes(), + [127, 0, 0, 1] + ); + assert_eq!( + call(&mut memory, "inet_pton", &[23, text, address + 4]), + u64::from(u32::MAX) + ); + assert_eq!(call(&mut memory, "WSAGetLastError", &[]), 10047); assert_eq!(call(&mut memory, "WSAStartup", &[0x0202, SCRATCH]), 0); let receiver = call(&mut memory, "socket", &[2, 2, 17]); let sender = call(&mut memory, "socket", &[2, 2, 17]); assert_ne!(receiver, u64::MAX); assert_ne!(sender, u64::MAX); assert_ne!(sender, receiver); - let mut endpoint = [0; 16]; - endpoint[..8].copy_from_slice(&[2, 0, 0x13, 0x88, 127, 0, 0, 1]); - memory.write_bytes(address, &endpoint).unwrap(); assert_eq!(call(&mut memory, "bind", &[receiver, address, 16]), 0); assert_eq!(call(&mut memory, "connect", &[sender, address, 16]), 0); memory.write_bytes(outgoing, b"loop").unwrap();