[ Upstream commit 52b330799e2d6f825ae2bb74662ec1b10eb954bb ]

Exynos Virtual Display driver performs memory alloc/free operations
without lock protection, which easily causes concurrency problem.

For example, use-after-free can occur in race scenario like this:
```
        CPU0                            CPU1                            CPU2
        ----                            ----                            ----
  vidi_connection_ioctl()
    if (vidi->connection) // true
      drm_edid = drm_edid_alloc(); // alloc drm_edid
      ...
      ctx->raw_edid = drm_edid;
      ...
                                                                
drm_mode_getconnector()
                                                                  
drm_helper_probe_single_connector_modes()
                                                                    
vidi_get_modes()
                                                                      if 
(ctx->raw_edid) // true
                                                                        
drm_edid_dup(ctx->raw_edid);
                                                                          if 
(!drm_edid) // false
                                                                          ...
                                vidi_connection_ioctl()
                                  if (vidi->connection) // false
                                    drm_edid_free(ctx->raw_edid); // free 
drm_edid
                                    ...
                                                                          
drm_edid_alloc(drm_edid->edid)
                                                                            
kmemdup(edid); // UAF!!
                                                                            ...
```

To prevent these vulns, at least in vidi_context, member variables related
to memory alloc/free should be protected with ctx->lock.

Cc: <[email protected]>
Signed-off-by: Jeongjun Park <[email protected]>
---
 drivers/gpu/drm/exynos/exynos_drm_vidi.c | 43 +++++++++++++++++++-----
 1 file changed, 35 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/exynos/exynos_drm_vidi.c 
b/drivers/gpu/drm/exynos/exynos_drm_vidi.c
index 576d79ebe9a8..b7eae2469b31 100644
--- a/drivers/gpu/drm/exynos/exynos_drm_vidi.c
+++ b/drivers/gpu/drm/exynos/exynos_drm_vidi.c
@@ -186,15 +186,17 @@ static ssize_t vidi_store_connection(struct device *dev,
                                const char *buf, size_t len)
 {
        struct vidi_context *ctx = dev_get_drvdata(dev);
-       int ret;
+       int ret, new_connected;
 
-       ret = kstrtoint(buf, 0, &ctx->connected);
+       ret = kstrtoint(buf, 0, &new_connected);
        if (ret)
                return ret;
 
-       if (ctx->connected > 1)
+       if (new_connected > 1)
                return -EINVAL;
 
+       mutex_lock(&ctx->lock);
+
        /* use fake edid data for test. */
        if (!ctx->raw_edid)
                ctx->raw_edid = (struct edid *)fake_edid_info;
@@ -202,14 +204,21 @@ static ssize_t vidi_store_connection(struct device *dev,
        /* if raw_edid isn't same as fake data then it can't be tested. */
        if (ctx->raw_edid != (struct edid *)fake_edid_info) {
                DRM_DEV_DEBUG_KMS(dev, "edid data is not fake data.\n");
-               return -EINVAL;
+               ret = -EINVAL;
+               goto fail;
        }
 
+       ctx->connected = new_connected;
+       mutex_unlock(&ctx->lock);
+
        DRM_DEV_DEBUG_KMS(dev, "requested connection.\n");
 
        drm_helper_hpd_irq_event(ctx->drm_dev);
 
        return len;
+fail:
+       mutex_unlock(&ctx->lock);
+       return ret;
 }
 
 static DEVICE_ATTR(connection, 0644, vidi_show_connection,
@@ -244,11 +253,14 @@ int vidi_connection_ioctl(struct drm_device *drm_dev, 
void *data,
                return -EINVAL;
        }
 
+       mutex_lock(&ctx->lock);
        if (ctx->connected == vidi->connection) {
+               mutex_unlock(&ctx->lock);
                DRM_DEV_DEBUG_KMS(ctx->dev,
                                  "same connection request.\n");
                return -EINVAL;
        }
+       mutex_unlock(&ctx->lock);
 
        if (vidi->connection) {
                struct edid *raw_edid;
@@ -271,20 +283,27 @@ int vidi_connection_ioctl(struct drm_device *drm_dev, 
void *data,
                                          "failed to allocate raw_edid.\n");
                        return -ENOMEM;
                }
+               mutex_lock(&ctx->lock);
                ctx->raw_edid = raw_edid;
+               mutex_unlock(&ctx->lock);
        } else {
                /*
                 * with connection = 0, free raw_edid
                 * only if raw edid data isn't same as fake data.
                 */
+               mutex_lock(&ctx->lock);
                if (ctx->raw_edid && ctx->raw_edid !=
                                (struct edid *)fake_edid_info) {
                        kfree(ctx->raw_edid);
                        ctx->raw_edid = NULL;
                }
+               mutex_unlock(&ctx->lock);
        }
 
+       mutex_lock(&ctx->lock);
        ctx->connected = vidi->connection;
+       mutex_unlock(&ctx->lock);
+
        drm_helper_hpd_irq_event(ctx->drm_dev);
 
        return 0;
@@ -299,7 +318,7 @@ static enum drm_connector_status vidi_detect(struct 
drm_connector *connector,
         * connection request would come from user side
         * to do hotplug through specific ioctl.
         */
-       return ctx->connected ? connector_status_connected :
+       return READ_ONCE(ctx->connected) ? connector_status_connected :
                        connector_status_disconnected;
 }
 
@@ -321,22 +340,24 @@ static int vidi_get_modes(struct drm_connector *connector)
        struct vidi_context *ctx = ctx_from_connector(connector);
        struct edid *edid;
        int edid_len;
-       int count;
+       int count = 0;
 
        /*
         * the edid data comes from user side and it would be set
         * to ctx->raw_edid through specific ioctl.
         */
+
+       mutex_lock(&ctx->lock);
        if (!ctx->raw_edid) {
                DRM_DEV_DEBUG_KMS(ctx->dev, "raw_edid is null.\n");
-               return 0;
+               goto fail;
        }
 
        edid_len = (1 + ctx->raw_edid->extensions) * EDID_LENGTH;
        edid = kmemdup(ctx->raw_edid, edid_len, GFP_KERNEL);
        if (!edid) {
                DRM_DEV_DEBUG_KMS(ctx->dev, "failed to allocate edid\n");
-               return 0;
+               goto fail;
        }
 
        drm_connector_update_edid_property(connector, edid);
@@ -345,6 +366,8 @@ static int vidi_get_modes(struct drm_connector *connector)
 
        kfree(edid);
 
+fail:
+       mutex_unlock(&ctx->lock);
        return count;
 }
 
@@ -490,11 +513,15 @@ static int vidi_remove(struct platform_device *pdev)
 {
        struct vidi_context *ctx = platform_get_drvdata(pdev);
 
+       mutex_lock(&ctx->lock);
+
        if (ctx->raw_edid != (struct edid *)fake_edid_info) {
                kfree(ctx->raw_edid);
                ctx->raw_edid = NULL;
        }
 
+       mutex_unlock(&ctx->lock);
+
        component_del(&pdev->dev, &vidi_component_ops);
 
        return 0;
--

Reply via email to