Repository navigation
Close the session when login fails in Token.open() - #247
Merged
MatthiasValvekens merged 3 commits intoOct 25, 2026
Merged
MatthiasValvekens merged 3 commits into
MatthiasValvekens merged 3 commits into
Conversation
Token.open() opens a session with C_OpenSession and then authenticates with C_Login. If authentication failed (wrong PIN, locked PIN, user already logged in, or protected authentication requested from a module that does not support it), the exception propagated without closing the session, and since the caller never received a Session object, the handle stayed open until C_Finalize. Close it before re-raising. The C_CloseSession return value is ignored on purpose, so that it cannot mask the original error.
MatthiasValvekens
requested changes
Oct 25, 2026
MatthiasValvekens
left a comment
Collaborator
There was a problem hiding this comment.
Good catch; I'd factor the fix a bit differently, though.
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #247 +/- ##
==========================================
+ Coverage 91.52% 92.32% +0.80%
==========================================
Files 15 15
Lines 2713 2788 +75
Branches 27 27
==========================================
+ Hits 2483 2574 +91
+ Misses 228 212 -16
Partials 2 2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Raise ArgumentsBad for an unsupported protected authentication path before C_OpenSession, and decide on the C_Login result whether to close the session and raise or to return it, instead of a catch-all except.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #246
Token.open()opens a session withC_OpenSessionand then authenticates withC_Login. When authenticationraised (wrong or locked PIN, user already logged in, or protected authentication requested from a module without
CKF_PROTECTED_AUTHENTICATION_PATH), the exception propagated without closing the session. The caller never gets aSessionobject, so the handle stayed open untilC_Finalize.This wraps the authentication block in
try ... except BaseException:, callsC_CloseSessionand re-raises. TheC_CloseSessionreturn value is ignored on purpose so that it cannot mask the original error.BaseExceptionalso covers
KeyboardInterruptduring the (GIL-released) login.Tests (
tests/test_sessions.py, SoftHSMv2 only, like the neighbouring login tests):test_failed_login_does_not_leak_session: a failed user login must not leave a read-only session behind. Detectedwithout ctypes: a leftover read-only session makes a following security officer login fail with
SessionReadOnlyExists.test_unsupported_protected_auth_does_not_leak_session: same for theArgumentsBadpath.Without the fix both tests fail, and the leaked session also makes later tests in the module fail (
test_init_pin,test_open_session_and_login_so). With the fix the full suite passes on SoftHSM2 2.6.1 (Debian 13, Python 3.13):147 passed, 18 skipped. I did not run opencryptoki, Windows or macOS locally.