Skip to contentSkip to conversation

drm/gem: Fix a leak in drm_gem_objects_lookup()

PATCH In review Dan Carpenter 2 participants 4 messages Mar 20, 2020 – May 17, 2020

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

PATCH 1/1 drm/gem: Fix a leak in drm_gem_objects_lookup()

If the "handles" allocation or the copy_from_user() fails then we leak "objs". It's supposed to be freed in panfrost_job_cleanup().

Fixes c117aa4d8701 ("drm: Add a drm_gem_objects_lookup helper")
---
 drivers/gpu/drm/drm_gem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_gem.c +2 -2
index a9e4a610445a..f28724f2eb69 100644
@@ -710,6 +710,8 @@ int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
 	if (!objs)
 		return -ENOMEM;
 
+	*objs_out = objs;
+
 	handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL);
 	if (!handles) {
 		ret = -ENOMEM;
@@ -723,8 +725,6 @@ int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
 	}
 
 	ret = objects_lookup(filp, handles, count, objs);
-	*objs_out = objs;
-
 out:
 	kvfree(handles);
 	return ret;
Signature · 1 line
2.25.1
#1 lore raw

Hi Dan,

Quoted 24 lines · Dan Carpenter

If the "handles" allocation or the copy_from_user() fails then we leak "objs". It's supposed to be freed in panfrost_job_cleanup().

Fixes c117aa4d8701 ("drm: Add a drm_gem_objects_lookup helper")
---
 drivers/gpu/drm/drm_gem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_gem.c +2 -0
index a9e4a610445a..f28724f2eb69 100644
@@ -710,6 +710,8 @@ int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
        if (!objs)
                return -ENOMEM;
 
+       *objs_out = objs;
+
        handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL);
        if (!handles) {
                ret = -ENOMEM;

drivers/gpu/drm/drm_gem.c int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles, · line 730

Quoted 8 lines
drivers/gpu/drm/drm_gem.c +0 -2
@@ -723,8 +725,6 @@ int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
        }
 
        ret = objects_lookup(filp, handles, count, objs);
-       *objs_out = objs;
-
 out:
        kvfree(handles);
        return ret;

It seems that this will return error to the caller, mangle the output pointer and effectively still leak the objs.

Better option IMHO is to:
- move the __user/copy_from_user into the caller
Removes a silly kvmalloc_array(1,...) in ~90+ users and drops the "out" label. Extra bonus, this is the only instance in drm_gem with __user - consistency is nice.
- add "err" or similar label, where the objs is freed before returning an error.

-Emil

#2 lore raw
Quoted 29 lines · Emil Velikov

Hi Dan,

Quoted 26 lines · Dan Carpenter

If the "handles" allocation or the copy_from_user() fails then we leak "objs". It's supposed to be freed in panfrost_job_cleanup().

Fixes c117aa4d8701 ("drm: Add a drm_gem_objects_lookup helper")
---
 drivers/gpu/drm/drm_gem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_gem.c +2 -0
index a9e4a610445a..f28724f2eb69 100644
@@ -710,6 +710,8 @@ int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
        if (!objs)
                return -ENOMEM;
 
+       *objs_out = objs;
+
        handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL);
        if (!handles) {
                ret = -ENOMEM;
@@ -723,8 +725,6 @@ int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
        }
 
Quoted 9 lines

drivers/gpu/drm/drm_gem.c int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles, · line 730

Quoted 6 lines
drivers/gpu/drm/drm_gem.c +0 -2
        ret = objects_lookup(filp, handles, count, objs);
-       *objs_out = objs;
-
 out:
        kvfree(handles);
        return ret;

It seems that this will return error to the caller, mangle the output pointer and effectively still leak the objs.

The patch works.

This is "one function frees everything" style error handling. It gets passed back to panfrost_ioctl_submit() which calls panfrost_job_put() which calls panfrost_job_cleanup() which frees it.

It's a horrible way to do error handling but this was the only actual bug I could see with the approach.

Quoted 6 lines

Better option IMHO is to:
- move the __user/copy_from_user into the caller
Removes a silly kvmalloc_array(1,...) in ~90+ users and drops the "out" label. Extra bonus, this is the only instance in drm_gem with __user - consistency is nice.
- add "err" or similar label, where the objs is freed before returning an error.

Those sound like good ideas. Also we could use kvcalloc() instead of kvmalloc_array() with __GFP_ZERO. But it's too much for me to do... I'm mostly focused on static analysis warnings.

regards,
dan carpenter

#3 lore raw
Quoted 57 lines · Dan Carpenter
Quoted 29 lines · Emil Velikov

Hi Dan,

Quoted 26 lines · Dan Carpenter

If the "handles" allocation or the copy_from_user() fails then we leak "objs". It's supposed to be freed in panfrost_job_cleanup().

Fixes c117aa4d8701 ("drm: Add a drm_gem_objects_lookup helper")
---
 drivers/gpu/drm/drm_gem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_gem.c +2 -0
index a9e4a610445a..f28724f2eb69 100644
@@ -710,6 +710,8 @@ int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
        if (!objs)
                return -ENOMEM;
 
+       *objs_out = objs;
+
        handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL);
        if (!handles) {
                ret = -ENOMEM;
@@ -723,8 +725,6 @@ int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
        }
 
Quoted 9 lines

drivers/gpu/drm/drm_gem.c int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles, · line 730

Quoted 6 lines
drivers/gpu/drm/drm_gem.c +0 -2
        ret = objects_lookup(filp, handles, count, objs);
-       *objs_out = objs;
-
 out:
        kvfree(handles);
        return ret;

It seems that this will return error to the caller, mangle the output pointer and effectively still leak the objs.

The patch works.

This is "one function frees everything" style error handling. It gets passed back to panfrost_ioctl_submit() which calls panfrost_job_put() which calls panfrost_job_cleanup() which frees it.

It's a horrible way to do error handling but this was the only actual bug I could see with the approach.

Quoted 6 lines

Better option IMHO is to:
- move the __user/copy_from_user into the caller
Removes a silly kvmalloc_array(1,...) in ~90+ users and drops the "out" label. Extra bonus, this is the only instance in drm_gem with __user - consistency is nice.
- add "err" or similar label, where the objs is freed before returning an error.

Quoted 4 lines

Those sound like good ideas. Also we could use kvcalloc() instead of kvmalloc_array() with __GFP_ZERO. But it's too much for me to do... I'm mostly focused on static analysis warnings.

Your patch addresses the issue with the smallest diffstat, so I've pushed it to drm-misc-next.

Thanks
Emil