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

Fix dangling pointer issue #344

Merged
merged 2 commits into from May 24, 2022
Merged

Conversation

ionut-arm
Copy link
Member

This fixes an issue with the handling of the nonce field when opening
authentication sessions. The ESYS layer expects either a pointer to a
valid nonce of at least 16 bytes, or NULL if no nonce is provided. Our
handling, however, passed down an invalid pointer that was referencing a
now-defunct structure.

What happened, both before #340 and after, was that the Nonce input
was moved into a separate scope, either a match or a lambda function,
then converted to TPM2B_NONCE. A reference to this TPM2B_NONCE was
taken and converted to *const TPM2B_NONCE, which was passed outside of
the scope. The pointer, therefore, ended up referencing a structure that
was dropped at the end of that inner scope.

To ensure memory safety, we need to keep ownership of the TPM2B_NONCE
while the call is being made.

@ionut-arm ionut-arm added the bug Something isn't working label May 23, 2022
@ionut-arm ionut-arm self-assigned this May 23, 2022
@Superhepper
Copy link
Collaborator

Is it this that I have been seeing in my valgrind reports but for my life not been able to figure out what it was?

@ionut-arm
Copy link
Member Author

Possibly! I do wonder if there are other cases like this, I'll have a skim through the commands.

@ionut-arm ionut-arm marked this pull request as ready for review May 23, 2022 13:45
@ionut-arm
Copy link
Member Author

Ok, I've checked, seems to be the only place where shady stuff was going on (that I can tell).

@Superhepper
Copy link
Collaborator

It was because we passed that pointer into a function in unsafe block that rust did not catch the error.

@Superhepper
Copy link
Collaborator

That was it! I just ran valgrind on using code that temporary stored the converted variable inside the function scope. And all errors were gone.

We should really have some kind of guideline saying that 'performing work inside of unsafe blocks should be kept to an absolute minimum'. I know I have done a lot of 'fancy' code inside those unsafe blocks. We should probably try to move as much code as possible out of the unsafe calls. I wonder if there is some way to do that without creating to much bloat...

Copy link
Collaborator

@Superhepper Superhepper left a comment

Choose a reason for hiding this comment

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

Nice that you found this.

This fixes an issue with the handling of the nonce field when opening
authentication sessions. The ESYS layer expects either a pointer to a
valid nonce of at least 16 bytes, or NULL if no nonce is provided. Our
handling, however, passed down an invalid pointer that was referencing a
now-defunct structure.

What happened, both before parallaxsecond#340 and after, was that the `Nonce` input
was moved into a separate scope, either a `match` or a lambda function,
then converted to `TPM2B_NONCE`. A reference to this `TPM2B_NONCE` was
taken and converted to `*const TPM2B_NONCE`, which was passed outside of
the scope. The pointer, therefore, ended up referencing a structure that
was dropped at the end of that inner scope.

To ensure memory safety, we need to keep ownership of the `TPM2B_NONCE`
while the call is being made.

Signed-off-by: Ionut Mihalcea <ionut.mihalcea@arm.com>
@ionut-arm ionut-arm force-pushed the dangling-pointer branch 2 times, most recently from 807505d to 7e7aaac Compare May 24, 2022 13:51
Signed-off-by: Ionut Mihalcea <ionut.mihalcea@arm.com>
@ionut-arm
Copy link
Member Author

Ok, I've added a workflow running valgrind over the test suite, which seems to work fine on Ubuntu (was failing due to some leaks lower down the stack on Fedora, oops 😬 ).

@ionut-arm ionut-arm merged commit a301e33 into parallaxsecond:main May 24, 2022
@ionut-arm ionut-arm deleted the dangling-pointer branch May 24, 2022 16:02
ionut-arm added a commit to ionut-arm/rust-tss-esapi that referenced this pull request May 25, 2022
Port the security fix related to the nonce dangling pointer during auth
session creation, and fix the other issues flagged by the compiler.

Signed-off-by: Ionut Mihalcea <ionut.mihalcea@arm.com>
@wiktor-k wiktor-k mentioned this pull request May 25, 2022
Superhepper added a commit that referenced this pull request May 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants