-
Notifications
You must be signed in to change notification settings - Fork 129
fix: use correct ioctl wrapper for create_device
#298
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
Conversation
08009fb to
b42e972
Compare
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]>
b42e972 to
f7c68d5
Compare
| 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) }; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
roypat
left a comment
There was a problem hiding this 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.
Summary of the PR
Use
ioctl_with_mut_refinstead ofioctl_with_refforcreate_deviceas it needs to write to thekvm_create_devicestruct passed to it.This incorrect usage of the
ioctl_with_refcauses an issue if used with Rust version 1.82(and later) inreleasebuilds. In those cases thekvm_create_devicestruct is considered to be immutable and the followingFile::from_raw_fdcreates a file from an initial value set in thekvm_create_devicebefore 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
--releaseoption which does not produce the error.Requirements
Before submitting your PR, please make sure you addressed the following
requirements:
git commit -s), and the commit message has max 60 characters for thesummary and max 75 characters for each description line.
test.
Release" section of CHANGELOG.md (if no such section exists, please create one).
unsafecode is properly documented.