From d784e763495c8d53e29a2debdd9c0e0578f15a6a Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Thu, 26 Nov 2020 12:16:56 +0100 Subject: [PATCH] scd: Do not try to use a non-enabled app after card switching. * scd/app.c (app_dump_state): Also print the refcount. (maybe_switch_app): Make sure the app exists on the card. -- When switching the current card and the card does not support the same apps as the former, we now reset the app to the first one. Testcase is to use a standard OpenPGP card and a P15 card. Signed-off-by: Werner Koch --- scd/app.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/scd/app.c b/scd/app.c index 7ba714b3e..933ad46d6 100644 --- a/scd/app.c +++ b/scd/app.c @@ -304,8 +304,8 @@ app_dump_state (void) npth_mutex_lock (&card_list_lock); for (c = card_top; c; c = c->next) { - log_info ("app_dump_state: card=%p slot=%d type=%s\n", - c, c->slot, strcardtype (c->cardtype)); + log_info ("app_dump_state: card=%p slot=%d type=%s refcount=%u\n", + c, c->slot, strcardtype (c->cardtype), c->ref_count); /* FIXME The use of log_info risks a race! */ for (a=c->app; a; a = a->next) log_info ("app_dump_state: app=%p type='%s'\n", @@ -1435,6 +1435,23 @@ maybe_switch_app (ctrl_t ctrl, card_t card, const char *keyref) { /* For whatever reasons the current apptype has not been set - * fix that and use the current app. */ + if (DBG_APP) + log_debug ("slot %d: no current app switching to %s\n", + card->slot, strapptype (card->app->apptype)); + ctrl->current_apptype = card->app->apptype; + return 0; + } + for (app = card->app; app; app = app->next) + if (app->apptype == ctrl->current_apptype) + break; + if (!app) + { + /* The current app is not supported by this card. Set the first + * app of the card as current. */ + if (DBG_APP) + log_debug ("slot %d: current app %s not available switching to %s\n", + card->slot, strapptype (ctrl->current_apptype), + strapptype (card->app->apptype)); ctrl->current_apptype = card->app->apptype; return 0; }