The security_memcg_charge_cgroup() hook allows security modules to control which processes may charge memcg memory to an arbitrary cgroup named by a cgroupfd passed in charge_fd. Without a policy implementation, the hook is a no-op, so a domain that has both dma-heap access and CAP_SYS_RESOURCE can charge any cgroup outside its own subtree, and SELinux cannot say which targets are acceptable.
Implement selinux_memcg_charge_cgroup() using avc_has_perm() with a new cgroup object class and a charge_to permission. This is the counterpart of the process2 charge_to permission used for the pidfd path.
The target SID is the SID of the inode of the cgroup directory the cgroupfd refers to, so access control follows the cgroup filesystem labeling. Policy authors can then grant cross-cgroup charging selectively, for example:
allow allocator_app_t cgroup_t:cgroup charge_to;
Signed-off-by: Albert Esteve aesteve@redhat.com --- security/selinux/hooks.c | 10 ++++++++++ security/selinux/include/classmap.h | 1 + 2 files changed, 11 insertions(+)
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 70de6094b2b4..fcf0b03a588d 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -2210,6 +2210,15 @@ static int selinux_memcg_charge_task(const struct cred *from, const struct cred SECCLASS_PROCESS2, PROCESS2__CHARGE_TO, NULL); }
+static int selinux_memcg_charge_cgroup(const struct cred *cred, + const struct file *file) +{ + struct inode_security_struct *isec = inode_security(file_inode(file)); + + return avc_has_perm(cred_sid(cred), isec->sid, SECCLASS_CGROUP, + CGROUP__CHARGE_TO, NULL); +} + static int selinux_quotactl(int cmds, int type, int id, const struct super_block *sb) { const struct cred *cred = current_cred(); @@ -7688,6 +7697,7 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = { LSM_HOOK_INIT(capset, selinux_capset), LSM_HOOK_INIT(capable, selinux_capable), LSM_HOOK_INIT(memcg_charge_task, selinux_memcg_charge_task), + LSM_HOOK_INIT(memcg_charge_cgroup, selinux_memcg_charge_cgroup), LSM_HOOK_INIT(quotactl, selinux_quotactl), LSM_HOOK_INIT(quota_on, selinux_quota_on), LSM_HOOK_INIT(syslog, selinux_syslog), diff --git a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h index 4004c05aae5b..32dd95de509a 100644 --- a/security/selinux/include/classmap.h +++ b/security/selinux/include/classmap.h @@ -181,6 +181,7 @@ const struct security_class_mapping secclass_map[] = { { "user_namespace", { "create", NULL } }, { "memfd_file", { COMMON_FILE_PERMS, "execute_no_trans", "entrypoint", NULL } }, + { "cgroup", { "charge_to", NULL } }, /* last one */ { NULL, {} } };