Skip to content

Commit

Permalink
Fix race with dwEventStatus
Browse files Browse the repository at this point in the history
The problem is (in part) with the switch() that may read the variable
more than once.

==================
WARNING: ThreadSanitizer: data race (pid=10828)
  Write of size 8 at 0x7b040000ae78 by thread T6 (mutexes: write M0):
    #0 RFSetReaderEventState PCSC/src/readerfactory.c:1321:32 (pcscd+0x4c45ce)
    #1 EHStatusHandlerThread PCSC/src/eventhandler.c:382:11 (pcscd+0x4bee54)

  Previous read of size 8 at 0x7b040000ae78 by thread T8:
    #0 RFCheckReaderEventState PCSC/src/readerfactory.c:1350:24 (pcscd+0x4c46c6)
    #1 SCardStatus PCSC/src/winscard.c:1286:7 (pcscd+0x4ce7b5)
    #2 ContextThread PCSC/src/winscard_svc.c:635:16 (pcscd+0x4d0cc1)

  Location is heap block of size 16 at 0x7b040000ae70 allocated by thread T8:
    #0 malloc <null> (pcscd+0x42a65c)
    #1 RFAddReaderHandle PCSC/src/readerfactory.c:1249:14 (pcscd+0x4c43b4)
    #2 SCardConnect PCSC/src/winscard.c:491:7 (pcscd+0x4ccc16)
    #3 ContextThread PCSC/src/winscard_svc.c:502:16 (pcscd+0x4d0a70)

  Mutex M0 (0x7b4c000000a0) created at:
    #0 pthread_mutex_init <null> (pcscd+0x42d2fd)
    #1 RFAddReader PCSC/src/readerfactory.c:329:8 (pcscd+0x4c1d18)
    #2 HPAddDevice PCSC/src/hotplug_libudev.c:534:8 (pcscd+0x4cba1c)
    #3 HPEstablishUSBNotifications PCSC/src/hotplug_libudev.c:668:6 (pcscd+0x4cb4d4)

  Thread T6 (tid=11122, running) created by thread T2 at:
    #0 pthread_create <null> (pcscd+0x42be8b)
    #1 ThreadCreate PCSC/src/utils.c:184:8 (pcscd+0x4cc2dc)
    #2 EHSpawnEventHandler PCSC/src/eventhandler.c:237:7 (pcscd+0x4beb59)
    #3 RFAddReader PCSC/src/readerfactory.c:426:8 (pcscd+0x4c21b9)
    #4 HPAddDevice PCSC/src/hotplug_libudev.c:534:8 (pcscd+0x4cba1c)
    #5 HPEstablishUSBNotifications PCSC/src/hotplug_libudev.c:668:6 (pcscd+0x4cb4d4)

  Thread T8 (tid=11173, running) created by main thread at:
    #0 pthread_create <null> (pcscd+0x42be8b)
    #1 ThreadCreate PCSC/src/utils.c:184:8 (pcscd+0x4cc2dc)
    #2 CreateContextThread PCSC/src/winscard_svc.c:236:7 (pcscd+0x4cfd8b)
    #3 SVCServiceRunLoop PCSC/src/pcscdaemon.c:134:9 (pcscd+0x4c1082)
    #4 main PCSC/src/pcscdaemon.c:786:2 (pcscd+0x4c09c4)

SUMMARY: ThreadSanitizer: data race PCSC/src/readerfactory.c:1321:32 in RFSetReaderEventState
==================
  • Loading branch information
LudovicRousseau committed Dec 19, 2021
1 parent 4a2fb8f commit c2d6e38
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/readerfactory.c
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,7 @@ LONG RFCheckReaderEventState(READER_CONTEXT * rContext, SCARDHANDLE hCard)
{
LONG rv;
RDR_CLIHANDLES *currentHandle;
DWORD dwEventStatus;

(void)pthread_mutex_lock(&rContext->handlesList_lock);
currentHandle = list_seek(&rContext->handlesList, &hCard);
Expand All @@ -1324,7 +1325,8 @@ LONG RFCheckReaderEventState(READER_CONTEXT * rContext, SCARDHANDLE hCard)
return SCARD_E_INVALID_HANDLE;
}

switch(currentHandle->dwEventStatus)
dwEventStatus = currentHandle->dwEventStatus;
switch(dwEventStatus)
{
case 0:
rv = SCARD_S_SUCCESS;
Expand Down
2 changes: 1 addition & 1 deletion src/readerfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
struct RdrCliHandles
{
SCARDHANDLE hCard; /**< hCard for this connection */
DWORD dwEventStatus; /**< Recent event that must be sent */
_Atomic DWORD dwEventStatus; /**< Recent event that must be sent */
};

typedef struct RdrCliHandles RDR_CLIHANDLES;
Expand Down

0 comments on commit c2d6e38

Please sign in to comment.