drm/vmwgfx: close TOCTOU on guest_memory_bo in surface reference ioctl
Subsystems (4)
PATCH 1/1 drm/vmwgfx: close TOCTOU on guest_memory_bo in surface reference ioctl
vmw_gb_surface_reference_internal() checks srf->res.guest_memory_bo for NULL and reads its buffer_map_handle and buffer_size outside the cmdbuf_mutex scope that protects the backup pointer.
A concurrent surface eviction can NULL out guest_memory_bo between the check and the use, causing a NULL pointer dereference in drm_gem_handle_create(). The post-mutex reads of vma_node and base.size race with any concurrent backup change.
Move the NULL check inside the mutex, and read buffer_map_handle and buffer_size while still holding it.
--- drivers/gpu/drm/vmwgfx/vmwgfx_surface.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +10 -7
@@ -1718,15 +1718,21 @@ vmw_gb_surface_reference_internal(struct drm_device *dev, user_srf = container_of(base, struct vmw_user_surface, prime.base); srf = &user_srf->srf; + metadata = &srf->metadata; + +mutex_lock (&dev_priv->cmdbuf_mutex);/* Protect res->guest_memory_bo */ if (!srf->res.guest_memory_bo) { +mutex_unlock (&dev_priv->cmdbuf_mutex);DRM_ERROR ("Shared GB surface is missing a backup buffer.\n" ); goto out_bad_resource; } - metadata = &srf->metadata; - -mutex_lock (&dev_priv->cmdbuf_mutex);/* Protect res->backup */ ret =drm_gem_handle_create (file_priv, &srf->res.guest_memory_bo->tbo.base, &backup_handle); +if (!ret) { + rep->crep.buffer_map_handle = +drm_vma_node_offset_addr (&srf->res.guest_memory_bo->tbo.base.vma_node); + rep->crep.buffer_size = srf->res.guest_memory_bo->tbo.base.size; + }mutex_unlock (&dev_priv->cmdbuf_mutex);if (ret !=0 ) { drm_err(dev, "Wasn't able to create a backing handle for surface sid = %u.\n",
@@ -1746,10 +1752,7 @@ vmw_gb_surface_reference_internal(struct drm_device *dev, rep->crep.handle = user_srf->prime.base.handle; rep->crep.backup_size = srf->res.guest_memory_size; rep->crep.buffer_handle = backup_handle; - rep->crep.buffer_map_handle = -drm_vma_node_offset_addr (&srf->res.guest_memory_bo->tbo.base.vma_node); - rep->crep.buffer_size = srf->res.guest_memory_bo->tbo.base.size; - +/* buffer_map_handle and buffer_size set inside the mutex above */ rep->creq.version = drm_vmw_gb_surface_v1; rep->creq.svga3d_flags_upper_32_bits =SVGA3D_FLAGS_UPPER_32 (metadata->flags);
Signature · 1 line
2.43.0
Gentle ping — any feedback on this patch?
thanks,
Aldo
Quoted 75 lines · Aldo Ariel Panzardo
vmw_gb_surface_reference_internal()checkssrf->res.guest_memory_bofor NULL and reads itsbuffer_map_handleandbuffer_sizeoutside thecmdbuf_mutexscope that protects the backup pointer.A concurrent surface eviction can NULL out
guest_memory_bobetween the check and the use, causing a NULL pointer dereference indrm_gem_handle_create(). The post-mutex reads ofvma_nodeand base.size race with any concurrent backup change.Move the NULL check inside the mutex, and read
buffer_map_handleandbuffer_sizewhile still holding it.Fixes 14b1c33e8429 ("drm/vmwgfx: Add new ioctl for GB surface create andreference")
Reported-by Sashiko <[email protected]>Signed-off-by Aldo Ariel Panzardo <[email protected]>--- drivers/gpu/drm/vmwgfx/vmwgfx_surface.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +3
-0@@ -1718,15 +1718,21 @@ vmw_gb_surface_reference_internal(structdrm_device *dev, user_srf = container_of(base, struct vmw_user_surface, prime.base); srf = &user_srf->srf; + metadata = &srf->metadata; + +mutex_lock (&dev_priv->cmdbuf_mutex);/* Protect
res->guest_memory_bo*/if (!srf->res.guest_memory_bo) { + mutex_unlock(&dev_priv->cmdbuf_mutex); DRM_ERROR("Shared GB surface is missing a backup buffer.\n"); goto out_bad_resource; }drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +0
-3- metadata = &srf->metadata; - -mutex_lock (&dev_priv->cmdbuf_mutex);/* Protect res->backup */ ret = drm_gem_handle_create(file_priv,
&srf->res.guest_memory_bo->tbo.base,&backup_handle);drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +3
-0+if (!ret) { + rep->crep.buffer_map_handle = + drm_vma_node_offset_addr(&
srf->res.guest_memory_bo->tbo.base.vma_node);
+rep->crep.buffer_size=srf->res.guest_memory_bo->tbo.
base.size;
+ }mutex_unlock(&dev_priv->cmdbuf_mutex); if (ret != 0) { drm_err(dev, "Wasn't able to create a backing handle forsurface sid = %u.\n",
drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +1
-4@@ -1746,10 +1752,7 @@ vmw_gb_surface_reference_internal(struct drm_device *dev, rep->crep.handle = user_srf->prime.base.handle; rep->crep.backup_size = srf->res.guest_memory_size; rep->crep.buffer_handle = backup_handle; - rep->crep.buffer_map_handle = -drm_vma_node_offset_addr (&srf->res.guest_memory_bo->tbo. base.vma_node); - rep->crep.buffer_size = srf->res.guest_memory_bo->tbo.base.size; - +/* buffer_map_handle and buffer_size set inside the mutex above */ rep->creq.version = drm_vmw_gb_surface_v1;rep->creq.svga3d_flags_upper_32_bits = SVGA3D_FLAGS_UPPER_32(metadata->flags);--
2.43.0
Zack Rusin
maintainerHi,
thanks for the patch. Just a quick note: I'm not a huge fan of patches like this because they attempt to fix a potential issue, usually at the cost of introducing real regressions, which is not a great tradeoff. Especially nowadays when writing code has become much cheaper, patches that don't mention the type of testing/validation that has been done are going to need to wait a long time because they trade the part that's cheap to reproduce "llm please fix this sashiko bug" for the expensive "run the full test/validation tests". However, lacking proper fail/skip lists for igt in the kernel for vmwgfx, explicit testing instructions, or a public CI machine is our fault and we need to fix that.
Quoted 9 lines · Aldo Ariel Panzardo
vmw_gb_surface_reference_internal()checkssrf->res.guest_memory_bofor NULL and reads itsbuffer_map_handleandbuffer_sizeoutside thecmdbuf_mutexscope that protects the backup pointer.A concurrent surface eviction can NULL out
guest_memory_bobetween the check and the use, causing a NULL pointer dereference indrm_gem_handle_create(). The post-mutex reads ofvma_nodeand base.size race with any concurrent backup change.
Thanks, the locking change itself looks right, but the description needs adjusting. A successful eviction keeps the backup. A concrete racing writer is a backup switch, e.g. from SVGA_3D_CMD_BIND_GB_SURFACE: vmw_resource_unreserve() drops its reference to the old buffer and installs the new one under cmdbuf_mutex. So the unlocked check can see a transient NULL, and the reads after the unlock can see NULL or return another buffer's offset and size. The new handle keeps the original buffer alive, but this ioctl holds no reference to that other buffer. A further switch can drop its last reference and free it while we read it. Please describe that instead.
Quoted 4 lines
Move the NULL check inside the mutex, and read
buffer_map_handleandbuffer_sizewhile still holding it.Fixes 14b1c33e8429 ("drm/vmwgfx: Add new ioctl for GB surface create and reference")
afaict the unlocked check and the reads after the unlock go back to the original ioctl:
Quoted 13 lines
Reported-by Sashiko <[email protected]>Signed-off-by Aldo Ariel Panzardo <[email protected]>--- drivers/gpu/drm/vmwgfx/vmwgfx_surface.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +0
-0@@ -1718,15 +1718,21 @@ vmw_gb_surface_reference_internal(struct drm_device *dev,
drivers/gpu/drm/vmwgfx/vmwgfx_surface.c vmw_gb_surface_reference_internal(struct drm_device *dev, · line 1728
Quoted 10 lines
drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +4
-0user_srf = container_of(base, struct vmw_user_surface, prime.base); srf = &user_srf->srf; + metadata = &srf->metadata; + +mutex_lock (&dev_priv->cmdbuf_mutex);/* Protect res->guest_memory_bo */ if (!srf->res.guest_memory_bo) { +mutex_unlock (&dev_priv->cmdbuf_mutex);DRM_ERROR ("Shared GB surface is missing a backup buffer.\n" ); goto out_bad_resource; }
This touches the same lines as your "return an error when a shared GB surface has no backup buffer" v2, and this branch still returns 0. I'd treat them as a series and just rebase this one on top of that patch, keeping both its ret = -EINVAL here and its surface-reference cleanup on failure.
z