All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: Falconet3 <Falconet3@proton.me>
Cc: "dri-devel@lists.freedesktop.org"
	<dri-devel@lists.freedesktop.org>,
	media-ci@linuxtv.org
Subject: Re: [PATCH] dma-buf: fix leak of ctx and dmabuf reference in dma_buf_io_ctx_create() WARN path
Date: Sun, 22 Nov 2026 00:11:31 +0000	[thread overview]
Message-ID: <sashiko-outbox-227561@kernel.org> (raw)
In-Reply-To: <htm5h9oaY-vk0g9JVYJmz5XCaKe_nPoWkUJYJqDulyNvv_sAWfzSMp0T6GWt5okc8R4SyvXXpJQ3VGhetKnjEa2KGem5uCYdxpmdzUFf6XA=@proton.me>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] dma-buf: use-after-free on WARN_ON_ONCE path in
  dma_buf_io_ctx_create()
- [Low] dma-buf: malformed Fixes trailer and scratch notes in commit
  message
--

commit e9d30f7c9b6146d91fac7e600ae7718d7d49f057
Author: Falconet3 <Falconet3@proton.me>

> dma-buf: fix leak of ctx and dmabuf reference in dma_buf_io_ctx_create() WARN path
>
> (leak of ctx + dmabuf ref on the WARN_ON_ONCE path, new file in v7.4-rc1, patch attached)
>
> The file drivers/dma-buf/dma-buf-io.c (and the io_uring dmabuf regbuf
> feature on top of it) is new in v7.4-rc1. In dma_buf_io_ctx_create(),
> the error path of init_dma_buf_io_ctx() correctly does
> kfree(ctx) + dma_buf_put(dmabuf) (dma-buf-io.c:178-182), but the
> WARN_ON_ONCE() path right below it returns -EINVAL without freeing
> either (dma-buf-io.c:184-188):
>
>         if (WARN_ON_ONCE(!ctx->dev_ops || ...))
>                 return -EINVAL;          /* leaks ctx and the get_dma_buf() ref */
>
> Reachable from userspace via io_uring buffer registration
> (IORING_REGISTER_BUF / rsrc update with IO_REGBUF_TYPE_DMABUF,
> io_uring/dma-buf.c -> dma_buf_io_ctx_create()) whenever an importer's
> init_dma_buf_io_ctx() succeeds but leaves dev_ops incomplete:
> kernel memory + dma-buf reference leak (DoS-grade).
>
> Affects v7.4-rc1..v7.4-rc3 only; the file does not exist in v7.3.5.
>
> Fix was verified with git apply --check against v7.4-rc3; not
> build-tested and no reproducer available in this environment.
>
> Fixes: FILL-IN-SHA ("dma-buf: introduce ... file I/O ...")
> Assisted-by: LLM Mistral Vibe
> Signed-off-by: Falconet3 <Falconet3@proton.me>

[Severity: Low]
The Fixes trailer is still a placeholder and should be:

  Fixes: 0d3120db2cbe ("dma-buf: introduce initial file I/O infrastructure")

Also, the submission scratch notes at the top and bottom should be dropped
from the changelog, and the claim that this path is reachable from
userspace is inaccurate because the only in-tree init_dma_buf_io_ctx()
implementation (nvme_init_io_ctx_locked()) populates all three callbacks in
nvme_dma_buf_io_ops.

> diff --git a/drivers/dma-buf/dma-buf-io.c b/drivers/dma-buf/dma-buf-io.c
> index 8440151de8600..d4d37e109a7e4 100644
> --- a/drivers/dma-buf/dma-buf-io.c
> +++ b/drivers/dma-buf/dma-buf-io.c
> @@ -184,8 +184,11 @@ int dma_buf_io_ctx_create(struct file *file,
>  	if (WARN_ON_ONCE(!ctx->dev_ops ||
>  			 !ctx->dev_ops->map ||
>  			 !ctx->dev_ops->unmap ||
> -			 !ctx->dev_ops->release))
> +			 !ctx->dev_ops->release)) {
> +		kfree(ctx);
> +		dma_buf_put(dmabuf);
>  		return -EINVAL;
> +	}
>  
>  	*out_ctx = ctx;
>  	return 0;

[Severity: Medium]
Can freeing ctx and dropping its dmabuf reference here cause a
use-after-free once file->f_op->init_dma_buf_io_ctx() has succeeded?

When init_dma_buf_io_ctx() returns 0, the importer has already attached ctx
to dmabuf and linked it into importer tracking lists:

drivers/nvme/host/pci-dmabuf.c:nvme_init_io_ctx_locked() {
	...
	attach = dma_buf_dynamic_attach(ctx->dmabuf, ctrl->dev,
					&nvme_dmabuf_importer_ops, ctx);
	...
	list_add(&nvme_ctx->list, &buf_list->ctxs);
	nvme_ctx->attach = attach;
	nvme_ctx->base = ctx;
	...
}

As in dma_buf_io_ctx_release(), ctx->dev_ops->release(ctx) must run before
dma_buf_put() and kfree(ctx) to detach the importer and remove ctx from its
tracking list.  Freeing ctx directly without detaching leaves dangling
pointers in attach->importer_priv and nvme_ctx->base, causing a
use-after-free on subsequent mapping invalidation or controller teardown in
__nvme_pci_remove_dmabuf().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/2xySoxkTOy_cwwTsVb3MjcYIN2J0LJftOab-bhMICCuE_LxaBfqRKUCBOrBQQpQeTBzizv3ci1RqJH09_VL9RbAFk9n8dvQO9YaWIoIT7tE=@proton.me?part=1

  reply	other threads:[~2026-11-22  0:11 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22  0:05 [PATCH] dma-buf: fix leak of ctx and dmabuf reference in dma_buf_io_ctx_create() WARN path Falconet3
2026-11-22  0:11 ` sashiko-bot [this message]
2026-11-22  0:49 ` Jens Axboe
2026-11-22 10:49 ` Falconet3
2026-11-23 13:42   ` Pavel Begunkov
  -- strict thread matches above, loose matches on Subject: below --
2026-11-22  0:03 Falconet3

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-227561@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Falconet3@proton.me \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=media-ci@linuxtv.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.