From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 9DB22A0A02; Wed, 24 Mar 2021 14:05:43 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 655A44067B; Wed, 24 Mar 2021 14:05:43 +0100 (CET) Received: from mail-qv1-f49.google.com (mail-qv1-f49.google.com [209.85.219.49]) by mails.dpdk.org (Postfix) with ESMTP id 5B3554014F; Wed, 24 Mar 2021 14:05:42 +0100 (CET) Received: by mail-qv1-f49.google.com with SMTP id t5so12197676qvs.5; Wed, 24 Mar 2021 06:05:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=3X+PGEDDwjONa2FfkZv7BrBg6K/ENbEi+30IW2a+0KI=; b=vK3WCo2vsKpXBJz+iUJSXPp4sAWSMXEk/mYqVy1JsbUMMUjOVvmIhmdHIV/D+WPFLx 6Dm9zH7h+kfrhM1wFNAqUCfg1XIQWAigy0oxdg6gnsUhchPikW9wrxZvY7zsUUNLCe8R wg/yp9S1IN4Jj6Lo8NW8B7ROS0yWlN25c+WsQdIlgU85k8+hw+G7hVUq1kqSVodXlzEm bT9b9ieE8S1U3RbZQBzzvSIcfNYiQILBSQvTEkCtu0b1oZL9+HUc3av8+qvuasGim7ZG O2ph/L9jWdu9ZohyKsT065TQq3nniTeEozL1+PeUtKXN+4oWSLAj3hNiknpcmh4W6mHb 2r+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=3X+PGEDDwjONa2FfkZv7BrBg6K/ENbEi+30IW2a+0KI=; b=NZeHFMEsEfxpDnlvsn0u23Xl4t23g65w/YjkB3jh+MBHnme+hd3g+C6z309Rudz382 zl7AdStQ9Y+quoYODz/gGR2AJ9BuAsJXwenxIBN21+MuYXSaUJthsytN4HPxw/E4Zosa zmDaAWVPKxG6p4U2x+faFoIEQpj67k/DBq77aT4cY/CCW5czlCEIlJhqjeiix/kG0Ov9 DV0gs4/QyboSH/vFFH1CjILfkNIZfTN0O8IZh+68OsmXSEx3VAkUueFggB9WyoM37i9k 4d0PBFL4y88HccQgL173g04UJls2DAKfxWdolEnPkM3tqcoay5CbTiwCVOAOd3JAyCCG WkJQ== X-Gm-Message-State: AOAM531RTGHDmrIB0KSgJSeiugmmhCE0UgwJT5kHesjdL5Lr2svGBGJT 3ccJDlqPI7e5lOiYP7nJ7Pg= X-Google-Smtp-Source: ABdhPJxioZvMES2R58fJu4S05Ay3JgNS771gmEtvy2FlsyPGFEqRMi13nJJKQrrViHGIw17P6lmKrQ== X-Received: by 2002:ad4:4745:: with SMTP id c5mr2802559qvx.39.1616591141850; Wed, 24 Mar 2021 06:05:41 -0700 (PDT) Received: from localhost.localdomain (bras-base-hullpq2034w-grc-28-74-12-223-140.dsl.bell.ca. [74.12.223.140]) by smtp.gmail.com with ESMTPSA id q2sm1632979qkq.59.2021.03.24.06.05.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Mar 2021 06:05:41 -0700 (PDT) From: Luc Pelletier To: olivier.matz@6wind.com, jianfeng.tan@intel.com Cc: dev@dpdk.org, Luc Pelletier , stable@dpdk.org Date: Wed, 24 Mar 2021 09:04:22 -0400 Message-Id: <20210324130422.92357-1-lucp.at.work@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH] eal: fix possible UB on creation of ctrl thread X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" The creation of control threads uses a pthread barrier for synchronization. This patch fixes a race condition where the pthread barrier could get destroyed while one of the threads has not yet returned from the pthread_barrier_wait function, which could result in undefined behaviour. Fixes: 3a0d465d4c53 ("eal: fix use-after-free on control thread creation") Cc: jianfeng.tan@intel.com Cc: stable@dpdk.org Signed-off-by: Luc Pelletier --- lib/librte_eal/common/eal_common_thread.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/librte_eal/common/eal_common_thread.c b/lib/librte_eal/common/eal_common_thread.c index 73a055902..bd9a9fdbc 100644 --- a/lib/librte_eal/common/eal_common_thread.c +++ b/lib/librte_eal/common/eal_common_thread.c @@ -170,6 +170,7 @@ struct rte_thread_ctrl_params { void *(*start_routine)(void *); void *arg; pthread_barrier_t configured; + bool barrier_in_use; }; static void *ctrl_thread_init(void *arg) @@ -186,9 +187,13 @@ static void *ctrl_thread_init(void *arg) ret = pthread_barrier_wait(¶ms->configured); if (ret == PTHREAD_BARRIER_SERIAL_THREAD) { + while (__atomic_load_n(¶ms->barrier_in_use, + __ATOMIC_ACQUIRE)) + sched_yield(); pthread_barrier_destroy(¶ms->configured); free(params); - } + } else + __atomic_store_n(¶ms->barrier_in_use, 0, __ATOMIC_RELEASE); return start_routine(routine_arg); } @@ -210,6 +215,7 @@ rte_ctrl_thread_create(pthread_t *thread, const char *name, params->start_routine = start_routine; params->arg = arg; + params->barrier_in_use = 1; pthread_barrier_init(¶ms->configured, NULL, 2); @@ -232,18 +238,26 @@ rte_ctrl_thread_create(pthread_t *thread, const char *name, ret = pthread_barrier_wait(¶ms->configured); if (ret == PTHREAD_BARRIER_SERIAL_THREAD) { + while (__atomic_load_n(¶ms->barrier_in_use, + __ATOMIC_ACQUIRE)) + sched_yield(); pthread_barrier_destroy(¶ms->configured); free(params); - } + } else + __atomic_store_n(¶ms->barrier_in_use, 0, __ATOMIC_RELEASE); return 0; fail: if (PTHREAD_BARRIER_SERIAL_THREAD == pthread_barrier_wait(¶ms->configured)) { + while (__atomic_load_n(¶ms->barrier_in_use, + __ATOMIC_ACQUIRE)) + sched_yield(); pthread_barrier_destroy(¶ms->configured); free(params); - } + } else + __atomic_store_n(¶ms->barrier_in_use, 0, __ATOMIC_RELEASE); pthread_cancel(*thread); pthread_join(*thread, NULL); return -ret; -- 2.25.1