xref: /linux/drivers/android/binder/deferred_close.rs (revision 4f38da1f027ea2c9f01bb71daa7a299c191b6940)
1*eafedbc7SAlice Ryhl // SPDX-License-Identifier: GPL-2.0
2*eafedbc7SAlice Ryhl 
3*eafedbc7SAlice Ryhl // Copyright (C) 2025 Google LLC.
4*eafedbc7SAlice Ryhl 
5*eafedbc7SAlice Ryhl //! Logic for closing files in a deferred manner.
6*eafedbc7SAlice Ryhl //!
7*eafedbc7SAlice Ryhl //! This file could make sense to have in `kernel::fs`, but it was rejected for being too
8*eafedbc7SAlice Ryhl //! Binder-specific.
9*eafedbc7SAlice Ryhl 
10*eafedbc7SAlice Ryhl use core::mem::MaybeUninit;
11*eafedbc7SAlice Ryhl use kernel::{
12*eafedbc7SAlice Ryhl     alloc::{AllocError, Flags},
13*eafedbc7SAlice Ryhl     bindings,
14*eafedbc7SAlice Ryhl     prelude::*,
15*eafedbc7SAlice Ryhl };
16*eafedbc7SAlice Ryhl 
17*eafedbc7SAlice Ryhl /// Helper used for closing file descriptors in a way that is safe even if the file is currently
18*eafedbc7SAlice Ryhl /// held using `fdget`.
19*eafedbc7SAlice Ryhl ///
20*eafedbc7SAlice Ryhl /// Additional motivation can be found in commit 80cd795630d6 ("binder: fix use-after-free due to
21*eafedbc7SAlice Ryhl /// ksys_close() during fdget()") and in the comments on `binder_do_fd_close`.
22*eafedbc7SAlice Ryhl pub(crate) struct DeferredFdCloser {
23*eafedbc7SAlice Ryhl     inner: KBox<DeferredFdCloserInner>,
24*eafedbc7SAlice Ryhl }
25*eafedbc7SAlice Ryhl 
26*eafedbc7SAlice Ryhl /// SAFETY: This just holds an allocation with no real content, so there's no safety issue with
27*eafedbc7SAlice Ryhl /// moving it across threads.
28*eafedbc7SAlice Ryhl unsafe impl Send for DeferredFdCloser {}
29*eafedbc7SAlice Ryhl /// SAFETY: This just holds an allocation with no real content, so there's no safety issue with
30*eafedbc7SAlice Ryhl /// moving it across threads.
31*eafedbc7SAlice Ryhl unsafe impl Sync for DeferredFdCloser {}
32*eafedbc7SAlice Ryhl 
33*eafedbc7SAlice Ryhl /// # Invariants
34*eafedbc7SAlice Ryhl ///
35*eafedbc7SAlice Ryhl /// If the `file` pointer is non-null, then it points at a `struct file` and owns a refcount to
36*eafedbc7SAlice Ryhl /// that file.
37*eafedbc7SAlice Ryhl #[repr(C)]
38*eafedbc7SAlice Ryhl struct DeferredFdCloserInner {
39*eafedbc7SAlice Ryhl     twork: MaybeUninit<bindings::callback_head>,
40*eafedbc7SAlice Ryhl     file: *mut bindings::file,
41*eafedbc7SAlice Ryhl }
42*eafedbc7SAlice Ryhl 
43*eafedbc7SAlice Ryhl impl DeferredFdCloser {
44*eafedbc7SAlice Ryhl     /// Create a new [`DeferredFdCloser`].
45*eafedbc7SAlice Ryhl     pub(crate) fn new(flags: Flags) -> Result<Self, AllocError> {
46*eafedbc7SAlice Ryhl         Ok(Self {
47*eafedbc7SAlice Ryhl             // INVARIANT: The `file` pointer is null, so the type invariant does not apply.
48*eafedbc7SAlice Ryhl             inner: KBox::new(
49*eafedbc7SAlice Ryhl                 DeferredFdCloserInner {
50*eafedbc7SAlice Ryhl                     twork: MaybeUninit::uninit(),
51*eafedbc7SAlice Ryhl                     file: core::ptr::null_mut(),
52*eafedbc7SAlice Ryhl                 },
53*eafedbc7SAlice Ryhl                 flags,
54*eafedbc7SAlice Ryhl             )?,
55*eafedbc7SAlice Ryhl         })
56*eafedbc7SAlice Ryhl     }
57*eafedbc7SAlice Ryhl 
58*eafedbc7SAlice Ryhl     /// Schedule a task work that closes the file descriptor when this task returns to userspace.
59*eafedbc7SAlice Ryhl     ///
60*eafedbc7SAlice Ryhl     /// Fails if this is called from a context where we cannot run work when returning to
61*eafedbc7SAlice Ryhl     /// userspace. (E.g., from a kthread.)
62*eafedbc7SAlice Ryhl     pub(crate) fn close_fd(self, fd: u32) -> Result<(), DeferredFdCloseError> {
63*eafedbc7SAlice Ryhl         use bindings::task_work_notify_mode_TWA_RESUME as TWA_RESUME;
64*eafedbc7SAlice Ryhl 
65*eafedbc7SAlice Ryhl         // In this method, we schedule the task work before closing the file. This is because
66*eafedbc7SAlice Ryhl         // scheduling a task work is fallible, and we need to know whether it will fail before we
67*eafedbc7SAlice Ryhl         // attempt to close the file.
68*eafedbc7SAlice Ryhl 
69*eafedbc7SAlice Ryhl         // Task works are not available on kthreads.
70*eafedbc7SAlice Ryhl         let current = kernel::current!();
71*eafedbc7SAlice Ryhl 
72*eafedbc7SAlice Ryhl         // Check if this is a kthread.
73*eafedbc7SAlice Ryhl         // SAFETY: Reading `flags` from a task is always okay.
74*eafedbc7SAlice Ryhl         if unsafe { ((*current.as_ptr()).flags & bindings::PF_KTHREAD) != 0 } {
75*eafedbc7SAlice Ryhl             return Err(DeferredFdCloseError::TaskWorkUnavailable);
76*eafedbc7SAlice Ryhl         }
77*eafedbc7SAlice Ryhl 
78*eafedbc7SAlice Ryhl         // Transfer ownership of the box's allocation to a raw pointer. This disables the
79*eafedbc7SAlice Ryhl         // destructor, so we must manually convert it back to a KBox to drop it.
80*eafedbc7SAlice Ryhl         //
81*eafedbc7SAlice Ryhl         // Until we convert it back to a `KBox`, there are no aliasing requirements on this
82*eafedbc7SAlice Ryhl         // pointer.
83*eafedbc7SAlice Ryhl         let inner = KBox::into_raw(self.inner);
84*eafedbc7SAlice Ryhl 
85*eafedbc7SAlice Ryhl         // The `callback_head` field is first in the struct, so this cast correctly gives us a
86*eafedbc7SAlice Ryhl         // pointer to the field.
87*eafedbc7SAlice Ryhl         let callback_head = inner.cast::<bindings::callback_head>();
88*eafedbc7SAlice Ryhl         // SAFETY: This pointer offset operation does not go out-of-bounds.
89*eafedbc7SAlice Ryhl         let file_field = unsafe { core::ptr::addr_of_mut!((*inner).file) };
90*eafedbc7SAlice Ryhl 
91*eafedbc7SAlice Ryhl         let current = current.as_ptr();
92*eafedbc7SAlice Ryhl 
93*eafedbc7SAlice Ryhl         // SAFETY: This function currently has exclusive access to the `DeferredFdCloserInner`, so
94*eafedbc7SAlice Ryhl         // it is okay for us to perform unsynchronized writes to its `callback_head` field.
95*eafedbc7SAlice Ryhl         unsafe { bindings::init_task_work(callback_head, Some(Self::do_close_fd)) };
96*eafedbc7SAlice Ryhl 
97*eafedbc7SAlice Ryhl         // SAFETY: This inserts the `DeferredFdCloserInner` into the task workqueue for the current
98*eafedbc7SAlice Ryhl         // task. If this operation is successful, then this transfers exclusive ownership of the
99*eafedbc7SAlice Ryhl         // `callback_head` field to the C side until it calls `do_close_fd`, and we don't touch or
100*eafedbc7SAlice Ryhl         // invalidate the field during that time.
101*eafedbc7SAlice Ryhl         //
102*eafedbc7SAlice Ryhl         // When the C side calls `do_close_fd`, the safety requirements of that method are
103*eafedbc7SAlice Ryhl         // satisfied because when a task work is executed, the callback is given ownership of the
104*eafedbc7SAlice Ryhl         // pointer.
105*eafedbc7SAlice Ryhl         //
106*eafedbc7SAlice Ryhl         // The file pointer is currently null. If it is changed to be non-null before `do_close_fd`
107*eafedbc7SAlice Ryhl         // is called, then that change happens due to the write at the end of this function, and
108*eafedbc7SAlice Ryhl         // that write has a safety comment that explains why the refcount can be dropped when
109*eafedbc7SAlice Ryhl         // `do_close_fd` runs.
110*eafedbc7SAlice Ryhl         let res = unsafe { bindings::task_work_add(current, callback_head, TWA_RESUME) };
111*eafedbc7SAlice Ryhl 
112*eafedbc7SAlice Ryhl         if res != 0 {
113*eafedbc7SAlice Ryhl             // SAFETY: Scheduling the task work failed, so we still have ownership of the box, so
114*eafedbc7SAlice Ryhl             // we may destroy it.
115*eafedbc7SAlice Ryhl             unsafe { drop(KBox::from_raw(inner)) };
116*eafedbc7SAlice Ryhl 
117*eafedbc7SAlice Ryhl             return Err(DeferredFdCloseError::TaskWorkUnavailable);
118*eafedbc7SAlice Ryhl         }
119*eafedbc7SAlice Ryhl 
120*eafedbc7SAlice Ryhl         // This removes the fd from the fd table in `current`. The file is not fully closed until
121*eafedbc7SAlice Ryhl         // `filp_close` is called. We are given ownership of one refcount to the file.
122*eafedbc7SAlice Ryhl         //
123*eafedbc7SAlice Ryhl         // SAFETY: This is safe no matter what `fd` is. If the `fd` is valid (that is, if the
124*eafedbc7SAlice Ryhl         // pointer is non-null), then we call `filp_close` on the returned pointer as required by
125*eafedbc7SAlice Ryhl         // `file_close_fd`.
126*eafedbc7SAlice Ryhl         let file = unsafe { bindings::file_close_fd(fd) };
127*eafedbc7SAlice Ryhl         if file.is_null() {
128*eafedbc7SAlice Ryhl             // We don't clean up the task work since that might be expensive if the task work queue
129*eafedbc7SAlice Ryhl             // is long. Just let it execute and let it clean up for itself.
130*eafedbc7SAlice Ryhl             return Err(DeferredFdCloseError::BadFd);
131*eafedbc7SAlice Ryhl         }
132*eafedbc7SAlice Ryhl 
133*eafedbc7SAlice Ryhl         // Acquire a second refcount to the file.
134*eafedbc7SAlice Ryhl         //
135*eafedbc7SAlice Ryhl         // SAFETY: The `file` pointer points at a file with a non-zero refcount.
136*eafedbc7SAlice Ryhl         unsafe { bindings::get_file(file) };
137*eafedbc7SAlice Ryhl 
138*eafedbc7SAlice Ryhl         // This method closes the fd, consuming one of our two refcounts. There could be active
139*eafedbc7SAlice Ryhl         // light refcounts created from that fd, so we must ensure that the file has a positive
140*eafedbc7SAlice Ryhl         // refcount for the duration of those active light refcounts. We do that by holding on to
141*eafedbc7SAlice Ryhl         // the second refcount until the current task returns to userspace.
142*eafedbc7SAlice Ryhl         //
143*eafedbc7SAlice Ryhl         // SAFETY: The `file` pointer is valid. Passing `current->files` as the file table to close
144*eafedbc7SAlice Ryhl         // it in is correct, since we just got the `fd` from `file_close_fd` which also uses
145*eafedbc7SAlice Ryhl         // `current->files`.
146*eafedbc7SAlice Ryhl         //
147*eafedbc7SAlice Ryhl         // Note: fl_owner_t is currently a void pointer.
148*eafedbc7SAlice Ryhl         unsafe { bindings::filp_close(file, (*current).files as bindings::fl_owner_t) };
149*eafedbc7SAlice Ryhl 
150*eafedbc7SAlice Ryhl         // We update the file pointer that the task work is supposed to fput. This transfers
151*eafedbc7SAlice Ryhl         // ownership of our last refcount.
152*eafedbc7SAlice Ryhl         //
153*eafedbc7SAlice Ryhl         // INVARIANT: This changes the `file` field of a `DeferredFdCloserInner` from null to
154*eafedbc7SAlice Ryhl         // non-null. This doesn't break the type invariant for `DeferredFdCloserInner` because we
155*eafedbc7SAlice Ryhl         // still own a refcount to the file, so we can pass ownership of that refcount to the
156*eafedbc7SAlice Ryhl         // `DeferredFdCloserInner`.
157*eafedbc7SAlice Ryhl         //
158*eafedbc7SAlice Ryhl         // When `do_close_fd` runs, it must be safe for it to `fput` the refcount. However, this is
159*eafedbc7SAlice Ryhl         // the case because all light refcounts that are associated with the fd we closed
160*eafedbc7SAlice Ryhl         // previously must be dropped when `do_close_fd`, since light refcounts must be dropped
161*eafedbc7SAlice Ryhl         // before returning to userspace.
162*eafedbc7SAlice Ryhl         //
163*eafedbc7SAlice Ryhl         // SAFETY: Task works are executed on the current thread right before we return to
164*eafedbc7SAlice Ryhl         // userspace, so this write is guaranteed to happen before `do_close_fd` is called, which
165*eafedbc7SAlice Ryhl         // means that a race is not possible here.
166*eafedbc7SAlice Ryhl         unsafe { *file_field = file };
167*eafedbc7SAlice Ryhl 
168*eafedbc7SAlice Ryhl         Ok(())
169*eafedbc7SAlice Ryhl     }
170*eafedbc7SAlice Ryhl 
171*eafedbc7SAlice Ryhl     /// # Safety
172*eafedbc7SAlice Ryhl     ///
173*eafedbc7SAlice Ryhl     /// The provided pointer must point at the `twork` field of a `DeferredFdCloserInner` stored in
174*eafedbc7SAlice Ryhl     /// a `KBox`, and the caller must pass exclusive ownership of that `KBox`. Furthermore, if the
175*eafedbc7SAlice Ryhl     /// file pointer is non-null, then it must be okay to release the refcount by calling `fput`.
176*eafedbc7SAlice Ryhl     unsafe extern "C" fn do_close_fd(inner: *mut bindings::callback_head) {
177*eafedbc7SAlice Ryhl         // SAFETY: The caller just passed us ownership of this box.
178*eafedbc7SAlice Ryhl         let inner = unsafe { KBox::from_raw(inner.cast::<DeferredFdCloserInner>()) };
179*eafedbc7SAlice Ryhl         if !inner.file.is_null() {
180*eafedbc7SAlice Ryhl             // SAFETY: By the type invariants, we own a refcount to this file, and the caller
181*eafedbc7SAlice Ryhl             // guarantees that dropping the refcount now is okay.
182*eafedbc7SAlice Ryhl             unsafe { bindings::fput(inner.file) };
183*eafedbc7SAlice Ryhl         }
184*eafedbc7SAlice Ryhl         // The allocation is freed when `inner` goes out of scope.
185*eafedbc7SAlice Ryhl     }
186*eafedbc7SAlice Ryhl }
187*eafedbc7SAlice Ryhl 
188*eafedbc7SAlice Ryhl /// Represents a failure to close an fd in a deferred manner.
189*eafedbc7SAlice Ryhl #[derive(Copy, Clone, Debug, Eq, PartialEq)]
190*eafedbc7SAlice Ryhl pub(crate) enum DeferredFdCloseError {
191*eafedbc7SAlice Ryhl     /// Closing the fd failed because we were unable to schedule a task work.
192*eafedbc7SAlice Ryhl     TaskWorkUnavailable,
193*eafedbc7SAlice Ryhl     /// Closing the fd failed because the fd does not exist.
194*eafedbc7SAlice Ryhl     BadFd,
195*eafedbc7SAlice Ryhl }
196*eafedbc7SAlice Ryhl 
197*eafedbc7SAlice Ryhl impl From<DeferredFdCloseError> for Error {
198*eafedbc7SAlice Ryhl     fn from(err: DeferredFdCloseError) -> Error {
199*eafedbc7SAlice Ryhl         match err {
200*eafedbc7SAlice Ryhl             DeferredFdCloseError::TaskWorkUnavailable => ESRCH,
201*eafedbc7SAlice Ryhl             DeferredFdCloseError::BadFd => EBADF,
202*eafedbc7SAlice Ryhl         }
203*eafedbc7SAlice Ryhl     }
204*eafedbc7SAlice Ryhl }
205