mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] drm/v3d: Fix double free of query arrays when extension parsing fails
@ 2026-11-10  6:13 Wang Xiaolei
  2026-11-16  6:28 ` 王孝雷
  2026-11-18 13:21 ` Maíra Canal
  0 siblings, 2 replies; 3+ messages in thread
From: Wang Xiaolei @ 2026-11-10  6:13 UTC (permalink / raw)
  To: mwen, mcanal, maarten.lankhorst, mripard, tzimmermann, airlied,
	simona, tvrtko.ursulin, wangxiaolei01
  Cc: dri-devel, linux-kernel, stable, Iago Toral Quiroga

v3d_get_cpu_timestamp_query_params() and v3d_copy_query_info() - the
latter used by both performance query parsers - free the query array on
their error paths through v3d_timestamp_query_info_free() /
v3d_performance_query_info_free(), but leave query_info->queries
pointing at the freed buffer.

The same job is then released by v3d_submit_cpu_ioctl()'s failure path
(v3d_submit_cleanup_jobs() -> v3d_job_cleanup() -> v3d_job_put() ->
v3d_cpu_job_free()), which calls the same helpers on the same
query_info. On these error paths query_info->count was never assigned -
it is only written after the parsing loop completes - so the syncobj
loop runs zero times and no syncobj is put twice, but the
"if (query_info->queries)" test still succeeds and the dangling pointer
is handed to kvfree() a second time.

Every failure inside the parsing loop takes this path: -ENOENT from
drm_syncobj_find(), -EFAULT from get_user(), or -ENOMEM from
kvmalloc_array() in v3d_copy_query_info(). The helpers are shared
between that error path and the job release path.

Clear the pointer once the buffer has been freed.

Fixes: 0e50fcc20bd8 ("drm/v3d: Fix potential memory leak in the timestamp extension")
Fixes: 32df4abc44f2 ("drm/v3d: Fix potential memory leak in the performance extension")
Cc: stable@vger.kernel.org
Cc: Maíra Canal <mcanal@igalia.com>
Cc: Iago Toral Quiroga <itoral@igalia.com>
Cc: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>
Signed-off-by: Wang Xiaolei <wangxiaolei01@kaihong.com>
---
 drivers/gpu/drm/v3d/v3d_sched.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/gpu/drm/v3d/v3d_sched.c b/drivers/gpu/drm/v3d/v3d_sched.c
index 1924e3440dc9..a96023b5dce0 100644
--- a/drivers/gpu/drm/v3d/v3d_sched.c
+++ b/drivers/gpu/drm/v3d/v3d_sched.c
@@ -106,6 +106,7 @@ v3d_timestamp_query_info_free(struct v3d_timestamp_query_info *query_info,
 			drm_syncobj_put(query_info->queries[i].syncobj);
 
 		kvfree(query_info->queries);
+		query_info->queries = NULL;
 	}
 }
 
@@ -122,6 +123,7 @@ v3d_performance_query_info_free(struct v3d_performance_query_info *query_info,
 		}
 
 		kvfree(query_info->queries);
+		query_info->queries = NULL;
 	}
 }
 
-- 
2.34.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/v3d: Fix double free of query arrays when extension parsing fails
  2026-11-10  6:13 [PATCH] drm/v3d: Fix double free of query arrays when extension parsing fails Wang Xiaolei
@ 2026-11-16  6:28 ` 王孝雷
  2026-11-18 13:21 ` Maíra Canal
  1 sibling, 0 replies; 3+ messages in thread
From: 王孝雷 @ 2026-11-16  6:28 UTC (permalink / raw)
  To: mwen, mcanal, maarten.lankhorst, mripard, tzimmermann, airlied,
	simona, tvrtko.ursulin
  Cc: dri-devel, linux-kernel, stable, Iago Toral Quiroga

