From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796346126; b=eYTHccmKbjp3/U0wtCiIY0iJIAvQQ0pRQWwfoA0Vp7NZgMg0lwZBHhII9AoXQyfbjSnl lIzHPaOVGDJ3J8exxFBEMjSxGfbEK97XYiEh8yMdY0LPri4KnEPZ2bXJkzbANT9aihBbM 2uF6oRvrwp1T+FWs2oL1kPjAql7R+jjkN0OVPQMKfrzN9g2fhe298RVjBcbw3gKTpdhV8 Zy1HnwvTVpIgnK65Ej544KR9kgzJ2H6qnyZh0L+5K/2pnpIA7oUyocJ4g6RwJQGc0LXxH sDbtFvjWg6zPxmxe6pf88lI/QkVVPsFKmqRjlkxAsst7j4opT9ZepF/9twDjkroripg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796346126; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=ms+MTE+ikFHQVuU3KRTZI17Hhq59xdY4d9tayPJDhe0=; b=iBluEpEiwJY7APh4fZis9/BSlLpgA8HmogreChqlD5rp5m9QxrIdxKRV/Dvsm/qi4AL+ Pv35O5rW7TT8ZRZyUdHOFBQcKGgwJtR4JCJcCglxrKMmbrM33PvZJVMRVdMl3LSDa6iSf UtF+dVpeOSqfvVoh6/RC2rc3TvF1ohxhtVHy6PF9bLcmAUt7Y7SjbqulhUDW3MDh41I72 4pIal2MZn9msCXHU+lEQcbABScSp5+12nwg2Wz5VuXuaPEqSHRallVeIYN5MnLv69bQ48 1rXVImeHMwMf+5Lu8fH7Yhu3v/BX5MUiPrxjsGJKN4tfQTWIs04Ix1AqmQr7yObodtQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=f4fErkgh; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E2D1F606A322 for ; Fri, 4 Dec 2026 01:02:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=f4fErkgh Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1D0401F000C2; Fri, 4 Dec 2026 01:02:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796346125; bh=ms+MTE+ikFHQVuU3KRTZI17Hhq59xdY4d9tayPJDhe0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=f4fErkghsgCHv7cr7pnAnG9avM3tHKwDR1dW8gLiVMqSPzrkPZVy/gOFFxZQ1vlgE n3xRwKVyHW0R6uFF+MU9W7s+XruWUsFQkoJQpT4PeJq1ofGwD/v/4tSSfImaBiFwD/ 82JmdCfBZuw6xuPpPF2YDHNujGiy2AhWMk0aHyn4w2RXnv5qP2/uvqYPgncxBCYrPM YQUblQXzz1Sz8DZLXc/irDnKo4nJPJfUZPDg7iv7IZ+7V8j56x3sFnVBBym3MjpUF+ bsTWkxaQCR+CFCLFH2gD0k6ahn9dBfOSxWyumcf2w0idQ/uovXm6M1uUY4wImTtfnf UN+U/IsBmt75A== From: Sasha Levin To: stable@vger.kernel.org Cc: Fei Yu , syzbot+95416f957d84e858b377@syzkaller.appspotmail.com, =?UTF-8?q?Christian=20K=C3=B6nig?= , Sasha Levin Subject: [PATCH 6.1.y] drm/syncobj: Limit the number of handles and use kvmalloc for user-sized arrays Date: Thu, 3 Dec 2026 20:02:03 -0500 Message-ID: <20261204010108.1565015-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120230-fondue-unsaved-1a14@gregkh> References: <2026120230-fondue-unsaved-1a14@gregkh> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Fei Yu [ Upstream commit 6ca87397aeebcfae3057653c3e7c008ccb0bb926 ] The syncobj ioctls allocate several temporary arrays whose size is taken directly from the u32 count_handles provided by userspace: - handles and syncobjs in drm_syncobj_array_find() - points and entries in drm_syncobj_array_wait_timeout() - points and chains in drm_syncobj_timeline_signal_ioctl() These are kmalloc() allocations, so a count above KMALLOC_MAX_SIZE / element size trips the WARN_ON_ONCE_GFP() in ___kmalloc_large_node(): WARNING: mm/slub.c:5459 at ___kmalloc_large_node+0x10c/0x170 mm/slub.c:5459 Call Trace: __kmalloc_large_node_noprof+0x18/0x90 mm/slub.c:5502 __do_kmalloc_node mm/slub.c:5519 [inline] __kmalloc_noprof+0x3f6/0x710 mm/slub.c:5556 _kmalloc_noprof include/linux/slab.h:995 [inline] _kmalloc_array_noprof include/linux/slab.h:1211 [inline] drm_syncobj_array_find+0x3a/0x440 drivers/gpu/drm/drm_syncobj.c:1241 drm_syncobj_timeline_wait_ioctl+0x18a/0x640 drivers/gpu/drm/drm_syncobj.c:1356 drm_ioctl_kernel+0x2df/0x3b0 drivers/gpu/drm/drm_ioctl.c:817 drm_ioctl+0x70e/0xba0 drivers/gpu/drm/drm_ioctl.c:914 The wait entries are 64 bytes each on 64-bit, so with a 4 MiB KMALLOC_MAX_SIZE a wait on more than about 64K handles already fails today, with a warning. Fix this in two parts: - Reject more than DRM_SYNCOBJ_MAX_HANDLES (1M) handles with -EINVAL in drm_syncobj_array_find(), which every ioctl taking a handle array goes through. The limit is a fixed number, so it is the same on all architectures. It is above what these ioctls can handle today: with a 4 MiB KMALLOC_MAX_SIZE the 8-byte arrays already fail above 512K handles on 64-bit, so no request that succeeds now is rejected. A static_assert() makes sure that the largest per-handle allocation, the wait entries, stays below the INT_MAX size limit of kvmalloc(). - Switch the six allocations to kvmalloc(), so that handle counts up to the limit (64 MiB of wait entries for 1M handles) no longer depend on physically contiguous memory. Using kvmalloc() alone is not enough: the reproducer asks for more than INT_MAX bytes, which hits the WARN_ON_ONCE() in __kvmalloc_node_noprof() instead. Fixes: 3e6fb72d6cef ("drm/syncobj: Add a syncobj_array_find helper") Reported-by: syzbot+95416f957d84e858b377@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=95416f957d84e858b377 Suggested-by: Christian König Cc: stable@vger.kernel.org Signed-off-by: Fei Yu Reviewed-by: Christian König Signed-off-by: Christian König Link: https://lore.kernel.org/r/20261109014852.2127723-1-fei.yu@intel.com [ sashal: Replaced the newer kmalloc_objs/kzalloc_objs/kvzalloc_objs helpers with the older kvmalloc_array/kvcalloc calls, with context adjusted accordingly. ] Signed-off-by: Sasha Levin --- drivers/gpu/drm/drm_syncobj.c | 45 +++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/drivers/gpu/drm/drm_syncobj.c b/drivers/gpu/drm/drm_syncobj.c index 8f2737075dc2f..10ffd03b9077a 100644 --- a/drivers/gpu/drm/drm_syncobj.c +++ b/drivers/gpu/drm/drm_syncobj.c @@ -188,6 +188,7 @@ #include #include #include +#include #include #include @@ -209,6 +210,16 @@ struct syncobj_wait_entry { u64 point; }; +/* + * Upper limit for the number of handles passed to a single ioctl. This is a + * fixed number so that the limit is the same on all architectures. The + * largest per-handle allocation is the wait entry array, which must stay + * below the INT_MAX size limit of kvmalloc(). + */ +#define DRM_SYNCOBJ_MAX_HANDLES SZ_1M +static_assert(DRM_SYNCOBJ_MAX_HANDLES <= + INT_MAX / sizeof(struct syncobj_wait_entry)); + static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj, struct syncobj_wait_entry *wait); @@ -984,7 +995,7 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE)) lockdep_assert_none_held_once(); - points = kmalloc_array(count, sizeof(*points), GFP_KERNEL); + points = kvmalloc_array(count, sizeof(*points), GFP_KERNEL); if (points == NULL) return -ENOMEM; @@ -997,7 +1008,7 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, goto err_free_points; } - entries = kcalloc(count, sizeof(*entries), GFP_KERNEL); + entries = kvcalloc(count, sizeof(*entries), GFP_KERNEL); if (!entries) { timeout = -ENOMEM; goto err_free_points; @@ -1109,10 +1120,10 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, &entries[i].fence_cb); dma_fence_put(entries[i].fence); } - kfree(entries); + kvfree(entries); err_free_points: - kfree(points); + kvfree(points); return timeout; } @@ -1192,7 +1203,10 @@ static int drm_syncobj_array_find(struct drm_file *file_private, struct drm_syncobj **syncobjs; int ret; - handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL); + if (count_handles > DRM_SYNCOBJ_MAX_HANDLES) + return -EINVAL; + + handles = kvmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL); if (handles == NULL) return -ENOMEM; @@ -1202,7 +1216,7 @@ static int drm_syncobj_array_find(struct drm_file *file_private, goto err_free_handles; } - syncobjs = kmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL); + syncobjs = kvmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL); if (syncobjs == NULL) { ret = -ENOMEM; goto err_free_handles; @@ -1216,16 +1230,16 @@ static int drm_syncobj_array_find(struct drm_file *file_private, } } - kfree(handles); + kvfree(handles); *syncobjs_out = syncobjs; return 0; err_put_syncobjs: while (i-- > 0) drm_syncobj_put(syncobjs[i]); - kfree(syncobjs); + kvfree(syncobjs); err_free_handles: - kfree(handles); + kvfree(handles); return ret; } @@ -1237,7 +1251,7 @@ static void drm_syncobj_array_free(struct drm_syncobj **syncobjs, for (i = 0; i < count; i++) drm_syncobj_put(syncobjs[i]); - kfree(syncobjs); + kvfree(syncobjs); } int @@ -1404,8 +1418,8 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, if (ret < 0) return ret; - points = kmalloc_array(args->count_handles, sizeof(*points), - GFP_KERNEL); + points = kvmalloc_array(args->count_handles, sizeof(*points), + GFP_KERNEL); if (!points) { ret = -ENOMEM; goto out; @@ -1418,7 +1432,8 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, goto err_points; } - chains = kmalloc_array(args->count_handles, sizeof(void *), GFP_KERNEL); + chains = kvmalloc_array(args->count_handles, sizeof(void *), + GFP_KERNEL); if (!chains) { ret = -ENOMEM; goto err_points; @@ -1441,9 +1456,9 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, dma_fence_put(fence); } err_chains: - kfree(chains); + kvfree(chains); err_points: - kfree(points); + kvfree(points); out: drm_syncobj_array_free(syncobjs, args->count_handles); -- 2.53.0