@@ -205,9 +205,15 @@ void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem)
if (obj->import_attach)
drm_prime_gem_destroy(obj, shmem->sgt);
- drm_WARN_ON(obj->dev, refcount_read(&shmem->vmap_use_count));
- drm_WARN_ON(obj->dev, refcount_read(&shmem->pages_use_count));
- drm_WARN_ON(obj->dev, refcount_read(&shmem->pages_pin_count));
+ /*
+ * Prevent memory corruption caused by the use-after-free bug in a
+ * case where shmem user erroneously holds reference to pages while
+ * GEM is freed by leaking the GEM.
+ */
+ if (drm_WARN_ON(obj->dev, refcount_read(&shmem->vmap_use_count)) ||
+ drm_WARN_ON(obj->dev, refcount_read(&shmem->pages_use_count)) ||
+ drm_WARN_ON(obj->dev, refcount_read(&shmem->pages_pin_count)))
+ return;
drm_gem_object_release(obj);
kfree(shmem);
Don't free shmem object if it has pages that are in use at the time of the GEM's freeing if DRM driver doesn't manage GEM/pages lifetime properly. This prevents memory corruption due to the use-after-free bug in exchange to leaking GEM. Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com> --- drivers/gpu/drm/drm_gem_shmem_helper.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)