[Pcsclite-git-commit] [PCSC] 01/10: SCardConnect(): Fix a possible duplicated hCard context

Ludovic Rousseau rousseau at moszumanska.debian.org
Fri Aug 5 15:48:03 UTC 2016


This is an automated email from the git hooks/post-receive script.

rousseau pushed a commit to branch master
in repository PCSC.

commit bb07a905c896e7a67e3d7b37a24b3637ada0546d
Author: Ludovic Rousseau <ludovic.rousseau at free.fr>
Date:   Thu Aug 4 11:00:04 2016 +0200

    SCardConnect(): Fix a possible duplicated hCard context
    
    It was possible for 2 applications to call SCardConnect() at the same
    time and that the same random hCard handle is generated and used for
    the 2 applications.
    
    The solution is to serialize the 2 generations of hCard handles. The
    second randomly generated handle will be found as already used and
    another handle will be generated.
    
    Thanks to Maksim Ivanov for the bug report
    "[Pcsclite-muscle] Possible generation of duplicate SCARDHANDLE"
    http://lists.alioth.debian.org/pipermail/pcsclite-muscle/Week-of-Mon-20160801/000673.html
---
 src/winscard.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/winscard.c b/src/winscard.c
index 78cac97..a7e1f46 100644
--- a/src/winscard.c
+++ b/src/winscard.c
@@ -190,6 +190,8 @@ static void profile_end(const char *f, int line)
 /** used for backward compatibility */
 #define SCARD_PROTOCOL_ANY_OLD	 0x1000
 
+static pthread_mutex_t LockMutex = PTHREAD_MUTEX_INITIALIZER;
+
 LONG SCardEstablishContext(DWORD dwScope, /*@unused@*/ LPCVOID pvReserved1,
 	/*@unused@*/ LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
 {
@@ -444,6 +446,11 @@ LONG SCardConnect(/*@unused@*/ SCARDCONTEXT hContext, LPCSTR szReader,
 	/*
 	 * Prepare the SCARDHANDLE identity
 	 */
+
+	/* we need a lock to avoid concurent generation of handles leading
+	 * to a possible hCard handle duplication */
+	(void)pthread_mutex_lock(&LockMutex);
+
 	*phCard = RFCreateReaderHandle(rContext);
 
 	Log2(PCSC_LOG_DEBUG, "hCard Identity: %lx", *phCard);
@@ -467,6 +474,7 @@ LONG SCardConnect(/*@unused@*/ SCARDCONTEXT hContext, LPCSTR szReader,
 			(void)RFDestroyReaderHandle(*phCard);
 			*phCard = 0;
 			rv = SCARD_E_SHARING_VIOLATION;
+			(void)pthread_mutex_unlock(&LockMutex);
 			goto exit;
 		}
 	}
@@ -483,6 +491,8 @@ LONG SCardConnect(/*@unused@*/ SCARDCONTEXT hContext, LPCSTR szReader,
 	 */
 	rv = RFAddReaderHandle(rContext, *phCard);
 
+	(void)pthread_mutex_unlock(&LockMutex);
+
 	if (rv != SCARD_S_SUCCESS)
 	{
 		/*

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pcsclite/PCSC.git



More information about the Pcsclite-cvs-commit mailing list