Skip to content

Commit 9c53de7

Browse files
committed
drm/gem: Acquire references on GEM handles for framebuffers
jira VULN-136707 cve CVE-2025-38449 commit-author Thomas Zimmermann <tzimmermann@suse.de> commit 5307dce upstream-diff Use mutex_lock/mutex_unlock in drm_gem_object_handle_get_unlocked instead of guard(mutex), which is not available in this kernel A GEM handle can be released while the GEM buffer object is attached to a DRM framebuffer. This leads to the release of the dma-buf backing the buffer object, if any. [1] Trying to use the framebuffer in further mode-setting operations leads to a segmentation fault. Most easily happens with driver that use shadow planes for vmap-ing the dma-buf during a page flip. An example is shown below. [ 156.791968] ------------[ cut here ]------------ [ 156.796830] WARNING: CPU: 2 PID: 2255 at drivers/dma-buf/dma-buf.c:1527 dma_buf_vmap+0x224/0x430 [...] [ 156.942028] RIP: 0010:dma_buf_vmap+0x224/0x430 [ 157.043420] Call Trace: [ 157.045898] <TASK> [ 157.048030] ? show_trace_log_lvl+0x1af/0x2c0 [ 157.052436] ? show_trace_log_lvl+0x1af/0x2c0 [ 157.056836] ? show_trace_log_lvl+0x1af/0x2c0 [ 157.061253] ? drm_gem_shmem_vmap+0x74/0x710 [ 157.065567] ? dma_buf_vmap+0x224/0x430 [ 157.069446] ? __warn.cold+0x58/0xe4 [ 157.073061] ? dma_buf_vmap+0x224/0x430 [ 157.077111] ? report_bug+0x1dd/0x390 [ 157.080842] ? handle_bug+0x5e/0xa0 [ 157.084389] ? exc_invalid_op+0x14/0x50 [ 157.088291] ? asm_exc_invalid_op+0x16/0x20 [ 157.092548] ? dma_buf_vmap+0x224/0x430 [ 157.096663] ? dma_resv_get_singleton+0x6d/0x230 [ 157.101341] ? __pfx_dma_buf_vmap+0x10/0x10 [ 157.105588] ? __pfx_dma_resv_get_singleton+0x10/0x10 [ 157.110697] drm_gem_shmem_vmap+0x74/0x710 [ 157.114866] drm_gem_vmap+0xa9/0x1b0 [ 157.118763] drm_gem_vmap_unlocked+0x46/0xa0 [ 157.123086] drm_gem_fb_vmap+0xab/0x300 [ 157.126979] drm_atomic_helper_prepare_planes.part.0+0x487/0xb10 [ 157.133032] ? lockdep_init_map_type+0x19d/0x880 [ 157.137701] drm_atomic_helper_commit+0x13d/0x2e0 [ 157.142671] ? drm_atomic_nonblocking_commit+0xa0/0x180 [ 157.147988] drm_mode_atomic_ioctl+0x766/0xe40 [...] [ 157.346424] ---[ end trace 0000000000000000 ]--- Acquiring GEM handles for the framebuffer's GEM buffer objects prevents this from happening. The framebuffer's cleanup later puts the handle references. Commit 1a148af ("drm/gem-shmem: Use dma_buf from GEM object instance") triggers the segmentation fault easily by using the dma-buf field more widely. The underlying issue with reference counting has been present before. v2: - acquire the handle instead of the BO (Christian) - fix comment style (Christian) - drop the Fixes tag (Christian) - rename err_ gotos - add missing Link tag Suggested-by: Christian König <christian.koenig@amd.com> Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de> Link: https://elixir.bootlin.com/linux/v6.15/source/drivers/gpu/drm/drm_gem.c#L241 # [1] Cc: Thomas Zimmermann <tzimmermann@suse.de> Cc: Anusha Srivatsa <asrivats@redhat.com> Cc: Christian König <christian.koenig@amd.com> Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com> Cc: Maxime Ripard <mripard@kernel.org> Cc: Sumit Semwal <sumit.semwal@linaro.org> Cc: "Christian König" <christian.koenig@amd.com> Cc: linux-media@vger.kernel.org Cc: dri-devel@lists.freedesktop.org Cc: linaro-mm-sig@lists.linaro.org Cc: <stable@vger.kernel.org> Reviewed-by: Christian König <christian.koenig@amd.com> Link: https://lore.kernel.org/r/20250630084001.293053-1-tzimmermann@suse.de (cherry picked from commit 5307dce) Signed-off-by: Brett Mastbergen <bmastbergen@ciq.com> squsah with acquire
1 parent 95e9e3a commit 9c53de7

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

drivers/gpu/drm/drm_gem.c

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,37 @@ void drm_gem_private_object_fini(struct drm_gem_object *obj)
182182
}
183183
EXPORT_SYMBOL(drm_gem_private_object_fini);
184184

185+
static void drm_gem_object_handle_get(struct drm_gem_object *obj)
186+
{
187+
struct drm_device *dev = obj->dev;
188+
189+
drm_WARN_ON(dev, !mutex_is_locked(&dev->object_name_lock));
190+
191+
if (obj->handle_count++ == 0)
192+
drm_gem_object_get(obj);
193+
}
194+
195+
/**
196+
* drm_gem_object_handle_get_unlocked - acquire reference on user-space handles
197+
* @obj: GEM object
198+
*
199+
* Acquires a reference on the GEM buffer object's handle. Required
200+
* to keep the GEM object alive. Call drm_gem_object_handle_put_unlocked()
201+
* to release the reference.
202+
*/
203+
void drm_gem_object_handle_get_unlocked(struct drm_gem_object *obj)
204+
{
205+
struct drm_device *dev = obj->dev;
206+
207+
mutex_lock(&dev->object_name_lock);
208+
209+
drm_WARN_ON(dev, !obj->handle_count); /* first ref taken in create-tail helper */
210+
drm_gem_object_handle_get(obj);
211+
212+
mutex_unlock(&dev->object_name_lock);
213+
}
214+
EXPORT_SYMBOL(drm_gem_object_handle_get_unlocked);
215+
185216
/**
186217
* drm_gem_object_handle_free - release resources bound to userspace handles
187218
* @obj: GEM object to clean up.
@@ -212,8 +243,14 @@ static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
212243
}
213244
}
214245

215-
static void
216-
drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj)
246+
/**
247+
* drm_gem_object_handle_put_unlocked - releases reference on user-space handles
248+
* @obj: GEM object
249+
*
250+
* Releases a reference on the GEM buffer object's handle. Possibly releases
251+
* the GEM buffer object and associated dma-buf objects.
252+
*/
253+
void drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj)
217254
{
218255
struct drm_device *dev = obj->dev;
219256
bool final = false;
@@ -238,6 +275,7 @@ drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj)
238275
if (final)
239276
drm_gem_object_put(obj);
240277
}
278+
EXPORT_SYMBOL(drm_gem_object_handle_put_unlocked);
241279

242280
/*
243281
* Called at device or object close to release the file's
@@ -366,8 +404,8 @@ drm_gem_handle_create_tail(struct drm_file *file_priv,
366404
int ret;
367405

368406
WARN_ON(!mutex_is_locked(&dev->object_name_lock));
369-
if (obj->handle_count++ == 0)
370-
drm_gem_object_get(obj);
407+
408+
drm_gem_object_handle_get(obj);
371409

372410
/*
373411
* Get the user-visible handle using idr. Preload and perform

drivers/gpu/drm/drm_gem_framebuffer_helper.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void drm_gem_fb_destroy(struct drm_framebuffer *fb)
9999
unsigned int i;
100100

101101
for (i = 0; i < fb->format->num_planes; i++)
102-
drm_gem_object_put(fb->obj[i]);
102+
drm_gem_object_handle_put_unlocked(fb->obj[i]);
103103

104104
drm_framebuffer_cleanup(fb);
105105
kfree(fb);
@@ -174,8 +174,10 @@ int drm_gem_fb_init_with_funcs(struct drm_device *dev,
174174
if (!objs[i]) {
175175
drm_dbg_kms(dev, "Failed to lookup GEM object\n");
176176
ret = -ENOENT;
177-
goto err_gem_object_put;
177+
goto err_gem_object_handle_put_unlocked;
178178
}
179+
drm_gem_object_handle_get_unlocked(objs[i]);
180+
drm_gem_object_put(objs[i]);
179181

180182
min_size = (height - 1) * mode_cmd->pitches[i]
181183
+ drm_format_info_min_pitch(info, i, width)
@@ -185,22 +187,22 @@ int drm_gem_fb_init_with_funcs(struct drm_device *dev,
185187
drm_dbg_kms(dev,
186188
"GEM object size (%zu) smaller than minimum size (%u) for plane %d\n",
187189
objs[i]->size, min_size, i);
188-
drm_gem_object_put(objs[i]);
190+
drm_gem_object_handle_put_unlocked(objs[i]);
189191
ret = -EINVAL;
190-
goto err_gem_object_put;
192+
goto err_gem_object_handle_put_unlocked;
191193
}
192194
}
193195

194196
ret = drm_gem_fb_init(dev, fb, mode_cmd, objs, i, funcs);
195197
if (ret)
196-
goto err_gem_object_put;
198+
goto err_gem_object_handle_put_unlocked;
197199

198200
return 0;
199201

200-
err_gem_object_put:
202+
err_gem_object_handle_put_unlocked:
201203
while (i > 0) {
202204
--i;
203-
drm_gem_object_put(objs[i]);
205+
drm_gem_object_handle_put_unlocked(objs[i]);
204206
}
205207
return ret;
206208
}

drivers/gpu/drm/drm_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ void drm_sysfs_lease_event(struct drm_device *dev);
159159

160160
/* drm_gem.c */
161161
int drm_gem_init(struct drm_device *dev);
162+
void drm_gem_object_handle_get_unlocked(struct drm_gem_object *obj);
163+
void drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj);
162164
int drm_gem_handle_create_tail(struct drm_file *file_priv,
163165
struct drm_gem_object *obj,
164166
u32 *handlep);

0 commit comments

Comments
 (0)