XCL_NETWORK=0: ConnectEx still connects (main a6f5f7a) #444

Open
opened 2026-11-26 04:22:00 +00:00 by github-bot · 0 comments
Owner

@⁠Kalmarv on GitHub · 2026-11-24 00:22 UTC · original

Follow-up to #305 (closed, fixed in db1ddb2). On main a6f5f7a, with the system hooks installed, a socket still connects through ConnectEx. connect to the same address is refused.

ConnectEx is not an export: a caller gets its address from WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER), so none of the 18 hooks is on its path. Code that connects on a completion port uses it.

A scratch test added to hle/netoff.rs calls each function before and after install_system_hooks(). Nothing leaves the machine: the listener is on 127.0.0.1 and is reached as ::ffff:127.0.0.1, an address the hooks do not count as loopback, so the hooked connect refuses it like any other host. "REACHED" means the listener accepted a connection (or, for a lookup, an address came back).

                               before the hooks                 with the hooks
connect                       r=0 err=0 REACHED         r=-1 err=10051 refused
ConnectEx                     r=1 err=0 REACHED              r=1 err=0 REACHED
WSAConnectByNameW             r=1 err=0 REACHED          r=0 err=11001 refused
getaddrinfo                   r=0 err=0 REACHED          r=11001 err=0 refused
GetAddrInfoExW                r=0 err=0 REACHED          r=11001 err=0 refused
GetAddrInfoExA                r=0 err=0 REACHED          r=11001 err=0 refused
past the XCL_NETWORK=0 hooks: ["ConnectEx"]
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 657 filtered out
  • WSAConnectByNameW and GetAddrInfoExA are not hooked either, but both are refused: each logs XCL_NETWORK=0: GetAddrInfoExW fails (no network), so they go through the hooked GetAddrInfoExW.
  • The lookups stay refused, so a ConnectEx caller that needs a name from one of the hooked lookups gets no address. What is open is a connection to an address it already has.
  • A possible fix: hook WSAIoctl and answer WSAID_CONNECTEX with a function that refuses as connect does.
The test (appended to crates/xcl/src/hle/netoff.rs)
/// Do ConnectEx, WSAConnectByNameW and GetAddrInfoExA get past the system hooks? Nothing leaves
/// this machine: the listener is on 127.0.0.1 and is reached through the IPv4-mapped address
/// ::ffff:127.0.0.1, which `loopback` does not let through – the hooked `connect` refuses it
/// as it refuses any other host. The lookups are of a numeric address with AI_NUMERICHOST
/// (no DNS query), which the hooked lookups refuse like any name that is not local.
#[cfg(test)]
mod gap_tests {
    use super::*;
    use std::ptr::{null, null_mut};

    #[repr(C)]
    struct FdSet {
        count: u32,
        array: [usize; 64],
    }

    #[link(name = "ws2_32", kind = "raw-dylib")]
    extern "system" {
        fn WSAStartup(version: u16, data: *mut u8) -> i32;
        fn WSAGetLastError() -> i32;
        fn WSACreateEvent() -> usize;
        fn socket(af: i32, ty: i32, proto: i32) -> usize;
        fn bind(s: usize, name: *const u8, len: i32) -> i32;
        fn listen(s: usize, backlog: i32) -> i32;
        fn accept(s: usize, addr: *mut u8, len: *mut i32) -> usize;
        fn getsockname(s: usize, name: *mut u8, len: *mut i32) -> i32;
        fn setsockopt(s: usize, level: i32, name: i32, val: *const u8, len: i32) -> i32;
        fn closesocket(s: usize) -> i32;
        fn select(n: i32, r: *mut FdSet, w: *mut FdSet, e: *mut FdSet, timeout: *const [i32; 2]) -> i32;
        fn WSAIoctl(s: usize, code: u32, inbuf: *const u8, inlen: u32, outbuf: *mut u8, outlen: u32, ret: *mut u32, ov: *mut c_void, cb: *const c_void) -> i32;
        fn WSAGetOverlappedResult(s: usize, ov: *const c_void, bytes: *mut u32, wait: i32, flags: *mut u32) -> i32;
        fn WSAConnectByNameW(s: usize, node: *const u16, service: *const u16, local_len: *mut u32, local: *mut u8, remote_len: *mut u32, remote: *mut u8, timeout: *const c_void, reserved: *mut c_void) -> i32;
        fn GetAddrInfoExA(name: *const u8, service: *const u8, ns: u32, id: *const c_void, hints: *const c_void, out: *mut *mut c_void, timeout: *const c_void, ov: *mut c_void, cb: *const c_void, handle: *mut c_void) -> i32;
        fn GetAddrInfoExW(name: *const u16, service: *const u16, ns: u32, id: *const c_void, hints: *const c_void, out: *mut *mut c_void, timeout: *const c_void, ov: *mut c_void, cb: *const c_void, handle: *mut c_void) -> i32;
    }

