@@ -11,7 +11,8 @@ use crate::{
11
11
error:: { code:: * , Error , Result } ,
12
12
types:: { ARef , AlwaysRefCounted , NotThreadSafe , Opaque } ,
13
13
} ;
14
- use core:: ptr;
14
+ use alloc:: boxed:: Box ;
15
+ use core:: { alloc:: AllocError , mem, ptr} ;
15
16
16
17
/// Flags associated with a [`File`].
17
18
pub mod flags {
@@ -313,6 +314,187 @@ impl Drop for FileDescriptorReservation {
313
314
}
314
315
}
315
316
317
+ /// Helper used for closing file descriptors in a way that is safe even if the file is currently
318
+ /// held using `fdget`.
319
+ ///
320
+ /// Additional motivation can be found in commit 80cd795630d6 ("binder: fix use-after-free due to
321
+ /// ksys_close() during fdget()") and in the comments on `binder_do_fd_close`.
322
+ pub struct DeferredFdCloser {
323
+ inner : Box < DeferredFdCloserInner > ,
324
+ }
325
+
326
+ /// SAFETY: This just holds an allocation with no real content, so there's no safety issue with
327
+ /// moving it across threads.
328
+ unsafe impl Send for DeferredFdCloser { }
329
+ unsafe impl Sync for DeferredFdCloser { }
330
+
331
+ /// # Invariants
332
+ ///
333
+ /// If the `file` pointer is non-null, then it points at a `struct file` and owns a refcount to
334
+ /// that file.
335
+ #[ repr( C ) ]
336
+ struct DeferredFdCloserInner {
337
+ twork : mem:: MaybeUninit < bindings:: callback_head > ,
338
+ file : * mut bindings:: file ,
339
+ }
340
+
341
+ impl DeferredFdCloser {
342
+ /// Create a new [`DeferredFdCloser`].
343
+ pub fn new ( ) -> Result < Self , AllocError > {
344
+ Ok ( Self {
345
+ // INVARIANT: The `file` pointer is null, so the type invariant does not apply.
346
+ inner : Box :: try_new ( DeferredFdCloserInner {
347
+ twork : mem:: MaybeUninit :: uninit ( ) ,
348
+ file : core:: ptr:: null_mut ( ) ,
349
+ } ) ?,
350
+ } )
351
+ }
352
+
353
+ /// Schedule a task work that closes the file descriptor when this task returns to userspace.
354
+ ///
355
+ /// Fails if this is called from a context where we cannot run work when returning to
356
+ /// userspace. (E.g., from a kthread.)
357
+ pub fn close_fd ( self , fd : u32 ) -> Result < ( ) , DeferredFdCloseError > {
358
+ use bindings:: task_work_notify_mode_TWA_RESUME as TWA_RESUME ;
359
+
360
+ // In this method, we schedule the task work before closing the file. This is because
361
+ // scheduling a task work is fallible, and we need to know whether it will fail before we
362
+ // attempt to close the file.
363
+
364
+ // Task works are not available on kthreads.
365
+ let current = crate :: current!( ) ;
366
+ if current. is_kthread ( ) {
367
+ return Err ( DeferredFdCloseError :: TaskWorkUnavailable ) ;
368
+ }
369
+
370
+ // Transfer ownership of the box's allocation to a raw pointer. This disables the
371
+ // destructor, so we must manually convert it back to a Box to drop it.
372
+ //
373
+ // Until we convert it back to a `Box`, there are no aliasing requirements on this
374
+ // pointer.
375
+ let inner = Box :: into_raw ( self . inner ) ;
376
+
377
+ // The `callback_head` field is first in the struct, so this cast correctly gives us a
378
+ // pointer to the field.
379
+ let callback_head = inner. cast :: < bindings:: callback_head > ( ) ;
380
+ // SAFETY: This pointer offset operation does not go out-of-bounds.
381
+ let file_field = unsafe { core:: ptr:: addr_of_mut!( ( * inner) . file) } ;
382
+
383
+ let current = current. as_raw ( ) ;
384
+
385
+ // SAFETY: This function currently has exclusive access to the `DeferredFdCloserInner`, so
386
+ // it is okay for us to perform unsynchronized writes to its `callback_head` field.
387
+ unsafe { bindings:: init_task_work ( callback_head, Some ( Self :: do_close_fd) ) } ;
388
+
389
+ // SAFETY: This inserts the `DeferredFdCloserInner` into the task workqueue for the current
390
+ // task. If this operation is successful, then this transfers exclusive ownership of the
391
+ // `callback_head` field to the C side until it calls `do_close_fd`, and we don't touch or
392
+ // invalidate the field during that time.
393
+ //
394
+ // When the C side calls `do_close_fd`, the safety requirements of that method are
395
+ // satisfied because when a task work is executed, the callback is given ownership of the
396
+ // pointer.
397
+ //
398
+ // The file pointer is currently null. If it is changed to be non-null before `do_close_fd`
399
+ // is called, then that change happens due to the write at the end of this function, and
400
+ // that write has a safety comment that explains why the refcount can be dropped when
401
+ // `do_close_fd` runs.
402
+ let res = unsafe { bindings:: task_work_add ( current, callback_head, TWA_RESUME ) } ;
403
+
404
+ if res != 0 {
405
+ // SAFETY: Scheduling the task work failed, so we still have ownership of the box, so
406
+ // we may destroy it.
407
+ unsafe { drop ( Box :: from_raw ( inner) ) } ;
408
+
409
+ return Err ( DeferredFdCloseError :: TaskWorkUnavailable ) ;
410
+ }
411
+
412
+ // This removes the fd from the fd table in `current`. The file is not fully closed until
413
+ // `filp_close` is called. We are given ownership of one refcount to the file.
414
+ //
415
+ // SAFETY: This is safe no matter what `fd` is. If the `fd` is valid (that is, if the
416
+ // pointer is non-null), then we call `filp_close` on the returned pointer as required by
417
+ // `file_close_fd`.
418
+ let file = unsafe { bindings:: file_close_fd ( fd) } ;
419
+ if file. is_null ( ) {
420
+ // We don't clean up the task work since that might be expensive if the task work queue
421
+ // is long. Just let it execute and let it clean up for itself.
422
+ return Err ( DeferredFdCloseError :: BadFd ) ;
423
+ }
424
+
425
+ // Acquire a second refcount to the file.
426
+ //
427
+ // SAFETY: The `file` pointer points at a file with a non-zero refcount.
428
+ unsafe { bindings:: get_file ( file) } ;
429
+
430
+ // This method closes the fd, consuming one of our two refcounts. There could be active
431
+ // light refcounts created from that fd, so we must ensure that the file has a positive
432
+ // refcount for the duration of those active light refcounts. We do that by holding on to
433
+ // the second refcount until the current task returns to userspace.
434
+ //
435
+ // SAFETY: The `file` pointer is valid. Passing `current->files` as the file table to close
436
+ // it in is correct, since we just got the `fd` from `file_close_fd` which also uses
437
+ // `current->files`.
438
+ //
439
+ // Note: fl_owner_t is currently a void pointer.
440
+ unsafe { bindings:: filp_close ( file, ( * current) . files as bindings:: fl_owner_t ) } ;
441
+
442
+ // We update the file pointer that the task work is supposed to fput. This transfers
443
+ // ownership of our last refcount.
444
+ //
445
+ // INVARIANT: This changes the `file` field of a `DeferredFdCloserInner` from null to
446
+ // non-null. This doesn't break the type invariant for `DeferredFdCloserInner` because we
447
+ // still own a refcount to the file, so we can pass ownership of that refcount to the
448
+ // `DeferredFdCloserInner`.
449
+ //
450
+ // When `do_close_fd` runs, it must be safe for it to `fput` the refcount. However, this is
451
+ // the case because all light refcounts that are associated with the fd we closed
452
+ // previously must be dropped when `do_close_fd`, since light refcounts must be dropped
453
+ // before returning to userspace.
454
+ //
455
+ // SAFETY: Task works are executed on the current thread right before we return to
456
+ // userspace, so this write is guaranteed to happen before `do_close_fd` is called, which
457
+ // means that a race is not possible here.
458
+ unsafe { * file_field = file } ;
459
+
460
+ Ok ( ( ) )
461
+ }
462
+
463
+ /// # Safety
464
+ ///
465
+ /// The provided pointer must point at the `twork` field of a `DeferredFdCloserInner` stored in
466
+ /// a `Box`, and the caller must pass exclusive ownership of that `Box`. Furthermore, if the
467
+ /// file pointer is non-null, then it must be okay to release the refcount by calling `fput`.
468
+ unsafe extern "C" fn do_close_fd ( inner : * mut bindings:: callback_head ) {
469
+ // SAFETY: The caller just passed us ownership of this box.
470
+ let inner = unsafe { Box :: from_raw ( inner. cast :: < DeferredFdCloserInner > ( ) ) } ;
471
+ if !inner. file . is_null ( ) {
472
+ // SAFETY: By the type invariants, we own a refcount to this file, and the caller
473
+ // guarantees that dropping the refcount now is okay.
474
+ unsafe { bindings:: fput ( inner. file ) } ;
475
+ }
476
+ // The allocation is freed when `inner` goes out of scope.
477
+ }
478
+ }
479
+
480
+ /// Represents a failure to close an fd in a deferred manner.
481
+ #[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
482
+ pub enum DeferredFdCloseError {
483
+ /// Closing the fd failed because we were unable to schedule a task work.
484
+ TaskWorkUnavailable ,
485
+ /// Closing the fd failed because the fd does not exist.
486
+ BadFd ,
487
+ }
488
+
489
+ impl From < DeferredFdCloseError > for Error {
490
+ fn from ( err : DeferredFdCloseError ) -> Error {
491
+ match err {
492
+ DeferredFdCloseError :: TaskWorkUnavailable => ESRCH ,
493
+ DeferredFdCloseError :: BadFd => EBADF ,
494
+ }
495
+ }
496
+ }
497
+
316
498
/// Represents the `EBADF` error code.
317
499
///
318
500
/// Used for methods that can only fail with `EBADF`.
0 commit comments