The security_memcg_charge_task() hook allows security modules
to control which processes may charge/transfer memcg
memory to another process's cgroup outside their tree. Without
a policy implementation, the hook is a no-op and the restriction
is not enforced.

On SELinux-managed systems the only limit is CAP_SYS_RESOURCE. A
domain that holds both dma-heap access (today's only exporter that
opts-in) and that capability, can charge memory to any cgroup outside
its own subtree. Since the capability is all-or-nothing, SELinux
policy cannot express which targets such a domain may charge to.

Implement selinux_memcg_charge_task() using avc_has_perm() from
the process2 object class and a new charge_to permission. Policy
authors can then grant cross-cgroup charging selectively,
for example:

  allow allocator_app_t client_app_t:process2 charge_to;

Signed-off-by: Albert Esteve <[email protected]>
---
 security/selinux/hooks.c            | 7 +++++++
 security/selinux/include/classmap.h | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 6168dd5f91c2..70de6094b2b4 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -2204,6 +2204,12 @@ static int selinux_capable(const struct cred *cred, 
struct user_namespace *ns,
        return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
 }
 
+static int selinux_memcg_charge_task(const struct cred *from, const struct 
cred *to)
+{
+       return avc_has_perm(cred_sid(from), cred_sid(to),
+                           SECCLASS_PROCESS2, PROCESS2__CHARGE_TO, NULL);
+}
+
 static int selinux_quotactl(int cmds, int type, int id, const struct 
super_block *sb)
 {
        const struct cred *cred = current_cred();
@@ -7681,6 +7687,7 @@ static struct security_hook_list selinux_hooks[] 
__ro_after_init = {
        LSM_HOOK_INIT(capget, selinux_capget),
        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(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 90cb61b16425..4004c05aae5b 100644
--- a/security/selinux/include/classmap.h
+++ b/security/selinux/include/classmap.h
@@ -60,7 +60,7 @@ const struct security_class_mapping secclass_map[] = {
            "siginh",       "setrlimit",     "rlimitinh",   "dyntransition",
            "setcurrent",   "execmem",       "execstack",   "execheap",
            "setkeycreate", "setsockcreate", "getrlimit",   NULL } },
-       { "process2", { "nnp_transition", "nosuid_transition", NULL } },
+       { "process2", { "nnp_transition", "nosuid_transition", "charge_to", 
NULL } },
        { "system",
          { "ipc_info", "syslog_read", "syslog_mod", "syslog_console",
            "module_request", "module_load", "firmware_load",

-- 
2.55.0

Reply via email to