mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 03:39:52 +00:00
Build guest UDP endpoints through Winsock address conversion
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
This commit is contained in:
1 parent
731f9448c3
commit
e3cd65cb79
2 files changed
+80
-3
No files matched your search
@@ -175,6 +175,7 @@ pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>
|
||||
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<ServicesState>
|
||||
}
|
||||
}
|
||||
|
||||
fn register_address_conversion(registry: &mut ImportRegistry, state: &Arc<ServicesState>) {
|
||||
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::<std::net::Ipv4Addr>().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<ServicesState>) {
|
||||
for (symbol, connect) in [("bind", false), ("connect", true)] {
|
||||
let state = Arc::clone(state);
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user