Humble ping...
Thanks
Xiaolei
> From: "Wang Xiaolei"<wangxiaolei01@kaihong.com>
> Date:  Tue, Nov 10, 2026, 14:14
> Subject:  [PATCH] drm/v3d: Fix double free of query arrays when extension parsing fails
> To: <mwen@igalia.com>, <mcanal@igalia.com>, <maarten.lankhorst@linux.intel.com>, <mripard@kernel.org>, <tzimmermann@suse.de>, <airlied@gmail.com>, <simona@ffwll.ch>, <tvrtko.ursulin@igalia.com>, <wangxiaolei01@kaihong.com>
> Cc: <dri-devel@lists.freedesktop.org>, <linux-kernel@vger.kernel.org>, <stable@vger.kernel.org>, "Iago Toral Quiroga"<itoral@igalia.com>
> v3d_get_cpu_timestamp_query_params() and v3d_copy_query_info() - the
> latter used by both performance query parsers - free the query array on
> their error paths through v3d_timestamp_query_info_free() /
> v3d_performance_query_info_free(), but leave query_info->queries
> pointing at the freed buffer.
> 
> The same job is then released by v3d_submit_cpu_ioctl()'s failure path
> (v3d_submit_cleanup_jobs() -> v3d_job_cleanup() -> v3d_job_put() ->
> v3d_cpu_job_free()), which calls the same helpers on the same
> query_info. On these error paths query_info->count was never assigned -
> it is only written after the parsing loop completes - so the syncobj
> loop runs zero times and no syncobj is put twice, but the
> "if (query_info->queries)" test still succeeds and the dangling pointer
> is handed to kvfree() a second time.
> 
> Every failure inside the parsing loop takes this path: -ENOENT from
> drm_syncobj_find(), -EFAULT from get_user(), or -ENOMEM from
> kvmalloc_array() in v3d_copy_query_info(). The helpers are shared
> between that error path and the job release path.
> 
> Clear the pointer once the buffer has been freed.
> 
> Fixes: 0e50fcc20bd8 ("drm/v3d: Fix potential memory leak in the timestamp extension")
> Fixes: 32df4abc44f2 ("drm/v3d: Fix potential memory leak in the performance extension")
> Cc: stable@vger.kernel.org
> Cc: Maíra Canal <mcanal@igalia.com>
> Cc: Iago Toral Quiroga <itoral@igalia.com>
> Cc: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>
> Signed-off-by: Wang Xiaolei <wangxiaolei01@kaihong.com>
> ---
>  drivers/gpu/drm/v3d/v3d_sched.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/gpu/drm/v3d/v3d_sched.c b/drivers/gpu/drm/v3d/v3d_sched.c
> index 1924e3440dc9..a96023b5dce0 100644
> --- a/drivers/gpu/drm/v3d/v3d_sched.c
> +++ b/drivers/gpu/drm/v3d/v3d_sched.c
> @@ -106,6 +106,7 @@ v3d_timestamp_query_info_free(struct v3d_timestamp_query_info *query_info,
>                          drm_syncobj_put(query_info->queries[i].syncobj);
>  
>                  kvfree(query_info->queries);
> +                query_info->queries = NULL;
>          }
>  }
>  
> @@ -122,6 +123,7 @@ v3d_performance_query_info_free(struct v3d_performance_query_info *query_info,
>                  }
>  
>                  kvfree(query_info->queries);
> +                query_info->queries = NULL;
>          }
>  }
>  
> -- 
> 2.34.1
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/v3d: Fix double free of query arrays when extension parsing fails
  2026-11-10  6:13 [PATCH] drm/v3d: Fix double free of query arrays when extension parsing fails Wang Xiaolei
  2026-11-16  6:28 ` 王孝雷
@ 2026-11-18 13:21 ` Maíra Canal
  1 sibling, 0 replies; 3+ messages in thread
From: Maíra Canal @ 2026-11-18 13:21 UTC (permalink / raw)
  To: Wang Xiaolei, mwen, maarten.lankhorst, mripard, tzimmermann,
	airlied, simona, tvrtko.ursulin
  Cc: dri-devel, linux-kernel, stable, Iago Toral Quiroga

Hi Wang,

Please, be patient with kernel maintainers. Kernel etiquette recommends
waiting at least a week before pinging maintainers [1].

On 10/11/26 03:13, Wang Xiaolei wrote:
> v3d_get_cpu_timestamp_query_params() and v3d_copy_query_info() - the
> latter used by both performance query parsers - free the query array on
> their error paths through v3d_timestamp_query_info_free() /
> v3d_performance_query_info_free(), but leave query_info->queries
> pointing at the freed buffer.
> 
> The same job is then released by v3d_submit_cpu_ioctl()'s failure path
> (v3d_submit_cleanup_jobs() -> v3d_job_cleanup() -> v3d_job_put() ->
> v3d_cpu_job_free()), which calls the same helpers on the same
> query_info. On these error paths query_info->count was never assigned -
> it is only written after the parsing loop completes - so the syncobj
> loop runs zero times and no syncobj is put twice, but the
> "if (query_info->queries)" test still succeeds and the dangling pointer
> is handed to kvfree() a second time.
> 
> Every failure inside the parsing loop takes this path: -ENOENT from
> drm_syncobj_find(), -EFAULT from get_user(), or -ENOMEM from
> kvmalloc_array() in v3d_copy_query_info(). The helpers are shared
> between that error path and the job release path.
> 
> Clear the pointer once the buffer has been freed.

This is indeed a bug, but the solution doesn't fix the ownership problem
here. We should actually leave the cleaning process to
v3d_cpu_job_free() instead of cleaning it in the parser. I wrote this
draft of what I was thinking on:

diff --git a/drivers/gpu/drm/v3d/v3d_sched.c 
b/drivers/gpu/drm/v3d/v3d_sched.c
index f29d79b15be3..15bec1e35e39 100644
--- a/drivers/gpu/drm/v3d/v3d_sched.c
+++ b/drivers/gpu/drm/v3d/v3d_sched.c
@@ -96,17 +96,18 @@ v3d_sched_job_free(struct drm_sched_job *sched_job)
  }

  void
