tilo-socket 0.5.0

Local sockets named by a path of any length: Unix-domain sockets where the kernel's fixed path buffer would refuse a deep profile or state root, and named pipes kept to one account on Windows.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! The same local sockets for tokio: a Unix socket, or on Windows the same
//! named pipe with the same account checks, behind one listener and stream.

#[cfg(unix)]
use std::{io, path::Path};

#[cfg(unix)]
pub use tokio::net::{UnixListener as Listener, UnixStream as Stream};

/// Serve `listener`, bound with [`crate::bind`] (or a caller's own private
/// bind), on the current tokio runtime.
///
/// # Errors
/// The listener could not be registered with the runtime.
#[cfg(unix)]
pub fn listener(listener: crate::Listener) -> io::Result<Listener> {
    listener.set_nonblocking(true)?;
    Listener::from_std(listener)
}

/// Connect to the socket at `path`, whatever its length.
///
/// # Errors
/// As [`crate::connect`], or the stream could not be registered with the runtime.
#[cfg(unix)]
#[expect(
    clippy::unused_async,
    reason = "one signature on every host: Windows' connect waits out a busy pipe"
)]
pub async fn connect(path: &Path) -> io::Result<Stream> {
    let stream = crate::connect(path)?;
    stream.set_nonblocking(true)?;
    Stream::from_std(stream)
}

/// Drive `stream`, connected by [`crate::connect`] or handed over, on the current tokio runtime.
///
/// # Errors
/// The stream could not be registered with the runtime.
#[cfg(unix)]
pub fn from_std(stream: crate::Stream) -> io::Result<Stream> {
    stream.set_nonblocking(true)?;
    Stream::from_std(stream)
}

#[cfg(windows)]
pub use self::pipe::{Listener, Stream, connect, from_std, listener};

/// The process at the other end of `stream`, as the kernel saw it connect.
///
/// # Errors
/// The kernel names no process for it.
pub fn peer_pid(stream: &Stream) -> std::io::Result<u32> {
    #[cfg(unix)]
    {
        stream
            .peer_cred()?
            .pid()
            .and_then(|pid| u32::try_from(pid).ok())
            .ok_or_else(|| std::io::Error::other("the peer's process is unknown"))
    }
    #[cfg(windows)]
    {
        stream.peer_pid()
    }
}

/// Wait for the first `buffer.len()` bytes the peer sends, and copy them into `buffer` without
/// consuming them: the next read still returns them. Fewer only when the peer closed first, `0`
/// when it sent nothing. Cancelling the wait loses nothing.
///
/// # Errors
/// The stream could not be read.
pub async fn peek(stream: &mut Stream, buffer: &mut [u8]) -> std::io::Result<usize> {
    #[cfg(unix)]
    {
        use std::os::fd::AsRawFd as _;
        loop {
            let ready = stream.ready(::tokio::io::Interest::READABLE).await?;
            // A peek that finds only part reports `WouldBlock`, so the readiness clears and the
            // next bytes wake this loop.
            match stream.try_io(::tokio::io::Interest::READABLE, || {
                // SAFETY: `buffer` is writable for its length and `stream` owns the live socket
                // for this whole call; MSG_PEEK leaves the bytes for the next read.
                let read = unsafe {
                    libc::recv(
                        stream.as_raw_fd(),
                        buffer.as_mut_ptr().cast(),
                        buffer.len(),
                        libc::MSG_PEEK | libc::MSG_DONTWAIT,
                    )
                };
                let read = usize::try_from(read).map_err(|_| std::io::Error::last_os_error())?;
                if read == 0 || read >= buffer.len() || ready.is_read_closed() {
                    return Ok(read);
                }
                Err(std::io::ErrorKind::WouldBlock.into())
            }) {
                Ok(read) => return Ok(read),
                Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {}
                Err(error) => return Err(error),
            }
        }
    }
    #[cfg(windows)]
    {
        stream.peek(buffer).await
    }
}

