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

potential resource leak #198

Open
baloo opened this issue Feb 27, 2021 · 10 comments
Open

potential resource leak #198

baloo opened this issue Feb 27, 2021 · 10 comments

Comments

@baloo
Copy link
Contributor

baloo commented Feb 27, 2021

When running tests with swtpm (0.5.2) linked with libtpms(0.8.0). I'm seeing a lot of:

WARNING:esys:src/tss2-esys/api/Esys_StartAuthSession.c:390:Esys_StartAuthSession_Finish() Received TPM Error
ERROR:esys:src/tss2-esys/api/Esys_StartAuthSession.c:136:Esys_StartAuthSession() Esys Finish ErrorCode (0x00000903)
[2021-02-27T04:12:22Z ERROR tss_esapi::context::tpm_commands::session_commands] Error when creating a session: out of memory for session contexts
WARNING:esys:src/tss2-esys/api/Esys_StartAuthSession.c:390:Esys_StartAuthSession_Finish() Received TPM Error
ERROR:esys:src/tss2-esys/api/Esys_StartAuthSession.c:136:Esys_StartAuthSession() Esys Finish ErrorCode (0x00000903)
[2021-02-27T04:12:22Z ERROR tss_esapi::context::tpm_commands::session_commands] Error when creating a session: out of memory for session contexts
WARNING:esys:src/tss2-esys/api/Esys_StartAuthSession.c:390:Esys_StartAuthSession_Finish() Received TPM Error
ERROR:esys:src/tss2-esys/api/Esys_StartAuthSession.c:136:Esys_StartAuthSession() Esys Finish ErrorCode (0x00000903)
[2021-02-27T04:12:22Z ERROR tss_esapi::context::tpm_commands::session_commands] Error when creating a session: out of memory for session contexts

---- test_create_ak_rsa_rsa stdout ----
thread 'test_create_ak_rsa_rsa' panicked at 'called `Result::unwrap()` on an `Err` value: Tss2Error(FormatZero(FormatZeroResponseCode { .0: 2307, error_number: 3, format_selector: false, version: true, tcg_vendor_indicator: false, severity: true }))', tss-esapi/tests/abstraction_ak_tests.rs:31:6
stack backtrace:
   0: rust_begin_unwind
             at /rustc/98f8cce6db6c6c6660eeffee2b3903104e547ecf/library/std/src/panicking.rs:493:5
   1: core::panicking::panic_fmt
             at /rustc/98f8cce6db6c6c6660eeffee2b3903104e547ecf/library/core/src/panicking.rs:92:14
   2: core::option::expect_none_failed
             at /rustc/98f8cce6db6c6c6660eeffee2b3903104e547ecf/library/core/src/option.rs:1300:5
   3: core::result::Result<T,E>::unwrap
             at /nix/store/wd3x41h39smq4js8ds0jbl35jxnydxvm-rust-1.52.0-nightly-2021-02-25-98f8cce6d/lib/rustlib/src/rust/library/core/src/result.rs:1037:23
   4: abstraction_ak_tests::test_create_ak_rsa_rsa
             at ./tests/abstraction_ak_tests.rs:23:5
   5: abstraction_ak_tests::test_create_ak_rsa_rsa::{{closure}}
             at ./tests/abstraction_ak_tests.rs:19:1
   6: core::ops::function::FnOnce::call_once
             at /nix/store/wd3x41h39smq4js8ds0jbl35jxnydxvm-rust-1.52.0-nightly-2021-02-25-98f8cce6d/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
   7: core::ops::function::FnOnce::call_once
             at /rustc/98f8cce6db6c6c6660eeffee2b3903104e547ecf/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

