Hello,

I found a bug that leads to an assert.
Check the lib is initialized before logging

C_CloseSession() and C_CloseAllSessions() were calling sc_debug() before
any check on the validity of the context. So if C_Initialize() was not
previously called you get an assert:
log.c:76: sc_do_log_va: Assertion `ctx != ((void *)0)' failed.

Patch attached and available at
https://github.com/LudovicRousseau/OpenSC/commit/ce2d3c09bed53e4a9e77e327d39f0a4e2187ba3a

The solution is to call sc_pkcs11_lock() _before_ using sc_debug() so
the context valitity is checked

Martin, please merge my master branch.

-- 
 Dr. Ludovic Rousseau
From ce2d3c09bed53e4a9e77e327d39f0a4e2187ba3a Mon Sep 17 00:00:00 2001
From: Ludovic Rousseau <ludovic.rousseau+git...@gmail.com>
Date: Tue, 21 Jun 2011 10:41:41 +0200
Subject: [PATCH 6/6] Check the lib is initialized before logging

C_CloseSession() and C_CloseAllSessions() were calling sc_debug() before
any check on the validity of the context. So if C_Initialize() was not
previously called you get an assert:
log.c:76: sc_do_log_va: Assertion `ctx != ((void *)0)' failed.
---
 src/pkcs11/pkcs11-session.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/pkcs11/pkcs11-session.c b/src/pkcs11/pkcs11-session.c
index b16962b..89f1289 100644
--- a/src/pkcs11/pkcs11-session.c
+++ b/src/pkcs11/pkcs11-session.c
@@ -137,11 +137,14 @@ CK_RV C_CloseSession(CK_SESSION_HANDLE hSession)
 {				/* the session's handle */
 	CK_RV rv;
 
+	rv = sc_pkcs11_lock();
+	if (rv != CKR_OK)
+		return rv;
+
 	sc_debug(context, SC_LOG_DEBUG_NORMAL, "C_CloseSession(0x%lx)\n", hSession);
 
-	rv = sc_pkcs11_lock();
-	if (rv == CKR_OK)
-		rv = sc_pkcs11_close_session(hSession);
+	rv = sc_pkcs11_close_session(hSession);
+
 	sc_pkcs11_unlock();
 	return rv;
 }
@@ -151,11 +154,12 @@ CK_RV C_CloseAllSessions(CK_SLOT_ID slotID)
 	CK_RV rv;
 	struct sc_pkcs11_slot *slot;
 
-	sc_debug(context, SC_LOG_DEBUG_NORMAL, "C_CloseAllSessions(0x%lx)\n", slotID);
 	rv = sc_pkcs11_lock();
 	if (rv != CKR_OK)
 		return rv;
 
+	sc_debug(context, SC_LOG_DEBUG_NORMAL, "C_CloseAllSessions(0x%lx)\n", slotID);
+
 	rv = slot_get_token(slotID, &slot);
 	if (rv != CKR_OK)
 		goto out;
-- 
1.7.2.5

_______________________________________________
opensc-devel mailing list
opensc-devel@lists.opensc-project.org
http://www.opensc-project.org/mailman/listinfo/opensc-devel

Reply via email to