Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp14129601rwb; Sun, 27 Nov 2022 18:13:02 -0800 (PST) X-Google-Smtp-Source: AA0mqf7Dhazc98P7ARKLlnqTKP8x5tkTgh1r9XKj1Tu19cbD+HEBpYBO8hUH6Q6MR+rGrQ3LhnhW X-Received: by 2002:a63:fa44:0:b0:476:d169:30ea with SMTP id g4-20020a63fa44000000b00476d16930eamr25628087pgk.474.1669601582284; Sun, 27 Nov 2022 18:13:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669601582; cv=none; d=google.com; s=arc-20160816; b=e76R18FdMU5XS5gNKMfNS8UmTc6EtmYjKcaLDnv+U44hyZmziJt5bKTHf9gBbQAnga BLHY5PWfTJoQ8cRLemn1D53ySilhowyFyeVWVGsAH9shbFqO2dIHNvEcBSjdXvlTksM9 PoPan75iZnhFMd5JZbrcUn/ZTBQrFW1CbZS9rppIzVDIhLXIOZJeIJasHgdMNovUyLar t2lctWluQLeyUcQ5FJkWUXWBak63QC+OBB+yBgQd49VbZrfobF5eXeTjPorp3C9GD0fu diqW4XHPyKrfXAb5Kr2y6E4aWQCHjyEv1aRwNIlctCMz6lQcxAuECWlibezMgmC4gzbl 117A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=tMMB0t/gORIeEsZ+px93MN8rafNAe9ozury07qdIQdo=; b=Uo9VCb3SzRq/htZb4Omyze37nXBkkdsfg6mM3iIzQFpj7sIiO6NdO6Q4gIIaGp6DlL O6JicKQBZdFfHqUZ2TF4EVXDMG4uD2OVOK8osR57xMoJ1LmnWltnrIMg7eZ/tyzMnlfT UWvcjMjA1KYvcUaIaFIxRkSuEeTRjdgrRYgRySsgW5YceeAJu91hXsFLnI8zznRsYQEd HsU/91jPqkIciQip2OlI6k7yiKNhloPqAMNpPxENa8Q09neWbLOjlA79s9JY/ECMPp7J iQWZ/gLSwOx7Mc4DWVJ8OsdxR0gIkJb+vOqyf3To3sBfqfOMQj/kPDdWAXfeTKuWaCCH AHMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=F3A7BvSJ; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j62-20020a638b41000000b0046ece301e82si3788208pge.756.2022.11.27.18.12.51; Sun, 27 Nov 2022 18:13:02 -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=@redhat.com header.s=mimecast20190719 header.b=F3A7BvSJ; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229640AbiK1Bpz (ORCPT + 85 others); Sun, 27 Nov 2022 20:45:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229509AbiK1Bpy (ORCPT ); Sun, 27 Nov 2022 20:45:54 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C704263AE for ; Sun, 27 Nov 2022 17:45:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669599902; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=tMMB0t/gORIeEsZ+px93MN8rafNAe9ozury07qdIQdo=; b=F3A7BvSJNxbHB3Rkews14kMckTF8MlgwK6zWtDD/9n0f9sXPYty1S2Uv2KCWVOHls2jnVu wVIgCf4lXXcV6kVWYGl91ptYjVbv0h1DiSZsH99CeXfaztVjPGXjCnBhA0Y7P33nkLpb7l IOZSx0l9oJoT8QPdNmbct45nhJDNOA4= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-529-BEpniTN2NtmUk9Lqt0ZPvA-1; Sun, 27 Nov 2022 20:44:57 -0500 X-MC-Unique: BEpniTN2NtmUk9Lqt0ZPvA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 144061C05ABB; Mon, 28 Nov 2022 01:44:57 +0000 (UTC) Received: from llong.com (unknown [10.22.32.57]) by smtp.corp.redhat.com (Postfix) with ESMTP id 70A02C15BA4; Mon, 28 Nov 2022 01:44:56 +0000 (UTC) From: Waiman Long To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira Cc: Phil Auld , Wenjie Li , =?UTF-8?q?David=20Wang=20=E7=8E=8B=E6=A0=87?= , linux-kernel@vger.kernel.org, Waiman Long , stable@vger.kernel.org Subject: [PATCH-tip] sched: Fix use-after-free bug in dup_user_cpus_ptr() Date: Sun, 27 Nov 2022 20:44:41 -0500 Message-Id: <20221128014441.1264867-1-longman@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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 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(-) 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; - 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); + cpumask_copy(dst->user_cpus_ptr, src->user_cpus_ptr); + } raw_spin_unlock_irqrestore(&src->pi_lock, flags); + + if (unlikely(user_mask)) + kfree(user_mask); + return 0; } -- 2.31.1