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

Handle short-lived removals of reader in reader driver for PC/SC #2803

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Check for invalid pcsc card handle before calling SCardStatus
  • Loading branch information
xhanulik committed Mar 20, 2024
commit d1c5e0d13b8e99b1b84a25bb86a97b19bdf0da62
19 changes: 10 additions & 9 deletions src/libopensc/reader-pcsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,19 +384,20 @@ static int refresh_attributes(sc_reader_t *reader)
reader->flags |= SC_READER_CARD_PRESENT;
}

rv = priv->gpriv->SCardStatus(priv->pcsc_card, NULL, &readers_len, &cstate, &prot, atr, &atr_len);
if (priv->pcsc_card != 0
&& (rv == (LONG)SCARD_W_REMOVED_CARD || rv == (LONG)SCARD_E_INVALID_VALUE || rv == (LONG)SCARD_E_INVALID_HANDLE)) {
/* Timeout should denote no change from previous recorded state,
* but check for valid card handle */
reader->flags &= ~SC_READER_CARD_CHANGED;
if (priv->pcsc_card != 0) {
/* When reader is removed between two subsequent calls to refresh_attributes,
* SCardGetStatusChange does not notice the change - set it here */
reader->flags |= SC_READER_CARD_CHANGED;
* SCardGetStatusChange does not notice the change, test the card handle with SCardStatus */
rv = priv->gpriv->SCardStatus(priv->pcsc_card, NULL, &readers_len, &cstate, &prot, atr, &atr_len);

if (rv == (LONG)SCARD_W_REMOVED_CARD || rv == (LONG)SCARD_E_INVALID_VALUE || rv == (LONG)SCARD_E_INVALID_HANDLE)
reader->flags |= SC_READER_CARD_CHANGED;
/* If this happens, card must be reconnected, otherwise SCardGetStatusChange() will still return timeout
* and card handle will be invalid. */
} else {
/* Otherwise timeout denotes no change from previous recorded state. Make sure that
* changed flag is not set. */
reader->flags &= ~SC_READER_CARD_CHANGED;
}

LOG_FUNC_RETURN(reader->ctx, SC_SUCCESS);
}

Expand Down
Loading