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

vtpm: add safety comments and fix small issues #574

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

stefano-garzarella
Copy link
Member

This is part of the effort for #228 to enable undocumented_unsafe_blocks clippy lint.

@stefano-garzarella
Copy link
Member Author

CC @cclaudio

@joergroedel joergroedel requested a review from cclaudio December 17, 2024 13:51
@joergroedel joergroedel added the wait-for-review PR needs for approval by reviewers label Dec 17, 2024
@stefano-garzarella
Copy link
Member Author

v2:

@stefano-garzarella
Copy link
Member Author

CI is failing https://github.com/coconut-svsm/svsm/actions/runs/12428229021/job/34699433885?pr=574#step:7:1353 with:

make[1]: bindgen: No such file or directory

@joergroedel I'm not sure if it's related to this PR.
Any change in the CI container?

@stefano-garzarella
Copy link
Member Author

It looks like for some reason now the CI container doesn't have bindgen installed, fixed in #583

@stefano-garzarella
Copy link
Member Author

v3:

@joergroedel
Copy link
Member

v3:

* rebased on main after merging [CI: install bindgen-cli #583](https://github.com/coconut-svsm/svsm/pull/583) that fixes the CI issue

Thanks, waiting a bit more for @cclaudio to approve.

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.

LGTM.

@cclaudio
Copy link
Member

v3:

* rebased on main after merging [CI: install bindgen-cli #583](https://github.com/coconut-svsm/svsm/pull/583) that fixes the CI issue

Thanks, waiting a bit more for @cclaudio to approve.

For some reason my inbox message filters did not pick this one, sorry about that.

Comment on lines +242 to 243
// SAFETY: vaddr is just mapped, and its size is PAGE_SIZE
let buffer = unsafe { from_raw_parts_mut(vaddr.as_mut_ptr::<u8>(), PAGE_SIZE) };
Copy link
Member

Choose a reason for hiding this comment

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

Is this truly safe though? Isn't this a shared buffer? Creating a mutable reference to it is unsound if the contents can be modified by the guest.

Copy link
Member Author

Choose a reason for hiding this comment

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

The guest should not touch it while issuing the SVSM call, which IIUC is also sync. @cclaudio can you confirm this?

kernel/src/vtpm/tcgtpm/wrapper.rs Outdated Show resolved Hide resolved
@joergroedel joergroedel added in-review PR is under active review and not yet approved and removed wait-for-review PR needs for approval by reviewers labels Jan 7, 2025
`Layout::from_size_align(size, align)` can return an error if following
conditions are not met:
 - align must not be zero,
 - align must be a power of two,
 - size, when rounded up to the nearest multiple of align, must not
   overflow isize (i.e., the rounded value must be less than or equal
   to isize::MAX).

Conditions on `align` are always met, but the size is specified by
the caller, so it can fail. Let's propagate the error instead of
panic.

Signed-off-by: Stefano Garzarella <[email protected]>
The current implementation lacks some requirements defined by
POSIX documentation of `void *realloc(void *ptr, size_t size);`:

- If ptr is a null pointer, realloc() shall be equivalent to malloc()
  for the specified size.

- If the size of the space requested is zero, the behavior shall be
  implementation-defined.
  In this case we are following Linux implementation, from malloc(3)
  man page:
  - If size is equal to zero, and ptr is not NULL, then the call is
    equivalent to free(ptr).

[1] https://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html

Signed-off-by: Stefano Garzarella <[email protected]>
Add SAFETY comments around calls to GlobalAlloc and checks as required
by the documentation[1], especially on non-zero size and pointer validity.

[1] https://doc.rust-lang.org/alloc/alloc/trait.GlobalAlloc.html

Signed-off-by: Stefano Garzarella <[email protected]>
Add SAFETY comments around FFI calls and some checks for return
values.

Signed-off-by: Stefano Garzarella <[email protected]>
Add SAFETY comment around `from_raw_parts_mut()` call.

Signed-off-by: Stefano Garzarella <[email protected]>
In several places we use `if let Ok(foo) = func() {` creating large
indented blocks, let's replace with `let Ok(foo) = func() else {` by
immediately returning a null pointer and reducing the indentation
to make the code more readable.

Suggested-by: Carlos López <[email protected]>
Signed-off-by: Stefano Garzarella <[email protected]>
@stefano-garzarella
Copy link
Member Author

v4:

  • rebased on main
  • add one patch (last one) reducing indentation [@00xc ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in-review PR is under active review and not yet approved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants