From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
patches@lists.linux.dev,
Jacek Lawrynowicz <jacek.lawrynowicz@linux.intel.com>,
Thomas Zimmermann <tzimmermann@suse.de>,
Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
Maxime Ripard <mripard@kernel.org>,
David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
Simona Vetter <simona.vetter@intel.com>,
Simona Vetter <simona.vetter@ffwll.ch>
Subject: [PATCH 5.15 32/77] drm/gem: Fix race in drm_gem_handle_create_tail()
Date: Tue, 15 Jul 2025 15:13:31 +0200 [thread overview]
Message-ID: <20250715130752.997780056@linuxfoundation.org> (raw)
In-Reply-To: <20250715130751.668489382@linuxfoundation.org>
5.15-stable review patch. If anyone has any objections, please let me know.
------------------
From: Simona Vetter <simona.vetter@ffwll.ch>
commit bd46cece51a36ef088f22ef0416ac13b0a46d5b0 upstream.
Object creation is a careful dance where we must guarantee that the
object is fully constructed before it is visible to other threads, and
GEM buffer objects are no difference.
Final publishing happens by calling drm_gem_handle_create(). After
that the only allowed thing to do is call drm_gem_object_put() because
a concurrent call to the GEM_CLOSE ioctl with a correctly guessed id
(which is trivial since we have a linear allocator) can already tear
down the object again.
Luckily most drivers get this right, the very few exceptions I've
pinged the relevant maintainers for. Unfortunately we also need
drm_gem_handle_create() when creating additional handles for an
already existing object (e.g. GETFB ioctl or the various bo import
ioctl), and hence we cannot have a drm_gem_handle_create_and_put() as
the only exported function to stop these issues from happening.
Now unfortunately the implementation of drm_gem_handle_create() isn't
living up to standards: It does correctly finishe object
initialization at the global level, and hence is safe against a
concurrent tear down. But it also sets up the file-private aspects of
the handle, and that part goes wrong: We fully register the object in
the drm_file.object_idr before calling drm_vma_node_allow() or
obj->funcs->open, which opens up races against concurrent removal of
that handle in drm_gem_handle_delete().
Fix this with the usual two-stage approach of first reserving the
handle id, and then only registering the object after we've completed
the file-private setup.
Jacek reported this with a testcase of concurrently calling GEM_CLOSE
on a freshly-created object (which also destroys the object), but it
should be possible to hit this with just additional handles created
through import or GETFB without completed destroying the underlying
object with the concurrent GEM_CLOSE ioctl calls.
Note that the close-side of this race was fixed in f6cd7daecff5 ("drm:
Release driver references to handle before making it available
again"), which means a cool 9 years have passed until someone noticed
that we need to make this symmetry or there's still gaps left :-/
Without the 2-stage close approach we'd still have a race, therefore
that's an integral part of this bugfix.
More importantly, this means we can have NULL pointers behind
allocated id in our drm_file.object_idr. We need to check for that
now:
- drm_gem_handle_delete() checks for ERR_OR_NULL already
- drm_gem.c:object_lookup() also chekcs for NULL
- drm_gem_release() should never be called if there's another thread
still existing that could call into an IOCTL that creates a new
handle, so cannot race. For paranoia I added a NULL check to
drm_gem_object_release_handle() though.
- most drivers (etnaviv, i915, msm) are find because they use
idr_find(), which maps both ENOENT and NULL to NULL.
- drivers using idr_for_each_entry() should also be fine, because
idr_get_next does filter out NULL entries and continues the
iteration.
- The same holds for drm_show_memory_stats().
v2: Use drm_WARN_ON (Thomas)
Reported-by: Jacek Lawrynowicz <jacek.lawrynowicz@linux.intel.com>
Tested-by: Jacek Lawrynowicz <jacek.lawrynowicz@linux.intel.com>
Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de>
Cc: stable@vger.kernel.org
Cc: Jacek Lawrynowicz <jacek.lawrynowicz@linux.intel.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: Thomas Zimmermann <tzimmermann@suse.de>
Cc: David Airlie <airlied@gmail.com>
Cc: Simona Vetter <simona@ffwll.ch>
Signed-off-by: Simona Vetter <simona.vetter@intel.com>
Signed-off-by: Simona Vetter <simona.vetter@ffwll.ch>
Link: https://patchwork.freedesktop.org/patch/msgid/20250707151814.603897-1-simona.vetter@ffwll.ch
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
drivers/gpu/drm/drm_gem.c | 10 +++++++++-
include/drm/drm_file.h | 3 +++
2 files changed, 12 insertions(+), 1 deletion(-)
--- a/drivers/gpu/drm/drm_gem.c
+++ b/drivers/gpu/drm/drm_gem.c
@@ -234,6 +234,9 @@ drm_gem_object_release_handle(int id, vo
struct drm_file *file_priv = data;
struct drm_gem_object *obj = ptr;
+ if (drm_WARN_ON(obj->dev, !data))
+ return 0;
+
if (obj->funcs->close)
obj->funcs->close(obj, file_priv);
@@ -361,7 +364,7 @@ drm_gem_handle_create_tail(struct drm_fi
idr_preload(GFP_KERNEL);
spin_lock(&file_priv->table_lock);
- ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
+ ret = idr_alloc(&file_priv->object_idr, NULL, 1, 0, GFP_NOWAIT);
spin_unlock(&file_priv->table_lock);
idr_preload_end();
@@ -382,6 +385,11 @@ drm_gem_handle_create_tail(struct drm_fi
goto err_revoke;
}
+ /* mirrors drm_gem_handle_delete to avoid races */
+ spin_lock(&file_priv->table_lock);
+ obj = idr_replace(&file_priv->object_idr, obj, handle);
+ WARN_ON(obj != NULL);
+ spin_unlock(&file_priv->table_lock);
*handlep = handle;
return 0;
--- a/include/drm/drm_file.h
+++ b/include/drm/drm_file.h
@@ -273,6 +273,9 @@ struct drm_file {
*
* Mapping of mm object handles to object pointers. Used by the GEM
* subsystem. Protected by @table_lock.
+ *
+ * Note that allocated entries might be NULL as a transient state when
+ * creating or deleting a handle.
*/
struct idr object_idr;
next prev parent reply other threads:[~2025-07-15 13:29 UTC|newest]
Thread overview: 86+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-15 13:12 [PATCH 5.15 00/77] 5.15.189-rc1 review Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 01/77] drm/exynos: exynos7_drm_decon: add vblank check in IRQ handling Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 02/77] ASoC: fsl_asrc: use internal measured ratio for non-ideal ratio mode Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 03/77] perf: Revert to requiring CAP_SYS_ADMIN for uprobes Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 04/77] fix proc_sys_compare() handling of in-lookup dentries Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 05/77] netlink: Fix wraparounds of sk->sk_rmem_alloc Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 06/77] tipc: Fix use-after-free in tipc_conn_close() Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 07/77] vsock: Fix transport_{g2h,h2g} TOCTOU Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 08/77] vsock: Fix transport_* TOCTOU Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 09/77] vsock: Fix IOCTL_VM_SOCKETS_GET_LOCAL_CID to check also `transport_local` Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 10/77] net: phy: smsc: Fix Auto-MDIX configuration when disabled by strap Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 11/77] net: phy: smsc: Fix link failure in forced mode with Auto-MDIX Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 12/77] atm: clip: Fix potential null-ptr-deref in to_atmarpd() Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 13/77] atm: clip: Fix memory leak of struct clip_vcc Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 14/77] atm: clip: Fix infinite recursive call of clip_push() Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 15/77] atm: clip: Fix NULL pointer dereference in vcc_sendmsg() Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 16/77] net/sched: Abort __tc_modify_qdisc if parent class does not exist Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 17/77] fs/proc: do_task_stat: use __for_each_thread() Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 18/77] ice: safer stats processing Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 19/77] rxrpc: Fix oops due to non-existence of prealloc backlog struct Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 20/77] bpf: fix precision backtracking instruction iteration Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 21/77] thermal/int340x_thermal: handle data_vault when the value is ZERO_SIZE_PTR Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 22/77] aoe: avoid potential deadlock at set_capacity Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 23/77] bpf, sockmap: Fix skb refcnt race after locking changes Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 24/77] jfs: fix null ptr deref in dtInsertEntry Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 25/77] xen: replace xen_remap() with memremap() Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 26/77] x86/mce/amd: Fix threshold limit reset Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 27/77] x86/mce: Dont remove sysfs if thresholding sysfs init fails Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 28/77] x86/mce: Make sure CMCI banks are cleared during shutdown on Intel Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 29/77] gre: Fix IPv6 multicast route creation Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 30/77] pinctrl: qcom: msm: mark certain pins as invalid for interrupts Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 31/77] drm/sched: Increment job count before swapping tail spsc queue Greg Kroah-Hartman
2025-07-15 13:13 ` Greg Kroah-Hartman [this message]
2025-07-15 13:13 ` [PATCH 5.15 33/77] usb: gadget: u_serial: Fix race condition in TTY wakeup Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 34/77] Revert "ACPI: battery: negate current when discharging" Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 35/77] btrfs: propagate last_unlink_trans earlier when doing a rmdir Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 36/77] btrfs: use btrfs_record_snapshot_destroy() during rmdir Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 37/77] RDMA/mlx5: Fix vport loopback for MPV device Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 38/77] pwm: mediatek: Ensure to disable clocks in error path Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 39/77] netlink: Fix rmem check in netlink_broadcast_deliver() Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 40/77] netlink: make sure we allow at least one dump skb Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 41/77] virtio-net: ensure the received length does not exceed allocated size Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 42/77] xhci: Allow RPM on the USB controller (1022:43f7) by default Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 43/77] usb: xhci: quirk for data loss in ISOC transfers Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 44/77] xhci: Disable stream for xHC controller with XHCI_BROKEN_STREAMS Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 45/77] Input: xpad - support Acer NGR 200 Controller Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 46/77] usb:cdnsp: remove TRB_FLUSH_ENDPOINT command Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 47/77] usb: cdnsp: Replace snprintf() with the safer scnprintf() variant Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 48/77] usb: cdnsp: Fix issue with CV Bad Descriptor test Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 49/77] usb: dwc3: Abort suspend on soft disconnect failure Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 50/77] dma-buf: add dma_resv_for_each_fence_unlocked v8 Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 51/77] dma-buf: use new iterator in dma_resv_wait_timeout Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 52/77] dma-buf: fix timeout handling in dma_resv_wait_timeout v2 Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 53/77] platform/x86: think-lmi: Fix sysfs group cleanup Greg Kroah-Hartman
2025-07-15 18:38 ` Kurt Borja
2025-07-15 19:00 ` Kurt Borja
2025-07-15 19:04 ` Sasha Levin
2025-07-15 19:22 ` Kurt Borja
2025-07-15 19:12 ` Sasha Levin
2025-07-15 21:18 ` Kurt Borja
2025-07-15 13:13 ` [PATCH 5.15 54/77] wifi: zd1211rw: Fix potential NULL pointer dereference in zd_mac_tx_to_dev() Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 55/77] md/raid1: Fix stack memory use after return in raid1_reshape Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 56/77] raid10: cleanup memleak at raid10_make_request Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 57/77] nbd: fix uaf in nbd_genl_connect() error path Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 58/77] smb: server: make use of rdma_destroy_qp() Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 59/77] ksmbd: fix a mount write count leak in ksmbd_vfs_kern_path_locked() Greg Kroah-Hartman
2025-07-15 13:13 ` [PATCH 5.15 60/77] netfilter: flowtable: account for Ethernet header in nf_flow_pppoe_proto() Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 61/77] net: appletalk: Fix device refcount leak in atrtr_create() Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 62/77] net: phy: microchip: limit 100M workaround to link-down events on LAN88xx Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 63/77] can: m_can: m_can_handle_lost_msg(): downgrade msg lost in rx message to debug level Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 64/77] net: ll_temac: Fix missing tx_pending check in ethtools_set_ringparam() Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 65/77] bnxt_en: Fix DCB ETS validation Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 66/77] bnxt_en: Set DMA unmap len correctly for XDP_REDIRECT Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 67/77] atm: idt77252: Add missing `dma_map_error()` Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 68/77] um: vector: Reduce stack usage in vector_eth_configure() Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 69/77] net: usb: qmi_wwan: add SIMCom 8230C composition Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 70/77] HID: lenovo: Add support for ThinkPad X1 Tablet Thin Keyboard Gen2 Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 71/77] vt: add missing notification when switching back to text mode Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 72/77] HID: Add IGNORE quirk for SMARTLINKTECHNOLOGY Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 73/77] HID: quirks: Add quirk for 2 Chicony Electronics HP 5MP Cameras Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 74/77] Input: atkbd - do not skip atkbd_deactivate() when skipping ATKBD_CMD_GETID Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 75/77] vhost-scsi: protect vq->log_used with vq->mutex Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 76/77] x86/mm: Disable hugetlb page table sharing on 32-bit Greg Kroah-Hartman
2025-07-15 13:14 ` [PATCH 5.15 77/77] x86: Fix X86_FEATURE_VERW_CLEAR definition Greg Kroah-Hartman
2025-07-15 16:20 ` [PATCH 5.15 00/77] 5.15.189-rc1 review Vijayendra Suman
2025-07-16 14:59 ` Shuah Khan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250715130752.997780056@linuxfoundation.org \
--to=gregkh@linuxfoundation.org \
--cc=airlied@gmail.com \
--cc=jacek.lawrynowicz@linux.intel.com \
--cc=maarten.lankhorst@linux.intel.com \
--cc=mripard@kernel.org \
--cc=patches@lists.linux.dev \
--cc=simona.vetter@ffwll.ch \
--cc=simona.vetter@intel.com \
--cc=simona@ffwll.ch \
--cc=stable@vger.kernel.org \
--cc=tzimmermann@suse.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).