Skip to content

Commit 60b7540

Browse files
name2965gregkh
authored andcommitted
drm/exynos: vidi: use ctx->lock to protect struct vidi_context member variables related to memory alloc/free
commit 52b3307 upstream. 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: <stable@vger.kernel.org> Signed-off-by: Jeongjun Park <aha310510@gmail.com> Signed-off-by: Inki Dae <inki.dae@samsung.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ed82e79 commit 60b7540

1 file changed

Lines changed: 32 additions & 6 deletions

File tree

drivers/gpu/drm/exynos/exynos_drm_vidi.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,29 +186,37 @@ static ssize_t vidi_store_connection(struct device *dev,
186186
const char *buf, size_t len)
187187
{
188188
struct vidi_context *ctx = dev_get_drvdata(dev);
189-
int ret;
189+
int ret, new_connected;
190190

191-
ret = kstrtoint(buf, 0, &ctx->connected);
191+
ret = kstrtoint(buf, 0, &new_connected);
192192
if (ret)
193193
return ret;
194-
195-
if (ctx->connected > 1)
194+
if (new_connected > 1)
196195
return -EINVAL;
197196

197+
mutex_lock(&ctx->lock);
198+
198199
/*
199200
* Use fake edid data for test. If raw_edid is set then it can't be
200201
* tested.
201202
*/
202203
if (ctx->raw_edid) {
203204
DRM_DEV_DEBUG_KMS(dev, "edid data is not fake data.\n");
204-
return -EINVAL;
205+
ret = -EINVAL;
206+
goto fail;
205207
}
206208

209+
ctx->connected = new_connected;
210+
mutex_unlock(&ctx->lock);
211+
207212
DRM_DEV_DEBUG_KMS(dev, "requested connection.\n");
208213

209214
drm_helper_hpd_irq_event(ctx->drm_dev);
210215

211216
return len;
217+
fail:
218+
mutex_unlock(&ctx->lock);
219+
return ret;
212220
}
213221

214222
static DEVICE_ATTR(connection, 0644, vidi_show_connection,
@@ -238,11 +246,14 @@ int vidi_connection_ioctl(struct drm_device *drm_dev, void *data,
238246
return -EINVAL;
239247
}
240248

249+
mutex_lock(&ctx->lock);
241250
if (ctx->connected == vidi->connection) {
251+
mutex_unlock(&ctx->lock);
242252
DRM_DEV_DEBUG_KMS(ctx->dev,
243253
"same connection request.\n");
244254
return -EINVAL;
245255
}
256+
mutex_unlock(&ctx->lock);
246257

247258
if (vidi->connection) {
248259
const struct drm_edid *drm_edid;
@@ -262,14 +273,21 @@ int vidi_connection_ioctl(struct drm_device *drm_dev, void *data,
262273
"edid data is invalid.\n");
263274
return -EINVAL;
264275
}
276+
mutex_lock(&ctx->lock);
265277
ctx->raw_edid = drm_edid;
278+
mutex_unlock(&ctx->lock);
266279
} else {
267280
/* with connection = 0, free raw_edid */
281+
mutex_lock(&ctx->lock);
268282
drm_edid_free(ctx->raw_edid);
269283
ctx->raw_edid = NULL;
284+
mutex_unlock(&ctx->lock);
270285
}
271286

287+
mutex_lock(&ctx->lock);
272288
ctx->connected = vidi->connection;
289+
mutex_unlock(&ctx->lock);
290+
273291
drm_helper_hpd_irq_event(ctx->drm_dev);
274292

275293
return 0;
@@ -284,7 +302,7 @@ static enum drm_connector_status vidi_detect(struct drm_connector *connector,
284302
* connection request would come from user side
285303
* to do hotplug through specific ioctl.
286304
*/
287-
return ctx->connected ? connector_status_connected :
305+
return READ_ONCE(ctx->connected) ? connector_status_connected :
288306
connector_status_disconnected;
289307
}
290308

@@ -307,11 +325,15 @@ static int vidi_get_modes(struct drm_connector *connector)
307325
const struct drm_edid *drm_edid;
308326
int count;
309327

328+
mutex_lock(&ctx->lock);
329+
310330
if (ctx->raw_edid)
311331
drm_edid = drm_edid_dup(ctx->raw_edid);
312332
else
313333
drm_edid = drm_edid_alloc(fake_edid_info, sizeof(fake_edid_info));
314334

335+
mutex_unlock(&ctx->lock);
336+
315337
drm_edid_connector_update(connector, drm_edid);
316338

317339
count = drm_edid_connector_add_modes(connector);
@@ -456,9 +478,13 @@ static void vidi_remove(struct platform_device *pdev)
456478
{
457479
struct vidi_context *ctx = platform_get_drvdata(pdev);
458480

481+
mutex_lock(&ctx->lock);
482+
459483
drm_edid_free(ctx->raw_edid);
460484
ctx->raw_edid = NULL;
461485

486+
mutex_unlock(&ctx->lock);
487+
462488
component_del(&pdev->dev, &vidi_component_ops);
463489
}
464490

0 commit comments

Comments
 (0)