From nobody Mon Sep 15 03:59:09 2025 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 BC965C677F1 for ; Sun, 15 Jan 2023 19:34:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231587AbjAOTcr (ORCPT ); Sun, 15 Jan 2023 14:32:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231213AbjAOTcp (ORCPT ); Sun, 15 Jan 2023 14:32:45 -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 4895C1043D for ; Sun, 15 Jan 2023 11:31:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1673811114; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=n4CmZaji8pvUDHfO+vX/Yeim0zrM+XTxLdvKCeZYpj4=; b=EU7HwaZirpFq5imgm+AMXOr/WV5N8QZm3XHD/lwuCItmrlN6g7+WtHyINrAfbO5KgcB0LS 2BjefrFOpBdmOPejRnvpjrhn9e3yfmz8gxO94TSc1mYfVwca3thapiNloxaEfoRAHqgRkB Wc4F5rq5XBf/tiUjP5xVZlDO/pR/LJA= 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-635-fovxcWLvNXa81h4NKpYbBw-1; Sun, 15 Jan 2023 14:31:53 -0500 X-MC-Unique: fovxcWLvNXa81h4NKpYbBw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E75D6801779; Sun, 15 Jan 2023 19:31:52 +0000 (UTC) Received: from llong.com (unknown [10.22.16.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2B5321121314; Sun, 15 Jan 2023 19:31:52 +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 Cc: Phil Auld , linux-kernel@vger.kernel.org, Waiman Long , kernel test robot Subject: [PATCH] sched/core: Fix NULL pointer access fault in sched_setaffinity() with non-SMP configs Date: Sun, 15 Jan 2023 14:31:22 -0500 Message-Id: <20230115193122.563036-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The kernel commit 9a5418bc48ba ("sched/core: Use kfree_rcu() in do_set_cpus_allowed()") introduces a bug for kernels built with non-SMP configs. Calling sched_setaffinity() on such a uniprocessor kernel will cause cpumask_copy() to be called with a NULL pointer leading to general protection fault. This is not really a problem in real use cases as there aren't that many uniprocessor kernel configs in use and calling sched_setaffinity() on such a uniprocessor system doesn't make sense. Fix this problem by making sure cpumask_copy() will not be called in such a case. Fixes: 9a5418bc48ba ("sched/core: Use kfree_rcu() in do_set_cpus_allowed()") Reported-by: kernel test robot Signed-off-by: Waiman Long --- kernel/sched/core.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index bb1ee6d7bdde..e838feb6adc5 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -8290,12 +8290,18 @@ long sched_setaffinity(pid_t pid, const struct cpum= ask *in_mask) if (retval) goto out_put_task; =20 + /* + * With non-SMP configs, user_cpus_ptr/user_mask isn't used and + * alloc_user_cpus_ptr() returns NULL. + */ user_mask =3D alloc_user_cpus_ptr(NUMA_NO_NODE); - if (IS_ENABLED(CONFIG_SMP) && !user_mask) { + if (user_mask) { + cpumask_copy(user_mask, in_mask); + } else if (IS_ENABLED(CONFIG_SMP)) { retval =3D -ENOMEM; goto out_put_task; } - cpumask_copy(user_mask, in_mask); + ac =3D (struct affinity_context){ .new_mask =3D in_mask, .user_mask =3D user_mask, --=20 2.31.1