Skip to content

Conversation

@alyssais
Copy link
Contributor

Summary of the PR

KVM_GET_DEVICE_ATTR causes the kernel to write to an arbitrary address. This is unsafe, as it can allow writing to an address Rust believes to be immutable.

I discovered this because an optimisation change in Rust 1.80.0 caused a Cloud Hypervisor test to start failing when built in release mode, because it was setting the addr passed to get_device_attr() to the address of an immutable variable.

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.

roypat
roypat previously approved these changes Aug 12, 2024
Copy link
Member

@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.

Thanks!

@roypat roypat dismissed their stale review August 12, 2024 15:14

argh, got ahead of myself, seems like CI found a missing unsafe block somewhere :(

alyssais added a commit to alyssais/cloud-hypervisor that referenced this pull request Aug 12, 2024
DeviceFd::get_device_attr should be marked as unsafe, because it
allows writing to an arbitrary address.  I have opened a kvm-ioctls
PR[1] to fix this.  The hypervisor crate was using the function
unsafely by passing it addresses of immutable variables.  I noticed
this because an optimisation change[2] in Rust 1.80.0 caused the
kvm::aarch64::gic::tests::test_get_set_icc_regs test to start failing
when built in release mode.

To fix this, I've broken up the _access functions into _set and _get
variants, with the _get variant using a pointer to a mutable variable.
This has the side effect of making these functions a bit nicer to use,
because the caller now has no need to use references at all, for
either getting or setting.

[1]: rust-vmm/kvm#273
[2]: rust-lang/rust@d2d24e3

Signed-off-by: Alyssa Ross <[email protected]>
alyssais added a commit to alyssais/cloud-hypervisor that referenced this pull request Aug 12, 2024
DeviceFd::get_device_attr should be marked as unsafe, because it
allows writing to an arbitrary address.  I have opened a kvm-ioctls
PR[1] to fix this.  The hypervisor crate was using the function
unsafely by passing it addresses of immutable variables.  I noticed
this because an optimisation change[2] in Rust 1.80.0 caused the
kvm::aarch64::gic::tests::test_get_set_icc_regs test to start failing
when built in release mode.

To fix this, I've broken up the _access functions into _set and _get
variants, with the _get variant using a pointer to a mutable variable.
This has the side effect of making these functions a bit nicer to use,
because the caller now has no need to use references at all, for
either getting or setting.

[1]: rust-vmm/kvm#273
[2]: rust-lang/rust@d2d24e3

Signed-off-by: Alyssa Ross <[email protected]>
KVM_GET_DEVICE_ATTR causes the kernel to write to an arbitrary
address.  This is unsafe, as it can allow writing to an address Rust
believes to be immutable.

I discovered this because an optimisation change[1] in Rust 1.80.0
caused a Cloud Hypervisor test to start failing when built in release
mode, because it was setting the addr passed to get_device_attr() to
the address of an immutable variable.

[1]: rust-lang/rust@d2d24e3

Fixes: 8ea124b ("Add support for `KVM_GET_DEVICE_ATTR` ioctl")
Signed-off-by: Alyssa Ross <[email protected]>
@ShadowCurse ShadowCurse merged commit 47665cb into rust-vmm:main Aug 12, 2024
@alyssais alyssais deleted the get_device_attr branch August 12, 2024 15:43
alyssais added a commit to alyssais/cloud-hypervisor that referenced this pull request Aug 12, 2024
DeviceFd::get_device_attr should be marked as unsafe, because it
allows writing to an arbitrary address.  I have opened a kvm-ioctls
PR[1] to fix this.  The hypervisor crate was using the function
unsafely by passing it addresses of immutable variables.  I noticed
this because an optimisation change[2] in Rust 1.80.0 caused the
kvm::aarch64::gic::tests::test_get_set_icc_regs test to start failing
when built in release mode.

To fix this, I've broken up the _access functions into _set and _get
variants, with the _get variant using a pointer to a mutable variable.
This has the side effect of making these functions a bit nicer to use,
because the caller now has no need to use references at all, for
either getting or setting.

[1]: rust-vmm/kvm#273
[2]: rust-lang/rust@d2d24e3

Signed-off-by: Alyssa Ross <[email protected]>
alyssais added a commit to alyssais/cloud-hypervisor that referenced this pull request Aug 12, 2024
DeviceFd::get_device_attr should be marked as unsafe, because it
allows writing to an arbitrary address.  I have opened a kvm-ioctls
PR[1] to fix this.  The hypervisor crate was using the function
unsafely by passing it addresses of immutable variables.  I noticed
this because an optimisation change[2] in Rust 1.80.0 caused the
kvm::aarch64::gic::tests::test_get_set_icc_regs test to start failing
when built in release mode.

To fix this, I've broken up the _access functions into _set and _get
variants, with the _get variant using a pointer to a mutable variable.
This has the side effect of making these functions a bit nicer to use,
because the caller now has no need to use references at all, for
either getting or setting.

[1]: rust-vmm/kvm#273
[2]: rust-lang/rust@d2d24e3

Signed-off-by: Alyssa Ross <[email protected]>
github-merge-queue bot pushed a commit to cloud-hypervisor/cloud-hypervisor that referenced this pull request Aug 13, 2024
DeviceFd::get_device_attr should be marked as unsafe, because it
allows writing to an arbitrary address.  I have opened a kvm-ioctls
PR[1] to fix this.  The hypervisor crate was using the function
unsafely by passing it addresses of immutable variables.  I noticed
this because an optimisation change[2] in Rust 1.80.0 caused the
kvm::aarch64::gic::tests::test_get_set_icc_regs test to start failing
when built in release mode.

To fix this, I've broken up the _access functions into _set and _get
variants, with the _get variant using a pointer to a mutable variable.
This has the side effect of making these functions a bit nicer to use,
because the caller now has no need to use references at all, for
either getting or setting.

[1]: rust-vmm/kvm#273
[2]: rust-lang/rust@d2d24e3

Signed-off-by: Alyssa Ross <[email protected]>
github-merge-queue bot pushed a commit to cloud-hypervisor/cloud-hypervisor that referenced this pull request Aug 13, 2024
DeviceFd::get_device_attr should be marked as unsafe, because it
allows writing to an arbitrary address.  I have opened a kvm-ioctls
PR[1] to fix this.  The hypervisor crate was using the function
unsafely by passing it addresses of immutable variables.  I noticed
this because an optimisation change[2] in Rust 1.80.0 caused the
kvm::aarch64::gic::tests::test_get_set_icc_regs test to start failing
when built in release mode.

To fix this, I've broken up the _access functions into _set and _get
variants, with the _get variant using a pointer to a mutable variable.
This has the side effect of making these functions a bit nicer to use,
because the caller now has no need to use references at all, for
either getting or setting.

[1]: rust-vmm/kvm#273
[2]: rust-lang/rust@d2d24e3

Signed-off-by: Alyssa Ross <[email protected]>
github-merge-queue bot pushed a commit to cloud-hypervisor/cloud-hypervisor that referenced this pull request Aug 13, 2024
DeviceFd::get_device_attr should be marked as unsafe, because it
allows writing to an arbitrary address.  I have opened a kvm-ioctls
PR[1] to fix this.  The hypervisor crate was using the function
unsafely by passing it addresses of immutable variables.  I noticed
this because an optimisation change[2] in Rust 1.80.0 caused the
kvm::aarch64::gic::tests::test_get_set_icc_regs test to start failing
when built in release mode.

To fix this, I've broken up the _access functions into _set and _get
variants, with the _get variant using a pointer to a mutable variable.
This has the side effect of making these functions a bit nicer to use,
because the caller now has no need to use references at all, for
either getting or setting.

[1]: rust-vmm/kvm#273
[2]: rust-lang/rust@d2d24e3

Signed-off-by: Alyssa Ross <[email protected]>
blitz pushed a commit to blitz/cloud-hypervisor that referenced this pull request Feb 10, 2025
DeviceFd::get_device_attr should be marked as unsafe, because it
allows writing to an arbitrary address.  I have opened a kvm-ioctls
PR[1] to fix this.  The hypervisor crate was using the function
unsafely by passing it addresses of immutable variables.  I noticed
this because an optimisation change[2] in Rust 1.80.0 caused the
kvm::aarch64::gic::tests::test_get_set_icc_regs test to start failing
when built in release mode.

To fix this, I've broken up the _access functions into _set and _get
variants, with the _get variant using a pointer to a mutable variable.
This has the side effect of making these functions a bit nicer to use,
because the caller now has no need to use references at all, for
either getting or setting.

[1]: rust-vmm/kvm#273
[2]: rust-lang/rust@d2d24e3

Signed-off-by: Alyssa Ross <[email protected]>
(cherry picked from commit 02f146f)
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.

3 participants