Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp1081206ybi; Fri, 2 Aug 2019 09:02:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqzYlV3htJYWrAhg8e1ngA5JRtu4Ind/8za9jPOUKIJsZhfQdKB/P0Vj46N4QYYard2MquhE X-Received: by 2002:a62:2a4d:: with SMTP id q74mr60484434pfq.86.1564761776757; Fri, 02 Aug 2019 09:02:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564761776; cv=none; d=google.com; s=arc-20160816; b=c/g4hKpGo2MvG4CAVd9RW7fW7sK6MmmFA5RkCfJQ8CdUUf3vunDr1vOTXFuESVWuX6 ad9Xp/JtPvYN4qkdAiehJ3GFNVXHkQ8PX5PpNJG8H+AoCkQwSMte15Z+xWjwGzHJWqB7 pJvI2Bg1PcgheNC4FytXE15W2biPrcCztdHVyWRQztoxTT+xgxI4aD3cvWZgSycnE+xA ftnMoQGrFx/TF4ogylcLoUkPCMBjd5RRsn9YpeTawaLoNqZbsbBBfQaM+V9Zvwb0tM3A Po7cIjtGAew57yOokCu+/Lz4HTtD6IKo/Q3qh1SfkJj7Gb7Cb6Q/q8K+52k9ovVLJUx4 dtDg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=nVrYnJ3bRVG2opwxtqvNB/X9ijj3655IRjpz+zwj6Gk=; b=P/dEL4rGjQqx/bVVhgr9rWX556/IqDuPHaGkara5Pyi8BHN9d2MNxLXHn/lGkSE/db jmAFowmlEj+h0pLJBpW8PnEwvLFJuNqPaFbmRvg4qpojMKlrUdNQdl1MFHO2rM0Egt4/ g+5cuS5ikzjJF8J6RkqXsDQGDYGiR7QqShYKgeGXPQ2Hf7pQvxOyeJaxO3qMZNVFy9Ap ClbgeybjPLAV0koT44+iQ7SIlN5ywvIEFvU/4RCC0+esT6AFDSXEPJh/SRMPKsLt814f f580KK8W2/6G6g5oMhcb9mH+vA6A0Ka2jDYIYH10O3S8SqiCrtm7smqHFFzDSRyPx7ls MR2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=bqcLDAhB; 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 bh8si34523133plb.175.2019.08.02.09.02.40; Fri, 02 Aug 2019 09:02:56 -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=@kernel.org header.s=default header.b=bqcLDAhB; 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 S2404838AbfHBJgQ (ORCPT + 99 others); Fri, 2 Aug 2019 05:36:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:36966 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404824AbfHBJgL (ORCPT ); Fri, 2 Aug 2019 05:36:11 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 02BCB217F5; Fri, 2 Aug 2019 09:36:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564738570; bh=+NZu1zTblNtFFEbvJ0ETvJhrVxJvu44Ilkp3ZcuF8Dc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bqcLDAhBShXZL4R8ONt71qE6yCLecaHQVlvwVDSjaXk8SY2dimuu6n+3jGI1tn1GP q9azzd+OMIeViiBGlFkQpxLTSafZmBZefIGvbMn0fzYlVMIwnN9PX6B9pa/HNe7dzU u6B8Wx5LOSg/RbKOtMpnjcxKKNGD9HVEamkbtVIg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jann Horn , "Peter Zijlstra (Intel)" , Linus Torvalds , Petr Mladek , Sergey Senozhatsky , Thomas Gleixner , Will Deacon , Ingo Molnar Subject: [PATCH 4.4 156/158] sched/fair: Dont free p->numa_faults with concurrent readers Date: Fri, 2 Aug 2019 11:29:37 +0200 Message-Id: <20190802092233.548358426@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190802092203.671944552@linuxfoundation.org> References: <20190802092203.671944552@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jann Horn commit 16d51a590a8ce3befb1308e0e7ab77f3b661af33 upstream. When going through execve(), zero out the NUMA fault statistics instead of freeing them. During execve, the task is reachable through procfs and the scheduler. A concurrent /proc/*/sched reader can read data from a freed ->numa_faults allocation (confirmed by KASAN) and write it back to userspace. I believe that it would also be possible for a use-after-free read to occur through a race between a NUMA fault and execve(): task_numa_fault() can lead to task_numa_compare(), which invokes task_weight() on the currently running task of a different CPU. Another way to fix this would be to make ->numa_faults RCU-managed or add extra locking, but it seems easier to wipe the NUMA fault statistics on execve. Signed-off-by: Jann Horn Signed-off-by: Peter Zijlstra (Intel) Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Petr Mladek Cc: Sergey Senozhatsky Cc: Thomas Gleixner Cc: Will Deacon Fixes: 82727018b0d3 ("sched/numa: Call task_numa_free() from do_execve()") Link: https://lkml.kernel.org/r/20190716152047.14424-1-jannh@google.com Signed-off-by: Ingo Molnar Signed-off-by: Greg Kroah-Hartman --- fs/exec.c | 2 +- include/linux/sched.h | 4 ++-- kernel/fork.c | 2 +- kernel/sched/fair.c | 24 ++++++++++++++++++++---- 4 files changed, 24 insertions(+), 8 deletions(-) --- a/fs/exec.c +++ b/fs/exec.c @@ -1642,7 +1642,7 @@ static int do_execveat_common(int fd, st current->fs->in_exec = 0; current->in_execve = 0; acct_update_integrals(current); - task_numa_free(current); + task_numa_free(current, false); free_bprm(bprm); kfree(pathbuf); putname(filename); --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1860,7 +1860,7 @@ extern int arch_task_struct_size __read_ extern void task_numa_fault(int last_node, int node, int pages, int flags); extern pid_t task_numa_group_id(struct task_struct *p); extern void set_numabalancing_state(bool enabled); -extern void task_numa_free(struct task_struct *p); +extern void task_numa_free(struct task_struct *p, bool final); extern bool should_numa_migrate_memory(struct task_struct *p, struct page *page, int src_nid, int dst_cpu); #else @@ -1875,7 +1875,7 @@ static inline pid_t task_numa_group_id(s static inline void set_numabalancing_state(bool enabled) { } -static inline void task_numa_free(struct task_struct *p) +static inline void task_numa_free(struct task_struct *p, bool final) { } static inline bool should_numa_migrate_memory(struct task_struct *p, --- a/kernel/fork.c +++ b/kernel/fork.c @@ -254,7 +254,7 @@ void __put_task_struct(struct task_struc WARN_ON(tsk == current); cgroup_free(tsk); - task_numa_free(tsk); + task_numa_free(tsk, true); security_task_free(tsk); exit_creds(tsk); delayacct_tsk_free(tsk); --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -2054,13 +2054,23 @@ no_join: return; } -void task_numa_free(struct task_struct *p) +/* + * Get rid of NUMA staticstics associated with a task (either current or dead). + * If @final is set, the task is dead and has reached refcount zero, so we can + * safely free all relevant data structures. Otherwise, there might be + * concurrent reads from places like load balancing and procfs, and we should + * reset the data back to default state without freeing ->numa_faults. + */ +void task_numa_free(struct task_struct *p, bool final) { struct numa_group *grp = p->numa_group; - void *numa_faults = p->numa_faults; + unsigned long *numa_faults = p->numa_faults; unsigned long flags; int i; + if (!numa_faults) + return; + if (grp) { spin_lock_irqsave(&grp->lock, flags); for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) @@ -2073,8 +2083,14 @@ void task_numa_free(struct task_struct * put_numa_group(grp); } - p->numa_faults = NULL; - kfree(numa_faults); + if (final) { + p->numa_faults = NULL; + kfree(numa_faults); + } else { + p->total_numa_faults = 0; + for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) + numa_faults[i] = 0; + } } /*