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 for context handle management in PCSC reader driver #2077
Conversation
|
Thanks a lot for tracking this down! It's good as is. Using |
|
So this bug has been lurking there since 2008, over a decade, before surfacing? Interesting! I am not sure about the motivations for the original code but will try to make a qualified guess. The original code used -1 since it relied on SCardListReaders to return SCARD_E_INVALID_HANDLE in order to call SCardEstablishContext. Since SCardListReaders accepts 0 as input in Windows, the author chose -1, assuming it would be rejected on all platforms. This was bad coding practice for multiple reasons:
The author updated the code in 4fc6c49, explicitly recognizing an uninitialized context instead of relying on an error from SCardListReaders. Now it would have been a good time to switch to 0 as the invalid context value, but the value -1 was kept. |
|
0 is not less or more invalid than -1. On pcsc-lite the SCARDCONTEXT is a random number in [0, RAND_MAX] (see rand(3)). If you want to check a PC/SC context is valid you can/should use the API function SCardIsValidContext(). |
|
You're right, my bad. (Being too quick when assuming that 0 was reserved in both Windows and pcsc-lite). Thanks for pointing it out. This means that the proposed solution must be updated to not assume anything about the returned handle. I will get back with an updated fix ASAP. |
- Reset context to undefined handle value on error since call may alter output parameter. - Continue to assume -1 as undefined handle value in all PCSC implementations, to keep this fix as small and surgical as possible.
19b0071
to
4cfa4f3
Compare
|
A new fix was proposed, starting fresh, discarding the previous commits in this change branch. The new fix is even more surgical - changing only the problem with the altered output parameter and adding the zero terminators as a safeguard. The -1 value is kept as the invalid handle value. Even though there are no guarantees from the API for this value being reserved, keeping it for now minimizes the risk for any unexpected side effects. |
|
thanks! |
Proposed fix for issue #1999, taken a minimalistic change approach.
Checklist