1#![allow(unsafe_code)] use std::collections::HashMap;
25use std::io::{self, Read, Write};
26use std::net::TcpStream;
27use std::os::fd::{IntoRawFd, RawFd};
28use std::path::Path;
29use std::sync::{Arc, Mutex};
30use std::time::Duration;
31
32use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};
33use rustls::pki_types::{CertificateDer, ServerName, UnixTime};
34use rustls::{
35 ClientConfig, ClientConnection, DigitallySignedStruct, Error as TlsError, RootCertStore,
36 SignatureScheme,
37};
38
39pub const TLS_FLAG_VERIFY: u32 = 1;
41pub const TLS_FLAG_PLAIN: u32 = 2;
46
47struct TlsSlot {
48 conn: ClientConnection,
49 pending_out: Vec<u8>,
51 pending_in: Vec<u8>,
54}
55
56static SLOTS: Mutex<Option<HashMap<i32, TlsSlot>>> = Mutex::new(None);
57
58fn with_slots<R>(f: impl FnOnce(&mut HashMap<i32, TlsSlot>) -> R) -> R {
59 let mut guard = SLOTS
60 .lock()
61 .unwrap_or_else(std::sync::PoisonError::into_inner);
62 let map = guard.get_or_insert_with(HashMap::new);
63 f(map)
64}
65
66#[must_use]
70#[inline]
71pub fn is_tls_fd(gfd: i32) -> bool {
72 crate::process::fd_is_tls(gfd)
73}
74
75pub fn take_tls(gfd: i32) -> bool {
77 crate::process::fd_set_tls(gfd, false);
78 with_slots(|m| m.remove(&gfd).is_some())
79}
80
81fn wants_write(gfd: i32) -> bool {
82 with_slots(|m| {
83 m.get(&gfd)
84 .is_some_and(|s| !s.pending_out.is_empty() || s.conn.wants_write())
85 })
86}
87
88fn has_pending_in(gfd: i32) -> bool {
89 with_slots(|m| m.get(&gfd).is_some_and(|s| !s.pending_in.is_empty()))
90}
91
92pub fn connect(
102 hostname: &str,
103 port: u16,
104 flags: u32,
105 ca_pem: Option<&Path>,
106) -> Result<i32, String> {
107 if hostname.is_empty() {
108 return Err("empty hostname".into());
109 }
110 if port == 0 {
111 return Err("invalid port".into());
112 }
113
114 if flags & TLS_FLAG_PLAIN != 0 {
115 return connect_plain(hostname, port);
116 }
117
118 let config = build_config((flags & TLS_FLAG_VERIFY) != 0, ca_pem)?;
119 let server_name =
120 ServerName::try_from(hostname.to_owned()).map_err(|e| format!("server name: {e}"))?;
121 let mut conn = ClientConnection::new(Arc::new(config), server_name)
122 .map_err(|e| format!("tls client: {e}"))?;
123
124 let mut sock = tcp_connect_stream(hostname, port)?;
125 sock.set_nonblocking(false)
127 .map_err(|e| format!("set blocking: {e}"))?;
128 while conn.is_handshaking() {
129 conn.complete_io(&mut sock)
130 .map_err(|e| format!("tls handshake: {e}"))?;
131 }
132 drop(conn.complete_io(&mut sock));
133
134 sock.set_nonblocking(true)
135 .map_err(|e| format!("set nonblocking: {e}"))?;
136 apply_socket_opts(&sock);
137
138 let host_fd = sock.into_raw_fd();
139 let Some(gfd) = crate::process::fd_alloc(host_fd) else {
140 let _ = unsafe { libc::close(host_fd) };
141 return Err("guest fd table full".into());
142 };
143 crate::process::fd_set_guest_nonblock(gfd, false);
144 crate::process::fd_set_tls(gfd, true);
145
146 with_slots(|m| {
147 m.insert(
148 gfd,
149 TlsSlot {
150 conn,
151 pending_out: Vec::new(),
152 pending_in: Vec::new(),
153 },
154 );
155 });
156
157 tracing::debug!(%hostname, port, gfd, host_fd, "tls_fd connect ok");
158 Ok(gfd)
159}
160
161fn connect_plain(hostname: &str, port: u16) -> Result<i32, String> {
163 let sock = tcp_connect_stream(hostname, port)?;
164 sock.set_nonblocking(true)
165 .map_err(|e| format!("set nonblocking: {e}"))?;
166 apply_socket_opts(&sock);
167
168 let host_fd = sock.into_raw_fd();
169 let Some(gfd) = crate::process::fd_alloc(host_fd) else {
170 let _ = unsafe { libc::close(host_fd) };
171 return Err("guest fd table full".into());
172 };
173 crate::process::fd_set_guest_nonblock(gfd, false);
174 crate::process::fd_set_tls(gfd, false);
176
177 tracing::debug!(%hostname, port, gfd, host_fd, "tcp plain connect ok");
178 Ok(gfd)
179}
180
181fn tcp_connect_stream(hostname: &str, port: u16) -> Result<TcpStream, String> {
182 let addr = format!("{hostname}:{port}");
183 let sock = TcpStream::connect(&addr).map_err(|e| format!("tcp connect {addr}: {e}"))?;
184 sock.set_read_timeout(Some(Duration::from_secs(600)))
185 .map_err(|e| format!("set read timeout: {e}"))?;
186 sock.set_write_timeout(Some(Duration::from_secs(600)))
187 .map_err(|e| format!("set write timeout: {e}"))?;
188 Ok(sock)
189}
190
191fn apply_socket_opts(sock: &TcpStream) {
192 #[cfg(target_os = "linux")]
193 {
194 use std::os::fd::AsRawFd;
195 let fd = sock.as_raw_fd();
196 let flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
197 if flags >= 0 {
198 let _ = unsafe { libc::fcntl(fd, libc::F_SETFD, flags | libc::FD_CLOEXEC) };
199 }
200 }
201
202 #[cfg(any(target_os = "linux", target_os = "macos"))]
205 {
206 use std::os::fd::AsRawFd;
207 let raw = sock.as_raw_fd();
208 let yes: libc::c_int = 1;
209 let yes_len = libc::socklen_t::try_from(core::mem::size_of_val(&yes)).unwrap_or(0);
210 let _ = unsafe {
211 libc::setsockopt(
212 raw,
213 libc::IPPROTO_TCP,
214 libc::TCP_NODELAY,
215 core::ptr::from_ref(&yes).cast(),
216 yes_len,
217 )
218 };
219 let rcv: libc::c_int = 4 * 1024 * 1024;
221 let rcv_len = libc::socklen_t::try_from(core::mem::size_of_val(&rcv)).unwrap_or(0);
222 let _ = unsafe {
223 libc::setsockopt(
224 raw,
225 libc::SOL_SOCKET,
226 libc::SO_RCVBUF,
227 core::ptr::from_ref(&rcv).cast(),
228 rcv_len,
229 )
230 };
231 let snd: libc::c_int = 4 * 1024 * 1024;
232 let snd_len = libc::socklen_t::try_from(core::mem::size_of_val(&snd)).unwrap_or(0);
233 let _ = unsafe {
234 libc::setsockopt(
235 raw,
236 libc::SOL_SOCKET,
237 libc::SO_SNDBUF,
238 core::ptr::from_ref(&snd).cast(),
239 snd_len,
240 )
241 };
242 }
243}
244
245pub fn read(gfd: i32, host_fd: RawFd, buf: &mut [u8], guest_blocking: bool) -> io::Result<usize> {
250 if buf.is_empty() {
251 return Ok(0);
252 }
253 let mut total = 0_usize;
254 loop {
255 let Some(dst) = buf.get_mut(total..) else {
256 return Ok(total);
257 };
258 if dst.is_empty() {
259 return Ok(total);
260 }
261 match try_read(gfd, host_fd, dst) {
262 Ok(0) => return Ok(total), Ok(n) => {
264 total = total.saturating_add(n);
265 if total >= buf.len() {
266 return Ok(total);
267 }
268 }
271 Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
272 if total > 0 {
273 return Ok(total);
275 }
276 if !guest_blocking {
277 return Err(e);
278 }
279 if wants_write(gfd) {
282 drop(flush_tls(gfd, host_fd));
283 if !crate::host::poll_fd_io(host_fd, -1) {
284 return Err(e);
285 }
286 } else if !has_pending_in(gfd)
287 && !crate::host::poll_fd_readable(host_fd, -1)
288 {
289 return Err(e);
290 }
291 }
292 Err(e) => return Err(e),
293 }
294 }
295}
296
297pub fn write(gfd: i32, host_fd: RawFd, buf: &[u8], guest_blocking: bool) -> io::Result<usize> {
299 if buf.is_empty() {
300 return Ok(0);
301 }
302 loop {
303 match try_write(gfd, host_fd, buf) {
304 Ok(n) => return Ok(n),
305 Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
306 if !guest_blocking {
307 return Err(e);
308 }
309 if !crate::host::poll_fd_io(host_fd, -1) {
310 return Err(e);
311 }
312 }
313 Err(e) => return Err(e),
314 }
315 }
316}
317
318fn try_read_plain(gfd: i32, buf: &mut [u8]) -> io::Result<Option<usize>> {
319 with_slots(|m| {
320 let Some(slot) = m.get_mut(&gfd) else {
321 return Err(io::Error::from_raw_os_error(libc::EBADF));
322 };
323 match slot.conn.reader().read(buf) {
324 Ok(0) => Ok(Some(0_usize)),
325 Ok(n) => Ok(Some(n)),
326 Err(e) if e.kind() == io::ErrorKind::WouldBlock => Ok(None),
327 Err(e) => Err(e),
328 }
329 })
330}
331
332fn is_message_buffer_full(err: &io::Error) -> bool {
333 let s = err.to_string();
334 s.contains("message buffer full") || s.contains("MessageBufferFull")
335}
336
337fn feed_pending_in(gfd: i32) -> io::Result<()> {
340 with_slots(|m| {
341 let Some(slot) = m.get_mut(&gfd) else {
342 return Err(io::Error::from_raw_os_error(libc::EBADF));
343 };
344 loop {
345 if slot.pending_in.is_empty() {
346 return Ok(());
347 }
348 let (read_res, consumed) = {
349 let mut cursor = slot.pending_in.as_slice();
350 let before = cursor.len();
351 let res = slot.conn.read_tls(&mut cursor);
352 let n = before.saturating_sub(cursor.len());
353 (res, n)
354 };
355 match read_res {
356 Ok(0) => {
357 slot.conn
358 .process_new_packets()
359 .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
360 return Ok(());
361 }
362 Ok(_) => {
363 if consumed == 0 {
364 return Ok(());
365 }
366 if consumed <= slot.pending_in.len() {
367 slot.pending_in.drain(..consumed);
368 } else {
369 slot.pending_in.clear();
370 }
371 let io_state = slot
372 .conn
373 .process_new_packets()
374 .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
375 if io_state.plaintext_bytes_to_read() > 0 {
376 return Ok(());
377 }
378 }
379 Err(e) if e.kind() == io::ErrorKind::WouldBlock => return Ok(()),
380 Err(e) if is_message_buffer_full(&e) => {
381 let io_state = slot
382 .conn
383 .process_new_packets()
384 .map_err(|e2| io::Error::new(io::ErrorKind::InvalidData, e2))?;
385 if io_state.plaintext_bytes_to_read() > 0 {
386 return Ok(());
387 }
388 return Err(e);
389 }
390 Err(e) => return Err(e),
391 }
392 }
393 })
394}
395
396fn try_read(gfd: i32, host_fd: RawFd, buf: &mut [u8]) -> io::Result<usize> {
397 match flush_tls(gfd, host_fd) {
398 Ok(()) => {}
399 Err(e) if e.kind() == io::ErrorKind::WouldBlock => {}
400 Err(e) => return Err(e),
401 }
402
403 if let Some(n) = try_read_plain(gfd, buf)? {
404 return Ok(n);
405 }
406
407 feed_pending_in(gfd)?;
408 if let Some(n) = try_read_plain(gfd, buf)? {
409 return Ok(n);
410 }
411
412 let mut wire = [0_u8; 16 * 1024];
414 let nr = match read_raw(host_fd, &mut wire) {
415 Ok(n) => n,
416 Err(e) if e.kind() == io::ErrorKind::WouldBlock => return Err(e),
417 Err(e) => return Err(e),
418 };
419 if nr == 0 {
420 feed_pending_in(gfd)?;
421 return match try_read_plain(gfd, buf)? {
422 Some(n) => Ok(n),
423 None => Ok(0),
424 };
425 }
426 with_slots(|m| {
427 let Some(slot) = m.get_mut(&gfd) else {
428 return Err(io::Error::from_raw_os_error(libc::EBADF));
429 };
430 if let Some(bytes) = wire.get(..nr) {
431 slot.pending_in.extend_from_slice(bytes);
432 }
433 Ok(())
434 })?;
435
436 feed_pending_in(gfd)?;
437
438 match flush_tls(gfd, host_fd) {
439 Ok(()) => {}
440 Err(e) if e.kind() == io::ErrorKind::WouldBlock => {}
441 Err(e) => return Err(e),
442 }
443
444 match try_read_plain(gfd, buf)? {
445 Some(n) => Ok(n),
446 None => Err(io::Error::from(io::ErrorKind::WouldBlock)),
447 }
448}
449
450fn try_write(gfd: i32, host_fd: RawFd, buf: &[u8]) -> io::Result<usize> {
451 flush_tls(gfd, host_fd)?;
452
453 let written = with_slots(|m| {
454 let Some(slot) = m.get_mut(&gfd) else {
455 return Err(io::Error::from_raw_os_error(libc::EBADF));
456 };
457 let n = slot.conn.writer().write(buf)?;
458 Ok(n)
459 })?;
460
461 match flush_tls(gfd, host_fd) {
462 Ok(()) => Ok(written),
463 Err(e) if e.kind() == io::ErrorKind::WouldBlock && written > 0 => Ok(written),
464 Err(e) => Err(e),
465 }
466}
467
468fn requeue_unsent(gfd: i32, unsent: &[u8]) {
469 if unsent.is_empty() {
470 return;
471 }
472 with_slots(|m| {
473 if let Some(slot) = m.get_mut(&gfd) {
474 let mut merged = unsent.to_vec();
475 merged.append(&mut slot.pending_out);
476 slot.pending_out = merged;
477 }
478 });
479}
480
481fn flush_tls(gfd: i32, host_fd: RawFd) -> io::Result<()> {
482 loop {
483 loop {
484 let (chunk, empty) = with_slots(|m| {
485 let Some(slot) = m.get_mut(&gfd) else {
486 return Err(io::Error::from_raw_os_error(libc::EBADF));
487 };
488 if slot.pending_out.is_empty() {
489 return Ok((Vec::new(), true));
490 }
491 let take = slot.pending_out.len().min(16 * 1024);
492 let chunk = slot.pending_out.drain(..take).collect::<Vec<u8>>();
493 Ok((chunk, false))
494 })?;
495 if empty {
496 break;
497 }
498 let mut off = 0_usize;
499 while off < chunk.len() {
500 let Some(rest) = chunk.get(off..) else {
501 break;
502 };
503 match write_raw(host_fd, rest) {
504 Ok(0) => {
505 requeue_unsent(gfd, chunk.get(off..).unwrap_or(&[]));
506 return Err(io::Error::new(
507 io::ErrorKind::WriteZero,
508 "tls wire write zero",
509 ));
510 }
511 Ok(w) => off = off.saturating_add(w),
512 Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
513 requeue_unsent(gfd, chunk.get(off..).unwrap_or(&[]));
514 return Err(e);
515 }
516 Err(e) => {
517 requeue_unsent(gfd, chunk.get(off..).unwrap_or(&[]));
518 return Err(e);
519 }
520 }
521 }
522 }
523
524 let extracted = with_slots(|m| {
525 let Some(slot) = m.get_mut(&gfd) else {
526 return Err(io::Error::from_raw_os_error(libc::EBADF));
527 };
528 if !slot.conn.wants_write() {
529 return Ok(0_usize);
530 }
531 let mut wire = [0_u8; 16 * 1024];
532 let mut out = TlsWireBuf {
533 buf: &mut wire,
534 filled: 0,
535 };
536 match slot.conn.write_tls(&mut out) {
537 Ok(0) => Ok(0_usize),
538 Ok(_) => {
539 let n = out.filled;
540 if let Some(bytes) = wire.get(..n) {
541 slot.pending_out.extend_from_slice(bytes);
542 }
543 Ok(n)
544 }
545 Err(e) if e.kind() == io::ErrorKind::WouldBlock => Ok(0_usize),
546 Err(e) => Err(e),
547 }
548 })?;
549 if extracted == 0 {
550 return Ok(());
551 }
552 }
553}
554
555struct TlsWireBuf<'a> {
556 buf: &'a mut [u8],
557 filled: usize,
558}
559
560impl Write for TlsWireBuf<'_> {
561 fn write(&mut self, data: &[u8]) -> io::Result<usize> {
562 let space = self.buf.len().saturating_sub(self.filled);
563 if space == 0 {
564 return Err(io::Error::from(io::ErrorKind::WouldBlock));
565 }
566 let n = space.min(data.len());
567 let end = self.filled.saturating_add(n);
568 let Some(dst) = self.buf.get_mut(self.filled..end) else {
569 return Err(io::Error::from(io::ErrorKind::WriteZero));
570 };
571 let Some(src) = data.get(..n) else {
572 return Err(io::Error::from(io::ErrorKind::WriteZero));
573 };
574 dst.copy_from_slice(src);
575 self.filled = end;
576 Ok(n)
577 }
578
579 fn flush(&mut self) -> io::Result<()> {
580 Ok(())
581 }
582}
583
584fn read_raw(fd: RawFd, buf: &mut [u8]) -> io::Result<usize> {
585 let n = unsafe { libc::read(fd, buf.as_mut_ptr().cast(), buf.len()) };
586 if n < 0 {
587 return Err(io::Error::last_os_error());
588 }
589 Ok(usize::try_from(n).unwrap_or(0))
590}
591
592fn write_raw(fd: RawFd, buf: &[u8]) -> io::Result<usize> {
593 let n = unsafe { libc::write(fd, buf.as_ptr().cast(), buf.len()) };
594 if n < 0 {
595 return Err(io::Error::last_os_error());
596 }
597 Ok(usize::try_from(n).unwrap_or(0))
598}
599
600fn build_config(verify: bool, ca_pem: Option<&Path>) -> Result<ClientConfig, String> {
601 if !verify {
602 let verifier = Arc::new(NoCertVerify);
603 return Ok(ClientConfig::builder()
604 .dangerous()
605 .with_custom_certificate_verifier(verifier)
606 .with_no_client_auth());
607 }
608
609 let mut roots = RootCertStore::empty();
610 let path = ca_pem.ok_or_else(|| "CA bundle path required when verify is on".to_owned())?;
611 let pem = std::fs::read(path).map_err(|e| format!("read CA {}: {e}", path.display()))?;
612 let mut cursor = std::io::Cursor::new(pem);
613 let certs: Vec<CertificateDer<'static>> = rustls_pemfile::certs(&mut cursor)
614 .collect::<Result<Vec<_>, _>>()
615 .map_err(|e| format!("parse CA PEM: {e}"))?;
616 if certs.is_empty() {
617 return Err(format!("no certs in CA bundle {}", path.display()));
618 }
619 for c in certs {
620 roots.add(c).map_err(|e| format!("add CA cert: {e}"))?;
621 }
622
623 Ok(ClientConfig::builder()
624 .with_root_certificates(roots)
625 .with_no_client_auth())
626}
627
628#[derive(Debug)]
629struct NoCertVerify;
630
631impl ServerCertVerifier for NoCertVerify {
632 fn verify_server_cert(
633 &self,
634 _end_entity: &CertificateDer<'_>,
635 _intermediates: &[CertificateDer<'_>],
636 _server_name: &ServerName<'_>,
637 _ocsp_response: &[u8],
638 _now: UnixTime,
639 ) -> Result<ServerCertVerified, TlsError> {
640 Ok(ServerCertVerified::assertion())
641 }
642
643 fn verify_tls12_signature(
644 &self,
645 _message: &[u8],
646 _cert: &CertificateDer<'_>,
647 _dss: &DigitallySignedStruct,
648 ) -> Result<HandshakeSignatureValid, TlsError> {
649 Ok(HandshakeSignatureValid::assertion())
650 }
651
652 fn verify_tls13_signature(
653 &self,
654 _message: &[u8],
655 _cert: &CertificateDer<'_>,
656 _dss: &DigitallySignedStruct,
657 ) -> Result<HandshakeSignatureValid, TlsError> {
658 Ok(HandshakeSignatureValid::assertion())
659 }
660
661 fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
662 vec![
663 SignatureScheme::ECDSA_NISTP256_SHA256,
664 SignatureScheme::ECDSA_NISTP384_SHA384,
665 SignatureScheme::RSA_PSS_SHA256,
666 SignatureScheme::RSA_PSS_SHA384,
667 SignatureScheme::RSA_PSS_SHA512,
668 SignatureScheme::RSA_PKCS1_SHA256,
669 SignatureScheme::ED25519,
670 ]
671 }
672}