Skip to content

Commit 129c3d2

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 <[email protected]> Co-developed-by: Alice Ryhl <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 9592aac commit 129c3d2

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-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/ethtool.h>
1213
#include <linux/file.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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Credentials management.
4+
//!
5+
//! C header: [`include/linux/cred.h`](srctree/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+
/// Most fields of credentials are immutable. When things have their credentials changed, that
17+
/// happens by replacing the credential instad of changing an existing credential. See the [kernel
18+
/// documentation][ref] for more info on this.
19+
///
20+
/// # Invariants
21+
///
22+
/// Instances of this type are always ref-counted, that is, a call to `get_cred` ensures that the
23+
/// allocation remains valid at least until the matching call to `put_cred`.
24+
///
25+
/// [ref]: https://www.kernel.org/doc/html/latest/security/credentials.html
26+
#[repr(transparent)]
27+
pub struct Credential(Opaque<bindings::cred>);
28+
29+
// SAFETY:
30+
// - `Credential::dec_ref` can be called from any thread.
31+
// - It is okay to send ownership of `Credential` across thread boundaries.
32+
unsafe impl Send for Credential {}
33+
34+
// SAFETY: It's OK to access `Credential` through shared references from other threads because
35+
// we're either accessing properties that don't change or that are properly synchronised by C code.
36+
unsafe impl Sync for Credential {}
37+
38+
impl Credential {
39+
/// Creates a reference to a [`Credential`] from a valid pointer.
40+
///
41+
/// # Safety
42+
///
43+
/// The caller must ensure that `ptr` is valid and remains valid for the lifetime of the
44+
/// returned [`Credential`] reference.
45+
pub unsafe fn from_ptr<'a>(ptr: *const bindings::cred) -> &'a Credential {
46+
// SAFETY: The safety requirements guarantee the validity of the dereference, while the
47+
// `Credential` type being transparent makes the cast ok.
48+
unsafe { &*ptr.cast() }
49+
}
50+
51+
/// Returns the effective UID of the given credential.
52+
pub fn euid(&self) -> bindings::kuid_t {
53+
// SAFETY: By the type invariant, we know that `self.0` is valid. Furthermore, the `euid`
54+
// field of a credential is never changed after initialization, so there is no potential
55+
// for data races.
56+
unsafe { (*self.0.get()).euid }
57+
}
58+
}
59+
60+
// SAFETY: The type invariants guarantee that `Credential` is always ref-counted.
61+
unsafe impl AlwaysRefCounted for Credential {
62+
fn inc_ref(&self) {
63+
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
64+
unsafe { bindings::get_cred(self.0.get()) };
65+
}
66+
67+
unsafe fn dec_ref(obj: core::ptr::NonNull<Credential>) {
68+
// SAFETY: The safety requirements guarantee that the refcount is nonzero. The cast is okay
69+
// because `Credential` has the same representation as `struct cred`.
70+
unsafe { bindings::put_cred(obj.cast().as_ptr()) };
71+
}
72+
}

rust/kernel/file.rs

Lines changed: 13 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
};
@@ -202,6 +203,18 @@ impl File {
202203
self.0.get()
203204
}
204205

206+
/// Returns the credentials of the task that originally opened the file.
207+
pub fn cred(&self) -> &Credential {
208+
// SAFETY: It's okay to read the `f_cred` field without synchronization because `f_cred` is
209+
// never changed after initialization of the file.
210+
let ptr = unsafe { (*self.as_ptr()).f_cred };
211+
212+
// SAFETY: The signature of this function ensures that the caller will only access the
213+
// returned credential while the file is still valid, and the C side ensures that the
214+
// credential stays valid at least as long as the file.
215+
unsafe { Credential::from_ptr(ptr) }
216+
}
217+
205218
/// Returns the flags associated with the file.
206219
///
207220
/// 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
@@ -32,6 +32,7 @@ extern crate self as kernel;
3232
#[cfg(not(testlib))]
3333
mod allocator;
3434
mod build_assert;
35+
pub mod cred;
3536
pub mod error;
3637
pub mod file;
3738
pub mod init;

0 commit comments

Comments
 (0)