Skip to contentSkip to conversation

drm/syncobj: Fix syncobj leak in drm_syncobj_eventfd_ioctl

PATCH In review T.J. Mercier 3 participants 5 messages Sep 9, 2024 – Sep 10, 2024

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

PATCH 1/1 drm/syncobj: Fix syncobj leak in drm_syncobj_eventfd_ioctl

A syncobj reference is taken in drm_syncobj_find, but not released if eventfd_ctx_fdget or kzalloc fails. Put the reference in these error paths.

Fixes c7a472297169 ("drm/syncobj: add IOCTL to register an eventfd")
---
 drivers/gpu/drm/drm_syncobj.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_syncobj.c +13 -4
index a0e94217b511..4fcfc0b9b386 100644
@@ -1464,6 +1464,7 @@ drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data,
 	struct drm_syncobj *syncobj;
 	struct eventfd_ctx *ev_fd_ctx;
 	struct syncobj_eventfd_entry *entry;
+	int ret;
 
 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
 		return -EOPNOTSUPP;
@@ -1479,13 +1480,15 @@ drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data,
 		return -ENOENT;
 
 	ev_fd_ctx = eventfd_ctx_fdget(args->fd);
-	if (IS_ERR(ev_fd_ctx))
-		return PTR_ERR(ev_fd_ctx);
+	if (IS_ERR(ev_fd_ctx)) {
+		ret = PTR_ERR(ev_fd_ctx);
+		goto err_fdget;
+	}
 
 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
 	if (!entry) {
-		eventfd_ctx_put(ev_fd_ctx);
-		return -ENOMEM;
+		ret = -ENOMEM;
+		goto err_kzalloc;
 	}
 	entry->syncobj = syncobj;
 	entry->ev_fd_ctx = ev_fd_ctx;
@@ -1496,6 +1499,12 @@ drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data,
 	drm_syncobj_put(syncobj);
 
 	return 0;
+
+err_kzalloc:
+	eventfd_ctx_put(ev_fd_ctx);
+err_fdget:
+	drm_syncobj_put(syncobj);
+	return ret;
 }
 
 int
Signature · 1 line
2.46.0.598.g6f2099f65c-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 46 lines · T.J. Mercier

A syncobj reference is taken in drm_syncobj_find, but not released if eventfd_ctx_fdget or kzalloc fails. Put the reference in these error paths.

Fixes c7a472297169 ("drm/syncobj: add IOCTL to register an eventfd")
---
  drivers/gpu/drm/drm_syncobj.c | 17 +++++++++++++----
  1 file changed, 13 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_syncobj.c +7 -4
index a0e94217b511..4fcfc0b9b386 100644
@@ -1464,6 +1464,7 @@ drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data,
  	struct drm_syncobj *syncobj;
  	struct eventfd_ctx *ev_fd_ctx;
  	struct syncobj_eventfd_entry *entry;
+	int ret;
 
  	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
  		return -EOPNOTSUPP;
@@ -1479,13 +1480,15 @@ drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data,
  		return -ENOENT;
 
  	ev_fd_ctx = eventfd_ctx_fdget(args->fd);
-	if (IS_ERR(ev_fd_ctx))
-		return PTR_ERR(ev_fd_ctx);
+	if (IS_ERR(ev_fd_ctx)) {
+		ret = PTR_ERR(ev_fd_ctx);
+		goto err_fdget;
+	}
 
  	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  	if (!entry) {
-		eventfd_ctx_put(ev_fd_ctx);
-		return -ENOMEM;
+		ret = -ENOMEM;
+		goto err_kzalloc;
  	}
  	entry->syncobj = syncobj;
  	entry->ev_fd_ctx = ev_fd_ctx;
@@ -1496,6 +1499,12 @@ drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data,
  	drm_syncobj_put(syncobj);
 

drivers/gpu/drm/drm_syncobj.c drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data, · line 1510

Quoted 10 lines
drivers/gpu/drm/drm_syncobj.c +6 -0
  	return 0;
+
+err_kzalloc:
+	eventfd_ctx_put(ev_fd_ctx);
+err_fdget:
+	drm_syncobj_put(syncobj);
+	return ret;
  }
 
  int

Easy enough to review while browsing the list:

Regards,

Tvrtko

#2 lore raw

Am 10.09.24 um 09:26 schrieb Tvrtko Ursulin:

Quoted 59 lines · T.J. Mercier
Quoted 53 lines

A syncobj reference is taken in drm_syncobj_find, but not released if eventfd_ctx_fdget or kzalloc fails. Put the reference in these error paths.

Fixes c7a472297169 ("drm/syncobj: add IOCTL to register an eventfd")

---
  drivers/gpu/drm/drm_syncobj.c | 17 +++++++++++++----

  1 file changed, 13 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_syncobj.c +13 -4
index a0e94217b511..4fcfc0b9b386 100644
@@ -1464,6 +1464,7 @@ drm_syncobj_eventfd_ioctl(struct drm_device 
 *dev, void *data,
       struct drm_syncobj *syncobj;
       struct eventfd_ctx *ev_fd_ctx;
       struct syncobj_eventfd_entry *entry;
+    int ret;
         if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
           return -EOPNOTSUPP;
@@ -1479,13 +1480,15 @@ drm_syncobj_eventfd_ioctl(struct drm_device 
 *dev, void *data,
           return -ENOENT;
         ev_fd_ctx = eventfd_ctx_fdget(args->fd);
-    if (IS_ERR(ev_fd_ctx))
-        return PTR_ERR(ev_fd_ctx);
+    if (IS_ERR(ev_fd_ctx)) {
+        ret = PTR_ERR(ev_fd_ctx);
+        goto err_fdget;
+    }
         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
       if (!entry) {
-        eventfd_ctx_put(ev_fd_ctx);
-        return -ENOMEM;
+        ret = -ENOMEM;
+        goto err_kzalloc;
       }
       entry->syncobj = syncobj;
       entry->ev_fd_ctx = ev_fd_ctx;
@@ -1496,6 +1499,12 @@ drm_syncobj_eventfd_ioctl(struct drm_device 
 *dev, void *data,
       drm_syncobj_put(syncobj);
         return 0;
+
+err_kzalloc:
+    eventfd_ctx_put(ev_fd_ctx);
+err_fdget:
+    drm_syncobj_put(syncobj);
+    return ret;
   }
Quoted 1 line

    int

Easy enough to review while browsing the list:

Quoted 1 line

Looks reasonable to me as well.

Reviewed-by. Christian König <[email protected]>
CC: stable?

Let me know when you need someone to push it to drm-misc-fixes.

Regards,
Christian.

Quoted 4 lines

Regards,

Tvrtko

#3 lore raw
Quoted 62 lines · Christian König

Am 10.09.24 um 09:26 schrieb Tvrtko Ursulin:

Quoted 60 lines · T.J. Mercier
Quoted 45 lines

A syncobj reference is taken in drm_syncobj_find, but not released if eventfd_ctx_fdget or kzalloc fails. Put the reference in these error paths.

Fixes c7a472297169 ("drm/syncobj: add IOCTL to register an eventfd")
---
  drivers/gpu/drm/drm_syncobj.c | 17 +++++++++++++----
  1 file changed, 13 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_syncobj.c +7 -4
index a0e94217b511..4fcfc0b9b386 100644
@@ -1464,6 +1464,7 @@ drm_syncobj_eventfd_ioctl(struct drm_device
 *dev, void *data,
      struct drm_syncobj *syncobj;
      struct eventfd_ctx *ev_fd_ctx;
      struct syncobj_eventfd_entry *entry;
+    int ret;
        if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
          return -EOPNOTSUPP;
@@ -1479,13 +1480,15 @@ drm_syncobj_eventfd_ioctl(struct drm_device
 *dev, void *data,
          return -ENOENT;
        ev_fd_ctx = eventfd_ctx_fdget(args->fd);
-    if (IS_ERR(ev_fd_ctx))
-        return PTR_ERR(ev_fd_ctx);
+    if (IS_ERR(ev_fd_ctx)) {
+        ret = PTR_ERR(ev_fd_ctx);
+        goto err_fdget;
+    }
        entry = kzalloc(sizeof(*entry), GFP_KERNEL);
      if (!entry) {
-        eventfd_ctx_put(ev_fd_ctx);
-        return -ENOMEM;
+        ret = -ENOMEM;
+        goto err_kzalloc;
      }
      entry->syncobj = syncobj;
      entry->ev_fd_ctx = ev_fd_ctx;
@@ -1496,6 +1499,12 @@ drm_syncobj_eventfd_ioctl(struct drm_device
 *dev, void *data,

drivers/gpu/drm/drm_syncobj.c drm_syncobj_eventfd_ioctl(struct drm_device · line 1509

Quoted 10 lines
drivers/gpu/drm/drm_syncobj.c +6 -0
      drm_syncobj_put(syncobj);
        return 0;
+
+err_kzalloc:
+    eventfd_ctx_put(ev_fd_ctx);
+err_fdget:
+    drm_syncobj_put(syncobj);
+    return ret;
  }
    int

Easy enough to review while browsing the list:

Quoted 5 lines
Quoted 1 line

Looks reasonable to me as well.

Reviewed-by. Christian König <[email protected]>

Thanks!

Quoted 1 line
CC stable?

Yes, I think we should. 6.6 and 6.10

Quoted 1 line

Let me know when you need someone to push it to drm-misc-fixes.

Anytime is good, no rush for this one.

Quoted 9 lines

Regards,
Christian.

Quoted 4 lines

Regards,

Tvrtko

#4 lore raw

Am 10.09.24 um 16:47 schrieb T.J. Mercier:

Quoted 64 lines
Quoted 62 lines · Christian König

Am 10.09.24 um 09:26 schrieb Tvrtko Ursulin:

Quoted 58 lines · T.J. Mercier
Quoted 45 lines

A syncobj reference is taken in drm_syncobj_find, but not released if eventfd_ctx_fdget or kzalloc fails. Put the reference in these error paths.

Fixes c7a472297169 ("drm/syncobj: add IOCTL to register an eventfd")
---
   drivers/gpu/drm/drm_syncobj.c | 17 +++++++++++++----
   1 file changed, 13 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_syncobj.c +7 -4
index a0e94217b511..4fcfc0b9b386 100644
@@ -1464,6 +1464,7 @@ drm_syncobj_eventfd_ioctl(struct drm_device
 *dev, void *data,
       struct drm_syncobj *syncobj;
       struct eventfd_ctx *ev_fd_ctx;
       struct syncobj_eventfd_entry *entry;
+    int ret;
         if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
           return -EOPNOTSUPP;
@@ -1479,13 +1480,15 @@ drm_syncobj_eventfd_ioctl(struct drm_device
 *dev, void *data,
           return -ENOENT;
         ev_fd_ctx = eventfd_ctx_fdget(args->fd);
-    if (IS_ERR(ev_fd_ctx))
-        return PTR_ERR(ev_fd_ctx);
+    if (IS_ERR(ev_fd_ctx)) {
+        ret = PTR_ERR(ev_fd_ctx);
+        goto err_fdget;
+    }
         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
       if (!entry) {
-        eventfd_ctx_put(ev_fd_ctx);
-        return -ENOMEM;
+        ret = -ENOMEM;
+        goto err_kzalloc;
       }
       entry->syncobj = syncobj;
       entry->ev_fd_ctx = ev_fd_ctx;
@@ -1496,6 +1499,12 @@ drm_syncobj_eventfd_ioctl(struct drm_device
 *dev, void *data,

drivers/gpu/drm/drm_syncobj.c drm_syncobj_eventfd_ioctl(struct drm_device · line 1509

Quoted 10 lines
drivers/gpu/drm/drm_syncobj.c +6 -0
       drm_syncobj_put(syncobj);
         return 0;
+
+err_kzalloc:
+    eventfd_ctx_put(ev_fd_ctx);
+err_fdget:
+    drm_syncobj_put(syncobj);
+    return ret;
   }
     int

Easy enough to review while browsing the list:

Quoted 1 line

Looks reasonable to me as well.

Quoted 8 lines
Quoted 1 line

Reviewed-by. Christian König <[email protected]>

Thanks!

Quoted 1 line
CC stable?

Yes, I think we should. 6.6 and 6.10

Quoted 1 line

Let me know when you need someone to push it to drm-misc-fixes.

Anytime is good, no rush for this one.

Done.

Christian.

Quoted 7 lines
Quoted 7 lines

Regards,
Christian.

Quoted 3 lines

Regards,

Tvrtko