Skip to content

Commit

Permalink
Make CardMonitor() thread safe
Browse files Browse the repository at this point in the history
We had a TOCTOU bug in the handling of CardMonitor.instance field.
https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use

Thanks to Lars Lengersdorf for the bug reprot
" pyscard seems to be not thread save #162 "
#162
  • Loading branch information
LudovicRousseau committed Mar 4, 2024
1 parent ba0da37 commit e88b2e9
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions smartcard/CardMonitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""

from threading import Thread, Event
from threading import Thread, Event, Lock
from time import sleep
import traceback

Expand Down Expand Up @@ -123,10 +123,12 @@ def __str__(self):

# the singleton
instance = None
lock = Lock()

def __init__(self):
if not CardMonitor.instance:
CardMonitor.instance = CardMonitor.__CardMonitorSingleton()
with CardMonitor.lock:
if not CardMonitor.instance:
CardMonitor.instance = CardMonitor.__CardMonitorSingleton()

def __getattr__(self, name):
return getattr(self.instance, name)
Expand Down

0 comments on commit e88b2e9

Please sign in to comment.