Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1419698pxb; Thu, 4 Nov 2021 01:52:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzU6MeeOV3bwkLO/i1dWhsKea3LYQtKkopZ7Q5IaSpGSlvrpviJSK7kG9etnrQwDHrCtlmY X-Received: by 2002:a5d:954b:: with SMTP id a11mr35092260ios.99.1636015927582; Thu, 04 Nov 2021 01:52:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1636015927; cv=none; d=google.com; s=arc-20160816; b=wPnqYjpgFOkyoFc7tUJe7yUwDtlUz/EkCnyF4d+6xL0LyVQSmiS5IIwo88exX4rP41 SEtu98lcZz4IsJKZVPtk+pCBgXLwPCCXksFU0VJ5qmYvKtzjFF9ZmA17YRn4SCmOlptU u3omFkLT9EGYHFs00Wx3u2rwHWchfEsgL9Aq7ErdhI1vSgZ9lPeLUaW4rXU0eIUe+itp NjJvz9dXUeat57YxvC5VEIEaauJaKKkLzmiySO5p0OxinPIZrZMtMSHDZjva3wTZvicm ccHqGJk1XM5f2ESHAZ8WiSBlq4h1EDIpF4sg/UFqqCZ14tQtORkI3vTcFf/gTEEDPE8G szcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=luSg9Qb/tGHJq8+n6BC8TW6q6YdjF45KbC8DWD2+zqQ=; b=NErsNEwvEgDP2ClZm3ZESnQ/zLVbxN5b+hLh27APzA7itkUKQx170JtNw27gWckP6E +5na2WVwpWIATDpfuqs5OZ7tUd3KsI0Ggu3RXQgq4weCxkZ8/fERq+S83BzaY+QQ/eC6 yjB0ts9G7yG/RpjRgCjwOY+RAQFPk+wgIFJEHcEtVp3EdxeMliExUxH31L3rGhCmCeMU KgP2uVklCpBEaBbcbn02LlPpWLuqQ3xEG+Z5hGrHzZsDQlzUYo4mQvSKog0RqnTolTF5 j6yhNmuj781BgCYhb/DomJOz8uls0lzGXciz3aNlcJAU8mgwQtz+3AYkZosez9VDC4fG XNtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ny+I01M6; 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 y13si5913725ioc.90.2021.11.04.01.51.54; Thu, 04 Nov 2021 01:52:07 -0700 (PDT) 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=ny+I01M6; 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 S230301AbhKDIxf (ORCPT + 99 others); Thu, 4 Nov 2021 04:53:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230084AbhKDIxe (ORCPT ); Thu, 4 Nov 2021 04:53:34 -0400 Received: from mail-yb1-xb2f.google.com (mail-yb1-xb2f.google.com [IPv6:2607:f8b0:4864:20::b2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09503C061714 for ; Thu, 4 Nov 2021 01:50:57 -0700 (PDT) Received: by mail-yb1-xb2f.google.com with SMTP id j75so12810544ybj.6 for ; Thu, 04 Nov 2021 01:50:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=luSg9Qb/tGHJq8+n6BC8TW6q6YdjF45KbC8DWD2+zqQ=; b=ny+I01M6DfzMT7XHSua+Juj0IjpRWPch8jPbIjYywjbDsCpTbgC56dZ6bahJl76/G6 lXg9yMQ+OwXWDMssQ79DhLQLQ77trUoXuje4T6+hxUw/1rebLqRc0IgmKYFOJvWUIz3z oy63K6zx3y/yD0ix3tQGigXDju9Kl9a1kbs5+/IiMk13lm7EZuIst8F0pBx8iXNdvp0w T8uVeVz0ivq64whQik5wxvyOmV5vqD5MJmAG61G7n2GNh25EVL7pXBbikoqvdMZ+kRFm xZFtqV1GLpkL/LOFQykHllMCcGJ6g+EusOKnmQfnJFksejnMhQBvs+gP0OyFV2yvl3kp cenw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=luSg9Qb/tGHJq8+n6BC8TW6q6YdjF45KbC8DWD2+zqQ=; b=zlUZ+roqzyWwdFf7VVhcnf/G8bt8lPaoEvhysJaqIMyFLcHNHHrPXrX1kVkY1P3D7s ay/cu/Yp1+7vZfHeacc/IMZdsZsHmch6yBWTBVjA4527kG1stGGKQP9iYlXRvJN/ia/u 4HyJrQIJrsU4m3KKQmWwEpgT/j5D3nggvtP4nbTFCQU7P0s/cCSB39eBqSeEc5InWqSi Yetgbfias4lk2ReOXjktPcU7hsEEvekOvGKgro0yJ4fFRBxF3fhVt2KWdtKtJ466Dzt/ P9ZJQXTFxrRkyf2ynLEiTk0nR9e2g4toI/j8SWEERhUQ6ZBSqUlvfcTqsh3YCtoMXndD /Gzw== X-Gm-Message-State: AOAM5326eFpXALV5URsO0nrITuPI/OxwvTlGV4nUqSnGeqMaV+OQC5YI xuNA5y/8V/IuqgPwqES7KjSPpUcfxuc4pmyEwCAWZA== X-Received: by 2002:a25:dc42:: with SMTP id y63mr18448828ybe.402.1636015856185; Thu, 04 Nov 2021 01:50:56 -0700 (PDT) MIME-Version: 1.0 References: <20211103190613.3595047-1-minipli@grsecurity.net> In-Reply-To: From: Vincent Guittot Date: Thu, 4 Nov 2021 09:50:44 +0100 Message-ID: Subject: Re: [PATCH] sched/fair: Prevent dead task groups from regaining cfs_rq's To: Benjamin Segall Cc: Mathias Krause , Ingo Molnar , Peter Zijlstra , Juri Lelli , =?UTF-8?Q?Michal_Koutn=C3=BD?= , Dietmar Eggemann , Steven Rostedt , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , linux-kernel@vger.kernel.org, Odin Ugedal , Kevin Tanguy , Brad Spengler Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 3 Nov 2021 at 23:04, Benjamin Segall wrote: > > Mathias Krause writes: > > > Kevin is reporting crashes which point to a use-after-free of a cfs_rq > > in update_blocked_averages(). Initial debugging revealed that we've liv= e > > cfs_rq's (on_list=3D1) in an about to be kfree()'d task group in > > free_fair_sched_group(). However, it was unclear how that can happen. > > [...] > > Fixes: a7b359fc6a37 ("sched/fair: Correctly insert cfs_rq's to list on = unthrottle") > > Cc: Odin Ugedal > > Cc: Michal Koutn=C3=BD > > Reported-by: Kevin Tanguy > > Suggested-by: Brad Spengler > > Signed-off-by: Mathias Krause > > --- > > kernel/sched/core.c | 18 +++++++++++++++--- > > 1 file changed, 15 insertions(+), 3 deletions(-) > > > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > > index 978460f891a1..60125a6c9d1b 100644 > > --- a/kernel/sched/core.c > > +++ b/kernel/sched/core.c > > @@ -9506,13 +9506,25 @@ void sched_offline_group(struct task_group *tg) > > { > > unsigned long flags; > > > > - /* End participation in shares distribution: */ > > - unregister_fair_sched_group(tg); > > - > > + /* > > + * Unlink first, to avoid walk_tg_tree_from() from finding us (vi= a > > + * sched_cfs_period_timer()). > > + */ > > spin_lock_irqsave(&task_group_lock, flags); > > list_del_rcu(&tg->list); > > list_del_rcu(&tg->siblings); > > spin_unlock_irqrestore(&task_group_lock, flags); > > + > > + /* > > + * Wait for all pending users of this task group to leave their R= CU > > + * critical section to ensure no new user will see our dying task > > + * group any more. Specifically ensure that tg_unthrottle_up() wo= n't > > + * add decayed cfs_rq's to it. > > + */ > > + synchronize_rcu(); > > I was going to suggest that we could just clear all of avg.load_sum/etc, = but > that breaks the speculative on_list read. Currently the final avg update > just races, but that's not good enough if we wanted to rely on it to > prevent UAF. synchronize_rcu() doesn't look so bad if the alternative is > taking every rqlock anyways. > > I do wonder if we can move the relevant part of > unregister_fair_sched_group into sched_free_group_rcu. After all > for_each_leaf_cfs_rq_safe is not _rcu and update_blocked_averages does > in fact hold the rqlock (though print_cfs_stats thinks it is _rcu and > should be updated). I was wondering the same thing. we would have to move unregister_fair_sched_group() completely in sched_free_group_rcu() and probably in cpu_cgroup_css_free() too. remove_entity_load_avg(tg->se[cpu]); has to be called only once we are sure that se->my_q will not be updated which means no more in the leaf_list > > > > + > > + /* End participation in shares distribution: */ > > + unregister_fair_sched_group(tg); > > } > > > > static void sched_change_group(struct task_struct *tsk, int type)