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

Improve usage of unsafe blocks #18

Merged
merged 1 commit into from
Dec 18, 2019
Merged

Conversation

ionut-arm
Copy link
Member

This commit improves our handling of unsafe blocks. It moves the
unsafe label to methods that could lead to undefined behaviour
instead of hiding the lack of safety inside the method.

All remaining unsafe blocks should be safe to a sane level - we control the inputs to the function calls within and can guarantee, based on trust (usually in the TSS stack below) that the calls are safe.
Thread safety is offered by all Context struct methods requiring mutable references to self.

Fixes #2

@ionut-arm ionut-arm added bug Something isn't working enhancement New feature or request labels Dec 18, 2019
@ionut-arm ionut-arm added this to the Rust tss-esapi v1.0 milestone Dec 18, 2019
@ionut-arm ionut-arm self-assigned this Dec 18, 2019

fn try_from(tss_signature: TPMT_SIGNATURE) -> Result<Self> {
impl Signature {
pub unsafe fn try_from(tss_signature: TPMT_SIGNATURE) -> Result<Self> {
Copy link
Member

Choose a reason for hiding this comment

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

I think that is a very good decision to put the unsafe on the method here as we can not trust that the tss_signature argument is always well-composed. The caller of this method needs to make the decision as only him knows where the parameter comes from.

Copy link
Member

Choose a reason for hiding this comment

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

I was thinking: "what if the method is not public?"
But even so, it would maybe be wrong to put it safe anyway.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe a comment would be nice to explain those things? Or that could be part of the documentation of the method itself.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I decided to just leave it to documentation, a comment isn't as visible for users


// Close the context.
unsafe { tss2_esys::Esys_Finalize(&mut esys_context.into_raw() as *mut *mut ESYS_CONTEXT) };
unsafe { tss2_esys::Esys_Finalize(&mut esys_context.into_raw()) };
Copy link
Member

Choose a reason for hiding this comment

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

💌

@@ -412,7 +412,7 @@ impl Context {

if ret.is_success() {
let signature = unsafe { MBox::from_raw(signature) };
Ok((*signature).try_into()?)
Ok(unsafe { Signature::try_from(*signature)? })
Copy link
Member

Choose a reason for hiding this comment

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

Maybe a comment to explain why this is safe would make us better unsafe citizens 👷

Copy link
Member

Choose a reason for hiding this comment

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

Same everywhere we use the unsafe block actually in my opinion. The same way we kind of have to justify when we panic explicitely.

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, will add

This commit improves our handling of unsafe blocks. It moves the
unsafe label to methods that could lead to undefined behaviour
instead of hiding the lack of safety inside the method.

Signed-off-by: Ionut Mihalcea <ionut.mihalcea@arm.com>
@ionut-arm ionut-arm merged commit bfb3bbf into parallaxsecond:master Dec 18, 2019
@ionut-arm ionut-arm deleted the unsafe branch December 18, 2019 14:52
tgonzalezorlandoarm pushed a commit to tgonzalezorlandoarm/rust-tss-esapi that referenced this pull request Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Investigate use of unsafe and panicking
2 participants