From: Al Viro <viro@zeniv.linux.org.uk>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Christian Brauner" <brauner@kernel.org>,
"Georgios Androutsopoulos" <georgeandrout13@gmail.com>,
"Miguel Ojeda" <ojeda@kernel.org>, "Jan Kara" <jack@suse.cz>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>,
linux-fsdevel@vger.kernel.org, rust-for-linux@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] rust: file: handle fd table teardown in file descriptor APIs
Date: Tue, 29 Sep 2026 14:28:39 +0100 [thread overview]
Message-ID: <20260929132839.GA989762@ZenIV> (raw)
In-Reply-To: <art7swUNAa3AyUrL@google.com>
On Tue, Sep 29, 2026 at 08:49:55AM +0000, Alice Ryhl wrote:
> So, I previously wrote some code that could invoke filp_close() to close
> a given fd ... from a workqueue. This was in the scenario where the
> process dies and the usual cleanup function gets called deferred from a
> workqueue instead of from the ioctl like usual.
Huh?
1) filp_close(file, NULL) doesn't do _anything_ to any descriptor tables;
the only requirements are that it should happen in _some_ thread context
(workqueue is fine) and that caller should not be holding any locks that
might be taken by ->flush() of the file in question (for a workqueue
callback it's fine as long as the callback itself is not holding any
of those).
2) any caller of filp_close(file, files_struct) must obviously guarantee
that files_struct won't be freed under it; passing current->files from
workqueue is safe in that respect, but obviously bogus. Note that
descriptor table in question will *still* not be accessed; it serves
only as an opaque tag that identifies POSIX locks (and dnotify_struct
instances) related to the descriptor table in question. IF you have
just manually removed the file in question from descriptor table
(file_close_fd()), you must call filp_close() passing it the same
descriptor table while that descriptor table is still guaranteed to
be alive.
Rationale is memory safety, actually - for POSIX locks descriptor table
serves as lock owner; the reference is opaque, but we don't want to have
it outlive freeing and reuse of the object it's pointing to. So anything
that removes some file reference from a descriptor table is responsible
for corresponding filp_close() call done *before* the descriptor table
is gone.
Note that we only need to take care of the reference we remove from
descriptor table; files_struct destructor will call filp_close() for
anything still referenced from it.
Places where file reference is removed from the table:
* do_close_on_exec(); filp_close() called in the same loop as
clearing the descriptor table slot.
* do_dup2() in case the new slot had already been in use;
filp_close() called just before return.
* file_close_fd_locked() callers. Three of those call filp_close()
as soon as they drop ->files_lock (close_fd(), __range_close() and
io_uring io_close()). Remaining caller (close_fd_locked()) leaves that to
_its_ callers (binder_deferred_fd_close() and its equivalent Rust-side).
Again, normally both removal from descriptor table and filp_close() are
done by the same primitive...
> In this case the correct behavior was just to do nothing.
... leaking an opened file? IDGI...
> It was a very easy mistake to make, and if such mistakes lead to null
> ptr derefs or worse, then I think it's worth doing something to reduce
> the bad consequences from this kind of mistake.
next prev parent reply other threads:[~2026-09-29 13:28 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-23 2:23 Georgios Androutsopoulos
2026-09-24 8:39 ` Alice Ryhl
2026-09-24 16:06 ` Georgios Androutsopoulos
2026-09-24 16:18 ` Pedro Falcato
2026-09-25 16:00 ` Christian Brauner
2026-09-29 4:48 ` Al Viro
2026-09-29 8:49 ` Alice Ryhl
2026-09-29 13:28 ` Al Viro [this message]
2026-09-29 13:35 ` Alice Ryhl
2026-09-29 14:53 ` Al Viro
2026-09-29 12:24 ` Gary Guo
2026-09-29 13:51 ` Al Viro
2026-09-29 16:07 ` Gary Guo
2026-09-29 17:02 ` Al Viro
2026-09-29 18:22 ` Gary Guo
2026-09-29 19:47 ` Al Viro
2026-09-28 10:42 ` kernel test robot
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=20260929132839.GA989762@ZenIV \
--to=viro@zeniv.linux.org.uk \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=brauner@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=georgeandrout13@gmail.com \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/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
all inboxes | Powered by JetHome®