Skip to contentSkip to conversation

drm/exynos: vidi: use memdup_user() instead of kmalloc() and copy_from_user()

PATCH In review [email protected] 3 participants 7 messages Jul 5 – Aug 19

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

PATCH 1/1 drm/exynos: vidi: use memdup_user() instead of kmalloc() and copy_from_user()

From: Mohammad Shahid <[email protected]>

Use memdup_user() to replace the open-coded kmalloc() and copy_from_user() sequence.

This simplifies the code while preserving the existing behavior.

This issue was reported by memdup_user.cocci.

---
 drivers/gpu/drm/exynos/exynos_drm_vidi.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/gpu/drm/exynos/exynos_drm_vidi.c +3 -8
index 67bbf9b8bc0e..183671de711e 100644
@@ -273,14 +273,9 @@ int vidi_connection_ioctl(struct drm_device *drm_dev, void *data,
 
 		size = (hdr.extensions + 1) * EDID_LENGTH;
 
-		edid_buf = kmalloc(size, GFP_KERNEL);
-		if (!edid_buf)
-			return -ENOMEM;
-
-		if (copy_from_user(edid_buf, edid_userptr, size)) {
-			kfree(edid_buf);
-			return -EFAULT;
-		}
+		edid_buf = memdup_user(edid_userptr, size);
+		if (IS_ERR(edid_buf))
+			return PTR_ERR(edid_buf);
 
 		drm_edid = drm_edid_alloc(edid_buf, size);
 		kfree(edid_buf);
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Concurrent executions of vidi_connection_ioctl can cause a memory leak of the drm_edid object due to a time-of-check to time-of-use (TOCTOU) race condition when checking and updating the connection state. --

commit 6f3818ed77cd21c4ace5a636e3cf596ff721c935
Author: Mohammad Shahid <[email protected]>

drm/exynos: vidi: use memdup_user() instead of kmalloc() and copy_from_user()

This patch simplifies the driver by replacing an open-coded sequence of kmalloc() and copy_from_user() with a single call to memdup_user().

Quoted 12 lines
drivers/gpu/drm/exynos/exynos_drm_vidi.c +0 -4
index 67bbf9b8bc0ef..183671de711e2 100644
@@ -273,14 +273,9 @@ int vidi_connection_ioctl(struct drm_device *drm_dev, void *data,
 
 		size = (hdr.extensions + 1) * EDID_LENGTH;
 
-		edid_buf = kmalloc(size, GFP_KERNEL);
-		if (!edid_buf)
-			return -ENOMEM;
-

drivers/gpu/drm/exynos/exynos_drm_vidi.c int vidi_connection_ioctl(struct drm_device *drm_dev, void *data, · line 281

Quoted 10 lines
drivers/gpu/drm/exynos/exynos_drm_vidi.c +3 -4
-		if (copy_from_user(edid_buf, edid_userptr, size)) {
-			kfree(edid_buf);
-			return -EFAULT;
-		}
+		edid_buf = memdup_user(edid_userptr, size);
+		if (IS_ERR(edid_buf))
+			return PTR_ERR(edid_buf);
 
 		drm_edid = drm_edid_alloc(edid_buf, size);
 		kfree(edid_buf);

[Severity: High]
This isn't a bug introduced by this patch, but is it possible for concurrent ioctl calls to leak the drm_edid object allocated here?

Looking at vidi_connection_ioctl() in drivers/gpu/drm/exynos/exynos_drm_vidi.c, the connection state is checked under ctx->lock, but the lock is then released before allocating the EDID:

    mutex_lock(&ctx->lock);
    if (ctx->connected == vidi->connection) {
        mutex_unlock(&ctx->lock);
        ...
    }
    mutex_unlock(&ctx->lock);

If multiple threads call vidi_connection_ioctl() concurrently with vidi->connection = 1 while ctx->connected is 0, won't they both pass the initial check, allocate a new drm_edid object from userspace data, and then sequentially re-acquire the lock to assign it?

    mutex_lock(&ctx->lock);
    ctx->raw_edid = drm_edid;
    mutex_unlock(&ctx->lock);

Does this unconditionally overwrite ctx->raw_edid, leaking any previously allocated drm_edid from the racing thread?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Hi,

Thanks for your contribution. I will review it soon.

Thanks,
Inki Dae

2026년 7월 24일 (금) 오후 9:27, <[email protected]>님이 작성:

Quoted 12 lines

Hi,

Gentle ping on this patch.

Could please take a look when time permits?

Thanks!

Mohammad Shahid

#4 lore raw

Hi Inki,

Thanks for the response. I appreciate you taking the time to review the patch.

For context, a similar cleanup patch of mine was recently accepted by Greg KH and is now in linux-next.

Thanks,
Shahid

author
#5 lore raw
In reply to [email protected]

Hi,

Gentle ping on this patch.

Could someone please review it when time permits?

Thanks!
Shahid.

Quoted 42 lines · On Sun, Jul 5, 2026 at 4:32 PM <[email protected]> wrote:

From: Mohammad Shahid <[email protected]>

Use memdup_user() to replace the open-coded kmalloc() and copy_from_user() sequence.

This simplifies the code while preserving the existing behavior.

This issue was reported by memdup_user.cocci.

---
 drivers/gpu/drm/exynos/exynos_drm_vidi.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/gpu/drm/exynos/exynos_drm_vidi.c +3 -8
index 67bbf9b8bc0e..183671de711e 100644
@@ -273,14 +273,9 @@ int vidi_connection_ioctl(struct drm_device *drm_dev,
 void *data,
 
                size = (hdr.extensions + 1) * EDID_LENGTH;
 
-               edid_buf = kmalloc(size, GFP_KERNEL);
-               if (!edid_buf)
-                       return -ENOMEM;
-
-               if (copy_from_user(edid_buf, edid_userptr, size)) {
-                       kfree(edid_buf);
-                       return -EFAULT;
-               }
+               edid_buf = memdup_user(edid_userptr, size);
+               if (IS_ERR(edid_buf))
+                       return PTR_ERR(edid_buf);
 
                drm_edid = drm_edid_alloc(edid_buf, size);
                kfree(edid_buf);

--
2.43.0

#6 lore raw
In reply to [email protected]

Sorry for being late.

2026년 7월 5일 (일) 오후 8:02, <[email protected]>님이 작성:

Quoted 24 lines

From: Mohammad Shahid <[email protected]>

Use memdup_user() to replace the open-coded kmalloc() and copy_from_user() sequence.

This simplifies the code while preserving the existing behavior.

This issue was reported by memdup_user.cocci.

---
 drivers/gpu/drm/exynos/exynos_drm_vidi.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/gpu/drm/exynos/exynos_drm_vidi.c +0 -1
index 67bbf9b8bc0e..183671de711e 100644
@@ -273,14 +273,9 @@ int vidi_connection_ioctl(struct drm_device *drm_dev, void *data,
 
                size = (hdr.extensions + 1) * EDID_LENGTH;
 
-               edid_buf = kmalloc(size, GFP_KERNEL);

drivers/gpu/drm/exynos/exynos_drm_vidi.c int vidi_connection_ioctl(struct drm_device *drm_dev, void *data, · line 278

Quoted 10 lines
drivers/gpu/drm/exynos/exynos_drm_vidi.c +3 -7
-               if (!edid_buf)
-                       return -ENOMEM;
-
-               if (copy_from_user(edid_buf, edid_userptr, size)) {
-                       kfree(edid_buf);
-                       return -EFAULT;
-               }
+               edid_buf = memdup_user(edid_userptr, size);
+               if (IS_ERR(edid_buf))
+                       return PTR_ERR(edid_buf);

exynos_drm_vidi.c does not include <linux/string.h>, where memdup_user() is declared. It only builds today because the header is pulled in transitively. You would add the include explicitly.

This is a trivial thing so I can handle it.

Merged. Thanks,
Inki Dae

Quoted 7 lines
drivers/gpu/drm/exynos/exynos_drm_vidi.c +0 -1
                drm_edid = drm_edid_alloc(edid_buf, size);
                kfree(edid_buf);
--

2.43.0