Skip to contentSkip to conversation

drm/amdgpu: validate fence_count in wait_fences ioctl

PATCH Superseded Jesse.Zhang 3 participants 8 messages Mar 17

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 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

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c +1 -1
index 341beec59537..4126e5026c20 100644
@@ -104,7 +104,7 @@ int amdgpu_sched_ioctl(struct drm_device *dev, void *data,
 	}
 
 	if (!amdgpu_ctx_priority_is_valid(args->in.priority)) {
-		WARN(1, "Invalid context priority %d\n", args->in.priority);
+		DRM_ERROR("Invalid context priority %d\n", args->in.priority);
 		return -EINVAL;
 	}
 
Signature · 1 line
2.49.0
maintainer
#2 lore raw

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

Quoted 28 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

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c +0 -0
index 341beec59537..4126e5026c20 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c int amdgpu_sched_ioctl(struct drm_device *dev, void *data, · line 107

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c +1 -1
@@ -104,7 +104,7 @@ int amdgpu_sched_ioctl(struct drm_device *dev, void *data,
 	}
 
 	if (!amdgpu_ctx_priority_is_valid(args->in.priority)) {
-		WARN(1, "Invalid context priority %d\n", args->in.priority);
+		DRM_ERROR("Invalid context priority %d\n", args->in.priority);

Please completely drop that warning. Invalid parameters should never clutter the system log.

Apart from that looks good to me.

Regards,
Christian

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c +0 -0
 		return -EINVAL;
 	}
 
#3 lore raw

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

Quoted 31 lines · Christian König
Quoted 30 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

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c +0 -0
index 341beec59537..4126e5026c20 100644
@@ -104,7 +104,7 @@ int amdgpu_sched_ioctl(struct drm_device *dev, void *data,
 	}
 
Quoted 5 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c int amdgpu_sched_ioctl(struct drm_device *dev, void *data, · line 107

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c +1 -1
 	if (!amdgpu_ctx_priority_is_valid(args->in.priority)) {
-		WARN(1, "Invalid context priority %d\n", args->in.priority);
+		DRM_ERROR("Invalid context priority %d\n", args->in.priority);

Please completely drop that warning. Invalid parameters should never clutter the system log.

Or make it drm_dbg(_driver).

Signature · 2 lines
Earthling Michel Dänzer       \        GNOME / Xwayland / Mesa developer
https://redhat.com             \               Libre software enthusiast
#4 lore raw

PATCH 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
- Updated code comments for clarity: describe the type width mismatch and why the early check is needed
- No functional changes to the code itself

---
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   | 37 ++++++++++++++++++-
 1 file changed, 35 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +35 -2
index fad595401a77..575dd58ed152 100644
@@ -480,8 +480,25 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
 	if (!amdgpu_userq_enabled(dev))
 		return -ENOTSUPP;
 
+	/*
+	 * num_syncobj_handles is __u64 in the UAPI but stored in a u32
+	 * in the driver.  Check all three handle counts against the
+	 * maximum *before* the narrowing assignment so that values
+	 * above 2^32 are correctly rejected instead of being silently
+	 * truncated to a smaller (possibly zero) value.
+	 */
 	if (args->num_bo_write_handles > AMDGPU_USERQ_MAX_HANDLES ||
-	    args->num_bo_read_handles > AMDGPU_USERQ_MAX_HANDLES)
+	    args->num_bo_read_handles  > AMDGPU_USERQ_MAX_HANDLES ||
+	    args->num_syncobj_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;
@@ -639,7 +656,23 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,
 		return -ENOTSUPP;
 
 	if (wait_info->num_bo_write_handles > AMDGPU_USERQ_MAX_HANDLES ||
-	    wait_info->num_bo_read_handles > AMDGPU_USERQ_MAX_HANDLES)
+	    wait_info->num_bo_read_handles  > AMDGPU_USERQ_MAX_HANDLES ||
+	    wait_info->num_syncobj_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;
Signature · 1 line
2.49.0
maintainer
#5 lore raw

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

Quoted 32 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
- Updated code comments for clarity: describe the type width mismatch and why the early check is needed
- No functional changes to the code itself

---
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   | 37 ++++++++++++++++++-
 1 file changed, 35 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0 -0
index fad595401a77..575dd58ed152 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data, · line 489

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +7 -0
@@ -480,8 +480,25 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
 	if (!amdgpu_userq_enabled(dev))
 		return -ENOTSUPP;
 
+	/*
+	 * num_syncobj_handles is __u64 in the UAPI but stored in a u32
+	 * in the driver.  Check all three handle counts against the
+	 * maximum *before* the narrowing assignment so that values
+	 * above 2^32 are correctly rejected instead of being silently
+	 * truncated to a smaller (possibly zero) value.
+	 */

That is completely unnecessary, just reduce the variables size in the UAPI as we already did for num_syncobj_handles.

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data, · line 492

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -1
 	if (args->num_bo_write_handles > AMDGPU_USERQ_MAX_HANDLES ||
-	    args->num_bo_read_handles > AMDGPU_USERQ_MAX_HANDLES)
+	    args->num_bo_read_handles  > AMDGPU_USERQ_MAX_HANDLES ||
+	    args->num_syncobj_handles  > AMDGPU_USERQ_MAX_HANDLES) {

You don't need to check num_syncobj_handles here.

Regards,
Christian.

Quoted 37 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +26 -1
+		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;
@@ -639,7 +656,23 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,
 		return -ENOTSUPP;
 
 	if (wait_info->num_bo_write_handles > AMDGPU_USERQ_MAX_HANDLES ||
-	    wait_info->num_bo_read_handles > AMDGPU_USERQ_MAX_HANDLES)
+	    wait_info->num_bo_read_handles  > AMDGPU_USERQ_MAX_HANDLES ||
+	    wait_info->num_syncobj_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;
#6 lore raw

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

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 61 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, March 17, 2026 3:08 PM
To: Zhang, Jesse(Jie) <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Prosyak, Vitaly <[email protected]>
Subject: Re: [PATCH 3/3] drm/amdgpu: harden SIGNAL/WAIT ioctl argument validation

Quoted 3 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

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_signal_ioctl(struct drm_device *dev,

Quoted 30 lines

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
- Updated code comments for clarity: describe the type width mismatch and why the early check is needed
- No functional changes to the code itself

---
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   | 37 ++++++++++++++++++-
 1 file changed, 35 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0 -0
index fad595401a77..575dd58ed152 100644
@@ -480,8 +480,25 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev,

void *data,

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data, · line 489

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +7 -0
    if (!amdgpu_userq_enabled(dev))
            return -ENOTSUPP;
 
+   /*
+    * num_syncobj_handles is __u64 in the UAPI but stored in a u32
+    * in the driver.  Check all three handle counts against the
+    * maximum *before* the narrowing assignment so that values
+    * above 2^32 are correctly rejected instead of being silently
+    * truncated to a smaller (possibly zero) value.
+    */

That is completely unnecessary, just reduce the variables size in the UAPI as we already did for num_syncobj_handles.

Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data, · line 492

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -1
    if (args->num_bo_write_handles > AMDGPU_USERQ_MAX_HANDLES ||
-       args->num_bo_read_handles > AMDGPU_USERQ_MAX_HANDLES)
+       args->num_bo_read_handles  > AMDGPU_USERQ_MAX_HANDLES ||
+       args->num_syncobj_handles  > AMDGPU_USERQ_MAX_HANDLES) {

You don't need to check num_syncobj_handles here.

Thanks Christian.
I will drop the num_syncobj_handles > AMDGPU_USERQ_MAX_HANDLES checks from both SIGNAL and WAIT paths.

Only keep the pointer/count consistency checks (non-NULL pointer with zero count -> -EINVAL), since those are still useful for deterministic early input validation and do not change behavior for valid userspace.

Thanks
Jesse

Quoted 46 lines

Regards,
Christian.

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +9 -0
+           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; @@ -639,7 +656,23

@@ int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,

Quoted 3 lines
            return -ENOTSUPP;

    if (wait_info->num_bo_write_handles >

AMDGPU_USERQ_MAX_HANDLES ||

Quoted 1 line

- wait_info->num_bo_read_handles >

AMDGPU_USERQ_MAX_HANDLES)

Quoted 1 line

+ wait_info->num_bo_read_handles >

AMDGPU_USERQ_MAX_HANDLES ||

Quoted 1 line

+ wait_info->num_syncobj_handles >

AMDGPU_USERQ_MAX_HANDLES)

Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +9 -0
+           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))

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +6 -0
+           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;
maintainer
#7 lore raw
In reply to Jesse.Zhang
Quoted 12 lines · Jesse.Zhang

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.

Quoted 7 lines

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

Good catch, Reviewed-by: Christian König <[email protected]>

Quoted 22 lines
---
 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,