Wave icon indicating copy to clipboard operation
Wave copied to clipboard

Reject unrepresentable POSIX descriptors before timed TCP reads and writes

Open LunaStev opened this issue 1 month ago • 0 comments

Problem

tcp_read_timeout and tcp_write_timeout can operate on another live POSIX socket when passed an unrepresentable i64 descriptor such as 2^32 + live_fd. The timeout path calls raw recv/send before reaching the guarded readiness helpers. Narrowing to the native descriptor therefore occurs before validation.

Starting points: TCP timeout entry points, direct POSIX I/O attempt, and the existing target-specific net_fd_valid in std/net/socket_base.wave.

Reproduction

Executed on Linux amd64 using the checkout-built wavec 0.0.1-alpha-dev, matching std, Rust 1.89.0 and LLVM 21, at local f7ff92c54bf8b41c2877245788890935b4ca3ba1. The affected std source matches canonical master 1d1751405e087f32c1b1f28399d1629ae1bcfbd2.

The following uses an inherited local stream socketpair and temporary source/output files. No remote service is needed:

from pathlib import Path
import socket
import subprocess
import tempfile

root = Path.cwd()
compiler = root / 'target/debug/wavec'
with tempfile.TemporaryDirectory() as temporary:
    directory = Path(temporary)
    for operation in ('read', 'write'):
        for optimization in ('-O0', '-O2'):
            left, right = socket.socketpair()
            try:
                if operation == 'read':
                    right.sendall(b'Z')
                source = directory / 'probe.wave'
                binary = directory / 'probe'
                source.write_text(f'''
import("std::net::tcp")::{{TcpStream, tcp_{operation}_timeout}};
import("std::net::error")::{{NetIoResult}};
fun main() -> i32 {{
    var sample: u8 = 65;
    var stream: TcpStream = TcpStream {{ fd: {left.fileno()} + 4294967296 }};
    var result: NetIoResult = tcp_{operation}_timeout(stream, &sample, 1, 0);
    println("{{d}} {{d}} {{d}}", result.count, result.error.native_code, sample);
    return 0;
}}
''', encoding='utf-8')
                subprocess.run(
                    [str(compiler), '--std-root', str(root / 'std'), 'build',
                     str(source), optimization, '-o', str(binary)],
                    check=True, timeout=45,
                )
                result = subprocess.run(
                    [str(binary)], pass_fds=(left.fileno(),),
                    capture_output=True, text=True, timeout=2,
                )
                print(operation, optimization, result.stdout.strip())
                if operation == 'write':
                    right.settimeout(1)
                    print('peer received:', right.recv(1))
            finally:
                left.close()
                right.close()

The audit observed a timed read report 1 0 90: it consumed the queued Z through the oversized descriptor. Timed writes reported a successful one-byte transfer at O0 and O2. These operations should reject the descriptor before accessing the real socket. Each read reproduction needs fresh queued input; reusing a drained socket is not a valid comparison.

Scope

Apply the existing target-aware descriptor validation before any nonempty timeout I/O attempt, including the directly callable POSIX timeout entry point. Keep full-width Windows SOCKET handling and existing zero-length, partial I/O, deadline and error contracts. Reading and writing are one issue because they share the missing guard.

Related: completed #988 fixed ordinary socket paths; #807 fixed readiness waits. This is the remaining direct timeout-I/O path, not a request to repeat those fixes.

Completion criteria

  • [ ] Oversized and truncating-negative POSIX descriptors fail before consuming or sending data or modifying the output buffer.
  • [ ] Read/write regressions use a real live descriptor plus 2^32 and cover O0/O2.
  • [ ] Valid descriptors, zero-length calls and timeout behavior remain correct.
  • [ ] Windows descriptor width is not restricted to the POSIX i32 range.

LunaStev avatar Nov 02 '26 05:11 LunaStev