Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1235359pxb; Fri, 21 Jan 2022 13:01:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJxWa20BaFVETlsHKnkJrU077N4o7LtP65i7/kLpYAhnSFXyrZdTjB64l+eirTkELpL1QmhJ X-Received: by 2002:a17:902:b903:b0:14a:db1b:6a57 with SMTP id bf3-20020a170902b90300b0014adb1b6a57mr5052924plb.67.1642798894064; Fri, 21 Jan 2022 13:01:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642798894; cv=none; d=google.com; s=arc-20160816; b=vPFDZDSwsKovsB5BIsVHTSawPaJJGtSzLF9dAZ+hI8TAJJSyOPTdCxfQjJAfIKeIbM h/XY9lxLJ7a8xwauwuoR79INdjj8gEIO0x+7ySYM/4qxskpjLVK/gx8MWBn11htpRukN vniqJ8eCEBoN+AKrdDYEjTG/ZpVU6j1PQ9UNeuEWiIN43MZj8f0RqK/oTSWeh/Ukq2JJ aTgdNWLl8LecxhcCNuYxQoktf2gltPmhq+nOATPRm2IjuYbqfDXGbUczqE1bV/dCxn3S 2O/QUoW+NGz4KgaqbSm/BAGSJIau/zkQsBOF0KXsFIJKUAwHySYCgz5WD2MwxwDOLlds 6emQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :references:cc:to:from:content-language:user-agent:mime-version:date :message-id:dkim-signature; bh=rceaFdVjTxyvongrGnOohxq0b18ZsFLz0zQ22QO1D3M=; b=FD1iDEj8akjtF8rT+zCqTFuCjHj7obZN3x8GOcg9KvczgpN0zIezfpSXoLrn9CVGKW 8r2gbuHYgUCGVXaQQSf0OF+UEj3n/aplaA6bXZisJHfqVjMVjElgcKQ+RjZIcCKwp/TI knZ8syBHECaq0I6PsIvCb2ICvisi29kCTJRXQbIrcrlTTFpA28R82QY5gDXtUq2YR31J LicD3oPpOCXu8bL1ZqGzEtpjr+/PsRPOlKThQoJmxxKBu2lYsjrgoZj7suesd45Cefnb Jyr4T+2B+bvSyO76HBlox6K9OlZ5BCi0ak6PfAlGBPwHw10uNhxlHgaNRxRqgMwGDO9Z xLsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MYd5aeMa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e192si6809209pgc.43.2022.01.21.13.01.22; Fri, 21 Jan 2022 13:01:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MYd5aeMa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358199AbiATCWO (ORCPT + 99 others); Wed, 19 Jan 2022 21:22:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229875AbiATCWM (ORCPT ); Wed, 19 Jan 2022 21:22:12 -0500 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54BB9C061574 for ; Wed, 19 Jan 2022 18:22:12 -0800 (PST) Received: by mail-pl1-x633.google.com with SMTP id e8so3941638plh.8 for ; Wed, 19 Jan 2022 18:22:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=message-id:date:mime-version:user-agent:content-language:from:to:cc :references:subject:in-reply-to:content-transfer-encoding; bh=rceaFdVjTxyvongrGnOohxq0b18ZsFLz0zQ22QO1D3M=; b=MYd5aeMah7d/2OviOmCdGzim4oA+/yT/Msp+XE+1ULtrdOq9ziZqj8ksolzxTRnTjw x+6RZ0W5QC3kTI6QP54OWyjry4KNDOd8h3Anpxu4BGI6j73LmmfTOG/uhotNV8FvcpmB JJPCKTuU4SadDfPMmX/eHworFkYoYuRwmfBu6h14Bm1MaVMkIZRE5/hQ2PofgbthhJzp IZ02Msxj4aRsFc4Nj9eBAo4wbuBCpZ2WrH922OIGxCMisEaP3LRx3puA3HCZDptuVV7h zbNvKsvCg4mG+7xKeJiY3cQCznXoSpxFg2n3coiQlzfhCMAF0zZ2X2KjrfhsKvgopjcZ h1kA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent :content-language:from:to:cc:references:subject:in-reply-to :content-transfer-encoding; bh=rceaFdVjTxyvongrGnOohxq0b18ZsFLz0zQ22QO1D3M=; b=0WgeoXd6QanjBrNKQuzCCInBO10XawPtad3JGMyWRJcZ9gAUQEC74xt6QBoii0c5vl UECpkYVpNQfgoriKzr5lBGxE9RFNqHW+wHx+4I9bWQh/G2eaOthMiRAHtl2D3/YUFbqz lu/vHhmvwBVo29I0NMoHVZZc7pkUSUgbSquYcfNOCTExhkpYEE1emgwRYfGlsigk1L/W ad+G6u/Evg1OPPDM3uAp9UxUp+mX4OEZJnUyRvDPBxeuD2swjQ3nM+MR9t+8nwopRKSr b/2K0hNCFq2DSeFtUQmJglA0REdWIWLEAEITcOoDh0789AT9z6oEOrvgUyAB/aLpKqwr IFDg== X-Gm-Message-State: AOAM533wwqay4caAhMFNWcwcFyNVx0MPkPGraAPGhZnEaT38VRZtRkcf yYxdH7ftiO2Lh3xEqhd142g1hA== X-Received: by 2002:a17:902:f54a:b0:14a:97ad:b877 with SMTP id h10-20020a170902f54a00b0014a97adb877mr26400676plf.152.1642645331817; Wed, 19 Jan 2022 18:22:11 -0800 (PST) Received: from [192.168.254.17] ([50.39.160.154]) by smtp.gmail.com with ESMTPSA id b21sm113171pgi.51.2022.01.19.18.22.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 19 Jan 2022 18:22:11 -0800 (PST) Message-ID: <799c3fee-aa4d-1c64-6c14-f6c032d37196@linaro.org> Date: Wed, 19 Jan 2022 18:22:10 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Content-Language: en-US From: Tadeusz Struk To: Peter Zijlstra Cc: mingo@redhat.com, Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Zhang Qiao , stable@vger.kernel.org, linux-kernel@vger.kernel.org References: <20220119012417.299060-1-tadeusz.struk@linaro.org> <4e13ba95-815a-79a1-e521-5f794963b691@linaro.org> Subject: Re: [PATCH] sched/fair: Fix fault in reweight_entity In-Reply-To: <4e13ba95-815a-79a1-e521-5f794963b691@linaro.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 1/19/22 07:43, Tadeusz Struk wrote: >>> Looks like after this change there is a time window, when >>> task_struct->se.cfs_rq can be NULL. This can be exploited to trigger >>> null-ptr-deref by calling setpriority on that task. >> Looks like isn't good enough, either there is, in which case you explain >> the window, or there isn't in which case what are we doing here? > > There surely is something wrong, otherwise it wouldn't crash. > I will try to narrow down the reproducer to better understand what causes > the fault. The race is between sched_post_fork() and setpriority(PRIO_PGRP) The scenario is that the main process spawns 3 new threads, which then call setpriority(PRIO_PGRP, 0, -20), wait, and exit. For each of the new thread the copy_process() gets invoked, which then calls sched_fork() and finally sched_post_fork(). There is a possibility that setpriority(PRIO_PGRP)->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() because it will try to access the CFS run queue pointer, which hasn't been set, resulting it a crash as below: KASAN: null-ptr-deref in range [0x00000000000000a0-0x00000000000000a7] CPU: 0 PID: 2392 Comm: reduced_repro Not tainted 5.16.0-11201-gb42c5a161ea3-dirty #13 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1.fc35 04/01/2014 RIP: 0010:reweight_entity+0x15d/0x440 RSP: 0018:ffffc900035dfcf8 EFLAGS: 00010006 Call Trace: reweight_task+0xde/0x1c0 set_load_weight+0x21c/0x2b0 set_user_nice.part.0+0x2d1/0x519 set_user_nice.cold+0x8/0xd set_one_prio+0x24f/0x263 __do_sys_setpriority+0x2d3/0x640 __x64_sys_setpriority+0x84/0x8b do_syscall_64+0x35/0xb0 entry_SYSCALL_64_after_hwframe+0x44/0xae ---[ end trace 9dc80a9d378ed00a ]--- Before the mentioned change the rq pointer has been set in sched_fork(), which is called much earlier in copy_process() as opposed to sched_post_fork(), before the new task is added to the thread_group. A stripped down version of the sysbot reproducer can be found here: https://termbin.com/axkq I can consistently reproduce the issue with it in 2-3 runs. The solution is either we set the pointer p->se.cfs_rq to a dummy rq in sched_fork(), or return from the set_one_prio() without doing anything if the rq is NULL, as it is done in the patch. I will update the description and resend it tomorrow. -- Thanks, Tadeusz