[PATCH v6 4/7] lsm: Add memcg_charge_task LSM hook
From: Albert Esteve
Date: Tue Nov 24 2026 - 08:05:10 EST
Both DMA_HEAP_IOCTL_ALLOC and the new DMA_BUF_IOCTL_XFER_CHARGE ioctl
accept a charge_fd field that, when set, causes the allocation to be
charged to the cgroup named by the fd (a pidfd or a cgroupfd) rather
than the caller's.
Charging is currently limited to the caller's own memcg subtree unless
the caller holds CAP_SYS_RESOURCE. But the capability is all-or-nothing.
A process holding it, along with a dma-heap device node or a dma-buf
fd can charge unlimited memory to any other process's cgroup, potentially
exhausting that cgroup's limit and triggering OOM kills independent of
the victim's own activity or privileges.
Add security_current_memcg_charge_task(), called in
get_obj_cgroup_from_fd() when charge_fd refers to another process.
The hook receives the credentials of the process whose cgroup will
be charged (cred), giving security modules a controlled enforcement
point for cross-cgroup objcg attribution policy.
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 | 11 +++++++++++
security/security.c | 14 ++++++++++++++
4 files changed, 32 insertions(+)
diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
index 87d9d06710cb..beb339663d76 100644
--- a/include/linux/lsm_hook_defs.h
+++ b/include/linux/lsm_hook_defs.h
@@ -43,6 +43,7 @@ LSM_HOOK(int, 0, capset, struct cred *new, const struct cred *old,
const kernel_cap_t *permitted)
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, 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 40831fab5470..6c6cb08bb1c6 100644
--- a/include/linux/security.h
+++ b/include/linux/security.h
@@ -352,6 +352,7 @@ int security_capable(const struct cred *cred,
struct user_namespace *ns,
int cap,
unsigned int opts);
+int security_current_memcg_charge_task(const struct cred *cred);
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);
@@ -710,6 +711,11 @@ static inline int security_capable(const struct cred *cred,
return cap_capable(cred, ns, cap, opts);
}
+static inline int security_current_memcg_charge_task(const struct cred *cred)
+{
+ 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 bf5b2672c0c7..41c81e492007 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -60,6 +60,7 @@
#include <linux/lockdep.h>
#include <linux/resume_user_mode.h>
#include <linux/psi.h>
+#include <linux/security.h>
#include <linux/seq_buf.h>
#include <linux/sched/isolation.h>
#include <linux/kmemleak.h>
@@ -3201,10 +3202,12 @@ static bool mem_cgroup_may_charge(struct mem_cgroup *memcg)
struct obj_cgroup *get_obj_cgroup_from_fd(int fd)
{
struct cgroup_subsys_state *css;
+ const struct cred *tcred;
struct obj_cgroup *objcg;
struct mem_cgroup *memcg;
struct task_struct *task;
unsigned int pidfd_flags;
+ int ret;
if (mem_cgroup_disabled())
return ERR_PTR(-EOPNOTSUPP);
@@ -3229,6 +3232,14 @@ struct obj_cgroup *get_obj_cgroup_from_fd(int fd)
if (IS_ERR(task))
return ERR_CAST(task);
+ tcred = get_task_cred(task);
+ ret = security_current_memcg_charge_task(tcred);
+ put_cred(tcred);
+ if (ret) {
+ put_task_struct(task);
+ return ERR_PTR(ret);
+ }
+
rcu_read_lock();
css = task_css(task, memory_cgrp_id);
put_task_struct(task);
diff --git a/security/security.c b/security/security.c
index 4727b8347fc4..78b462b0ece5 100644
--- a/security/security.c
+++ b/security/security.c
@@ -674,6 +674,20 @@ int security_capable(const struct cred *cred,
return call_int_hook(capable, cred, ns, cap, opts);
}
+/**
+ * security_current_memcg_charge_task() - Check if cross-cgroup memory charging is allowed
+ * @cred: credentials of the process to charge
+ *
+ * Check whether the current process is allowed to charge
+ * memory to the cgroup of the process with credentials @cred.
+ *
+ * Return: Returns 0 if permission is granted.
+ */
+int security_current_memcg_charge_task(const struct cred *cred)
+{
+ return call_int_hook(memcg_charge_task, cred);
+}
+
/**
* security_quotactl() - Check if a quotactl() syscall is allowed for this fs
* @cmds: commands
--
2.55.0