Skip to content

Commit

Permalink
fix: use correct ioctl wrapper for create_device
Browse files Browse the repository at this point in the history
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 <yegorlz@amazon.co.uk>
  • Loading branch information
ShadowCurse committed Dec 10, 2024
1 parent 701517a commit 9210e9d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Upcoming Release

### Fixed

- [[#298](https://github.com/rust-vmm/kvm/pull/298)]: Fixed incorrect usage of `ioctl_wit_ref` in the
`create_device` method. Replace it with `ioctl_wit_mut_ref` as the passed parameter may be mutated by the
ioctl.

## v0.19.0

### Added
Expand Down
8 changes: 4 additions & 4 deletions src/ioctls/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use crate::ioctls::{KvmRunWrapper, Result};
use crate::kvm_ioctls::*;
use vmm_sys_util::errno;
use vmm_sys_util::eventfd::EventFd;
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
use vmm_sys_util::ioctl::ioctl;
#[cfg(target_arch = "x86_64")]
use vmm_sys_util::ioctl::ioctl_with_mut_ptr;
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
use vmm_sys_util::ioctl::{ioctl, ioctl_with_mut_ref};
use vmm_sys_util::ioctl::{ioctl_with_ref, ioctl_with_val};
use vmm_sys_util::ioctl::{ioctl_with_mut_ref, ioctl_with_ref, ioctl_with_val};

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

0 comments on commit 9210e9d

Please sign in to comment.