---- test_create_and_use_ak stdout ----
thread 'test_create_and_use_ak' panicked at 'called `Result::unwrap()` on an `Err` value: Tss2Error(FormatZero(FormatZeroResponseCode { .0: 2307, error_number: 3, format_selector: false, version: true, tcg_vendor_indicator: false, severity: true }))', tss-esapi/tests/abstraction_ak_tests.rs:68:6
stack backtrace:
   0: rust_begin_unwind
             at /rustc/98f8cce6db6c6c6660eeffee2b3903104e547ecf/library/std/src/panicking.rs:493:5
   1: core::panicking::panic_fmt
             at /rustc/98f8cce6db6c6c6660eeffee2b3903104e547ecf/library/core/src/panicking.rs:92:14
   2: core::option::expect_none_failed
             at /rustc/98f8cce6db6c6c6660eeffee2b3903104e547ecf/library/core/src/option.rs:1300:5
   3: core::result::Result<T,E>::unwrap
             at /nix/store/wd3x41h39smq4js8ds0jbl35jxnydxvm-rust-1.52.0-nightly-2021-02-25-98f8cce6d/lib/rustlib/src/rust/library/core/src/result.rs:1037:23
   4: abstraction_ak_tests::test_create_and_use_ak
             at ./tests/abstraction_ak_tests.rs:60:19
   5: abstraction_ak_tests::test_create_and_use_ak::{{closure}}
             at ./tests/abstraction_ak_tests.rs:55:1
   6: core::ops::function::FnOnce::call_once
             at /nix/store/wd3x41h39smq4js8ds0jbl35jxnydxvm-rust-1.52.0-nightly-2021-02-25-98f8cce6d/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
   7: core::ops::function::FnOnce::call_once
             at /rustc/98f8cce6db6c6c6660eeffee2b3903104e547ecf/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Should I restart the TPM, the tests go further, but still crash at some point.
This is how I start my swtpm:
https://github.com/baloo/reproducibility-lab/blob/baloo/wip/pcr-eventlog-attestation/pkgs/pcr-eventlog-attestation/shell.nix#L152

This would look like a resources leak of some sort.

@Superhepper
Copy link
Collaborator

Yes, we have this for a while I think. Some tests just never seem to work when running them locally (i.e. running them for a second time on a machine). So my guess is that it is some kind if permanent handle that does not get removed. I have not been looking closely at this though so I am just guessing. It would be really nice if it could be fixed though. The first thing I would do here in this test would be to remove the unwrap() to expect() so it gets easier to see what went wrong.

@baloo
Copy link
Contributor Author

baloo commented Feb 28, 2021

I triggered the issue elsewhere. A tpm2 flushcontext --transient-object fixed it.

@ionut-arm
Copy link
Member

I have previously seen this, thought it was solved 😕 damn... We are leaking sessions somewhere, will try and rat it out at some point.

@ionut-arm
Copy link
Member

Ok, turns out something like this happens in the Parsec service sometimes, but it's due to how the service handles signals.

@baloo - did the tpm2_flushcontext fix it permanently? I can run all the tests multiple times in a row with no failure due to sessions sticking around

@baloo
Copy link
Contributor Author

baloo commented Mar 29, 2021

no, but after a flush context I could re-run the tests again.

@DNJha
Copy link

DNJha commented Oct 12, 2022

Hi, just checking if this issue is already being fixed or not as I'm getting a similar error with the unit tests.

---- tpm::test::test::test_sign stdout ---- thread 'main' panicked at 'Error in starting the auth session: Tss2Error(FormatZero(FormatZeroResponseCode { .0: 2307, error_number: 3, format_selector: false, version: true, tcg_vendor_indicator: false, severity: true }))', src/tpm/mod.rs:55:10
The tests are passing in the first few instances and then failing. It can only be resolved after I restart the TPM.
I'm using SWTPM with Rust esapi.

@ionut-arm
Copy link
Member

Hey - is that one of our unit tests? If not, could you post a link to/copy of the test?

@DNJha
Copy link

DNJha commented Oct 14, 2022

Hi @ionut-arm, It's one of the tests I wrote. It actually tries to make the context static for further use.

You can find the minimal function and test on playground

The test executes successfully for 3 times and then fails for the remaining time.

Any help is appreciated.

@kcking
Copy link
Contributor

kcking commented Jan 23, 2024

This could be because you are storing Context in a static variable, which will prevent Context::drop from running on program exit. Note that D::drop never runs in this example.

Instead of storing Context in a static, what about passing around an Arc<Mutex<Context>>, which should one drop once the last reference to it is dropped.

@Superhepper
Copy link
Collaborator

What version of the rust-tss-esapi is this? If possible could you run it with latest from master and see what the error says?
I am guessing that the things mentioned above are the most likely causes.

But I think I should point out that I have also seen problems with Esys_StartAuthSession in the ekand the ak functions when the TPM software simulator have been built with a smaller amount of memory. I was trying to find the cause of this at one point but was unable to exactly find anything.

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

No branches or pull requests

5 participants