drm/amdgpu/userq: don't publish the queue before create is done with it
Subsystems (2)
PATCH 1/3 drm/amdgpu/userq: don't publish the queue before create is done with it
amdgpu_userq_create() inserts the queue into userq_xa and then sets up its debugfs entry without holding a reference. A concurrent AMDGPU_USERQ_OP_FREE on the (easily guessed) queue ID can free the queue in that window, so create writes to freed memory and leaves a queue_N debugfs directory whose mqd_info points to the freed queue:
debugfs: 'queue_1' already exists in 'client-137'
Only reserve the ID in xa_alloc() and store the queue once create is done with it. Don't let FREE release a reserved ID.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +12 -2
@@ -775,7 +775,12 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)atomic_inc (&uq_mgr->userq_count[queue->queue_type]);mutex_unlock (&uq_mgr->userq_mutex); - r =xa_alloc (&uq_mgr->userq_xa, &qid, queue, +/* +* Only reserve the ID here. Once the queue is visible in userq_xa, +* a concurrent AMDGPU_USERQ_OP_FREE can drop the last reference, so +* publish it only after we are done with it. +*/ + r =xa_alloc (&uq_mgr->userq_xa, &qid,NULL ,XA_LIMIT (1 ,AMDGPU_MAX_USERQ_COUNT ),GFP_KERNEL );if (r) {
@@ -790,6 +795,8 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)amdgpu_debugfs_userq_init (filp, queue, qid);trace_amdgpu_userq_create_end (queue,0 ); +/* Storing to a reserved entry doesn't allocate memory and can't fail */ +xa_store (&uq_mgr->userq_xa, qid, queue,GFP_KERNEL ); args->out.queue_id = qid;return 0 ;
@@ -924,7 +931,10 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,case AMDGPU_USERQ_OP_FREE : {xa_lock (&fpriv->userq_mgr.userq_xa); - queue =__xa_erase (&fpriv->userq_mgr.userq_xa, args->in.queue_id); +/* Don't release an ID reserved by an in-flight CREATE */ + queue =xa_load (&fpriv->userq_mgr.userq_xa, args->in.queue_id); +if (queue) +__xa_erase (&fpriv->userq_mgr.userq_xa, args->in.queue_id);xa_unlock (&fpriv->userq_mgr.userq_xa);if (!queue)return -ENOENT ;
Signature · 1 line
2.50.1
PATCH 2/3 drm/amdgpu/userq: only accept doorbell BOs as queue doorbell
amdgpu_userq_get_doorbell_index() pins any BO passed as doorbell_handle into the doorbell domain. For a regular BO, TTM moves it with ttm_bo_move_null() and its contents are lost. Since an importer on the same device gets the exporter's GEM object, a client can do this to a buffer another client shared with it.
Reject BOs that were not created in the doorbell domain.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
@@ -477,6 +477,16 @@ amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr,return -EINVAL ; } +/* +* Pinning a regular BO into the doorbell domain would discard its +* contents, possibly those of a buffer shared by another client. +*/ +if (!(gem_to_amdgpu_bo (gobj)->preferred_domains &AMDGPU_GEM_DOMAIN_DOORBELL )) { +drm_file_err (uq_mgr->file,"Doorbell handle is not a doorbell BO\n" ); +drm_gem_object_put (gobj); +return -EINVAL ; + } + db_obj->obj =amdgpu_bo_ref (gem_to_amdgpu_bo (gobj));drm_gem_object_put (gobj);
Signature · 1 line
2.50.1
Christian König
maintainerRe: [PATCH 2/3] drm/amdgpu/userq: only accept doorbell BOs as queue doorbell
Quoted 19 lines · Jesse Zhang
amdgpu_userq_get_doorbell_index()pins any BO passed asdoorbell_handleinto the doorbell domain. For a regular BO, TTM moves it withttm_bo_move_null()and its contents are lost. Since an importer on the same device gets the exporter's GEM object, a client can do this to a buffer another client shared with it.Reject BOs that were not created in the doorbell domain.
Fixes f09c1e6077ab ("drm/amdgpu: generate doorbell index for userqueue")Signed-off-by Jesse Zhang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr, · line 485
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6
-0@@ -477,6 +477,16 @@ amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr,return -EINVAL ; } +/* +* Pinning a regular BO into the doorbell domain would discard its +* contents, possibly those of a buffer shared by another client. +*/ +if (!(gem_to_amdgpu_bo (gobj)->preferred_domains &AMDGPU_GEM_DOMAIN_DOORBELL )) { +drm_file_err (uq_mgr->file,"Doorbell handle is not a doorbell BO\n" );
Drop the error message, userspace could abuse that to internally spam the logs.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr, · line 490
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-0+drm_gem_object_put (gobj); +return -EINVAL ; + } + db_obj->obj =amdgpu_bo_ref (gem_to_amdgpu_bo (gobj));
Maybe have a local abo variable so that you don't have the cast twice.
Apart from that looks good to me,
Christian.
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0drm_gem_object_put (gobj);
PATCH 3/3 drm/amdgpu/userq: return the memdup_user() error for the user MQD
In reply to Jesse Zhangmes_userq_mqd_create() returns -ENOMEM when copying the user MQD fails, even though memdup_user() returns -EFAULT for a bad pointer. Return PTR_ERR() instead.
--- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -3
@@ -408,7 +408,7 @@ static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue, compute_mqd =memdup_user (u64_to_user_ptr (mqd_user->mqd), mqd_user->mqd_size);if (IS_ERR (compute_mqd)) {DRM_ERROR ("Failed to read user MQD\n" ); - r = -ENOMEM ; + r =PTR_ERR (compute_mqd); goto free_mqd; }
@@ -453,7 +453,7 @@ static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue, mqd_gfx_v11 =memdup_user (u64_to_user_ptr (mqd_user->mqd), mqd_user->mqd_size);if (IS_ERR (mqd_gfx_v11)) {DRM_ERROR ("Failed to read user MQD\n" ); - r = -ENOMEM ; + r =PTR_ERR (mqd_gfx_v11); goto free_mqd; }
@@ -498,7 +498,7 @@ static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue, mqd_sdma_v11 =memdup_user (u64_to_user_ptr (mqd_user->mqd), mqd_user->mqd_size);if (IS_ERR (mqd_sdma_v11)) {DRM_ERROR ("Failed to read sdma user MQD\n" ); - r = -ENOMEM ; + r =PTR_ERR (mqd_sdma_v11); goto free_mqd; }
Signature · 1 line
2.50.1
Christian König
maintainerRe: [PATCH 3/3] drm/amdgpu/userq: return the memdup_user() error for the user MQD
Quoted 16 lines · Jesse Zhang
mes_userq_mqd_create()returns-ENOMEMwhen copying the user MQD fails, even thoughmemdup_user()returns-EFAULTfor a bad pointer. ReturnPTR_ERR()instead.Fixes 2c695d7c0720 ("drm/amdgpu: enable compute/gfx usermode queue")Fixes 543b61453774 ("drm/amdgpu: enable SDMA usermode queues")Signed-off-by Jesse Zhang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue, · line 410
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0@@ -408,7 +408,7 @@ static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue, compute_mqd =memdup_user (u64_to_user_ptr (mqd_user->mqd), mqd_user->mqd_size);if (IS_ERR (compute_mqd)) {DRM_ERROR ("Failed to read user MQD\n" );
While at it please drop all those error message.
First of all memdup_user() can be interrupted by a signal and that isn't really an error, it just aborts the operation.
And then second spamming the logs with error messages on invalid IOCTL parameters is usually seen as really bad idea.
Regards,
Christian.
Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3
-3- r = -ENOMEM ; + r =PTR_ERR (compute_mqd); goto free_mqd; }@@ -453,7 +453,7 @@ static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue, mqd_gfx_v11 =memdup_user (u64_to_user_ptr (mqd_user->mqd), mqd_user->mqd_size);if (IS_ERR (mqd_gfx_v11)) {DRM_ERROR ("Failed to read user MQD\n" ); - r = -ENOMEM ; + r =PTR_ERR (mqd_gfx_v11); goto free_mqd; }@@ -498,7 +498,7 @@ static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue, mqd_sdma_v11 =memdup_user (u64_to_user_ptr (mqd_user->mqd), mqd_user->mqd_size);if (IS_ERR (mqd_sdma_v11)) {DRM_ERROR ("Failed to read sdma user MQD\n" ); - r = -ENOMEM ; + r =PTR_ERR (mqd_sdma_v11); goto free_mqd; }
Christian König
maintainerQuoted 7 lines · Jesse Zhang
amdgpu_userq_create()inserts the queue intouserq_xaand then sets up its debugfs entry without holding a reference. A concurrentAMDGPU_USERQ_OP_FREEon the (easily guessed) queue ID can free the queue in that window, so create writes to freed memory and leaves aqueue_Ndebugfs directory whosemqd_infopoints to the freed queue:debugfs: '
queue_1' already exists in 'client-137'
I honestly don't see that as problematic.
debugfs has tons of such issues and that is generally ok.
Regards,
Christian.
Quoted 49 lines
Only reserve the ID in
xa_alloc()and store the queue once create is done with it. Don't let FREE release a reserved ID.Fixes 28cacaace5cd ("drm/amdgpu/userq: defer queue publication until create completes")Signed-off-by Jesse Zhang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +12
-2@@ -775,7 +775,12 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)atomic_inc (&uq_mgr->userq_count[queue->queue_type]);mutex_unlock (&uq_mgr->userq_mutex); - r =xa_alloc (&uq_mgr->userq_xa, &qid, queue, +/* +* Only reserve the ID here. Once the queue is visible in userq_xa, +* a concurrent AMDGPU_USERQ_OP_FREE can drop the last reference, so +* publish it only after we are done with it. +*/ + r =xa_alloc (&uq_mgr->userq_xa, &qid,NULL ,XA_LIMIT (1 ,AMDGPU_MAX_USERQ_COUNT ),GFP_KERNEL );if (r) {@@ -790,6 +795,8 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)amdgpu_debugfs_userq_init (filp, queue, qid);trace_amdgpu_userq_create_end (queue,0 ); +/* Storing to a reserved entry doesn't allocate memory and can't fail */ +xa_store (&uq_mgr->userq_xa, qid, queue,GFP_KERNEL ); args->out.queue_id = qid;return 0 ;@@ -924,7 +931,10 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,case AMDGPU_USERQ_OP_FREE : {xa_lock (&fpriv->userq_mgr.userq_xa); - queue =__xa_erase (&fpriv->userq_mgr.userq_xa, args->in.queue_id); +/* Don't release an ID reserved by an in-flight CREATE */ + queue =xa_load (&fpriv->userq_mgr.userq_xa, args->in.queue_id); +if (queue) +__xa_erase (&fpriv->userq_mgr.userq_xa, args->in.queue_id);xa_unlock (&fpriv->userq_mgr.userq_xa);if (!queue)return -ENOENT ;