From 18844d096a4e0348120040368d53542fad5cca6e Mon Sep 17 00:00:00 2001 From: Janek Bevendorff Date: Fri, 24 Feb 2017 17:50:19 +0100 Subject: [PATCH] Make other YubiKey driver methods thread-safe --- src/keys/YkChallengeResponseKey.cpp | 1 - src/keys/drivers/YubiKey.cpp | 21 ++++++++++++++++++--- src/keys/drivers/YubiKey.h | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/keys/YkChallengeResponseKey.cpp b/src/keys/YkChallengeResponseKey.cpp index 31ad1d86..60db4282 100644 --- a/src/keys/YkChallengeResponseKey.cpp +++ b/src/keys/YkChallengeResponseKey.cpp @@ -33,7 +33,6 @@ YkChallengeResponseKey::YkChallengeResponseKey(int slot, bool blocking) : m_slot(slot), m_blocking(blocking) { - connect(this, SIGNAL(userInteractionRequired()), KEEPASSXC_MAIN_WINDOW, SLOT(showYubiKeyPopup())); connect(this, SIGNAL(userConfirmed()), KEEPASSXC_MAIN_WINDOW, SLOT(hideYubiKeyPopup())); } diff --git a/src/keys/drivers/YubiKey.cpp b/src/keys/drivers/YubiKey.cpp index bab0d721..a490f069 100644 --- a/src/keys/drivers/YubiKey.cpp +++ b/src/keys/drivers/YubiKey.cpp @@ -34,7 +34,7 @@ #define m_yk (static_cast(m_yk_void)) #define m_ykds (static_cast(m_ykds_void)) -YubiKey::YubiKey() : m_yk_void(NULL), m_ykds_void(NULL) +YubiKey::YubiKey() : m_yk_void(NULL), m_ykds_void(NULL), m_mutex(QMutex::Recursive) { } @@ -51,11 +51,14 @@ YubiKey* YubiKey::instance() bool YubiKey::init() { + m_mutex.lock(); + // previously initialized if (m_yk != NULL && m_ykds != NULL) { if (yk_get_status(m_yk, m_ykds)) { // Still connected + m_mutex.unlock(); return true; } else { // Initialized but not connected anymore, re-init @@ -64,12 +67,14 @@ bool YubiKey::init() } if (!yk_init()) { + m_mutex.unlock(); return false; } // TODO: handle multiple attached hardware devices m_yk_void = static_cast(yk_open_first_key()); if (m_yk == NULL) { + m_mutex.unlock(); return false; } @@ -77,14 +82,18 @@ bool YubiKey::init() if (m_ykds == NULL) { yk_close_key(m_yk); m_yk_void = NULL; + m_mutex.unlock(); return false; } + m_mutex.unlock(); return true; } bool YubiKey::deinit() { + m_mutex.lock(); + if (m_yk) { yk_close_key(m_yk); m_yk_void = NULL; @@ -95,6 +104,8 @@ bool YubiKey::deinit() m_ykds_void = NULL; } + m_mutex.unlock(); + return true; } @@ -119,9 +130,13 @@ void YubiKey::detect() emit notFound(); } -bool YubiKey::getSerial(unsigned int& serial) const +bool YubiKey::getSerial(unsigned int& serial) { - if (!yk_get_serial(m_yk, 1, 0, &serial)) { + m_mutex.lock(); + int result = yk_get_serial(m_yk, 1, 0, &serial); + m_mutex.unlock(); + + if (!result) { return false; } diff --git a/src/keys/drivers/YubiKey.h b/src/keys/drivers/YubiKey.h index b938ff86..47341f9a 100644 --- a/src/keys/drivers/YubiKey.h +++ b/src/keys/drivers/YubiKey.h @@ -70,7 +70,7 @@ public: * @param serial serial number * @return true on success */ - bool getSerial(unsigned int& serial) const; + bool getSerial(unsigned int& serial); /** * @brief YubiKey::detect - probe for attached YubiKeys