From f7c68d51890dd2f3ab61a94529602c4d1addccd3 Mon Sep 17 00:00:00 2001 From: Egor Lazarchuk Date: Mon, 9 Dec 2024 11:09:43 +0000 Subject: [PATCH] This is a combination of 2 commits. 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 --- kvm-ioctls/CHANGELOG.md | 6 ++++++ kvm-ioctls/src/ioctls/vm.rs | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/kvm-ioctls/CHANGELOG.md b/kvm-ioctls/CHANGELOG.md index 526bef11..a8affafd 100644 --- a/kvm-ioctls/CHANGELOG.md +++ b/kvm-ioctls/CHANGELOG.md @@ -7,6 +7,12 @@ - [[#288](https://github.com/rust-vmm/kvm-ioctls/pull/288)]: Introduce `Cap::GuestMemfd`, `Cap::MemoryAttributes` and `Cap::UserMemory2` capabilities enum variants for use with `VmFd::check_extension`. +### 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 diff --git a/kvm-ioctls/src/ioctls/vm.rs b/kvm-ioctls/src/ioctls/vm.rs index edc63e41..d293c6ad 100644 --- a/kvm-ioctls/src/ioctls/vm.rs +++ b/kvm-ioctls/src/ioctls/vm.rs @@ -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. /// @@ -1300,7 +1300,7 @@ impl VmFd { /// ``` pub fn create_device(&self, device: &mut kvm_create_device) -> Result { // 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.