linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Al Viro <viro@zeniv.linux.org.uk>
To: Vladis Dronov <vdronov@redhat.com>
Cc: Richard Cochran <richardcochran@gmail.com>,
	linux-fsdevel@vger.kernel.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] fs: fix use-after-free in __fput() when a chardev is removed but a file is still open
Date: Sun, 8 Dec 2019 19:53:40 +0000	[thread overview]
Message-ID: <20191208195340.GX4203@ZenIV.linux.org.uk> (raw)
In-Reply-To: <20191208194907.GW4203@ZenIV.linux.org.uk>

On Sun, Dec 08, 2019 at 07:49:07PM +0000, Al Viro wrote:
> On Mon, Nov 25, 2019 at 01:53:42PM +0100, Vladis Dronov wrote:
> > In a case when a chardev file (like /dev/ptp0) is open but an underlying
> > device is removed, closing this file leads to a use-after-free. This
> > reproduces easily in a KVM virtual machine:
> > 
> > # cat openptp0.c
> > int main() { ... fp = fopen("/dev/ptp0", "r"); ... sleep(10); }
> 
> > static void __fput(struct file *file)
> > {   ...
> >     if (file->f_op->release)
> >         file->f_op->release(inode, file); <<< cdev is kfree'd here
> 
> >     if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
> >              !(mode & FMODE_PATH))) {
> >         cdev_put(inode->i_cdev); <<< cdev fields are accessed here
> > 
> > because of:
> > 
> > __fput()
> >   posix_clock_release()
> >     kref_put(&clk->kref, delete_clock) <<< the last reference
> >       delete_clock()
> >         delete_ptp_clock()
> >           kfree(ptp) <<< cdev is embedded in ptp
> >   cdev_put
> >     module_put(p->owner) <<< *p is kfree'd
> > 
> > The fix is to call cdev_put() before file->f_op->release(). This fix the
> > class of bugs when a chardev device is removed when its file is open, for
> > example:
> 
> And what's to prevent rmmod coming and freeing ->release code right as you
> are executing it?

FWIW, the bug here seems to be that the lifetime rules of cdev are fucked -
if it can get freed while its ->kobj is still alive, we have something
very wrong there.  IOW, you have ptp lifetime controlled by *TWO*
refcounts - that of clk and that of of cdev->kobj.  That's doesn't work.
Replace that kfree() with dropping a kobject reference, perhaps, so
that freeing would've been done by its release callback?

  reply	other threads:[~2019-12-08 19:53 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-25 12:53 [PATCH] fs: fix use-after-free in __fput() when a chardev is removed but a file is still open Vladis Dronov
2019-12-08 19:49 ` Al Viro
2019-12-08 19:53   ` Al Viro [this message]
2019-12-27  2:26     ` [PATCH v2] ptp: fix the race between the release of ptp_clock and cdev Vladis Dronov
2019-12-27 15:02       ` Richard Cochran
2019-12-27 17:24         ` Vladis Dronov
2019-12-31  4:19       ` David Miller

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=20191208195340.GX4203@ZenIV.linux.org.uk \
    --to=viro@zeniv.linux.org.uk \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=richardcochran@gmail.com \
    --cc=vdronov@redhat.com \
    /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;
as well as URLs for NNTP newsgroup(s).