/// Whether `stream`'s peer is a program of this login: this account, and not
/// one a sandbox confined. On Linux the peer's uid must be this process's and it
/// must live in this process's user and mount namespaces, as a session's other
/// sockets require; a program a sandbox put in namespaces of its own is the same
/// account and is refused all the same. On Windows the pipe's DACL grants this
/// account and SYSTEM alone, which an `AppContainer`'s token is not given, and
/// [`Listener::accept`] took only this account's peers.
#[must_use]
pub fn peer_is_this_login(stream: &Stream) -> bool {
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt as _;
        let owner = std::fs::metadata("/proc/self")
            .ok()
            .map(|metadata| metadata.uid());
        stream
            .peer_cred()
            .ok()
            .filter(|credentials| Some(credentials.uid()) == owner)
            .and_then(|credentials| credentials.pid())
            .and_then(|pid| u32::try_from(pid).ok())
            .is_some_and(|pid| {
                let same = |kind: &str| {
                    let ours = std::fs::metadata(format!("/proc/self/ns/{kind}"));
                    let theirs = std::fs::metadata(format!("/proc/{pid}/ns/{kind}"));
                    // A process that cannot be read is not one this login knows.
                    matches!((ours, theirs), (Ok(ours), Ok(theirs))
                        if ours.dev() == theirs.dev() && ours.ino() == theirs.ino())
                };
                same("user") && same("mnt")
            })
    }
    #[cfg(windows)]
    {
        let _ = stream;
        true
    }
}

/// One end of a connected datagram pair ([`crate::Datagram`]) on the runtime. Any number of
/// tasks send on it at once; one task reads it at a time, and a read is not cancel-safe on Windows:
/// a datagram there is a header and its bytes, and a read dropped between them loses its place.
#[derive(Debug)]
pub struct Datagram {
    #[cfg(unix)]
    socket: tokio::net::UnixDatagram,
    #[cfg(windows)]
    reader: tokio::sync::Mutex<tokio::io::ReadHalf<Stream>>,
    #[cfg(windows)]
    writer: tokio::sync::Mutex<tokio::io::WriteHalf<Stream>>,
}

#[cfg(any(unix, windows))]
impl Datagram {
    /// Drive `datagram` on the current runtime.
    ///
    /// # Errors
    /// The runtime could not take it.
    pub fn from_std(datagram: crate::Datagram) -> std::io::Result<Self> {
        #[cfg(unix)]
        {
            let socket = datagram.into_socket();
            socket.set_nonblocking(true)?;
            Ok(Self {
                socket: tokio::net::UnixDatagram::from_std(socket)?,
            })
        }
        #[cfg(windows)]
        {
            let (reader, writer) = tokio::io::split(from_std(datagram.into_stream())?);
            Ok(Self {
                reader: tokio::sync::Mutex::new(reader),
                writer: tokio::sync::Mutex::new(writer),
            })
        }
    }

    /// Send `payload` as one datagram.
    ///
    /// # Errors
    /// The other end is gone, or `payload` is longer than [`crate::datagram::MAX_DATAGRAM`].
    pub async fn send(&self, payload: &[u8]) -> std::io::Result<usize> {
        if payload.len() > crate::datagram::MAX_DATAGRAM {
            return Err(crate::datagram::too_long(payload.len()));
        }
        #[cfg(unix)]
        {
            self.socket.send(payload).await
        }
        #[cfg(windows)]
        {
            use tokio::io::AsyncWriteExt as _;
            let mut writer = self.writer.lock().await;
            writer.write_all(&crate::datagram::framed(payload)).await?;
            writer.flush().await?;
            drop(writer);
            Ok(payload.len())
        }
    }

    /// The next datagram, into `buffer`, as [`crate::Datagram::recv`] reads it.
    ///
    /// # Errors
    /// The system's error reading.
    pub async fn recv(&self, buffer: &mut [u8]) -> std::io::Result<usize> {
        #[cfg(unix)]
        {
            self.socket.recv(buffer).await
        }
        #[cfg(windows)]
        {
            use tokio::io::AsyncReadExt as _;
            let mut reader = self.reader.lock().await;
            let mut header = [0_u8; crate::datagram::HEADER];
            let length = match reader.read_exact(&mut header).await {
                Ok(_) => usize::try_from(u32::from_le_bytes(header)).unwrap_or(usize::MAX),
                Err(error) if error.kind() == std::io::ErrorKind::UnexpectedEof => return Ok(0),
                Err(error) => return Err(error),
            };
            if length > crate::datagram::MAX_DATAGRAM {
                return Err(crate::datagram::too_long(length));
            }
            let kept = length.min(buffer.len());
            let mut rest = vec![0_u8; length.saturating_sub(kept)];
            for part in [buffer.get_mut(..kept).unwrap_or_default(), &mut rest] {
                match reader.read_exact(part).await {
                    Ok(_) => {}
                    Err(error) if error.kind() == std::io::ErrorKind::UnexpectedEof => {
                        return Ok(0);
                    }
                    Err(error) => return Err(error),
                }
            }
            Ok(kept)
        }
    }
}

#[cfg(windows)]
mod pipe {
    use crate::account::{Account, LocalAllocation};
    use crate::windows::{SocketAddr, open, peer_is};
    use std::io;
    use std::os::windows::io::{AsRawHandle, IntoRawHandle, OwnedHandle};
    use std::path::Path;
    use std::pin::Pin;
    use std::sync::Mutex;
    use std::task::{Context, Poll};
    use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
    use tokio::net::windows::named_pipe::{NamedPipeClient, NamedPipeServer};

    /// A listening pipe on the runtime: the instance the next client connects to.
    pub struct Listener {
        name: Vec<u16>,
        account: Account,
        descriptor: LocalAllocation,
        next: Mutex<Option<NamedPipeServer>>,
    }

    /// An instance the synchronous listener made, served on the runtime.
    fn served(pipe: OwnedHandle) -> io::Result<NamedPipeServer> {
        // SAFETY: an overlapped pipe instance owned here, handed over once.
        unsafe { NamedPipeServer::from_raw_handle(pipe.into_raw_handle()) }
    }

    fn instance(name: &[u16], descriptor: &LocalAllocation) -> io::Result<NamedPipeServer> {
        served(crate::windows::instance(name, descriptor, false)?)
    }

    /// Serve `listener`, made by [`crate::bind`], on the current runtime: the
    /// runtime takes over its instance, which holds the pipe's name.
    ///
    /// # Errors
    /// The runtime could not take the instance.
    pub fn listener(listener: crate::Listener) -> io::Result<Listener> {
        let (name, account, descriptor, first) = listener.into_parts();
        let next = first.map(served).transpose()?;
        Ok(Listener {
            name,
            account,
            descriptor,
            next: Mutex::new(next),
        })
    }

    impl Listener {
        /// Accept the next connection from this account; a client of another
        /// account is let go and the wait goes on.
        ///
        /// # Errors
        /// Another task is accepting on this listener, or the pipe could not
        /// take a connection or make its next instance.
        pub async fn accept(&self) -> io::Result<(Stream, SocketAddr)> {
            loop {
                let pending = self
                    .next
                    .lock()
                    .unwrap_or_else(std::sync::PoisonError::into_inner)
                    .take();
                let server = match pending {
                    Some(server) => server,
                    None => instance(&self.name, &self.descriptor)?,
                };
                server.connect().await?;
                let next = instance(&self.name, &self.descriptor)?;
                *self
                    .next
                    .lock()
                    .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(next);
                if peer_is(server.as_raw_handle(), true, &self.account)? {
                    return Ok((Stream::new(End::Server(server)), SocketAddr));
                }
                server.disconnect()?;
            }
        }
    }

    /// Drive `stream`, connected by [`crate::connect`] or handed over, on the current runtime:
    /// its end of the pipe, server or client, is taken over by the runtime.
    ///
    /// # Errors
    /// The runtime could not take the pipe.
    pub fn from_std(stream: crate::Stream) -> io::Result<Stream> {
        let (pipe, server_end) = stream.into_end();
        let raw = pipe.into_raw_handle();
        // SAFETY: an overlapped pipe handle `stream` owned, handed over once.
        unsafe {
            Ok(if server_end {
                Stream::new(End::Server(NamedPipeServer::from_raw_handle(raw)?))
            } else {
                Stream::new(End::Client(NamedPipeClient::from_raw_handle(raw)?))
            })
        }
    }

    /// One end of a connected pipe on the runtime.
    #[derive(Debug)]
    pub struct Stream {
        end: End,
        /// What [`Stream::peek`] read off the pipe ahead of the reader. The runtime reads a
        /// pipe into a buffer of its own as soon as data arrives, so `PeekNamedPipe` cannot be
        /// trusted to see what came.
        peeked: Vec<u8>,
    }

    #[derive(Debug)]
    enum End {
        Server(NamedPipeServer),
        Client(NamedPipeClient),
    }

    impl Stream {
        const fn new(end: End) -> Self {
            Self {
                end,
                peeked: Vec::new(),
            }
        }

        /// As [`super::peer_pid`].
        pub(super) fn peer_pid(&self) -> io::Result<u32> {
            match &self.end {
                End::Server(pipe) => crate::windows::peer_pid(pipe.as_raw_handle(), true),
                End::Client(pipe) => crate::windows::peer_pid(pipe.as_raw_handle(), false),
            }
        }

        /// Whether this is the server's end, the one the pipe's maker accepted on.
        #[must_use]
        pub const fn is_server_end(&self) -> bool {
            matches!(self.end, End::Server(_))
        }