-v3d_timestamp_query_info_free(struct v3d_timestamp_query_info *query_info,
-                             unsigned int count)
+v3d_timestamp_query_info_free(struct v3d_timestamp_query_info *query_info)
  {
-       if (query_info->queries) {
-               unsigned int i;
+       unsigned int i;

-               for (i = 0; i < count; i++)
-                       drm_syncobj_put(query_info->queries[i].syncobj);
+       for (i = 0; i < query_info->count; i++) {
+               if (!query_info->queries[i].syncobj)
+                       break;

-               kvfree(query_info->queries);
+               drm_syncobj_put(query_info->queries[i].syncobj);
         }
+
+       kvfree(query_info->queries);
  }

  void
diff --git a/drivers/gpu/drm/v3d/v3d_submit.c 
b/drivers/gpu/drm/v3d/v3d_submit.c
index eada30a31701..a3e92e097423 100644
--- a/drivers/gpu/drm/v3d/v3d_submit.c
+++ b/drivers/gpu/drm/v3d/v3d_submit.c
@@ -734,7 +734,6 @@ v3d_get_cpu_timestamp_query_params(struct drm_file 
*file_priv,
         struct drm_v3d_timestamp_query timestamp;
         struct v3d_timestamp_query_info *query_info = 
&job->timestamp_query;
         unsigned int i;
-       int err;

         if (!v3d_validate_cpu_job(file_priv, job))
                 return -EINVAL;
@@ -747,7 +746,7 @@ v3d_get_cpu_timestamp_query_params(struct drm_file 
*file_priv,

         job->job_type = V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY;

-       query_info->queries = kvmalloc_objs(struct v3d_timestamp_query,
+       query_info->queries = kvzalloc_objs(struct v3d_timestamp_query,
                                             timestamp.count);
         if (!query_info->queries)
                 return -ENOMEM;
@@ -758,32 +757,22 @@ v3d_get_cpu_timestamp_query_params(struct drm_file 
*file_priv,
         for (i = 0; i < timestamp.count; i++) {
                 u32 offset, sync;

-               if (get_user(offset, offsets++)) {
-                       err = -EFAULT;
-                       goto error;
-               }
+               if (get_user(offset, offsets++))
+                       return -EFAULT;

                 query_info->queries[i].offset = offset;

-               if (get_user(sync, syncs++)) {
-                       err = -EFAULT;
-                       goto error;
-               }
+               if (get_user(sync, syncs++))
+                       return -EFAULT;

                 query_info->queries[i].syncobj = 
drm_syncobj_find(file_priv,
                                                                   sync);
-               if (!query_info->queries[i].syncobj) {
-                       err = -ENOENT;
-                       goto error;
-               }
+               if (!query_info->queries[i].syncobj)
+                       return -ENOENT;
         }
         query_info->count = timestamp.count;

         return 0;
-
-error:
-       v3d_timestamp_query_info_free(&job->timestamp_query, i);
-       return err;
  }

It doesn't even compile, it's just a draft to give you a more concrete
example of what I'm thinking on.

[1] 
https://docs.kernel.org/process/submitting-patches.html#don-t-get-discouraged-or-impatient

Best regards,
- Maíra

> 
> Fixes: 0e50fcc20bd8 ("drm/v3d: Fix potential memory leak in the timestamp extension")
> Fixes: 32df4abc44f2 ("drm/v3d: Fix potential memory leak in the performance extension")
> Cc: stable@vger.kernel.org
> Cc: Maíra Canal <mcanal@igalia.com>
> Cc: Iago Toral Quiroga <itoral@igalia.com>
> Cc: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>
> Signed-off-by: Wang Xiaolei <wangxiaolei01@kaihong.com>
> ---
>   drivers/gpu/drm/v3d/v3d_sched.c | 2 ++
>   1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/gpu/drm/v3d/v3d_sched.c b/drivers/gpu/drm/v3d/v3d_sched.c
> index 1924e3440dc9..a96023b5dce0 100644
> --- a/drivers/gpu/drm/v3d/v3d_sched.c
> +++ b/drivers/gpu/drm/v3d/v3d_sched.c
> @@ -106,6 +106,7 @@ v3d_timestamp_query_info_free(struct v3d_timestamp_query_info *query_info,
>   			drm_syncobj_put(query_info->queries[i].syncobj);
>   
>   		kvfree(query_info->queries);
> +		query_info->queries = NULL;
>   	}
>   }
>   
> @@ -122,6 +123,7 @@ v3d_performance_query_info_free(struct v3d_performance_query_info *query_info,
>   		}
>   
>   		kvfree(query_info->queries);
> +		query_info->queries = NULL;
>   	}
>   }
>   


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-18 13:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-10  6:13 [PATCH] drm/v3d: Fix double free of query arrays when extension parsing fails Wang Xiaolei
2026-11-16  6:28 ` 王孝雷
2026-11-18 13:21 ` Maíra Canal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®