eal: fix use-after-free on control thread creation
authorJianfeng Tan <jianfeng.tan@intel.com>
Wed, 2 May 2018 13:52:14 +0000 (13:52 +0000)
committerThomas Monjalon <thomas@monjalon.net>
Wed, 2 May 2018 15:23:37 +0000 (17:23 +0200)
After below commit, we encounter some strange issue:
  1) Dead lock as described here:
     http://dpdk.org/ml/archives/dev/2018-April/099806.html
  2) SIGSEGV issue when starting a testpmd in VM.

Considering below commit changes to use dynamic memory instead of
stack for memory barrier, we doubt it's caused by use-after-free.

Fixes: 3d09a6e26d8b ("eal: fix threads block on barrier")

Reported-by: Maxime Coquelin <maxime.coquelin@redhat.com>
Reported-by: Lei Yao <lei.a.yao@intel.com>
Suggested-by: Stephen Hemminger <stephen@networkplumber.org>
Suggested-by: Olivier Matz <olivier.matz@6wind.com>
Signed-off-by: Jianfeng Tan <jianfeng.tan@intel.com>
Reviewed-by: Olivier Matz <olivier.matz@6wind.com>
lib/librte_eal/common/eal_common_thread.c

index a2c05e4..4239863 100644 (file)
@@ -149,11 +149,16 @@ struct rte_thread_ctrl_params {
 
 static void *rte_thread_init(void *arg)
 {
+       int ret;
        struct rte_thread_ctrl_params *params = arg;
        void *(*start_routine)(void *) = params->start_routine;
        void *routine_arg = params->arg;
 
-       pthread_barrier_wait(&params->configured);
+       ret = pthread_barrier_wait(&params->configured);
+       if (ret == PTHREAD_BARRIER_SERIAL_THREAD) {
+               pthread_barrier_destroy(&params->configured);
+               free(params);
+       }
 
        return start_routine(routine_arg);
 }
@@ -206,14 +211,21 @@ rte_ctrl_thread_create(pthread_t *thread, const char *name,
        if (ret < 0)
                goto fail;
 
-       pthread_barrier_wait(&params->configured);
-       free(params);
+       ret = pthread_barrier_wait(&params->configured);
+       if (ret == PTHREAD_BARRIER_SERIAL_THREAD) {
+               pthread_barrier_destroy(&params->configured);
+               free(params);
+       }
 
        return 0;
 
 fail:
+       if (PTHREAD_BARRIER_SERIAL_THREAD ==
+           pthread_barrier_wait(&params->configured)) {
+               pthread_barrier_destroy(&params->configured);
+               free(params);
+       }
        pthread_cancel(*thread);
        pthread_join(*thread, NULL);
-       free(params);
        return ret;
 }