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

add marker trait to help check safety of guest memory reads #794

Merged
merged 3 commits into from
Oct 18, 2024

Conversation

iximeow
Copy link
Member

@iximeow iximeow commented Oct 17, 2024

we noted that a pointer into guest memory must point to a properly-initialized T when read into Propolis, but there was no way to actually check that was a case. for example, it may be tempting to write an enum describing states of a guest device like:

enum MyCoolDevicePower {
  Off = 0,
  On = 1,
}

and read/write to guest memory using the convenient read/write helpers. but a devious guest could put a 2 at that address, where reading that into Propolis would be UB.

zerocopy::FromBytes happens to have the same requirements about its implementors as we need, that they're always valid to view from bytes, so use it to check that we can safely read a type out of guest memory. in our case we'll always copy those bytes to our own buffer, but zerocopy::FromBytes also comes with a great proc macro so we can #[derive(FromBytes)] on structs to be copied out.

we noted that a pointer into guest memory must point to a
properly-initialized T when read into Propolis, but there was no way to
actually check that was a case. for example, it may be tempting to write
an enum describing states of a guest device like:
```
enum MyCoolDevicePower {
  Off = 0,
  On = 1,
}
```
and read/write to guest memory using the convenient read/write helpers.
but a devious guest could put a `2` at that address, where reading that
into Propolis would be UB.

so, add a new `unsafe trait AlwaysInhabited` that `MemCtx::read` and friends can
rely on as attestation that they are safe to read guest memory as a given type.
impl this new trait for the handful of types we read from guest memory, as well.
Copy link
Collaborator

@pfmooney pfmooney left a comment

Choose a reason for hiding this comment

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

Nice added safety

@iximeow iximeow merged commit ff0b76d into master Oct 18, 2024
11 checks passed
@iximeow iximeow deleted the ixi/vmm-read-invariants branch October 18, 2024 02:01
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.

2 participants