Skip to contentSkip to conversation

drm/amdgpu: validate fence_count in wait_fences ioctl

PATCH In review Jesse.Zhang 2 participants 5 messages Mar 17

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

PATCH v3 1/3 drm/amdgpu: validate fence_count in wait_fences ioctl

Add an early parameter check in amdgpu_cs_wait_fences_ioctl() to reject a zero fence_count with -EINVAL.

dma_fence_wait_any_timeout() requires count > 0. When userspace passes fence_count == 0, the call propagates down to dma_fence core which does not expect a zero-length array and triggers a WARN_ON.

Return -EINVAL immediately so the caller gets a clear error instead of hitting an unexpected warning in the DMA fence subsystem.

No functional change for well-formed userspace callers.

v2:
- Reworked commit message to clarify the parameter validation rationale
- Removed verbose crash log from commit description
- Simplified inline code comment

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +7 -0
index b8d23a9f6dd3..22aafa969b3d 100644
@@ -1743,6 +1743,13 @@ int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
 	struct drm_amdgpu_fence *fences;
 	int r;
 
+	/*
+	 * fence_count must be non-zero; dma_fence_wait_any_timeout()
+	 * does not accept an empty fence array.
+	 */
+	if (!wait->in.fence_count)
+		return -EINVAL;
+
 	/* Get the fences from userspace */
 	fences = memdup_array_user(u64_to_user_ptr(wait->in.fences),
 				   wait->in.fence_count,
Signature · 1 line
2.49.0
#1 lore raw

PATCH v3 2/3 drm/amdgpu: replace WARN with DRM_ERROR for invalid sched priority

amdgpu_sched_ioctl() currently uses WARN(1, ...) when userspace passes an out-of-range context priority value. WARN(1, ...) is unconditional and produces a full stack trace, which is disproportionate for a simple input validation failure -- the invalid value is already rejected with -EINVAL on the next line.

Replace WARN(1, ...) with DRM_ERROR() to log the invalid value at an appropriate level without generating a stack dump. The -EINVAL return to userspace is unchanged.

No functional change for well-formed userspace callers.

v2:
- Reworked commit message to focus on appropriate log level for parameter validation
- Clarified that -EINVAL behavior is preserved (Vitaly)

v3: completely drop that warning.
Invalid parameters should never clutter the system log. (Christian)

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c +1 -3
index 341beec59537..0eecfaa3a94c 100644
@@ -103,10 +103,8 @@ int amdgpu_sched_ioctl(struct drm_device *dev, void *data,
 		return -EINVAL;
 	}
 
-	if (!amdgpu_ctx_priority_is_valid(args->in.priority)) {
-		WARN(1, "Invalid context priority %d\n", args->in.priority);
+	if (!amdgpu_ctx_priority_is_valid(args->in.priority))
 		return -EINVAL;
-	}
 
 	switch (args->in.op) {
 	case AMDGPU_SCHED_OP_PROCESS_PRIORITY_OVERRIDE:
Signature · 1 line
2.49.0
maintainer Reviewed-by on v3 2/3
#2 lore raw

Re: [PATCH v3 2/3] drm/amdgpu: replace WARN with DRM_ERROR for invalid sched priority

Quoted 17 lines · Jesse.Zhang

amdgpu_sched_ioctl() currently uses WARN(1, ...) when userspace passes an out-of-range context priority value. WARN(1, ...) is unconditional and produces a full stack trace, which is disproportionate for a simple input validation failure -- the invalid value is already rejected with -EINVAL on the next line.

Replace WARN(1, ...) with DRM_ERROR() to log the invalid value at an appropriate level without generating a stack dump. The -EINVAL return to userspace is unchanged.

No functional change for well-formed userspace callers.

v2:
- Reworked commit message to focus on appropriate log level for parameter validation
- Clarified that -EINVAL behavior is preserved (Vitaly)

Quoted 5 lines

v3: completely drop that warning.
Invalid parameters should never clutter the system log. (Christian)

Quoted 20 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c +1 -3
index 341beec59537..0eecfaa3a94c 100644
@@ -103,10 +103,8 @@ int amdgpu_sched_ioctl(struct drm_device *dev, void *data,
 		return -EINVAL;
 	}
 
-	if (!amdgpu_ctx_priority_is_valid(args->in.priority)) {
-		WARN(1, "Invalid context priority %d\n", args->in.priority);
+	if (!amdgpu_ctx_priority_is_valid(args->in.priority))
 		return -EINVAL;
-	}
 
 	switch (args->in.op) {
 	case AMDGPU_SCHED_OP_PROCESS_PRIORITY_OVERRIDE:
#3 lore raw

PATCH v3 3/3 drm/amdgpu: harden SIGNAL/WAIT ioctl argument validation

In reply to Jesse.Zhang

Tighten the early parameter checks in the USERQ SIGNAL and WAIT ioctls:

1. Validate num_syncobj_handles against AMDGPU_USERQ_MAX_HANDLES in addition to the BO handle counts that are already checked. The UAPI field is __u64 but the driver stores it in a u32, so the comparison must happen before the narrowing assignment to prevent unintended truncation (e.g. 0x1_0000_0000 would silently become 0).

2. Reject inconsistent pointer/count pairs where a non-NULL userspace pointer is provided with a zero element count. This is clearly malformed input and returning -EINVAL early gives userspace a deterministic error rather than silently proceeding with empty data.

No functional change for well-formed userspace callers.

v2:
- Reworked commit message to focus on parameter validation correctness (Vitaly)
- Updated code comments for clarity: describe the type width mismatch and why the early check is needed (Vitaly)
- No functional changes to the code itself (Vitaly)

v3: drop the num_syncobj_handles > AMDGPU_USERQ_MAX_HANDLES checks (Christian)

---
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   | 29 +++++++++++++++++++
 1 file changed, 29 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +29 -0
index fad595401a77..5ff8fc815e45 100644
@@ -484,6 +484,16 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
 	    args->num_bo_read_handles > AMDGPU_USERQ_MAX_HANDLES)
 		return -EINVAL;
 
+	/* Reject non-NULL pointers paired with a zero count. */
+	if (!args->num_syncobj_handles && args->syncobj_handles)
+		return -EINVAL;
+
+	if (!args->num_bo_read_handles && args->bo_read_handles)
+		return -EINVAL;
+
+	if (!args->num_bo_write_handles && args->bo_write_handles)
+		return -EINVAL;
+
 	num_syncobj_handles = args->num_syncobj_handles;
 	syncobj_handles = memdup_array_user(u64_to_user_ptr(args->syncobj_handles),
 					    num_syncobj_handles, sizeof(u32));
@@ -642,6 +652,25 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,
 	    wait_info->num_bo_read_handles > AMDGPU_USERQ_MAX_HANDLES)
 		return -EINVAL;
 
+	/* Reject non-NULL pointers paired with a zero count: the pointer
+	 * is meaningless and indicates inconsistent input from userspace.
+	 */
+	if (!wait_info->num_syncobj_handles && wait_info->syncobj_handles)
+		return -EINVAL;
+
+	if (!wait_info->num_syncobj_timeline_handles &&
+	    (wait_info->syncobj_timeline_handles || wait_info->syncobj_timeline_points))
+		return -EINVAL;
+
+	if (!wait_info->num_bo_read_handles && wait_info->bo_read_handles)
+		return -EINVAL;
+
+	if (!wait_info->num_bo_write_handles && wait_info->bo_write_handles)
+		return -EINVAL;
+
+	if (!wait_info->num_fences && wait_info->out_fences)
+		return -EINVAL;
+
 	num_syncobj = wait_info->num_syncobj_handles;
 	syncobj_handles = memdup_array_user(u64_to_user_ptr(wait_info->syncobj_handles),
 					    num_syncobj, sizeof(u32));
Signature · 1 line
2.49.0
maintainer Reviewed-by on v3 3/3
#4 lore raw

Re: [PATCH v3 3/3] drm/amdgpu: harden SIGNAL/WAIT ioctl argument validation

Quoted 21 lines · Jesse.Zhang

Tighten the early parameter checks in the USERQ SIGNAL and WAIT ioctls:

1. Validate num_syncobj_handles against AMDGPU_USERQ_MAX_HANDLES in addition to the BO handle counts that are already checked. The UAPI field is __u64 but the driver stores it in a u32, so the comparison must happen before the narrowing assignment to prevent unintended truncation (e.g. 0x1_0000_0000 would silently become 0).

2. Reject inconsistent pointer/count pairs where a non-NULL userspace pointer is provided with a zero element count. This is clearly malformed input and returning -EINVAL early gives userspace a deterministic error rather than silently proceeding with empty data.

No functional change for well-formed userspace callers.

v2:
- Reworked commit message to focus on parameter validation correctness (Vitaly)
- Updated code comments for clarity: describe the type width mismatch and why the early check is needed (Vitaly)
- No functional changes to the code itself (Vitaly)

Quoted 4 lines

v3: drop the num_syncobj_handles > AMDGPU_USERQ_MAX_HANDLES checks (Christian)

We should also take another look at the UAPI and make sure that all those num_* fields are either u16 or u32 and not u64.

Regards,
Christian.

Quoted 51 lines
---
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   | 29 +++++++++++++++++++
 1 file changed, 29 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +29 -0
index fad595401a77..5ff8fc815e45 100644
@@ -484,6 +484,16 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
 	    args->num_bo_read_handles > AMDGPU_USERQ_MAX_HANDLES)
 		return -EINVAL;
 
