Skip to content

Commit a3df991

Browse files
wedsonafbrauner
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. This Rust abstraction makes the following assumptions about the C side: * `struct cred` is refcounted with `get_cred`/`put_cred`. * It's okay to transfer a `struct cred` across threads, that is, you do not need to call `put_cred` on the same thread as where you called `get_cred`. * The `euid` field of a `struct cred` never changes after initialization. * The `f_cred` field of a `struct file` never changes after initialization. Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> Co-developed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Trevor Gross <tmgross@umich.edu> Reviewed-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com> Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Alice Ryhl <aliceryhl@google.com> Link: https://lore.kernel.org/r/20240915-alice-file-v10-4-88484f7a3dcf@google.com Reviewed-by: Kees Cook <kees@kernel.org> Reviewed-by: Paul Moore <paul@paul-moore.com> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 8518498 commit a3df991

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/blk-mq.h>
1111
#include <linux/blk_types.h>
1212
#include <linux/blkdev.h>
13+
#include <linux/cred.h>
1314
#include <linux/errname.h>
1415
#include <linux/ethtool.h>
1516
#include <linux/file.h>

rust/helpers/cred.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/cred.h>
4+
5+
const struct cred *rust_helper_get_cred(const struct cred *cred)
6+
{
7+
return get_cred(cred);
8+
}
9+
10+
void rust_helper_put_cred(const struct cred *cred)
11+
{
12+
put_cred(cred);
13+
}

rust/helpers/helpers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "bug.c"
1212
#include "build_assert.c"
1313
#include "build_bug.c"
14+
#include "cred.c"
1415
#include "err.c"
1516
#include "fs.c"
1617
#include "kunit.c"

rust/kernel/cred.rs

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

rust/kernel/fs/file.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
1010
use crate::{
1111
bindings,
12+
cred::Credential,
1213
error::{code::*, Error, Result},
1314
types::{ARef, AlwaysRefCounted, Opaque},
1415
};
@@ -308,6 +309,18 @@ impl LocalFile {
308309
self.inner.get()
309310
}
310311

312+
/// Returns the credentials of the task that originally opened the file.
313+
pub fn cred(&self) -> &Credential {
314+
// SAFETY: It's okay to read the `f_cred` field without synchronization because `f_cred` is
315+
// never changed after initialization of the file.
316+
let ptr = unsafe { (*self.as_ptr()).f_cred };
317+
318+
// SAFETY: The signature of this function ensures that the caller will only access the
319+
// returned credential while the file is still valid, and the C side ensures that the
320+
// credential stays valid at least as long as the file.
321+
unsafe { Credential::from_ptr(ptr) }
322+
}
323+
311324
/// Returns the flags associated with the file.
312325
///
313326
/// 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
@@ -30,6 +30,7 @@ pub mod alloc;
3030
#[cfg(CONFIG_BLOCK)]
3131
pub mod block;
3232
mod build_assert;
33+
pub mod cred;
3334
pub mod device;
3435
pub mod error;
3536
#[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]

0 commit comments

Comments
 (0)