Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Albert Esteve" <aesteve@redhat.com>, "Tejun Heo" <tj@kernel.org>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	"Michal Koutný" <mkoutny@suse.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Sumit Semwal" <sumit.semwal@linaro.org>,
	"Christian König" <christian.koenig@amd.com>,
	"Michal Hocko" <mhocko@kernel.org>,
	"Roman Gushchin" <roman.gushchin@linux.dev>,
	"Shakeel Butt" <shakeel.butt@linux.dev>,
	"Muchun Song" <muchun.song@linux.dev>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Benjamin Gaignard" <benjamin.gaignard@collabora.com>,
	"Brian Starkey" <Brian.Starkey@arm.com>,
	"John Stultz" <jstultz@google.com>,
	"T.J. Mercier" <tjmercier@google.com>,
	"Christian Brauner" <brauner@kernel.org>,
	"Paul Moore" <paul@paul-moore.com>,
	"James Morris" <jmorris@namei.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	"Stephen Smalley" <stephen.smalley.work@gmail.com>,
	"Shuah Khan" <shuah@kernel.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Ondrej Mosnáček" <omosnacek@gmail.com>,
	"Jens Wiklander" <jenswi@kernel.org>,
	"Sumit Garg" <sumit.garg@kernel.org>
Cc: dri-devel@lists.freedesktop.org, linux-mm@kvack.org,
	linux-security-module@vger.kernel.org, selinux@vger.kernel.org,
	op-tee@lists.trustedfirmware.org, echanude@redhat.com,
	mripard@kernel.org, media-ci@linuxtv.org
Subject: Re: [PATCH v3 7/8] dma-buf: transfer memcg charges via DMA_BUF_IOCTL_XFER_CHARGE
Date: Wed, 28 Oct 2026 15:04:39 +0000	[thread overview]
Message-ID: <sashiko-outbox-188032@kernel.org> (raw)
In-Reply-To: <20261027-v2_20230123_tjmercier_google_com-v3-7-78d2c4cb8647@redhat.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] dma-buf: unprivileged cgroup escape and OOM via dma_buf_xfer_charge()
- [High] dma-buf: race in mem_cgroup_transfer_dmabuf_charge() double-charges memcg
- [Medium] dma-buf: broken disabled-memcg handling in dma_buf_xfer_charge()
--

commit 1660729e62b15fd915c69f26225530bd5ae30ce0
Author: Albert Esteve <aesteve@redhat.com>

dma-buf: transfer memcg charges via DMA_BUF_IOCTL_XFER_CHARGE

This commit adds DMA_BUF_IOCTL_XFER_CHARGE to allow transferring a dma-buf's
memory cgroup charge to a cgroup identified by a cgroupfd or pidfd after
allocation. It also introduces a memcg_account flag in dma_buf_export_info for
exporters to opt in to memcg accounting, enabling it for the system heap.

> diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
> index f31f49ebd0af3..07513ecedc105 100644
> --- a/drivers/dma-buf/dma-buf.c
> +++ b/drivers/dma-buf/dma-buf.c
> @@ -540,6 +540,45 @@ static long dma_buf_import_sync_file(struct dma_buf *dmabuf,
>  }
>  #endif
>  
> +/* Transfer a dma-buf's memcg charge; see struct dma_buf_transfer_charge */
> +static long dma_buf_xfer_charge(struct dma_buf *dmabuf,
> +				const void __user *user_data)
> +{
> +	struct dma_buf_transfer_charge arg;
> +	struct mem_cgroup *memcg;
> +	int ret = 0;
> +
> +	if (copy_from_user(&arg, user_data, sizeof(arg)))
> +		return -EFAULT;
> +
> +	if (!IS_ENABLED(CONFIG_MEMCG))
> +		return 0;
> +
> +	if (arg.flags != 0)
> +		return -EINVAL;
> +
> +	if (!dmabuf->memcg_account)
> +		return -EOPNOTSUPP;

[Severity: Medium]
When CONFIG_MEMCG is disabled, should dma_buf_xfer_charge() still validate
arg.flags and dmabuf->memcg_account, or return -EOPNOTSUPP?

Returning 0 before checking arg.flags != 0 and !dmabuf->memcg_account causes
calls with invalid flags, non-accounting exporters, or invalid file
descriptors to silently succeed under CONFIG_MEMCG=n, while also leaving the
!CONFIG_MEMCG stubs for mem_cgroup_get_from_fd() (which returns
ERR_PTR(-EOPNOTSUPP)) and mem_cgroup_transfer_dmabuf_charge() in
include/linux/memcontrol.h unreachable.

Also, when CONFIG_MEMCG=y and cgroup_disable=memory is passed on the kernel
command line (so mem_cgroup_disabled() is true at runtime),
mem_cgroup_get_from_fd() behaves inconsistently across its two branches:

mm/memcontrol.c:mem_cgroup_get_from_fd() {
	cgrp = cgroup_get_from_fd(fd);
	if (!IS_ERR(cgrp)) {
		rcu_read_lock();
		css = cgroup_css(cgrp, &memory_cgrp_subsys);
		if (!css || !css_tryget_online(css)) {
			rcu_read_unlock();
			cgroup_put(cgrp);
			return ERR_PTR(-ENOENT);
		}
...
	memcg = get_mem_cgroup_from_mm(mm);
	mmput(mm);
	return memcg;
}

For a cgroupfd, cgroup_css() is NULL and it returns ERR_PTR(-ENOENT), whereas
for a pidfd, get_mem_cgroup_from_mm() returns NULL rather than an ERR_PTR, so
dma_buf_xfer_charge() returns 0 for pidfds and -ENOENT for cgroupfds.

> +
> +	memcg = mem_cgroup_get_from_fd(arg.charge_fd);
> +	if (IS_ERR(memcg))
> +		return PTR_ERR(memcg);
> +
> +	if (dmabuf->memcg == memcg)
> +		goto skip_transfer;
> +
> +	if (!mem_cgroup_transfer_dmabuf_charge(dmabuf, memcg)) {
> +		ret = -ENOMEM;
> +		goto skip_transfer;
> +	}

[Severity: High]
Can an unprivileged holder of a dma-buf file descriptor use
dma_buf_xfer_charge() to escape its own memory cgroup limit or trigger OOM
kills in another cgroup?

In the call chain:

  dma_buf_ioctl()
    -> dma_buf_xfer_charge()
      -> mem_cgroup_get_from_fd()
      -> mem_cgroup_transfer_dmabuf_charge()
        -> mem_cgroup_reserve_dmabuf(new_memcg, GFP_KERNEL, nr_pages)
          -> try_charge()

neither dma_buf_xfer_charge() nor mem_cgroup_get_from_fd() checks DAC or
cgroup delegation/write permissions (such as inode_permission() with MAY_WRITE
or cgroup_procs_write_permission()), capabilities, or ptrace_may_access() on
either the source cgroup (dmabuf->memcg) being uncharged or the target
cgroup/task (new_memcg) being charged.

Because /sys/fs/cgroup directories are normally world-readable (mode 0755, so
any user can open them with O_RDONLY | O_DIRECTORY) and pidfd_open() can open
a pidfd for PID 1 or any visible process without privilege, an unprivileged
process can resolve a handle to the root cgroup or to another cgroup on the
system.

When SELinux is not active, security_dma_buf_charge_cgroup() and
security_dma_buf_charge_task() return 0. Even with SELinux enabled,
selinux_dma_buf_charge_cgroup() returns 0 when the cgroup kernfs node has no
explicit security.selinux xattr set:

security/selinux/hooks.c:selinux_dma_buf_charge_cgroup() {
	rc = kernfs_xattr_get(cgrp->kn, XATTR_NAME_SELINUX, NULL, 0);
	if (rc == -ENODATA)
		return 0;
}

and neither hook checks permission to uncharge the existing dmabuf->memcg. As
a result, a confined client receiving a pre-charged dma-buf fd can move the
charge to the root cgroup to bypass its own memory.max limit, or move the
charge into a victim cgroup where mem_cgroup_reserve_dmabuf() with GFP_KERNEL
triggers direct reclaim and the memcg OOM killer.

> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 7b841b9057594..3adc84fe6668d 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -5786,6 +5786,47 @@ void mem_cgroup_cancel_dmabuf(struct mem_cgroup *memcg, unsigned int nr_pages)
>  		refill_stock(memcg, nr_pages);
>  }
>  
> +/**
> + * mem_cgroup_transfer_dmabuf_charge - move a dma-buf charge
> + * @dmabuf: buffer to transfer charge
> + * @new_memcg: target memcg; caller must hold a reference
> + *
> + * Transfers the charge for @dmabuf to @new_memcg and, on
> + * success, stores @new_memcg in @dmabuf->memcg.
> + * Returns %true on success, %false if the charge against @new_memcg
> + * failed or a concurrent transfer raced on @dmabuf->memcg.
> + * On failure the caller retains its reference to @new_memcg.
> + */
> +bool mem_cgroup_transfer_dmabuf_charge(struct dma_buf *dmabuf,
> +				       struct mem_cgroup *new_memcg)
> +{
> +	struct mem_cgroup *old_memcg = READ_ONCE(dmabuf->memcg);
> +	unsigned int nr_pages = PAGE_ALIGN(dmabuf->size) >> PAGE_SHIFT;
> +
> +	if (mem_cgroup_disabled())
> +		return true;
> +
> +	if (!mem_cgroup_reserve_dmabuf(new_memcg, GFP_KERNEL, nr_pages))
> +		return false;
> +
> +	mod_memcg_state(new_memcg, MEMCG_DMABUF, nr_pages);
> +
> +	if (cmpxchg(&dmabuf->memcg, old_memcg, new_memcg) != old_memcg) {
> +		mod_memcg_state(new_memcg, MEMCG_DMABUF, -nr_pages);
> +		mem_cgroup_cancel_dmabuf(new_memcg, nr_pages);
> +		return false;
> +	}

[Severity: High]
What happens if two threads concurrently call DMA_BUF_IOCTL_XFER_CHARGE on the
same dma-buf?

In dma_buf_xfer_charge(), dmabuf->memcg == memcg is checked without
synchronization before calling mem_cgroup_transfer_dmabuf_charge(), which then
re-reads old_memcg = READ_ONCE(dmabuf->memcg) without checking whether
old_memcg == new_memcg.

If thread 1 transfers the buffer to new_memcg after thread 2 passes the
dmabuf->memcg == memcg check in dma_buf_xfer_charge() (or if both threads
concurrently transfer the buffer to the same new_memcg), thread 2 sees
old_memcg == new_memcg and still calls mem_cgroup_reserve_dmabuf(new_memcg,
GFP_KERNEL, nr_pages) before uncharging old_memcg. That transiently charges
2 * nr_pages against new_memcg for the same buffer, which can trigger reclaim,
memcg OOM kills, or -ENOMEM in new_memcg even though the buffer is already
charged to it.

In addition, old_memcg is read before mem_cgroup_reserve_dmabuf(new_memcg,
GFP_KERNEL, nr_pages), which can sleep in direct reclaim. If another thread
updates dmabuf->memcg while mem_cgroup_reserve_dmabuf() is sleeping, the
cmpxchg(&dmabuf->memcg, old_memcg, new_memcg) fails without retrying against
the updated old_memcg and returns false, which dma_buf_xfer_charge() reports
to userspace as -ENOMEM even though charging new_memcg succeeded.

Finally, if mem_cgroup_transfer_dmabuf_charge() is called when
mem_cgroup_disabled() is true, returning true at the top without storing
new_memcg in dmabuf->memcg or dropping new_memcg's reference would leak the
 caller's reference to new_memcg, since callers only call mem_cgroup_put() when
this function returns false.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261027-v2_20230123_tjmercier_google_com-v3-0-78d2c4cb8647@redhat.com?part=7


  parent reply	other threads:[~2026-10-28 15:04 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-27 15:02 [PATCH v3 0/8] memcg: dma-buf cgroup accounting and charge transfer Albert Esteve
2026-10-27 15:02 ` [PATCH v3 1/8] memcg: Add MEMCG_DMABUF accounting infrastructure Albert Esteve
2026-10-27 15:02 ` [PATCH v3 2/8] dma-heap: charge dma-buf memory via explicit memcg Albert Esteve
2026-10-28 15:04   ` sashiko-bot
2026-10-27 15:02 ` [PATCH v3 3/8] security: dma-buf: Add dma_buf_charge_task LSM hook Albert Esteve
2026-10-28 15:04   ` sashiko-bot
2026-10-27 15:02 ` [PATCH v3 4/8] security: dma-buf: Add dma_buf_charge_cgroup " Albert Esteve
2026-10-27 15:02 ` [PATCH v3 5/8] selinux: Restrict cross-cgroup dma-buf charging Albert Esteve
2026-10-27 15:02 ` [PATCH v3 6/8] selinux: Restrict dma-buf direct cgroup charging Albert Esteve
2026-10-28 15:04   ` sashiko-bot
2026-10-27 15:02 ` [PATCH v3 7/8] dma-buf: transfer memcg charges via DMA_BUF_IOCTL_XFER_CHARGE Albert Esteve
2026-10-28  9:08   ` Christian König
2026-10-28 16:09     ` Albert Esteve
2026-10-28 15:04   ` sashiko-bot [this message]
2026-10-27 15:02 ` [PATCH v3 8/8] selftests/dmabuf-heaps: Add dma-buf memcg accounting tests Albert Esteve
2026-10-28 15:04   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-188032@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Brian.Starkey@arm.com \
    --cc=aesteve@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=benjamin.gaignard@collabora.com \
    --cc=brauner@kernel.org \
    --cc=christian.koenig@amd.com \
    --cc=corbet@lwn.net \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=echanude@redhat.com \
    --cc=hannes@cmpxchg.org \
    --cc=jenswi@kernel.org \
    --cc=jmorris@namei.org \
    --cc=jstultz@google.com \
    --cc=linux-mm@kvack.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=media-ci@linuxtv.org \
    --cc=mhocko@kernel.org \
    --cc=mkoutny@suse.com \
    --cc=mripard@kernel.org \
    --cc=muchun.song@linux.dev \
    --cc=omosnacek@gmail.com \
    --cc=op-tee@lists.trustedfirmware.org \
    --cc=paul@paul-moore.com \
    --cc=rdunlap@infradead.org \
    --cc=roman.gushchin@linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=selinux@vger.kernel.org \
    --cc=serge@hallyn.com \
    --cc=shakeel.butt@linux.dev \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=stephen.smalley.work@gmail.com \
    --cc=sumit.garg@kernel.org \
    --cc=sumit.semwal@linaro.org \
    --cc=tj@kernel.org \
    --cc=tjmercier@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox