Fix race condition in tls half open ctx get/put 34/14534/3
authorPing Yu <ping.yu@intel.com>
Tue, 28 Aug 2018 22:56:27 +0000 (18:56 -0400)
committerFlorin Coras <florin.coras@gmail.com>
Wed, 29 Aug 2018 17:34:14 +0000 (17:34 +0000)
Change-Id: I603094215162bfe7d41bbff1b9fe8ab974aa3fab
Signed-off-by: Ping Yu <ping.yu@intel.com>
src/vnet/tls/tls.c

index b576c00..4fb0dfb 100644 (file)
@@ -94,16 +94,17 @@ tls_ctx_half_open_alloc (void)
     {
       clib_rwlock_writer_lock (&tm->half_open_rwlock);
       pool_get (tm->half_open_ctx_pool, ctx);
-      memset (ctx, 0, sizeof (*ctx));
-      ctx_index = ctx - tm->half_open_ctx_pool;
       clib_rwlock_writer_unlock (&tm->half_open_rwlock);
     }
   else
     {
+      /* reader lock assumption: only main thread will call pool_get */
+      clib_rwlock_reader_lock (&tm->half_open_rwlock);
       pool_get (tm->half_open_ctx_pool, ctx);
-      memset (ctx, 0, sizeof (*ctx));
-      ctx_index = ctx - tm->half_open_ctx_pool;
+      clib_rwlock_reader_unlock (&tm->half_open_rwlock);
     }
+  memset (ctx, 0, sizeof (*ctx));
+  ctx_index = ctx - tm->half_open_ctx_pool;
   return ctx_index;
 }