From cccc9bd5db1f00c35dff192935e91d46cafb259f Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Mon, 10 May 2021 10:31:08 +0900 Subject: [PATCH] scd: Make sure releasing PC/SC context. * scd/apdu.c (release_pcsc_context): New. (close_pcsc_reader): Use release_pcsc_context. Add assertion. (apdu_dev_list_start): Replace call of close_pcsc_reader into release_pcsc_context, add condition. -- GnuPG-bug-id: 5416 Signed-off-by: NIIBE Yutaka --- scd/apdu.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/scd/apdu.c b/scd/apdu.c index 34e606ada..08e195ccf 100644 --- a/scd/apdu.c +++ b/scd/apdu.c @@ -826,19 +826,27 @@ control_pcsc (int slot, pcsc_dword_t ioctl_code, } +static void +release_pcsc_context (void) +{ + /*log_debug ("%s: releasing context\n", __func__);*/ + log_assert (pcsc.context != 0); + pcsc_release_context (pcsc.context); + pcsc.context = 0; +} + static int close_pcsc_reader (int slot) { /*log_debug ("%s: count=%d (ctx=%x)\n", __func__, pcsc.count, pcsc.context);*/ (void)slot; - if (!pcsc.count || !--pcsc.count) + log_assert (pcsc.count > 0); + if (!--pcsc.count) { int i; /*log_debug ("%s: releasing context\n", __func__);*/ - if (pcsc.context) - pcsc_release_context (pcsc.context); - pcsc.context = 0; + release_pcsc_context (); for (i = 0; i < MAX_READER; i++) pcsc.rdrname[i] = NULL; } @@ -2041,8 +2049,9 @@ apdu_dev_list_start (const char *portstr, struct dev_list **l_p) err = gpg_error_from_syserror (); log_error ("error allocating memory for reader list\n"); + if (pcsc.count == 0) + release_pcsc_context (); npth_mutex_unlock (&reader_table_lock); - close_pcsc_reader (0); xfree (dl); return err; } @@ -2053,8 +2062,9 @@ apdu_dev_list_start (const char *portstr, struct dev_list **l_p) log_error ("pcsc_list_readers failed: %s (0x%lx)\n", pcsc_error_string (r), r); xfree (p); + if (pcsc.count == 0) + release_pcsc_context (); npth_mutex_unlock (&reader_table_lock); - close_pcsc_reader (0); xfree (dl); return iso7816_map_sw (pcsc_error_to_sw (r)); }