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: [email protected]
Closes: 
https://sashiko.dev/#/patchset/20260727-drm_crtc_atomic_commit_leak-v1-1-23d9948a9...@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 <[email protected]>
---
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-7582b2444...@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-a5074aae0...@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-08429b920...@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 <[email protected]>

Reply via email to