    /// an IPv6 socket that also reaches IPv4 (IPV6_V6ONLY off)
    unsafe fn dual() -> usize {
        let s = socket(23, 1, 6);
        assert_ne!(s, usize::MAX);
        let off = 0u32;
        assert_eq!(setsockopt(s, 41, 27, &off as *const u32 as *const u8, 4), 0);
        s
    }

    /// whether a connection arrived at the listener within a second (it is accepted and closed)
    unsafe fn arrived(listener: usize) -> bool {
        let mut r = FdSet { count: 1, array: [0; 64] };
        r.array[0] = listener;
        if select(0, &mut r, null_mut(), null_mut(), &[1, 0]) != 1 {
            return false;
        }
        let c = accept(listener, null_mut(), null_mut());
        closesocket(c);
        c != usize::MAX
    }

    /// (function, its result, the error after it, whether a connection arrived / an address came back)
    unsafe fn probe(listener: usize, to: &[u8; 28], port: u16) -> Vec<(&'static str, i32, i32, bool)> {
        let mut v = Vec::new();

        // connect: one of the hooked functions, the control
        let s = dual();
        let r = real_connect(s, to.as_ptr(), 28);
        v.push(("connect", r, WSAGetLastError(), arrived(listener)));
        closesocket(s);

        // ConnectEx, found the only way there is: WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER)
        let s = dual();
        let mut local = [0u8; 28];
        local[0] = 23;
        local[18..24].copy_from_slice(&[0xff, 0xff, 127, 0, 0, 1]);
        assert_eq!(bind(s, local.as_ptr(), 28), 0, "ConnectEx needs a bound socket");
        let wsaid_connectex: [u8; 16] = [0xb9, 0x07, 0xa2, 0x25, 0xf3, 0xdd, 0x60, 0x46, 0x8e, 0xe9, 0x76, 0xe5, 0x8c, 0x74, 0x06, 0x3e];
        let (mut f, mut n) = (0usize, 0u32);
        assert_eq!(WSAIoctl(s, 0xC800_0006, wsaid_connectex.as_ptr(), 16, &mut f as *mut usize as *mut u8, 8, &mut n, null_mut(), null()), 0);
        let connect_ex: unsafe extern "system" fn(usize, *const u8, i32, *const c_void, u32, *mut u32, *mut c_void) -> i32 = std::mem::transmute(f);
        let mut ov = [0usize; 4];
        ov[3] = WSACreateEvent();
        let mut r = connect_ex(s, to.as_ptr(), 28, null(), 0, null_mut(), ov.as_mut_ptr() as *mut c_void);
        let mut e = WSAGetLastError();
        if r == 0 && e == 997 {
            let mut flags = 0u32;
            r = WSAGetOverlappedResult(s, ov.as_ptr() as *const c_void, &mut n, 1, &mut flags);
            e = if r != 0 { 0 } else { WSAGetLastError() };
        }
        v.push(("ConnectEx", r, e, arrived(listener)));
        closesocket(s);

        // WSAConnectByNameW: looks the name up and connects, in one call
        let s = dual();
        let r = WSAConnectByNameW(s, crate::util::wide("::ffff:127.0.0.1").as_ptr(), crate::util::wide(&port.to_string()).as_ptr(), null_mut(), null_mut(), null_mut(), null_mut(), null(), null_mut());
        v.push(("WSAConnectByNameW", r, if r != 0 { 0 } else { WSAGetLastError() }, arrived(listener)));
        closesocket(s);

        // the lookups: 192.0.2.1 as a numeric address (AI_NUMERICHOST = 4: no DNS query)
        let mut hints = [0u8; 72];
        hints[0] = 4;
        let h = hints.as_ptr() as *const c_void;
        let mut out = null_mut();
        let r = real_getaddrinfo(b"192.0.2.1\0".as_ptr(), null(), h, &mut out);
        v.push(("getaddrinfo", r, 0, !out.is_null()));
        out = null_mut();
        let r = GetAddrInfoExW(crate::util::wide("192.0.2.1").as_ptr(), null(), 0, null(), h, &mut out, null(), null_mut(), null(), null_mut());
        v.push(("GetAddrInfoExW", r, 0, !out.is_null()));
        out = null_mut();
        let r = GetAddrInfoExA(b"192.0.2.1\0".as_ptr(), null(), 0, null(), h, &mut out, null(), null_mut(), null(), null_mut());
        // ADDRINFOEXA: ai_addr is at 32; a sockaddr_in has its address at 4
        let got = !out.is_null() && {
            let addr = *((out as *const u8).add(32) as *const *const u8);
            std::slice::from_raw_parts(addr.add(4), 4) == [192, 0, 2, 1]
        };
        v.push(("GetAddrInfoExA", r, 0, got));
        v
    }

    #[test]
    fn what_gets_past_the_system_hooks() {
        unsafe {
            let mut wsa = [0u8; 512];
            assert_eq!(WSAStartup(0x0202, wsa.as_mut_ptr()), 0);
            let listener = socket(2, 1, 6);
            let mut a = [0u8; 16];
            a[0] = 2;
            a[4..8].copy_from_slice(&[127, 0, 0, 1]);
            assert_eq!(bind(listener, a.as_ptr(), 16), 0);
            assert_eq!(listen(listener, 8), 0);
            let mut len = 16;
            assert_eq!(getsockname(listener, a.as_mut_ptr(), &mut len), 0);
            let port = u16::from_be_bytes([a[2], a[3]]);
            // [::ffff:127.0.0.1]:port
            let mut to = [0u8; 28];
            to[0] = 23;
            to[2..4].copy_from_slice(&a[2..4]);
            to[18..24].copy_from_slice(&[0xff, 0xff, 127, 0, 0, 1]);
            assert!(!loopback(to.as_ptr(), 28), "the hooks treat this address as any other host");
            assert!(!local_name("::ffff:127.0.0.1") && !local_name("192.0.2.1"));

            let before = probe(listener, &to, port);
            install_system_hooks();
            let after = probe(listener, &to, port);
            println!("{:<18} {:>28}   {:>28}", "", "before the hooks", "with the hooks");
            for (b, a) in before.iter().zip(&after) {
                let show = |x: &(&str, i32, i32, bool)| format!("r={} err={} {}", x.1, x.2, if x.3 { "REACHED" } else { "refused" });
                println!("{:<18} {:>28}   {:>28}", b.0, show(b), show(a));
            }
            // every function works before the hooks, so a refusal after them is the hooks'
            assert!(before.iter().all(|x| x.3), "{before:?}");
            let past: Vec<_> = after.iter().filter(|x| x.3).map(|x| x.0).collect();
            assert!(past.is_empty(), "past the XCL_NETWORK=0 hooks: {past:?}");
        }
    }
}

Tested

Windows 11 26200, Ryzen 9 9950X, main a6f5f7a plus the test above, debug. CPU only, loopback only.

  • cargo test -p xcl --lib -- gap_tests --nocapture: 0 passed, 1 failed, the same table in 3 runs.

