summaryrefslogtreecommitdiff
path: root/rust/kernel
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2025-01-21 20:03:04 -0800
committerLinus Torvalds <torvalds@linux-foundation.org>2025-01-21 20:03:04 -0800
commitf96a974170b749e3a56844e25b31d46a7233b6f6 (patch)
treef1e391e42ae5ba62b856520e0201a28fc7e6dbb2 /rust/kernel
parent678ca9f78e40ec8ebbd054b0c22bd3b5ecc6c7e4 (diff)
parent714d87c90a766e6917f7d69f618b864d350f09d3 (diff)
Merge tag 'lsm-pr-20250121' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/lsm
Pull lsm updates from Paul Moore: - Improved handling of LSM "secctx" strings through lsm_context struct The LSM secctx string interface is from an older time when only one LSM was supported, migrate over to the lsm_context struct to better support the different LSMs we now have and make it easier to support new LSMs in the future. These changes explain the Rust, VFS, and networking changes in the diffstat. - Only build lsm_audit.c if CONFIG_SECURITY and CONFIG_AUDIT are enabled Small tweak to be a bit smarter about when we build the LSM's common audit helpers. - Check for absurdly large policies from userspace in SafeSetID SafeSetID policies rules are fairly small, basically just "UID:UID", it easy to impose a limit of KMALLOC_MAX_SIZE on policy writes which helps quiet a number of syzbot related issues. While work is being done to address the syzbot issues through other mechanisms, this is a trivial and relatively safe fix that we can do now. - Various minor improvements and cleanups A collection of improvements to the kernel selftests, constification of some function parameters, removing redundant assignments, and local variable renames to improve readability. * tag 'lsm-pr-20250121' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/lsm: lockdown: initialize local array before use to quiet static analysis safesetid: check size of policy writes net: corrections for security_secid_to_secctx returns lsm: rename variable to avoid shadowing lsm: constify function parameters security: remove redundant assignment to return variable lsm: Only build lsm_audit.c if CONFIG_SECURITY and CONFIG_AUDIT are set selftests: refactor the lsm `flags_overset_lsm_set_self_attr` test binder: initialize lsm_context structure rust: replace lsm context+len with lsm_context lsm: secctx provider check on release lsm: lsm_context in security_dentry_init_security lsm: use lsm_context in security_inode_getsecctx lsm: replace context+len with lsm_context lsm: ensure the correct LSM context releaser
Diffstat (limited to 'rust/kernel')
-rw-r--r--rust/kernel/security.rs38
1 files changed, 17 insertions, 21 deletions
diff --git a/rust/kernel/security.rs b/rust/kernel/security.rs
index ea4c58c81703..25d2b1ac3833 100644
--- a/rust/kernel/security.rs
+++ b/rust/kernel/security.rs
@@ -15,60 +15,56 @@ use crate::{
///
/// # Invariants
///
-/// The `secdata` and `seclen` fields correspond to a valid security context as returned by a
-/// successful call to `security_secid_to_secctx`, that has not yet been destroyed by calling
-/// `security_release_secctx`.
+/// The `ctx` field corresponds to a valid security context as returned by a successful call to
+/// `security_secid_to_secctx`, that has not yet been destroyed by `security_release_secctx`.
pub struct SecurityCtx {
- secdata: *mut crate::ffi::c_char,
- seclen: usize,
+ ctx: bindings::lsm_context,
}
impl SecurityCtx {
/// Get the security context given its id.
pub fn from_secid(secid: u32) -> Result<Self> {
- let mut secdata = core::ptr::null_mut();
- let mut seclen = 0u32;
- // SAFETY: Just a C FFI call. The pointers are valid for writes.
- to_result(unsafe { bindings::security_secid_to_secctx(secid, &mut secdata, &mut seclen) })?;
+ // SAFETY: `struct lsm_context` can be initialized to all zeros.
+ let mut ctx: bindings::lsm_context = unsafe { core::mem::zeroed() };
+
+ // SAFETY: Just a C FFI call. The pointer is valid for writes.
+ to_result(unsafe { bindings::security_secid_to_secctx(secid, &mut ctx) })?;
// INVARIANT: If the above call did not fail, then we have a valid security context.
- Ok(Self {
- secdata,
- seclen: seclen as usize,
- })
+ Ok(Self { ctx })
}
/// Returns whether the security context is empty.
pub fn is_empty(&self) -> bool {
- self.seclen == 0
+ self.ctx.len == 0
}
/// Returns the length of this security context.
pub fn len(&self) -> usize {
- self.seclen
+ self.ctx.len as usize
}
/// Returns the bytes for this security context.
pub fn as_bytes(&self) -> &[u8] {
- let ptr = self.secdata;
+ let ptr = self.ctx.context;
if ptr.is_null() {
- debug_assert_eq!(self.seclen, 0);
+ debug_assert_eq!(self.len(), 0);
// We can't pass a null pointer to `slice::from_raw_parts` even if the length is zero.
return &[];
}
// SAFETY: The call to `security_secid_to_secctx` guarantees that the pointer is valid for
- // `seclen` bytes. Furthermore, if the length is zero, then we have ensured that the
+ // `self.len()` bytes. Furthermore, if the length is zero, then we have ensured that the
// pointer is not null.
- unsafe { core::slice::from_raw_parts(ptr.cast(), self.seclen) }
+ unsafe { core::slice::from_raw_parts(ptr.cast(), self.len()) }
}
}
impl Drop for SecurityCtx {
fn drop(&mut self) {
- // SAFETY: By the invariant of `Self`, this frees a pointer that came from a successful
+ // SAFETY: By the invariant of `Self`, this frees a context that came from a successful
// call to `security_secid_to_secctx` and has not yet been destroyed by
// `security_release_secctx`.
- unsafe { bindings::security_release_secctx(self.secdata, self.seclen as u32) };
+ unsafe { bindings::security_release_secctx(&mut self.ctx) };
}
}