Skip to contentSkip to conversation

accel/amdxdna: Use caller client for debug BO sync

PATCH Applied Shuvam Pandey 3 participants 4 messages Jun 15 – Jun 16

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 accel/amdxdna: Use caller client for debug BO sync

amdxdna_drm_sync_bo_ioctl() looks up args->handle in the ioctl caller's drm_file. For SYNC_DIRECT_FROM_DEVICE, it then calls amdxdna_hwctx_sync_debug_bo(), but passes abo->client.

amdxdna_hwctx_sync_debug_bo() uses the passed client both as the handle namespace for debug_bo_hdl and as the owner of the hardware context xarray. Those must match the file that supplied args->handle. The BO's stored client pointer is object state, not the ioctl context.

Pass filp->driver_priv instead, matching the original handle lookup.

Fixes 7ea046838021 ("accel/amdxdna: Support firmware debug buffer")
Cc [email protected] # v6.19+
---
 drivers/accel/amdxdna/amdxdna_gem.c | 3 ++-
drivers/accel/amdxdna/amdxdna_gem.c +2 -1
index 6e367ddb9e1becb8d03cb4badec25deed38a851d..6c16b21994abc8f0ce58ee6dede219a84ade6825 100644
@@ -1027,6 +1027,7 @@ int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm
 int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev,
 			      void *data, struct drm_file *filp)
 {
+	struct amdxdna_client *client = filp->driver_priv;
 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
 	struct amdxdna_drm_sync_bo *args = data;
 	struct amdxdna_gem_obj *abo;
@@ -1061,7 +1062,7 @@ int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev,
 		 args->handle, args->offset, args->size);
 
 	if (args->direction == SYNC_DIRECT_FROM_DEVICE)
-		ret = amdxdna_hwctx_sync_debug_bo(abo->client, args->handle);
+		ret = amdxdna_hwctx_sync_debug_bo(client, args->handle);
 
 put_obj:
 	drm_gem_object_put(gobj);
Signature · 1 line
2.50.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but there is a missing bounds check on user-supplied offset and size in amdxdna_drm_sync_bo_ioctl, allowing arbitrary kernel memory access via cache flushing.
- [High] This problem wasn't introduced by this patch, but there is a Use-After-Free vulnerability in amdxdna_gem_del_bo_usage when a shared BO outlives its creator.
- [High] This problem wasn't introduced by this patch, but calling amdxdna_hwctx_sync_debug_bo or amdxdna_drm_config_hwctx_ioctl triggers an AA deadlock during runtime PM resume. --

commit a01d4fd71144f0f677a3563f73b31b1641290aa0
Author: Shuvam Pandey <[email protected]>

accel/amdxdna: Use caller client for debug BO sync

The commit updates amdxdna_drm_sync_bo_ioctl() to use the caller's client context rather than the BO's stored client when synchronizing the debug BO. This ensures the client matching the provided handle is correctly used as the namespace owner.

drivers/accel/amdxdna/amdxdna_gem.c int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm · line 1033

Quoted 12 lines
drivers/accel/amdxdna/amdxdna_gem.c +1 -0
index 6e367ddb9e1be..6c16b21994abc 100644
@@ -1027,6 +1027,7 @@ int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm
 int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev,
 			      void *data, struct drm_file *filp)
 {
+	struct amdxdna_client *client = filp->driver_priv;
 	struct amdxdna_dev *xdna = to_xdna_dev(dev);
 	struct amdxdna_drm_sync_bo *args = data;
 	struct amdxdna_gem_obj *abo;

[Severity: High]
This isn't a bug introduced by this patch, but is it possible for an unprivileged user to trigger an out-of-bounds memory access here?

Looking at amdxdna_drm_sync_bo_ioctl(), the user-controlled args->offset and args->size are used without validation against gobj->size:

else if (amdxdna_gem_vmap(abo))
drm_clflush_virt_range(amdxdna_gem_vmap(abo) + args->offset, args->size);

Could this allow arbitrary kernel memory access via cache flushing?

[ ... ]

drivers/accel/amdxdna/amdxdna_gem.c int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev, · line 1064

Quoted 5 lines
drivers/accel/amdxdna/amdxdna_gem.c +0 -1
@@ -1061,7 +1062,7 @@ int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev,
 		 args->handle, args->offset, args->size);
 
 	if (args->direction == SYNC_DIRECT_FROM_DEVICE)
-		ret = amdxdna_hwctx_sync_debug_bo(abo->client, args->handle);

[Severity: High]
This is a pre-existing issue, but does relying on abo->client here and in other parts of the driver risk a use-after-free if a shared BO outlives its creator?

When a BO is exported and imported by another client, it can outlive the original creator. If the creator closes its DRM file, abo->client is not cleared because open_ref remains greater than 0.

When the importer finally closes the BO, amdxdna_gem_del_bo_usage() is called and dereferences the now-freed client pointer:

amdxdna_gem_del_bo_usage()

    guard(mutex)(&client->mm_lock);

Could this cause memory corruption when the dangling client pointer is dereferenced?

drivers/accel/amdxdna/amdxdna_gem.c int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev, · line 1065

