Skip to main content

starnix_core/vfs/socket/
syscalls.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::bpf::attachments::SetSockOptProgramResult;
6use crate::mm::{IOVecPtr, MemoryAccessor, MemoryAccessorExt};
7use crate::security;
8use crate::syscalls::time::TimeSpecPtr;
9use crate::task::{CurrentTask, IpTables, Task, WaitCallback, Waiter};
10use crate::vfs::buffers::{
11    AncillaryData, ControlMsg, UserBuffersInputBuffer, UserBuffersOutputBuffer,
12};
13use crate::vfs::socket::{
14    SA_FAMILY_SIZE, SA_STORAGE_SIZE, Socket, SocketAddress, SocketDomain, SocketFile,
15    SocketMessageFlags, SocketPeer, SocketProtocol, SocketShutdownFlags, SocketType, UnixSocket,
16    resolve_unix_socket_address,
17};
18use crate::vfs::{FdFlags, FdNumber, FileHandle, FsString, LookupContext};
19use starnix_logging::{log_trace, track_stub};
20use starnix_types::augmented::Augmented;
21use starnix_types::time::duration_from_timespec;
22use starnix_types::user_buffer::{UserBuffer, UserBuffers};
23use starnix_uapi::auth::CAP_NET_BIND_SERVICE;
24use starnix_uapi::errors::{EEXIST, EINPROGRESS, Errno};
25use starnix_uapi::file_mode::FileMode;
26use starnix_uapi::math::round_up_to_increment;
27use starnix_uapi::open_flags::OpenFlags;
28use starnix_uapi::user_address::{
29    ArchSpecific, MappingMultiArchUserRef, MultiArchUserRef, UserAddress, UserRef,
30};
31use starnix_uapi::user_value::UserValue;
32use starnix_uapi::vfs::FdEvents;
33use starnix_uapi::{
34    MSG_CTRUNC, MSG_DONTWAIT, MSG_TRUNC, MSG_WAITFORONE, SHUT_RD, SHUT_RDWR, SHUT_WR, SOCK_CLOEXEC,
35    SOCK_NONBLOCK, UIO_MAXIOV, errno, error, socklen_t, uapi,
36};
37use std::ops::DerefMut;
38
39uapi::check_arch_independent_layout! {
40    socklen_t {}
41}
42
43/// A `msghdr` can be augmented with a `UserBuffer`. In that case, the `UserBuffer` is used for
44/// the I/O, instead of the `iovec` fields from the `msghdr`.
45pub type WithAlternateBuffer<T> = Augmented<T, UserBuffer>;
46pub type MsgHdrPtr = MappingMultiArchUserRef<MsgHdr, uapi::msghdr, uapi::arch32::msghdr>;
47
48#[derive(Debug, Clone)]
49pub struct MsgHdr {
50    pub name: UserAddress,
51    pub name_len: socklen_t,
52    pub iov: IOVecPtr,
53    pub iovlen: UserValue<usize>,
54    pub control: UserAddress,
55    pub control_len: usize,
56    pub flags: u32,
57}
58
59/// A reference to a `msghdr`.
60///
61/// This enum is used to abstract over whether the `msghdr` is in user memory (and needs to be
62/// read) or has been constructed in the kernel. This is used by `io_uring` to provide a buffer
63/// for `recvmsg`.
64#[derive(Debug, Clone)]
65pub enum MsgHdrRef {
66    Ptr(MsgHdrPtr),
67    Value(WithAlternateBuffer<MsgHdr>),
68}
69
70impl From<MsgHdrPtr> for MsgHdrRef {
71    fn from(ptr: MsgHdrPtr) -> Self {
72        Self::Ptr(ptr)
73    }
74}
75
76impl From<WithAlternateBuffer<MsgHdr>> for MsgHdrRef {
77    fn from(value: WithAlternateBuffer<MsgHdr>) -> Self {
78        Self::Value(value)
79    }
80}
81
82pub type MMsgHdrPtr = MappingMultiArchUserRef<MMsgHdr, uapi::mmsghdr, uapi::arch32::mmsghdr>;
83
84pub struct MMsgHdr {
85    hdr: MsgHdr,
86    len: usize,
87}
88
89uapi::arch_map_data! {
90    BidiTryFrom<MsgHdr, msghdr> {
91        name = msg_name;
92        name_len = msg_namelen;
93        iov = msg_iov;
94        iovlen = msg_iovlen;
95        control = msg_control;
96        control_len = msg_controllen;
97        flags = msg_flags;
98    }
99
100    BidiTryFrom<MMsgHdr, mmsghdr> {
101        hdr = msg_hdr;
102        len = msg_len;
103    }
104}
105
106pub type CMsgHdrPtr = MultiArchUserRef<uapi::cmsghdr, uapi::arch32::cmsghdr>;
107
108pub fn sys_socket(
109    current_task: &CurrentTask,
110    domain: u32,
111    socket_type: u32,
112    protocol: u32,
113) -> Result<FdNumber, Errno> {
114    let flags = socket_type & (SOCK_NONBLOCK | SOCK_CLOEXEC);
115    let domain = parse_socket_domain(domain)?;
116    let socket_type = parse_socket_type(domain, socket_type)?;
117    // Should we use parse_socket_protocol here?
118    let protocol = SocketProtocol::from_raw(protocol);
119    let open_flags = socket_flags_to_open_flags(flags);
120    let socket_file = SocketFile::new_socket(
121        current_task,
122        domain,
123        socket_type,
124        open_flags,
125        protocol,
126        /*kernel_private=*/ false,
127    )?;
128
129    let fd_flags = socket_flags_to_fd_flags(flags);
130    let fd = current_task.add_file(socket_file, fd_flags)?;
131    Ok(fd)
132}
133
134fn socket_flags_to_open_flags(flags: u32) -> OpenFlags {
135    OpenFlags::RDWR
136        | if flags & SOCK_NONBLOCK != 0 { OpenFlags::NONBLOCK } else { OpenFlags::empty() }
137}
138
139fn socket_flags_to_fd_flags(flags: u32) -> FdFlags {
140    if flags & SOCK_CLOEXEC != 0 { FdFlags::CLOEXEC } else { FdFlags::empty() }
141}
142
143fn parse_socket_domain(domain: u32) -> Result<SocketDomain, Errno> {
144    SocketDomain::from_raw(domain.try_into().map_err(|_| errno!(EAFNOSUPPORT))?).ok_or_else(|| {
145        track_stub!(TODO("https://fxbug.dev/322875074"), "parse socket domain", domain);
146        errno!(EAFNOSUPPORT)
147    })
148}
149
150fn parse_socket_type(domain: SocketDomain, socket_type: u32) -> Result<SocketType, Errno> {
151    let socket_type = SocketType::from_raw(socket_type & 0xf).ok_or_else(|| {
152        track_stub!(TODO("https://fxbug.dev/322875418"), "parse socket type", socket_type);
153        errno!(EINVAL)
154    })?;
155    // For AF_UNIX, SOCK_RAW sockets are treated as if they were SOCK_DGRAM.
156    Ok(if domain == SocketDomain::Unix && socket_type == SocketType::Raw {
157        SocketType::Datagram
158    } else {
159        socket_type
160    })
161}
162
163fn parse_socket_protocol(
164    domain: SocketDomain,
165    socket_type: SocketType,
166    protocol: u32,
167) -> Result<SocketProtocol, Errno> {
168    let protocol = SocketProtocol::from_raw(protocol);
169    if domain == SocketDomain::Inet {
170        match (socket_type, protocol) {
171            (SocketType::Raw, _) => {
172                // Should we have different behavior error when called by root?
173                return error!(EPROTONOSUPPORT);
174            }
175            (SocketType::Datagram, SocketProtocol::UDP) => (),
176            (SocketType::Datagram, _) => return error!(EPROTONOSUPPORT),
177            (SocketType::Stream, SocketProtocol::TCP) => (),
178            (SocketType::Stream, _) => return error!(EPROTONOSUPPORT),
179            _ => (),
180        }
181    }
182    Ok(protocol)
183}
184
185fn parse_socket_address(
186    task: &Task,
187    user_socket_address: UserAddress,
188    user_address_length: usize,
189) -> Result<SocketAddress, Errno> {
190    if user_address_length < SA_FAMILY_SIZE || user_address_length > SA_STORAGE_SIZE {
191        return error!(EINVAL);
192    }
193
194    let address = task.read_memory_to_vec(user_socket_address, user_address_length)?;
195
196    SocketAddress::from_bytes(address)
197}
198
199fn maybe_parse_socket_address(
200    task: &Task,
201    user_socket_address: UserAddress,
202    user_address_length: usize,
203) -> Result<Option<SocketAddress>, Errno> {
204    if user_address_length > i32::MAX as usize {
205        return error!(EINVAL);
206    }
207    Ok(if user_socket_address.is_null() {
208        None
209    } else {
210        Some(parse_socket_address(task, user_socket_address, user_address_length)?)
211    })
212}
213
214// See "Autobind feature" section of https://man7.org/linux/man-pages/man7/unix.7.html
215fn generate_autobind_address() -> FsString {
216    let mut bytes = [0u8; 4];
217    starnix_crypto::cprng_draw(&mut bytes);
218    let value = u32::from_ne_bytes(bytes) & 0xFFFFF;
219    format!("\0{value:05x}").into()
220}
221
222pub fn sys_bind(
223    current_task: &CurrentTask,
224    fd: FdNumber,
225    user_socket_address: UserAddress,
226    user_address_length: usize,
227) -> Result<(), Errno> {
228    let file = current_task.files().get(fd)?;
229    let socket = Socket::get_from_file(&file)?;
230    let address = parse_socket_address(current_task, user_socket_address, user_address_length)?;
231    if !address.valid_for_domain(socket.domain) {
232        return match socket.domain {
233            SocketDomain::Unix
234            | SocketDomain::Vsock
235            | SocketDomain::Inet6
236            | SocketDomain::Netlink
237            | SocketDomain::Key
238            | SocketDomain::Packet
239            | SocketDomain::Qipcrtr => error!(EINVAL),
240            SocketDomain::Inet => error!(EAFNOSUPPORT),
241        };
242    }
243    if let Some(port) = address.maybe_inet_port() {
244        // See <https://man7.org/linux/man-pages/man7/ip.7.html>:
245        //
246        //   The port numbers below 1024 are called privileged ports (or
247        //   sometimes: reserved ports).  Only a privileged process (on Linux:
248        //   a process that has the CAP_NET_BIND_SERVICE capability in the
249        //   user namespace governing its network namespace) may bind(2) to
250        //   these sockets.
251        if port != 0 && port < 1024 {
252            security::check_task_capable(current_task, CAP_NET_BIND_SERVICE)
253                .map_err(|_| errno!(EACCES))?;
254        }
255    }
256    security::check_socket_bind_access(current_task, socket, &address)?;
257    match address {
258        SocketAddress::Unspecified => return error!(EINVAL),
259        SocketAddress::Unix(mut name) => {
260            if name.is_empty() {
261                // If the name is empty, then we're supposed to generate an
262                // autobind address, which is always abstract.
263                name = generate_autobind_address();
264            }
265            // If there is a null byte at the start of the sun_path, then the
266            // address is abstract.
267            if name[0] == b'\0' {
268                current_task.running_state().abstract_socket_namespace.bind(
269                    current_task,
270                    name,
271                    socket,
272                )?;
273            } else {
274                let mode = file.node().info().mode;
275                let mode = current_task.fs().apply_umask(mode).with_type(FileMode::IFSOCK);
276                let (parent, basename) = current_task.lookup_parent_at(
277                    &mut LookupContext::default(),
278                    FdNumber::AT_FDCWD,
279                    name.as_ref(),
280                )?;
281
282                parent
283                    .bind_socket(
284                        current_task,
285                        basename,
286                        socket.clone(),
287                        SocketAddress::Unix(name.clone()),
288                        mode,
289                    )
290                    .map_err(|errno| if errno == EEXIST { errno!(EADDRINUSE) } else { errno })?;
291            }
292        }
293        SocketAddress::Vsock { port, .. } => {
294            current_task.running_state().abstract_vsock_namespace.bind(
295                current_task,
296                port,
297                socket,
298            )?;
299        }
300        SocketAddress::Inet(_)
301        | SocketAddress::Inet6(_)
302        | SocketAddress::Netlink(_)
303        | SocketAddress::Packet(_)
304        | SocketAddress::Qipcrtr(_) => socket.bind(current_task, address)?,
305    }
306
307    Ok(())
308}
309
310pub fn sys_listen(current_task: &CurrentTask, fd: FdNumber, backlog: i32) -> Result<(), Errno> {
311    let file = current_task.files().get(fd)?;
312    let socket = Socket::get_from_file(&file)?;
313    socket.listen(current_task, backlog)?;
314    Ok(())
315}
316
317pub fn sys_accept(
318    current_task: &CurrentTask,
319    fd: FdNumber,
320    user_socket_address: UserAddress,
321    user_address_length: UserRef<socklen_t>,
322) -> Result<FdNumber, Errno> {
323    sys_accept4(current_task, fd, user_socket_address, user_address_length, 0)
324}
325
326pub fn sys_accept4(
327    current_task: &CurrentTask,
328    fd: FdNumber,
329    user_socket_address: UserAddress,
330    user_address_length: UserRef<socklen_t>,
331    flags: u32,
332) -> Result<FdNumber, Errno> {
333    let file = current_task.files().get(fd)?;
334    let listening_socket = Socket::get_from_file(&file)?;
335    let accepted_socket =
336        file.blocking_op(current_task, FdEvents::POLLIN | FdEvents::POLLHUP, None, || {
337            listening_socket.accept(current_task)
338        })?;
339
340    if !user_socket_address.is_null() {
341        let address_bytes = accepted_socket.getpeername()?.to_bytes();
342        write_socket_address(
343            current_task,
344            user_socket_address,
345            user_address_length,
346            &address_bytes,
347        )?;
348    }
349
350    let open_flags = socket_flags_to_open_flags(flags);
351    let accepted_socket_file = SocketFile::from_socket(
352        current_task,
353        accepted_socket,
354        open_flags,
355        /* kernel_private= */ false,
356    )?;
357    let listening_socket = SocketFile::get_from_file(&file)?;
358    let accepted_socket = SocketFile::get_from_file(&accepted_socket_file)?;
359    security::socket_accept(current_task, listening_socket, accepted_socket)?;
360    let fd_flags = if flags & SOCK_CLOEXEC != 0 { FdFlags::CLOEXEC } else { FdFlags::empty() };
361    let accepted_fd = current_task.add_file(accepted_socket_file, fd_flags)?;
362    Ok(accepted_fd)
363}
364
365pub fn sys_connect(
366    current_task: &CurrentTask,
367    fd: FdNumber,
368    user_socket_address: UserAddress,
369    user_address_length: usize,
370) -> Result<(), Errno> {
371    let client = current_task.files().get(fd)?;
372    let client = SocketFile::get_from_file(&client)?;
373    let address = parse_socket_address(current_task, user_socket_address, user_address_length)?;
374    let peer = match address {
375        SocketAddress::Unspecified => return error!(EAFNOSUPPORT),
376        SocketAddress::Unix(ref name) => {
377            log_trace!("connect to unix socket named \"{name}\"");
378            if name.is_empty() {
379                return error!(ECONNREFUSED);
380            }
381            SocketPeer::Handle(resolve_unix_socket_address(current_task, name.as_ref())?)
382        }
383        // TODO(https://fxbug.dev/445433238): Connect not available for AF_VSOCK
384        SocketAddress::Vsock { .. } => return error!(ENOSYS),
385        SocketAddress::Inet(ref addr) | SocketAddress::Inet6(ref addr) => {
386            log_trace!("connect to inet socket named {:?}", addr);
387            SocketPeer::Address(address)
388        }
389        SocketAddress::Netlink(_) => SocketPeer::Address(address),
390        SocketAddress::Packet(ref addr) => {
391            log_trace!("connect to packet socket named {:?}", addr);
392            SocketPeer::Address(address)
393        }
394        SocketAddress::Qipcrtr(ref addr) => {
395            log_trace!("connect to qipcrtr socket named {:?}", addr);
396            SocketPeer::Address(address)
397        }
398    };
399    let result = client.connect(current_task, peer.clone());
400
401    if client.file().is_non_blocking() {
402        return result;
403    }
404
405    match result {
406        // EINPROGRESS may be returned for inet sockets when `connect()` is completed
407        // asynchronously.
408        Err(errno) if errno.code == EINPROGRESS => {
409            let waiter = Waiter::new();
410            client.file().wait_async(
411                current_task,
412                &waiter,
413                FdEvents::POLLOUT,
414                WaitCallback::none(),
415            );
416            if !client.file().query_events(current_task)?.contains(FdEvents::POLLOUT) {
417                waiter.wait(current_task)?;
418            }
419            client.connect(current_task, peer)
420        }
421        // TODO(tbodt): Support blocking when the UNIX domain socket queue fills up. This one's
422        // weird because as far as I can tell, removing a socket from the queue does not actually
423        // trigger FdEvents on anything.
424        result => result,
425    }
426}
427
428fn write_socket_address(
429    current_task: &CurrentTask,
430    user_socket_address: UserAddress,
431    user_address_length: UserRef<socklen_t>,
432    address_bytes: &[u8],
433) -> Result<(), Errno> {
434    let capacity = current_task.read_object(user_address_length)?;
435    if capacity > i32::MAX as socklen_t {
436        return error!(EINVAL);
437    }
438    let length = address_bytes.len() as socklen_t;
439    if length > 0 {
440        let actual = std::cmp::min(length, capacity) as usize;
441        current_task.write_memory(user_socket_address, &address_bytes[..actual])?;
442    }
443    current_task.write_object(user_address_length, &length)?;
444    Ok(())
445}
446
447pub fn sys_getsockname(
448    current_task: &CurrentTask,
449    fd: FdNumber,
450    user_socket_address: UserAddress,
451    user_address_length: UserRef<socklen_t>,
452) -> Result<(), Errno> {
453    let file = current_task.files().get(fd)?;
454    let socket = Socket::get_from_file(&file)?;
455    security::check_socket_getsockname_access(current_task, socket)?;
456    let address_bytes = socket.getsockname()?.to_bytes();
457
458    write_socket_address(current_task, user_socket_address, user_address_length, &address_bytes)?;
459
460    Ok(())
461}
462
463pub fn sys_getpeername(
464    current_task: &CurrentTask,
465    fd: FdNumber,
466    user_socket_address: UserAddress,
467    user_address_length: UserRef<socklen_t>,
468) -> Result<(), Errno> {
469    let file = current_task.files().get(fd)?;
470    let socket = Socket::get_from_file(&file)?;
471    security::check_socket_getpeername_access(current_task, socket)?;
472    let address_bytes = socket.getpeername()?.to_bytes();
473
474    write_socket_address(current_task, user_socket_address, user_address_length, &address_bytes)?;
475
476    Ok(())
477}
478
479pub fn sys_socketpair(
480    current_task: &CurrentTask,
481    domain: u32,
482    socket_type: u32,
483    protocol: u32,
484    user_sockets: UserRef<[FdNumber; 2]>,
485) -> Result<(), Errno> {
486    let flags = socket_type & (SOCK_NONBLOCK | SOCK_CLOEXEC);
487    let domain = parse_socket_domain(domain)?;
488    if !matches!(domain, SocketDomain::Unix | SocketDomain::Inet) {
489        return error!(EAFNOSUPPORT);
490    }
491    let socket_type = parse_socket_type(domain, socket_type)?;
492    let _protocol = parse_socket_protocol(domain, socket_type, protocol)?;
493    if domain != SocketDomain::Unix {
494        return error!(EOPNOTSUPP);
495    }
496    let open_flags = socket_flags_to_open_flags(flags);
497
498    let (left, right) = UnixSocket::new_pair(current_task, domain, socket_type, open_flags)?;
499
500    let fd_flags = socket_flags_to_fd_flags(flags);
501    // TODO: Eventually this will need to allocate two fd numbers (each of which could
502    // potentially fail), and only populate the fd numbers (which can't fail) if both allocations
503    // succeed.
504    let left_fd = current_task.add_file(left, fd_flags)?;
505    let right_fd = current_task.add_file(right, fd_flags)?;
506
507    let fds = [left_fd, right_fd];
508    log_trace!("socketpair -> [{:#x}, {:#x}]", fds[0].raw(), fds[1].raw());
509    current_task.write_object(user_sockets, &fds)?;
510
511    Ok(())
512}
513
514fn read_iovec_from_msghdr(
515    current_task: &CurrentTask,
516    message_header: WithAlternateBuffer<&MsgHdr>,
517) -> Result<UserBuffers, Errno> {
518    if let WithAlternateBuffer::WithAux(_, b) = message_header {
519        return Ok(UserBuffers::from_buf([b]));
520    }
521    let iovec_count = message_header.iovlen;
522
523    // In `CurrentTask::read_iovec()` the same check fails with `EINVAL`. This works for all
524    // syscalls that use `iovec`, except `sendmsg()` and `recvmsg()`, which need to fail with
525    // EMSGSIZE.
526    if iovec_count.raw() > UIO_MAXIOV as usize {
527        return error!(EMSGSIZE);
528    }
529
530    current_task.read_iovec(message_header.iov, iovec_count)
531}
532
533fn recvmsg_internal(
534    current_task: &CurrentTask,
535    file: &FileHandle,
536    user_message_header: &mut MsgHdrRef,
537    flags: u32,
538    deadline: Option<zx::MonotonicInstant>,
539) -> Result<usize, Errno> {
540    let mut message_header = match *user_message_header {
541        MsgHdrRef::Ptr(ptr) => current_task.read_multi_arch_object(ptr)?.into(),
542        MsgHdrRef::Value(ref value) => value.clone(),
543    };
544    let result =
545        recvmsg_internal_with_header(current_task, file, message_header.as_mut(), flags, deadline)?;
546    match *user_message_header {
547        MsgHdrRef::Ptr(ptr) => {
548            current_task.write_multi_arch_object(ptr, message_header.extract())?;
549        }
550        MsgHdrRef::Value(ref mut value) => {
551            *value.deref_mut() = message_header.extract();
552        }
553    }
554    Ok(result)
555}
556
557fn recvmsg_internal_with_header(
558    current_task: &CurrentTask,
559    file: &FileHandle,
560    mut message_header: WithAlternateBuffer<&mut MsgHdr>,
561    flags: u32,
562    deadline: Option<zx::MonotonicInstant>,
563) -> Result<usize, Errno> {
564    let iovec = read_iovec_from_msghdr(current_task, message_header.as_unmut())?;
565
566    let flags = SocketMessageFlags::from_bits(flags).ok_or_else(|| errno!(EINVAL))?;
567    let socket_ops = file.downcast_file::<SocketFile>().unwrap();
568    let info = socket_ops.recvmsg(
569        current_task,
570        file,
571        &mut UserBuffersOutputBuffer::unified_new(current_task, iovec)?,
572        flags,
573        deadline,
574    )?;
575
576    message_header.flags = 0;
577
578    let cmsg_buffer_size = message_header.control_len;
579
580    let mut cmsg_bytes_written = 0;
581    let header_size = CMsgHdrPtr::size_of_object_for(current_task);
582
583    for ancillary_data in info.ancillary_data {
584        if ancillary_data.total_size(current_task) == 0 {
585            // Skip zero-byte ancillary data on the receiving end. Not doing this trips this
586            // assert:
587            // https://cs.android.com/android/platform/superproject/+/master:system/libbase/cmsg.cpp;l=144;drc=15ec2c7a23cda814351a064a345a8270ed8c83ab
588            continue;
589        }
590
591        // Calculate the offset where the current message will be written, after alignment.
592        let aligned_offset = cmsg_align(current_task, cmsg_bytes_written)?;
593        let space_available = cmsg_buffer_size.saturating_sub(aligned_offset);
594
595        if space_available < header_size {
596            // Can't fit the header, so stop trying to write.
597            message_header.flags |= MSG_CTRUNC;
598            break;
599        }
600
601        let conversion = ancillary_data.into_bytes(current_task, flags, space_available)?;
602
603        if conversion.truncated {
604            message_header.flags |= MSG_CTRUNC;
605        }
606
607        assert!(aligned_offset + conversion.bytes.len() <= cmsg_buffer_size);
608        // Write the message at the aligned offset.
609        current_task.write_memory((message_header.control + aligned_offset)?, &conversion.bytes)?;
610        // Update the total bytes written to the end of this message.
611        cmsg_bytes_written = std::cmp::min(
612            cmsg_align(current_task, aligned_offset + conversion.bytes.len())?,
613            cmsg_buffer_size,
614        );
615    }
616
617    message_header.control_len = cmsg_bytes_written;
618
619    let msg_name = message_header.name;
620    if !msg_name.is_null() {
621        if message_header.name_len > i32::MAX as u32 {
622            return error!(EINVAL);
623        }
624        let bytes = info.address.map(|a| a.to_bytes()).unwrap_or_else(|| vec![]);
625        let num_bytes = std::cmp::min(message_header.name_len as usize, bytes.len());
626        message_header.name_len = bytes.len() as u32;
627        if num_bytes > 0 {
628            current_task.write_memory(msg_name, &bytes[..num_bytes])?;
629        }
630    }
631
632    if info.bytes_read != info.message_length {
633        message_header.flags |= MSG_TRUNC;
634    }
635
636    if flags.contains(SocketMessageFlags::TRUNC) {
637        Ok(info.message_length)
638    } else {
639        Ok(info.bytes_read)
640    }
641}
642
643pub fn sys_recvmsg(
644    current_task: &CurrentTask,
645    fd: FdNumber,
646    user_message_header: MsgHdrPtr,
647    flags: u32,
648) -> Result<usize, Errno> {
649    recvmsg_impl(current_task, fd, &mut user_message_header.into(), flags)
650}
651
652/// Implementation of `recvmsg`.
653///
654/// This function is used by `sys_recvmsg`, but can also be called from other parts of the kernel
655/// that need to override the `iovec` from the `msghdr`. For example, when using `io_uring` with
656/// ring buffers.
657pub fn recvmsg_impl(
658    current_task: &CurrentTask,
659    fd: FdNumber,
660    user_message_header: &mut MsgHdrRef,
661    flags: u32,
662) -> Result<usize, Errno> {
663    let file = current_task.files().get(fd)?;
664    if !file.node().is_sock() {
665        return error!(ENOTSOCK);
666    }
667    recvmsg_internal(current_task, &file, user_message_header, flags, None)
668}
669
670pub fn sys_recvmmsg(
671    current_task: &CurrentTask,
672    fd: FdNumber,
673    user_mmsgvec: MMsgHdrPtr,
674    vlen: u32,
675    mut flags: u32,
676    user_timeout: TimeSpecPtr,
677) -> Result<usize, Errno> {
678    let file = current_task.files().get(fd)?;
679    if !file.node().is_sock() {
680        return error!(ENOTSOCK);
681    }
682
683    if vlen > UIO_MAXIOV {
684        return error!(EINVAL);
685    }
686
687    let deadline = if user_timeout.is_null() {
688        None
689    } else {
690        let ts = current_task.read_multi_arch_object(user_timeout)?;
691        Some(zx::MonotonicInstant::after(duration_from_timespec(ts)?))
692    };
693
694    let mut index = 0usize;
695    while index < vlen as usize {
696        let current_ptr = user_mmsgvec.at(index)?;
697        let mut current_mmsghdr = current_task.read_multi_arch_object(current_ptr)?;
698        match recvmsg_internal_with_header(
699            current_task,
700            &file,
701            (&mut current_mmsghdr.hdr).into(),
702            flags,
703            deadline,
704        ) {
705            Err(error) => {
706                if index == 0 {
707                    return Err(error);
708                }
709                break;
710            }
711            Ok(bytes_read) => {
712                current_mmsghdr.len = bytes_read;
713                current_task.write_multi_arch_object(current_ptr, current_mmsghdr)?;
714            }
715        }
716        index += 1;
717        if flags & MSG_WAITFORONE != 0 {
718            flags |= MSG_DONTWAIT;
719        }
720    }
721    Ok(index)
722}
723
724pub fn sys_recvfrom(
725    current_task: &CurrentTask,
726    fd: FdNumber,
727    user_buffer: UserAddress,
728    buffer_length: usize,
729    flags: u32,
730    user_src_address: UserAddress,
731    user_src_address_length: UserRef<socklen_t>,
732) -> Result<usize, Errno> {
733    let file = current_task.files().get(fd)?;
734    if !file.node().is_sock() {
735        return error!(ENOTSOCK);
736    }
737
738    let flags = SocketMessageFlags::from_bits(flags).ok_or_else(|| errno!(EINVAL))?;
739    let socket_ops = file.downcast_file::<SocketFile>().unwrap();
740    let info = socket_ops.recvmsg(
741        current_task,
742        &file,
743        &mut UserBuffersOutputBuffer::unified_new_at(current_task, user_buffer, buffer_length)?,
744        flags,
745        None,
746    )?;
747
748    if !user_src_address.is_null() {
749        let bytes = info.address.map(|a| a.to_bytes()).unwrap_or_else(|| vec![]);
750        write_socket_address(current_task, user_src_address, user_src_address_length, &bytes)?;
751    }
752
753    if flags.contains(SocketMessageFlags::TRUNC) {
754        Ok(info.message_length)
755    } else {
756        Ok(info.bytes_read)
757    }
758}
759
760fn sendmsg_internal(
761    current_task: &CurrentTask,
762    file: &FileHandle,
763    user_message_header: MsgHdrPtr,
764    flags: u32,
765) -> Result<usize, Errno> {
766    let message_header = current_task.read_multi_arch_object(user_message_header)?;
767    sendmsg_internal_with_header(current_task, file, &message_header, flags)
768}
769
770fn sendmsg_internal_with_header(
771    current_task: &CurrentTask,
772    file: &FileHandle,
773    message_header: &MsgHdr,
774    flags: u32,
775) -> Result<usize, Errno> {
776    if message_header.name_len > i32::MAX as u32 {
777        return error!(EINVAL);
778    }
779    if message_header.control_len > 20480 {
780        return error!(ENOBUFS);
781    }
782    let dest_address = maybe_parse_socket_address(
783        current_task,
784        message_header.name,
785        message_header.name_len as usize,
786    )?;
787    let iovec = read_iovec_from_msghdr(current_task, message_header.into())?;
788
789    let mut next_message_offset: usize = 0;
790    let mut ancillary_data = Vec::new();
791    let header_size = CMsgHdrPtr::size_of_object_for(current_task);
792    loop {
793        let space = message_header.control_len.saturating_sub(next_message_offset);
794        if space < header_size {
795            break;
796        }
797        let cmsg_ref =
798            CMsgHdrPtr::new(current_task, (message_header.control + next_message_offset)?);
799        let cmsg = current_task.read_multi_arch_object(cmsg_ref)?;
800        // If the message header is not long enough to fit the required fields of the
801        // control data, return EINVAL.
802        if (cmsg.cmsg_len as usize) < header_size {
803            return error!(EINVAL);
804        }
805
806        let data_size = std::cmp::min(cmsg.cmsg_len as usize - header_size, space);
807        let next_data_offset = next_message_offset + header_size;
808        let data = current_task
809            .read_memory_to_vec((message_header.control + next_data_offset)?, data_size)?;
810        next_message_offset += cmsg_align(current_task, header_size + data.len())?;
811        let data = AncillaryData::from_cmsg(
812            current_task,
813            ControlMsg::new(cmsg.cmsg_level, cmsg.cmsg_type, data),
814        )?;
815        if data.total_size(current_task) == 0 {
816            continue;
817        }
818        ancillary_data.push(data);
819    }
820
821    let flags = SocketMessageFlags::from_bits(flags).ok_or_else(|| errno!(EOPNOTSUPP))?;
822    let socket_ops = file.downcast_file::<SocketFile>().unwrap();
823    socket_ops.sendmsg(
824        current_task,
825        file,
826        &mut UserBuffersInputBuffer::unified_new(current_task, iovec)?,
827        dest_address,
828        ancillary_data,
829        flags,
830    )
831}
832
833pub fn sys_sendmsg(
834    current_task: &CurrentTask,
835    fd: FdNumber,
836    user_message_header: MsgHdrPtr,
837    flags: u32,
838) -> Result<usize, Errno> {
839    let file = current_task.files().get(fd)?;
840    if !file.node().is_sock() {
841        return error!(ENOTSOCK);
842    }
843    sendmsg_internal(current_task, &file, user_message_header, flags)
844}
845
846pub fn sys_sendmmsg(
847    current_task: &CurrentTask,
848    fd: FdNumber,
849    user_mmsgvec: MMsgHdrPtr,
850    mut vlen: u32,
851    flags: u32,
852) -> Result<usize, Errno> {
853    let file = current_task.files().get(fd)?;
854    if !file.node().is_sock() {
855        return error!(ENOTSOCK);
856    }
857
858    // vlen is capped at UIO_MAXIOV.
859    if vlen > UIO_MAXIOV {
860        vlen = UIO_MAXIOV;
861    }
862
863    let mut index = 0usize;
864    while index < vlen as usize {
865        let current_ptr = user_mmsgvec.at(index)?;
866        let mut current_mmsghdr = current_task.read_multi_arch_object(current_ptr)?;
867        match sendmsg_internal_with_header(current_task, &file, &current_mmsghdr.hdr, flags) {
868            Err(error) => {
869                if index == 0 {
870                    return Err(error);
871                }
872                break;
873            }
874            Ok(bytes_read) => {
875                current_mmsghdr.len = bytes_read;
876                current_task.write_multi_arch_object(current_ptr, current_mmsghdr)?;
877            }
878        }
879        index += 1;
880    }
881    Ok(index)
882}
883
884pub fn sys_sendto(
885    current_task: &CurrentTask,
886    fd: FdNumber,
887    user_buffer: UserAddress,
888    user_buffer_length: usize,
889    flags: u32,
890    user_dest_address: UserAddress,
891    user_dest_address_length: socklen_t,
892) -> Result<usize, Errno> {
893    let file = current_task.files().get(fd)?;
894    if !file.node().is_sock() {
895        return error!(ENOTSOCK);
896    }
897
898    let dest_address = maybe_parse_socket_address(
899        current_task,
900        user_dest_address,
901        user_dest_address_length as usize,
902    )?;
903    let mut data =
904        UserBuffersInputBuffer::unified_new_at(current_task, user_buffer, user_buffer_length)?;
905
906    let flags = SocketMessageFlags::from_bits(flags).ok_or_else(|| errno!(EOPNOTSUPP))?;
907    let socket_file = file.downcast_file::<SocketFile>().unwrap();
908    socket_file.sendmsg(current_task, &file, &mut data, dest_address, vec![], flags)
909}
910
911pub fn sys_getsockopt(
912    current_task: &CurrentTask,
913    fd: FdNumber,
914    level: u32,
915    optname: u32,
916    user_optval: UserAddress,
917    user_optlen: UserRef<socklen_t>,
918) -> Result<(), Errno> {
919    let file = current_task.files().get(fd)?;
920    let socket = Socket::get_from_file(&file)?;
921
922    let optlen = current_task.read_object(user_optlen)? as usize;
923    let optval_buffer_len = optlen;
924    let mut optval = current_task.read_memory_to_vec(user_optval, optlen as usize)?;
925
926    let result = if socket.domain.is_inet() && IpTables::can_handle_getsockopt(level, optname) {
927        current_task.kernel().iptables().getsockopt(current_task, socket, optname, optval.clone())
928    } else {
929        socket.getsockopt(current_task, level, optname, optlen as u32)
930    };
931
932    // Even if `getsockopt()` above returned an error we still need to run
933    // the eBPF program - it may handle the error.
934    let (optlen, error) = match result {
935        Ok(new_optval) => {
936            // Linux getsockopt allows the user to pass a buffer smaller than the option's
937            // actual size, in which case the kernel truncates the returned value to fit
938            // the buffer and returns success.
939            //
940            // On the other hand, if the option is smaller than the user provided buffer,
941            // the eBPF program can still use the entire user allocated buffer if needed.
942            let len = std::cmp::min(new_optval.len(), optval.len());
943            optval[..len].copy_from_slice(&new_optval[..len]);
944            (len, None)
945        }
946        Err(e) => (optlen, Some(e)),
947    };
948
949    let root_cgroup = current_task.kernel().ebpf_state.attachments.root_cgroup();
950    let (optval, optlen) = root_cgroup.run_getsockopt_prog(
951        current_task,
952        level,
953        optname,
954        optval,
955        optlen,
956        error,
957        socket,
958    )?;
959
960    assert!(optlen <= optval_buffer_len);
961    current_task.write_memory(user_optval, &optval[..optlen])?;
962    current_task.write_object(user_optlen, &(optlen as u32))?;
963
964    Ok(())
965}
966
967pub fn sys_setsockopt(
968    current_task: &CurrentTask,
969    fd: FdNumber,
970    level: u32,
971    optname: u32,
972    user_optval: UserAddress,
973    optlen: socklen_t,
974) -> Result<(), Errno> {
975    let file = current_task.files().get(fd)?;
976    let socket = Socket::get_from_file(&file)?;
977
978    let user_opt = UserBuffer { address: user_optval, length: optlen as usize };
979
980    // Run eBPF program if any.
981    let root_cgroup = current_task.kernel().ebpf_state.attachments.root_cgroup();
982    let optval = match root_cgroup.run_setsockopt_prog(
983        current_task,
984        level,
985        optname,
986        user_opt.into(),
987        socket,
988    ) {
989        SetSockOptProgramResult::Allow(value) => value,
990        SetSockOptProgramResult::Fail(errno) => return Err(errno),
991        SetSockOptProgramResult::Bypass => return Ok(()), // The option was handled by eBPF.
992    };
993
994    if socket.domain.is_inet() && IpTables::can_handle_setsockopt(level, optname) {
995        current_task.kernel().iptables().setsockopt(current_task, socket, optname, optval)
996    } else {
997        socket.setsockopt(current_task, level, optname, optval)
998    }
999}
1000
1001pub fn sys_shutdown(current_task: &CurrentTask, fd: FdNumber, how: u32) -> Result<(), Errno> {
1002    let file = current_task.files().get(fd)?;
1003    let socket = Socket::get_from_file(&file)?;
1004    let how = match how {
1005        SHUT_RD => SocketShutdownFlags::READ,
1006        SHUT_WR => SocketShutdownFlags::WRITE,
1007        SHUT_RDWR => SocketShutdownFlags::READ | SocketShutdownFlags::WRITE,
1008        _ => return error!(EINVAL),
1009    };
1010    socket.shutdown(current_task, how)?;
1011    Ok(())
1012}
1013
1014pub fn cmsg_align(current_task: &CurrentTask, value: usize) -> Result<usize, Errno> {
1015    let alignment = if current_task.is_arch32() { 4 } else { 8 };
1016    round_up_to_increment(value, alignment)
1017}
1018
1019// Syscalls for arch32 usage
1020#[cfg(target_arch = "aarch64")]
1021mod arch32 {
1022    use crate::task::CurrentTask;
1023    use crate::vfs::FdNumber;
1024    use starnix_uapi::errors::Errno;
1025    use starnix_uapi::user_address::UserAddress;
1026
1027    pub use super::{
1028        sys_accept as sys_arch32_accept, sys_accept4 as sys_arch32_accept4,
1029        sys_bind as sys_arch32_bind, sys_getpeername as sys_arch32_getpeername,
1030        sys_getsockname as sys_arch32_getsockname, sys_getsockopt as sys_arch32_getsockopt,
1031        sys_listen as sys_arch32_listen, sys_recvfrom as sys_arch32_recvfrom,
1032        sys_recvmmsg as sys_arch32_recvmmsg, sys_recvmsg as sys_arch32_recvmsg,
1033        sys_sendmsg as sys_arch32_sendmsg, sys_sendto as sys_arch32_sendto,
1034        sys_setsockopt as sys_arch32_setsockopt, sys_shutdown as sys_arch32_shutdown,
1035        sys_socketpair as sys_arch32_socketpair,
1036    };
1037
1038    pub fn sys_arch32_send(
1039        current_task: &CurrentTask,
1040        fd: FdNumber,
1041        user_buffer: UserAddress,
1042        user_buffer_length: usize,
1043        flags: u32,
1044    ) -> Result<usize, Errno> {
1045        super::sys_sendto(
1046            current_task,
1047            fd,
1048            user_buffer,
1049            user_buffer_length,
1050            flags,
1051            Default::default(),
1052            Default::default(),
1053        )
1054    }
1055
1056    pub fn sys_arch32_recv(
1057        current_task: &CurrentTask,
1058        fd: FdNumber,
1059        user_buffer: UserAddress,
1060        buffer_length: usize,
1061        flags: u32,
1062    ) -> Result<usize, Errno> {
1063        super::sys_recvfrom(
1064            current_task,
1065            fd,
1066            user_buffer,
1067            buffer_length,
1068            flags,
1069            Default::default(),
1070            Default::default(),
1071        )
1072    }
1073}
1074
1075#[cfg(target_arch = "aarch64")]
1076pub use arch32::*;
1077
1078#[cfg(test)]
1079mod tests {
1080    use super::*;
1081    use crate::testing::spawn_kernel_and_run;
1082    use starnix_uapi::{AF_INET, AF_UNIX, SOCK_STREAM};
1083
1084    #[::fuchsia::test]
1085    async fn test_socketpair_invalid_arguments() {
1086        spawn_kernel_and_run(async |current_task| {
1087            assert_eq!(
1088                sys_socketpair(
1089                    current_task,
1090                    AF_INET as u32,
1091                    SOCK_STREAM,
1092                    0,
1093                    UserRef::new(UserAddress::default())
1094                ),
1095                error!(EPROTONOSUPPORT)
1096            );
1097            assert_eq!(
1098                sys_socketpair(
1099                    current_task,
1100                    AF_UNIX as u32,
1101                    7,
1102                    0,
1103                    UserRef::new(UserAddress::default())
1104                ),
1105                error!(EINVAL)
1106            );
1107            assert_eq!(
1108                sys_socketpair(
1109                    current_task,
1110                    AF_UNIX as u32,
1111                    SOCK_STREAM,
1112                    0,
1113                    UserRef::new(UserAddress::default())
1114                ),
1115                error!(EFAULT)
1116            );
1117        })
1118        .await;
1119    }
1120
1121    #[::fuchsia::test]
1122    fn test_generate_autobind_address() {
1123        let address = generate_autobind_address();
1124        assert_eq!(address.len(), 6);
1125        assert_eq!(address[0], 0);
1126        for byte in address[1..].iter() {
1127            match byte {
1128                b'0'..=b'9' | b'a'..=b'f' => {
1129                    // Ok.
1130                }
1131                bad => {
1132                    panic!("bad byte: {bad}");
1133                }
1134            }
1135        }
1136    }
1137}