xref: /linux/rust/kernel/security.rs (revision 4f9786035f9e519db41375818e1d0b5f20da2f10)
194d356c0SAlice Ryhl // SPDX-License-Identifier: GPL-2.0
294d356c0SAlice Ryhl 
394d356c0SAlice Ryhl // Copyright (C) 2024 Google LLC.
494d356c0SAlice Ryhl 
594d356c0SAlice Ryhl //! Linux Security Modules (LSM).
694d356c0SAlice Ryhl //!
794d356c0SAlice Ryhl //! C header: [`include/linux/security.h`](srctree/include/linux/security.h).
894d356c0SAlice Ryhl 
994d356c0SAlice Ryhl use crate::{
1094d356c0SAlice Ryhl     bindings,
1194d356c0SAlice Ryhl     error::{to_result, Result},
1294d356c0SAlice Ryhl };
1394d356c0SAlice Ryhl 
1494d356c0SAlice Ryhl /// A security context string.
1594d356c0SAlice Ryhl ///
1694d356c0SAlice Ryhl /// # Invariants
1794d356c0SAlice Ryhl ///
189c76eaf7SAlice Ryhl /// The `ctx` field corresponds to a valid security context as returned by a successful call to
19*0a01beacSAlice Ryhl /// `security_secid_to_secctx`, that has not yet been released by `security_release_secctx`.
2094d356c0SAlice Ryhl pub struct SecurityCtx {
219c76eaf7SAlice Ryhl     ctx: bindings::lsm_context,
2294d356c0SAlice Ryhl }
2394d356c0SAlice Ryhl 
2494d356c0SAlice Ryhl impl SecurityCtx {
2594d356c0SAlice Ryhl     /// Get the security context given its id.
2655e16418SAlice Ryhl     #[inline]
2794d356c0SAlice Ryhl     pub fn from_secid(secid: u32) -> Result<Self> {
289c76eaf7SAlice Ryhl         // SAFETY: `struct lsm_context` can be initialized to all zeros.
299c76eaf7SAlice Ryhl         let mut ctx: bindings::lsm_context = unsafe { core::mem::zeroed() };
309c76eaf7SAlice Ryhl 
319c76eaf7SAlice Ryhl         // SAFETY: Just a C FFI call. The pointer is valid for writes.
329c76eaf7SAlice Ryhl         to_result(unsafe { bindings::security_secid_to_secctx(secid, &mut ctx) })?;
3394d356c0SAlice Ryhl 
3494d356c0SAlice Ryhl         // INVARIANT: If the above call did not fail, then we have a valid security context.
359c76eaf7SAlice Ryhl         Ok(Self { ctx })
3694d356c0SAlice Ryhl     }
3794d356c0SAlice Ryhl 
3894d356c0SAlice Ryhl     /// Returns whether the security context is empty.
3955e16418SAlice Ryhl     #[inline]
4094d356c0SAlice Ryhl     pub fn is_empty(&self) -> bool {
419c76eaf7SAlice Ryhl         self.ctx.len == 0
4294d356c0SAlice Ryhl     }
4394d356c0SAlice Ryhl 
4494d356c0SAlice Ryhl     /// Returns the length of this security context.
4555e16418SAlice Ryhl     #[inline]
4694d356c0SAlice Ryhl     pub fn len(&self) -> usize {
479c76eaf7SAlice Ryhl         self.ctx.len as usize
4894d356c0SAlice Ryhl     }
4994d356c0SAlice Ryhl 
5094d356c0SAlice Ryhl     /// Returns the bytes for this security context.
5155e16418SAlice Ryhl     #[inline]
5294d356c0SAlice Ryhl     pub fn as_bytes(&self) -> &[u8] {
539c76eaf7SAlice Ryhl         let ptr = self.ctx.context;
5494d356c0SAlice Ryhl         if ptr.is_null() {
559c76eaf7SAlice Ryhl             debug_assert_eq!(self.len(), 0);
5694d356c0SAlice Ryhl             // We can't pass a null pointer to `slice::from_raw_parts` even if the length is zero.
5794d356c0SAlice Ryhl             return &[];
5894d356c0SAlice Ryhl         }
5994d356c0SAlice Ryhl 
6094d356c0SAlice Ryhl         // SAFETY: The call to `security_secid_to_secctx` guarantees that the pointer is valid for
619c76eaf7SAlice Ryhl         // `self.len()` bytes. Furthermore, if the length is zero, then we have ensured that the
6294d356c0SAlice Ryhl         // pointer is not null.
639c76eaf7SAlice Ryhl         unsafe { core::slice::from_raw_parts(ptr.cast(), self.len()) }
6494d356c0SAlice Ryhl     }
6594d356c0SAlice Ryhl }
6694d356c0SAlice Ryhl 
6794d356c0SAlice Ryhl impl Drop for SecurityCtx {
6855e16418SAlice Ryhl     #[inline]
6994d356c0SAlice Ryhl     fn drop(&mut self) {
70*0a01beacSAlice Ryhl         // SAFETY: By the invariant of `Self`, this releases an lsm context that came from a
71*0a01beacSAlice Ryhl         // successful call to `security_secid_to_secctx` and has not yet been released.
729c76eaf7SAlice Ryhl         unsafe { bindings::security_release_secctx(&mut self.ctx) };
7394d356c0SAlice Ryhl     }
7494d356c0SAlice Ryhl }
75