Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp6014287pxb; Mon, 14 Feb 2022 13:09:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJzFA4Bc6whWH2DO4qPf7Ug0j5ZfaoNW3Lk1hsT3eLgaFY/+d1Jpq1Vt62wXTthxMNXwgsHC X-Received: by 2002:a17:902:e804:: with SMTP id u4mr689995plg.41.1644872973627; Mon, 14 Feb 2022 13:09:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644872973; cv=none; d=google.com; s=arc-20160816; b=i2BdmYipFw1qZGrBTItj7CL5P4fuYjvHyWZagPmDSVgubL9xR2o5WPLsr5CTwVkMAJ XvswsByVy2HCilTlAOiiwkTfAqFJw1gcMdOce5cSkd+cMPw8Xcp9mxakDpLrOVK0WQOK 6FQ+iXoBzOfMWDnSZSzfRT98BJwq9JP6w08mzYotd+tB+EsraC4t25ld03hYeH0P9585 0lxeFpRK+g4RRKdm2rHladrn2j03rBB21b90rb+96HNb7yi1sJrfnI9XDlG96CWcXeMz taplP2JyjBZnJKPxpXUgGy+DG58oOK+cUMei6zEt0eFu5NQ8NgBINj/ckqhWkg+5HNY/ okHw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=nMm9KrCV++T8kHdQKpTzL4M9bvp6CjkRA0VCrstiunA=; b=B+JQnIq/WPhTQMpTMv+49RiU1PjU4IfEGmm7v7Gc9IbND5oQ5prkUmWDiRbbWcqy22 co+wempovmzY8B1k6BxzzVm7DQvDoJchYmeF/fmvjGE7HcYQuJIQFFVSqkussxSqu4Zo pTg6tujUVNdaUmuxubDt08ZTvqQxjbTpZuoz2IB4duumMH2rWEzotQDlI7S4ECDTfWq+ VuemGice63PvZkaLuM2vcqddU5CrgkU7WlJI9AvTnRmiS89S50lpinSy6ncrwJEEGbr4 WA/cfn0EnClBW+qxlyVEyBa28M/ARuU5TfpvhtR8yBMUYQTMFPMvPUrXeqHJXn6TLZfx liBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=yIVHU0zi; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id c17si14411297plg.443.2022.02.14.13.09.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Feb 2022 13:09:33 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=yIVHU0zi; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 91C6B20A35A; Mon, 14 Feb 2022 12:29:45 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243901AbiBNKSc (ORCPT + 99 others); Mon, 14 Feb 2022 05:18:32 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:47952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347184AbiBNKQV (ORCPT ); Mon, 14 Feb 2022 05:16:21 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9EB8E6D86F; Mon, 14 Feb 2022 01:53:39 -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 ams.source.kernel.org (Postfix) with ESMTPS id 3F99CB80DBF; Mon, 14 Feb 2022 09:53:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3E053C340E9; Mon, 14 Feb 2022 09:53:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1644832416; bh=J4TfmdW9SmmCxPVvKGSAjHUPbDY8uIQC53QynwaK9Pk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yIVHU0ziXghpd8yCMWVcRqb2iNzGNBXQvI69r/0RZZeSX1vPPPGrqXAJvyDWEJGYU RTXG3hL9sJ8WD2Wvv1kYFM/C0q36ghY4zczIVMJcVR+Wwih4st3R6Ihrwlyqa6/ZEq 8BL6fYbCKM5t7/AG95LdUeGIG1ET9aULQRf1bY6A= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+af7a719bc92395ee41b3@syzkaller.appspotmail.com, Tadeusz Struk , "Peter Zijlstra (Intel)" , Dietmar Eggemann Subject: [PATCH 5.15 165/172] sched/fair: Fix fault in reweight_entity Date: Mon, 14 Feb 2022 10:27:03 +0100 Message-Id: <20220214092512.081826010@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220214092506.354292783@linuxfoundation.org> References: <20220214092506.354292783@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 From: Tadeusz Struk commit 13765de8148f71fa795e0a6607de37c49ea5915a upstream. Syzbot found a GPF in reweight_entity. This has been bisected to commit 4ef0c5c6b5ba ("kernel/sched: Fix sched_fork() access an invalid sched_task_group") There is a race between sched_post_fork() and setpriority(PRIO_PGRP) within a thread group that causes a null-ptr-deref in reweight_entity() in CFS. The scenario is that the main process spawns number of new threads, which then call setpriority(PRIO_PGRP, 0, -20), wait, and exit. For each of the new threads the copy_process() gets invoked, which adds the new task_struct and calls sched_post_fork() for it. In the above scenario there is a possibility that setpriority(PRIO_PGRP) and set_one_prio() will be called for a thread in the group that is just being created by copy_process(), and for which the sched_post_fork() has not been executed yet. This will trigger a null pointer dereference in reweight_entity(), as it will try to access the run queue pointer, which hasn't been set. Before the mentioned change the cfs_rq pointer for the task has been set in sched_fork(), which is called much earlier in copy_process(), before the new task is added to the thread_group. Now it is done in the sched_post_fork(), which is called after that. To fix the issue the remove the update_load param from the update_load param() function and call reweight_task() only if the task flag doesn't have the TASK_NEW flag set. Fixes: 4ef0c5c6b5ba ("kernel/sched: Fix sched_fork() access an invalid sched_task_group") Reported-by: syzbot+af7a719bc92395ee41b3@syzkaller.appspotmail.com Signed-off-by: Tadeusz Struk Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Dietmar Eggemann Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/20220203161846.1160750-1-tadeusz.struk@linaro.org Signed-off-by: Greg Kroah-Hartman --- kernel/sched/core.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1199,8 +1199,9 @@ int tg_nop(struct task_group *tg, void * } #endif -static void set_load_weight(struct task_struct *p, bool update_load) +static void set_load_weight(struct task_struct *p) { + bool update_load = !(READ_ONCE(p->__state) & TASK_NEW); int prio = p->static_prio - MAX_RT_PRIO; struct load_weight *load = &p->se.load; @@ -4358,7 +4359,7 @@ int sched_fork(unsigned long clone_flags p->static_prio = NICE_TO_PRIO(0); p->prio = p->normal_prio = p->static_prio; - set_load_weight(p, false); + set_load_weight(p); /* * We don't need the reset flag anymore after the fork. It has @@ -6902,7 +6903,7 @@ void set_user_nice(struct task_struct *p put_prev_task(rq, p); p->static_prio = NICE_TO_PRIO(nice); - set_load_weight(p, true); + set_load_weight(p); old_prio = p->prio; p->prio = effective_prio(p); @@ -7193,7 +7194,7 @@ static void __setscheduler_params(struct */ p->rt_priority = attr->sched_priority; p->normal_prio = normal_prio(p); - set_load_weight(p, true); + set_load_weight(p); } /* @@ -9431,7 +9432,7 @@ void __init sched_init(void) #endif } - set_load_weight(&init_task, false); + set_load_weight(&init_task); /* * The boot idle thread does lazy MMU switching as well: