From nobody Fri Apr 10 14:24:11 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 84645ECAAD8 for ; Fri, 16 Sep 2022 18:33:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230084AbiIPSdA (ORCPT ); Fri, 16 Sep 2022 14:33:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230043AbiIPSch (ORCPT ); Fri, 16 Sep 2022 14:32:37 -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 5E9BFB7756 for ; Fri, 16 Sep 2022 11:32:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663353155; 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=T+PEuxHCi+ATSWjwodTYHg19fW+xLGaox+jYGObcwjs=; b=R5m03Ml6RI9X4E4Akzepr4KknHBtBKazf/2dHE6+ah9YZqTHGI7QbwEip8nySKwubkslca Ydyec8mKRBWgOlxFPTIwYoqrx3ojKqkz0fM9CAt6ymDpvZO/tNWXmI+y43o3pJblR5D5J/ XWoexWIYM2yOGS18aKjx39V/Oidg+K8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-121-Oq0xZ-C2MairowVbc7yeXA-1; Fri, 16 Sep 2022 14:32:31 -0400 X-MC-Unique: Oq0xZ-C2MairowVbc7yeXA-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 04EEF862FDC; Fri, 16 Sep 2022 18:32:31 +0000 (UTC) Received: from llong.com (unknown [10.22.17.61]) by smtp.corp.redhat.com (Postfix) with ESMTP id 54AF3140EBF5; Fri, 16 Sep 2022 18:32:30 +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 v9 6/7] sched: Fix sched_setaffinity() and fork/clone() race Date: Fri, 16 Sep 2022 14:32:16 -0400 Message-Id: <20220916183217.1172225-7-longman@redhat.com> In-Reply-To: <20220916183217.1172225-1-longman@redhat.com> References: <20220916183217.1172225-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 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 c748e56ba254..ce626cad4105 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2612,6 +2612,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 @@ -2619,7 +2621,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