Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp585554rwb; Thu, 1 Dec 2022 06:06:29 -0800 (PST) X-Google-Smtp-Source: AA0mqf6CfFMQeuidRS0SNRvJ2RTvRLNWS7bTKqjIm3Ofr/W8//gp5K56++RrPvaa72Oebdt8QlQo X-Received: by 2002:a63:e442:0:b0:477:bfab:5a81 with SMTP id i2-20020a63e442000000b00477bfab5a81mr33928506pgk.64.1669903588707; Thu, 01 Dec 2022 06:06:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669903588; cv=none; d=google.com; s=arc-20160816; b=Ii4765ae1t4lB+GBD5Zf5aqA9rROjtwYXov5iCsuis4SZfkP8eZFFUe0JXCh3MIGtp UUHcsb4WpB+rq233GWAYcbm4W2Ys7Arg4qbj3NL/VyiKBkxReoyOlKvSnXkxd4ck33x4 oHE0Aab8VrP9mzmD5CMIquPGaunim6wb0J2cHDOTTtk9fxRDnNFioJl5q6M2iJLIpmTM CCmnlI7yWM4GyCIMgqbvr/bs+huan9toflpjmAThdH4QuVIDKmn9mVsa3+ujufdCbhAz lbtm1JqKN1kIshqXCyPbtSCz9b/eomYn/TQTtva3AMT25VqmxaQX1ZrWVJJUcaXRNAzd nobQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=bvn3VEYxFD8Xh42/3DooZ5UVilk3Dm7McEkWGvLSesI=; b=Ey9n6b6u1y0WDqwq9S4/6kAXSYK2iuNvThcjKFewiC/C3Zv55ccL/n1NKj4kkyU0O2 RYEmXWV22y71SdbZfcFqqhnkORbGJneBbOOHOzwplpTHqnkqA09XCuOFBKMLF1BOzi9v ekMF9i29o1Ukg61RvwuwRokC28O+yU8dMsbnqa66dzOXya3twBoKbrCm8e4Sdvg5Eaq6 C3+PsIlAfzEQWEwyAUJC1IPSa3jlpy1ud6jHBWDhCnnu9aXTM2PxmlZEiyY13HsfHoGH 5uiygl7EGetIHERq+4kGopoNMZIuESjuo27xuYnaO0aiBfWIzkV9DtqiTctx7ZRGQnlN EDoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=owJLq6bF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id on17-20020a17090b1d1100b00218de7c19efsi8874936pjb.108.2022.12.01.06.06.13; Thu, 01 Dec 2022 06:06:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=owJLq6bF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231594AbiLANoz (ORCPT + 82 others); Thu, 1 Dec 2022 08:44:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231261AbiLANoy (ORCPT ); Thu, 1 Dec 2022 08:44:54 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6991D1021; Thu, 1 Dec 2022 05:44:53 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 07F5962010; Thu, 1 Dec 2022 13:44:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B47B5C433D7; Thu, 1 Dec 2022 13:44:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669902292; bh=5sRFITPs44j4r5KcAiIfITkBuIoIWOP6e18wJXIjo7A=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=owJLq6bFIEy6vvDGINrSVr4BkcjrKupAsXEu+OWJWk1BBf1NIvmKqC877C+C9KSMZ BnuTVrkkiS7VROWbnvi49Eoucv6qk7Nr7JPi9HCLBbL8YENNfCQ/qSjcXLZI9q4S1Z GabyMTN5qpO4y90HaWX3zFM1Fzau0WVUMJh72ey6z7WV99cnnxfBK/bVqa1/xpHY9l bkPrCwWfKtjtgsQw7W07iaxZg+u9C+0xJjg/DuY4VERPPPUXO2UqnRbS+qWOjNAnua daPVmPW9TzkmyN+hbx2czrcT7VoH/m7iRGqZy7Qp+m8fG4ZXlp8Oh6gec30bjCY07E vb6H4oPjbVn5Q== Date: Thu, 1 Dec 2022 13:44:46 +0000 From: Will Deacon To: Waiman Long Cc: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Phil Auld , Wenjie Li , David Wang =?utf-8?B?546L5qCH?= , linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH-tip] sched: Fix use-after-free bug in dup_user_cpus_ptr() Message-ID: <20221201134445.GC28489@willie-the-truck> References: <20221128014441.1264867-1-longman@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20221128014441.1264867-1-longman@redhat.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Nov 27, 2022 at 08:44:41PM -0500, Waiman Long wrote: > Since commit 07ec77a1d4e8 ("sched: Allow task CPU affinity to be > restricted on asymmetric systems"), the setting and clearing of > user_cpus_ptr are done under pi_lock for arm64 architecture. However, > dup_user_cpus_ptr() accesses user_cpus_ptr without any lock > protection. When racing with the clearing of user_cpus_ptr in > __set_cpus_allowed_ptr_locked(), it can lead to user-after-free and > double-free in arm64 kernel. > > Commit 8f9ea86fdf99 ("sched: Always preserve the user requested > cpumask") fixes this problem as user_cpus_ptr, once set, will never > be cleared in a task's lifetime. However, this bug was re-introduced > in commit 851a723e45d1 ("sched: Always clear user_cpus_ptr in > do_set_cpus_allowed()") which allows the clearing of user_cpus_ptr in > do_set_cpus_allowed(). This time, it will affect all arches. > > Fix this bug by always clearing the user_cpus_ptr of the newly > cloned/forked task before the copying process starts and check the > user_cpus_ptr state of the source task under pi_lock. > > Note to stable, this patch won't be applicable to stable releases. > Just copy the new dup_user_cpus_ptr() function over. > > Fixes: 07ec77a1d4e8 ("sched: Allow task CPU affinity to be restricted on asymmetric systems") > Fixes: 851a723e45d1 ("sched: Always clear user_cpus_ptr in do_set_cpus_allowed()") > CC: stable@vger.kernel.org > Reported-by: David Wang 王标 > Signed-off-by: Waiman Long > --- > kernel/sched/core.c | 32 ++++++++++++++++++++++++++++---- > 1 file changed, 28 insertions(+), 4 deletions(-) As per my comments on the previous version of this patch: https://lore.kernel.org/lkml/20221201133602.GB28489@willie-the-truck/T/#t I think there are other issues to fix when racing affinity changes with fork() too. > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index 8df51b08bb38..f2b75faaf71a 100644 > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -2624,19 +2624,43 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) > int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src, > int node) > { > + cpumask_t *user_mask; > unsigned long flags; > > + /* > + * Always clear dst->user_cpus_ptr first as their user_cpus_ptr's > + * may differ by now due to racing. > + */ > + dst->user_cpus_ptr = NULL; > + > + /* > + * This check is racy and losing the race is a valid situation. > + * It is not worth the extra overhead of taking the pi_lock on > + * every fork/clone. > + */ > if (!src->user_cpus_ptr) > return 0; data_race() ? > > - dst->user_cpus_ptr = kmalloc_node(cpumask_size(), GFP_KERNEL, node); > - if (!dst->user_cpus_ptr) > + user_mask = kmalloc_node(cpumask_size(), GFP_KERNEL, node); > + if (!user_mask) > return -ENOMEM; > > - /* Use pi_lock to protect content of user_cpus_ptr */ > + /* > + * Use pi_lock to protect content of user_cpus_ptr > + * > + * Though unlikely, user_cpus_ptr can be reset to NULL by a concurrent > + * do_set_cpus_allowed(). > + */ > raw_spin_lock_irqsave(&src->pi_lock, flags); > - cpumask_copy(dst->user_cpus_ptr, src->user_cpus_ptr); > + if (src->user_cpus_ptr) { > + swap(dst->user_cpus_ptr, user_mask); Isn't 'dst->user_cpus_ptr' always NULL here? Why do we need the swap() instead of just assigning the thing directly? Will