[PATCH v6 5/7] lsm: Add memcg_charge_cgroup LSM hook
From: Albert Esteve
Date: Tue Nov 24 2026 - 08:05:25 EST
Similar to how security_current_memcg_charge_task adds an access-control
point for pidfd for the charge_fd of a DMA_HEAP_IOCTL_ALLOC or
DMA_BUF_IOCTL_XFER_CHARGE ioctl, we need a new access-control point
for the case where charge_fd represents a cgroupfd.
Add security_current_memcg_charge_cgroup(), called in
get_obj_cgroup_from_fd() when charge_fd refers to a cgroupfs
directory. The hook receives the open file of the cgroup directory to
charge (file), giving security modules a controlled enforcement point
for cross-cgroup memcg attribution policy.
The file is passed instead of the cgroup so that a module can
label-check the object the cgroupfd actually names.
get_obj_cgroup_from_fd() resolves the memory css from that same file
with css_tryget_online_from_dir(), so the fd cannot be swapped between
the check and the charge.
When CONFIG_SECURITY is not set the hook compiles to an inline
returning 0, adding no overhead to the fast path.
Signed-off-by: Albert Esteve <aesteve@xxxxxxxxxx>
---
include/linux/lsm_hook_defs.h | 1 +
include/linux/security.h | 6 ++++++
mm/memcontrol.c | 9 ++++++++-
security/security.c | 17 +++++++++++++++++
4 files changed, 32 insertions(+), 1 deletion(-)
diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
index beb339663d76..72f474596bf0 100644
--- a/include/linux/lsm_hook_defs.h
+++ b/include/linux/lsm_hook_defs.h
@@ -44,6 +44,7 @@ LSM_HOOK(int, 0, capset, struct cred *new, const struct cred *old,
LSM_HOOK(int, 0, capable, const struct cred *cred, struct user_namespace *ns,
int cap, unsigned int opts)
LSM_HOOK(int, 0, memcg_charge_task, const struct cred *cred)
+LSM_HOOK(int, 0, memcg_charge_cgroup, const struct file *file)
LSM_HOOK(int, 0, quotactl, int cmds, int type, int id, const struct super_block *sb)
LSM_HOOK(int, 0, quota_on, struct dentry *dentry)
LSM_HOOK(int, 0, syslog, int type)
diff --git a/include/linux/security.h b/include/linux/security.h
index 6c6cb08bb1c6..69a309919ea7 100644
--- a/include/linux/security.h
+++ b/include/linux/security.h
@@ -353,6 +353,7 @@ int security_capable(const struct cred *cred,
int cap,
unsigned int opts);
int security_current_memcg_charge_task(const struct cred *cred);
+int security_current_memcg_charge_cgroup(const struct file *file);
int security_quotactl(int cmds, int type, int id, const struct super_block *sb);
int security_quota_on(struct dentry *dentry);
int security_syslog(int type);
@@ -716,6 +717,11 @@ static inline int security_current_memcg_charge_task(const struct cred *cred)
return 0;
}
+static inline int security_current_memcg_charge_cgroup(const struct file *file)
+{
+ return 0;
+}
+
static inline int security_quotactl(int cmds, int type, int id,
const struct super_block *sb)
{
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 41c81e492007..725a910d8eb5 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -3223,7 +3223,14 @@ struct obj_cgroup *get_obj_cgroup_from_fd(int fd)
css = ERR_PTR(-EBADF);
}
- if (IS_ERR(css)) {
+ if (!IS_ERR(css)) {
+ /* fd is a cgroupfd */
+ ret = security_current_memcg_charge_cgroup(fd_file(f));
+ if (ret) {
+ css_put(css);
+ return ERR_PTR(ret);
+ }
+ } else {
if (PTR_ERR(css) != -EBADF)
return ERR_CAST(css);
diff --git a/security/security.c b/security/security.c
index 78b462b0ece5..72cad6b1d3e7 100644
--- a/security/security.c
+++ b/security/security.c
@@ -688,6 +688,23 @@ int security_current_memcg_charge_task(const struct cred *cred)
return call_int_hook(memcg_charge_task, cred);
}
+/**
+ * security_current_memcg_charge_cgroup() - Check if charging memory to a cgroup is allowed
+ * @file: open file of the cgroup directory to charge
+ *
+ * Check whether the current process is allowed to charge memory to the
+ * cgroup referenced by @file. Called when userspace passes a cgroupfd to
+ * identify the charge target instead of a pidfd. @file is the file the
+ * cgroup was resolved from, so the check applies to the object that is
+ * actually charged. It must have been already validated by the caller.
+ *
+ * Return: Returns 0 if permission is granted.
+ */
+int security_current_memcg_charge_cgroup(const struct file *file)
+{
+ return call_int_hook(memcg_charge_cgroup, file);
+}
+
/**
* security_quotactl() - Check if a quotactl() syscall is allowed for this fs
* @cmds: commands
--
2.55.0