Re: [PATCH v5 4/8] lsm: Add memcg_charge_task LSM hook

From: Albert Esteve

Date: Wed Nov 18 2026 - 08:55:48 EST


On Tue, Nov 17, 2026 at 11:54 PM Paul Moore <paul@xxxxxxxxxxxxxx> wrote:
>
> On Nov 17, 2026 Albert Esteve <aesteve@xxxxxxxxxx> wrote:
> >
> > 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 an arbitrary process's cgroup 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_memcg_charge_task(), called in get_obj_cgroup_from_fd()
> > when charge_fd refers to another process. The hook receives
> > the credentials of the allocating process (from) and the credentials
> > of the process whose cgroup will be charged (to), 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 | 7 +++++++
> > mm/memcontrol.c | 11 +++++++++++
> > security/security.c | 15 +++++++++++++++
> > 4 files changed, 34 insertions(+)
>
> ...
>
> > diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> > index 03cd90d5e623..2e6fcbe14d43 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>
> > @@ -3198,10 +3199,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);
> > @@ -3226,6 +3229,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_memcg_charge_task(current_cred(), tcred);
>
> Do you ever envision this "from" cred being something other than
> current_cred()?

Good point. No, I do not.

> If the answer is "no", we might want to consider changing
> the LSM hook slightly to help prevent misuse in the future:
>
> int security_current_memcg_charge_task(const struct cred *tcred);

Agreed. I'll apply the same to the cgroupfd variant for symmetry (its
"from" is also always current_cred()).

BR,
Albert.


>
> > + 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);
>
> --
> paul-moore.com
>