Re: [PATCH v6 6/7] selinux: Restrict cross-cgroup memcg charging
From: Christian Göttsche
Date: Tue Nov 24 2026 - 08:50:18 EST
On Tue, 24 Nov 2026 at 14:05, Albert Esteve <aesteve@xxxxxxxxxx> wrote:
>
> The security_current_memcg_charge_task() and
> security_current_memcg_charge_cgroup() hooks allow security modules to
> control which processes may charge or transfer memcg memory to another
> process's cgroup, named by a pidfd, or to an arbitrary cgroup named by
> a cgroupfd, both passed in charge_fd. Without a policy implementation,
> either 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 both selinux_memcg_charge_task() and
> selinux_memcg_charge_cgroup() using avc_has_perm() with a new cgroup
> object class and a new charge_to permission. Both charge_fd flavours
> ultimately charge a cgroup, so one permission covers them and the
> target type keeps the distinction: for the pidfd path the target is
> the referred task's SID, as there is no cgroup object to take a label
> from; for the cgroupfd path it is the cgroup directory's inode SID,
> which comes from the genfscon entry for cgroupfs unless the directory
> is relabeled.
>
> The checks run when the charge target is resolved, before the
> CAP_SYS_RESOURCE baseline is evaluated, and apply to every explicit
> charge_fd target, including the caller's own subtree, so policy needs
> a rule even for self-charging. Policy authors can then grant
> cross-cgroup charging selectively, for example:
>
> allow allocator_app_t client_app_t:cgroup charge_to;
> allow allocator_app_t cgroup_t:cgroup charge_to;
>
> Signed-off-by: Albert Esteve <aesteve@xxxxxxxxxx>
> ---
> security/selinux/hooks.c | 19 +++++++++++++++++++
> security/selinux/include/classmap.h | 1 +
> 2 files changed, 20 insertions(+)
>
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index 6c4c64d6e904..76a714107ae4 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -2204,6 +2204,23 @@ 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 *to)
> +{
> + const struct cred *cred = current_cred();
> +
> + return avc_has_perm(cred_sid(cred), cred_sid(to),
Small nit:
`cred_sid(current_cred())` could be simplified using `current_sid()`
> + SECCLASS_CGROUP, CGROUP__CHARGE_TO, NULL);
> +}
> +
> +static int selinux_memcg_charge_cgroup(const struct file *file)
> +{
> + const struct cred *cred = current_cred();
> + 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();
> @@ -7683,6 +7700,8 @@ 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(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 90cb61b16425..1f1f2fc4d35d 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, {} }
> };
>
>
> --
> 2.55.0
>
>