Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] drm/imagination: UAF when pvr_context_create() fails during
  hard reset
--

commit 56c699205a21c02f74c49f4ededa3a23cd759daf
Author: Ryan Brue <[email protected]>

drm/imagination: Don't hold queues.lock across drm_sched_stop() on reset

This commit pins each queue's context and moves the queues onto
pvr_dev->queues.resetting under pvr_dev->queues.lock so
pvr_queue_device_pre_reset() and pvr_queue_device_post_reset() can stop and
restart the schedulers without holding queues.lock.

> diff --git a/drivers/gpu/drm/imagination/pvr_queue.c 
> b/drivers/gpu/drm/imagination/pvr_queue.c
> index 34b826381aa2b..56751c192ce68 100644
> --- a/drivers/gpu/drm/imagination/pvr_queue.c
> +++ b/drivers/gpu/drm/imagination/pvr_queue.c
> @@ -1242,6 +1242,23 @@ static void reg_state_init(void *cpu_ptr, void *priv)
>       }
>  }
>  
> +static void pvr_queue_reset_put_work(struct work_struct *w)
> +{
> +     struct pvr_queue *queue = container_of(w, struct pvr_queue, 
> reset_put_work);
> +
> +     pvr_context_put(queue->ctx);
> +}
> +
> +static void pvr_queue_reset_hold(struct pvr_queue *queue, struct list_head 
> *list)
> +{
> +     /* A put still pending from the last reset hands its reference over. */
> +     if (!cancel_work(&queue->reset_put_work) &&
> +         !pvr_context_get_if_referenced(queue->ctx))
> +             return;
> +
> +     list_add_tail(&queue->reset_node, list);
> +}
> +
[ ... ]
> @@ -1402,26 +1422,39 @@ struct pvr_queue *pvr_queue_create(struct pvr_context 
> *ctx,
>  
>  void pvr_queue_device_pre_reset(struct pvr_device *pvr_dev)
>  {
> +     struct list_head *resetting = &pvr_dev->queues.resetting;
>       struct pvr_queue *queue;
>  
> +     lockdep_assert_held_write(&pvr_dev->reset_sem);
> +
> +     /*
> +      * drm_sched_stop() waits for the run_job work, which takes
> +      * queues.lock, so it can't be called with queues.lock held. Pin the
> +      * queues and stop them without it. A queue whose context is already
> +      * being released has no jobs left, so it needs no stopping.
> +      */
>       mutex_lock(&pvr_dev->queues.lock);
>       list_for_each_entry(queue, &pvr_dev->queues.idle, node)
> -             pvr_queue_stop(queue, NULL);
> +             pvr_queue_reset_hold(queue, resetting);
>       list_for_each_entry(queue, &pvr_dev->queues.active, node)
> -             pvr_queue_stop(queue, NULL);
> +             pvr_queue_reset_hold(queue, resetting);
>       mutex_unlock(&pvr_dev->queues.lock);
> +
> +     list_for_each_entry(queue, resetting, reset_node)
> +             pvr_queue_stop(queue, NULL);
>  }

[Severity: High]
Can this cause a use-after-free on queue and ctx if pvr_context_create()
fails after pvr_queue_create() has added a queue to pvr_dev->queues.idle?

pvr_context_create() initializes ctx->ref_count to 1 and calls
pvr_context_create_queues(), where pvr_queue_create() publishes queue->node
onto pvr_dev->queues.idle under pvr_dev->queues.lock.  If a hard reset runs
before pvr_context_create() finishes and a later step such as init_fw_objs()
fails, pvr_context_create() unwinds by calling pvr_context_destroy_queues()
and kfree(ctx) directly instead of dropping ctx->ref_count:

drivers/gpu/drm/imagination/pvr_context.c:pvr_context_create() {
        ...
        err = pvr_context_create_queues(ctx, args, ctx->data);
        if (err)
                goto err_free_ctx_id;

        err = init_fw_objs(ctx, args, ctx->data);
        if (err)
                goto err_destroy_queues;
        ...
err_destroy_queues:
        pvr_context_destroy_queues(ctx, true);
        ...
err_free_ctx:
        kfree(ctx);
        return err;
}

Because ctx->fw_obj is still NULL when init_fw_objs() fails,
pvr_queue_cleanup_fw_context() returns without taking pvr_dev->reset_sem,
and pvr_queue_destroy() only removes queue->node under pvr_dev->queues.lock
before freeing queue:

CPU0                                  CPU1
pvr_context_create()
  kref_init(&ctx->ref_count)
  pvr_context_create_queues()
    pvr_queue_create()
      list_add_tail(&queue->node,
                    &queues.idle)
                                      pvr_queue_device_pre_reset()
                                        mutex_lock(&queues.lock)
                                        pvr_queue_reset_hold()
                                          pvr_context_get_if_referenced()
                                          list_add_tail(&queue->reset_node,
                                                        resetting)
                                        mutex_unlock(&queues.lock)
  init_fw_objs() -> fails
  pvr_context_destroy_queues()
    pvr_queue_destroy()
      list_del_init(&queue->node)
      kfree(queue)
  kfree(ctx)
                                        pvr_queue_stop(queue, NULL)
                                          <-- use-after-free

pvr_queue_device_post_reset() and pvr_queue_reset_put_work() will then also
access the freed queue and ctx.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/20261125-rbrue-suez-pvr-pre-reset-lock-v1-1-0207245c5...@gmail.com?part=1

Reply via email to