dma-buf: dmabuf->file release-time cleanup
Subsystems (8)
v3 cover dma-buf: dmabuf->file release-time cleanup
Hi all,
A fix for DMABUF's file-release path, to set dmabuf->file = NULL when the struct file itself is being freed and avoid a stale pointer lingering. Previously it could be assumed that dmabuf->file was always valid until .release was called, but commit 4ab59c3c638c6 ("dma-buf: Move dma_buf_release() from fops to dentry_ops") separated dentry release (calls .release) from file release (can happen before .release). Setting the pointer to NULL clearly delimits the period in which it points to a valid struct file.
Raw dmabuf->file access is bad news unless the file is known to have a reference held (for example, during export). A single-threaded problem scenario is a dentry held open (e.g. using O_PATH) but the file closed; .release is not called, so the exporter was unaware that dmabuf->file became stale. Exporter activities using dmabuf->file then led to a UAF. Or, threads racing concurrent access with a file closing.
VFIO uses get_file_active(&dmabuf->file), which is a UAF when the pointer goes stale. Alex Mastro has created a VFIO-centric reproducer [1], which shows up with KASAN.
To fix, dmabuf->file is either valid or NULL. get_file_active() is then safe and a recommended way to access dmabuf->file without an existing reference. The only possible outcomes are for it to get a reference to the file, to observe a valid pointer but the file in the DEAD state (being closed, fail), or NULL (fail).
Patch [3/3] sets the pointer NULL when the file closes, which get_file_active() handles correctly.
vmwgfx was identified as doing a problematic dmabuf->file dereference. Although already a UAF, making dmabuf->file = NULL turns it into an oops. So, patch [1/3] correspondingly converts vmwgfx to use get_file_active() too.
i915 GVT unsafely dereferences dmabuf->file for a trivial debug printout after the file reference moves to the dmabuf's fd, so patch [2/3] dereferences in the safe window.
Added comments to highlight that dmabuf->file could go away before .release, and to use get_file_active() if a reference isn't otherwise already held.
Thanks,
Matt
[1]: https://github.com/opsound/vfio-dmabuf-lab/blob/main/tests/vfio_dmabuf_opath_uaf_test.c
Changes:
v3:
- Better comment for struct dma_buf's file (refer to synchronisation against ->release).
- Added trivial i915 patch to remove unsafe debug dmabuf->file accesss.
v2: https://lore.kernel.org/all/[email protected]/
"v1" was the single "dma-buf: Annul dmabuf->file on file release" patch:
https://lore.kernel.org/all/[email protected]/
Matt Evans (3):
drm/vmwgfx: Use get_file_active() for dmabuf->file drm/i915/gvt: Read dmabuf->file only while valid
dma-buf: Annul dmabuf->file on file release
drivers/dma-buf/dma-buf.c | 21 ++++++++++++++++++--- drivers/gpu/drm/i915/gvt/dmabuf.c | 5 +++-- drivers/gpu/drm/vmwgfx/ttm_object.c | 2 +- include/linux/dma-buf.h | 5 ++++- 4 files changed, 26 insertions(+), 7 deletions(-)
Signature · 1 line
2.47.3
PATCH v3 1/3 drm/vmwgfx: Use get_file_active() for dmabuf->file
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.
--- drivers/gpu/drm/vmwgfx/ttm_object.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/vmwgfx/ttm_object.c +1 -1
@@ -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 ; }/**
Signature · 1 line
2.47.3
Re: [PATCH v3 1/3] drm/vmwgfx: Use get_file_active() for dmabuf->file
Quoted 34 lines · Matt Evans
4ab59c3c638c6("dma-buf: Movedma_buf_release()from fops todentry_ops") moved the dmabuf .release, and broke the vmwgfx driver's assumption thatdmabuf->filewould remain valid until release. Currently,dmabuf->filecan be stale after the file closes even if .release has not yet been called, and a future commit makesdmabuf->filebecome NULL instead of stale.In preparation for that future commit, convert
get_dma_buf_unless_doomed()to useget_file_active(&dmabuf->file)which is the safe method to access and get a reference, even whendmabuf->filecould be NULL.Fixes 4ab59c3c638c6 ("dma-buf: Move dma_buf_release() from fops to dentry_ops")Signed-off-by Matt Evans <[email protected]>--- drivers/gpu/drm/vmwgfx/ttm_object.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/gpu/drm/vmwgfx/ttm_object.c +1
-2@@ -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 ; }/** --
Quoted 1 line
2.47.3
Looks good. Thanks! Feel free to use either Suggested-by or
z
PATCH v3 2/3 drm/i915/gvt: Read dmabuf->file only while valid
In reply to Matt EvansAfter 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.
--- drivers/gpu/drm/i915/gvt/dmabuf.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gvt/dmabuf.c +3 -2
@@ -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);
Signature · 1 line
2.47.3
Re: [PATCH v3 2/3] drm/i915/gvt: Read dmabuf->file only while valid
Quoted 51 lines · Matt Evans
After
4ab59c3c638c6("dma-buf: Movedma_buf_release()from fops todentry_ops"),dmabuf->filecan close prior to the .release callback being invoked.
intel_vgpu_get_dmabuf()exports the dmabuf, thendma_buf_fd()effectively transfers its file's reference to the fd. A debug print dereferencesdmabuf->fileafter thedma_buf_fd(); however, the fd could be closed by another thread, makingdmabuf->filestale.Read
file_count()while the reference todmabuf->fileis still held, for later debug printing.Fixes 4ab59c3c638c6 ("dma-buf: Move dma_buf_release() from fops to dentry_ops")Signed-off-by Matt Evans <[email protected]>--- drivers/gpu/drm/i915/gvt/dmabuf.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)drivers/gpu/drm/i915/gvt/dmabuf.c +3
-2@@ -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);Signature · 1 line
2.47.3
PATCH v3 3/3 dma-buf: Annul dmabuf->file on file release
In reply to Matt EvansThe 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.
--- drivers/dma-buf/dma-buf.c | 21 ++++++++++++++++++--- include/linux/dma-buf.h | 5 ++++- 2 files changed, 22 insertions(+), 4 deletions(-)
drivers/dma-buf/dma-buf.c +18 -3
@@ -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 ; }
include/linux/dma-buf.h +4 -1
@@ -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;
Signature · 1 line
2.47.3
Re: [PATCH v3 3/3] dma-buf: Annul dmabuf->file on file release
Quoted 32 lines · Matt Evans
The dmabuf release path is split between file and dentry release:
dma_buf_file_release()is called shortly before the file is freed, anddma_buf_release()calls an exporter'sdmabuf->ops->releasewhen the dentry is freed. However, the dentry can outlive the file (for example, if opened withO_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->filepoints 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()ondmabuf->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->fileto NULL indma_buf_file_release()to reflect that the associated file is now dead even if the DMABUF is not yet gone. Aget_file_active()...fput()sequence concurrent with a file close then only executes as one of:- Gets the file before
file_ref_put()(dmabuf->filevalid)
- Observesdmabuf->filepointing to a file, but it's DEAD (no file)
- Observesdmabuf->file= NULL (no file)Originally, drivers could assume
dmabuf->filewas valid until .release was called fromfops->release. This assumption was no longer valid after4ab59c3c638c6("dma-buf: Movedma_buf_release()from fops todentry_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 thatdmabuf->filecould be NULL before .release.
Quoted 3 lines
Fixes 4ab59c3c638c6 ("dma-buf: Move dma_buf_release() from fops to dentry_ops")Signed-off-by Matt Evans <[email protected]>
Quoted 55 lines
--- drivers/dma-buf/dma-buf.c | 21 ++++++++++++++++++--- include/linux/dma-buf.h | 5 ++++- 2 files changed, 22 insertions(+), 4 deletions(-)drivers/dma-buf/dma-buf.c +18
-3@@ -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 ; }include/linux/dma-buf.h +4
-1@@ -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;
Re: [PATCH v3 3/3] dma-buf: Annul dmabuf->file on file release
Hello DMA and DRM folks,
Quoted 33 lines · Christian König
Quoted 32 lines · Matt Evans
The dmabuf release path is split between file and dentry release:
dma_buf_file_release()is called shortly before the file is freed, anddma_buf_release()calls an exporter'sdmabuf->ops->releasewhen the dentry is freed. However, the dentry can outlive the file (for example, if opened withO_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->filepoints 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()ondmabuf->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->fileto NULL indma_buf_file_release()to reflect that the associated file is now dead even if the DMABUF is not yet gone. Aget_file_active()...fput()sequence concurrent with a file close then only executes as one of:- Gets the file before
file_ref_put()(dmabuf->filevalid)
- Observesdmabuf->filepointing to a file, but it's DEAD (no file)
- Observesdmabuf->file= NULL (no file)Originally, drivers could assume
dmabuf->filewas valid until .release was called fromfops->release. This assumption was no longer valid after4ab59c3c638c6("dma-buf: Movedma_buf_release()from fops todentry_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 thatdmabuf->filecould be NULL before .release.
Quoted 5 lines
Quoted 3 lines
Fixes 4ab59c3c638c6 ("dma-buf: Move dma_buf_release() from fops to dentry_ops")Signed-off-by Matt Evans <[email protected]>Reviewed-by Christian König <[email protected]>
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
Quoted 57 lines
Quoted 56 lines
--- drivers/dma-buf/dma-buf.c | 21 ++++++++++++++++++--- include/linux/dma-buf.h | 5 ++++- 2 files changed, 22 insertions(+), 4 deletions(-)drivers/dma-buf/dma-buf.c +18
-3@@ -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 ; }include/linux/dma-buf.h +4
-1@@ -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;
Re: [PATCH v3 3/3] dma-buf: Annul dmabuf->file on file release
In reply to Matt EvansQuoted 92 lines · Matt Evans
The dmabuf release path is split between file and dentry release:
dma_buf_file_release()is called shortly before the file is freed, anddma_buf_release()calls an exporter'sdmabuf->ops->releasewhen the dentry is freed. However, the dentry can outlive the file (for example, if opened withO_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->filepoints 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()ondmabuf->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->fileto NULL indma_buf_file_release()to reflect that the associated file is now dead even if the DMABUF is not yet gone. Aget_file_active()...fput()sequence concurrent with a file close then only executes as one of:- Gets the file before
file_ref_put()(dmabuf->filevalid)
- Observesdmabuf->filepointing to a file, but it's DEAD (no file)
- Observesdmabuf->file= NULL (no file)Originally, drivers could assume
dmabuf->filewas valid until .release was called fromfops->release. This assumption was no longer valid after4ab59c3c638c6("dma-buf: Movedma_buf_release()from fops todentry_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 thatdmabuf->filecould be NULL before .release.Fixes 4ab59c3c638c6 ("dma-buf: Move dma_buf_release() from fops to dentry_ops")Signed-off-by Matt Evans <[email protected]>--- drivers/dma-buf/dma-buf.c | 21 ++++++++++++++++++--- include/linux/dma-buf.h | 5 ++++- 2 files changed, 22 insertions(+), 4 deletions(-)drivers/dma-buf/dma-buf.c +18
-3@@ -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 ; }include/linux/dma-buf.h +4
-2@@ -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; --
Quoted 2 lines
2.47.3
z
Patchwork
CI✗ LGCI.VerificationFailed: failure for dma-buf: dmabuf->file release-time cleanup
In reply to Matt Evans== Series Details ==
== Series Details ==
Series: dma-buf: dmabuf->file release-time cleanup
URL : https://patchwork.freedesktop.org/series/176136/
State : failure
== Summary ==
Series author address '[email protected]' is not on the allowlist, which prevents CI from being automatically triggered. If you want CI to run for this series, ask Patchwork project owners to click 'retest' on the series in Patchwork. Exception occurred during validation, bailing out!
Build URL: http://gfx-ci.igk.intel.com:8080/job/CI_PW_kernel/190226/ (on built-in)