Skip to content

Commit 9c76eaf

Browse files
Darksonnpcmoore
authored andcommitted
rust: replace lsm context+len with lsm_context
This brings the Rust SecurityCtx abstraction [1] up to date with the new API where context+len is replaced with an lsm_context [2] struct. Link: https://lore.kernel.org/r/[email protected] [1] Link: https://lore.kernel.org/r/[email protected] [2] Reported-by: Linux Kernel Functional Testing <[email protected]> Closes: https://lore.kernel.org/r/CA+G9fYv_Y2tzs+uYhMGtfUK9dSYV2mFr6WyKEzJazDsdk9o5zw@mail.gmail.com Signed-off-by: Alice Ryhl <[email protected]> [PM: subj line tweak] Signed-off-by: Paul Moore <[email protected]>
1 parent a4626e9 commit 9c76eaf

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

rust/helpers/security.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ void rust_helper_security_cred_getsecid(const struct cred *c, u32 *secid)
88
security_cred_getsecid(c, secid);
99
}
1010

11-
int rust_helper_security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
11+
int rust_helper_security_secid_to_secctx(u32 secid, struct lsm_context *cp)
1212
{
13-
return security_secid_to_secctx(secid, secdata, seclen);
13+
return security_secid_to_secctx(secid, cp);
1414
}
1515

16-
void rust_helper_security_release_secctx(char *secdata, u32 seclen)
16+
void rust_helper_security_release_secctx(struct lsm_context *cp)
1717
{
18-
security_release_secctx(secdata, seclen);
18+
security_release_secctx(cp);
1919
}
2020
#endif

rust/kernel/security.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,56 @@ use crate::{
1515
///
1616
/// # Invariants
1717
///
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`.
18+
/// The `ctx` field corresponds to a valid security context as returned by a successful call to
19+
/// `security_secid_to_secctx`, that has not yet been destroyed by `security_release_secctx`.
2120
pub struct SecurityCtx {
22-
secdata: *mut core::ffi::c_char,
23-
seclen: usize,
21+
ctx: bindings::lsm_context,
2422
}
2523

2624
impl SecurityCtx {
2725
/// Get the security context given its id.
2826
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) })?;
27+
// SAFETY: `struct lsm_context` can be initialized to all zeros.
28+
let mut ctx: bindings::lsm_context = unsafe { core::mem::zeroed() };
29+
30+
// SAFETY: Just a C FFI call. The pointer is valid for writes.
31+
to_result(unsafe { bindings::security_secid_to_secctx(secid, &mut ctx) })?;
3332

3433
// 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-
})
34+
Ok(Self { ctx })
3935
}
4036

4137
/// Returns whether the security context is empty.
4238
pub fn is_empty(&self) -> bool {
43-
self.seclen == 0
39+
self.ctx.len == 0
4440
}
4541

4642
/// Returns the length of this security context.
4743
pub fn len(&self) -> usize {
48-
self.seclen
44+
self.ctx.len as usize
4945
}
5046

5147
/// Returns the bytes for this security context.
5248
pub fn as_bytes(&self) -> &[u8] {
53-
let ptr = self.secdata;
49+
let ptr = self.ctx.context;
5450
if ptr.is_null() {
55-
debug_assert_eq!(self.seclen, 0);
51+
debug_assert_eq!(self.len(), 0);
5652
// We can't pass a null pointer to `slice::from_raw_parts` even if the length is zero.
5753
return &[];
5854
}
5955

6056
// 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
57+
// `self.len()` bytes. Furthermore, if the length is zero, then we have ensured that the
6258
// pointer is not null.
63-
unsafe { core::slice::from_raw_parts(ptr.cast(), self.seclen) }
59+
unsafe { core::slice::from_raw_parts(ptr.cast(), self.len()) }
6460
}
6561
}
6662

6763
impl Drop for SecurityCtx {
6864
fn drop(&mut self) {
69-
// SAFETY: By the invariant of `Self`, this frees a pointer that came from a successful
65+
// SAFETY: By the invariant of `Self`, this frees a context that came from a successful
7066
// call to `security_secid_to_secctx` and has not yet been destroyed by
7167
// `security_release_secctx`.
72-
unsafe { bindings::security_release_secctx(self.secdata, self.seclen as u32) };
68+
unsafe { bindings::security_release_secctx(&mut self.ctx) };
7369
}
7470
}

0 commit comments

Comments
 (0)