Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] drm/amdgpu: UAF on bo_va in amdgpu_vm_update_ctx_fini() after
  amdgpu_vm_bo_del()
- [High] drm/amdgpu: skipped delete in
  amdgpu_vm_update_ctx_add_freed_mapping()
- [Low] drm/amdgpu: update_link placed under wrong lock comment in
  amdgpu_bo_va
--

commit 3f1753904c640653510160bf642992a7a55fbc3d
Author: Natalie Vock <[email protected]>

drm/amdgpu: Track dirty bo_va structures in update ctx

This commit adds an update_link list head to struct amdgpu_bo_va and tracks
modified bo_va structures in a dirty_bo_vas list on struct amdgpu_vm_update_ctx.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h 
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
> index e0090dc10682f..068c57ed537fd 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
> @@ -80,6 +80,8 @@ struct amdgpu_bo_va {
>       /* protected by bo being reserved */
>       unsigned                        ref_count;
>  
> +     struct list_head                update_link;
> +
>       /* all other members protected by the VM PD being reserved */
>       struct dma_fence                *last_pt_update;
>  

[Severity: Low]
Should update_link be placed below the "all other members protected by the VM
PD being reserved" comment?  It is modified by the update_ctx helpers while
only the VM PD reservation lock is held, including when the individual BO is
NULL (such as fpriv->prt_va) or not reserved (when clearing overlapping
mappings of other BOs in the VM).

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c 
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> index 1e2d45979efdb..330c288053352 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c

[ ... ]

> @@ -1733,8 +1735,10 @@ static void
>  amdgpu_vm_update_ctx_add_freed_mapping(struct amdgpu_vm_update_ctx *ctx,
>                                      struct amdgpu_bo_va_mapping *mapping)
>  {
> -     WARN_ON(!list_empty(&mapping->pending_list));
> -     list_add(&mapping->pending_list, &ctx->deleted);
> +     if (list_empty(&mapping->bo_va->update_link))
> +             list_add(&mapping->bo_va->update_link, &ctx->dirty_bo_vas);
> +     if (list_empty(&mapping->pending_list))
> +             list_add(&mapping->pending_list, &ctx->deleted);
>  }

[Severity: High]
Can skipping list_add(&mapping->pending_list, &ctx->deleted) when
mapping->pending_list is non-empty leave freed mappings in vm->va?

Within one update_ctx, amdgpu_vm_bo_clear_mappings() and
amdgpu_vm_bo_replace_map() put overlapping mappings onto ctx->cleared via
mapping->pending_list; if amdgpu_vm_bo_unmap() is then called on that mapping
in the same context, amdgpu_vm_update_ctx_add_freed_mapping() leaves it on
ctx->cleared instead of moving it to ctx->deleted, so
amdgpu_vm_update_ctx_execute_replace() re-inserts any non-cleared remainder.

Furthermore, if an update_ctx is aborted before
amdgpu_vm_update_ctx_execute_replace() finishes processing ctx->cleared (for
example, if amdgpu_vm_bo_replace_map() fails in
amdgpu_vm_update_ctx_add_new_mapping()), neither amdgpu_vm_update_ctx_fini()
here nor amdgpu_vm_update_ctx_rollback() in commit "drm/amdgpu: Add function to
roll back a VM update ctx" unlinks entries from ctx->cleared.  When the BO is
later removed in amdgpu_vm_bo_del(), amdgpu_vm_update_ctx_add_freed_mapping()
silently skips adding the mapping to update_ctx.deleted and kfree(bo_va) frees
bo_va while the mapping remains in vm->va with a dangling mapping->bo_va
pointer, causing a use-after-free on subsequent vm->va traversals.

>  int amdgpu_vm_update_ctx_execute_replace(struct amdgpu_vm_update_ctx *ctx)

[ ... ]

> @@ -1878,10 +1883,16 @@ void amdgpu_vm_update_ctx_init(struct 
> amdgpu_vm_update_ctx *ctx,
>  void amdgpu_vm_update_ctx_fini(struct amdgpu_vm_update_ctx *ctx)
>  {
>       struct interval_tree_node *node, *next_node;
> +     struct amdgpu_bo_va *bo_va, *next_bo_va;
>  
>       list_splice(&ctx->freed, &ctx->vm->delayed_freed);
>       amdgpu_sync_free(&ctx->sync);
>  
> +     list_for_each_entry_safe(bo_va, next_bo_va, &ctx->dirty_bo_vas,
> +                              update_link) {
> +             list_del_init(&bo_va->update_link);
> +     }

[Severity: High]
Does this cause a use-after-free on bo_va when a caller unmaps and deletes a
bo_va before finalizing update_ctx?

In amdgpu_unmap_static_csa(), amdgpu_vm_bo_unmap() adds bo_va->update_link to
update_ctx.dirty_bo_vas, and amdgpu_vm_bo_del() then frees bo_va before
amdgpu_vm_update_ctx_fini() runs:

drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c:amdgpu_unmap_static_csa() {
    ...
        r = amdgpu_vm_bo_unmap(&update_ctx, bo_va, csa_addr);
        if (r) {
                drm_err(adev_to_drm(adev),
                        "failed to do bo_unmap on static CSA, err=%d\n", r);
                goto error_ctx;
        }
        amdgpu_vm_update_ctx_commit(&update_ctx);

        amdgpu_vm_bo_del(adev, bo_va);

error_ctx:
        amdgpu_vm_update_ctx_fini(&update_ctx);
    ...
}

Because amdgpu_vm_bo_del() calls kfree(bo_va) without unlinking
bo_va->update_link from the outer update_ctx.dirty_bo_vas,
amdgpu_vm_update_ctx_fini() executes list_del_init(&bo_va->update_link) on the
freed bo_va.  Unlinking bo_va->update_link in amdgpu_vm_bo_del() before
kfree(bo_va) would avoid this.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=5

Reply via email to