diff --git a/CHANGELOG.md b/CHANGELOG.md index fbb7d906..5b564482 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This file records each release. The format follows [Keep a Changelog](https://ke ### Added - Multipath routes, as Linux. An IPv4 `RTM_NEWROUTE` with `RTA_MULTIPATH` makes a route with a path for each `rtnexthop`, with the checks of `fib_count_nexthops`, `fib_get_nhs` and `fib_check_nh`, and a weight of `rtnh_hops` and one. A request with one path makes a route with one path. The dump shows `RTA_MULTIPATH`, and `RTM_DELROUTE` with paths matches them in order, as `fib_nh_match`. A device that goes down marks its paths `dead linkdown`, and the route goes away when all of its paths are dead. An IPv6 route with `RTA_MULTIPATH` makes a route for each path, and these routes are siblings, as `ip6_route_multipath_add`. The dump and the notifications show the siblings in one `RTA_MULTIPATH` with their weights, and a delete without a gateway removes the siblings too. An IPv6 path whose device goes down also stays, marked `dead linkdown`, while another path is alive, as `fib6_ifdown`. A flow takes a path from a hash of its addresses and the weights, so its packets stay in order. Before, the kernel did not use `RTA_MULTIPATH` in a route (#538). +- 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). ### Fixed 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