Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1797013pxp; Mon, 7 Mar 2022 02:33:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJxkLEHKqvXbo3zC/vIJhpzD3CokVci+xUiE9uyTP76kiXlTaHnVgcupEXRPl9gzSoL5ZkAj X-Received: by 2002:a17:903:1d0:b0:151:c879:3428 with SMTP id e16-20020a17090301d000b00151c8793428mr11362316plh.52.1646649213062; Mon, 07 Mar 2022 02:33:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646649213; cv=none; d=google.com; s=arc-20160816; b=kX4Yf+ONkDtKmbdX3/ZZZ0fGCDkP2LrZul9s4zCDVMmc7k4KdWicebBGov7GikPtke HQtCnAc9oqgbA+wwm9OhFHLFppMcevd+vNlxWjSJbrMH7afWI5rfM7TF/1yAzbMDFOYs e2m5YMNIFVv1nVEw2rVRp9ui6luAi6ApSDxmb8PV5ytx2OXkdOwI09daKFfiu1zTZ481 6j6SHSvZBzs2ZXl9sQCvWqipce07XfEPW3+GYEYvq3HYA2jECTWksVVCHnoYTdfxZEaY nAPCCwcTblqUjFXlD0aH2sxM8yAqMaOixw+8/BXQHLgI+yurHk2YWIfa+tmpyfwjbpZK wPhg== 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=UoMsCJGuNNa+tfsHjGzd07VNv0uPKpVKggqHBL9fkjo=; b=yt+ObTrr1slfHKmWEYhIA4WekfyOeGJc9yw+Al+9Wpt3RHYsO5J7chb9ncCa8ok0Zd g45xhpQl9ZjifBBCuWV2mlx7ekRVPIbTMz8VkNOf03khXoN6gME7VRsZ1Vu/Vb14cJL+ kKPraY1FjW1qBu55HWDBsEiLSX3T6sYYiDe8CSmppL2I8UIhRyGBHDihkWQyjwqJylRJ r7Ejn9pVeev+e0HDbFKKSAeeY/BfrRuS86Cuh7AKkCYoHu+rioqu2nM23kmWiEX+pHLR xKumT5+nezJ60Bywmj2uEJHVHTm9fnkg77aNuN0W6k+WGe3DP0gBATvKWUTcTdB5lPgS 7pDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="t8sRntp/"; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c2-20020a170903234200b00151ec830d9dsi2199913plh.199.2022.03.07.02.33.19; Mon, 07 Mar 2022 02:33:33 -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=@linuxfoundation.org header.s=korg header.b="t8sRntp/"; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240214AbiCGKGG (ORCPT + 99 others); Mon, 7 Mar 2022 05:06:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238364AbiCGJpB (ORCPT ); Mon, 7 Mar 2022 04:45:01 -0500 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E219265483; Mon, 7 Mar 2022 01:41:44 -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 sin.source.kernel.org (Postfix) with ESMTPS id 05C2CCE0EA6; Mon, 7 Mar 2022 09:36:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E1127C340E9; Mon, 7 Mar 2022 09:36:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1646645798; bh=M4VKDxamwSQeNGGlXuPwb4g02Kpws/cOYKRyL0pwvRg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=t8sRntp/kr8TtsDHdDPWlVvgpenw3AJ6CQYuUk6ZcJ5QEFXtiqYKBUQPMqMEJdKFk SZyyl93ttJGsntpisTvufp9mrtZkCJGk9afCKTrk2zIpsZ+2HmPvYALFzG9dMp7GMd 6ppQd+jM1FCGgrufW5O7KmJyCPAcZWaNK+ERiIH4= 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 , Sasha Levin Subject: [PATCH 5.15 038/262] sched/fair: Fix fault in reweight_entity Date: Mon, 7 Mar 2022 10:16:22 +0100 Message-Id: <20220307091703.600785457@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220307091702.378509770@linuxfoundation.org> References: <20220307091702.378509770@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=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 From: Tadeusz Struk [ Upstream commit 13765de8148f71fa795e0a6607de37c49ea5915a ] 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: Sasha Levin --- kernel/sched/core.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index c2dec6ce98091..57e5c79142964 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1199,8 +1199,9 @@ int tg_nop(struct task_group *tg, void *data) } #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, struct task_struct *p) 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, long nice) 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 task_struct *p, */ 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: -- 2.34.1