Quoted 1 line
drivers/accel/amdxdna/amdxdna_gem.c +1 -0
+		ret = amdxdna_hwctx_sync_debug_bo(client, args->handle);

[Severity: High]
This isn't a bug introduced by this patch, but can calling amdxdna_hwctx_sync_debug_bo() trigger an AA deadlock during runtime PM resume?

In amdxdna_hwctx_sync_debug_bo(), the xdna->dev_lock is acquired:

    guard(mutex)(&xdna->dev_lock);
    hwctx = xa_load(&client->hwctx_xa, abo->assigned_hwctx);
    ...
    ret = xdna->dev_info->ops->hwctx_sync_debug_bo(hwctx, debug_bo_hdl);

If this hardware callback unconditionally calls amdxdna_cmd_submit(), which then calls amdxdna_pm_resume_get(), the PM framework may synchronously execute the resume callback amdxdna_pm_resume() if the device is suspended.

The resume callback then attempts to acquire the same lock:

amdxdna_pm_resume()

    guard(mutex)(&xdna->dev_lock);

Would this result in an AA deadlock?

Quoted 3 lines
drivers/accel/amdxdna/amdxdna_gem.c +0 -0
 put_obj:
 	drm_gem_object_put(gobj);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Shuvam Pandey
Quoted 30 lines · Shuvam Pandey

amdxdna_drm_sync_bo_ioctl() looks up args->handle in the ioctl caller's drm_file. For SYNC_DIRECT_FROM_DEVICE, it then calls amdxdna_hwctx_sync_debug_bo(), but passes abo->client.

amdxdna_hwctx_sync_debug_bo() uses the passed client both as the handle namespace for debug_bo_hdl and as the owner of the hardware context xarray. Those must match the file that supplied args->handle. The BO's stored client pointer is object state, not the ioctl context.

Pass filp->driver_priv instead, matching the original handle lookup.

Fixes 7ea046838021 ("accel/amdxdna: Support firmware debug buffer")
Cc [email protected] # v6.19+
---
  drivers/accel/amdxdna/amdxdna_gem.c | 3 ++-
drivers/accel/amdxdna/amdxdna_gem.c +1 -0
index 6e367ddb9e1becb8d03cb4badec25deed38a851d..6c16b21994abc8f0ce58ee6dede219a84ade6825 100644
@@ -1027,6 +1027,7 @@ int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm
  int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev,
  			      void *data, struct drm_file *filp)
  {
+	struct amdxdna_client *client = filp->driver_priv;
  	struct amdxdna_dev *xdna = to_xdna_dev(dev);
  	struct amdxdna_drm_sync_bo *args = data;
  	struct amdxdna_gem_obj *abo;

drivers/accel/amdxdna/amdxdna_gem.c int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev, · line 1065

Quoted 5 lines
drivers/accel/amdxdna/amdxdna_gem.c +1 -1
@@ -1061,7 +1062,7 @@ int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev,
  		 args->handle, args->offset, args->size);
 
  	if (args->direction == SYNC_DIRECT_FROM_DEVICE)
-		ret = amdxdna_hwctx_sync_debug_bo(abo->client, args->handle);
+		ret = amdxdna_hwctx_sync_debug_bo(client, args->handle);
Quoted 3 lines
drivers/accel/amdxdna/amdxdna_gem.c +0 -0
  put_obj:
  	drm_gem_object_put(gobj);
maintainer Applied
#3 lore raw
In reply to Shuvam Pandey

Applied to drm-misc-fixes.

Quoted 38 lines · Shuvam Pandey

amdxdna_drm_sync_bo_ioctl() looks up args->handle in the ioctl caller's drm_file. For SYNC_DIRECT_FROM_DEVICE, it then calls amdxdna_hwctx_sync_debug_bo(), but passes abo->client.

amdxdna_hwctx_sync_debug_bo() uses the passed client both as the handle namespace for debug_bo_hdl and as the owner of the hardware context xarray. Those must match the file that supplied args->handle. The BO's stored client pointer is object state, not the ioctl context.

Pass filp->driver_priv instead, matching the original handle lookup.

Fixes 7ea046838021 ("accel/amdxdna: Support firmware debug buffer")
Cc [email protected] # v6.19+
---
  drivers/accel/amdxdna/amdxdna_gem.c | 3 ++-
drivers/accel/amdxdna/amdxdna_gem.c +2 -1
index 6e367ddb9e1becb8d03cb4badec25deed38a851d..6c16b21994abc8f0ce58ee6dede219a84ade6825 100644
@@ -1027,6 +1027,7 @@ int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm
  int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev,
  			      void *data, struct drm_file *filp)
  {
+	struct amdxdna_client *client = filp->driver_priv;
  	struct amdxdna_dev *xdna = to_xdna_dev(dev);
  	struct amdxdna_drm_sync_bo *args = data;
  	struct amdxdna_gem_obj *abo;
@@ -1061,7 +1062,7 @@ int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev,
  		 args->handle, args->offset, args->size);
 
  	if (args->direction == SYNC_DIRECT_FROM_DEVICE)
-		ret = amdxdna_hwctx_sync_debug_bo(abo->client, args->handle);
+		ret = amdxdna_hwctx_sync_debug_bo(client, args->handle);
 
  put_obj:
  	drm_gem_object_put(gobj);