Not tested

  • A title, or any connection to another machine.
  • Other ways to an address or a socket that are not in the hook list (DnsQuery_* in dnsapi.dll, WSAConnectByList, WSAConnectByNameA).
  • The fix sketched above.

🤖 Claude Fable 5.2

**@⁠Kalmarv** on GitHub · 2026-11-24 00:22 UTC · [original](https://github.com/xcl-dev/xcl/issues/444) Follow-up to #305 (closed, fixed in db1ddb2). On main a6f5f7a, with the system hooks installed, a socket still connects through `ConnectEx`. `connect` to the same address is refused. `ConnectEx` is not an export: a caller gets its address from `WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER)`, so none of the 18 hooks is on its path. Code that connects on a completion port uses it. A scratch test added to `hle/netoff.rs` calls each function before and after `install_system_hooks()`. Nothing leaves the machine: the listener is on 127.0.0.1 and is reached as `::ffff:127.0.0.1`, an address the hooks do not count as loopback, so the hooked `connect` refuses it like any other host. "REACHED" means the listener accepted a connection (or, for a lookup, an address came back). ``` before the hooks with the hooks connect r=0 err=0 REACHED r=-1 err=10051 refused ConnectEx r=1 err=0 REACHED r=1 err=0 REACHED WSAConnectByNameW r=1 err=0 REACHED r=0 err=11001 refused getaddrinfo r=0 err=0 REACHED r=11001 err=0 refused GetAddrInfoExW r=0 err=0 REACHED r=11001 err=0 refused GetAddrInfoExA r=0 err=0 REACHED r=11001 err=0 refused past the XCL_NETWORK=0 hooks: ["ConnectEx"] test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 657 filtered out ``` - `WSAConnectByNameW` and `GetAddrInfoExA` are not hooked either, but both are refused: each logs `XCL_NETWORK=0: GetAddrInfoExW fails (no network)`, so they go through the hooked `GetAddrInfoExW`. - The lookups stay refused, so a `ConnectEx` caller that needs a name from one of the hooked lookups gets no address. What is open is a connection to an address it already has. - A possible fix: hook `WSAIoctl` and answer `WSAID_CONNECTEX` with a function that refuses as `connect` does. <details><summary>The test (appended to <code>crates/xcl/src/hle/netoff.rs</code>)</summary> ```rust /// Do ConnectEx, WSAConnectByNameW and GetAddrInfoExA get past the system hooks? Nothing leaves /// this machine: the listener is on 127.0.0.1 and is reached through the IPv4-mapped address /// ::ffff:127.0.0.1, which `loopback` does not let through – the hooked `connect` refuses it /// as it refuses any other host. The lookups are of a numeric address with AI_NUMERICHOST /// (no DNS query), which the hooked lookups refuse like any name that is not local. #[cfg(test)] mod gap_tests { use super::*; use std::ptr::{null, null_mut}; #[repr(C)] struct FdSet { count: u32, array: [usize; 64], } #[link(name = "ws2_32", kind = "raw-dylib")] extern "system" { fn WSAStartup(version: u16, data: *mut u8) -> i32; fn WSAGetLastError() -> i32; fn WSACreateEvent() -> usize; fn socket(af: i32, ty: i32, proto: i32) -> usize; fn bind(s: usize, name: *const u8, len: i32) -> i32; fn listen(s: usize, backlog: i32) -> i32; fn accept(s: usize, addr: *mut u8, len: *mut i32) -> usize; fn getsockname(s: usize, name: *mut u8, len: *mut i32) -> i32; fn setsockopt(s: usize, level: i32, name: i32, val: *const u8, len: i32) -> i32; fn closesocket(s: usize) -> i32; fn select(n: i32, r: *mut FdSet, w: *mut FdSet, e: *mut FdSet, timeout: *const [i32; 2]) -> i32; fn WSAIoctl(s: usize, code: u32, inbuf: *const u8, inlen: u32, outbuf: *mut u8, outlen: u32, ret: *mut u32, ov: *mut c_void, cb: *const c_void) -> i32; fn WSAGetOverlappedResult(s: usize, ov: *const c_void, bytes: *mut u32, wait: i32, flags: *mut u32) -> i32; fn WSAConnectByNameW(s: usize, node: *const u16, service: *const u16, local_len: *mut u32, local: *mut u8, remote_len: *mut u32, remote: *mut u8, timeout: *const c_void, reserved: *mut c_void) -> i32; fn GetAddrInfoExA(name: *const u8, service: *const u8, ns: u32, id: *const c_void, hints: *const c_void, out: *mut *mut c_void, timeout: *const c_void, ov: *mut c_void, cb: *const c_void, handle: *mut c_void) -> i32; fn GetAddrInfoExW(name: *const u16, service: *const u16, ns: u32, id: *const c_void, hints: *const c_void, out: *mut *mut c_void, timeout: *const c_void, ov: *mut c_void, cb: *const c_void, handle: *mut c_void) -> i32; } /// an IPv6 socket that also reaches IPv4 (IPV6_V6ONLY off) unsafe fn dual() -> usize { let s = socket(23, 1, 6); assert_ne!(s, usize::MAX); let off = 0u32; assert_eq!(setsockopt(s, 41, 27, &off as *const u32 as *const u8, 4), 0); s } /// whether a connection arrived at the listener within a second (it is accepted and closed) unsafe fn arrived(listener: usize) -> bool { let mut r = FdSet { count: 1, array: [0; 64] }; r.array[0] = listener; if select(0, &mut r, null_mut(), null_mut(), &[1, 0]) != 1 { return false; } let c = accept(listener, null_mut(), null_mut()); closesocket(c); c != usize::MAX } /// (function, its result, the error after it, whether a connection arrived / an address came back) unsafe fn probe(listener: usize, to: &[u8; 28], port: u16) -> Vec<(&'static str, i32, i32, bool)> { let mut v = Vec::new(); // connect: one of the hooked functions, the control let s = dual(); let r = real_connect(s, to.as_ptr(), 28); v.push(("connect", r, WSAGetLastError(), arrived(listener))); closesocket(s); // ConnectEx, found the only way there is: WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER) let s = dual(); let mut local = [0u8; 28]; local[0] = 23; local[18..24].copy_from_slice(&[0xff, 0xff, 127, 0, 0, 1]); assert_eq!(bind(s, local.as_ptr(), 28), 0, "ConnectEx needs a bound socket"); let wsaid_connectex: [u8; 16] = [0xb9, 0x07, 0xa2, 0x25, 0xf3, 0xdd, 0x60, 0x46, 0x8e, 0xe9, 0x76, 0xe5, 0x8c, 0x74, 0x06, 0x3e]; let (mut f, mut n) = (0usize, 0u32); assert_eq!(WSAIoctl(s, 0xC800_0006, wsaid_connectex.as_ptr(), 16, &mut f as *mut usize as *mut u8, 8, &mut n, null_mut(), null()), 0); let connect_ex: unsafe extern "system" fn(usize, *const u8, i32, *const c_void, u32, *mut u32, *mut c_void) -> i32 = std::mem::transmute(f); let mut ov = [0usize; 4]; ov[3] = WSACreateEvent(); let mut r = connect_ex(s, to.as_ptr(), 28, null(), 0, null_mut(), ov.as_mut_ptr() as *mut c_void); let mut e = WSAGetLastError(); if r == 0 && e == 997 { let mut flags = 0u32; r = WSAGetOverlappedResult(s, ov.as_ptr() as *const c_void, &mut n, 1, &mut flags); e = if r != 0 { 0 } else { WSAGetLastError() }; } v.push(("ConnectEx", r, e, arrived(listener))); closesocket(s); // WSAConnectByNameW: looks the name up and connects, in one call let s = dual(); let r = WSAConnectByNameW(s, crate::util::wide("::ffff:127.0.0.1").as_ptr(), crate::util::wide(&port.to_string()).as_ptr(), null_mut(), null_mut(), null_mut(), null_mut(), null(), null_mut()); v.push(("WSAConnectByNameW", r, if r != 0 { 0 } else { WSAGetLastError() }, arrived(listener))); closesocket(s); // the lookups: 192.0.2.1 as a numeric address (AI_NUMERICHOST = 4: no DNS query) let mut hints = [0u8; 72]; hints[0] = 4; let h = hints.as_ptr() as *const c_void; let mut out = null_mut(); let r = real_getaddrinfo(b"192.0.2.1\0".as_ptr(), null(), h, &mut out); v.push(("getaddrinfo", r, 0, !out.is_null())); out = null_mut(); let r = GetAddrInfoExW(crate::util::wide("192.0.2.1").as_ptr(), null(), 0, null(), h, &mut out, null(), null_mut(), null(), null_mut()); v.push(("GetAddrInfoExW", r, 0, !out.is_null())); out = null_mut(); let r = GetAddrInfoExA(b"192.0.2.1\0".as_ptr(), null(), 0, null(), h, &mut out, null(), null_mut(), null(), null_mut()); // ADDRINFOEXA: ai_addr is at 32; a sockaddr_in has its address at 4 let got = !out.is_null() && { let addr = *((out as *const u8).add(32) as *const *const u8); std::slice::from_raw_parts(addr.add(4), 4) == [192, 0, 2, 1] }; v.push(("GetAddrInfoExA", r, 0, got)); v } #[test] fn what_gets_past_the_system_hooks() { unsafe { let mut wsa = [0u8; 512]; assert_eq!(WSAStartup(0x0202, wsa.as_mut_ptr()), 0); let listener = socket(2, 1, 6); let mut a = [0u8; 16]; a[0] = 2; a[4..8].copy_from_slice(&[127, 0, 0, 1]); assert_eq!(bind(listener, a.as_ptr(), 16), 0); assert_eq!(listen(listener, 8), 0); let mut len = 16; assert_eq!(getsockname(listener, a.as_mut_ptr(), &mut len), 0); let port = u16::from_be_bytes([a[2], a[3]]); // [::ffff:127.0.0.1]:port let mut to = [0u8; 28]; to[0] = 23; to[2..4].copy_from_slice(&a[2..4]); to[18..24].copy_from_slice(&[0xff, 0xff, 127, 0, 0, 1]); assert!(!loopback(to.as_ptr(), 28), "the hooks treat this address as any other host"); assert!(!local_name("::ffff:127.0.0.1") && !local_name("192.0.2.1")); let before = probe(listener, &to, port); install_system_hooks(); let after = probe(listener, &to, port); println!("{:<18} {:>28} {:>28}", "", "before the hooks", "with the hooks"); for (b, a) in before.iter().zip(&after) { let show = |x: &(&str, i32, i32, bool)| format!("r={} err={} {}", x.1, x.2, if x.3 { "REACHED" } else { "refused" }); println!("{:<18} {:>28} {:>28}", b.0, show(b), show(a)); } // every function works before the hooks, so a refusal after them is the hooks' assert!(before.iter().all(|x| x.3), "{before:?}"); let past: Vec<_> = after.iter().filter(|x| x.3).map(|x| x.0).collect(); assert!(past.is_empty(), "past the XCL_NETWORK=0 hooks: {past:?}"); } } } ``` </details> **Tested** Windows 11 26200, Ryzen 9 9950X, main a6f5f7a plus the test above, debug. CPU only, loopback only. - `cargo test -p xcl --lib -- gap_tests --nocapture`: 0 passed, 1 failed, the same table in 3 runs. **Not tested** - A title, or any connection to another machine. - Other ways to an address or a socket that are not in the hook list (`DnsQuery_*` in `dnsapi.dll`, `WSAConnectByList`, `WSAConnectByNameA`). - The fix sketched above. 🤖 Claude Fable 5.2
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:22:00 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.