Skip to contentSkip to conversation

drm: hold gem reference until object is no longer accessed

PATCH Superseded Steve Cohen 2 participants 3 messages Jul 10, 2020 – Jul 19, 2020

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm: hold gem reference until object is no longer accessed

BUG: KASAN: use-after-free in drm_gem_open_ioctl

There is potential for use-after-free here if the GEM object handle is closed between the idr lookup and retrieving the size from the object since a local reference is not being held at that point. Hold the local reference while the object can still be accessed to resolve this.

---
 drivers/gpu/drm/drm_gem.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem.c +4 -3
index 7bf628e..4b2891c 100644
@@ -898,14 +898,15 @@ drm_gem_open_ioctl(struct drm_device *dev, void *data,
 
 	/* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
 	ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
-	drm_gem_object_put_unlocked(obj);
 	if (ret)
-		return ret;
+		goto out;
 
 	args->handle = handle;
 	args->size = obj->size;
 
-	return 0;
+out:
+	drm_gem_object_put_unlocked(obj);
+	return ret;
 }
 
 /**
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#1 lore raw

Hi Steve and others.

Quoted 23 lines · Steve Cohen
BUG: KASAN: use-after-free in drm_gem_open_ioctl

There is potential for use-after-free here if the GEM object handle is closed between the idr lookup and retrieving the size from the object since a local reference is not being held at that point. Hold the local reference while the object can still be accessed to resolve this.

---
 drivers/gpu/drm/drm_gem.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem.c +0 -1
index 7bf628e..4b2891c 100644
@@ -898,14 +898,15 @@ drm_gem_open_ioctl(struct drm_device *dev, void *data,
 
 	/* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
 	ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
-	drm_gem_object_put_unlocked(obj);
 	if (ret)

drivers/gpu/drm/drm_gem.c drm_gem_open_ioctl(struct drm_device *dev, void *data, · line 909

Quoted 10 lines
drivers/gpu/drm/drm_gem.c +4 -2
-		return ret;
+		goto out;
 
 	args->handle = handle;
 	args->size = obj->size;
 
-	return 0;
+out:
+	drm_gem_object_put_unlocked(obj);
+	return ret;

Lookign at drm_gem_flink_ioctl() that is implmented just above this functions there are two things that I noted.

1) In drm_gem_flink_ioctl() the label is named "err:" - and my OCD likes that similar labels have the same name.

2) The function takes the object_name_lock but fails to release it in the error situation.

Danile Vetter updated the locking in
20228c447846da9399ead53fdbbc8ab69b47788a ("drm/gem: completely close gem_open vs. gem_close races")

but I failed to follow it all.

Sam

Quoted 11 lines
drivers/gpu/drm/drm_gem.c +0 -0
 }
 
 /**
Signature · 7 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by
#2 lore raw

Hi Steve.

Quoted 40 lines · Sam Ravnborg

Hi Steve and others.

Quoted 23 lines · Steve Cohen
BUG: KASAN: use-after-free in drm_gem_open_ioctl

There is potential for use-after-free here if the GEM object handle is closed between the idr lookup and retrieving the size from the object since a local reference is not being held at that point. Hold the local reference while the object can still be accessed to resolve this.

---
 drivers/gpu/drm/drm_gem.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem.c +0 -1
index 7bf628e..4b2891c 100644
@@ -898,14 +898,15 @@ drm_gem_open_ioctl(struct drm_device *dev, void *data,
 
 	/* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
 	ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
-	drm_gem_object_put_unlocked(obj);
 	if (ret)

drivers/gpu/drm/drm_gem.c drm_gem_open_ioctl(struct drm_device *dev, void *data, · line 909

Quoted 10 lines
drivers/gpu/drm/drm_gem.c +4 -2
-		return ret;
+		goto out;
 
 	args->handle = handle;
 	args->size = obj->size;
 
-	return 0;
+out:
+	drm_gem_object_put_unlocked(obj);
+	return ret;

Lookign at drm_gem_flink_ioctl() that is implmented just above this functions there are two things that I noted.

Quoted 5 lines

1) In drm_gem_flink_ioctl() the label is named "err:" - and my OCD likes that similar labels have the same name.

2) The function takes the object_name_lock but fails to release it in the error situation.

Daniel pointed out on irc that drm_gem_handle_create_tail releases the lock. If I had read the comment I would have noticed too - sigh.

With the label name fixed to "err:" like used in the function above:

Please re-submit.

Sam

Quoted 15 lines

Danile Vetter updated the locking in
20228c447846da9399ead53fdbbc8ab69b47788a ("drm/gem: completely close gem_open vs. gem_close races")

but I failed to follow it all.

Sam

Quoted 7 lines
drivers/gpu/drm/drm_gem.c +0 -0
 }
 
 /**
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
Quoted 8 lines
Quoted 4 lines

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel