Skip to content

Commit f8d1723

Browse files
Darksonnfbq
authored andcommitted
rust: security: add abstraction for secctx
Adds an abstraction for viewing the string representation of a security context. This is needed by Rust Binder because it has 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 <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 129c3d2 commit f8d1723

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
@@ -15,6 +15,7 @@
1515
#include <linux/jiffies.h>
1616
#include <linux/mdio.h>
1717
#include <linux/phy.h>
18+
#include <linux/security.h>
1819
#include <linux/slab.h>
1920
#include <linux/refcount.h>
2021
#include <linux/wait.h>

rust/helpers.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <linux/mutex.h>
3131
#include <linux/refcount.h>
3232
#include <linux/sched/signal.h>
33+
#include <linux/security.h>
3334
#include <linux/spinlock.h>
3435
#include <linux/wait.h>
3536
#include <linux/workqueue.h>
@@ -177,6 +178,26 @@ void rust_helper_put_cred(const struct cred *cred)
177178
}
178179
EXPORT_SYMBOL_GPL(rust_helper_put_cred);
179180

181+
#ifndef CONFIG_SECURITY
182+
void rust_helper_security_cred_getsecid(const struct cred *c, u32 *secid)
183+
{
184+
security_cred_getsecid(c, secid);
185+
}
186+
EXPORT_SYMBOL_GPL(rust_helper_security_cred_getsecid);
187+
188+
int rust_helper_security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
189+
{
190+
return security_secid_to_secctx(secid, secdata, seclen);
191+
}
192+
EXPORT_SYMBOL_GPL(rust_helper_security_secid_to_secctx);
193+
194+
void rust_helper_security_release_secctx(char *secdata, u32 seclen)
195+
{
196+
security_release_secctx(secdata, seclen);
197+
}
198+
EXPORT_SYMBOL_GPL(rust_helper_security_release_secctx);
199+
#endif
200+
180201
/*
181202
* `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
182203
* 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
@@ -48,6 +48,14 @@ impl Credential {
4848
unsafe { &*ptr.cast() }
4949
}
5050

51+
/// Get the id for this security context.
52+
pub fn get_secid(&self) -> u32 {
53+
let mut secid = 0;
54+
// SAFETY: The invariants of this type ensures that the pointer is valid.
55+
unsafe { bindings::security_cred_getsecid(self.0.get(), &mut secid) };
56+
secid
57+
}
58+
5159
/// Returns the effective UID of the given credential.
5260
pub fn euid(&self) -> bindings::kuid_t {
5361
// 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
@@ -43,6 +43,7 @@ pub mod kunit;
4343
pub mod net;
4444
pub mod prelude;
4545
pub mod print;
46+
pub mod security;
4647
mod static_assert;
4748
#[doc(hidden)]
4849
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)