From: Matt Evans <matt@ozlabs.org>
To: "Christian König" <christian.koenig@amd.com>,
"Sumit Semwal" <sumit.semwal@linaro.org>,
"Zack Rusin" <zack.rusin@broadcom.com>,
"Zhenyu Wang" <zhenyuw.linux@gmail.com>,
"Zhi Wang" <zhi.wang.linux@gmail.com>,
"Jani Nikula" <jani.nikula@linux.intel.com>,
"Joonas Lahtinen" <joonas.lahtinen@linux.intel.com>,
"Rodrigo Vivi" <rodrigo.vivi@intel.com>,
"Tvrtko Ursulin" <tursulin@ursulin.net>
Cc: intel-gfx@lists.freedesktop.org, linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org, Alex Mastro <amastro@fb.com>,
Alex Williamson <alex@shazbot.org>,
Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
Maxime Ripard <mripard@kernel.org>,
Thomas Zimmermann <tzimmermann@suse.de>,
David Airlie <airlied@gmail.com>,
stable@vger.kernel.org
Subject: Re: [PATCH v3 3/3] dma-buf: Annul dmabuf->file on file release
Date: Mon, 2 Nov 2026 15:13:26 +0000 [thread overview]
Message-ID: <e9464db2-0cfa-4a61-8adc-eee51e30fd05@ozlabs.org> (raw)
In-Reply-To: <3e3e0974-20f9-4aaa-a6b8-a7fb4727ad8e@amd.com>
Hello DMA and DRM folks,
On 13/10/2026 13:13, Christian König wrote:
> On 10/13/26 14:02, Matt Evans wrote:
>> The dmabuf release path is split between file and dentry release:
>> dma_buf_file_release() is called shortly before the file is freed, and
>> dma_buf_release() calls an exporter's dmabuf->ops->release when the
>> dentry is freed. However, the dentry can outlive the file (for
>> example, if opened with O_PATH), meaning .release might be called some
>> time after the file is freed.
>>
>> This presents a window, when closing a DMABUF file, in which
>> dmabuf->file points to freed memory yet .release op has not yet been
>> called.
>>
>> For VFIO, if a buffer's .release has not been called it's considered
>> still active and subject to move/cleanup. If so, it attempts to
>> get_file_active() on dmabuf->file: a file close with dentry held open
>> will call this function with a stale pointer, a UAF.
>>
>> To make this pattern safe, set dmabuf->file to NULL in
>> dma_buf_file_release() to reflect that the associated file is now dead
>> even if the DMABUF is not yet gone. A get_file_active() ... fput()
>> sequence concurrent with a file close then only executes as one of:
>>
>> - Gets the file before file_ref_put() (dmabuf->file valid)
>> - Observes dmabuf->file pointing to a file, but it's DEAD (no file)
>> - Observes dmabuf->file = NULL (no file)
>>
>> Originally, drivers could assume dmabuf->file was valid until .release
>> was called from fops->release. This assumption was no longer valid
>> after 4ab59c3c638c6 ("dma-buf: Move dma_buf_release() from fops to
>> dentry_ops"), which moved the callback to the dentry release (by which
>> point the file might have been freed). With this commit, drivers must
>> still consider that dmabuf->file could be NULL before .release.
>>
>> Cc: stable@vger.kernel.org
>> Fixes: 4ab59c3c638c6 ("dma-buf: Move dma_buf_release() from fops to dentry_ops")
>> Signed-off-by: Matt Evans <matt@ozlabs.org>
>
> Reviewed-by: Christian König <christian.koenig@amd.com>
Thank you. Zack's also added R-Bs for 1/3 and 3/3, and Zhenyu an Ack on
2/3.
If this is acceptable, which tree would you recommend could take this fix?
Thanks,
Matt
>
>> ---
>> drivers/dma-buf/dma-buf.c | 21 ++++++++++++++++++---
>> include/linux/dma-buf.h | 5 ++++-
>> 2 files changed, 22 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
>> index 4c9add51f9ef..61485fc28b8b 100644
>> --- a/drivers/dma-buf/dma-buf.c
>> +++ b/drivers/dma-buf/dma-buf.c
>> @@ -193,11 +193,26 @@ static void dma_buf_release(struct dentry *dentry)
>>
>> static int dma_buf_file_release(struct inode *inode, struct file *file)
>> {
>> - if (!is_dma_buf_file(file))
>> - return -EINVAL;
>> + struct dma_buf *dmabuf = file->private_data;
>> +
>> + if (!dmabuf)
>> + return 0;
>>
>> - __dma_buf_list_del(file->private_data);
>> + __dma_buf_list_del(dmabuf);
>>
>> + /*
>> + * As the file is being closed, dmabuf->file is no longer
>> + * valid and NULL replaces an otherwise stale pointer. If a
>> + * reference to the file isn't already held, the file can be
>> + * closed at any time, including before dmabuf->ops->release()
>> + * happens: dmabuf->file _must not_ be accessed directly. An
>> + * exporter should use get_file_active(&dmabuf->file) which
>> + * atomically tests and acquires a reference to the file (or
>> + * returns NULL if the file has closed). The barrier ensures
>> + * get_file_active() observes the store before file_free()
>> + * frees the memory.
>> + */
>> + smp_store_mb(dmabuf->file, NULL);
>> return 0;
>> }
>>
>> diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
>> index d15b2b31d3c9..a31eb0ab9ecf 100644
>> --- a/include/linux/dma-buf.h
>> +++ b/include/linux/dma-buf.h
>> @@ -303,7 +303,10 @@ struct dma_buf {
>> * @file:
>> *
>> * File pointer used for sharing buffers across, and for refcounting.
>> - * See dma_buf_get() and dma_buf_put().
>> + * See dma_buf_get() and dma_buf_put(). When an exporter holds a
>> + * common lock with the ->release() callback, it can use
>> + * get_file_active(&dma_buf->file) to safely acquire a reference;
>> + * the file could otherwise be closed at any time.
>> */
>> struct file *file;
>>
>
next prev parent reply other threads:[~2026-11-02 15:13 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-13 12:02 [PATCH v3 0/3] dma-buf: dmabuf->file release-time cleanup Matt Evans
2026-10-13 12:02 ` [PATCH v3 1/3] drm/vmwgfx: Use get_file_active() for dmabuf->file Matt Evans
2026-10-15 16:42 ` Zack Rusin
2026-10-13 12:02 ` [PATCH v3 2/3] drm/i915/gvt: Read dmabuf->file only while valid Matt Evans
2026-10-15 5:25 ` Zhenyu Wang
2026-10-13 12:02 ` [PATCH v3 3/3] dma-buf: Annul dmabuf->file on file release Matt Evans
2026-10-13 12:13 ` Christian König
2026-11-02 15:13 ` Matt Evans [this message]
2026-10-15 16:45 ` Zack Rusin
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=e9464db2-0cfa-4a61-8adc-eee51e30fd05@ozlabs.org \
--to=matt@ozlabs.org \
--cc=airlied@gmail.com \
--cc=alex@shazbot.org \
--cc=amastro@fb.com \
--cc=christian.koenig@amd.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=intel-gfx@lists.freedesktop.org \
--cc=jani.nikula@linux.intel.com \
--cc=joonas.lahtinen@linux.intel.com \
--cc=linaro-mm-sig@lists.linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=maarten.lankhorst@linux.intel.com \
--cc=mripard@kernel.org \
--cc=rodrigo.vivi@intel.com \
--cc=stable@vger.kernel.org \
--cc=sumit.semwal@linaro.org \
--cc=tursulin@ursulin.net \
--cc=tzimmermann@suse.de \
--cc=zack.rusin@broadcom.com \
--cc=zhenyuw.linux@gmail.com \
--cc=zhi.wang.linux@gmail.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
all inboxes | Powered by JetHome®