Skip to content

Commit 9eb1102

Browse files
authored
Fix I/O safety in io_uring_setup and configure_virtual_memory_map (#1351)
* Fix I/O safety in `io_uring_setup` and `configure_virtual_memory_map` Use `RawFd` for fd parameters in `PrctlMmMap` and `io_uring_params`, and make functions that take them as arguments `unsafe` to reflect that they depend on any file descriptors being valid. Fixes #1001. * Temporarily disable riscv32imc-esp-espidf in CI.
1 parent 2429fb6 commit 9eb1102

5 files changed

Lines changed: 18 additions & 7 deletions

File tree

.github/workflows/main.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ jobs:
216216
- run: cargo check -Z build-std --target aarch64-unknown-linux-gnu_ilp32 --all-targets --features=all-apis
217217
- run: cargo check -Z build-std --target x86_64-unknown-haiku --all-targets --features=all-apis
218218
- run: cargo check -Z build-std --target x86_64-uwp-windows-msvc --all-targets --features=all-apis
219-
- run: cargo check -Z build-std --target=riscv32imc-esp-espidf --features=all-apis
219+
# Temporarily disable riscv32imc-esp-espidf, as it gets build errors.
220+
# - run: cargo check -Z build-std --target=riscv32imc-esp-espidf --features=all-apis
220221
- run: cargo check -Z build-std --target=aarch64-unknown-nto-qnx710 --features=all-apis
221222
- run: cargo check -Z build-std --target=x86_64-pc-nto-qnx710 --features=all-apis
222223
# Temporarily disable --features=all-apis, which doesn't build yet.

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,5 +254,10 @@ renamed to [`SocketAddrXdpFlags`].
254254
[`SocketAddrXdpWithSharedUmem`]: https://docs.rs/rustix/1.0.0/rustix/net/xdp/struct.SocketAddrXdpWithSharedUmem.html
255255
[`SocketAddrXdpFlags`]: https://docs.rs/rustix/1.0.0/rustix/net/xdp/struct.SocketAddrXdpFlags.html
256256

257+
[`rustix::io_uring::io_uring_setup`] is now unsafe, due its `io_uring_params`
258+
argument optionallly containing a raw file descriptor.
259+
260+
[`rustix::io_uring::io_uring_setup`]: https://docs.rs/rustix/1.0.0/rustix/io_uring/fn.io_uring_setup.html
261+
257262
All explicitly deprecated functions and types have been removed. Their
258263
deprecation messages will have identified alternatives.

src/io_uring/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,17 @@ pub struct MsgHdr {
8080
/// `io_uring_setup(entries, params)`—Setup a context for performing
8181
/// asynchronous I/O.
8282
///
83+
/// # Safety
84+
///
85+
/// If [`IoringSetupFlags::ATTACH_WQ`] is set, the `wq_fd` field of
86+
/// `io_uring_params` must be an open file descriptor.
87+
///
8388
/// # References
8489
/// - [Linux]
8590
///
8691
/// [Linux]: https://www.man7.org/linux/man-pages/man2/io_uring_setup.2.html
8792
#[inline]
88-
pub fn io_uring_setup(entries: u32, params: &mut io_uring_params) -> io::Result<OwnedFd> {
93+
pub unsafe fn io_uring_setup(entries: u32, params: &mut io_uring_params) -> io::Result<OwnedFd> {
8994
backend::io_uring::syscalls::io_uring_setup(entries, params)
9095
}
9196

@@ -1469,7 +1474,7 @@ pub struct io_uring_params {
14691474
pub sq_thread_cpu: u32,
14701475
pub sq_thread_idle: u32,
14711476
pub features: IoringFeatureFlags,
1472-
pub wq_fd: u32,
1477+
pub wq_fd: RawFd,
14731478
pub resv: [u32; 3],
14741479
pub sq_off: io_sqring_offsets,
14751480
pub cq_off: io_cqring_offsets,

src/process/prctl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use core::ptr::{null, null_mut, NonNull};
1212
use bitflags::bitflags;
1313

1414
use crate::backend::prctl::syscalls;
15-
use crate::fd::{AsRawFd as _, BorrowedFd};
15+
use crate::fd::{AsRawFd as _, BorrowedFd, RawFd};
1616
use crate::ffi::{c_int, c_uint, c_void, CStr};
1717
use crate::io;
1818
use crate::prctl::*;
@@ -765,7 +765,7 @@ pub struct PrctlMmMap {
765765
pub auxv_size: u32,
766766
/// File descriptor of executable file that was used to create this
767767
/// process.
768-
pub exe_fd: u32,
768+
pub exe_fd: RawFd,
769769
}
770770

771771
/// Provides one-shot access to all the addresses by passing in a

tests/io_uring/register.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ where
108108
#[test]
109109
fn test_io_uring_register_with() {
110110
let mut params = io_uring_params::default();
111-
let ring_fd = io_uring_setup(4, &mut params).unwrap();
111+
let ring_fd = unsafe { io_uring_setup(4, &mut params).unwrap() };
112112
assert_eq!(params.sq_entries, 4);
113113
assert_eq!(params.cq_entries, 8);
114114

@@ -130,7 +130,7 @@ fn io_uring_buf_ring_can_be_registered() {
130130
const BGID: u16 = 42;
131131

132132
let mut params = io_uring_params::default();
133-
let ring_fd = io_uring_setup(4, &mut params).unwrap();
133+
let ring_fd = unsafe { io_uring_setup(4, &mut params).unwrap() };
134134

135135
// Test that the kernel version supports IORING_REGISTER_PBUF_RING. If it
136136
// doesn't, the kernel will return EINVAL. Not setting a `ring_addr` on

0 commit comments

Comments
 (0)