Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp816340ybp; Wed, 9 Oct 2019 04:48:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqzyBeul6QxBHoQ+XSPL+s/Ze5Ase/xicezzFr/kPbeK92jHg/l8vlHeNQwwu81ybKYunxk+ X-Received: by 2002:a50:f701:: with SMTP id g1mr2518902edn.62.1570621734427; Wed, 09 Oct 2019 04:48:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570621734; cv=none; d=google.com; s=arc-20160816; b=oTLT1f9TGpUktYnivABkutXyX8ALmKCWsHzerppX1HkcCy3jXOA9jw/tL0llVAG7we yN6dPTsJMPub1i449jkccE6DqhEUDypJLYE1qCmGL2txV4+PsUa/lt7GjsHn2ZY2UZHH F96F0UwjOHDdVA6hoY/gxev3/L05SRmomPqccY/c/h/UlJ3+twb6ADSilBPsCnTcbdCm iy/wrFy7ogSWD3w+Trcsi0q7UZ6corodKoiXuM5dw27vQQy/b3hIGHYB+JipMaSHcN67 czO9z0FbMDmV50Tna3fkmsK1lrNPWp+AvyEcdKwstJjhxfKlMPgBM9TgxfmcoIufGQuX IZZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=s0v5X0YB5mzNCCzfapvzbu47v2JdFuCbE/V7tEVoYJI=; b=Ypw+44AMYBSDOfaZp5Oyg4kUnm3gQPtmtd9+lRbMiqUz949j60b5LuUDDiMrg7ssK9 +HUfVrlyBdgW8kaSsitirs8zUylappOSggR0lIo8eTfKS0qVGKGli3obX7/cIPEgWhu0 9tTvpw00AKy1Pmu2Wb0LkgCSg44ehtuuMWzTCONgDhhXadZYWVwzgETSK0QSZtgNPwMd /SwI0Qn3L1R9zY908cSnuZMI4P61pNaUE/AvFY4A8NGttJI9VKeRNCnvwwn80mTJ51Ee 0O+NMOD0yFx9C3dLk1av74dmWnUbrWwLhoqZQncpy2ryOJP6YwFAdWoB53ppCAm6kYbJ af6g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y23si1233454edb.208.2019.10.09.04.48.30; Wed, 09 Oct 2019 04:48:54 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730490AbfJILsU (ORCPT + 99 others); Wed, 9 Oct 2019 07:48:20 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:58852 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725914AbfJILsU (ORCPT ); Wed, 9 Oct 2019 07:48:20 -0400 Received: from [79.140.115.128] (helo=localhost.localdomain) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1iIAS0-0000pL-Aq; Wed, 09 Oct 2019 11:48:16 +0000 From: Christian Brauner To: christian.brauner@ubuntu.com Cc: bsingharora@gmail.com, dvyukov@google.com, elver@google.com, linux-kernel@vger.kernel.org, parri.andrea@gmail.com, stable@vger.kernel.org, syzbot+c5d03165a1bd1dead0c1@syzkaller.appspotmail.com, syzkaller-bugs@googlegroups.com Subject: [PATCH v5] taskstats: fix data-race Date: Wed, 9 Oct 2019 13:48:09 +0200 Message-Id: <20191009114809.8643-1-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191009113134.5171-1-christian.brauner@ubuntu.com> References: <20191009113134.5171-1-christian.brauner@ubuntu.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When assiging and testing taskstats in taskstats_exit() there's a race when writing and reading sig->stats when a thread-group with more than one thread exits: cpu0: thread catches fatal signal and whole thread-group gets taken down do_exit() do_group_exit() taskstats_exit() taskstats_tgid_alloc() The tasks reads sig->stats without holding sighand lock seeing garbage. cpu1: task calls exit_group() do_exit() do_group_exit() taskstats_exit() taskstats_tgid_alloc() The task takes sighand lock and assigns new stats to sig->stats. Fix this by using smp_load_acquire() and smp_store_release(). Reported-by: syzbot+c5d03165a1bd1dead0c1@syzkaller.appspotmail.com Fixes: 34ec12349c8a ("taskstats: cleanup ->signal->stats allocation") Cc: stable@vger.kernel.org Signed-off-by: Christian Brauner Reviewed-by: Dmitry Vyukov --- /* v1 */ Link: https://lore.kernel.org/r/20191005112806.13960-1-christian.brauner@ubuntu.com /* v2 */ Link: https://lore.kernel.org/r/20191006235216.7483-1-christian.brauner@ubuntu.com - Dmitry Vyukov , Marco Elver : - fix the original double-checked locking using memory barriers /* v3 */ Link: https://lore.kernel.org/r/20191007110117.1096-1-christian.brauner@ubuntu.com - Andrea Parri : - document memory barriers to make checkpatch happy /* v4 */ Link: https://lore.kernel.org/r/20191009113134.5171-1-christian.brauner@ubuntu.com - Andrea Parri : - use smp_load_acquire(), not READ_ONCE() - update commit message /* v5 */ - Andrea Parri : - fix typo in smp_load_acquire() --- kernel/taskstats.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/kernel/taskstats.c b/kernel/taskstats.c index 13a0f2e6ebc2..6e18fdc4f7c8 100644 --- a/kernel/taskstats.c +++ b/kernel/taskstats.c @@ -554,24 +554,30 @@ static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info) static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk) { struct signal_struct *sig = tsk->signal; - struct taskstats *stats; + struct taskstats *stats_new, *stats; - if (sig->stats || thread_group_empty(tsk)) - goto ret; + /* Pairs with smp_store_release() below. */ + stats = smp_load_acquire(&sig->stats); + if (stats || thread_group_empty(tsk)) + return stats; /* No problem if kmem_cache_zalloc() fails */ - stats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL); + stats_new = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL); spin_lock_irq(&tsk->sighand->siglock); if (!sig->stats) { - sig->stats = stats; - stats = NULL; + /* + * Pairs with smp_store_release() above and order the + * kmem_cache_zalloc(). + */ + smp_store_release(&sig->stats, stats_new); + stats_new = NULL; } spin_unlock_irq(&tsk->sighand->siglock); - if (stats) - kmem_cache_free(taskstats_cache, stats); -ret: + if (stats_new) + kmem_cache_free(taskstats_cache, stats_new); + return sig->stats; } -- 2.23.0