linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Suren Baghdasaryan <surenb@google.com>
To: Hillf Danton <hdanton@sina.com>
Cc: Munehisa Kamata <kamatam@amazon.com>, Tejun Heo <tj@kernel.org>,
	ebiggers@kernel.org,  hannes@cmpxchg.org,
	linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	 mengcc@amazon.com
Subject: Re: another use-after-free in ep_remove_wait_queue()
Date: Fri, 20 Jan 2023 08:28:25 -0800	[thread overview]
Message-ID: <CAJuCfpFShuXs_CcfXR6PjYyXemapbuHPG0oZQJb_Y9ZpCUGZXg@mail.gmail.com> (raw)
In-Reply-To: <20230120090001.3807-1-hdanton@sina.com>

On Fri, Jan 20, 2023 at 1:00 AM Hillf Danton <hdanton@sina.com> wrote:
>
> On Thu, 19 Jan 2023 17:37:11 -0800 Suren Baghdasaryan <surenb@google.com> wrote:
> > On Thu, Jan 19, 2023 at 5:31 PM Hillf Danton <hdanton@sina.com> wrote:
> > > On Thu, 19 Jan 2023 13:01:42 -0800 Suren Baghdasaryan <surenb@google.com> 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);
>  }
>


  reply	other threads:[~2023-01-20 16:28 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20230106224859.4123476-1-kamatam@amazon.com>
2023-01-07  8:07 ` Hillf Danton
2023-01-08 22:25   ` Munehisa Kamata
2023-01-08 23:49     ` Hillf Danton
2023-01-10  1:33       ` Suren Baghdasaryan
2023-01-10  3:06         ` Suren Baghdasaryan
2023-01-12 22:01           ` Suren Baghdasaryan
2023-01-13  2:25             ` Munehisa Kamata
2023-01-13 17:52               ` Suren Baghdasaryan
2023-01-19  3:06                 ` Suren Baghdasaryan
2023-01-19 21:01                   ` Suren Baghdasaryan
2023-01-19 22:25                     ` Johannes Weiner
2023-01-20  1:30                     ` Hillf Danton
2023-01-20  1:37                       ` Suren Baghdasaryan
2023-01-20  2:46                         ` Munehisa Kamata
2023-01-20  2:52                           ` Munehisa Kamata
2023-01-20  9:00                         ` Hillf Danton
2023-01-20 16:28                           ` Suren Baghdasaryan [this message]
2023-01-21  5:17                             ` Hillf Danton
2023-01-22  3:01                               ` Suren Baghdasaryan
2023-01-20  1:45                     ` Munehisa Kamata
2023-02-02  3:00                     ` [PATCH] sched/psi: fix " Munehisa Kamata
2023-02-02  4:56                       ` Eric Biggers
2023-02-02 21:11                         ` Suren Baghdasaryan
2023-02-09 17:09                           ` Suren Baghdasaryan
2023-02-09 18:46                             ` Eric Biggers
2023-02-09 19:13                               ` Suren Baghdasaryan
2023-02-13 23:50                                 ` Suren Baghdasaryan
2023-02-14  7:04                                   ` [PATCH v2] " Munehisa Kamata
2023-02-14 17:10                                     ` Suren Baghdasaryan
2023-02-14 18:13                                       ` [PATCH v3] " Munehisa Kamata
2023-02-14 18:28                                         ` Suren Baghdasaryan
2023-02-14 18:29                                           ` Suren Baghdasaryan
2023-02-14 18:55                                         ` Eric Biggers
2023-02-14 19:13                                           ` Suren Baghdasaryan
2023-02-14 18:37                                       ` [PATCH v2] " Munehisa Kamata

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAJuCfpFShuXs_CcfXR6PjYyXemapbuHPG0oZQJb_Y9ZpCUGZXg@mail.gmail.com \
    --to=surenb@google.com \
    --cc=ebiggers@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=hdanton@sina.com \
    --cc=kamatam@amazon.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mengcc@amazon.com \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox