Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp4143755ybp; Mon, 7 Oct 2019 04:02:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqx799B/mBm41yZJeyLXIM8ch7Per/OO+DpaG7AN8e/x0Rwfh6dQa92OihpvbdZCTLiJxq9p X-Received: by 2002:aa7:dad9:: with SMTP id x25mr28518939eds.7.1570446145371; Mon, 07 Oct 2019 04:02:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570446145; cv=none; d=google.com; s=arc-20160816; b=SHmqP2ZzKT4w5NTIuGjMPmeCvhYyaDt4t1Gp2QVgNkYW7Vctkt37xakmjuQeI+ruq+ 8JFw6Iu4Z6QPUgQpfphjbQvK10BVILhH0t5a0tdI0fzegVgr93fYnh+G9f0j2kYy54j7 P3ntO5Qc/OYACNNXYCuVDHQmWV1rkAFKzPLRJbodyK6zStRcZ26+XPv3XKTF0JkEaQZT 0s1BRj9qeZ4eS6Gnm06P0KabTbmF8Wy1eMPn6pemrLgw09UUwdx8lJ3koWNugOvFxOuF 0Ji926X1pcZuN+J9e3IiyTir0EK7JbLBnKQ/5WV2FgP2rYEAoIDrGHOonzDD6j0Zi0/d eevQ== 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=ugEmW3dnUJPKTuArD31VQ5eA+5hWdsksBBkEEECg8WI=; b=ZpxOUwOSDNTYCF2y7LcCzEui024aDVHdFxdynW/e8erV1an0UkfoBFkPiFxNeOaEer klJShdvjaDxggJrz89sRw89WAm3Kv0ZJOr/Ky7bbVT3RF+/y8izGQE7uFLeNKTntpHQ2 VnHJIxMa2Fw6vp8a1INozFUQZRpQbFzSFCSrF3RkNdqpT4oIneSwBVCgkXWJkAfbXp9Z xIg17xyjQJdxNHH7FhokBa9812bhfhIHAg6mwJS37q8HuqqDvIMAAxyorlXJPz6Ugt8i cBVtGnC+huJKDMiWDqXPjpbyEzVYMRWmxSreC4WrlgrNuXCaSkYhaABW0AK/2p9Md9C1 gZzA== 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 b42si8296090edb.11.2019.10.07.04.02.01; Mon, 07 Oct 2019 04:02:25 -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 S1727604AbfJGLB0 (ORCPT + 99 others); Mon, 7 Oct 2019 07:01:26 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:44598 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727317AbfJGLB0 (ORCPT ); Mon, 7 Oct 2019 07:01:26 -0400 Received: from [185.66.195.251] (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 1iHQlX-0004pK-8t; Mon, 07 Oct 2019 11:01:23 +0000 From: Christian Brauner To: parri.andrea@gmail.com Cc: bsingharora@gmail.com, christian.brauner@ubuntu.com, dvyukov@google.com, elver@google.com, linux-kernel@vger.kernel.org, syzbot+c5d03165a1bd1dead0c1@syzkaller.appspotmail.com, syzkaller-bugs@googlegroups.com, stable@vger.kernel.org Subject: [PATCH v2] taskstats: fix data-race Date: Mon, 7 Oct 2019 13:01:17 +0200 Message-Id: <20191007110117.1096-1-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191007104039.GA16085@andrea.guest.corp.microsoft.com> References: <20191007104039.GA16085@andrea.guest.corp.microsoft.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 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 READ_ONCE() 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 Link: https://lore.kernel.org/r/20191006235216.7483-1-christian.brauner@ubuntu.com --- /* v1 */ Link: https://lore.kernel.org/r/20191005112806.13960-1-christian.brauner@ubuntu.com /* v2 */ - Dmitry Vyukov , Marco Elver : - fix the original double-checked locking using memory barriers /* v3 */ - Andrea Parri : - document memory barriers to make checkpatch happy --- kernel/taskstats.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/kernel/taskstats.c b/kernel/taskstats.c index 13a0f2e6ebc2..978d7931fb65 100644 --- a/kernel/taskstats.c +++ b/kernel/taskstats.c @@ -554,24 +554,27 @@ 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 = READ_ONCE(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 READ_ONCE() above. */ + 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