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

Pre-existing issues:
- [High] Possible Use-After-Free of xe_device when a foreign dma-fence signals 
after device teardown.
--

commit 3bd2b743d1b606804ebc5081e49badce29c41a21
Author: Srinivasan Shanmugam <[email protected]>

drm/xe: Convert xe_user_fence to drm_user_fence

Replace the open-coded user fence implementation in xe_sync.c with the
new common drm_user_fence helper.

> diff --git a/drivers/gpu/drm/xe/xe_sync.c b/drivers/gpu/drm/xe/xe_sync.c
> index 37866768d64c2..2d1e07792506e 100644
> --- a/drivers/gpu/drm/xe/xe_sync.c
> +++ b/drivers/gpu/drm/xe/xe_sync.c
> @@ -19,36 +18,60 @@

[ ... ]

> +static void xe_ufence_worker(struct drm_user_fence *base, bool mm_ok)
> +{
> +     struct xe_user_fence *ufence = container_of(base, struct xe_user_fence, 
> base);

[ ... ]

> +     if (mm_ok) {
> +             if (copy_to_user(ufence->addr, &ufence->value, 
> sizeof(ufence->value)))
> +                     drm_dbg(&ufence->xe->drm,
> +                             "copy_to_user failed, user fence wasn't 
> signaled\n");
> +     } else {
> +             drm_dbg(&ufence->xe->drm,
> +                     "mmget_not_zero() failed, ufence wasn't signaled\n");
> +     }
> +
> +     wake_up_all(&ufence->xe->ufence_wq);
> +}

[ ... ]

>  static struct xe_user_fence *user_fence_create(struct xe_device *xe, u64 
> addr,
>                                              u64 value)
>  {
> @@ -63,51 +86,22 @@ static struct xe_user_fence *user_fence_create(struct 
> xe_device *xe, u64 addr,
>       if (!ufence)
>               return ERR_PTR(-ENOMEM);
>  
> -     ufence->xe = xe;
> -     kref_init(&ufence->refcount);
> -     ufence->addr = ptr;
> +     ufence->xe    = xe;
> +     ufence->addr  = ptr;
>       ufence->value = value;
> -     ufence->mm = current->mm;
> -     mmgrab(ufence->mm);
> +     drm_user_fence_init(&ufence->base, xe->ordered_wq, &xe_ufence_ops);

[Severity: High]
This is a pre-existing issue, but does this code leave the xe_device vulnerable
to a use-after-free when an external dma-fence signals after device teardown?

When a user creates a user fence that depends on an external dma-fence (e.g.,
from amdgpu) by passing a syncobj, xe_sync_entry_parse() creates the user fence
without taking a reference to the xe_device.

If the user then closes the xe file descriptor or the device is hot-unplugged,
the last reference to xe_device can be dropped. xe_sync_entry_cleanup()
drops the sync entry's reference to the user fence but does not cancel
the pending callback.

When the external dma-fence eventually signals, the callback invokes
queue_work(). Because the workqueue points to xe->ordered_wq which was already
destroyed during device teardown, this causes a UAF crash. If the worker
does run, it will also access the freed xe_device pointer in xe_ufence_worker()
to wake up waiters.

Could user_fence_create() take a reference to the xe_device, or could
xe_sync_entry_cleanup() be updated to explicitly cancel the pending callback?

>  
>       return ufence;
>  }

-- 
Sashiko AI review · 
https://sashiko.dev/#/patchset/[email protected]?part=3

Reply via email to