Re: [PATCH v2] binder: fix use-after-free due to fdget() optimization
From: Al Viro
Date: Wed Dec 05 2018 - 19:40:47 EST
On Wed, Dec 05, 2018 at 04:21:55PM -0800, Todd Kjos wrote:
> > How about grabbing the references to all victims (*before* screwing with
> > ksys_close()), sticking them into a structure with embedded callback_head
> > and using task_work_add() on it, the callback doing those fput()?
> >
> > The callback would trigger before the return to userland, so observable
> > timing of the final close wouldn't be changed. And it would avoid the
> > kludges like this.
>
> I'll rework it according to your suggestion. I had hoped to do this in a way
> that doesn't require adding calls to non-exported functions since we are
> trying to clean up binder (I hear you snickering) to be a better citizen and
> not rely on internal functions that drivers shouldn't be using. I presume
> there are no plans to export task_work_add()...
Er... Your variant critically depends upon binder being non-modular; if it
*was* built as a module, you could
* lose the timeslice just after your fput()
* have another process hit the final fput() *and* close the struct file
* now that module refcount is not pinned by anything, get rmmod remove
your module
* have the process in binder_ioctl() regain the timeslice and find the
code under it gone.
That's one of the reasons why such kludges are brittle as hell - normally you
are guaranteed that once fdget() has succeeded, the final fput() won't happen
until fdput(). With everything that guarantees in terms of code/data not going
away under you. This patch relies upon the lack of accesses to anything
sensitive after that fput() added into binder_ioctl(). Which is actually
true, but only because the driver is not modular...
At least this variant (task_work_add()-based) doesn't depend on anything
subtle - the lack of exports is the only problem there (IOW, it would've
worked in a module if not for that).