Commit Graph

593 Commits

Author SHA1 Message Date
Alan Somers
8e91b28b64 Fix UB in the SO_TYPE sockopt
When reading a value into an enum from getsockopt, we must validate it.
Failing to do so can lead to UB for example with SOCK_PACKET on Linux.

Perform the validation in GetSockOpt::get.  Currently SockType is the
only type that requires validation.

Fixes #1819
2022-11-28 23:07:54 -07:00
bors[bot]
33a4d495d9
Merge #1865
1865: Add IpMtu sockopt r=asomers a=ShadowJonathan

Resolves https://github.com/nix-rust/nix/issues/1864

Co-authored-by: Jonathan de Jong <jonathandejong02@gmail.com>
2022-11-25 22:35:51 +00:00
Alan Somers
0ae109ddad Clippy cleanup with the latest nightly. 2022-11-25 08:27:55 -07:00
Jonathan de Jong
e9f07eea61 add IpMtu sockopt 2022-11-23 10:48:48 +00:00
bors[bot]
79f04fb3a3
Merge #1857
1857: Add better support for unnamed unix socket addrs r=asomers a=stevenengler

This adds the following 2 functions/methods: `UnixAddr::new_unnamed` and `UnixAddr::is_unnamed`.

Closes #1585

unix(7) on Linux:

> unnamed: A stream socket that has not been bound to a pathname using bind(2) has no name. Likewise, the two sockets created by socketpair(2) are unnamed. When the address of an unnamed socket is returned, its length is `sizeof(sa_family_t)`, and `sun_path` should not be inspected.

**Edit:** This currently isn't working on BSD, but I see why. Will fix it shortly.

Co-authored-by: Steven Engler <opara@cs.georgetown.edu>
2022-11-21 17:45:29 +00:00
Steven Engler
f6a22198a4 fixup! Added better support for unnamed unix socket addrs
Removed test assertion
2022-11-21 12:11:50 -05:00
Steven Engler
49bab984ee fixup! Added better support for unnamed unix socket addrs
Make Linux-only
2022-11-21 12:03:11 -05:00
Steven Engler
8884ea38ec Added better support for unnamed unix socket addrs 2022-11-21 12:03:11 -05:00
Jonathan
63c56263fb mmap non-zero length 2022-11-20 22:53:40 +00:00
bors[bot]
33b5f928ea
Merge #1853
1853: Adds IP_TOS, IPV6_TCLASS and SO_PRIORITY sockopt wrappers for Linux r=asomers a=mzachar

Added socket option wrappers for DiffServ related parameters on Linux

Co-authored-by: mzachar <mzachar@users.noreply.github.com>
2022-11-20 22:03:22 +00:00
Jonathan
d34696c84b mmap addr 2022-11-20 17:16:25 +00:00
mzachar
faac24c779 Adds IP_TOS, IPV6_TCLASS and SO_PRIORITY sockopt wrappers 2022-11-20 13:41:26 +01:00
Alex Saveau
58f99947bf
Run a round of clippy to fix CI
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
2022-11-06 11:40:46 -08:00
Michael Baikov
19c83afbbf reimplement recvmsg/sendmmsg
New implementation performs no allocations after all the necessary
structures are created, removes potentially unsound code that
was used by the old version (see below) and adds a bit more
documentation about bugs in how timeout is actually handled

```
    let timeout = if let Some(mut t) = timeout {
        t.as_mut() as *mut libc::timespec
    } else {
        ptr::null_mut()
    };
```
2022-10-14 08:57:41 -04:00
Steve Lau
04e409b376 add eaccess on FreeBSD, DragonFly and Linux (musl and glibc) 2022-10-13 09:23:09 +08:00
Ryan Zoeller
6e7bddd154 Fix clippy warnings on nightly
Clippy is now smarter about detecting unnecessary casts and
useless conversions, which means we need to be more explicit
about when the conversions are needed for a subset of platforms.

Required changes found by repeatedly running the following command
against a list of the supported platforms.

`xargs -t -I {} sh -c "cargo clippy -Zbuild-std --target {} --all-targets -- -D warnings || exit 255"`

I removed the casts it complained about, and then restored them
with an `#[allow]` if a later target needed the cast.
2022-10-08 14:08:54 -05:00
Jonas Schievink
945f743aa7 Add a sched_getcpu wrapper 2022-09-27 14:26:35 +02:00
Alan Somers
3aedbfa08e Clippy cleanup 2022-09-11 16:25:33 -06:00
Ryan Zoeller
3dc163e780 Add sched_getaffinity and sched_setaffinity on FreeBSD 2022-08-25 18:42:20 -05:00
Alan Somers
c6371db662 Fix a new clippy lint 2022-08-14 10:12:57 -06:00
bors[bot]
ffc793ed4d
Merge #1776
1776: Add support for the IP_SENDSRCADDR control message r=rtzoeller a=matttpt

This control message is available on FreeBSD, NetBSD, and OpenBSD. When used with `sendmsg`, it sets the IPv4 source address. This adds support through a new `ControlMessage::Ipv4SendSrcAddr` variant that complements `ControlMessageOwned::Ipv4RecvDstAddr`.

A few notes:
* `IP_SENDSRCADDR` is actually just an alias for `IP_RECVDSTADDR` (though the code doesn't depend on this).
* On NetBSD, `IP_PKTINFO` can be used to accomplish the same thing and is already supported by nix. On FreeBSD and OpenBSD, though, `IP_SENDSRCADDR` is the only method I'm aware of.
* The accompanying test binds a UDP socket to all local interfaces (0.0.0.0). If this is not acceptable, please let me know; however, FreeBSD requires this to use `IP_SENDSRCADDR`.

I'll add a change-log entry once I see the PR number.

Thanks!

Co-authored-by: Matthew Ingwersen <matttpt@gmail.com>
2022-08-12 04:38:44 +00:00
SteveLau
9161a294d7
Merge branch 'nix-rust:master' into remove-deprecated-items 2022-08-12 08:57:10 +08:00
Michael Baikov
384d47d25e Folloup for !1778, remove some of the less helpful error msgs 2022-08-12 08:29:29 +08:00
SteveLauC
347915bdb5 format code 2022-08-09 12:17:27 +08:00
SteveLauC
d458ecd4e1 remove deprecated items 2022-08-09 12:15:52 +08:00
Zhang Miaolei
f8c79c63e5 add faccessat 2022-08-05 20:15:51 -04:00
Matthew Ingwersen
6bf07fdbb9 Add support for the IP_SENDSRCADDR control message
This control message (actually just an alias for IP_RECVDSTADDR) sets
the IPv4 source address when used with sendmsg. It is available on
FreeBSD, NetBSD, OpenBSD, and DragonFlyBSD.
2022-08-05 12:35:57 -04:00
Michael Baikov
a6ee63ac32 fix clippy assertions_on_result_states
https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_result_states
2022-08-04 09:44:40 +08:00
Brian May
c45cd74f42 Add support for RecvOrigDstAddr on Linux
Fixes #1767
2022-07-26 07:39:00 +10:00
Alan Somers
e0e768e7b9 Fix a buffer overflow in sys::socket::recvfrom
IPv4 and stream sockets are unaffected, but for datagram sockets of
other address types libc::recvfrom might overwrite part of the stack.

Fixes #1762
2022-07-14 11:37:56 -06:00
musikid
137a9abb66 Add chflags 2022-07-13 21:27:36 -06:00
Alan Somers
dc1a34b864 Clippy cleanup in the tests.
* Remove a redundant closure.
* Comparison with null
* Manual implementation of find
* Suppress a false positive
2022-07-10 08:43:04 -06:00
Costin-Robert Sin
3e6cb639f0 Fix all formating problems to pass CI formating test
Signed-off-by: Costin-Robert Sin <sin.costinrobert@gmail.com>
2022-06-24 00:35:52 +03:00
bors[bot]
01a5927e2c
Merge #1739
1739: ppoll: make sigmask parameter optional r=rtzoeller a=stefano-garzarella

ppoll(2) supports 'sigmask' as NULL. In that case no signal mask
manipulation is performed.

Let's make `sigmask` parameter of `nix::poll::ppoll` optional
to allow that behaviour.

Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>

Co-authored-by: Stefano Garzarella <sgarzare@redhat.com>
2022-06-09 11:31:52 +00:00
Stefano Garzarella
c3081e4896 ppoll: make sigmask parameter optional
ppoll(2) supports 'sigmask' as NULL. In that case no signal mask
manipulation is performed.

Let's make `sigmask` parameter of `nix::poll::ppoll` optional
to allow that behaviour.

Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
2022-06-09 09:09:23 +02:00
bors[bot]
32d597bbd6
Merge #1735
1735: Cleanup cfg blocks r=asomers a=rtzoeller

Remove obsolete references to target_env = wasi, target_os = nacl, target_os = osx, and a typo'd target_os = fushsia that didn't compile when fixed.

 - target_env = wasi is dead: https://github.com/rust-lang/rust/pull/60117
 - target_os = nacl is dead: https://github.com/rust-lang/rust/pull/45041
 - target_os = osx is dead, but I can't find a link.

Found while exploring `--check-cfg`, as mentioned in #1734.

Co-authored-by: Ryan Zoeller <rtzoeller@rtzoeller.com>
2022-06-09 03:52:06 +00:00
Alan Somers
c8a9cf3351 Fix the test_acct test
It has never actually executed its command, so the only reason that it
ever worked is that on most systems there are usually processes starting
and exiting all the time.
2022-06-07 20:10:46 -06:00
Ryan Zoeller
80f8320ffd Cleanup cfg blocks
Remove obsolete references to target_env = wasi, target_os = nacl,
target_os = osx, and a typo'd target_os = fushsia that didn't compile
when fixed.

 - target_env = wasi is dead: https://github.com/rust-lang/rust/pull/60117
 - target_os = nacl is dead: https://github.com/rust-lang/rust/pull/45041
 - target_os = osx is dead, but I can't find a link.
2022-06-06 18:50:19 -05:00
Ryan Zoeller
598925c78f Fix nightly clippy in tests 2022-05-15 23:32:24 -05:00
Nikita Baksalyar
c1c1c6cfe4 Add ptrace::read_user and ptrace::write_user 2022-05-15 23:36:02 +01:00
Al Hoang
d02e27476c add haiku support
* enabled as much functionality and defines that match
  updated libc definitions for haiku
2022-05-14 18:45:18 -05:00
Alan Somers
0c07a9e469 Rewrite the aio module
The existing AIO implementation has some problems:
1) The in_progress field is checked at runtime, not compile time.
2) The mutable field is checked at runtime, not compile time.
3) A downstream lio_listio user must store extra state to track whether
   the whole operation is partially, completely, or not at all
   submitted.
4) Nix does heap allocation itself, rather than allowing the caller to
   choose it.  This can result in double (or triple, or quadruple)
   boxing.
5) There's no easy way to use lio_listio to submit multiple operations with
   a single syscall, but poll each individually.
6) The lio_listio usage is far from transparent and zero-cost.
7) No aio_readv or aio_writev support.
8) priority has type c_int; should be i32
9) aio_return should return a usize instead of an isize, since it only
   uses negative values to indicate errors, which Rust represents via
   the Result type.

This rewrite solves several problems:
1) Unsolved.  I don't think it can be solved without something like
   C++'s guaranteed type elision.  It might require changing the
   signature of Future::poll too.
2) Solved.
3) Solved, by the new in_progress method and by removing the complicated
   lio_listio resubmit code.
4) Solved.
5) Solved.
6) Solved, by removing the lio_listo resubmit code.  It can be
   reimplemented downstream if necessary.  Or even in Nix, but it
   doesn't fit Nix's theme of zero-cost abstractions.
7) Solved.
8) Solved.
9) Solved.

The rewrite includes functions that don't work on FreeBSD, so add CI
testing for FreeBSD 14 too.

By default only enable tests that will pass on FreeBSD 12.3.  But run a
CI job on FreeBSD 14 and set a flag that will enable such tests.
2022-05-14 13:34:11 -06:00
Kyle Huey
024c0d7dde Restore conversions from ip v4/6 Sockaddr types to std::net equivalents.
Fixes #1710
2022-05-02 08:53:08 -07:00
Ryan Zoeller
e543733a4c Upgrade sysctl to 0.4
Upgrade sysctl dev-dependency to 0.4 and handle its breaking API changes.
2022-04-24 19:25:16 -05:00
bors[bot]
256707e8c7
Merge #1643
1643: Replace the IoVec struct with IoSlice and IoSliceMut from the standard library r=asomers a=notgull

As per discussion in #1637, the `IoVec<&[u8]>` and `IoVec<&mut [u8]>` types have been replaced with `std::io::IoSlice` and `IoSliceMut`, respectively. Notable changes made in this pull request include:

- The complete replacement of `IoVec` with `IoSlice*` types in both public API, private API, and tests.
- Replacing `IoVec` with `IoSlice` in docs.
- Replacing `&[IoVec<&mut [u8]>]` with `&mut [IoSliceMut]`, note that the slice requires a mutable reference now. This is how it's done in the standard library, and there might be a soundness issue in doing it the other way.

Resolves #1637 

Co-authored-by: not_a_seagull <notaseagull048@gmail.com>
2022-04-08 20:05:41 +00:00
not_a_seagull
0b58f29772 Replace the IoVec type with IoSlice and IoSliceMut 2022-04-08 12:39:16 -07:00
Junho Choi
a4b5dfc059 Add IP_DONTFRAG and IPV6_DONTFRAG SockOpts
IP_DONTFRAG: iOS, macOS
IPV6_DONTFRAG: android, iOS, linux and macOS

Test: `cargo test --test test dontfrag_opts`

Some CI tests running ENOPROTOOPT are disabled (qemu-based).
2022-04-07 17:39:46 -07:00
Jan Bujak
4ae4cfd058 Make uname always safe
This fixes several issues with the current `uname` bindings:

  - Do not ignore `uname` errors; at least on glibc `uname` can fail,
    so now it returns a `Result` instead of assuming that the call
    will always succeed.

  - Do not assume `uname` will initialize every member of `utsname`;
    not every implementation initializes every field, so internally
    the struct is now zero-initialized.

  - Do not blindly assume strings returned by `uname` will always be valid UTF-8;
    `UtsName`'s accessors will now return `&OsStr`s instead of `&str`s.
2022-03-23 14:28:37 +09:00
Alan Somers
fbeabf3730 Deprecate IpAddr, Ipv4Addr, and Ipv6Addr
Because they're redundant with types in the standard library.

Fixes #1681
2022-03-22 21:09:52 -06:00
Alan Somers
76d70b4b25 Replace the Sockaddr enum with a union
The SockAddr enum is quite large, and the user must allocate space for
the whole thing even though he usually knows what type he needs.
Furthermore, thanks to the sa_family field, the sockaddr types are
basically an enum even in C.

So replace the ungainly enum with a SockaddrLike trait implemented by
all sockaddr types and  a SockaddrStorage union that has safe accessors.

Also, deprecate InetAddr, which only existed to support SockAddr.

Supplants #1504
Fixes #1544
2022-03-21 20:50:24 -06:00