        /// As [`super::peek`].
        pub(super) async fn peek(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
            use tokio::io::AsyncReadExt as _;
            let mut chunk = [0_u8; 256];
            while self.peeked.len() < buffer.len() {
                let wanted = buffer
                    .len()
                    .saturating_sub(self.peeked.len())
                    .min(chunk.len());
                let Some(chunk) = chunk.get_mut(..wanted) else {
                    break;
                };
                let read = match &mut self.end {
                    End::Server(pipe) => pipe.read(chunk).await?,
                    End::Client(pipe) => pipe.read(chunk).await?,
                };
                let Some(read) = chunk.get(..read).filter(|read| !read.is_empty()) else {
                    break;
                };
                self.peeked.extend_from_slice(read);
            }
            let have = self.peeked.len().min(buffer.len());
            if let (Some(into), Some(from)) = (buffer.get_mut(..have), self.peeked.get(..have)) {
                into.copy_from_slice(from);
            }
            Ok(have)
        }
    }

    /// Connect to the pipe `path` names, served by this account alone.
    ///
    /// # Errors
    /// Nothing serves it, it is served by another account, or the runtime
    /// could not take the connection.
    pub async fn connect(path: &Path) -> io::Result<Stream> {
        let account = Account::current()?;
        let name = crate::windows::pipe_name(&account, path)?;
        let pipe = tokio::task::spawn_blocking(move || open(&crate::windows::wide(&name)))
            .await
            .map_err(io::Error::other)??;
        if !peer_is(pipe.as_raw_handle(), false, &account)? {
            return Err(io::Error::new(
                io::ErrorKind::PermissionDenied,
                format!("{}: the pipe is served by another account", path.display()),
            ));
        }
        // SAFETY: an overlapped pipe handle this function owns, handed over once.
        let client = unsafe { NamedPipeClient::from_raw_handle(pipe.into_raw_handle())? };
        Ok(Stream::new(End::Client(client)))
    }

    impl AsyncRead for Stream {
        fn poll_read(
            self: Pin<&mut Self>,
            cx: &mut Context<'_>,
            buf: &mut ReadBuf<'_>,
        ) -> Poll<io::Result<()>> {
            let this = self.get_mut();
            if !this.peeked.is_empty() {
                let given = this.peeked.len().min(buf.remaining());
                buf.put_slice(this.peeked.get(..given).unwrap_or_default());
                this.peeked.drain(..given);
                return Poll::Ready(Ok(()));
            }
            match &mut this.end {
                End::Server(pipe) => Pin::new(pipe).poll_read(cx, buf),
                End::Client(pipe) => Pin::new(pipe).poll_read(cx, buf),
            }
        }
    }

    impl AsyncWrite for Stream {
        fn poll_write(
            self: Pin<&mut Self>,
            cx: &mut Context<'_>,
            buf: &[u8],
        ) -> Poll<io::Result<usize>> {
            match &mut self.get_mut().end {
                End::Server(pipe) => Pin::new(pipe).poll_write(cx, buf),
                End::Client(pipe) => Pin::new(pipe).poll_write(cx, buf),
            }
        }

        fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
            match &mut self.get_mut().end {
                End::Server(pipe) => Pin::new(pipe).poll_flush(cx),
                End::Client(pipe) => Pin::new(pipe).poll_flush(cx),
            }
        }

        fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
            match &mut self.get_mut().end {
                End::Server(pipe) => Pin::new(pipe).poll_shutdown(cx),
                End::Client(pipe) => Pin::new(pipe).poll_shutdown(cx),
            }
        }
    }
}

#[cfg(all(test, windows))]
mod tests {
    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};

    #[test]
    fn a_pipe_on_the_runtime_carries_a_request_and_its_answer() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("api.sock");
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();
        runtime.block_on(async {
            let listener = super::listener(crate::bind(&path).unwrap()).unwrap();
            let serving = async {
                let (mut stream, _) = listener.accept().await.unwrap();
                assert!(super::peer_is_this_login(&stream));
                let mut asked = [0; 4];
                stream.read_exact(&mut asked).await.unwrap();
                stream.write_all(b"pong").await.unwrap();
                asked
            };
            let asking = async {
                let mut stream = super::connect(&path).await.unwrap();
                stream.write_all(b"ping").await.unwrap();
                let mut answer = [0; 4];
                stream.read_exact(&mut answer).await.unwrap();
                answer
            };
            let (asked, answer) = tokio::join!(serving, asking);
            assert_eq!((&asked, &answer), (b"ping", b"pong"));
        });
    }
}