From nobody Mon Apr 6 21:32:23 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 292D8ECAAD5 for ; Fri, 2 Sep 2022 15:39:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237231AbiIBPjy (ORCPT ); Fri, 2 Sep 2022 11:39:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237232AbiIBPij (ORCPT ); Fri, 2 Sep 2022 11:38:39 -0400 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 4BA2B65825 for ; Fri, 2 Sep 2022 08:26:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1662132380; 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: in-reply-to:in-reply-to:references:references; bh=b+EEFoR1hsnuIiJiHIQPhbUi8fFyaPXLGuuGMxFE3I0=; b=Yhl915Pr07j6uHHbwwfDzhEb8WJKAPTfWmA+jBc+DnTzIbjK3n9nKG0tPsRS5uP5XUoKFH nmBrAmVR/dcbudAC6hwoOfQ5ndqXwatPDvJxnDvq/HZDD9BxCfBHV9+IEWWvkxrLw1RQd4 XvEGgUp2B5A2cSTx/LiOU3tVOrdPHIg= 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-49-QkvML-CBMrKS7ltBc-uHlA-1; Fri, 02 Sep 2022 11:26:13 -0400 X-MC-Unique: QkvML-CBMrKS7ltBc-uHlA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1E52F2919EC7; Fri, 2 Sep 2022 15:26:13 +0000 (UTC) Received: from llong.com (unknown [10.22.10.219]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6A0DE1415139; Fri, 2 Sep 2022 15:26:12 +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 , Valentin Schneider , Tejun Heo , Zefan Li , Johannes Weiner , Will Deacon Cc: linux-kernel@vger.kernel.org, Linus Torvalds , Lai Jiangshan , Waiman Long Subject: [PATCH v7 5/5] sched: Fix sched_setaffinity() and fork/clone() race Date: Fri, 2 Sep 2022 11:25:56 -0400 Message-Id: <20220902152556.373658-6-longman@redhat.com> In-Reply-To: <20220902152556.373658-1-longman@redhat.com> References: <20220902152556.373658-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" sched_setaffinity() can also race with a concurrent fork/clone() syscall calling dup_user_cpus_ptr(). That may lead to a use after free problem. Fix that by protecting the cpumask copying using pi_lock of the source task. Signed-off-by: Waiman Long --- kernel/sched/core.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 618341d0fa51..7157c9a3f31e 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2602,6 +2602,8 @@ 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) { + unsigned long flags; + if (!src->user_cpus_ptr) return 0; =20 @@ -2609,7 +2611,10 @@ int dup_user_cpus_ptr(struct task_struct *dst, struc= t task_struct *src, if (!dst->user_cpus_ptr) return -ENOMEM; =20 + /* Use pi_lock to protect content of user_cpus_ptr */ + raw_spin_lock_irqsave(&src->pi_lock, flags); cpumask_copy(dst->user_cpus_ptr, src->user_cpus_ptr); + raw_spin_unlock_irqrestore(&src->pi_lock, flags); return 0; } =20 --=20 2.31.1