Skip to contentSkip to conversation

drm/v3d: Limit ioctl extension chain depth to prevent infinite loop

PATCH Superseded Ashutosh Desai 2 participants 7 messages Apr 10 – Apr 14

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

PATCH 1/1 drm/v3d: Limit ioctl extension chain depth to prevent infinite loop

v3d_get_extensions() walks a userspace-provided singly-linked list of ioctl extensions without any bound on the chain length. A local user can craft a self-referential extension (ext->next == &ext) with zero in_sync_count and out_sync_count, which bypasses the existing duplicate- extension guard:

    if (se->in_sync_count || se->out_sync_count)
            return -EINVAL;

The guard never fires because v3d_get_multisync_post_deps() returns immediately when count is zero, leaving both fields at zero on every iteration. The result is an infinite loop in kernel context, blocking the calling thread and pegging a CPU core indefinitely.

Both i915 (stackdepth = 512) and xe (MAX_USER_EXTENSIONS = 16) impose an explicit depth limit on the same pattern. Apply the same defence to V3D by capping the walk at 16 extensions.

---
 drivers/gpu/drm/v3d/v3d_submit.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/v3d/v3d_submit.c +6 -0
index 18f2bf1fe..491eeb6b3 100644
@@ -802,12 +802,18 @@ v3d_get_extensions(struct drm_file *file_priv,
 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
 	struct v3d_dev *v3d = v3d_priv->v3d;
 	struct drm_v3d_extension __user *user_ext;
+	unsigned int ext_count = 0;
 	int ret;
 
 	user_ext = u64_to_user_ptr(ext_handles);
 	while (user_ext) {
 		struct drm_v3d_extension ext;
 
+		if (ext_count++ >= 16) {
+			drm_dbg(&v3d->drm, "Too many V3D ioctl extensions\n");
+			return -E2BIG;
+		}
+
 		if (copy_from_user(&ext, user_ext, sizeof(ext))) {
 			drm_dbg(&v3d->drm, "Failed to copy submit extension\n");
 			return -EFAULT;
Signature · 1 line
2.34.1
maintainer
#1 lore raw

Hi Ashutosh,

Quoted 14 lines · Ashutosh Desai

v3d_get_extensions() walks a userspace-provided singly-linked list of ioctl extensions without any bound on the chain length. A local user can craft a self-referential extension (ext->next == &ext) with zero in_sync_count and out_sync_count, which bypasses the existing duplicate- extension guard:

     if (se->in_sync_count || se->out_sync_count)
             return -EINVAL;

The guard never fires because v3d_get_multisync_post_deps() returns immediately when count is zero, leaving both fields at zero on every iteration. The result is an infinite loop in kernel context, blocking the calling thread and pegging a CPU core indefinitely.

Quoted 6 lines

Both i915 (stackdepth = 512) and xe (MAX_USER_EXTENSIONS = 16) impose an explicit depth limit on the same pattern. Apply the same defence to V3D by capping the walk at 16 extensions.

As Jani mentioned in a previous e-mail, if this is AI-generated, please add an AI coding assistant attribution.

That said, I believe this makes a lot of sense. Some comments below.

Quoted 10 lines
---
  drivers/gpu/drm/v3d/v3d_submit.c | 6 ++++++
  1 file changed, 6 insertions(+)
drivers/gpu/drm/v3d/v3d_submit.c +0 -0
index 18f2bf1fe..491eeb6b3 100644
@@ -802,12 +802,18 @@ v3d_get_extensions(struct drm_file *file_priv,
  	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;

drivers/gpu/drm/v3d/v3d_submit.c v3d_get_extensions(struct drm_file *file_priv, · line 812

Quoted 10 lines
drivers/gpu/drm/v3d/v3d_submit.c +2 -0
  	struct v3d_dev *v3d = v3d_priv->v3d;
  	struct drm_v3d_extension __user *user_ext;
+	unsigned int ext_count = 0;
  	int ret;
 
  	user_ext = u64_to_user_ptr(ext_handles);
  	while (user_ext) {
  		struct drm_v3d_extension ext;
 
+		if (ext_count++ >= 16) {

I believe we could limit it to 7, which is the number of existing V3D extensions. Also, we could add this number as a #define, to make things more clear.

Best regards,
- Maíra

Quoted 7 lines
drivers/gpu/drm/v3d/v3d_submit.c +4 -0
+			drm_dbg(&v3d->drm, "Too many V3D ioctl extensions\n");
+			return -E2BIG;
+		}
+
  		if (copy_from_user(&ext, user_ext, sizeof(ext))) {
  			drm_dbg(&v3d->drm, "Failed to copy submit extension\n");
  			return -EFAULT;
#2 lore raw

PATCH v2 1/1 drm/v3d: Limit ioctl extension chain depth to prevent infinite loop

v3d_get_extensions() walks a userspace-provided singly-linked list of ioctl extensions without any bound on the chain length. A local user can craft a self-referential extension (ext->next == &ext) with zero in_sync_count and out_sync_count, which bypasses the existing duplicate- extension guard:

    if (se->in_sync_count || se->out_sync_count)
            return -EINVAL;

The guard never fires because v3d_get_multisync_post_deps() returns immediately when count is zero, leaving both fields at zero on every iteration. The result is an infinite loop in kernel context, blocking the calling thread and pegging a CPU core indefinitely.

Both i915 (stackdepth = 512) and xe (MAX_USER_EXTENSIONS = 16) impose an explicit depth limit on the same pattern. Apply the same defence to V3D by introducing V3D_MAX_EXTENSIONS and capping the walk at 7, which matches the number of currently defined V3D extension types.

---
 drivers/gpu/drm/v3d/v3d_submit.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/gpu/drm/v3d/v3d_submit.c +8 -0
index 18f2bf1fe89f..8951909198c2 100644
@@ -11,6 +11,8 @@
 #include "v3d_regs.h"
 #include "v3d_trace.h"
 
+#define V3D_MAX_EXTENSIONS 7
+
 /* Takes the reservation lock on all the BOs being referenced, so that
  * we can attach fences and update the reservations after pushing the job
  * to the queue.
@@ -802,12 +804,18 @@ v3d_get_extensions(struct drm_file *file_priv,
 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
 	struct v3d_dev *v3d = v3d_priv->v3d;
 	struct drm_v3d_extension __user *user_ext;
+	unsigned int ext_count = 0;
 	int ret;
 
 	user_ext = u64_to_user_ptr(ext_handles);
 	while (user_ext) {
 		struct drm_v3d_extension ext;
 
+		if (ext_count++ >= V3D_MAX_EXTENSIONS) {
+			drm_dbg(&v3d->drm, "Too many V3D ioctl extensions\n");
+			return -E2BIG;
+		}
+
 		if (copy_from_user(&ext, user_ext, sizeof(ext))) {
 			drm_dbg(&v3d->drm, "Failed to copy submit extension\n");
 			return -EFAULT;
Signature · 1 line
2.34.1
maintainer
#3 lore raw

Hi Ashutosh,

Quoted 9 lines · Ashutosh Desai

v3d_get_extensions() walks a userspace-provided singly-linked list of ioctl extensions without any bound on the chain length. A local user can craft a self-referential extension (ext->next == &ext) with zero in_sync_count and out_sync_count, which bypasses the existing duplicate- extension guard:

     if (se->in_sync_count || se->out_sync_count)
             return -EINVAL;
Quoted 7 lines

The guard never fires because v3d_get_multisync_post_deps() returns immediately when count is zero, leaving both fields at zero on every iteration. The result is an infinite loop in kernel context, blocking the calling thread and pegging a CPU core indefinitely.

Both i915 (stackdepth = 512) and xe (MAX_USER_EXTENSIONS = 16) impose an explicit depth limit on the same pattern. Apply the same defence to

s/defence/defense

Quoted 14 lines

V3D by introducing V3D_MAX_EXTENSIONS and capping the walk at 7, which matches the number of currently defined V3D extension types.

---
  drivers/gpu/drm/v3d/v3d_submit.c | 8 ++++++++
  1 file changed, 8 insertions(+)
drivers/gpu/drm/v3d/v3d_submit.c +0 -0
index 18f2bf1fe89f..8951909198c2 100644

drivers/gpu/drm/v3d/v3d_submit.c line 14

Quoted 4 lines
drivers/gpu/drm/v3d/v3d_submit.c +1 -0
@@ -11,6 +11,8 @@
  #include "v3d_regs.h"
  #include "v3d_trace.h"
 
+#define V3D_MAX_EXTENSIONS 7

I'm sorry for my previous mistake. I was re-thinking it right now and actually, at the moment, a job can have 2 extensions at max, as a CPU job cannot have more than one CPU job extension (check v3d_validate_cpu_job()).

v3d_validate_cpu_job() already guarantees that a CPU job can only have a single CPU job extension. So, we are safe on that side. Now, we need to harden the multisync side.

How about checking if (!multisync.in_sync_count && !multisync.out_sync_count)? After all, it doesn't make any sense to have an empty multisync.

I believe this is better strategy than using a hard-coded max.

Best regards,
- Maíra

Quoted 23 lines
drivers/gpu/drm/v3d/v3d_submit.c +7 -0
+
  /* Takes the reservation lock on all the BOs being referenced, so that
   * we can attach fences and update the reservations after pushing the job
   * to the queue.
@@ -802,12 +804,18 @@ v3d_get_extensions(struct drm_file *file_priv,
  	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
  	struct v3d_dev *v3d = v3d_priv->v3d;
  	struct drm_v3d_extension __user *user_ext;
+	unsigned int ext_count = 0;
  	int ret;
 
  	user_ext = u64_to_user_ptr(ext_handles);
  	while (user_ext) {
  		struct drm_v3d_extension ext;
 
+		if (ext_count++ >= V3D_MAX_EXTENSIONS) {
+			drm_dbg(&v3d->drm, "Too many V3D ioctl extensions\n");
+			return -E2BIG;
+		}
+
  		if (copy_from_user(&ext, user_ext, sizeof(ext))) {
  			drm_dbg(&v3d->drm, "Failed to copy submit extension\n");
  			return -EFAULT;
#4 lore raw

Hi Maíra,

Quoted 5 lines · Maíra Canal

How about checking if (!multisync.in_sync_count && !multisync.out_sync_count)? After all, it doesn't make any sense to have an empty multisync.

I believe this is better strategy than using a hard-coded max.

That check makes good semantic sense and directly closes the attack vector - I agree an empty multisync isn't useful.

That said, I'd like to raise one thought before sending v3. The reason xe and i915 added a general depth limit wasn't just to fix a specific known loop - it was to make the extension walker generically robust as the extension set grows. If a future extension is added that can also tolerate a second visit without erroring out, the unbounded walk vulnerability class reappears without a counter. Since the current architectural maximum is 2 (one multisync + one CPU job extension), capping the walk at 2 would reflect the real limit and provide that safety net without being arbitrary.

Happy to go either way - just wanted to raise it before sending v3. What do you think?

Best regards,
Ashutosh

maintainer
#5 lore raw

Hi Ashutosh,

Quoted 12 lines · Ashutosh Desai

Hi Maíra,

Quoted 5 lines · Maíra Canal

How about checking if (!multisync.in_sync_count && !multisync.out_sync_count)? After all, it doesn't make any sense to have an empty multisync.

I believe this is better strategy than using a hard-coded max.

That check makes good semantic sense and directly closes the attack vector - I agree an empty multisync isn't useful.

Quoted 9 lines

That said, I'd like to raise one thought before sending v3. The reason xe and i915 added a general depth limit wasn't just to fix a specific known loop - it was to make the extension walker generically robust as the extension set grows. If a future extension is added that can also tolerate a second visit without erroring out, the unbounded walk vulnerability class reappears without a counter. Since the current architectural maximum is 2 (one multisync + one CPU job extension), capping the walk at 2 would reflect the real limit and provide that safety net without being arbitrary.

The problem I see is maintainability. We will need to keep updating this macro as the number of extensions increase. So far, all the extensions we have can only be added once, so we can guarantee that per-extension.

If in the future we have an extension that can be added multiple times, it would make sense to add this hard-coded limit.

I'd prefer to check the multisync extension and make sure that a single one non-empty is added.

Best regards,
- Maíra

Quoted 6 lines

Happy to go either way - just wanted to raise it before sending v3. What do you think?

Best regards,
Ashutosh

#6 lore raw

Hi Maíra,

Quoted 6 lines · Maíra Canal

The problem I see is maintainability. We will need to keep updating this macro as the number of extensions increase. So far, all the extensions we have can only be added once, so we can guarantee that per-extension.

I'd prefer to check the multisync extension and make sure that a single one non-empty is added.

That is a great point - I had not considered the maintainability angle. Agreed, the per-extension guarantee is the right place to enforce this.

I have sent v3 with the empty-multisync check in
v3d_get_multisync_submit_deps() as you suggested.

Thanks for the guidance.

Best regards,
Ashutosh