+	/* Reject non-NULL pointers paired with a zero count. */
+	if (!args->num_syncobj_handles && args->syncobj_handles)
+		return -EINVAL;
+
+	if (!args->num_bo_read_handles && args->bo_read_handles)
+		return -EINVAL;
+
+	if (!args->num_bo_write_handles && args->bo_write_handles)
+		return -EINVAL;
+
 	num_syncobj_handles = args->num_syncobj_handles;
 	syncobj_handles = memdup_array_user(u64_to_user_ptr(args->syncobj_handles),
 					    num_syncobj_handles, sizeof(u32));
@@ -642,6 +652,25 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,
 	    wait_info->num_bo_read_handles > AMDGPU_USERQ_MAX_HANDLES)
 		return -EINVAL;
 
+	/* Reject non-NULL pointers paired with a zero count: the pointer
+	 * is meaningless and indicates inconsistent input from userspace.
+	 */
+	if (!wait_info->num_syncobj_handles && wait_info->syncobj_handles)
+		return -EINVAL;
+
+	if (!wait_info->num_syncobj_timeline_handles &&
+	    (wait_info->syncobj_timeline_handles || wait_info->syncobj_timeline_points))
+		return -EINVAL;
+
+	if (!wait_info->num_bo_read_handles && wait_info->bo_read_handles)
+		return -EINVAL;
+
+	if (!wait_info->num_bo_write_handles && wait_info->bo_write_handles)
+		return -EINVAL;
+
+	if (!wait_info->num_fences && wait_info->out_fences)
+		return -EINVAL;
+
 	num_syncobj = wait_info->num_syncobj_handles;
 	syncobj_handles = memdup_array_user(u64_to_user_ptr(wait_info->syncobj_handles),
 					    num_syncobj, sizeof(u32));