* [PATCH v3 1/3] drm/vmwgfx: Use get_file_active() for dmabuf->file
2026-10-13 12:02 [PATCH v3 0/3] dma-buf: dmabuf->file release-time cleanup Matt Evans
@ 2026-10-13 12:02 ` Matt Evans
2026-10-13 12:02 ` [PATCH v3 2/3] drm/i915/gvt: Read dmabuf->file only while valid Matt Evans
2026-10-13 12:02 ` [PATCH v3 3/3] dma-buf: Annul dmabuf->file on file release Matt Evans
2 siblings, 0 replies; 5+ messages in thread
From: Matt Evans @ 2026-10-13 12:02 UTC (permalink / raw)
To: Christian König, Sumit Semwal, Zack Rusin, Zhenyu Wang,
Zhi Wang, Jani Nikula, Joonas Lahtinen, Rodrigo Vivi,
Tvrtko Ursulin
Cc: intel-gfx, linux-media, dri-devel, linaro-mm-sig, linux-kernel,
Alex Mastro, Alex Williamson, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann, David Airlie, stable
4ab59c3c638c6 ("dma-buf: Move dma_buf_release() from fops to
dentry_ops") moved the dmabuf .release, and broke the vmwgfx driver's
assumption that dmabuf->file would remain valid until release.
Currently, dmabuf->file can be stale after the file closes even if
.release has not yet been called, and a future commit makes
dmabuf->file become NULL instead of stale.
In preparation for that future commit, convert
get_dma_buf_unless_doomed() to use get_file_active(&dmabuf->file)
which is the safe method to access and get a reference, even when
dmabuf->file could be NULL.
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>
---
drivers/gpu/drm/vmwgfx/ttm_object.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/vmwgfx/ttm_object.c b/drivers/gpu/drm/vmwgfx/ttm_object.c
index f9042bafdc93..9137a31eaa1a 100644
--- a/drivers/gpu/drm/vmwgfx/ttm_object.c
+++ b/drivers/gpu/drm/vmwgfx/ttm_object.c
@@ -471,7 +471,7 @@ void ttm_object_device_release(struct ttm_object_device **p_tdev)
*/
static bool __must_check get_dma_buf_unless_doomed(struct dma_buf *dmabuf)
{
- return file_ref_get(&dmabuf->file->f_ref);
+ return get_file_active(&dmabuf->file) != NULL;
}
/**
--
2.47.3
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v3 2/3] drm/i915/gvt: Read dmabuf->file only while valid
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-13 12:02 ` Matt Evans
2026-10-13 12:02 ` [PATCH v3 3/3] dma-buf: Annul dmabuf->file on file release Matt Evans
2 siblings, 0 replies; 5+ messages in thread
From: Matt Evans @ 2026-10-13 12:02 UTC (permalink / raw)
To: Christian König, Sumit Semwal, Zack Rusin, Zhenyu Wang,
Zhi Wang, Jani Nikula, Joonas Lahtinen, Rodrigo Vivi,
Tvrtko Ursulin
Cc: intel-gfx, linux-media, dri-devel, linaro-mm-sig, linux-kernel,
Alex Mastro, Alex Williamson, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann, David Airlie, stable
After 4ab59c3c638c6 ("dma-buf: Move dma_buf_release() from fops to
dentry_ops"), dmabuf->file can close prior to the .release callback
being invoked.
intel_vgpu_get_dmabuf() exports the dmabuf, then dma_buf_fd()
effectively transfers its file's reference to the fd. A debug print
dereferences dmabuf->file after the dma_buf_fd(); however, the fd
could be closed by another thread, making dmabuf->file stale.
Read file_count() while the reference to dmabuf->file is still held,
for later debug printing.
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>
---
drivers/gpu/drm/i915/gvt/dmabuf.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/drivers/gpu/drm/i915/gvt/dmabuf.c b/drivers/gpu/drm/i915/gvt/dmabuf.c
index 8a1d6c5636c2..b6f50f2b912d 100644
--- a/drivers/gpu/drm/i915/gvt/dmabuf.c
+++ b/drivers/gpu/drm/i915/gvt/dmabuf.c
@@ -503,6 +503,7 @@ int intel_vgpu_get_dmabuf(struct intel_vgpu *vgpu, unsigned int dmabuf_id)
struct drm_device *dev = &vgpu->gvt->gt->i915->drm;
struct intel_vgpu_dmabuf_obj *dmabuf_obj;
struct drm_i915_gem_object *obj;
+ unsigned long db_fcount;
struct dma_buf *dmabuf;
int dmabuf_fd;
int ret = 0;
@@ -531,6 +532,7 @@ int intel_vgpu_get_dmabuf(struct intel_vgpu *vgpu, unsigned int dmabuf_id)
ret = PTR_ERR(dmabuf);
goto out_free_gem;
}
+ db_fcount = file_count(dmabuf->file);
ret = dma_buf_fd(dmabuf, DRM_CLOEXEC | DRM_RDWR);
if (ret < 0) {
@@ -552,8 +554,7 @@ int intel_vgpu_get_dmabuf(struct intel_vgpu *vgpu, unsigned int dmabuf_id)
" file count: %ld, GEM ref: %d\n",
vgpu->id, dmabuf_obj->dmabuf_id,
kref_read(&dmabuf_obj->kref),
- dmabuf_fd,
- file_count(dmabuf->file),
+ dmabuf_fd, db_fcount,
kref_read(&obj->base.refcount));
i915_gem_object_put(obj);
--
2.47.3
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v3 3/3] dma-buf: Annul dmabuf->file on file release
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-13 12:02 ` [PATCH v3 2/3] drm/i915/gvt: Read dmabuf->file only while valid Matt Evans
@ 2026-10-13 12:02 ` Matt Evans
2026-10-13 12:13 ` Christian König
2 siblings, 1 reply; 5+ messages in thread
From: Matt Evans @ 2026-10-13 12:02 UTC (permalink / raw)
To: Christian König, Sumit Semwal, Zack Rusin, Zhenyu Wang,
Zhi Wang, Jani Nikula, Joonas Lahtinen, Rodrigo Vivi,
Tvrtko Ursulin
Cc: intel-gfx, linux-media, dri-devel, linaro-mm-sig, linux-kernel,
Alex Mastro, Alex Williamson, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann, David Airlie, stable
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>
---
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;
--
2.47.3
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v3 3/3] dma-buf: Annul dmabuf->file on file release
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
0 siblings, 0 replies; 5+ messages in thread
From: Christian König @ 2026-10-13 12:13 UTC (permalink / raw)
To: Matt Evans, Sumit Semwal, Zack Rusin, Zhenyu Wang, Zhi Wang,
Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin
Cc: intel-gfx, linux-media, dri-devel, linaro-mm-sig, linux-kernel,
Alex Mastro, Alex Williamson, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann, David Airlie, stable
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>
> ---
> 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;
>
^ permalink raw reply [flat|nested] 5+ messages in thread