To avoid iterating through the API Session list Anchor->SessListBeg every time we need a session reference, use the session's own pointer as handle.
This change MAY introduce segfaults if the caller application passes invalid session handlers that cannot be directly dereferenced, but that usually only means that the application is buggy and needs fixing. Also removes tests with invalid session handlers from obj_mgmt.c Signed-off-by: Klaus Heinrich Kiwi <[email protected]> --- testcases/driver/obj_mgmt.c | 6 +++++- usr/include/pkcs11/apictl.h | 3 ++- usr/lib/pkcs11/api/api_interface.c | 1 + usr/lib/pkcs11/api/apiutil.c | 27 ++++++++++----------------- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/testcases/driver/obj_mgmt.c b/testcases/driver/obj_mgmt.c index 92b227c..285017f 100755 --- a/testcases/driver/obj_mgmt.c +++ b/testcases/driver/obj_mgmt.c @@ -342,9 +342,13 @@ int do_CopyObject( void ) return FALSE; } + + // try to extract CKA_APPLICATION from the copy. this should fail since all sessions // are now closed. // + /* Commenting-out this test as opencryptoki now segfaults when referencing invalid + * session handles verify_attribs[0].ulValueLen = sizeof(buf1); rc = funcs->C_GetAttributeValue( h_session, h_copy, verify_attribs, 1 ); if (rc != CKR_SESSION_HANDLE_INVALID) { @@ -352,7 +356,7 @@ int do_CopyObject( void ) printf(" Expected CKR_SESSION_HANDLE_INVALID\n"); return FALSE; } - + */ printf("Looks okay...\n"); return TRUE; diff --git a/usr/include/pkcs11/apictl.h b/usr/include/pkcs11/apictl.h index 1e5ab1d..449e021 100755 --- a/usr/include/pkcs11/apictl.h +++ b/usr/include/pkcs11/apictl.h @@ -305,11 +305,12 @@ #define _APILOCAL_H -typedef struct { +typedef struct _Session_struct { void *Previous; void *Next; CK_SLOT_ID SltId; // Slot ID for indexing into the function list pointer CK_SESSION_HANDLE RealHandle; // Handle returned by the STDLL + struct _Session_struct *Handle; // our own handle, cheap check for valid session } Session_Struct_t; #ifdef PK64 diff --git a/usr/lib/pkcs11/api/api_interface.c b/usr/lib/pkcs11/api/api_interface.c index 91dc658..d387cf9 100755 --- a/usr/lib/pkcs11/api/api_interface.c +++ b/usr/lib/pkcs11/api/api_interface.c @@ -3787,6 +3787,7 @@ C_OpenSession ( CK_SLOT_ID slotID, // uniqueness. + apiSessp->Handle = apiSessp; apiSessp->SltId = slotID; // Add to the linked list AddToSessionList(apiSessp); diff --git a/usr/lib/pkcs11/api/apiutil.c b/usr/lib/pkcs11/api/apiutil.c index ab8a3f1..5e7166e 100755 --- a/usr/lib/pkcs11/api/apiutil.c +++ b/usr/lib/pkcs11/api/apiutil.c @@ -352,8 +352,6 @@ set_perm(int file) - - #ifdef DEBUG #define LOGIT logit #else @@ -564,24 +562,19 @@ Valid_Session(pSession,rSession) int rv=FALSE; // Assume that it is not on the list Session_Struct_t *cSessionp; - if ( !pSession ) - return FALSE; - - // Walk the Anchor block session linked list - // return TRUE if the pointer is on the list - // False if it is not + return rv; + pthread_mutex_lock(&(Anchor->SessListMutex)); - cSessionp = Anchor->SessListBeg; - while (cSessionp) { - if (cSessionp == pSession){ - rv = TRUE; - rSession->sessionh = pSession->RealHandle; - rSession->slotID = pSession->SltId; - break; - } - cSessionp = (Session_Struct_t *)cSessionp->Next; + // Try to dereference pSession and double-check by + // comparing the handle value. MAY segfault if the + // session handle pSession is invalid, but that + // often means that the caller is buggy. + if ( pSession->Handle == pSession ) { + rSession->sessionh = pSession->RealHandle; + rSession->slotID = pSession->SltId; + rv = TRUE; } pthread_mutex_unlock(&(Anchor->SessListMutex)); -- 1.6.6.1 ------------------------------------------------------------------------------ ThinkGeek and WIRED's GeekDad team up for the Ultimate GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the lucky parental unit. See the prize list and enter to win: http://p.sf.net/sfu/thinkgeek-promo _______________________________________________ Opencryptoki-tech mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/opencryptoki-tech
