Skip to contentSkip to conversation

drm/vkms: Fix use after free and double free on init error

PATCH In review José Expósito 3 participants 6 messages Feb 12, 2025 – Feb 14, 2025

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

PATCH 1/1 drm/vkms: Fix use after free and double free on init error

If the driver initialization fails, the vkms_exit() function might access an uninitialized or freed default_config pointer and it might double free it.

Fix both possible errors by initializing default_config only when the driver initialization succeeded.

---
 drivers/gpu/drm/vkms/vkms_drv.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/gpu/drm/vkms/vkms_drv.c +9 -6
index 7c142bfc3bd9..b6de91134a22 100644
@@ -235,17 +235,19 @@ static int __init vkms_init(void)
 	if (!config)
 		return -ENOMEM;
 
-	default_config = config;
-
 	config->cursor = enable_cursor;
 	config->writeback = enable_writeback;
 	config->overlay = enable_overlay;
 
 	ret = vkms_create(config);
-	if (ret)
+	if (ret) {
 		kfree(config);
+		return ret;
+	}
 
-	return ret;
+	default_config = config;
+
+	return 0;
 }
 
 static void vkms_destroy(struct vkms_config *config)
@@ -269,9 +271,10 @@ static void vkms_destroy(struct vkms_config *config)
 
 static void __exit vkms_exit(void)
 {
-	if (default_config->dev)
-		vkms_destroy(default_config);
+	if (!default_config)
+		return;
 
+	vkms_destroy(default_config);
 	kfree(default_config);
 }
 
Signature · 1 line
2.48.1
maintainer Reviewed-by on v1 1/1
#1 lore raw

Am 12.02.25 um 09:49 schrieb José Expósito:

Quoted 11 lines

If the driver initialization fails, the vkms_exit() function might access an uninitialized or freed default_config pointer and it might double free it.

Fix both possible errors by initializing default_config only when the driver initialization succeeded.

Thanks for posting this patch separately.

Best regards
Thomas

Quoted 45 lines
---
  drivers/gpu/drm/vkms/vkms_drv.c | 15 +++++++++------
  1 file changed, 9 insertions(+), 6 deletions(-)
drivers/gpu/drm/vkms/vkms_drv.c +9 -6
index 7c142bfc3bd9..b6de91134a22 100644
@@ -235,17 +235,19 @@ static int __init vkms_init(void)
  	if (!config)
  		return -ENOMEM;
 
-	default_config = config;
-
  	config->cursor = enable_cursor;
  	config->writeback = enable_writeback;
  	config->overlay = enable_overlay;
 
  	ret = vkms_create(config);
-	if (ret)
+	if (ret) {
  		kfree(config);
+		return ret;
+	}
 
-	return ret;
+	default_config = config;
+
+	return 0;
  }
 
  static void vkms_destroy(struct vkms_config *config)
@@ -269,9 +271,10 @@ static void vkms_destroy(struct vkms_config *config)
 
  static void __exit vkms_exit(void)
  {
-	if (default_config->dev)
-		vkms_destroy(default_config);
+	if (!default_config)
+		return;
 
+	vkms_destroy(default_config);
  	kfree(default_config);
  }
 
Signature · 7 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
maintainer Reviewed-by
#2 lore raw

Le 12/02/2025 à 09:53, Thomas Zimmermann a écrit :

Quoted 10 lines

Am 12.02.25 um 09:49 schrieb José Expósito:

Quoted 7 lines

If the driver initialization fails, the vkms_exit() function might access an uninitialized or freed default_config pointer and it might double free it.

Fix both possible errors by initializing default_config only when the driver initialization succeeded.

Quoted 6 lines
Quoted 4 lines

lore.kernel.org is broken currently, to avoid doing mistakes, I will wait for it to be working again so I can apply your patch using dim+b4.

(I removed [email protected] from CC, the mail server rejected the mail)

Thanks!
Louis Chauvet

Quoted 51 lines

Thanks for posting this patch separately.

Best regards
Thomas

Quoted 45 lines
---
   drivers/gpu/drm/vkms/vkms_drv.c | 15 +++++++++------
   1 file changed, 9 insertions(+), 6 deletions(-)
drivers/gpu/drm/vkms/vkms_drv.c +9 -6
index 7c142bfc3bd9..b6de91134a22 100644
@@ -235,17 +235,19 @@ static int __init vkms_init(void)
   	if (!config)
   		return -ENOMEM;
 
