Skip to content

Commit 83b7658

Browse files
wedsonaffbq
authored andcommitted
rust: cred: add Rust abstraction for struct cred
Add a wrapper around `struct cred` called `Credential`, and provide functionality to get the `Credential` associated with a `File`. Rust Binder must check the credentials of processes when they attempt to perform various operations, and these checks usually take a `&Credential` as parameter. The security_binder_set_context_mgr function would be one example. This patch is necessary to access these security_* methods from Rust. Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> Co-developed-by: Alice Ryhl <aliceryhl@google.com> Signed-off-by: Alice Ryhl <aliceryhl@google.com> Link: https://lore.kernel.org/r/20231206-alice-file-v2-2-af617c0d9d94@google.com
1 parent 153f5cc commit 83b7658

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#include <kunit/test.h>
10+
#include <linux/cred.h>
1011
#include <linux/errname.h>
1112
#include <linux/file.h>
1213
#include <linux/fs.h>

rust/helpers.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <kunit/test-bug.h>
2424
#include <linux/bug.h>
2525
#include <linux/build_bug.h>
26+
#include <linux/cred.h>
2627
#include <linux/err.h>
2728
#include <linux/errname.h>
2829
#include <linux/fs.h>
@@ -164,6 +165,18 @@ struct file *rust_helper_get_file(struct file *f)
164165
}
165166
EXPORT_SYMBOL_GPL(rust_helper_get_file);
166167

168+
const struct cred *rust_helper_get_cred(const struct cred *cred)
169+
{
170+
return get_cred(cred);
171+
}
172+
EXPORT_SYMBOL_GPL(rust_helper_get_cred);
173+
174+
void rust_helper_put_cred(const struct cred *cred)
175+
{
176+
put_cred(cred);
177+
}
178+
EXPORT_SYMBOL_GPL(rust_helper_put_cred);
179+
167180
/*
168181
* `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
169182
* use it in contexts where Rust expects a `usize` like slice (array) indices.

rust/kernel/cred.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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::{
10+
bindings,
11+
types::{AlwaysRefCounted, Opaque},
12+
};
13+
14+
/// Wraps the kernel's `struct cred`.
15+
///
16+
/// # Invariants
17+
///
18+
/// Instances of this type are always ref-counted, that is, a call to `get_cred` ensures that the
19+
/// allocation remains valid at least until the matching call to `put_cred`.
20+
#[repr(transparent)]
21+
pub struct Credential(pub(crate) Opaque<bindings::cred>);
22+
23+
// SAFETY: By design, the only way to access a `Credential` is via an immutable reference or an
24+
// `ARef`. This means that the only situation in which a `Credential` can be accessed mutably is
25+
// when the refcount drops to zero and the destructor runs. It is safe for that to happen on any
26+
// thread, so it is ok for this type to be `Send`.
27+
unsafe impl Send for Credential {}
28+
29+
// SAFETY: It's OK to access `Credential` through shared references from other threads because
30+
// we're either accessing properties that don't change or that are properly synchronised by C code.
31+
unsafe impl Sync for Credential {}
32+
33+
impl Credential {
34+
/// Creates a reference to a [`Credential`] from a valid pointer.
35+
///
36+
/// # Safety
37+
///
38+
/// The caller must ensure that `ptr` is valid and remains valid for the lifetime of the
39+
/// returned [`Credential`] reference.
40+
pub unsafe fn from_ptr<'a>(ptr: *const bindings::cred) -> &'a Credential {
41+
// SAFETY: The safety requirements guarantee the validity of the dereference, while the
42+
// `Credential` type being transparent makes the cast ok.
43+
unsafe { &*ptr.cast() }
44+
}
45+
46+
/// Returns the effective UID of the given credential.
47+
pub fn euid(&self) -> bindings::kuid_t {
48+
// SAFETY: By the type invariant, we know that `self.0` is valid.
49+
unsafe { (*self.0.get()).euid }
50+
}
51+
}
52+
53+
// SAFETY: The type invariants guarantee that `Credential` is always ref-counted.
54+
unsafe impl AlwaysRefCounted for Credential {
55+
fn inc_ref(&self) {
56+
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
57+
unsafe { bindings::get_cred(self.0.get()) };
58+
}
59+
60+
unsafe fn dec_ref(obj: core::ptr::NonNull<Self>) {
61+
// SAFETY: The safety requirements guarantee that the refcount is nonzero.
62+
unsafe { bindings::put_cred(obj.cast().as_ptr()) };
63+
}
64+
}

rust/kernel/file.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
use crate::{
99
bindings,
10+
cred::Credential,
1011
error::{code::*, Error, Result},
1112
types::{ARef, AlwaysRefCounted, Opaque},
1213
};
@@ -151,6 +152,21 @@ impl File {
151152
self.0.get()
152153
}
153154

155+
/// Returns the credentials of the task that originally opened the file.
156+
pub fn cred(&self) -> &Credential {
157+
// SAFETY: Since the caller holds a reference to the file, it is guaranteed that its
158+
// refcount does not hit zero during this function call.
159+
//
160+
// It's okay to read the `f_cred` field without synchronization as `f_cred` is never
161+
// changed after initialization of the file.
162+
let ptr = unsafe { (*self.as_ptr()).f_cred };
163+
164+
// SAFETY: The signature of this function ensures that the caller will only access the
165+
// returned credential while the file is still valid, and the C side ensures that the
166+
// credential stays valid at least as long as the file.
167+
unsafe { Credential::from_ptr(ptr) }
168+
}
169+
154170
/// Returns the flags associated with the file.
155171
///
156172
/// The flags are a combination of the constants in [`flags`].

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern crate self as kernel;
3333
#[cfg(not(testlib))]
3434
mod allocator;
3535
mod build_assert;
36+
pub mod cred;
3637
pub mod error;
3738
pub mod file;
3839
pub mod init;

0 commit comments

Comments
 (0)