Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp7759739rwl; Tue, 10 Jan 2023 05:11:27 -0800 (PST) X-Google-Smtp-Source: AMrXdXs9pr0RpvCbUDcANrT4SCoNemrYXncXKzFVQO7TSqY1iZD0bBs3BmlXDUUxUvN0HMEFtuFz X-Received: by 2002:aa7:c2c5:0:b0:498:2223:2df9 with SMTP id m5-20020aa7c2c5000000b0049822232df9mr11994866edp.4.1673356286890; Tue, 10 Jan 2023 05:11:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673356286; cv=none; d=google.com; s=arc-20160816; b=yU51+AlIYDyx2ouV3tZPss24IrsCl1wxnlMnZndGog0JjwQVvCKcq2IpVCZwK14Y03 iZzHkH9DnmxawnJcJqNSKMKz7190g70ZNGJ1KXDg/wL3aWpSOHP6ww/KY27UEYIYrrKY RoiGoYBtjwwsGCOEk5Jl9pIJorgb3Bq1JT5a2aej/F8zfcnog3RJ3i88wo/KuAiSvDxs net+9VhOo3jb++rEpknCuc43UXDYUODdcjtL2gUCnxrWmdYqTqKFMzuJmK4NmeMRQfjZ gd94s1j6pjUJv9B3+etqEKvtfaWvremwOYEQIC1+IGZpW/AqT/YTIVbJJqyNaQOeCtE5 sG+Q== 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=VTKycRE9qo8e6FGDFOpkZPyPAwiNdhOd1mOeLz94gYU=; b=jl0dndC9yZBtPNjwSAjZMAp0Op5DhShbMikHx6QH8LkYLPGwDixzl6R6LK/iEgB5J0 +b5+1bR/qIPFSI27EJtpaLSj+KC4jsIpGM2FGnXWG3XNGaMhc1+eZu0Z2LZYQHTVJr/i q6EtcVYfvJ2VCZYSfuqJP9WtgvfXbW3LkKgPaUU3/Pc+EznYuA+ayXXmp9ZZ+xfgxPBf FAwmrPNTiJcuzBit2fRVzUWzsEpcdSPa2iSsrCtoGmpnX9Ysmdbdd0XC9Gn6P/9tIPiN uQaQoBenq7fLJ7OWL5lnWoATSra8haSYGD6hloK91klEBvHW7FK1M9NiBwip+EcDxWNF BjGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FB3evCvK; 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 h19-20020a05640250d300b0048b0ae570d8si14466876edb.508.2023.01.10.05.11.14; Tue, 10 Jan 2023 05:11:26 -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=FB3evCvK; 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 S231519AbjAJNDj (ORCPT + 53 others); Tue, 10 Jan 2023 08:03:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238648AbjAJNDd (ORCPT ); Tue, 10 Jan 2023 08:03:33 -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 0648E517FD; Tue, 10 Jan 2023 05:03:33 -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 964E4616DE; Tue, 10 Jan 2023 13:03:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EFF22C433EF; Tue, 10 Jan 2023 13:03:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1673355812; bh=tMAJplNMu5w1rrrs8QfI2RyE5frRVobjXl2ItdE9iRE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=FB3evCvKqzN4h1XROqvItFBBPt9ZEZinl3EhlU3lwXvLVvz4lhBLOIGiSLWSIFzPQ jwanItiorJhY84gT+v8DFLFJJEwi9JcP1VhVPEqIEpzHpfl/zuXsVBjSvfOBkqPS2k Z31ReAMDPMpBK6t4k2HwA9W3WffW3gEGzRNxtTxNbjBaexfW+sMVGhW08R613aLOHs nRaSTYlVVrqEqcnl3hriHSy6myQkYv+t2xpOV0kV+FO2UJSQ490sOJdjvTe5beJpBQ DUr+b1Yt0j8yq1o3xDg47uwMZbgOD8CshEOfxdrZH6+o47ZgWF04MIzcai5+2ILRB2 jlHDN85X6H1LA== Date: Tue, 10 Jan 2023 13:03:25 +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 , Valentin Schneider , Phil Auld , Wenjie Li , David Wang =?utf-8?B?546L5qCH?= , Quentin Perret , linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH v6 1/2] sched: Fix use-after-free bug in dup_user_cpus_ptr() Message-ID: <20230110130324.GA9180@willie-the-truck> References: <20221231041120.440785-1-longman@redhat.com> <20221231041120.440785-2-longman@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20221231041120.440785-2-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 Fri, Dec 30, 2022 at 11:11:19PM -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. Since sched_setaffinity() can be invoked from another > process, the process being modified may be undergoing fork() at > the same time. When racing with the clearing of user_cpus_ptr in > __set_cpus_allowed_ptr_locked(), it can lead to user-after-free and > possibly 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 | 34 +++++++++++++++++++++++++++++----- > 1 file changed, 29 insertions(+), 5 deletions(-) Acked-by: Will Deacon Will