On Wed, Mar 20, 2024 at 3:22 PM Christian Brauner <brauner@xxxxxxxxxx> wrote: > > On Fri, Feb 09, 2024 at 11:18:21AM +0000, Alice Ryhl wrote: > > > > +/// Helper used for closing file descriptors in a way that is safe even if the file is currently > > +/// held using `fdget`. > > +/// > > +/// Additional motivation can be found in commit 80cd795630d6 ("binder: fix use-after-free due to > > +/// ksys_close() during fdget()") and in the comments on `binder_do_fd_close`. > > +pub struct DeferredFdCloser { > > + inner: Box<DeferredFdCloserInner>, > > +} > > + > > +/// SAFETY: This just holds an allocation with no real content, so there's no safety issue with > > +/// moving it across threads. > > +unsafe impl Send for DeferredFdCloser {} > > +unsafe impl Sync for DeferredFdCloser {} > > + > > +/// # Invariants > > +/// > > +/// If the `file` pointer is non-null, then it points at a `struct file` and owns a refcount to > > +/// that file. > > +#[repr(C)] > > +struct DeferredFdCloserInner { > > + twork: mem::MaybeUninit<bindings::callback_head>, > > + file: *mut bindings::file, > > +} > > + > > +impl DeferredFdCloser { > > So the explicitly deferred close is due to how binder works so it's not > much of a general purpose interface as I don't recall having other > codepaths with similar problems. So this should live in the binder > specific rust code imo. Hmm. Are there really no other ioctls that call ksys_close on a user-provided fd? As far as I can tell, this kind of deferred API is the only way for us to provide a fully safe Rust api for closing an fd. Directly calling ksys_close must unsafely assert that the fd does not have an active fdget call. So it makes sense to me as an API that others might want to use. Still I can move it elsewhere if necessary. Alice