-	default_config = config;
-
   	config->cursor = enable_cursor;
   	config->writeback = enable_writeback;
   	config->overlay = enable_overlay;
 
   	ret = vkms_create(config);
-	if (ret)
+	if (ret) {
   		kfree(config);
+		return ret;
+	}
 
-	return ret;
+	default_config = config;
+
+	return 0;
   }
 
   static void vkms_destroy(struct vkms_config *config)
@@ -269,9 +271,10 @@ static void vkms_destroy(struct vkms_config *config)
 
   static void __exit vkms_exit(void)
   {
-	if (default_config->dev)
-		vkms_destroy(default_config);
+	if (!default_config)
+		return;
 
+	vkms_destroy(default_config);
   	kfree(default_config);
   }
 
Signature · 3 lines
Louis Chauvet, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
maintainer
#3 lore raw
Quoted 22 lines · Louis Chauvet

Le 12/02/2025 à 09:53, Thomas Zimmermann a écrit :

Quoted 10 lines

Am 12.02.25 um 09:49 schrieb José Expósito:

Quoted 7 lines

If the driver initialization fails, the vkms_exit() function might access an uninitialized or freed default_config pointer and it might double free it.

Fix both possible errors by initializing default_config only when the driver initialization succeeded.

Quoted 6 lines
Quoted 4 lines
Quoted 7 lines

lore.kernel.org is broken currently, to avoid doing mistakes, I will wait for it to be working again so I can apply your patch using dim+b4.

(I removed [email protected] from CC, the mail server rejected the mail)

Thanks!
Louis Chauvet

Hello,

I tried to apply the commit, but I have a strange issue:

	$ dim push
	Enumerating objects: 13, done.
	Counting objects: 100% (13/13), done.
	Delta compression using up to 20 threads
	Compressing objects: 100% (7/7), done.
	Writing objects: 100% (7/7), 1.67 KiB | 113.00 KiB/s, done.
	Total 7 (delta 6), reused 0 (delta 0), pack-reused 0 (from 0)
	remote:
	remote: ========================================================================
	remote:
	remote:    Equinix is shutting down its operations with us on April 30, 2025.
	remote:    They have graciously supported us for almost 5 years, but all good
	remote:    things come to an end. Given the time frame, it's going to be hard
	remote:       to make a smooth transition of the cluster to somewhere else
	remote: ([TBD](https://gitlab.freedesktop.org/freedesktop/freedesktop/-/issues/2011)).
	remote:     Please expect in the next months some hiccups in the service and
	remote:    probably at least a full week of downtime to transfer gitlab to a
	remote:                different place. All help is appreciated.
	remote:
	remote: ========================================================================
	remote:
	To gitlab.freedesktop.org:drm/misc/kernel.git
	   ff3881cc6a58..ed15511a773d  drm-misc-next -> drm-misc-next
	Pushing drm-misc-fixes to for-linux-next-fixes... Everything up-to-date
	Done.
	Out of merge window. Pushing drm-misc-next to for-linux-next... 
	remote:
	remote: ========================================================================
	remote:
	remote: ERROR: Internal API unreachable

remote:
remote: ======================================================================== remote:
fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

Is this expected?

Thanks,
Louis Chauvet

Quoted 50 lines
Quoted 44 lines

Thanks for posting this patch separately.

Best regards
Thomas

Quoted 38 lines
---
   drivers/gpu/drm/vkms/vkms_drv.c | 15 +++++++++------
   1 file changed, 9 insertions(+), 6 deletions(-)
drivers/gpu/drm/vkms/vkms_drv.c +9 -6
index 7c142bfc3bd9..b6de91134a22 100644
@@ -235,17 +235,19 @@ static int __init vkms_init(void)
   	if (!config)
   		return -ENOMEM;
-	default_config = config;
-
   	config->cursor = enable_cursor;
   	config->writeback = enable_writeback;
   	config->overlay = enable_overlay;
   	ret = vkms_create(config);
-	if (ret)
+	if (ret) {
   		kfree(config);
+		return ret;
+	}
-	return ret;
+	default_config = config;
+
+	return 0;
   }
   static void vkms_destroy(struct vkms_config *config)
@@ -269,9 +271,10 @@ static void vkms_destroy(struct vkms_config *config)
   static void __exit vkms_exit(void)
   {
-	if (default_config->dev)
-		vkms_destroy(default_config);
+	if (!default_config)
+		return;
+	vkms_destroy(default_config);
   	kfree(default_config);
   }
Signature · 3 lines
Louis Chauvet, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#4 lore raw
Quoted 71 lines · Louis Chauvet
Quoted 22 lines · Louis Chauvet

Le 12/02/2025 à 09:53, Thomas Zimmermann a écrit :

Quoted 10 lines

Am 12.02.25 um 09:49 schrieb José Expósito:

Quoted 7 lines

If the driver initialization fails, the vkms_exit() function might access an uninitialized or freed default_config pointer and it might double free it.

Fix both possible errors by initializing default_config only when the driver initialization succeeded.

Quoted 6 lines
Quoted 4 lines
Quoted 7 lines

lore.kernel.org is broken currently, to avoid doing mistakes, I will wait for it to be working again so I can apply your patch using dim+b4.

(I removed [email protected] from CC, the mail server rejected the mail)

Thanks!
Louis Chauvet

Hello,

I tried to apply the commit, but I have a strange issue:

	$ dim push
	Enumerating objects: 13, done.
	Counting objects: 100% (13/13), done.
	Delta compression using up to 20 threads
	Compressing objects: 100% (7/7), done.
	Writing objects: 100% (7/7), 1.67 KiB | 113.00 KiB/s, done.
	Total 7 (delta 6), reused 0 (delta 0), pack-reused 0 (from 0)
	remote:
	remote: ========================================================================
	remote:
	remote:    Equinix is shutting down its operations with us on April 30, 2025.
	remote:    They have graciously supported us for almost 5 years, but all good
	remote:    things come to an end. Given the time frame, it's going to be hard
	remote:       to make a smooth transition of the cluster to somewhere else
	remote: ([TBD](https://gitlab.freedesktop.org/freedesktop/freedesktop/-/issues/2011)).
	remote:     Please expect in the next months some hiccups in the service and
	remote:    probably at least a full week of downtime to transfer gitlab to a
	remote:                different place. All help is appreciated.
	remote:
	remote: ========================================================================
	remote:
	To gitlab.freedesktop.org:drm/misc/kernel.git
	   ff3881cc6a58..ed15511a773d  drm-misc-next -> drm-misc-next
	Pushing drm-misc-fixes to for-linux-next-fixes... Everything up-to-date
	Done.
	Out of merge window. Pushing drm-misc-next to for-linux-next... 
	remote:
	remote: ========================================================================
	remote:
	remote: ERROR: Internal API unreachable

remote:
remote: ======================================================================== remote:
fatal: Could not read from remote repository.

Quoted 4 lines

Please make sure you have the correct access rights and the repository exists.

Is this expected?

I guess you managed to fix the issue. I git pull-ed this morning and the patch was applied.

Jose

Quoted 53 lines

Thanks,
Louis Chauvet

Quoted 50 lines
Quoted 44 lines

Thanks for posting this patch separately.

Best regards
Thomas

Quoted 38 lines
---
   drivers/gpu/drm/vkms/vkms_drv.c | 15 +++++++++------
   1 file changed, 9 insertions(+), 6 deletions(-)
drivers/gpu/drm/vkms/vkms_drv.c +9 -6
index 7c142bfc3bd9..b6de91134a22 100644
@@ -235,17 +235,19 @@ static int __init vkms_init(void)
   	if (!config)
   		return -ENOMEM;
-	default_config = config;
-
   	config->cursor = enable_cursor;
   	config->writeback = enable_writeback;
   	config->overlay = enable_overlay;
   	ret = vkms_create(config);
-	if (ret)
+	if (ret) {
   		kfree(config);
+		return ret;
+	}
-	return ret;
+	default_config = config;
+
+	return 0;
   }
   static void vkms_destroy(struct vkms_config *config)
@@ -269,9 +271,10 @@ static void vkms_destroy(struct vkms_config *config)
   static void __exit vkms_exit(void)
   {
-	if (default_config->dev)
-		vkms_destroy(default_config);
+	if (!default_config)
+		return;
+	vkms_destroy(default_config);
   	kfree(default_config);
   }
Signature · 3 lines
Louis Chauvet, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
maintainer
#5 lore raw

Le 14/02/2025 à 16:44, José Expósito a écrit :

Quoted 72 lines · Louis Chauvet
Quoted 71 lines · Louis Chauvet
Quoted 22 lines

Le 12/02/2025 à 09:53, Thomas Zimmermann a écrit :

Quoted 10 lines

Am 12.02.25 um 09:49 schrieb José Expósito:

Quoted 7 lines

If the driver initialization fails, the vkms_exit() function might access an uninitialized or freed default_config pointer and it might double free it.

Fix both possible errors by initializing default_config only when the driver initialization succeeded.

Quoted 6 lines
Quoted 4 lines
Quoted 7 lines

lore.kernel.org is broken currently, to avoid doing mistakes, I will wait for it to be working again so I can apply your patch using dim+b4.

(I removed [email protected] from CC, the mail server rejected the mail)

Thanks!
Louis Chauvet

Hello,

I tried to apply the commit, but I have a strange issue:

	$ dim push
	Enumerating objects: 13, done.
	Counting objects: 100% (13/13), done.
	Delta compression using up to 20 threads
	Compressing objects: 100% (7/7), done.
	Writing objects: 100% (7/7), 1.67 KiB | 113.00 KiB/s, done.
	Total 7 (delta 6), reused 0 (delta 0), pack-reused 0 (from 0)
	remote:
	remote: ========================================================================
	remote:
	remote:    Equinix is shutting down its operations with us on April 30, 2025.
	remote:    They have graciously supported us for almost 5 years, but all good
	remote:    things come to an end. Given the time frame, it's going to be hard
	remote:       to make a smooth transition of the cluster to somewhere else
	remote: ([TBD](https://gitlab.freedesktop.org/freedesktop/freedesktop/-/issues/2011)).
	remote:     Please expect in the next months some hiccups in the service and
	remote:    probably at least a full week of downtime to transfer gitlab to a
	remote:                different place. All help is appreciated.
	remote:
	remote: ========================================================================
	remote:
	To gitlab.freedesktop.org:drm/misc/kernel.git
	   ff3881cc6a58..ed15511a773d  drm-misc-next -> drm-misc-next
	Pushing drm-misc-fixes to for-linux-next-fixes... Everything up-to-date
	Done.
	Out of merge window. Pushing drm-misc-next to for-linux-next...
	remote:
	remote: ========================================================================
	remote:
	remote: ERROR: Internal API unreachable
	remote:
	remote: ========================================================================
	remote:
	fatal: Could not read from remote repository.
Quoted 7 lines
Quoted 4 lines

Please make sure you have the correct access rights and the repository exists.

Is this expected?

I guess you managed to fix the issue. I git pull-ed this morning and the patch was applied.

It is applied to drm-misc/drm-misc-next, but not on drm-misc/for-linux-next.

I don't know if this is important and how to fix the issue if yes.

Louis Chauvet

Quoted 55 lines

Jose

Quoted 53 lines

Thanks,
Louis Chauvet

Quoted 50 lines
Quoted 44 lines

Thanks for posting this patch separately.

Best regards
Thomas

Quoted 38 lines
---
    drivers/gpu/drm/vkms/vkms_drv.c | 15 +++++++++------
    1 file changed, 9 insertions(+), 6 deletions(-)
drivers/gpu/drm/vkms/vkms_drv.c +9 -6
index 7c142bfc3bd9..b6de91134a22 100644
@@ -235,17 +235,19 @@ static int __init vkms_init(void)
    	if (!config)
    		return -ENOMEM;
-	default_config = config;
-
    	config->cursor = enable_cursor;
    	config->writeback = enable_writeback;
    	config->overlay = enable_overlay;
    	ret = vkms_create(config);
-	if (ret)
+	if (ret) {
    		kfree(config);
+		return ret;
+	}
-	return ret;
+	default_config = config;
+
+	return 0;
    }
    static void vkms_destroy(struct vkms_config *config)
@@ -269,9 +271,10 @@ static void vkms_destroy(struct vkms_config *config)
    static void __exit vkms_exit(void)
    {
-	if (default_config->dev)
-		vkms_destroy(default_config);
+	if (!default_config)
+		return;
+	vkms_destroy(default_config);
    	kfree(default_config);
    }
Signature · 3 lines
Louis Chauvet, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Signature · 3 lines
Louis Chauvet, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com