nix
nix copied to clipboard
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
I fixed the formatting. And rather than figure out how to determine whether a Fuchsia process can create raw sockets, I'm just skipping that test on Fuchsia.
@ahcodedthat does this patch solve your original problem?
Yes, that works. The example program now just fails cleanly with EINVAL instead of segfaulting. 👍
I suggest mentioning in the documentation for nix::sys::socket::sockopt::SockType that it will fail with EINVAL if the socket type is unknown.
It might also be wise to use a different error code, since the getsockopt system call can fail with EINVAL for other reasons too. ESOCKTNOSUPPORT seems best, but it doesn't exist on cfg(target_os = "haiku"), so consider using EPROTONOSUPPORT instead.
@rtzoeller I stupidly forgot about this PR when I made the 0.26.0 release a few minutes ago.
bors r+
Build succeeded:
- Android aarch64
- Android arm
- Android armv7
- Android i686
- Android x86_64
- DragonFly BSD x86_64
- FreeBSD 12 amd64 & i686
- FreeBSD 14 amd64 & i686
- Fuchsia x86_64
- Haiku x86_64
- Illumos
- iOS aarch64
- iOS x86_64
- Linux aarch64
- Linux arm gnueabi
- Linux arm-musleabi
- Linux armv7 gnueabihf
- Linux armv7 uclibceabihf
- Linux i686
- Linux i686 musl
- Linux MIPS
- Linux MIPS64
- Linux MIPS64 el
- Linux mipsel
- Linux powerpc
- Linux powerpc64
- Linux powerpc64le
- Linux s390x
- Linux x32
- Linux x86_64
- Linux x86_64 musl
- macOS aarch64
- macOS x86_64
- Minver
- NetBSD x86_64
- OpenBSD x86_64
- Redox x86_64
- Rust Formatter
- Rust Stable