Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp4126371ybp; Mon, 7 Oct 2019 03:41:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqzudPZyl2kB4UJMrZ7Gkb93dA7k7tElNpWY3B75IQ1OYHibiXt79RpiRsl6DKVOhWcACwrr X-Received: by 2002:a50:fc0c:: with SMTP id i12mr28438654edr.82.1570444895094; Mon, 07 Oct 2019 03:41:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570444895; cv=none; d=google.com; s=arc-20160816; b=HGlk9GzACetd2HwB0BDw6dGDcrBZY3C5bRPS3XKtpQkB89FfHKoIH2bP163k/Sbx8m NoGL8BlfzKnoOQrlc3Nc70c0S8GwDkTIv0seryS9kTUV9Fv6RuBsgG4t32ghu0GCWUeZ 9FqS6q8PTPEv7icMtHy7FuT3y5hdwg/5bxxHmWa6hf5ThRN30FKHb2c6xy6zqliqaWI1 pa+ZtbcNvNltfo06jxCAUOpVEO2UJCQZDRGFge9o1dHzulSvEHI3dKfG+V4Ul+qnPgIu 8nFiTh9dxhry+9Wxg9DG0CkSQZUn8nmwvfsC8O7FLzWn5LZOxqggnR9Dr8qmVKd7/Xl0 tyQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=Hlqn0Qia24Xv7B5txaeOBUpq8YG1iSw7nB1lyh2UV8E=; b=Y+/ev46Sn2h7T+LJewWbZRWvtRfoFFugU0d9vi3aUD0awg82gJujah/7qCJkpRfUdd PxHJ/gcmAh+atrXL7UHuQLxznmEr8BQ3yodsdA3qL3+mKmQa3jZv8f7Vm0ZxbGTxi3sy aAnYt0jrhq1mfZFFemdpiHk0Pfie+W4wVXSBGWcox1oBY0GdeW8f9FhCjWsX623c+Tbl RkZ9U4/TiMD74lTiiwp0foa7dWM1FPgKpgIP8NN0t/YGGr1v4MjKVRjLI19h2QEzA4jM +ULdqxcEMHnAwdJcSaseFkecPB5DsLHx2A/3rRz9rEr+wc8wJ226kwWjN1s9lOX7Svhe Fkow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=HRzjKtXa; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k16si6797929ejc.134.2019.10.07.03.41.09; Mon, 07 Oct 2019 03:41:35 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=HRzjKtXa; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727416AbfJGKkt (ORCPT + 99 others); Mon, 7 Oct 2019 06:40:49 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:40947 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727252AbfJGKkt (ORCPT ); Mon, 7 Oct 2019 06:40:49 -0400 Received: by mail-wm1-f66.google.com with SMTP id b24so11740949wmj.5 for ; Mon, 07 Oct 2019 03:40:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=Hlqn0Qia24Xv7B5txaeOBUpq8YG1iSw7nB1lyh2UV8E=; b=HRzjKtXaBi5os6pzxPm2NShM16nO0rw+HM7y5Lr8co/MeeiYnY2hXh+W298mSPHHN2 m4GbY0sI94ScQKub7pGOjqCmr3nQ+tijk3Zu/3QUtPLZSGC6M0IZ/IqlxZ12ND5ljlGH N63aPHpuO5ewSyFuen1nEEfGQfNjinFJNx4TylA7LDbLZGTfggIG8RK8ayVnU5xXp5Lp MVMrRgbTjghitzYOTXUvL1khVP0m1Lf7k0ItL6FLoaotbTiJX233hclt/UouVyfOnjf+ jjqVzZ2qK37prV2i5KgPVA9NdlL++8RKgzuDHf3G2UgWUAdOoztpRgtuFCzG0BNTYd5I lAaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=Hlqn0Qia24Xv7B5txaeOBUpq8YG1iSw7nB1lyh2UV8E=; b=fnvamQq8Ks+BDaTH8t3gCWsZffCjSSW35tUOmUrnAUVfy+DPaOQw8SG2QYmsSMnKTx CZ+RrXgFqBi+1MER2M+V8bPmwkMGcXocHaby6T572xQ+E0y5LXa/fhjHILOSrhe0gOLd GWQTmLdeBswVnpQCu+uhbNtcjIKA2RAMOXBpIeQbcRdeMgNBgdeooyyLJJXPof834CKz y3WnzNbr7dIN6FwffzoKsmWt/rb8bQq94w7AxjRAYbcpJJxgWgELZSpRlG2usWsDzMuT qU0wu9VFgJWv/1wowd+Ln1WwjexNliP2FdUPuY53NppFTwoTgog11gAN6fm90rKRLmGb uXxA== X-Gm-Message-State: APjAAAVI6A3Uskr4/C85L4Lf+I2jIC8D6VissD4vnD2s/pCWRhRxW0CM Wd+irvOyNAApemONxBoLTnk= X-Received: by 2002:a1c:23d7:: with SMTP id j206mr19618654wmj.57.1570444847085; Mon, 07 Oct 2019 03:40:47 -0700 (PDT) Received: from andrea.guest.corp.microsoft.com ([2a01:110:8012:1012:69a4:816:a2c:e717]) by smtp.gmail.com with ESMTPSA id z142sm40291608wmc.24.2019.10.07.03.40.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2019 03:40:46 -0700 (PDT) Date: Mon, 7 Oct 2019 12:40:39 +0200 From: Andrea Parri To: Christian Brauner Cc: bsingharora@gmail.com, elver@google.com, linux-kernel@vger.kernel.org, syzbot+c5d03165a1bd1dead0c1@syzkaller.appspotmail.com, syzkaller-bugs@googlegroups.com, Dmitry Vyukov Subject: Re: [PATCH] taskstats: fix data-race Message-ID: <20191007104039.GA16085@andrea.guest.corp.microsoft.com> References: <20191005112806.13960-1-christian.brauner@ubuntu.com> <20191006235216.7483-1-christian.brauner@ubuntu.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191006235216.7483-1-christian.brauner@ubuntu.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Christian, On Mon, Oct 07, 2019 at 01:52:16AM +0200, Christian Brauner wrote: > 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 FYI, checkpatch.pl says: WARNING: memory barrier without comment #62: FILE: kernel/taskstats.c:568: + smp_store_release(&sig->stats, stats_new); Maybe you can make checkpatch.pl happy ;-) and add a comment to stress the 'pairing' between this barrier and the added READ_ONCE() (as Dmitry was alluding to in a previous post)? Thanks, Andrea > --- > /* 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 >