From 2dab1d02b1b0986b095aecaecc6398107275a4f4 Mon Sep 17 00:00:00 2001 From: tamnd <1218621+tamnd@users.noreply.github.com> Date: Mon, 23 Nov 2026 15:52:03 +0700 Subject: [PATCH 1/2] SELinux: the check of the capabilities The capable hook gets the user namespace of the request, as on Linux. SELinux checks a capability in the class capability or capability2, or in cap_userns or cap2_userns for another user namespace, as selinux_capable and cred_has_capability. The record has capability=N. The test policy has the new classes and the rules of the capabilities. --- CHANGELOG.md | 1 + fs/rk-vfs/src/cred.rs | 12 ++-- fs/rk-vfs/src/security/capability.rs | 3 +- fs/rk-vfs/src/security/hooks.rs | 7 +- fs/rk-vfs/src/security/mod.rs | 13 ++-- fs/rk-vfs/src/security/tests.rs | 2 +- sec/rk-lsm-apparmor/src/lib.rs | 3 +- sec/rk-lsm-apparmor/src/ns.rs | 4 +- sec/rk-lsm-selinux/src/file.rs | 2 +- sec/rk-lsm-selinux/src/inode.rs | 3 +- sec/rk-lsm-selinux/src/lib.rs | 13 ++-- sec/rk-lsm-selinux/src/policydb.rs | 2 +- sec/rk-lsm-selinux/src/task.rs | 92 ++++++++++++++++++++++-- sec/rk-lsm-selinux/src/xattr.rs | 3 +- sec/rk-lsm-selinux/tests/rk_test.24.bin | Bin 6078 -> 7134 bytes sec/rk-lsm-selinux/tests/rk_test.33.bin | Bin 6721 -> 7825 bytes sec/rk-lsm-selinux/tests/rk_test.35.bin | Bin 6733 -> 7837 bytes sec/rk-lsm-selinux/tests/rk_test.conf | 9 +++ tests/user/init.c | 18 +++++ 19 files changed, 152 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fbb7d906..8d526233 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ This file records each release. The format follows [Keep a Changelog](https://ke ### Added +- SELinux checks the capabilities, as `selinux_capable` and `cred_has_capability` of Linux. After the check of the capability module, a capability needs its permission on the task itself in the class `capability` or `capability2`, or in `cap_userns` or `cap2_userns` for a user namespace that is not the first one. The record has `capability=N`, and a check with `CAP_OPT_NOAUDIT` makes no record. The `capable` hook now gets the user namespace of the request, as on Linux. The check of `CAP_MAC_ADMIN` for a context that the policy does not know is that of `has_cap_mac_admin`, without a record for a read of the label (#540). - SELinux checks the mounts, as `selinux_mount`, `selinux_move_mount` and `selinux_umount` of Linux. A new mount and a move of a mount need `mounton` on the place of the mount, a remount needs `remount` of the file system, and an unmount needs `unmount` of the file system (#540). - SELinux checks the operations on other tasks, as the task, `ptrace` and `capget` hooks of hooks.c of Linux. A signal needs `sigkill`, `sigstop`, `sigchld`, `signull` for the signal 0, or `signal` for another signal. `getpgid` and `getsid` of a PID need `getpgid` and `getsession`, `setpgid` needs `setpgid`, a change of the nice value, of the I/O priority or of the scheduler needs `setsched`, and a read of them needs `getsched`. `prlimit` on another process needs `getrlimit` or `setrlimit`, `capget` needs `getcap` and `capset` needs `setcap`. `PTRACE_ATTACH` and `PTRACE_TRACEME` need `ptrace`. `prlimit` on the caller itself does not call the hook `task_prlimit`, as `check_prlimit_permission` of Linux (#540). - SELinux checks the maps of files and of anonymous memory, as `selinux_mmap_file` and `file_map_prot_check` of Linux. A map of a file needs `map`, `read`, `write` if the map is shared and can write, and `execute` if it can run code. A map that can run code needs `execmem` of the process if it is anonymous memory, a file without an inode, or a private map that can also write (#540). diff --git a/fs/rk-vfs/src/cred.rs b/fs/rk-vfs/src/cred.rs index c440261a..6e14ecd9 100644 --- a/fs/rk-vfs/src/cred.rs +++ b/fs/rk-vfs/src/cred.rs @@ -303,7 +303,8 @@ impl Cred { /// effective set, and no security module denies it, as `ns_capable(current_user_ns(), cap)` /// of Linux. pub fn has_cap(&self, cap: u32) -> bool { - self.cap_effective & cap::bit(cap) != 0 && crate::security::capable_lsm(self, cap, 0) + self.cap_effective & cap::bit(cap) != 0 + && crate::security::capable_lsm(self, &self.user_ns, cap, 0) } /// The process has the capability in the user namespace `ns`, as `ns_capable` and @@ -311,18 +312,19 @@ impl Cred { /// namespaces below it. The owner of a namespace has all the capabilities in it, if the /// process is in the parent of the namespace. After this check of the capability module, the /// other security modules check the capability, as `security_capable` of Linux. - pub fn ns_capable(&self, ns: &UserNs, cap: u32) -> bool { - let mut ns = ns; + pub fn ns_capable(&self, target: &UserNs, cap: u32) -> bool { + let mut ns = target; loop { if *ns == *self.user_ns { - return self.has_cap(cap); + return self.cap_effective & cap::bit(cap) != 0 + && crate::security::capable_lsm(self, target, cap, 0); } if ns.level() <= self.user_ns.level() { return false; } let Some(parent) = ns.parent() else { return false }; if **parent == *self.user_ns && ns.owner() == self.euid { - return crate::security::capable_lsm(self, cap, 0); + return crate::security::capable_lsm(self, target, cap, 0); } ns = parent; } diff --git a/fs/rk-vfs/src/security/capability.rs b/fs/rk-vfs/src/security/capability.rs index 9a2b4d31..11b836d8 100644 --- a/fs/rk-vfs/src/security/capability.rs +++ b/fs/rk-vfs/src/security/capability.rs @@ -8,13 +8,14 @@ use super::hooks::SecurityHooks; use super::{BlobSizes, LsmId, LsmInfo, Order, id}; use crate::cred::{Cred, cap}; use crate::inode::Result; +use crate::user_ns::UserNs; use rk_abi::Errno; /// The hooks of the capability module. pub struct Capability; impl SecurityHooks for Capability { - fn capable(&self, cred: &Cred, c: u32, _opts: u32) -> Result<()> { + fn capable(&self, cred: &Cred, _ns: &UserNs, c: u32, _opts: u32) -> Result<()> { if cred.cap_effective & cap::bit(c) != 0 { Ok(()) } else { Err(Errno::EPERM) } } } diff --git a/fs/rk-vfs/src/security/hooks.rs b/fs/rk-vfs/src/security/hooks.rs index 9412711f..32687101 100644 --- a/fs/rk-vfs/src/security/hooks.rs +++ b/fs/rk-vfs/src/security/hooks.rs @@ -12,6 +12,7 @@ use crate::cred::{Cred, IpcOwner}; use crate::dentry::Location; use crate::file::File; use crate::inode::{Inode, Result, SetAttr}; +use crate::user_ns::UserNs; use alloc::vec::Vec; /// The names of the LSM hooks of Linux, in the order of include/linux/lsm_hook_defs.h. @@ -311,9 +312,9 @@ pub fn hook_index(name: &str) -> Option { /// Linux. The methods without a result are the hooks of `call_void_hook`. #[allow(unused_variables)] pub trait SecurityHooks: Sync { - /// `capable`: the task with `cred` uses the capability `cap`. `opts` has the `CAP_OPT_*` - /// bits. - fn capable(&self, cred: &Cred, cap: u32, opts: u32) -> Result<()> { + /// `capable`: the task with `cred` uses the capability `cap` in the user namespace `ns`. + /// `opts` has the `CAP_OPT_*` bits. + fn capable(&self, cred: &Cred, ns: &UserNs, cap: u32, opts: u32) -> Result<()> { Ok(()) } diff --git a/fs/rk-vfs/src/security/mod.rs b/fs/rk-vfs/src/security/mod.rs index 539be432..d777c35d 100644 --- a/fs/rk-vfs/src/security/mod.rs +++ b/fs/rk-vfs/src/security/mod.rs @@ -23,6 +23,7 @@ use crate::cred::{Cred, IpcOwner}; use crate::dentry::Location; use crate::file::File; use crate::inode::{Inode, InodeRef, Result, SetAttr}; +use crate::user_ns::UserNs; use alloc::string::String; use alloc::sync::Arc; use alloc::vec::Vec; @@ -408,18 +409,18 @@ pub trait SockNames { /// `security_capable`: the task with `cred` has the capability `cap`. `opts` has the /// `CAP_OPT_*` bits. -pub fn capable(cred: &Cred, cap: u32, opts: u32) -> bool { - call_int_hook!(capable, cred, cap, opts).is_ok() +pub fn capable(cred: &Cred, ns: &UserNs, cap: u32, opts: u32) -> bool { + call_int_hook!(capable, cred, ns, cap, opts).is_ok() } /// `security_capable` after the capability module: the other active modules check the -/// capability `cap` of the task with `cred`. The caller has done the check of the capability -/// module, `cap_capable` of Linux, in the user namespace of the request. -pub fn capable_lsm(cred: &Cred, cap: u32, opts: u32) -> bool { +/// capability `cap` of the task with `cred` in the user namespace `ns`. The caller has done the +/// check of the capability module, `cap_capable` of Linux, in `ns`. +pub fn capable_lsm(cred: &Cred, ns: &UserNs, cap: u32, opts: u32) -> bool { modules() .iter() .filter(|m| m.info.id.id != id::CAPABILITY) - .all(|m| m.info.hooks.capable(cred, cap, opts).is_ok()) + .all(|m| m.info.hooks.capable(cred, ns, cap, opts).is_ok()) } /// `security_capget`. diff --git a/fs/rk-vfs/src/security/tests.rs b/fs/rk-vfs/src/security/tests.rs index 7e3c3fb4..f8c318bb 100644 --- a/fs/rk-vfs/src/security/tests.rs +++ b/fs/rk-vfs/src/security/tests.rs @@ -118,7 +118,7 @@ fn the_first_error_stops_the_call() { #[test] fn the_capability_module_checks_the_effective_set() { let mut c = Cred::root(); - assert!(capable(&c, cap::SYS_ADMIN, 0)); + assert!(capable(&c, crate::user_ns::init(), cap::SYS_ADMIN, 0)); c.cap_effective = cap::bit(cap::NET_BIND_SERVICE); assert!(!c.capable(cap::SYS_ADMIN)); assert!(c.capable(cap::NET_BIND_SERVICE)); diff --git a/sec/rk-lsm-apparmor/src/lib.rs b/sec/rk-lsm-apparmor/src/lib.rs index a3a0d453..e5abf21a 100644 --- a/sec/rk-lsm-apparmor/src/lib.rs +++ b/sec/rk-lsm-apparmor/src/lib.rs @@ -53,12 +53,13 @@ use rk_vfs::security::{ self, BlobSizes, Bprm, ExecFiles, FLAG_EXCLUSIVE, FLAG_LEGACY_MAJOR, LsmId, LsmInfo, Order, SecurityHooks, Sock, Task, id, }; +use rk_vfs::user_ns::UserNs; /// The hooks of AppArmor. A hook gives the error of a denial of a profile in enforce mode. struct AppArmor; impl SecurityHooks for AppArmor { - fn capable(&self, cred: &Cred, cap: u32, opts: u32) -> Result<()> { + fn capable(&self, cred: &Cred, _ns: &UserNs, cap: u32, opts: u32) -> Result<()> { capability::capable(cred, cap, opts) } diff --git a/sec/rk-lsm-apparmor/src/ns.rs b/sec/rk-lsm-apparmor/src/ns.rs index a2f4f367..a0948cf2 100644 --- a/sec/rk-lsm-apparmor/src/ns.rs +++ b/sec/rk-lsm-apparmor/src/ns.rs @@ -33,7 +33,7 @@ use core::sync::atomic::{AtomicI32, Ordering}; use rk_abi::Errno; use rk_vfs::cred::{self, Cred, cap}; use rk_vfs::inode::Result; -use rk_vfs::security::Sysctl; +use rk_vfs::security::{self, Sysctl}; /// `AA_USERNS_CREATE`. const USERNS_CREATE: u32 = 8; @@ -160,7 +160,7 @@ fn profile_ns_perm(p: Option<&Arc>, cred: &Cred) -> Result