Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] drm/amdgpu:bo_va UAF in amdgpu_vm_bo_del() when
  group_leader->mm is NULL
--

commit cfb11e728a1b3b102cad849d9bad6a5262a6b72c
Author: Jesse Zhang <[email protected]>

drm/amdgpu: unlink userptr bo_vas under the notifier_lock

This commit takes adev->notifier_lock in amdgpu_vm_bo_del() when unlinking
a userptr bo_va from bo->vm_bo to serialize against concurrent MMU
notifier invalidations.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c 
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> index 94b1cfe05e3e0..7a6de95e902e4 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> @@ -2252,10 +2252,18 @@ void amdgpu_vm_bo_del(struct amdgpu_device *adev,
>       dma_resv_assert_held(vm->root.bo->tbo.base.resv);
>  
>       if (bo) {
> +             /*
> +              * The MMU notifier of a userptr walks bo->vm_bo under the
> +              * notifier_lock, it can't take the BO reservation.
> +              */
> +             bool userptr = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
> +
>               dma_resv_assert_held(bo->tbo.base.resv);
>               if (amdgpu_vm_is_bo_always_valid(vm, bo))
>                       ttm_bo_set_bulk_move(&bo->tbo, NULL);
>  
> +             if (userptr)
> +                     mutex_lock(&adev->notifier_lock);
>               for (base = &bo_va->base.bo->vm_bo; *base;
>                    base = &(*base)->next) {
>                       if (*base != &bo_va->base)
> @@ -2265,6 +2273,8 @@ void amdgpu_vm_bo_del(struct amdgpu_device *adev,
>                       *base = bo_va->base.next;
>                       break;
>               }
> +             if (userptr)
> +                     mutex_unlock(&adev->notifier_lock);
>       }
>  
>       spin_lock(&vm->individual_lock);

[Severity: High]
Can amdgpu_vm_bo_del() still skip adev->notifier_lock and free bo_va while
amdgpu_hmm_invalidate_gfx() walks bo->vm_bo if the thread group leader has
already exited?

amdgpu_ttm_tt_set_userptr() binds gtt->usertask to current->group_leader,
and amdgpu_ttm_tt_get_usermm() returns gtt->usertask->mm:

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c:amdgpu_ttm_tt_get_usermm() {
    ...
        if (gtt->usertask == NULL)
                return NULL;

        return gtt->usertask->mm;
}

Meanwhile, amdgpu_hmm_register() inserts bo->notifier into current->mm,
where it stays active until amdgpu_gem_object_free() calls
amdgpu_hmm_unregister().  In a multi-threaded process, if the thread group
leader exits via pthread_exit() while other threads remain alive, exit_mm()
clears gtt->usertask->mm to NULL while the shared mm_struct and
bo->notifier are still live:

CPU0 (group leader)           CPU1                          CPU2
do_exit()
  exit_mm()
    tsk->mm = NULL
                              drm_gem_close_ioctl()
                                ...
                                amdgpu_vm_bo_del()
                                  /* get_usermm() == NULL */
                                                            
madvise(MADV_DONTNEED)
                                                              ...
                                                              
amdgpu_hmm_invalidate_gfx()
                                                                
mutex_lock(&adev->notifier_lock)
                                                                
amdgpu_vm_bo_invalidate()
                                  *base = bo_va->base.next
                                  kfree(bo_va)
                                                                  /* walks 
bo->vm_bo */
                                                                  <-- 
use-after-free

The same happens during process exit when do_exit() runs exit_mm() before
exit_files() closes the DRM file descriptor while another task sharing the
mm or file triggers an invalidation.

Checking amdgpu_ttm_tt_get_usertask() (or bo->notifier.mm) instead of
amdgpu_ttm_tt_get_usermm() would keep adev->notifier_lock serialized for as
long as the userptr BO exists.

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

Reply via email to