|
6 | 6 | //!
|
7 | 7 | //! Reference: <https://www.kernel.org/doc/html/latest/security/credentials.html>
|
8 | 8 |
|
9 |
| -use crate::bindings; |
10 |
| -use core::{marker::PhantomData, mem::ManuallyDrop, ops::Deref}; |
| 9 | +use crate::{bindings, AlwaysRefCounted}; |
| 10 | +use core::cell::UnsafeCell; |
11 | 11 |
|
12 | 12 | /// Wraps the kernel's `struct cred`.
|
13 | 13 | ///
|
14 | 14 | /// # Invariants
|
15 | 15 | ///
|
16 |
| -/// The pointer `Credential::ptr` is non-null and valid. Its reference count is also non-zero. |
17 |
| -pub struct Credential { |
18 |
| - pub(crate) ptr: *const bindings::cred, |
19 |
| -} |
20 |
| - |
21 |
| -impl Clone for Credential { |
22 |
| - fn clone(&self) -> Self { |
23 |
| - // SAFETY: The type invariants guarantee that `self.ptr` has a non-zero reference count. |
24 |
| - let ptr = unsafe { bindings::get_cred(self.ptr) }; |
25 |
| - |
26 |
| - // INVARIANT: We incremented the reference count to account for the new `Credential` being |
27 |
| - // created. |
28 |
| - Self { ptr } |
29 |
| - } |
30 |
| -} |
31 |
| - |
32 |
| -impl Drop for Credential { |
33 |
| - fn drop(&mut self) { |
34 |
| - // SAFETY: The type invariants guarantee that `ptr` has a non-zero reference count. |
35 |
| - unsafe { bindings::put_cred(self.ptr) }; |
36 |
| - } |
37 |
| -} |
| 16 | +/// Instances of this type are always ref-counted, that is, a call to `get_cred` ensures that the |
| 17 | +/// allocation remains valid at least until the matching call to `put_cred`. |
| 18 | +#[repr(transparent)] |
| 19 | +pub struct Credential(pub(crate) UnsafeCell<bindings::cred>); |
38 | 20 |
|
39 |
| -/// A wrapper for [`Credential`] that doesn't automatically decrement the refcount when dropped. |
40 |
| -/// |
41 |
| -/// We need the wrapper because [`ManuallyDrop`] alone would allow callers to call |
42 |
| -/// [`ManuallyDrop::into_inner`]. This would allow an unsafe sequence to be triggered without |
43 |
| -/// `unsafe` blocks because it would trigger an unbalanced call to `put_cred`. |
44 |
| -/// |
45 |
| -/// # Invariants |
46 |
| -/// |
47 |
| -/// The wrapped [`Credential`] remains valid for the lifetime of the object. |
48 |
| -pub struct CredentialRef<'a> { |
49 |
| - cred: ManuallyDrop<Credential>, |
50 |
| - _p: PhantomData<&'a ()>, |
51 |
| -} |
52 |
| - |
53 |
| -impl CredentialRef<'_> { |
54 |
| - /// Constructs a new [`struct cred`] wrapper that doesn't change its reference count. |
| 21 | +impl Credential { |
| 22 | + /// Creates a reference to a [`Credential`] from a valid pointer. |
55 | 23 | ///
|
56 | 24 | /// # Safety
|
57 | 25 | ///
|
58 |
| - /// The pointer `ptr` must be non-null and valid for the lifetime of the object. |
59 |
| - pub(crate) unsafe fn from_ptr(ptr: *const bindings::cred) -> Self { |
60 |
| - Self { |
61 |
| - cred: ManuallyDrop::new(Credential { ptr }), |
62 |
| - _p: PhantomData, |
63 |
| - } |
| 26 | + /// The caller must ensure that `ptr` is valid and remains valid for the lifetime of the |
| 27 | + /// returned [`Credential`] reference. |
| 28 | + pub(crate) unsafe fn from_ptr<'a>(ptr: *const bindings::cred) -> &'a Self { |
| 29 | + // SAFETY: The safety requirements guarantee the validity of the dereference, while the |
| 30 | + // `Credential` type being transparent makes the cast ok. |
| 31 | + unsafe { &*ptr.cast() } |
64 | 32 | }
|
65 | 33 | }
|
66 | 34 |
|
67 |
| -impl Deref for CredentialRef<'_> { |
68 |
| - type Target = Credential; |
| 35 | +// SAFETY: The type invariants guarantee that `Credential` is always ref-counted. |
| 36 | +unsafe impl AlwaysRefCounted for Credential { |
| 37 | + fn inc_ref(&self) { |
| 38 | + // SAFETY: The existence of a shared reference means that the refcount is nonzero. |
| 39 | + unsafe { bindings::get_cred(self.0.get()) }; |
| 40 | + } |
69 | 41 |
|
70 |
| - fn deref(&self) -> &Self::Target { |
71 |
| - self.cred.deref() |
| 42 | + unsafe fn dec_ref(obj: core::ptr::NonNull<Self>) { |
| 43 | + // SAFETY: The safety requirements guarantee that the refcount is nonzero. |
| 44 | + unsafe { bindings::put_cred(obj.cast().as_ptr()) }; |
72 | 45 | }
|
73 | 46 | }
|
0 commit comments