2

I have an embedded application that does client-side ssl transactions that uses OpenSSL v0.9.7e. I've been using a global session pointer that is shared with child threads. There is one thread per COM port that it monitors. It works for the most part. However, every so often (every 500 or so transactions) it crashes inside of SSL_connect(). I did a little poking around and it appears to be deleting the session on its own. Therefore, I suspect that one of the threads is in the middle of a transaction when it does so. I would have thought that it would not delete it if the reference count is > 0.

Does anyone know if it's possible to do this with a globally shared session? Should I be using one session per thread (COM port). Here is the code. I ommitted all the error checks for brevity.

    int send_ssl_post(){
  BIO *bio;
  SSL *ssl;
  int ret = -1;
  int sockfd;
  int sslRet;

  ssl = NULL;

  g_ctx = setup_client_ctx( );
  ssl = SSL_new( g_ctx );
  SSL_set_session( ssl, g_session );
  sockfd = tcpConnect( url, true );
  bio = BIO_new_socket( sockfd, BIO_NOCLOSE );
  SSL_set_bio( ssl, bio, bio ); // SSL_set_bio cannot fail
  sslRet = SSL_connect( ssl );

  // free the session. We may change sessions below
  if ( g_session != NULL ) {
     SSL_SESSION_free( g_session );
  }

  // SSL write
  ret = SSL_write( ssl, data, strlen( data ) );

  ret = SSL_read( ssl, resp, respSize ); // time-out logic should be in

  g_session = SSL_get1_session( ssl );

  if ( ssl != NULL ) {
     SSL_shutdown( ssl );
     SSL_free( ssl );
  }

  if ( sockfd > 0 ){
     close( sockfd ); 
  }

  ERR_remove_state( 0 ); // free the memory that did not freed (buggy in this SSL version)
  return 0;
}

I also have the two thead locking callback functions as such:

static void locking_function( int mode, int n, const char * file, int line ) {
   if ( mode & CRYPTO_LOCK ){
      //logger( DEBUG, "CRYPTO Lock file: %s, line: %d, n: %d", file, line, n );
      pthread_mutex_lock( &ssl_mutex_array[n] );
   } else {
      //logger( DEBUG, "CRYPTO Unlock file: %s, line: %d, n: %d", file, line, n );
      pthread_mutex_unlock( &ssl_mutex_array[n] );
   }
}

static unsigned long id_function( void ) {
   //logger( DEBUG, "CRYPTO Id function");
   return ((unsigned long) pthread_self());
}

int setupSSLThreadLock( void ) {
   int i;

   //logger( DEBUG, "setupSSLThreadLock with %d number of locks", CRYPTO_num_locks() );
   ssl_mutex_array = OPENSSL_malloc( CRYPTO_num_locks( ) * sizeof (pthread_mutex_t) );

   if ( !ssl_mutex_array ){
      return 0;
   }

   for ( i = 0; i < CRYPTO_num_locks( ); i++ ){
      pthread_mutex_init( &ssl_mutex_array[i], NULL );
   }
   CRYPTO_set_id_callback( id_function );
   CRYPTO_set_locking_callback( locking_function );
   return 1;
}

int cleanupSSLThreadLock( void ) {
   int i;

   if ( ssl_mutex_array == NULL ){
      return 0;
   }
   CRYPTO_set_id_callback( NULL );
   CRYPTO_set_locking_callback( NULL );
   for ( i = 0; i < CRYPTO_num_locks( ); i++ ){
      pthread_mutex_destroy( &ssl_mutex_array[i] );
   }
   OPENSSL_free( ssl_mutex_array );
   ssl_mutex_array = NULL;
   return 1;
}

Any help will be appreciated.

1 Answer 1

1

If send_ssl_post() is run by two threads simultaneously, you have a race. Consider these two parts of the function:

  SSL_set_session( ssl, g_session );

And

  // free the session. We may change sessions below
  if ( g_session != NULL ) {
     SSL_SESSION_free( g_session );
  }

There is a race between when one thread calls SSL_SESSION_free() on the global session, and some other thread trying to use it to set the session. If the free happens first, the thread that tries to use it will be accessing freed memory.

You can resolve this race with mutual exclusion. One around the SSL_set_session() call.

acquire_session_lock();
if ( g_session != NULL ) {
    SSL_set_session( ssl, g_session );
}
release_session_lock();

The other around the SSL_SESSION_free() call:

acquire_session_lock();
if ( g_session != NULL ) {
    SSL_SESSION_free( g_session );
    g_session = NULL;
}
release_session_lock();
7
  • The session scheme uses reference counting. I may be incrementing/decrementing (setting/freeing) incorrectly, but, as long as I don't decrement too many times, it should be okay. It looks to my like every so often, OpenSSL deletes the session within SSL_connect(). That's when it crashes. I can't tell why it deletes it yet. However, if it does delete the session on its own when I'm trying to control the reference counting, there's a serious problem. I must be missing something.
    – Jim Lo
    Jun 2, 2013 at 14:26
  • The SSL server can refuse to use the session that the client presents, forcing a full handshake. The SSL_connect deletes the old session when that happens.
    – jxh
    Jun 2, 2013 at 14:35
  • So, what's the bottom line? Is the session free and get calls in the wrong place or is it not possible for threads to share a session this way. I don't think its a good idea to put a mutex lock on the global session pointer. It could cause greater delays than what you gain with session reuse.
    – Jim Lo
    Jun 3, 2013 at 13:18
  • Session reuse removes one RTT and some cryptographic compute time from the SSL handshake. This is at least on the order of milliseconds, unless you are only doing SSL on a very high speed local network (or over loopback on the same machine). Lock contention will be on the order of microseconds on your typical GHz processor.
    – jxh
    Jun 3, 2013 at 15:58
  • Sorry to ask so many followup questions. However, it seems like the problem is that the session is deleted somewhere between SSL_set_session() and SSL_connect(). Just locking it right at the SSL_set_session would not solve the problem, seems to me. If you put a lock around SSL_connect too, and you have connection issues, you block other threads, which is not acceptable for this app.
    – Jim Lo
    Jun 3, 2013 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.