From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Arnd Bergmann" <arnd@arndb.de>,
"Alexander Viro" <viro@zeniv.linux.org.uk>,
"Christian Brauner" <brauner@kernel.org>,
"Jan Kara" <jack@suse.cz>, "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>, "Lee Jones" <lee@kernel.org>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org
Subject: Re: [PATCH v2 2/2] rust: miscdevice: access the `struct miscdevice` from fops->open()
Date: Mon, 9 Dec 2024 13:08:28 +0100 [thread overview]
Message-ID: <2024120939-aide-epidermal-076e@gregkh> (raw)
In-Reply-To: <CAH5fLgjO50OsNb7sYd8fY4VNoHOzX40w3oH-24uqkuL3Ga4iVQ@mail.gmail.com>
On Mon, Dec 09, 2024 at 01:00:05PM +0100, Alice Ryhl wrote:
> On Mon, Dec 9, 2024 at 12:53 PM Greg Kroah-Hartman
> <gregkh@linuxfoundation.org> wrote:
> >
> > On Mon, Dec 09, 2024 at 12:38:32PM +0100, Alice Ryhl wrote:
> > > On Mon, Dec 9, 2024 at 12:10 PM Greg Kroah-Hartman
> > > <gregkh@linuxfoundation.org> wrote:
> > > >
> > > > On Mon, Dec 09, 2024 at 11:50:57AM +0100, Alice Ryhl wrote:
> > > > > On Mon, Dec 9, 2024 at 9:48 AM Greg Kroah-Hartman
> > > > > <gregkh@linuxfoundation.org> wrote:
> > > > > >
> > > > > > On Mon, Dec 09, 2024 at 07:27:47AM +0000, Alice Ryhl wrote:
> > > > > > > Providing access to the underlying `struct miscdevice` is useful for
> > > > > > > various reasons. For example, this allows you access the miscdevice's
> > > > > > > internal `struct device` for use with the `dev_*` printing macros.
> > > > > > >
> > > > > > > Note that since the underlying `struct miscdevice` could get freed at
> > > > > > > any point after the fops->open() call, only the open call is given
> > > > > > > access to it. To print from other calls, they should take a refcount on
> > > > > > > the device to keep it alive.
> > > > > >
> > > > > > The lifespan of the miscdevice is at least from open until close, so
> > > > > > it's safe for at least then (i.e. read/write/ioctl/etc.)
> > > > >
> > > > > How is that enforced? What happens if I call misc_deregister while
> > > > > there are open fds?
> > > >
> > > > You shouldn't be able to do that as the code that would be calling
> > > > misc_deregister() (i.e. in a module unload path) would not work because
> > > > the module reference count is incremented at this point in time due to
> > > > the file operation module reference.
> > >
> > > Oh .. so misc_deregister must only be called when the module is being unloaded?
> >
> > Traditionally yes, that's when it is called. Do you see it happening in
> > any other place in the kernel today?
>
> I had not looked, but I know that Binder allows dynamically creating
> and removing its devices at runtime. It happens to be the case that
> this is only supported when binderfs is used, which is when it doesn't
> use miscdevice, so technically Binder does not call misc_deregister()
> outside of module unload, but following its example it's not hard to
> imagine that such removals could happen.
That's why those are files and not misc devices :)
> > > > Yeah, it's a horrid hack, and one day we will put "real" revoke logic in
> > > > here to detach the misc device from the file operations if this were to
> > > > happen. It's a very very common anti-pattern that many subsystems have
> > > > that is a bug that we all have been talking about for a very very long
> > > > time. Wolfram even has a plan for how to fix it all up (see his Japan
> > > > LinuxCon talk from 2 years ago), but I don't think anyone is doing the
> > > > work on it :(
> > > >
> > > > The media and drm layers have internal hacks/work-arounds to try to
> > > > handle this issue, but luckily for us, the odds of a misc device being
> > > > dynamically removed from the system is pretty low.
> > > >
> > > > Once / if ever, we get the revoke type logic implemented, then we can
> > > > apply that to the misc device code and follow it through to the rust
> > > > side if needed.
> > >
> > > If dynamically deregistering is not safe, then we need to change the
> > > Rust abstractions to prevent it.
> >
> > Dynamically deregistering is not unsafe, it's just that I don't think
> > you will physically ever have the misc_deregister() path called if a
> > file handle is open. Same should be the case for rust code, it should
> > "just work" without any extra code to do so.
>
> Well, if I give files access to the struct miscdevice in all fops
> hooks, then deregistering does become unsafe since accessing it in an
> ioctl after deregistering would be a UAF. I'd like to prevent the user
> from doing that.
I don't think that the deregister would succeed in the vfs layer if an
open file reference was currently held, but I haven't tried that in a
long time.
If you can come up with a way to prevent that, wonderful, but I wouldn't
worry too much as again, this "should not" happen due to the file
reference count, and if it does, it's a major logic error on the
driver's part, just like we have today in C.
thanks,
greg k-h
next prev parent reply other threads:[~2024-12-09 12:08 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-09 7:27 [PATCH v2 0/2] Additional miscdevice fops parameters Alice Ryhl
2024-12-09 7:27 ` [PATCH v2 1/2] rust: miscdevice: access file in fops Alice Ryhl
2024-12-09 7:27 ` [PATCH v2 2/2] rust: miscdevice: access the `struct miscdevice` from fops->open() Alice Ryhl
2024-12-09 8:48 ` Greg Kroah-Hartman
2024-12-09 10:50 ` Alice Ryhl
2024-12-09 11:09 ` Greg Kroah-Hartman
2024-12-09 11:38 ` Alice Ryhl
2024-12-09 11:53 ` Greg Kroah-Hartman
2024-12-09 12:00 ` Alice Ryhl
2024-12-09 12:08 ` Greg Kroah-Hartman [this message]
2024-12-09 12:53 ` Alice Ryhl
2024-12-09 13:13 ` Greg Kroah-Hartman
2024-12-09 13:36 ` Alice Ryhl
2024-12-09 15:01 ` Danilo Krummrich
2024-12-09 15:04 ` Alice Ryhl
2024-12-09 15:11 ` Danilo Krummrich
2024-12-09 11:07 ` Danilo Krummrich
2024-12-09 11:17 ` Greg Kroah-Hartman
2024-12-09 11:36 ` Alice Ryhl
2024-12-09 14:42 ` kernel test robot
2024-12-09 8:43 ` [PATCH v2 0/2] Additional miscdevice fops parameters Greg Kroah-Hartman
2024-12-09 10:19 ` Miguel Ojeda
2024-12-09 10:44 ` Alice Ryhl
2024-12-09 20:06 ` Konstantin Ryabitsev
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=2024120939-aide-epidermal-076e@gregkh \
--to=gregkh@linuxfoundation.org \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=arnd@arndb.de \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=brauner@kernel.org \
--cc=gary@garyguo.net \
--cc=jack@suse.cz \
--cc=lee@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=viro@zeniv.linux.org.uk \
/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