Skip to content

Commit 092c3a3

Browse files
Darksonnfbq
authored andcommitted
rust: security: add abstraction for secctx
Add an abstraction for viewing the string representation of a security context. This is needed by Rust Binder because it has a feature where a process can view the string representation of the security context for incoming transactions. The process can use that to authenticate incoming transactions, and since the feature is provided by the kernel, the process can trust that the security context is legitimate. Reviewed-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com> Reviewed-by: Trevor Gross <tmgross@umich.edu> Signed-off-by: Alice Ryhl <aliceryhl@google.com> Link: https://lore.kernel.org/r/20240628-alice-file-v7-5-4d701f6335f3@google.com
1 parent 53dc0ab commit 092c3a3

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/phy.h>
1818
#include <linux/refcount.h>
1919
#include <linux/sched.h>
20+
#include <linux/security.h>
2021
#include <linux/slab.h>
2122
#include <linux/wait.h>
2223
#include <linux/workqueue.h>

rust/helpers.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <linux/mutex.h>
3333
#include <linux/refcount.h>
3434
#include <linux/sched/signal.h>
35+
#include <linux/security.h>
3536
#include <linux/slab.h>
3637
#include <linux/spinlock.h>
3738
#include <linux/wait.h>
@@ -219,6 +220,26 @@ void rust_helper_put_cred(const struct cred *cred)
219220
}
220221
EXPORT_SYMBOL_GPL(rust_helper_put_cred);
221222

223+
#ifndef CONFIG_SECURITY
224+
void rust_helper_security_cred_getsecid(const struct cred *c, u32 *secid)
225+
{
226+
security_cred_getsecid(c, secid);
227+
}
228+
EXPORT_SYMBOL_GPL(rust_helper_security_cred_getsecid);
229+
230+
int rust_helper_security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
231+
{
232+
return security_secid_to_secctx(secid, secdata, seclen);
233+
}
234+
EXPORT_SYMBOL_GPL(rust_helper_security_secid_to_secctx);
235+
236+
void rust_helper_security_release_secctx(char *secdata, u32 seclen)
237+
{
238+
security_release_secctx(secdata, seclen);
239+
}
240+
EXPORT_SYMBOL_GPL(rust_helper_security_release_secctx);
241+
#endif
242+
222243
/*
223244
* `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
224245
* use it in contexts where Rust expects a `usize` like slice (array) indices.

rust/kernel/cred.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ impl Credential {
5050
unsafe { &*ptr.cast() }
5151
}
5252

53+
/// Get the id for this security context.
54+
pub fn get_secid(&self) -> u32 {
55+
let mut secid = 0;
56+
// SAFETY: The invariants of this type ensures that the pointer is valid.
57+
unsafe { bindings::security_cred_getsecid(self.0.get(), &mut secid) };
58+
secid
59+
}
60+
5361
/// Returns the effective UID of the given credential.
5462
pub fn euid(&self) -> bindings::kuid_t {
5563
// SAFETY: By the type invariant, we know that `self.0` is valid. Furthermore, the `euid`

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub mod net;
4040
pub mod page;
4141
pub mod prelude;
4242
pub mod print;
43+
pub mod security;
4344
mod static_assert;
4445
#[doc(hidden)]
4546
pub mod std_vendor;

rust/kernel/security.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+
//! Linux Security Modules (LSM).
4+
//!
5+
//! C header: [`include/linux/security.h`](srctree/include/linux/security.h).
6+
7+
use crate::{
8+
bindings,
9+
error::{to_result, Result},
10+
};
11+
12+
/// A security context string.
13+
///
14+
/// # Invariants
15+
///
16+
/// The `secdata` and `seclen` fields correspond to a valid security context as returned by a
17+
/// successful call to `security_secid_to_secctx`, that has not yet been destroyed by calling
18+
/// `security_release_secctx`.
19+
pub struct SecurityCtx {
20+
secdata: *mut core::ffi::c_char,
21+
seclen: usize,
22+
}
23+
24+
impl SecurityCtx {
25+
/// Get the security context given its id.
26+
pub fn from_secid(secid: u32) -> Result<Self> {
27+
let mut secdata = core::ptr::null_mut();
28+
let mut seclen = 0u32;
29+
// SAFETY: Just a C FFI call. The pointers are valid for writes.
30+
to_result(unsafe { bindings::security_secid_to_secctx(secid, &mut secdata, &mut seclen) })?;
31+
32+
// INVARIANT: If the above call did not fail, then we have a valid security context.
33+
Ok(Self {
34+
secdata,
35+
seclen: seclen as usize,
36+
})
37+
}
38+
39+
/// Returns whether the security context is empty.
40+
pub fn is_empty(&self) -> bool {
41+
self.seclen == 0
42+
}
43+
44+
/// Returns the length of this security context.
45+
pub fn len(&self) -> usize {
46+
self.seclen
47+
}
48+
49+
/// Returns the bytes for this security context.
50+
pub fn as_bytes(&self) -> &[u8] {
51+
let ptr = self.secdata;
52+
if ptr.is_null() {
53+
debug_assert_eq!(self.seclen, 0);
54+
// We can't pass a null pointer to `slice::from_raw_parts` even if the length is zero.
55+
return &[];
56+
}
57+
58+
// SAFETY: The call to `security_secid_to_secctx` guarantees that the pointer is valid for
59+
// `seclen` bytes. Furthermore, if the length is zero, then we have ensured that the
60+
// pointer is not null.
61+
unsafe { core::slice::from_raw_parts(ptr.cast(), self.seclen) }
62+
}
63+
}
64+
65+
impl Drop for SecurityCtx {
66+
fn drop(&mut self) {
67+
// SAFETY: By the invariant of `Self`, this frees a pointer that came from a successful
68+
// call to `security_secid_to_secctx` and has not yet been destroyed by
69+
// `security_release_secctx`.
70+
unsafe { bindings::security_release_secctx(self.secdata, self.seclen as u32) };
71+
}
72+
}

0 commit comments

Comments
 (0)