Skip to contentSkip to conversation

drm/amdkfd: fix integer overflow in CWSR buffer size validation

PATCH New Yongqiang Sun 1 participant 1 message Jul 6

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdkfd: fix integer overflow in CWSR buffer size validation

CREATE_QUEUE takes an unprivileged, only lower-bounded ctx_save_restore_area_size. kfd_queue_acquire_buffers() computes the total CWSR size as (ctx_save_restore_area_size + debug_memory_size) * NUM_XCC() in a u32 with no overflow check. On multi-XCC parts (NUM_XCC up to 8) a large size wraps to a small value, so an undersized BO passes validation while init_mqd_v9_4_3() still strides by the raw size -- a GPU-side out-of-bounds write from an unprivileged /dev/kfd caller.

Compute the total with check_add/mul_overflow() and reject on overflow, and bound-check the per-XCC CWSR stride in the MQD managers.

---
 .../drm/amd/amdkfd/kfd_mqd_manager_v12_1.c    |  8 ++-
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c   |  8 ++-
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h         | 31 ++++++++++++
 drivers/gpu/drm/amd/amdkfd/kfd_queue.c        | 49 ++++++++++++++++---
 4 files changed, 88 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v12_1.c +7 -1
index a6fff8032dce..d45b3fc3baec 100644
@@ -473,7 +473,8 @@ static void init_mqd_v12_1(struct mqd_manager *mm, void **mqd,
 		 * and CWSR area is allocated in thunk
 		 */
 		if (mm->dev->kfd->cwsr_enabled &&
-		    q->ctx_save_restore_area_address) {
+		    q->ctx_save_restore_area_address &&
+		    !WARN_ON_ONCE(!kfd_queue_cwsr_xcc_in_bounds(q, xcc))) {
 			xcc_ctx_save_restore_area_address =
 				q->ctx_save_restore_area_address +
 				(xcc * q->ctx_save_restore_area_size);
@@ -596,6 +597,11 @@ static int get_wave_state_v12_1(struct mqd_manager *mm, void *mqd,
 	u32 tmp_ctl_stack_used_size = 0, tmp_save_area_used_size = 0;
 
 	for (xcc = 0; xcc < NUM_XCC(mm->dev->xcc_mask); xcc++) {
+		if (WARN_ON_ONCE(!kfd_queue_cwsr_xcc_in_bounds(q, xcc))) {
+			err = -EINVAL;
+			break;
+		}
+
 		xcc_mqd = mqd + mqd_stride_size * xcc;
 		xcc_ctl_stack = (void __user *)((uintptr_t)ctl_stack +
 					q->ctx_save_restore_area_size * xcc);
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +7 -1
index 415e9d8130cf..0dc5e6acb5dc 100644
@@ -737,7 +737,8 @@ static void init_mqd_v9_4_3(struct mqd_manager *mm, void **mqd,
 		 * and CWSR area is allocated in thunk
 		 */
 		if (mm->dev->kfd->cwsr_enabled &&
-		    q->ctx_save_restore_area_address) {
+		    q->ctx_save_restore_area_address &&
+		    !WARN_ON_ONCE(!kfd_queue_cwsr_xcc_in_bounds(q, xcc))) {
 			xcc_ctx_save_restore_area_address =
 				q->ctx_save_restore_area_address +
 				(xcc * q->ctx_save_restore_area_size);
@@ -928,6 +929,11 @@ static int get_wave_state_v9_4_3(struct mqd_manager *mm, void *mqd,
 	u32 tmp_ctl_stack_used_size = 0, tmp_save_area_used_size = 0;
 
 	for (xcc = 0; xcc < NUM_XCC(mm->dev->xcc_mask); xcc++) {
+		if (WARN_ON_ONCE(!kfd_queue_cwsr_xcc_in_bounds(q, xcc))) {
+			err = -EINVAL;
+			break;
+		}
+
 		xcc_mqd = mqd + mqd_stride_size * xcc;
 		xcc_ctl_stack = (void __user *)((uintptr_t)ctl_stack +
 					q->ctx_save_restore_area_size * xcc);
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +31 -0
index 6e559aab4009..ef6d1c8aa780 100644
@@ -541,6 +541,14 @@ struct queue_properties {
 	uint32_t eop_ring_buffer_size;
 	uint64_t ctx_save_restore_area_address;
 	uint32_t ctx_save_restore_area_size;
+	/*
+	 * Total, overflow-checked size of the CWSR buffer across all XCCs as
+	 * validated against the pinned BO in kfd_queue_acquire_buffers(). The
+	 * MQD managers use this as an upper bound when programming the per-XCC
+	 * CWSR save base so the raw per-XCC stride can never index past the
+	 * validated allocation.
+	 */
+	uint32_t cwsr_area_total_size;
 	uint32_t ctl_stack_size;
 	uint64_t tba_addr;
 	uint64_t tma_addr;
@@ -553,6 +561,29 @@ struct queue_properties {
 	struct amdgpu_bo *cwsr_bo;
 };
 
+/*
+ * Bound-check a per-XCC CWSR save area against the validated total CWSR buffer
+ * size. The MQD managers program the per-XCC CWSR base using the raw
+ * ctx_save_restore_area_size as the stride; this confirms the save area for
+ * @xcc lies within the allocation that kfd_queue_acquire_buffers() validated.
+ *
+ * Returns true when the access is in bounds. A false return indicates a kernel
+ * bug (the overflow-checked validation in kfd_queue_acquire_buffers() should
+ * have already rejected the queue), and the caller must not program an
+ * out-of-bounds CWSR base. When no validated size was recorded
+ * (cwsr_area_total_size == 0, e.g. non-CWSR queues) the legacy behavior is
+ * preserved.
+ */
+static inline bool kfd_queue_cwsr_xcc_in_bounds(const struct queue_properties *q,
+						unsigned int xcc)
+{
+	if (!q->cwsr_area_total_size)
+		return true;
+
+	return (u64)(xcc + 1) * q->ctx_save_restore_area_size <=
+	       q->cwsr_area_total_size;
+}
+
 #define QUEUE_IS_ACTIVE(q) ((q).queue_size > 0 &&	\
 			    (q).queue_address != 0 &&	\
 			    (q).queue_percent > 0 &&	\
drivers/gpu/drm/amd/amdkfd/kfd_queue.c +43 -6

73 lines not on this page · raw message

Signature · 1 line
2.43.0