Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp2990473rwb; Fri, 20 Jan 2023 09:45:20 -0800 (PST) X-Google-Smtp-Source: AMrXdXvJq2/E53D4tv9akE/iEiBUh1cmtm4JLXAFaNTInLqhrdXxlNKIp5r4G7o8iG5hlXJzVBpt X-Received: by 2002:a05:6a20:9c8a:b0:af:745f:7838 with SMTP id mj10-20020a056a209c8a00b000af745f7838mr15761775pzb.30.1674236720603; Fri, 20 Jan 2023 09:45:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674236720; cv=none; d=google.com; s=arc-20160816; b=elp4cqhl6ruNb31mpdcF1iVzk2C5Dj2Gu7m40TkoI6uSve4C52YheJizjvpCgiM1lq +LbKsqtBXq8NAWpCvMIEAIW5gms9OhA2WI9hA46omr1Pe67CbCvu2ScCYRkraEHdukBz xCkKttZ6hB4mYI8GW3OpNVnXr5w1OgENO55qNG8ezOCjdeQcV15I34GW1BZNnuCa0edi /qRJY1WlxsU8jKIjIBPkBHPASHHqLAz6JssdZH6q3Ip7EY4NbbY7/XerB65FOAsBuE5f hw9x4l/1yEpU09D0D8eaDGWRmdwHHIXEdrUdrA76vKPOfd+Dxo26aXntw6Dg6co4A4h0 HAUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=UsgwD1M2WCaGKZ28mz2lre3YTN/XGgKTUhmzvANIwrs=; b=FhotsgwDsW02XSdH0i2OgAKfpOawBMcaTn1hLdDZhUmiKJEFH9ikrjwUDqXIgg72iq 3uFMQqiPESNjmsvJxr2tctDE+y3AjfG6ktEC1yiGj7ykQ9pEtKUIuk++8ffuhejNp4SF 8CU4xJx0gaCb94MHS2r0B9T8uJ0s7PXq4iwr4j9NAugIr2tgHiLO6OCPU7PZTtWW3DcG uphcd8NqRMCFHgGWT8ZBMyN2aUaWS6O9dIK0Nh++iD31BMr5PEc/fahEMlEoCMK50Wbb WKLOOsvHFsMPzRmg25WkApeYTMqzJThZGbsb8DpXQ5znYZpDnfsrQa4Og9HcKayaKrkG SLaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=UWgCAbzs; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f1-20020a656281000000b004960bccc53esi43127160pgv.698.2023.01.20.09.45.14; Fri, 20 Jan 2023 09:45:20 -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=@google.com header.s=20210112 header.b=UWgCAbzs; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230162AbjATQ31 (ORCPT + 50 others); Fri, 20 Jan 2023 11:29:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230120AbjATQ3Y (ORCPT ); Fri, 20 Jan 2023 11:29:24 -0500 Received: from mail-yb1-xb33.google.com (mail-yb1-xb33.google.com [IPv6:2607:f8b0:4864:20::b33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12B075FF0 for ; Fri, 20 Jan 2023 08:28:44 -0800 (PST) Received: by mail-yb1-xb33.google.com with SMTP id 188so7337071ybi.9 for ; Fri, 20 Jan 2023 08:28:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=UsgwD1M2WCaGKZ28mz2lre3YTN/XGgKTUhmzvANIwrs=; b=UWgCAbzsC3BkBkDEqEdU8jLnBSczGtgp4NjRW6kn+6R2j3opMeudfAuaqqqMFkgHMN Z9P5B9yuyMwdGPkgRNgnz/TOfWo4Wt5uUNjhZ9I83OZjL9gygtUTM8AtpJylYaZz4T75 llL+lsUaE67/CZ536Vs50o4WQOxUgeiSOOc2utQEH0qXqFdF+cnrQO8WfbOxo72M94IP EpLffVj4j9OiAUW1gjBCCgOnTUNBiCRg+9C5Ji2JKF0CUrBWHcpBBMDgAm5WTkUeKzl5 8Iq3R9Gt3UyVNdl2fwcUJN8gE7Zx9keZ2uWloUQNxAYituoqb67QgwmozacBZS8hJrOt R4pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=UsgwD1M2WCaGKZ28mz2lre3YTN/XGgKTUhmzvANIwrs=; b=2kK643k51JHi5aqDQnL1meAILSR/aqZIDsafq1NZZwevhlcWIkcSHW+LRcF3PZQ4mT Zf2u5Ibgy0hDbLm/wjG+ZJFikj1Nn3/w95HMl0VXGZIXoJo4UyRiFa4o1dev4Q5PtCU9 MBqT1BXN+2nnl/tYTe2b6fJDEltd0Luid/JPZoReofaYufrR8zQLI9b+elvrjhhjdV05 J8h5vlpyIGPQSo/+0PpyC7yTJ8OlqcKnXYH1+vaHAw1pNi8oBOPlgyM99t70CGaz16k3 pTq2SmfJ4hwFVayc58CjyLG5oTInYDfNtYMLQptM0jOZEunDIWDJfI0YwsNXPzrv8BI6 IIqw== X-Gm-Message-State: AFqh2kr5o1HyQjtqPG5vBpeRyIH+bWyWsWOSHgCWI8BS2ErHOGfRDTQC +1FDTZD+XScOUPWjdmQXWyLJbB36lJ77Eed2stqWzA== X-Received: by 2002:a25:ceca:0:b0:7e4:115c:9cf6 with SMTP id x193-20020a25ceca000000b007e4115c9cf6mr1790830ybe.316.1674232116013; Fri, 20 Jan 2023 08:28:36 -0800 (PST) MIME-Version: 1.0 References: <20230113022555.2467724-1-kamatam@amazon.com> <20230120013055.3628-1-hdanton@sina.com> <20230120090001.3807-1-hdanton@sina.com> In-Reply-To: <20230120090001.3807-1-hdanton@sina.com> From: Suren Baghdasaryan Date: Fri, 20 Jan 2023 08:28:25 -0800 Message-ID: Subject: Re: another use-after-free in ep_remove_wait_queue() To: Hillf Danton Cc: Munehisa Kamata , Tejun Heo , ebiggers@kernel.org, hannes@cmpxchg.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, mengcc@amazon.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 On Fri, Jan 20, 2023 at 1:00 AM Hillf Danton wrote: > > On Thu, 19 Jan 2023 17:37:11 -0800 Suren Baghdasaryan wrote: > > On Thu, Jan 19, 2023 at 5:31 PM Hillf Danton wrote: > > > On Thu, 19 Jan 2023 13:01:42 -0800 Suren Baghdasaryan wrote: > > > > > > > > Hi Folks, > > > > I spent some more time digging into the details and this is what's > > > > happening. When we call rmdir to delete the cgroup with the pressure > > > > file being epoll'ed, roughly the following call chain happens in the > > > > context of the shell process: > > > > > > > > do_rmdir > > > > cgroup_rmdir > > > > kernfs_drain_open_files > > > > cgroup_file_release > > > > cgroup_pressure_release > > > > psi_trigger_destroy > > > > > > > > Later on in the context of our reproducer, the last fput() is called > > > > causing wait queue removal: > > > > > > > > fput > > > > ep_eventpoll_release > > > > ep_free > > > > ep_remove_wait_queue > > > > remove_wait_queue > > > > > > > > By this time psi_trigger_destroy() already destroyed the trigger's > > > > waitqueue head and we hit UAF. > > > > I think the conceptual problem here (or maybe that's by design?) is > > > > that cgroup_file_release() is not really tied to the file's real > > > > lifetime (when the last fput() is issued). Otherwise fput() would call > > > > eventpoll_release() before f_op->release() and the order would be fine > > > > (we would remove the wait queue first in eventpoll_release() and then > > > > f_op->release() would cause trigger's destruction). > > > > > > eventpoll_release > > > eventpoll_release_file > > > ep_remove > > > ep_unregister_pollwait > > > ep_remove_wait_queue > > > > > > > Yes but fput() calls eventpoll_release() *before* f_op->release(), so > > waitqueue_head would be removed before trigger destruction. > > Then check if file is polled before destroying trigger. > > +++ b/kernel/sched/psi.c > @@ -1529,6 +1529,7 @@ static int psi_fop_release(struct inode > { > struct seq_file *seq = file->private_data; > > + eventpoll_release_file(file); Be careful here and see the comment in https://elixir.bootlin.com/linux/latest/source/fs/eventpoll.c#L912. eventpoll_release_file() assumes that the last fput() was called and nobody other than ep_free() will race with us. So, this will not be that simple. Besides if we really need to fix the order here, the fix should be somewhere at the level of cgroup_file_release() or even kernfs to work for other similar situations. > psi_trigger_destroy(seq->private); > return single_release(inode, file); > } >