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

fw_meta: reduce raw pointer and unsafe usage #71

Merged
merged 4 commits into from
Aug 17, 2023

Conversation

00xc
Copy link
Member

@00xc 00xc commented Aug 10, 2023

Refactor parse_fw_meta_data() to avoid using unsafe and raw pointers as much as possible. This mainly consists in properly typing the metadata structures and going from raw pointers to typed references or values as soon as possible. Since the firmware metadata comes from the hypervisor, it can be considered untrusted in a confidential computing model, so extra care is taken when using lengths and offsets to index into memory.

All uses of unsafe are documented, and all potential memory safety issues should no longer be present, making the parsing much more robust.

The length field in the firmware metadata takes into account the size
of the length field itself plus the associated UUID. To get the size
of the contained tables, one must substract those two values. In
parse_fw_meta_data(), this was done incorrectly due to a lack of
parenthesis and operator precedence working from left to right.

For comparison, this is already done correctly in find_table():

    let len = orig_len - (mem::size_of::<Uuid>() + mem::size_of::<u16>());

Since the metadata is parsed backwards, the excessive computed length
would result in accesses to the memory before the firmware metadata.
This region would always be mapped, so no memory faults could end up
occuring.

Signed-off-by: Carlos López <[email protected]>
The functions that do the parsing are contained to the fw_meta module,
so they should return errors related to firmware. This simplifies
error handling in a few places.

Signed-off-by: Carlos López <[email protected]>
Copy link
Member

@cclaudio cclaudio left a comment

Choose a reason for hiding this comment

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

Looks good to me. I have only a few minor comments.

src/fw_meta.rs Outdated Show resolved Hide resolved
src/fw_meta.rs Outdated Show resolved Hide resolved
src/fw_meta.rs Show resolved Hide resolved
Refactor parse_fw_meta_data() to avoid using unsafe and raw pointers
as much as possible. This mainly consists in properly typing the
metadata structures and going from raw pointers to typed references
or values as soon as possible. Since the firmware metadata comes from
the hypervisor, it can be considered untrusted in a confidential
computing model, so extra care is taken when using lengths and
offsets to index into memory.

Signed-off-by: Carlos López <[email protected]>
@joergroedel joergroedel merged commit ed3f199 into coconut-svsm:main Aug 17, 2023
2 checks passed
@00xc 00xc deleted the fw_meta branch January 25, 2024 14:45
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