Skip to content

Commit 94d356c

Browse files
Darksonnbrauner
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. This abstraction makes the following assumptions about the C side: * When a call to `security_secid_to_secctx` is successful, it returns a pointer and length. The pointer references a byte string and is valid for reading for that many bytes. * The string may be referenced until `security_release_secctx` is called. * If CONFIG_SECURITY is set, then the three methods mentioned in rust/helpers are available without a helper. (That is, they are not a #define or `static inline`.) Reviewed-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com> Reviewed-by: Trevor Gross <tmgross@umich.edu> 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-5-88484f7a3dcf@google.com Acked-by: Paul Moore <paul@paul-moore.com> Reviewed-by: Kees Cook <kees@kernel.org> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent a3df991 commit 94d356c

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
@@ -21,6 +21,7 @@
2121
#include <linux/phy.h>
2222
#include <linux/refcount.h>
2323
#include <linux/sched.h>
24+
#include <linux/security.h>
2425
#include <linux/slab.h>
2526
#include <linux/wait.h>
2627
#include <linux/workqueue.h>

rust/helpers/helpers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "page.c"
2020
#include "rbtree.c"
2121
#include "refcount.c"
22+
#include "security.c"
2223
#include "signal.c"
2324
#include "slab.c"
2425
#include "spinlock.c"

rust/helpers/security.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/security.h>
4+
5+
#ifndef CONFIG_SECURITY
6+
void rust_helper_security_cred_getsecid(const struct cred *c, u32 *secid)
7+
{
8+
security_cred_getsecid(c, secid);
9+
}
10+
11+
int rust_helper_security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
12+
{
13+
return security_secid_to_secctx(secid, secdata, seclen);
14+
}
15+
16+
void rust_helper_security_release_secctx(char *secdata, u32 seclen)
17+
{
18+
security_release_secctx(secdata, seclen);
19+
}
20+
#endif

rust/kernel/cred.rs

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

55+
/// Get the id for this security context.
56+
pub fn get_secid(&self) -> u32 {
57+
let mut secid = 0;
58+
// SAFETY: The invariants of this type ensures that the pointer is valid.
59+
unsafe { bindings::security_cred_getsecid(self.0.get(), &mut secid) };
60+
secid
61+
}
62+
5563
/// Returns the effective UID of the given credential.
5664
pub fn euid(&self) -> bindings::kuid_t {
5765
// 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
@@ -47,6 +47,7 @@ pub mod page;
4747
pub mod prelude;
4848
pub mod print;
4949
pub mod rbtree;
50+
pub mod security;
5051
mod static_assert;
5152
#[doc(hidden)]
5253
pub mod std_vendor;

rust/kernel/security.rs

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

0 commit comments

Comments
 (0)