Skip to content

Commit e0734ef

Browse files
authored
Merge pull request #573 from wedsonaf/cred
rust: add abstraction for credentials.
2 parents 9a15cd9 + ad7f801 commit e0734ef

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

rust/helpers.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,17 @@ void rust_helper_chained_irq_exit(struct irq_chip *chip,
402402
}
403403
EXPORT_SYMBOL_GPL(rust_helper_chained_irq_exit);
404404

405+
const struct cred *rust_helper_get_cred(const struct cred *cred)
406+
{
407+
return get_cred(cred);
408+
}
409+
EXPORT_SYMBOL_GPL(rust_helper_get_cred);
410+
411+
void rust_helper_put_cred(const struct cred *cred) {
412+
put_cred(cred);
413+
}
414+
EXPORT_SYMBOL_GPL(rust_helper_put_cred);
415+
405416
/* We use bindgen's --size_t-is-usize option to bind the C size_t type
406417
* as the Rust usize type, so we can use it in contexts where Rust
407418
* expects a usize like slice (array) indices. usize is defined to be

rust/kernel/cred.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Credentials management.
4+
//!
5+
//! C header: [`include/linux/cred.h`](../../../../include/linux/cred.h)
6+
//!
7+
//! Reference: <https://www.kernel.org/doc/html/latest/security/credentials.html>
8+
9+
use crate::bindings;
10+
use core::{marker::PhantomData, mem::ManuallyDrop, ops::Deref};
11+
12+
/// Wraps the kernel's `struct cred`.
13+
///
14+
/// # Invariants
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+
}
38+
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.
55+
///
56+
/// # Safety
57+
///
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+
}
64+
}
65+
}
66+
67+
impl Deref for CredentialRef<'_> {
68+
type Target = Credential;
69+
70+
fn deref(&self) -> &Self::Target {
71+
self.cred.deref()
72+
}
73+
}

rust/kernel/file.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! C headers: [`include/linux/fs.h`](../../../../include/linux/fs.h) and
66
//! [`include/linux/file.h`](../../../../include/linux/file.h)
77
8-
use crate::{bindings, error::Error, Result};
8+
use crate::{bindings, cred::CredentialRef, error::Error, Result};
99
use core::{mem::ManuallyDrop, ops::Deref};
1010

1111
/// Wraps the kernel's `struct file`.
@@ -44,6 +44,16 @@ impl File {
4444
// SAFETY: `File::ptr` is guaranteed to be valid by the type invariants.
4545
unsafe { (*self.ptr).f_flags & bindings::O_NONBLOCK == 0 }
4646
}
47+
48+
/// Returns the credentials of the task that originally opened the file.
49+
pub fn cred(&self) -> CredentialRef<'_> {
50+
// SAFETY: `File::ptr` is guaranteed to be valid by the type invariants.
51+
let ptr = unsafe { (*self.ptr).f_cred };
52+
// SAFETY: The lifetimes of `self` and `CredentialRef` are tied, so it is guaranteed that
53+
// the credential pointer remains valid (because the file is still alive, and it doesn't
54+
// change over the lifetime of a file).
55+
unsafe { CredentialRef::from_ptr(ptr) }
56+
}
4757
}
4858

4959
impl Drop for File {

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub mod amba;
4747
pub mod buffer;
4848
pub mod c_types;
4949
pub mod chrdev;
50+
pub mod cred;
5051
pub mod device;
5152
pub mod driver;
5253
mod error;

0 commit comments

Comments
 (0)