dma-buf: Annul dmabuf->file on file release
Subsystems (2)
PATCH 1/1 dma-buf: Annul dmabuf->file on file release
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 will only execute 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.
---
Hi,
This issue was found (by Claude Opus 5.5) in the context of VFIO's DMABUF export path. VFIO iterates live DMABUFs with a get_file_active()/fput() block, which now becomes safe if the file is closed (and memory freed!) yet DMABUF .release hasn't yet occurred.
However, there are a couple of other places that directly use dmabuf->file and seem able to race a closing file (i.e. without holding the file reference)? If this is so, they'd be a UAF today; with this patch that goes away, but instead of a stale pointer dmabuf->file could be NULL:
1. drivers/gpu/drm/vmwgfx/ttm_object.c:get_dma_buf_unless_doomed()
file_ref_get(&dmabuf->file->f_ref); on a non-refcounted DMABUF
The commit message of 90ee6ed776c0 ("fs: port files to file_ref") hints this might be more subtle than replacing it with a get_file() variant (so as to accept a NULL file *).
2. drivers/gpu/drm/i915/gvt/dmabuf.c:intel_vgpu_get_dmabuf()
gvt_dbg_dpy(... file_count(dmabuf->file) ...);
Respective vmwgfx & i915 maintainers, what is your view?
Or, indeed, if anyone sees any other questionable uses of dmabuf->file.
Thanks,
Matt
drivers/dma-buf/dma-buf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/dma-buf/dma-buf.c +5 -1
@@ -193,11 +193,15 @@ static void dma_buf_release(struct dentry *dentry)static int dma_buf_file_release (struct inode *inode,struct file *file) { +struct dma_buf *dmabuf = file->private_data; +if (!is_dma_buf_file (file))return -EINVAL ; -__dma_buf_list_del (file->private_data); +__dma_buf_list_del (dmabuf); +/* Must be observed by __get_file_rcu() before file_free() */ +smp_store_mb (dmabuf->file,NULL );return 0 ; }
Signature · 1 line
2.47.3
Christian König
maintainerHi Matt,
Quoted 25 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 will only execute 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)
Quoted 6 lines
Originally, drivers could assume
dmabuf->filewas valid until .release was called from fops->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.
Oh well, really good catch. And that problem is wider than this.
Quoted 2 lines
Fixes 4ab59c3c638c6 ("dma-buf: Move dma_buf_release() from fops to dentry_ops")Signed-off-by Matt Evans <[email protected]>
We seriously need a CC: stable here.
Quoted 23 lines
---
Hi,
This issue was found (by Claude Opus 5.5) in the context of VFIO's DMABUF export path. VFIO iterates live DMABUFs with a get_file_active()/fput() block, which now becomes safe if the file is closed (and memory freed!) yet DMABUF .release hasn't yet occurred.
However, there are a couple of other places that directly use
dmabuf->fileand seem able to race a closing file (i.e. without holding the file reference)? If this is so, they'd be a UAF today; with this patch that goes away, but instead of a stale pointerdmabuf->filecould be NULL:1. drivers/gpu/drm/vmwgfx/ttm_object.c:get_dma_buf_unless_doomed()
file_ref_get(&dmabuf->file->f_ref); on a non-refcounted DMABUF
The commit message of
90ee6ed776c0("fs: port files tofile_ref") hints this might be more subtle than replacing it with aget_file()variant (so as to accept a NULL file *).
Quoted 7 lines
2. drivers/gpu/drm/i915/gvt/dmabuf.c:intel_vgpu_get_dmabuf()
gvt_dbg_dpy(...
file_count(dmabuf->file)...);Respective vmwgfx & i915 maintainers, what is your view?
Or, indeed, if anyone sees any other questionable uses ofdmabuf->file.
*sigh* we had multiple occasions suggesting to add a dma_buf_get_rcu()/active() because exporters wanted to keep their DMA-buf on linked lists or other container structures.
That was always rejected because the DMA-buf framework had never guaranteed that such things work correctly exactly because of those lifetime issues here. In other words nobody thought that through and document what should happen when etc...
So the use cases in vmgfx and i915 are just the tip of the iceberg :)
My suggestion is to get this patch reviewed and backported first and then come up with a dma_buf_get_active() function including documentation and comments and test case to make sure that this feature works *and* keeps working.
One minor comment below, apart from that the patch itself looks good to me.
Quoted 15 lines
Thanks,
Matt
drivers/dma-buf/dma-buf.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)drivers/dma-buf/dma-buf.c +0
-0@@ -193,11 +193,15 @@ static void dma_buf_release(struct dentry *dentry)
Quoted 7 lines
static intdma_buf_file_release(struct inode *inode, struct file *file){
+ structdma_buf*dmabuf =file->private_data;
+
if (!is_dma_buf_file(file))
return-EINVAL;
I would do the casting only after the check.
On the other hand this function is a callback of the file_operations so checking if file->ops is really pointing to the file operations is completely superfluous.
So if you want to remove the check instead I'm perfectly fine with that as well.
Regards,
Christian.
Quoted 9 lines
-__dma_buf_list_del(file->private_data);
+__dma_buf_list_del(dmabuf);
+ /* Must be observed by__get_file_rcu()beforefile_free()*/ +smp_store_mb(dmabuf->file, NULL);
return 0;
}
Hi Christian,
Quoted 35 lines · Christian König
Hi Matt,
Quoted 25 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 will only execute 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)Quoted 6 lines
Originally, drivers could assume
dmabuf->filewas valid until .release was called from fops->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
Oh well, really good catch. And that problem is wider than this.
Quoted 2 lines
Fixes 4ab59c3c638c6 ("dma-buf: Move dma_buf_release() from fops to dentry_ops")Signed-off-by Matt Evans <[email protected]>We seriously need a CC: stable here.
I knew I forgot something :(
Quoted 28 lines
Quoted 28 lines
---
Hi,
This issue was found (by Claude Opus 5.5) in the context of VFIO's DMABUF export path. VFIO iterates live DMABUFs with a get_file_active()/fput() block, which now becomes safe if the file is closed (and memory freed!) yet DMABUF .release hasn't yet occurred.
However, there are a couple of other places that directly use
dmabuf->fileand seem able to race a closing file (i.e. without holding the file reference)? If this is so, they'd be a UAF today; with this patch that goes away, but instead of a stale pointerdmabuf->filecould be NULL:1. drivers/gpu/drm/vmwgfx/ttm_object.c:get_dma_buf_unless_doomed()
file_ref_get(&dmabuf->file->f_ref); on a non-refcounted DMABUF
The commit message of
90ee6ed776c0("fs: port files tofile_ref") hints this might be more subtle than replacing it with aget_file()variant (so as to accept a NULL file *).2. drivers/gpu/drm/i915/gvt/dmabuf.c:intel_vgpu_get_dmabuf()
gvt_dbg_dpy(...
file_count(dmabuf->file)...);
Quoted 8 lines
Quoted 2 lines
Respective vmwgfx & i915 maintainers, what is your view?
Or, indeed, if anyone sees any other questionable uses ofdmabuf->file.*sigh* we had multiple occasions suggesting to add a dma_buf_get_rcu()/active() because exporters wanted to keep their DMA-buf on linked lists or other container structures.
That was always rejected because the DMA-buf framework had never guaranteed that such things work correctly exactly because of those lifetime issues here. In other words nobody thought that through and document what should happen when etc...
So the use cases in vmgfx and i915 are just the tip of the iceberg :)
Yay :)
Quoted 3 lines
My suggestion is to get this patch reviewed and backported first and then come up with a
dma_buf_get_active()function including documentation and comments and test case to make sure that this feature works *and* keeps working.One minor comment below, apart from that the patch itself looks good to me.
This makes sense to me, thank you for the context also.
Just to make sure I thoroughly understand, do you envisage a dma_buf_get_active() to take both a reference on a dmabuf and its corresponding dmabuf->file so such users can move to a pattern of get- test-use-put on the file portion too? Sorry if it's a silly question, I'm not sure what to search for.
Quoted 18 lines
Quoted 8 lines
drivers/dma-buf/dma-buf.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)drivers/dma-buf/dma-buf.c +0
-0@@ -193,11 +193,15 @@ static void dma_buf_release(struct dentry *dentry)Quoted 7 lines
static intdma_buf_file_release(struct inode *inode, struct file *file){
+ structdma_buf*dmabuf =file->private_data;
+
if (!is_dma_buf_file(file))
return-EINVAL;I would do the casting only after the check.
Quoted 3 lines
On the other hand this function is a callback of the
file_operationsso checking iffile->opsis really pointing to the file operations is completely superfluous.So if you want to remove the check instead I'm perfectly fine with that as well.
I had given the unfamiliar code the benefit of the doubt, but this did look a bit suspect! Will do, thanks.
Matt
PS: I should've mentioned that Alex Mastro has kindly built a reproducer using a VFIO DMABUF export and dentry-held-open scenario. KASAN will detect the UAF:
https://github.com/opsound/vfio-dmabuf-lab/blob/main/tests/vfio_dmabuf_opath_uaf_test.c
Quoted 14 lines
Regards,
Christian.Quoted 9 lines
-__dma_buf_list_del(file->private_data);
+__dma_buf_list_del(dmabuf);
+ /* Must be observed by__get_file_rcu()beforefile_free()*/ +smp_store_mb(dmabuf->file, NULL);
return 0;
}
Christian König
maintainerQuoted 88 lines · Matt Evans
Hi Christian,
Quoted 35 lines · Christian König
Hi Matt,
Quoted 25 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 will only execute 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)Quoted 6 lines
Originally, drivers could assume
dmabuf->filewas valid until .release was called from fops->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
Oh well, really good catch. And that problem is wider than this.
Quoted 2 lines
Fixes 4ab59c3c638c6 ("dma-buf: Move dma_buf_release() from fops to dentry_ops")Signed-off-by Matt Evans <[email protected]>We seriously need a CC: stable here.
I knew I forgot something :(
Quoted 28 lines
Quoted 28 lines
---
Hi,
This issue was found (by Claude Opus 5.5) in the context of VFIO's DMABUF export path. VFIO iterates live DMABUFs with a get_file_active()/fput() block, which now becomes safe if the file is closed (and memory freed!) yet DMABUF .release hasn't yet occurred.
However, there are a couple of other places that directly use
dmabuf->fileand seem able to race a closing file (i.e. without holding the file reference)? If this is so, they'd be a UAF today; with this patch that goes away, but instead of a stale pointerdmabuf->filecould be NULL:1. drivers/gpu/drm/vmwgfx/ttm_object.c:get_dma_buf_unless_doomed()
file_ref_get(&dmabuf->file->f_ref); on a non-refcounted DMABUF
The commit message of
90ee6ed776c0("fs: port files tofile_ref") hints this might be more subtle than replacing it with aget_file()variant (so as to accept a NULL file *).2. drivers/gpu/drm/i915/gvt/dmabuf.c:intel_vgpu_get_dmabuf()
gvt_dbg_dpy(...
file_count(dmabuf->file)...);Quoted 8 lines
Quoted 2 lines
Respective vmwgfx & i915 maintainers, what is your view?
Or, indeed, if anyone sees any other questionable uses ofdmabuf->file.*sigh* we had multiple occasions suggesting to add a dma_buf_get_rcu()/active() because exporters wanted to keep their DMA-buf on linked lists or other container structures.
That was always rejected because the DMA-buf framework had never guaranteed that such things work correctly exactly because of those lifetime issues here. In other words nobody thought that through and document what should happen when etc...
So the use cases in vmgfx and i915 are just the tip of the iceberg :)
Yay :)
Quoted 3 lines
My suggestion is to get this patch reviewed and backported first and then come up with a
dma_buf_get_active()function including documentation and comments and test case to make sure that this feature works *and* keeps working.One minor comment below, apart from that the patch itself looks good to me.
Quoted 7 lines
This makes sense to me, thank you for the context also.
Just to make sure I thoroughly understand, do you envisage a
dma_buf_get_active()to take both a reference on a dmabuf and its correspondingdmabuf->fileso such users can move to a pattern of get- test-use-put on the file portion too? Sorry if it's a silly question, I'm not sure what to search for.
Yeah that won't really work. The dma_buf object uses the file to handles it's lifetime and doesn't have a separate refcount.
I'm also not even sure if adding a dma_buf_get_active() is a good idea or not.
The problem with dma_buf_get_active() is still that it only works in the exporter *if* there is a common lock hold while calling dma_buf_get_active() and the cleanup inside the ->release callback.
That is totally specific to each exporter and can't be used by importers at all, making stuff like that available to everybody is usually a recipe for trouble.
Maybe it is better to just properly document on the dma_buf->file member that exporters can use get_file_active(&dma_buf->file) but need to keep a bunch of things in mind.
See below for some more thoughts.
Quoted 26 lines
Quoted 18 lines
Quoted 8 lines
drivers/dma-buf/dma-buf.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)drivers/dma-buf/dma-buf.c +0
-0@@ -193,11 +193,15 @@ static void dma_buf_release(struct dentry *dentry)Quoted 7 lines
static intdma_buf_file_release(struct inode *inode, struct file *file){
+ structdma_buf*dmabuf =file->private_data;
+
if (!is_dma_buf_file(file))
return-EINVAL;I would do the casting only after the check.
Quoted 3 lines
On the other hand this function is a callback of the
file_operationsso checking iffile->opsis really pointing to the file operations is completely superfluous.So if you want to remove the check instead I'm perfectly fine with that as well.
I had given the unfamiliar code the benefit of the doubt, but this did look a bit suspect! Will do, thanks.
Quoted 17 lines
Matt
PS: I should've mentioned that Alex Mastro has kindly built a reproducer using a VFIO DMABUF export and dentry-held-open scenario. KASAN will detect the UAF:
https://github.com/opsound/vfio-dmabuf-lab/blob/main/tests/vfio_dmabuf_opath_uaf_test.c
Quoted 9 lines
Regards,
Christian.Quoted 5 lines
-__dma_buf_list_del(file->private_data);
+__dma_buf_list_del(dmabuf);
+ /* Must be observed by__get_file_rcu()beforefile_free()*/
This here needs more meat and really explain the "why we need it" part.
Regards,
Christian.
Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
+
smp_store_mb(dmabuf->file, NULL);
return 0;
}
Review
Quoted 3 lines · Matt Evans
static int
dma_buf_file_release(struct inode *inode, struct file *file){(quoted diff) +1
-0+ struct dma_buf *dmabuf = file->private_data;
I think dmabuf can be NULL here if the dmabuf allocation fails during dma_buf_export().
Review
Quoted 7 lines
(quoted diff) +4
-1+ if (!is_dma_buf_file(file)) return -EINVAL; - __dma_buf_list_del(file->private_data); + __dma_buf_list_del(dmabuf); + /* Must be observed by __get_file_rcu() before file_free() */ + smp_store_mb(dmabuf->file, NULL);
Resulting in NULL deref here -- guard with null check?
Quoted 2 lines
return 0;
}
via this path
drivers/dma-buf/dma-buf.c +3 -0
@@ -725,6 +725,7 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)if (!try_module_get (exp_info->owner))return ERR_PTR (-ENOENT ); +// succeeds file =dma_buf_getfile (exp_info->size, exp_info->flags);if (IS_ERR (file)) { ret =PTR_ERR (file);
@@ -739,6 +740,7 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info) dmabuf =kzalloc (alloc_size,GFP_KERNEL );if (!dmabuf) { ret = -ENOMEM ; +// go here goto err_file; }
@@ -771,6 +773,7 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)return dmabuf; err_file: +// ends up calling dma_buf_file_release() fput (file); err_module:module_put (exp_info->owner);
Hi Alex,
Quoted 8 lines · Alex Mastro
Review
Quoted 3 lines · Matt Evans
static int
dma_buf_file_release(struct inode *inode, struct file *file){(quoted diff) +1
-0+ struct dma_buf *dmabuf = file->private_data;I think dmabuf can be NULL here if the dmabuf allocation fails during
dma_buf_export().
Quoted 9 lines
Review
Quoted 7 lines
(quoted diff) +4
-1+ if (!is_dma_buf_file(file)) return -EINVAL; - __dma_buf_list_del(file->private_data); + __dma_buf_list_del(dmabuf); + /* Must be observed by __get_file_rcu() before file_free() */ + smp_store_mb(dmabuf->file, NULL);Resulting in NULL deref here -- guard with null check?
Oof, yes! Thanks for catching this. __dma_buf_list_del() protects itself against a NULL arg. Done.
Thanks,
Matt
Quoted 35 lines
Quoted 3 lines
return 0;
}via this path
drivers/dma-buf/dma-buf.c +3
-0@@ -725,6 +725,7 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)if (!try_module_get (exp_info->owner))return ERR_PTR (-ENOENT ); +// succeeds file =dma_buf_getfile (exp_info->size, exp_info->flags);if (IS_ERR (file)) { ret =PTR_ERR (file);@@ -739,6 +740,7 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info) dmabuf =kzalloc (alloc_size,GFP_KERNEL );if (!dmabuf) { ret = -ENOMEM ; +// go here goto err_file; }@@ -771,6 +773,7 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)return dmabuf; err_file: +// ends up calling dma_buf_file_release() fput (file); err_module:module_put (exp_info->owner);
Quoted 50 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 will only execute 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]>---
Hi,
This issue was found (by Claude Opus 5.5) in the context of VFIO's DMABUF export path. VFIO iterates live DMABUFs with a get_file_active()/fput() block, which now becomes safe if the file is closed (and memory freed!) yet DMABUF .release hasn't yet occurred.
However, there are a couple of other places that directly use
dmabuf->fileand seem able to race a closing file (i.e. without holding the file reference)? If this is so, they'd be a UAF today; with this patch that goes away, but instead of a stale pointerdmabuf->filecould be NULL:
Quoted 3 lines
1. drivers/gpu/drm/vmwgfx/ttm_object.c:get_dma_buf_unless_doomed()
file_ref_get(&dmabuf->file->f_ref); on a non-refcounted DMABUF
Yes, that's a real UAF today. vmwgfx relied on the exporter's dma_buf_ops.release running inside __fput() before file_free(), which stopped being true with 4ab59c3c638c and we missed it.
But this patch on its own makes vmwgfx worse. In the ordinary case, where closing the file drops the last dentry reference, a close racing PRIME_HANDLE_TO_FD is safe today: ttm_prime_dmabuf_release() runs from the same __fput() and blocks on prime->mutex, keeping the file allocated, so file_ref_get() just fails on the dead refcount. With this patch dmabuf->file is NULL in that window and we oops. So this shouldn't go to stable without a vmwgfx fix.
For vmwgfx we'd probably need this one line change in drivers/gpu/drm/vmwgfx/ttm_object.c to accompany this patch:
@@ -471,7 +475,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;
}
under prime->mutex, which keeps the struct dma_buf alive. It needs the NULL store from this patch to fix the existing UAF, since otherwise the recheck can accept a recycled file through the unchanged stale pointer.
Quoted 29 lines
The commit message of
90ee6ed776c0("fs: port files tofile_ref") hints this might be more subtle than replacing it with aget_file()variant (so as to accept a NULL file *).2. drivers/gpu/drm/i915/gvt/dmabuf.c:intel_vgpu_get_dmabuf()
gvt_dbg_dpy(... file_count(dmabuf->file) ...);Respective vmwgfx & i915 maintainers, what is your view?
Or, indeed, if anyone sees any other questionable uses ofdmabuf->file.Thanks,
Matt
drivers/dma-buf/dma-buf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)drivers/dma-buf/dma-buf.c +0
-0@@ -193,11 +193,15 @@ static void dma_buf_release(struct dentry *dentry)static int dma_buf_file_release (struct inode *inode,struct file *file) {
drivers/dma-buf/dma-buf.c static void dma_buf_release(struct dentry *dentry) · line 204
Quoted 10 lines
drivers/dma-buf/dma-buf.c +5
-1+struct dma_buf *dmabuf = file->private_data; +if (!is_dma_buf_file (file))return -EINVAL ; -__dma_buf_list_del (file->private_data); +__dma_buf_list_del (dmabuf); +/* Must be observed by __get_file_rcu() before file_free() */ +smp_store_mb (dmabuf->file,NULL );
I think Alex is right and dmabuf can be NULL here. An early if (!dmabuf) return 0" will fix that.
z
Hi Zack,
Quoted 78 lines · Zack Rusin
Quoted 50 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 will only execute 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]>---
Hi,
This issue was found (by Claude Opus 5.5) in the context of VFIO's DMABUF export path. VFIO iterates live DMABUFs with a get_file_active()/fput() block, which now becomes safe if the file is closed (and memory freed!) yet DMABUF .release hasn't yet occurred.
However, there are a couple of other places that directly use
dmabuf->fileand seem able to race a closing file (i.e. without holding the file reference)? If this is so, they'd be a UAF today; with this patch that goes away, but instead of a stale pointerdmabuf->filecould be NULL:Quoted 3 lines
1. drivers/gpu/drm/vmwgfx/ttm_object.c:get_dma_buf_unless_doomed()
file_ref_get(&dmabuf->file->f_ref); on a non-refcounted DMABUFYes, that's a real UAF today. vmwgfx relied on the exporter's dma_buf_ops.release running inside
__fput()beforefile_free(), which stopped being true with4ab59c3c638cand we missed it.But this patch on its own makes vmwgfx worse. In the ordinary case, where closing the file drops the last dentry reference, a close racing
PRIME_HANDLE_TO_FDis safe today:ttm_prime_dmabuf_release()runs from the same__fput()and blocks onprime->mutex, keeping the file allocated, sofile_ref_get()just fails on the dead refcount. With this patchdmabuf->fileis NULL in that window and we oops. So this shouldn't go to stable without a vmwgfx fix.For vmwgfx we'd probably need this one line change in
drivers/gpu/drm/vmwgfx/ttm_object.cto accompany this patch:(quoted diff) +1
-1@@ -471,7 +475,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 4 lines
under
prime->mutex, which keeps the structdma_bufalive. It needs the NULL store from this patch to fix the existing UAF, since otherwise the recheck can accept a recycled file through the unchanged stale pointer.
Thanks for confirming! Ah, so get_file_active() is good here, cheers. I'll repost as a 2-part series to first include this in vmwgfx and second add the anNULL.
Quoted 7 lines
Quoted 7 lines
The commit message of
90ee6ed776c0("fs: port files tofile_ref") hints this might be more subtle than replacing it with aget_file()variant (so as to accept a NULL file *).2. drivers/gpu/drm/i915/gvt/dmabuf.c:intel_vgpu_get_dmabuf()
gvt_dbg_dpy(... file_count(dmabuf->file) ...);
FWIW aside, I re-read this, and there does appear to be a ref held (dma_buf_export() has just happened, and dma_buf_fd() generated) so dmabuf->file is stable at that point.
Quoted 25 lines
Quoted 25 lines
Respective vmwgfx & i915 maintainers, what is your view?
Or, indeed, if anyone sees any other questionable uses ofdmabuf->file.Thanks,
Matt
drivers/dma-buf/dma-buf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)drivers/dma-buf/dma-buf.c +2
-0@@ -193,11 +193,15 @@ static void dma_buf_release(struct dentry *dentry)static int dma_buf_file_release (struct inode *inode,struct file *file) { +struct dma_buf *dmabuf = file->private_data; +if (!is_dma_buf_file (file))return -EINVAL ;
Quoted 8 lines
drivers/dma-buf/dma-buf.c static void dma_buf_release(struct dentry *dentry) · line 204
Quoted 5 lines
drivers/dma-buf/dma-buf.c +3
-1-__dma_buf_list_del (file->private_data); +__dma_buf_list_del (dmabuf); +/* Must be observed by __get_file_rcu() before file_free() */ +smp_store_mb (dmabuf->file,NULL );I think Alex is right and dmabuf can be NULL here. An early if (!dmabuf) return 0" will fix that.
Yep, on it.
Many thanks,
Matt
Hi i915 folks,
Quoted 92 lines · Matt Evans
Hi Zack,
Quoted 78 lines · Zack Rusin
Quoted 50 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 will only execute 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 from fops->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]>---
Hi,
This issue was found (by Claude Opus 5.5) in the context of VFIO's DMABUF export path. VFIO iterates live DMABUFs with a get_file_active()/fput() block, which now becomes safe if the file is closed (and memory freed!) yet DMABUF .release hasn't yet occurred.
However, there are a couple of other places that directly use
dmabuf->fileand seem able to race a closing file (i.e. without holding the file reference)? If this is so, they'd be a UAF today; with this patch that goes away, but instead of a stale pointerdmabuf->filecould be NULL:Quoted 3 lines
1. drivers/gpu/drm/vmwgfx/ttm_object.c:get_dma_buf_unless_doomed()
file_ref_get(&dmabuf->file->f_ref); on a non-refcounted DMABUFYes, that's a real UAF today. vmwgfx relied on the exporter's dma_buf_ops.release running inside
__fput()beforefile_free(), which stopped being true with4ab59c3c638cand we missed it.But this patch on its own makes vmwgfx worse. In the ordinary case, where closing the file drops the last dentry reference, a close racing
PRIME_HANDLE_TO_FDis safe today:ttm_prime_dmabuf_release()runs from the same__fput()and blocks onprime->mutex, keeping the file allocated, sofile_ref_get()just fails on the dead refcount. With this patchdmabuf->fileis NULL in that window and we oops. So this shouldn't go to stable without a vmwgfx fix.For vmwgfx we'd probably need this one line change in
drivers/gpu/drm/vmwgfx/ttm_object.cto accompany this patch:(quoted diff) +1
-1@@ -471,7 +475,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 4 lines
under
prime->mutex, which keeps the structdma_bufalive. It needs the NULL store from this patch to fix the existing UAF, since otherwise the recheck can accept a recycled file through the unchanged stale pointer.Thanks for confirming! Ah, so
get_file_active()is good here, cheers. I'll repost as a 2-part series to first include this in vmwgfx and second add the anNULL.Quoted 4 lines
Quoted 4 lines
The commit message of
90ee6ed776c0("fs: port files tofile_ref") hints this might be more subtle than replacing it with aget_file()variant (so as to accept a NULL file *).
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
2. drivers/gpu/drm/i915/gvt/dmabuf.c:intel_vgpu_get_dmabuf()
gvt_dbg_dpy(...
file_count(dmabuf->file)...);FWIW aside, I re-read this, and there does appear to be a ref held (
dma_buf_export()has just happened, anddma_buf_fd()generated) sodmabuf->fileis stable at that point.
I think I was mistaken here: the reference is moved to the file table by dma_buf_fd(), and seems another thread could immediately close it.
If so, there's an opportunity for userspace to (after this fix) cause a NULL deref here.
I'm about to repost the series but am happy to include a oneliner third patch to mitigate this, if you feel appropriate.
Is file_count() still interesting here, i.e. do you expect it to be >1 immediately after export? If still required, reading file_count(dmabuf->file) before the dma_buf_fd() would be safe (printing it later at the existing place).
Matt
Quoted 42 lines
Quoted 26 lines
Quoted 26 lines
Respective vmwgfx & i915 maintainers, what is your view?
Or, indeed, if anyone sees any other questionable uses ofdmabuf->file.Thanks,
Matt
drivers/dma-buf/dma-buf.c| 6 +++++-1 file changed, 5 insertions(+), 1 deletion(-)drivers/dma-buf/dma-buf.c +2
-0@@ -193,11 +193,15 @@ static void dma_buf_release(struct dentry *dentry) static int dma_buf_file_release(struct inode *inode, struct file *file) { + struct dma_buf *dmabuf = file->private_data; + if (!is_dma_buf_file(file)) return -EINVAL;Quoted 8 lines
drivers/dma-buf/dma-buf.c static void dma_buf_release(struct dentry *dentry) · line 204
Quoted 5 lines
drivers/dma-buf/dma-buf.c +3
-1- __dma_buf_list_del(file->private_data); + __dma_buf_list_del(dmabuf); + /* Must be observed by __get_file_rcu() before file_free() */ + smp_store_mb(dmabuf->file, NULL);I think Alex is right and dmabuf can be NULL here. An early if (!dmabuf) return 0" will fix that.
Yep, on it.
Many thanks,
Matt