1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-22 10:19:57 +01:00

scd: Fix problem with reader list becoming empty.

* scd/apdu.c (close_pcsc_reader): Do not decrement refcount if already
zero.  Always release context if or becomes zero.
(apdu_dev_list_start): Unlock prior to close_pcsc_reader.  For PC/SC
increment the count.  Always release the lock.
(apdu_dev_list_finish): No more unlocking.  Use close_pcsc_reader
instead of code duplication.

* scd/apdu.c (pcsc_error_string): Add an error code.
* scd/scdaemon.c (scd_kick_the_loop): Fix a diagnostic.
--

There was an obvious bug in that the pcsc.count could go below zero
and thus there was no chance to get the context release.  Releasing
and recreating the context is at least under Windows important to get
rit of the PCSC_E_SERVICE_STOPPED.

Also removes a potential problem in holding the reader_table_lock
between calls to apdu_dev_list_start apdu_dev_list_finish.  There is
no need for this.  Instead we bump the pcsc.count.

The reader_table_lock strategy should be reviewed; we may be able to
remove it.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2021-04-28 18:19:33 +02:00
parent 178e4eb655
commit bb8e3996e4
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
2 changed files with 26 additions and 22 deletions

View File

@ -625,6 +625,7 @@ pcsc_error_string (long err)
case 0x001c: s = "card unsupported"; break; case 0x001c: s = "card unsupported"; break;
case 0x001d: s = "no service"; break; case 0x001d: s = "no service"; break;
case 0x001e: s = "service stopped"; break; case 0x001e: s = "service stopped"; break;
case 0x002e: s = "no readers available"; break;
default: s = "unknown PC/SC error code"; break; default: s = "unknown PC/SC error code"; break;
} }
return s; return s;
@ -827,12 +828,16 @@ control_pcsc (int slot, pcsc_dword_t ioctl_code,
static int static int
close_pcsc_reader (int slot) close_pcsc_reader (int slot)
{ {
/*log_debug ("%s: count=%d (ctx=%x)\n", __func__, pcsc.count, pcsc.context);*/
(void)slot; (void)slot;
if (--pcsc.count == 0 && npth_mutex_trylock (&reader_table_lock) == 0) if (!pcsc.count || !--pcsc.count)
{ {
int i; int i;
pcsc_release_context (pcsc.context); /*log_debug ("%s: releasing context\n", __func__);*/
npth_mutex_lock (&reader_table_lock);
if (pcsc.context)
pcsc_release_context (pcsc.context);
pcsc.context = 0; pcsc.context = 0;
for (i = 0; i < MAX_READER; i++) for (i = 0; i < MAX_READER; i++)
pcsc.rdrname[i] = NULL; pcsc.rdrname[i] = NULL;
@ -2008,12 +2013,15 @@ apdu_dev_list_start (const char *portstr, struct dev_list **l_p)
char *p = NULL; char *p = NULL;
if (!pcsc.context) if (!pcsc.context)
if (pcsc_init () < 0) {
{ /* log_debug ("%s: No context - calling init\n", __func__); */
xfree (dl); if (pcsc_init () < 0)
npth_mutex_unlock (&reader_table_lock); {
return gpg_error (GPG_ERR_NO_SERVICE); xfree (dl);
} npth_mutex_unlock (&reader_table_lock);
return gpg_error (GPG_ERR_NO_SERVICE);
}
}
r = pcsc_list_readers (pcsc.context, NULL, NULL, &nreader); r = pcsc_list_readers (pcsc.context, NULL, NULL, &nreader);
if (!r) if (!r)
@ -2024,9 +2032,9 @@ apdu_dev_list_start (const char *portstr, struct dev_list **l_p)
err = gpg_error_from_syserror (); err = gpg_error_from_syserror ();
log_error ("error allocating memory for reader list\n"); log_error ("error allocating memory for reader list\n");
npth_mutex_unlock (&reader_table_lock);
close_pcsc_reader (0); close_pcsc_reader (0);
xfree (dl); xfree (dl);
npth_mutex_unlock (&reader_table_lock);
return err; return err;
} }
r = pcsc_list_readers (pcsc.context, NULL, p, &nreader); r = pcsc_list_readers (pcsc.context, NULL, p, &nreader);
@ -2036,9 +2044,9 @@ apdu_dev_list_start (const char *portstr, struct dev_list **l_p)
log_error ("pcsc_list_readers failed: %s (0x%lx)\n", log_error ("pcsc_list_readers failed: %s (0x%lx)\n",
pcsc_error_string (r), r); pcsc_error_string (r), r);
xfree (p); xfree (p);
npth_mutex_unlock (&reader_table_lock);
close_pcsc_reader (0); close_pcsc_reader (0);
xfree (dl); xfree (dl);
npth_mutex_unlock (&reader_table_lock);
return iso7816_map_sw (pcsc_error_to_sw (r)); return iso7816_map_sw (pcsc_error_to_sw (r));
} }
@ -2074,8 +2082,12 @@ apdu_dev_list_start (const char *portstr, struct dev_list **l_p)
break; break;
} }
} }
pcsc.count++;
} }
npth_mutex_unlock (&reader_table_lock);
*l_p = dl; *l_p = dl;
return 0; return 0;
} }
@ -2092,20 +2104,10 @@ apdu_dev_list_finish (struct dev_list *dl)
else else
#endif #endif
{ /* PC/SC readers. */ { /* PC/SC readers. */
int i;
xfree (dl->table); xfree (dl->table);
for (i = 0; i < MAX_READER; i++) close_pcsc_reader (0);
pcsc.rdrname[i] = NULL;
if (pcsc.count == 0)
{
pcsc_release_context (pcsc.context);
pcsc.context = 0;
}
} }
xfree (dl); xfree (dl);
npth_mutex_unlock (&reader_table_lock);
} }

View File

@ -1231,7 +1231,7 @@ scd_kick_the_loop (void)
#else #else
int ret = kill (main_thread_pid, SIGCONT); int ret = kill (main_thread_pid, SIGCONT);
if (ret < 0) if (ret < 0)
log_error ("SetEvent for scd_kick_the_loop failed: %s\n", log_error ("sending signal for scd_kick_the_loop failed: %s\n",
gpg_strerror (gpg_error_from_syserror ())); gpg_strerror (gpg_error_from_syserror ()));
#endif #endif
} }
@ -1287,6 +1287,8 @@ handle_connections (int listen_fd)
events[0] = the_event = INVALID_HANDLE_VALUE; events[0] = the_event = INVALID_HANDLE_VALUE;
events[1] = INVALID_HANDLE_VALUE; events[1] = INVALID_HANDLE_VALUE;
/* Create event for manual reset, initially non-signaled. Make it
* waitable and inheritable. */
h = CreateEvent (&sa, TRUE, FALSE, NULL); h = CreateEvent (&sa, TRUE, FALSE, NULL);
if (!h) if (!h)
log_error ("can't create scd event: %s\n", w32_strerror (-1) ); log_error ("can't create scd event: %s\n", w32_strerror (-1) );