mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4] drm: Fix drm_pending_vblank_event leak in error path for out_fence_ptr
@ 2026-08-26 11:46 Thadeu Lima de Souza Cascardo
  2026-09-11 13:58 ` Melissa Wen
  2026-09-11 14:43 ` Markus Elfring
  0 siblings, 2 replies; 4+ messages in thread
From: Thadeu Lima de Souza Cascardo @ 2026-08-26 11:46 UTC (permalink / raw)
  To: Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	David Airlie, Simona Vetter
  Cc: dri-devel, linux-kernel, Melissa Wen, kernel-dev, sashiko-bot,
	Thadeu Lima de Souza Cascardo

When an out_fence_ptr is provided but DRM_MODE_PAGE_FLIP_EVENT is not
set, a drm_pending_vblank_event will be allocated. If later, there is an
allocation failure or another failure at setup_out_fence(), that event
will not have base.fence set and it will not be released at
complete_signaling().

Release the event and set crtc_state->event to NULL just like in the
DRM_MODE_PAGE_FLIP_EVENT case when there is a failure at
drm_event_reserve_init(). That is, prepare_signaling() releases the
event and there is nothing to be done at complete_signaling(). Use
drm_event_cancel_free() as that will also undo drm_event_reserve_init()
in case it has been called.

Reported-by: sashiko-bot@kernel.org
Closes: https://sashiko.dev/#/patchset/20260727-drm_crtc_atomic_commit_leak-v1-1-23d9948a9d7c@igalia.com?part=1
Fixes: 92c715fca907 ("drm/atomic: Fix double free in drm_atomic_state_default_clear")
Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
---
Changes in v4:
- Rename label to err_free_event.
- Use crtc_state->event instead of a local variable e.
- Link to v3: https://patch.msgid.link/20260817-drm_pending_vblank_event_leak-v3-1-7582b24447d0@igalia.com

Changes in v3:
- Use a label for the common exit pattern.
- Link to v2: https://patch.msgid.link/20260729-drm_pending_vblank_event_leak-v2-1-a5074aae07df@igalia.com

Changes in v2:
- Fix UAF when DRM_MODE_PAGE_FLIP_EVENT is used.
- Link to v1: https://patch.msgid.link/20260728-drm_pending_vblank_event_leak-v1-1-08429b920b16@igalia.com
---
 drivers/gpu/drm/drm_atomic_uapi.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/drm_atomic_uapi.c b/drivers/gpu/drm/drm_atomic_uapi.c
