Skip to content

Commit 9210e9d

Browse files
committed
fix: use correct ioctl wrapper for create_device
Use `ioctl_with_mut_ref` instead of `ioctl_with_ref` in the `create_device` method as it needs to write to the `kvm_create_device` struct passed to it. This incorrect usage of `ioctl_with_ref` causes newer versions of Rust compiler (1.82 and above) to treat the `kvm_create_device` struct as read-only and bypass following reads from it. This optimization lead to incorrect value being passed to the `File::from_raw_fd` call. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 701517a commit 9210e9d

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## Upcoming Release
44

5+
### Fixed
6+
7+
- [[#298](https://github.com/rust-vmm/kvm/pull/298)]: Fixed incorrect usage of `ioctl_wit_ref` in the
8+
`create_device` method. Replace it with `ioctl_wit_mut_ref` as the passed parameter may be mutated by the
9+
ioctl.
10+
511
## v0.19.0
612

713
### Added

src/ioctls/vm.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ use crate::ioctls::{KvmRunWrapper, Result};
2222
use crate::kvm_ioctls::*;
2323
use vmm_sys_util::errno;
2424
use vmm_sys_util::eventfd::EventFd;
25+
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
26+
use vmm_sys_util::ioctl::ioctl;
2527
#[cfg(target_arch = "x86_64")]
2628
use vmm_sys_util::ioctl::ioctl_with_mut_ptr;
27-
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
28-
use vmm_sys_util::ioctl::{ioctl, ioctl_with_mut_ref};
29-
use vmm_sys_util::ioctl::{ioctl_with_ref, ioctl_with_val};
29+
use vmm_sys_util::ioctl::{ioctl_with_mut_ref, ioctl_with_ref, ioctl_with_val};
3030

3131
/// An address either in programmable I/O space or in memory mapped I/O space.
3232
///
@@ -1306,7 +1306,7 @@ impl VmFd {
13061306
/// ```
13071307
pub fn create_device(&self, device: &mut kvm_create_device) -> Result<DeviceFd> {
13081308
// SAFETY: Safe because we are calling this with the VM fd and we trust the kernel.
1309-
let ret = unsafe { ioctl_with_ref(self, KVM_CREATE_DEVICE(), device) };
1309+
let ret = unsafe { ioctl_with_mut_ref(self, KVM_CREATE_DEVICE(), device) };
13101310
if ret == 0 {
13111311
// SAFETY: We validated the return of the function creating the fd and we trust the
13121312
// kernel.

0 commit comments

Comments
 (0)