Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use correct ioctl wrapper for create_device #298

Merged
merged 1 commit into from
Dec 9, 2024

Conversation

ShadowCurse
Copy link
Collaborator

@ShadowCurse ShadowCurse commented Dec 9, 2024

Summary of the PR

Use ioctl_with_mut_ref instead of ioctl_with_ref for create_device as it needs to write to the
kvm_create_device struct passed to it.

This incorrect usage of the ioctl_with_ref causes an issue if used with Rust version 1.82(and later) in release builds. In those cases the kvm_create_device struct is considered to be immutable and the following File::from_raw_fd creates a file from an initial value set in the kvm_create_device before the call.

The reason unit tests don't fail in CI is because it uses 1.80.1 version and does not run unit tests with --release option which does not produce the error.

Requirements

Before submitting your PR, please make sure you addressed the following
requirements:

  • All commits in this PR have Signed-Off-By trailers (with
    git commit -s), and the commit message has max 60 characters for the
    summary and max 75 characters for each description line.
  • All added/changed functionality has a corresponding unit/integration
    test.
  • All added/changed public-facing functionality has entries in the "Upcoming
    Release" section of CHANGELOG.md (if no such section exists, please create one).
  • Any newly added unsafe code is properly documented.

@ShadowCurse ShadowCurse force-pushed the fix_aarch64_create_device branch from 08009fb to b42e972 Compare December 9, 2024 13:09
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]>
@ShadowCurse ShadowCurse force-pushed the fix_aarch64_create_device branch from b42e972 to f7c68d5 Compare December 9, 2024 13:15
@ShadowCurse ShadowCurse marked this pull request as ready for review December 9, 2024 13:19
@@ -1300,7 +1300,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) };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test that would trigger the error? As we discussed offline this would not be triggering in the CI because we're not using the latest Rust version. It's enough if we can trigger it locally because eventually we'll be using the latest version in the CI as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have tests which can trigger this behavior. Tests are: test_register_unregister_irqfd and test_set_irq_line.

Copy link
Collaborator

@roypat roypat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth it to check all other uses of ioctl_with_ref for their correctness, but let's not block this fix on that.

@ShadowCurse ShadowCurse merged commit ade910b into rust-vmm:main Dec 9, 2024
34 checks passed
@ShadowCurse ShadowCurse deleted the fix_aarch64_create_device branch December 9, 2024 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants