Commit graph

371 commits

Author SHA1 Message Date
Erik Schilling
e117afb739 vhost-user-backend: simplify the use of generics
In order to allow zero-cost exchanging of the concrete bitmap and vring
types, a lot of the generic code required using a tuple of
`<VringType, BitmapType>` for parameterizing the impl's. Once code is
also oblivious of the concrete backend (and a lot of code is), this
tuple turns into a triplet. Juggling these three single letter generic
parameters while making sure that all the type constraints (that are
also changing depending on the abstraction layer) does not feel very
ergonomic.

While one can argue that within this crate, this would be fine since
people probably know the internals, the design choice is leaking out
into every consumer of vhost-user-backend. Instead of just being able
to reference "some backend" one needs to copy a lot of boilerplate code
for also passing the other type parameters (again, while making sure
that all the constraints are met).

Instead, this commit changes things to utilize associated types [1].
This makes the Bitmap and Vring types part of the backend trait and
requires the implementations to spell them out. Code that just wants to
use the backend without needing to know the details can now just use
the trait without needing to specify the Bitmap and Vring types again.
Where needed, restricting Bitmap and Vring further is still possible
(though one no longer needs to copy all the existing restrictions and
can keep the code more maintainable by only listing new ones).

Overall, my main target was to improve the ergonomics of the consumers
of the crate. But I think the change also improves the readability and
maintainability within this crate. Combined, this hopefully justifies
the small code breakage in consumers.

No functional changes intended.
No change in type flexibility intended.

BREAKING CHANGE, consumers of the lib will need to adjust their code
(but it should improve the general readability).

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>

[1] https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#specifying-placeholder-types-in-trait-definitions-with-associated-types
2023-10-02 09:52:53 +02:00
Stefano Garzarella
1ab08b7074 vhost-user-backend: remove return value from handle_event
The return value of VhostUserBackend::handle_event() is
undocumented and difficult to interpret.
The current implementation used it to interrupt the event
loop as it does when we receive an exit event.

All current implementations checked (rust-vmm/vhost-device,
virtiofsd) return an error or always false, effectively not
using this feature.

Since we already have a mechanism for breaking the event loop,
we can avoid this ambiguous and redundant feature.

Closes #144

Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
2023-09-28 16:47:08 +02:00
dependabot[bot]
5278cb549f build(deps): update nix requirement from 0.26 to 0.27
Updates the requirements on [nix](https://github.com/nix-rust/nix) to permit the latest version.
- [Changelog](https://github.com/nix-rust/nix/blob/master/CHANGELOG.md)
- [Commits](https://github.com/nix-rust/nix/compare/v0.26.0...v0.27.1)

---
updated-dependencies:
- dependency-name: nix
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

nix 0.27 required these changes:
- memfd_create() is now only available with crate feature `fs`.
- the return value of memfd_create() is now an OwnedFd and not RawFd
  anymore.

We are using nix only for testing, so no functional changes at all.

Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
2023-09-26 09:23:16 +02:00
Erik Schilling
3808f9d003 vhost-user-backend: add convenience function serve
We figured that all vhost-device daemons just ended up copying code from
each other. During an earlier attempt to solve that with a helper crate
for vhost-device [1], it was suggested to extend the vhost-user-backend
API directly. This way more people can benefit from this.

The function just groups together some functions that (at least in
vhost-device) are always called together. Some usually "expected" error
results are filtered out. The remaining additions are extending the mock
backend so that it allows sensible testing of exit events and the test
for the functionality itself.

[1] https://github.com/rust-vmm/vhost-device/pull/362

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
2023-09-14 11:14:40 +02:00
Erik Schilling
f699ca9b0f vhost-user-backend: simplify tests
Scoping the threads allows us to just borrow the values from the thread and
removes the need for the clones with the slightly awkward names.

Also, we no longer need to remember to join threads (happens
automatically upon end of scope).

No functional changes intended.

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
2023-09-14 11:14:40 +02:00
Erik Schilling
f50b135212 vhost-user-backend: simplify path concatination
Shortens code a bit and allows to keep the field non-mutable.

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
2023-09-14 11:14:40 +02:00
Erik Schilling
bc63db7b57 vhost-user-backend: fix docs for exit_event
The function used to return a tuple, but no longer does that.

Fixes: e8beb23 ("epoll: refine the way to manage event id")
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
2023-09-14 11:14:40 +02:00
Stefano Garzarella
f29830ab87 coverage: update the score
After updating the CI the coverage value changed causing failures:

    Current code coverage (76.33%) deviates by 7.67% from the
    previous code coverage 84.00%.

Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
2023-09-14 16:40:26 +08:00
dependabot[bot]
4848a657bf build(deps): bump rust-vmm-ci from 9dfe5b2 to 665f31f
Bumps [rust-vmm-ci](https://github.com/rust-vmm/rust-vmm-ci) from `9dfe5b2` to `665f31f`.
- [Commits](9dfe5b267c...665f31f4b4)

---
updated-dependencies:
- dependency-name: rust-vmm-ci
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-09-14 16:40:26 +08:00
Stefano Garzarella
5790bed767 vhost/tests: avoid useless vec! usage
Clippy reported the following suggestions:

error: useless use of `vec!`
   --> crates/vhost/src/vhost_user/connection.rs:676:20
    |
676 |         let buf1 = vec![0x1, 0x2, 0x3, 0x4];
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `[0x1, 0x2, 0x3, 0x4]`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec
    = note: `-D clippy::useless-vec` implied by `-D warnings`
error: useless use of `vec!`
   --> crates/vhost/src/vhost_user/connection.rs:706:20
    |
706 |         let buf1 = vec![0x1, 0x2, 0x3, 0x4];
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `[0x1, 0x2, 0x3, 0x4]`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec

Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
2023-09-14 16:40:26 +08:00
Stefano Garzarella
b8bf8b6603 vhost: replace if-then-else with a bool assignment
Clippy reported the following suggestions:

error: this if-then-else expression assigns a bool literal
   --> crates/vhost/src/vhost_user/backend_req_handler.rs:739:9
    |
739 | /         if (self.virtio_features & vflag) != 0
740 | |             && self.protocol_features.contains(pflag)
741 | |             && (self.acked_protocol_features & pflag.bits()) != 0
742 | |         {
...   |
745 | |             self.reply_ack_enabled = false;
746 | |         }
    | |_________^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_bool_assign
    = note: `-D clippy::needless-bool-assign` implied by `-D warnings`
help: you can reduce it to
    |
739 ~         self.reply_ack_enabled = (self.virtio_features & vflag) != 0
740 +             && self.protocol_features.contains(pflag) && (self.acked_protocol_features & pflag.bits()) != 0;
    |

Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
2023-09-14 16:40:26 +08:00
Stefano Garzarella
33717b3717 vhost: fix __IncompleteArrayField clone implementation
Clippy reported the following suggestions:

error: incorrect implementation of `clone` on a `Copy` type
   --> crates/vhost/src/vhost_kern/vhost_binding.rs:134:29
    |
134 |       fn clone(&self) -> Self {
    |  _____________________________^
135 | |         Self::new()
136 | |     }
    | |_____^ help: change this to: `{ *self }`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#incorrect_clone_impl_on_copy_type
    = note: `#[deny(clippy::incorrect_clone_impl_on_copy_type)]` on by default

Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
2023-09-14 16:40:26 +08:00
dependabot[bot]
5db32c64ae build(deps): update bitflags requirement from 1.0 to 2.4
Updates the requirements on [bitflags](https://github.com/bitflags/bitflags) to permit the latest version.
- [Release notes](https://github.com/bitflags/bitflags/releases)
- [Changelog](https://github.com/bitflags/bitflags/blob/main/CHANGELOG.md)
- [Commits](https://github.com/bitflags/bitflags/compare/1.0.0...2.4.0)

---
updated-dependencies:
- dependency-name: bitflags
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

The new bitflag 2.4 required these changes:
- used `from_bits_retain()` instead of deprecated
  `from_bits_unchecked()`
- derived `Copy, Clone, Debug, Eq, PartialEq` traits
- used the new `bits()`

Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
2023-09-13 16:29:02 +02:00
dependabot[bot]
66a7c3aeb9 build(deps): update serial_test requirement from 0.5 to 2.0
Updates the requirements on [serial_test](https://github.com/palfrey/serial_test) to permit the latest version.
- [Release notes](https://github.com/palfrey/serial_test/releases)
- [Commits](https://github.com/palfrey/serial_test/compare/v0.5.0...v2.0.0)

---
updated-dependencies:
- dependency-name: serial_test
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-09-13 16:16:14 +02:00
Albert Esteve
282a10f8e1 vhost-user-backend: Adopt new backend naming
Following vhost-user specification changes,
replace all uses of slave/master with backend/frontend
for the vhost-user-backend crate.

Signed-off-by: Albert Esteve <aesteve@redhat.com>
2023-09-13 13:00:10 +02:00
Albert Esteve
4db81adcd2 vhost: Adopt new backend naming
Following vhost-user specification, replace
all uses of master/slave with backend/frontend
in the vhost crate.

Signed-off-by: Albert Esteve <aesteve@redhat.com>
2023-09-13 13:00:10 +02:00
Li Zebin
5c9bef626c vhost: Fix clippy warnings.
use into() to convert bool into integer
rather than 'if enable { 1 } else { 0 }'

Signed-off-by: Li Zebin <cutelizebin@gmail.com>
2023-09-11 11:00:13 +02:00
Manish Goregaokar
10bf1e9123 Make VhostUserMsgValidator use ByteValued
`extract_request_body()` assumes these types are POD as a safety
invariant, which is only possible if `VhostUserMsgValidator` is itself
an `unsafe trait` or depends on an `unsafe trait` with the correct
invariants.

`VhostUserMemoryRegion` is the only type that implemented
`VhostUserMsgValidator` and did not yet implement `ByteValued`

Signed-off-by: Manish Goregaokar <manishsmail@gmail.com>
2023-08-28 18:24:57 +02:00
Stefano Garzarella
6ca88e160a vhost-user-backend: release v0.10.1
Commit 8a4ba9d ("vhost-user-backend: fetch 'used' index from guest")
fixes an issue introduced in v0.10.0 that affects all vhost-user
backends like virtiofsd [1] or rust-vmm's vhost-device.
I easily reproduced the problem with vhost-device-vsock as well:
just restart the guest kernel and the device no longer works.

[1] https://gitlab.com/virtio-fs/virtiofsd/-/issues/120

Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
2023-08-22 16:02:40 +08:00
German Maglione
8a4ba9d0c5 vhost-user-backend: fetch 'used' index from guest
commit 958cdec2b8 stopped GET_VRING_BASE
from resetting the vring. This was required because when the VM is
stopped/resumed the index will be 0 instead of the correct value.

However, after the guest driver changes, for instance, after reboot,
the 'used' index should be reset to 0. The bug fixed in that commit had
the unintended side effect of setting the 'used' index to 0 after a
driver change.

QEMU's vhost-user library sets the 'used' index when receiving
SET_VRING_ADDR, _probably_ to make sure that the VQ is configured.
Perhaps the appropriate place is when receiving the first kick.
A better solution would be to send the 'used' index in SET_VRING_BASE,
as is done when using packed VQs.

To keep compatibility with QEMU and just in case, any implementation
expects the 'used' index to be set when receiving SET_VRING_ADDR let's
fetch the 'used' index from the guest when receiving that message.

Signed-off-by: Hanna Czenczek <hreitz@redhat.com>
Signed-off-by: German Maglione <gmaglione@redhat.com>
2023-08-09 13:55:22 +02:00
Stefano Garzarella
a1822baa6c vhost: update release v0.8.1 in Cargo.toml
In the previous commit I forgot to update the version in
vhost/Cargo.toml, let's set it to 0.8.1

Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
2023-07-17 14:31:19 +02:00
Stefano Garzarella
751f00b2f6 vhost: Release v0.8.1
We experienced a small dependency issue with the `vm-memory` crate
features, let's release this version to fix it.

Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
2023-07-17 13:26:11 +02:00
dependabot[bot]
a742edc3da build(deps): bump rust-vmm-ci from 7e9af57 to 9dfe5b2
Bumps [rust-vmm-ci](https://github.com/rust-vmm/rust-vmm-ci) from `7e9af57` to `9dfe5b2`.
- [Commits](7e9af57588...9dfe5b267c)

---
updated-dependencies:
- dependency-name: rust-vmm-ci
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-07-16 17:13:47 +08:00
Sergio Lopez
9306ce05a6 vhost: Always enable vm-memory/backend-mmap
Since 4029089f ('vhost: Add support for Xen memory mappings') the
feature backend-mmap of the vm-memory crate is no longer just a
dev-dependency, as we're unconditionally importing GuestMemoryMmap from
'src/backend.rs'. Make it a general build dependency.

Signed-off-by: Sergio Lopez <slp@redhat.com>
2023-07-16 16:58:21 +08:00
Stefano Garzarella
900b9a5c41 vhost: Release v0.8.0
Release a new version with Xen support.
We broke the build with the default features, but not yet made the
release, so this should be the real release.

Fixes: b128901 ("vhost: Release v0.8.0")
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
2023-07-05 13:42:54 +02:00
Viresh Kumar
1ce5280417 vhost: Use vhost_user definitions from within #[cfg] block
The `vhost-user` feature isn't enabled by default and using it without
protection in backend.rs makes the vhost only build fails.

Fix it by protecting it with the `#[cfg]` block.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2023-07-05 13:42:54 +02:00
Viresh Kumar
8783a8dda0 vhost-user-backend: Release v0.10.0
Release a new version with Xen support.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2023-07-05 16:49:12 +08:00
Viresh Kumar
78f062ac0b vhost-user-backend: Add support for Xen memory mappings
Migrate to a newer version of the vhost and other dependencies and add
support for xen memory mappings. Add a corresponding xen feature for
vhost-user-backend crate.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2023-07-05 16:49:12 +08:00
Viresh Kumar
b128901ad3 vhost: Release v0.8.0
Release a new version with Xen support.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2023-07-05 16:49:12 +08:00
Viresh Kumar
c63c9b3c83 vhost: Enable XEN_MMAP protocol feature for xen
Automatically enable the VhostUserProtocolFeatures::XEN_MMAP feature for
backends for Xen specific builds. With these the backends don't need to
enable this feature and can directly support Xen.

Suggested-by: Erik Schilling <erik.schilling@linaro.org>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2023-07-05 16:49:12 +08:00
Viresh Kumar
4029089f75 vhost: Add support for Xen memory mappings
The vm-memory crate now supports Xen specific memory mappings via a
special feature: "xen".

Add a corresponding feature for vhost crate and add support for Xen
memory regions. Update various dependencies to align to the same version
of vm-memory crate.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2023-07-05 16:49:12 +08:00
Erik Schilling
3e6ba3cfb7 vhost-user-backend: de-duplicate match branches
No need to repeat the struct definition here. The only purpose is to
register an event_fd at epoll if one exists.

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
2023-07-04 11:19:29 +02:00
Viresh Kumar
3cbdceaaf7 Update coverage
Update coverage to 84.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2023-06-29 09:09:31 +02:00
Viresh Kumar
fc2b99d35a vhost: Add VhostUserMemoryRegionInfo::new() for tests
A lot of tests are creating objects of this structure directly, it would
be better if they all use a function instead to do so.

This will also be beneficial for future commits where more fields will
be added to this structure.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2023-06-29 09:09:31 +02:00
Viresh Kumar
26fd3cea33 vhost_user: Simplify VhostUserSingleMemoryRegion
VhostUserSingleMemoryRegion contains an extra padding before
VhostUserMemoryRegion, and everything else remains the same. Lets reuse
the same structure instead of duplicating implementation here.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2023-06-29 09:09:31 +02:00
Viresh Kumar
dd4597ad8a vhost-user-backend: Don't create MmapRegion separately
GuestRegionMmap::from_range() can take care of creating the region as
well now, use it.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2023-06-29 09:09:31 +02:00
German Maglione
958cdec2b8 get_vring_base should not reset the queue
The spec specifies that on receiving `GET_VRING_BASE` the backend should
stop the vring, but not that it must be reset. This is intended for
`VHOST_USER_RESET_DEVICE`, also in this case the spec makes a
difference between stopping and disabling the ring.

The spec also doesn't forbid to send `VHOST_USER_SET_VRING_ENABLE` to
enable the vring after receiving `GET_VRING_BASE` or sending more
`GET_VRING_BASE` messages, which would always respond 0. Moreover, qemu
doesn't reset the vring either.

Signed-off-by: German Maglione <gmaglione@redhat.com>
2023-06-28 16:56:46 +02:00
Viresh Kumar
29a7f8c68f vhost: Fix clippy::derivable-impls warnings
Clippy warns saying:

error: this `impl` can be derived

Derive them instead to fix those.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
2023-06-28 22:49:13 +08:00
Stefano Garzarella
a48da0890c vhost: add safety comments on unsafe blocks
In rust-vmm-ci we are enabling `clippy::undocumented_unsafe_blocks` as
errors, so let's comment all unsafe blocks to avoid failures in the
CI.

Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
2023-06-28 22:49:13 +08:00
dependabot[bot]
1256ab9bc9 build(deps): bump rust-vmm-ci from 99fe2eb to 7e9af57
Bumps [rust-vmm-ci](https://github.com/rust-vmm/rust-vmm-ci) from `99fe2eb` to `7e9af57`.
- [Commits](99fe2eb2e0...7e9af57588)

---
updated-dependencies:
- dependency-name: rust-vmm-ci
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-06-28 22:49:13 +08:00
Bo Chen
bdc6f2ab2b vhost: vdpa: Provide custom set_vring_addr() implementation
Unlike other vhost backends (e.g. vhost-net and vhost-vsock), vDPA
backends can not work with host virtual address (HVA), instead they
expect I/O virtual address (IOVA). The IOVA can be mapped 1:1 with
guest physical address (GPA) when no IOMMU is involved. This is why
the default implementation of `set_vring_addr()` from Trait
`VhostBackend` is no longer working with vDPA backends. To solve this
issue, this patch provides a custom `set_vring_addr()` implementation
for Trait `VhostKernVdpa`.

Fixes: #164

Signed-off-by: Bo Chen <chen.bo@intel.com>
2023-06-06 09:56:50 +02:00
Sergio Lopez
03d31fae7a Release vhost v0.7.0 and vhost-user-backend v0.9.0
Set up the stage to release vhost v0.7.0 and vhost-user-backend v0.9.0
to accommodate to the new vm-memory, virtio-bindings and virtio-queue
releases.

Signed-off-by: Sergio Lopez <slp@redhat.com>
2023-05-19 10:23:30 +02:00
Sergio Lopez
a66bd0d69c Bulk update dependencies across workspace
vhost:
 - vm-memory 0.10.0 -> 0.11.0

vhost-user-backend:
 - virtio-bindings 0.1.0 -> 0.2.0
 - virtio-queue 0.7.0 -> 0.8.0
 - vm-memory 0.10.0 -> 0.11.0

Signed-off-by: Sergio Lopez <slp@redhat.com>
2023-05-19 10:23:30 +02:00
German Maglione
1d2368e5f6 Fix return value of GET_VRING_BASE message
When the frontend sends the `GET_VRING_BASE` message, we should return
 the vring's last available index and stop the vring. To return the
 correct value we should not reset the queue before getting its value,
 otherwise we will always return 0.

Signed-off-by: German Maglione <gmaglione@redhat.com>
2023-05-17 12:56:00 +02:00
Xuewei Niu
7e76859b3e Fix set_vring_addr issues
`VhostBackend::set_vring_addr()` receives a vring config data which
contains the addresses of desc table, used ring and avail ring.
`VhostBackend::is_valid()` checks the addresses in the guest address space.
`VHOST_SET_VRING_ADDR` uses the addresses in the host address space.
However, the method doesn't convert those addresses.

To address this issue, the addresses passed by the config are checked in
the guest address space. Then, they are converted by
`VringConfigData::to_vhost_vring_addr()` into the host address space to
setup the vring on the kernel.

Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
2023-05-12 17:37:38 +08:00
Viresh Kumar
ef9ae28a6a vhost_user: Slave requests aren't only FS specific
There are a lot of slave requests supported by vhost-user protocol, it
isn't just about FS_MAP and FS_UNMAP. Rename the files and declarations
to slave request specific names.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2023-03-03 22:21:45 +08:00
dependabot[bot]
9b6c40035f build(deps): update nix requirement from 0.25 to 0.26
Updates the requirements on [nix](https://github.com/nix-rust/nix) to permit the latest version.
- [Release notes](https://github.com/nix-rust/nix/releases)
- [Changelog](https://github.com/nix-rust/nix/blob/master/CHANGELOG.md)
- [Commits](https://github.com/nix-rust/nix/compare/v0.25.0...v0.26.1)

---
updated-dependencies:
- dependency-name: nix
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-01-12 09:54:26 +01:00
Alyssa Ross
9e5396942f vhost-user-backend: add repository metadata
I was surprised I couldn't click through to the source from
<https://lib.rs/crates/vhost-user-backend>.

Signed-off-by: Alyssa Ross <hi@alyssa.is>
2023-01-04 14:01:46 +01:00
Alyssa Ross
0152e88b42 vhost_user: fix UB on invalid master request
Since VhostUserMsgHeader implements ByteValued, it is supposed to be
safe to construct from any correctly-sized arbitrary byte array.
But that means we can do this:

	let bytes = b"\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00";
	let header = VhostUserMsgHeader::<MasterReq>::from_slice(bytes).unwrap();
	header.get_code()

constructing an invalid MasterReq, using only functions that are
marked as safe.  Constructing an invalid enum value is undefined
behavior in Rust, so this API is unsound.  This wasn't considered by
the safety comment in VhostUserMsgHeader::get_code, which only
considered the safety of requests that were valid enum variants.

If the vhost-user frontend process sends a message that the backend
doesn't recognise, that's exactly what will happen, so the UB can be
triggered from an external process (but a trusted one).

To fix this, we need to check whether the value is valid _before_
converting it.  Req::is_valid is changed to be a non-instance method,
so it can be called before constructing the Req.
VhostUserMsgHeader::get_code is changed to return a Result, to
accomodate the case where the request number is not a valid value for
R.

Signed-off-by: Alyssa Ross <alyssa.ross@unikie.com>
2023-01-02 15:13:49 +01:00
Alyssa Ross
7a874476e8 Fix clippy 0.1.66 warnings
Signed-off-by: Alyssa Ross <hi@alyssa.is>
2023-01-02 15:13:49 +01:00