diff mbox series

drm: fix null-ptr-deref in drm_minor_alloc_release

Message ID 20211018135428.3971329-1-wanghai38@huawei.com (mailing list archive)
State New, archived
Headers show
Series drm: fix null-ptr-deref in drm_minor_alloc_release | expand

Commit Message

Wang Hai Oct. 18, 2021, 1:54 p.m. UTC
I got a null-ptr-deref report:

BUG: kernel NULL pointer dereference, address: 0000000000000030
...
RIP: 0010:kobject_put+0x2a/0x180
...
Call Trace:
 put_device+0x25/0x30
 drm_minor_alloc_release.cold+0x45/0x7f [drm]
 drm_managed_release+0x158/0x2d0 [drm]
 drm_dev_init+0x3a7/0x4a0 [drm]
 __devm_drm_dev_alloc+0x55/0xd0 [drm]
 mi0283qt_probe+0x8a/0x2b5 [mi0283qt]
 spi_probe+0xeb/0x130
...
 entry_SYSCALL_64_after_hwframe+0x44/0xae

If drm_sysfs_minor_alloc() fails in drm_minor_alloc(), minor->kdev will
point to PTR_ERR(...) instead of NULL. This will result in null-ptr-deref
when put_device(minor->kdev) is called.

drm_dev_init()
	drm_minor_alloc()
		drm_sysfs_minor_alloc(); // fail, minor->kdev = PTR_ERR(...)
	drm_managed_release()
		drm_minor_alloc_release()
			put_device(minor->kdev); // access non-existent kdev

Define a temp variable and assign it to minor->kdev if the temp variable
is not PTR_ERR.

Fixes: f96306f9892b ("drm: manage drm_minor cleanup with drmm_")
Reported-by: Hulk Robot <hulkci@huawei.com>
Signed-off-by: Wang Hai <wanghai38@huawei.com>
---
 drivers/gpu/drm/drm_drv.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c
index b3a1636d1b98..b302536edbce 100644
--- a/drivers/gpu/drm/drm_drv.c
+++ b/drivers/gpu/drm/drm_drv.c
@@ -112,6 +112,7 @@  static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
 {
 	struct drm_minor *minor;
 	unsigned long flags;
+	struct device *kdev;
 	int r;
 
 	minor = drmm_kzalloc(dev, sizeof(*minor), GFP_KERNEL);
@@ -140,9 +141,11 @@  static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
 	if (r)
 		return r;
 
-	minor->kdev = drm_sysfs_minor_alloc(minor);
-	if (IS_ERR(minor->kdev))
-		return PTR_ERR(minor->kdev);
+	kdev = drm_sysfs_minor_alloc(minor);
+	if (IS_ERR(kdev))
+		return PTR_ERR(kdev);
+
+	minor->kdev = kdev;
 
 	*drm_minor_get_slot(dev, type) = minor;
 	return 0;