Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp2035390ybp; Sat, 5 Oct 2019 04:30:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqxjMkwJEKRQybidu+IvuXaiPS3np8kuD11Vka9xgkBuStfTFSy9PPpr+t8Xom31R2blYGky X-Received: by 2002:a50:9250:: with SMTP id j16mr20286123eda.160.1570275005531; Sat, 05 Oct 2019 04:30:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570275005; cv=none; d=google.com; s=arc-20160816; b=SLvUJViSeFwY3a9RZMRW+NWmgD2RE3Cu/pKvHPnFcoO4hvfY9nQuWhqMSuXfJC3VnI gxCv5W0irwk35Jro4Y1UJCJVK9wXyWdmRCbn+nrJRhRqEprOrqIiLTWF4o6Kj4qYb0CC XuW0MHVBw/wTPmNGfkUcXp6T6mK1n2IFxSdZs8xjXgVc2OUoCMFkhLFNca4Zh9vE0kuH OisO42NXnked7aXO3j55UcOwC4eQPiBpmLZIFe8oKkMQRia35DvXVlFL9f+yF4b1i448 drYQlcZ77UUHxf2KGb2VHfJ/eAYDFl3YtpllHupORI7Uc2AKodgCT9pxH8apUSbwZYIw Fw+Q== 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=/Jnp2tXj0y7lE83JDvs4WR/7ngMEFIzfUViYh5KJU5c=; b=teBFSnY5eov+fmlk9RF6iHKeIj90GitpMHK1K3z3zV5hYBLs1TbTW2uEaFB8Aog4Fr u3Chjn7Ej8LO8C3y+8D9E2IYeBdOJzHmcP4qz1yfzivRvtTHIv6Z660zNN48vZoRTpLA sqD5uAgjHJxkWnUQJKiEJqoJ0GoOODsTTntn6pkY4cHsCiVEknlaJhnIKX1Qk0ns3SQI uB8SWSMllATpMVTcgnOxpXSJVyKkzATvDGnkrKHw2GKjYFLldMLU33b/AhLFpXxLlm2b tzki0O+e4E8y2noK3/4QAwCrR9o/GKY2MZheeD9oBmSWUCja6kkg5e8f7dftbjU6nudh EFmA== 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 v2si4472836eju.3.2019.10.05.04.29.41; Sat, 05 Oct 2019 04:30:05 -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 S1727941AbfJEL2N (ORCPT + 99 others); Sat, 5 Oct 2019 07:28:13 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:40128 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726198AbfJEL2N (ORCPT ); Sat, 5 Oct 2019 07:28:13 -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 1iGiEL-0007UH-Tx; Sat, 05 Oct 2019 11:28:10 +0000 From: Christian Brauner To: syzbot+c5d03165a1bd1dead0c1@syzkaller.appspotmail.com Cc: bsingharora@gmail.com, elver@google.com, linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com, Christian Brauner Subject: [PATCH] taskstats: fix data-race Date: Sat, 5 Oct 2019 13:28:06 +0200 Message-Id: <20191005112806.13960-1-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <0000000000009b403005942237bf@google.com> References: <0000000000009b403005942237bf@google.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 taskstats_exit() there's a race around writing and reading sig->stats. cpu0: task calls exit() do_exit() -> taskstats_exit() -> taskstats_tgid_alloc() The task takes sighand lock and assigns new stats to sig->stats. cpu1: task catches signal do_exit() -> taskstats_tgid_alloc() -> taskstats_exit() The tasks reads sig->stats __without__ holding sighand lock seeing garbage. Fix this by taking sighand lock when reading sig->stats. Reported-by: syzbot+c5d03165a1bd1dead0c1@syzkaller.appspotmail.com Signed-off-by: Christian Brauner --- kernel/taskstats.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/kernel/taskstats.c b/kernel/taskstats.c index 13a0f2e6ebc2..58b145234c4a 100644 --- a/kernel/taskstats.c +++ b/kernel/taskstats.c @@ -553,26 +553,32 @@ static int taskstats_user_cmd(struct sk_buff *skb, struct genl_info *info) static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk) { + int empty; + struct taskstats *stats_new, *stats = NULL; struct signal_struct *sig = tsk->signal; - struct taskstats *stats; - - if (sig->stats || thread_group_empty(tsk)) - goto ret; /* No problem if kmem_cache_zalloc() fails */ - stats = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL); + stats_new = kmem_cache_zalloc(taskstats_cache, GFP_KERNEL); + + empty = thread_group_empty(tsk); spin_lock_irq(&tsk->sighand->siglock); + if (sig->stats || empty) { + stats = sig->stats; + spin_unlock_irq(&tsk->sighand->siglock); + goto free_cache; + } + if (!sig->stats) { - sig->stats = stats; - stats = NULL; + sig->stats = stats_new; + spin_unlock_irq(&tsk->sighand->siglock); + return stats_new; } spin_unlock_irq(&tsk->sighand->siglock); - if (stats) - kmem_cache_free(taskstats_cache, stats); -ret: - return sig->stats; +free_cache: + kmem_cache_free(taskstats_cache, stats_new); + return stats; } /* Send pid data out on exit */ -- 2.23.0