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 <[email protected]> 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-78d2c4cb8...@redhat.com?part=7
