Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp3644272ybp; Sun, 6 Oct 2019 16:53:15 -0700 (PDT) X-Google-Smtp-Source: APXvYqyeCFP3qwWOAoosDn+13aRXk1Yfen2LL/hJqiK2Wvh2cCaNUZJs7udTHj267NgXeTKDWsbn X-Received: by 2002:a17:906:1d03:: with SMTP id n3mr21620599ejh.287.1570405995392; Sun, 06 Oct 2019 16:53:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570405995; cv=none; d=google.com; s=arc-20160816; b=Si48gj5jl97NGEdEeBth8ITyJDLJ+4g3tC1nU5+7DD3VS5c+yNGDzSLUd8OH2NrjJk +qExm0V7e7zFl6fp22QqwI6kB3YbQ1dEt0/xhDRJmO5XGVZ/hkEI+H6y2/S+t/iDUy1v KfxgZng4yDQcoq6GzDuU9znzzxcc9Q6FHQctvS2DUsgMqL8H3y3jjxTSwwRpw7P89ji1 VoikMmmtdA3dZ/5AwPd5RxeM6S+L2IP3b3Al6YK+dIZ2WTsFSdtoOPxPehYvYMUEWr/k Auc235O/JX6KZy0l3lq6bHExq+qD03qyEEed/CgYSvIxolF4QvXkJfPY1Vn5CxD3XqYI 4+KA== 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=DnN4PoU4BredYEAPgAWKgOYyYcZ35q+pbsIwt+ab+nM=; b=XPb7UdvRfdE4QCPrJsdKnFlicUI0+VqGMspRwsR9UJmFQXt4//ftdtu/kBQ8rvSsmk s0sA9QUA1W2BboZAJwDyuzwt5eRf9n2dwFRDg0wXIKA2uL9IV4iX+MbuTp8qRwUYNcml fH5OXNqXBzhr/IhREb3cZbz+98lWlBey8T+2VGNihABCMk20/yF11GLYwiiDG4cj4fKH 4HhvvK9pQggKeKs63n/oi931ufzSRBifIal3De6530np5tHBqrgG1gHl95IijEem/fiM 5aSYmjSX5RUzZtTvAyih4NPSu2C1jO/JW8uzJn7Q65bI1PvPdp7jFWe7ZKQ0YArCNMFI XLIA== 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 r17si7191131edp.400.2019.10.06.16.52.48; Sun, 06 Oct 2019 16:53:15 -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 S1726605AbfJFXw2 (ORCPT + 99 others); Sun, 6 Oct 2019 19:52:28 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:60462 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726000AbfJFXw2 (ORCPT ); Sun, 6 Oct 2019 19:52:28 -0400 Received: from [213.220.153.21] (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 1iHGK9-0002Y6-HK; Sun, 06 Oct 2019 23:52:25 +0000 From: Christian Brauner To: christian.brauner@ubuntu.com Cc: bsingharora@gmail.com, elver@google.com, linux-kernel@vger.kernel.org, syzbot+c5d03165a1bd1dead0c1@syzkaller.appspotmail.com, syzkaller-bugs@googlegroups.com, Dmitry Vyukov Subject: [PATCH] taskstats: fix data-race Date: Mon, 7 Oct 2019 01:52:16 +0200 Message-Id: <20191006235216.7483-1-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191005112806.13960-1-christian.brauner@ubuntu.com> References: <20191005112806.13960-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 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 Cc: Dmitry Vyukov Signed-off-by: Christian Brauner --- /* 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 --- kernel/taskstats.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/kernel/taskstats.c b/kernel/taskstats.c index 13a0f2e6ebc2..8ee046e8a792 100644 --- a/kernel/taskstats.c +++ b/kernel/taskstats.c @@ -554,24 +554,25 @@ 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; + 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; + 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