index ae7667d1072d..8c0eea2fed4a 100644
--- a/drivers/gpu/drm/drm_atomic_uapi.c
+++ b/drivers/gpu/drm/drm_atomic_uapi.c
@@ -1458,10 +1458,12 @@ static int prepare_signaling(struct drm_device *dev,
 			struct dma_fence *fence;
 			struct drm_out_fence_state *f;
 
+			ret = -ENOMEM;
+
 			f = krealloc(*fence_state, sizeof(**fence_state) *
 				     (*num_fences + 1), GFP_KERNEL);
 			if (!f)
-				return -ENOMEM;
+				goto err_free_event;
 
 			memset(&f[*num_fences], 0, sizeof(*f));
 
@@ -1470,12 +1472,12 @@ static int prepare_signaling(struct drm_device *dev,
 
 			fence = drm_crtc_create_fence(crtc);
 			if (!fence)
-				return -ENOMEM;
+				goto err_free_event;
 
 			ret = setup_out_fence(&f[(*num_fences)++], fence);
 			if (ret) {
 				dma_fence_put(fence);
-				return ret;
+				goto err_free_event;
 			}
 
 			crtc_state->event->base.fence = fence;
@@ -1531,6 +1533,11 @@ static int prepare_signaling(struct drm_device *dev,
 	}
 
 	return 0;
+
+err_free_event:
+	drm_event_cancel_free(dev, &crtc_state->event->base);
+	crtc_state->event = NULL;
+	return ret;
 }
 
 static void complete_signaling(struct drm_device *dev,

---
base-commit: 4d4be202165e832d74849b4a68e289a2a377039c
change-id: 20260728-drm_pending_vblank_event_leak-a36cedb296ba

Best regards,
--  
Thadeu Lima de Souza Cascardo <cascardo@igalia.com>


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

* Re: [PATCH v4] drm: Fix drm_pending_vblank_event leak in error path for out_fence_ptr
  2026-08-26 11:46 [PATCH v4] drm: Fix drm_pending_vblank_event leak in error path for out_fence_ptr Thadeu Lima de Souza Cascardo
@ 2026-09-11 13:58 ` Melissa Wen
  2026-09-11 14:26   ` Melissa Wen
  2026-09-11 14:43 ` Markus Elfring
  1 sibling, 1 reply; 4+ messages in thread
From: Melissa Wen @ 2026-09-11 13:58 UTC (permalink / raw)
  To: Thadeu Lima de Souza Cascardo, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, David Airlie, Simona Vetter
  Cc: dri-devel, linux-kernel, kernel-dev, sashiko-bot



On 26/08/2026 13:46, Thadeu Lima de Souza Cascardo wrote:
> When an out_fence_ptr is provided but DRM_MODE_PAGE_FLIP_EVENT is not
> set, a drm_pending_vblank_event will be allocated. If later, there is an
> allocation failure or another failure at setup_out_fence(), that event
> will not have base.fence set and it will not be released at
> complete_signaling().
>
> Release the event and set crtc_state->event to NULL just like in the
> DRM_MODE_PAGE_FLIP_EVENT case when there is a failure at
> drm_event_reserve_init(). That is, prepare_signaling() releases the
> event and there is nothing to be done at complete_signaling(). Use
> drm_event_cancel_free() as that will also undo drm_event_reserve_init()
> in case it has been called.

LGTM. Thanks!

Reviewed-by: Melissa Wen <mwen@igalia.com>

>
> Reported-by: sashiko-bot@kernel.org
> Closes: https://sashiko.dev/#/patchset/20260727-drm_crtc_atomic_commit_leak-v1-1-23d9948a9d7c@igalia.com?part=1
> Fixes: 92c715fca907 ("drm/atomic: Fix double free in drm_atomic_state_default_clear")
> Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
> ---
> Changes in v4:
> - Rename label to err_free_event.
> - Use crtc_state->event instead of a local variable e.
> - Link to v3: https://patch.msgid.link/20260817-drm_pending_vblank_event_leak-v3-1-7582b24447d0@igalia.com
>
> Changes in v3:
> - Use a label for the common exit pattern.
> - Link to v2: https://patch.msgid.link/20260729-drm_pending_vblank_event_leak-v2-1-a5074aae07df@igalia.com
>
> Changes in v2:
> - Fix UAF when DRM_MODE_PAGE_FLIP_EVENT is used.
> - Link to v1: https://patch.msgid.link/20260728-drm_pending_vblank_event_leak-v1-1-08429b920b16@igalia.com
> ---
>   drivers/gpu/drm/drm_atomic_uapi.c | 13 ++++++++++---
>   1 file changed, 10 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpu/drm/drm_atomic_uapi.c b/drivers/gpu/drm/drm_atomic_uapi.c
> index ae7667d1072d..8c0eea2fed4a 100644
> --- a/drivers/gpu/drm/drm_atomic_uapi.c
> +++ b/drivers/gpu/drm/drm_atomic_uapi.c
> @@ -1458,10 +1458,12 @@ static int prepare_signaling(struct drm_device *dev,
>   			struct dma_fence *fence;
>   			struct drm_out_fence_state *f;
>   
> +			ret = -ENOMEM;
> +
>   			f = krealloc(*fence_state, sizeof(**fence_state) *
>   				     (*num_fences + 1), GFP_KERNEL);
>   			if (!f)
> -				return -ENOMEM;
> +				goto err_free_event;
>   
>   			memset(&f[*num_fences], 0, sizeof(*f));
>   
> @@ -1470,12 +1472,12 @@ static int prepare_signaling(struct drm_device *dev,
>   
>   			fence = drm_crtc_create_fence(crtc);
>   			if (!fence)
> -				return -ENOMEM;
> +				goto err_free_event;
>   
>   			ret = setup_out_fence(&f[(*num_fences)++], fence);
>   			if (ret) {
>   				dma_fence_put(fence);
> -				return ret;
> +				goto err_free_event;
>   			}
>   
>   			crtc_state->event->base.fence = fence;
> @@ -1531,6 +1533,11 @@ static int prepare_signaling(struct drm_device *dev,
>   	}
>   
>   	return 0;
> +
> +err_free_event:
> +	drm_event_cancel_free(dev, &crtc_state->event->base);
> +	crtc_state->event = NULL;
> +	return ret;
>   }
>   
>   static void complete_signaling(struct drm_device *dev,
>
> ---
> base-commit: 4d4be202165e832d74849b4a68e289a2a377039c
> change-id: 20260728-drm_pending_vblank_event_leak-a36cedb296ba
>
> Best regards,
> --
> Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
>


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

* Re: [PATCH v4] drm: Fix drm_pending_vblank_event leak in error path for out_fence_ptr
  2026-09-11 13:58 ` Melissa Wen
@ 2026-09-11 14:26   ` Melissa Wen
  0 siblings, 0 replies; 4+ messages in thread
From: Melissa Wen @ 2026-09-11 14:26 UTC (permalink / raw)
  To: Thadeu Lima de Souza Cascardo, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, David Airlie, Simona Vetter
  Cc: dri-devel, linux-kernel, kernel-dev, sashiko-bot



On 11/09/2026 15:58, Melissa Wen wrote:
>
>
> On 26/08/2026 13:46, Thadeu Lima de Souza Cascardo wrote:
>> When an out_fence_ptr is provided but DRM_MODE_PAGE_FLIP_EVENT is not
>> set, a drm_pending_vblank_event will be allocated. If later, there is an
>> allocation failure or another failure at setup_out_fence(), that event
>> will not have base.fence set and it will not be released at
>> complete_signaling().
>>
>> Release the event and set crtc_state->event to NULL just like in the
>> DRM_MODE_PAGE_FLIP_EVENT case when there is a failure at
>> drm_event_reserve_init(). That is, prepare_signaling() releases the
>> event and there is nothing to be done at complete_signaling(). Use
>> drm_event_cancel_free() as that will also undo drm_event_reserve_init()
>> in case it has been called.
>
> LGTM. Thanks!
>
> Reviewed-by: Melissa Wen <mwen@igalia.com>

Applied to drm-misc-fixes:
9eb1a393c89a ("drm: Fix drm_pending_vblank_event leak in error path for 
out_fence_ptr")

>
>>
>> Reported-by: sashiko-bot@kernel.org
>> Closes: 
>> https://sashiko.dev/#/patchset/20260727-drm_crtc_atomic_commit_leak-v1-1-23d9948a9d7c@igalia.com?part=1
>> Fixes: 92c715fca907 ("drm/atomic: Fix double free in 
>> drm_atomic_state_default_clear")
>> Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
>> ---
>> Changes in v4:
>> - Rename label to err_free_event.
>> - Use crtc_state->event instead of a local variable e.
>> - Link to v3: 
>> https://patch.msgid.link/20260817-drm_pending_vblank_event_leak-v3-1-7582b24447d0@igalia.com
>>
>> Changes in v3:
>> - Use a label for the common exit pattern.
>> - Link to v2: 
>> https://patch.msgid.link/20260729-drm_pending_vblank_event_leak-v2-1-a5074aae07df@igalia.com
>>
>> Changes in v2:
>> - Fix UAF when DRM_MODE_PAGE_FLIP_EVENT is used.
>> - Link to v1: 
>> https://patch.msgid.link/20260728-drm_pending_vblank_event_leak-v1-1-08429b920b16@igalia.com
>> ---
>>   drivers/gpu/drm/drm_atomic_uapi.c | 13 ++++++++++---
>>   1 file changed, 10 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/drm_atomic_uapi.c 
>> b/drivers/gpu/drm/drm_atomic_uapi.c
>> index ae7667d1072d..8c0eea2fed4a 100644
>> --- a/drivers/gpu/drm/drm_atomic_uapi.c
>> +++ b/drivers/gpu/drm/drm_atomic_uapi.c
>> @@ -1458,10 +1458,12 @@ static int prepare_signaling(struct 
>> drm_device *dev,
>>               struct dma_fence *fence;
>>               struct drm_out_fence_state *f;
>>   +            ret = -ENOMEM;
>> +
>>               f = krealloc(*fence_state, sizeof(**fence_state) *
>>                        (*num_fences + 1), GFP_KERNEL);
>>               if (!f)
>> -                return -ENOMEM;
>> +                goto err_free_event;
>>                 memset(&f[*num_fences], 0, sizeof(*f));
>>   @@ -1470,12 +1472,12 @@ static int prepare_signaling(struct 
>> drm_device *dev,
>>                 fence = drm_crtc_create_fence(crtc);
>>               if (!fence)
>> -                return -ENOMEM;
>> +                goto err_free_event;
>>                 ret = setup_out_fence(&f[(*num_fences)++], fence);
>>               if (ret) {
>>                   dma_fence_put(fence);
>> -                return ret;
>> +                goto err_free_event;
>>               }
>>                 crtc_state->event->base.fence = fence;
>> @@ -1531,6 +1533,11 @@ static int prepare_signaling(struct drm_device 
>> *dev,
>>       }
>>         return 0;
>> +
>> +err_free_event:
>> +    drm_event_cancel_free(dev, &crtc_state->event->base);
>> +    crtc_state->event = NULL;
>> +    return ret;
>>   }
>>     static void complete_signaling(struct drm_device *dev,
>>
>> ---
>> base-commit: 4d4be202165e832d74849b4a68e289a2a377039c
>> change-id: 20260728-drm_pending_vblank_event_leak-a36cedb296ba
>>
>> Best regards,
>> -- 
>> Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
>>
>


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

* Re: [PATCH v4] drm: Fix drm_pending_vblank_event leak in error path for out_fence_ptr
  2026-08-26 11:46 [PATCH v4] drm: Fix drm_pending_vblank_event leak in error path for out_fence_ptr Thadeu Lima de Souza Cascardo
  2026-09-11 13:58 ` Melissa Wen
@ 2026-09-11 14:43 ` Markus Elfring
  1 sibling, 0 replies; 4+ messages in thread
From: Markus Elfring @ 2026-09-11 14:43 UTC (permalink / raw)
  To: Thadeu Lima de Souza Cascardo, dri-devel, kernel-dev,
	David Airlie, Maarten Lankhorst, Maxime Ripard, Melissa Wen,
	Simona Vetter, Thomas Zimmermann
  Cc: sashiko-bot, LKML, kernel-janitors

> +++ b/drivers/gpu/drm/drm_atomic_uapi.c
> @@ -1458,10 +1458,12 @@ static int prepare_signaling(struct drm_device *dev,
>  			struct dma_fence *fence;
>  			struct drm_out_fence_state *f;
>  
> +			ret = -ENOMEM;
> +

How do you think about to refine the source code a bit more?

I imagine that this assignment statement can be moved behind an additional label
like e_nomem.


>  			f = krealloc(*fence_state, sizeof(**fence_state) *
>  				     (*num_fences + 1), GFP_KERNEL);
>  			if (!f)
> -				return -ENOMEM;
> +				goto err_free_event;
>  
>  			memset(&f[*num_fences], 0, sizeof(*f));
>  
> @@ -1531,6 +1533,11 @@ static int prepare_signaling(struct drm_device *dev,
>  	}
>  
>  	return 0;
> +
> +err_free_event:
> +	drm_event_cancel_free(dev, &crtc_state->event->base);

+err_reset_event:

Would you like to avoid a bit of duplicate source code from an other if branch?
https://elixir.bootlin.com/linux/v7.3-rc1/source/drivers/gpu/drm/drm_atomic_uapi.c#L1457-L1461

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v7.3-rc2#n572


> +	crtc_state->event = NULL;
> +	return ret;
>  }


Regards,
Markus

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

end of thread, other threads:[~2026-09-11 14:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-26 11:46 [PATCH v4] drm: Fix drm_pending_vblank_event leak in error path for out_fence_ptr Thadeu Lima de Souza Cascardo
2026-09-11 13:58 ` Melissa Wen
2026-09-11 14:26   ` Melissa Wen
2026-09-11 14:43 ` Markus Elfring

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®