Skip to contentSkip to conversation

drm/lease: Use kvmalloc and bound object_count in create lease ioctl

RFC PATCH syzbot 2 participants 2 messages Oct 23 – Nov 13

Subsystems (2)
lore
Share & tools
lore

syzbot

author syzbot
#0 lore raw

In drm_mode_create_lease_ioctl(), user space supplies an object_count and an array of object_ids to be leased. The kernel copies the IDs from user space using memdup_array_user(), and then fill_object_idr() allocates a temporary array of pointers using kzalloc_objs(struct drm_mode_object *, object_count) (at drivers/gpu/drm/drm_lease.c:389 in the reported tree).

Because kzalloc_objs() performs a kmalloc-family allocation with GFP_KERNEL (which lacks __GFP_NOWARN), if user space requests a sufficiently large object_count (for example, 1,000,000 mode objects), the allocation for the pointer array (1,000,000 * 8 = 8,000,000 bytes on 64-bit systems) requires order 11 pages. This exceeds MAX_PAGE_ORDER and triggers a warning in the page allocator at historical warning-site line drm_lease.c:389 in the reported tree:

WARNING: mm/page_alloc.c:5340 at alloc_order_allowed mm/page_alloc.c:5340
[inline]
WARNING: mm/page_alloc.c:5340 at __alloc_frozen_pages_noprof+0xe4/0x640
mm/page_alloc.c:5397
Call trace:
 alloc_order_allowed mm/page_alloc.c:5340 [inline]
 __alloc_frozen_pages_noprof+0xe4/0x640 mm/page_alloc.c:5397
 alloc_pages_mpol+0x1fc/0x3d0 mm/mempolicy.c:2486
 alloc_frozen_pages_noprof+0xe0/0x214 mm/mempolicy.c:2557
 ___kmalloc_large_node+0xf0/0x160 mm/slub.c:5353
 __kmalloc_large_node_noprof+0x2c/0x8c mm/slub.c:5385
 fill_object_idr drivers/gpu/drm/drm_lease.c:389 [inline]
 drm_mode_create_lease_ioctl+0x45c/0x15c8 drivers/gpu/drm/drm_lease.c:521
 drm_ioctl_kernel+0x250/0x328 drivers/gpu/drm/drm_ioctl.c:817
 drm_ioctl+0x65c/0xa58 drivers/gpu/drm/drm_ioctl.c:914
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:597 [inline]

Furthermore, cl->object_count is an unbounded __u32, whereas helper functions such as fill_object_idr() take object_count as a signed integer. A value of cl->object_count greater than INT_MAX is converted to a signed parameter (becoming negative) rather than causing signed arithmetic overflow. In addition, an allocation size exceeding INT_MAX would trigger a WARN_ON_ONCE inside __kvmalloc_node_noprof().

Fix these issues by validating cl->object_count early in drm_mode_create_lease_ioctl() so that values exceeding INT_MAX are rejected with -EINVAL. In addition, cap object_count to DRM_MAX_LEASE_OBJECTS (1024). In DRM, only CRTCs, connectors, and planes are leaseable, and DRM core bounds each to 32 objects via 32-bit index bitmasks (totaling at most 96 leaseable objects). A limit of 1024 covers all supported leaseable objects while staying within a reasonable allocation budget. Additionally, switch the object_ids allocation to vmemdup_array_user() and the objects pointer array allocation to kvzalloc_objs(), freeing both with kvfree(). The vmalloc fallback eliminates the high-order allocation warning, while the count limit controls resource usage.

Fixes 62884cd386b8 ("drm: Add four ioctls for managing drm mode object leases [v7]")
Assisted-by Gemini:gemini-4-argon Gemini:gemini-3.8-flash syzbot
Reported-by [email protected]

To: "David Airlie" <[email protected]>
To: <[email protected]>
To: "Maarten Lankhorst" <[email protected]>
To: "Maxime Ripard" <[email protected]>
To: "Simona Vetter" <[email protected]>
To: "Thomas Zimmermann" <[email protected]>
To: "Keith Packard" <[email protected]>
Cc: <[email protected]>

---
v2:
- Define DRM_MAX_LEASE_OBJECTS (1024) and cap object_count to it to bound per-request resource usage based on supported leaseable DRM objects.
- Validate cl->object_count > INT_MAX directly in early checks.
- Update commit description to clarify conversion to signed parameter, allocation budget rationale, and historical warning site line in reported tree.

v1:
https://lore.kernel.org/all/[email protected]/T/
---

drivers/gpu/drm/drm_lease.c +28 -5
index 5d2cf724c..6f05dde38 100644
@@ -3,6 +3,9 @@
  * Copyright © 2017 Keith Packard <[email protected]>
  */
 #include <linux/file.h>
+#include <linux/limits.h>
+#include <linux/slab.h>
+#include <linux/string.h>
 #include <linux/uaccess.h>
 
 #include <drm/drm_auth.h>
@@ -69,6 +72,14 @@
 #define drm_for_each_lessee(lessee, lessor) \
 	list_for_each_entry((lessee), &(lessor)->lessees, lessee_list)
 
+/*
+ * Only CRTCs, connectors, and planes are leaseable, and DRM core bounds
+ * each to 32 objects (32-bit index bitmasks). Cap per-request object_count
+ * to a small finite limit that covers all supported leaseable objects
+ * within a reasonable allocation budget.
+ */
+#define DRM_MAX_LEASE_OBJECTS 1024
+
 static uint64_t drm_lease_idr_object;
 
 struct drm_master *drm_lease_owner(struct drm_master *master)
@@ -386,7 +397,7 @@ static int fill_object_idr(struct drm_device *dev,
 	int ret;
 	bool universal_planes = READ_ONCE(lessor_priv->universal_planes);
 
-	objects = kzalloc_objs(struct drm_mode_object *, object_count);
+	objects = kvzalloc_objs(struct drm_mode_object *, object_count);
 	if (!objects)
 		return -ENOMEM;
 
@@ -462,7 +473,7 @@ static int fill_object_idr(struct drm_device *dev,
 		if (objects[o])
 			drm_mode_object_put(objects[o]);
 	}
-	kfree(objects);
+	kvfree(objects);
 	return ret;
 }
 
@@ -496,6 +507,11 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
 		return -EINVAL;
 	}
 
+	if (cl->object_count > INT_MAX) {
+		drm_dbg_lease(dev, "too many objects: %u\n", cl->object_count);
+		return -EINVAL;
+	}
+
 	lessor = drm_file_get_master(lessor_priv);
 	/* Do not allow sub-leases */
 	if (lessor->lessor) {
@@ -509,8 +525,15 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
 	/* Handle leased objects, if any */
 	idr_init(&leases);
 	if (object_count != 0) {
-		object_ids = memdup_array_user(u64_to_user_ptr(cl->object_ids),
-					       object_count, sizeof(__u32));
+		if (object_count > DRM_MAX_LEASE_OBJECTS) {
+			drm_dbg_lease(dev, "too many objects: %zu\n", object_count);
+			ret = -EINVAL;
+			idr_destroy(&leases);
+			goto out_lessor;
+		}
+
+		object_ids = vmemdup_array_user(u64_to_user_ptr(cl->object_ids),
+						object_count, sizeof(__u32));
 		if (IS_ERR(object_ids)) {
 			ret = PTR_ERR(object_ids);
 			idr_destroy(&leases);
@@ -520,7 +543,7 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
 		/* fill and validate the object idr */
 		ret = fill_object_idr(dev, lessor_priv, &leases,
 				      object_count, object_ids);
-		kfree(object_ids);
+		kvfree(object_ids);
 		if (ret) {
 			drm_dbg_lease(dev, "lease object lookup failed: %i\n", ret);
 			idr_destroy(&leases);

base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907

Signature · 9 lines
This is an AI-generated patch subject to moderation.
Reply with '#syz upstream' to Sign-off the patch as a human author
and send it to the upstream kernel mailing lists.
Reply with '#syz reject' to reject it ('#syz unreject' to undo).

See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
You can comment on the patch as usual, syzbot will try to address
the comments and send a new version of the patch if necessary.
syzbot engineers can be reached at [email protected].
#1 lore raw

The v2 code change is sound in the tested scenario; no code change is requested. The patch still needs corrections to its description of the reported tree and warning trace.

Required changes:
- Correct the reported-tree allocation to kcalloc() at drm_lease.c:389. kzalloc_objs() belongs to the later moderation base.
- Use the reported mm/page_alloc.c:4727 warning, or label the quoted mm/page_alloc.c:5340 stack as coming from a later source revision.

Keep the corrected signed-conversion explanation and the 1024-object rationale. The existing Fixes: tag and recipients need no change.

Testing:
With the same base commit, C reproducer and comparable arm64 configuration, the earlier unpatched test reproduced the selected warning and the v2 test reproduced no crashes. The recorded v2 run used one guest and a 180-second reproducer budget; total elapsed time, including guest startup, was 9 minutes 42 seconds under QEMU TCG.

Count-boundary, zero-object and real-lease tests passed, as did invalid-ID recovery. Allocation-failure unwind was inspected in source but not tested with forced ENOMEM.

Context:
The 1024-object cap addresses the resource-limit request from the v1 review. It exceeds the DRM core limit of 96 distinct leaseable objects and bounds both temporary arrays before user IDs are copied. The signed-range guard remains separate, and both vmalloc-capable arrays keep matching kvfree() cleanup. The new rejection path destroys the temporary IDR and releases the lessor.

Impact assessment:
The selected request requires DRM-master access and demonstrated an allocator warning and failed ioctl. A system configured to panic on warnings could turn that warning into an outage. No memory corruption or disclosure was shown. Sustained memory pressure and concurrency were not tested.

Kind Regards,
BART CHRONOWSKI

Quoted 173 lines · syzbot

In drm_mode_create_lease_ioctl(), user space supplies an object_count and an array of object_ids to be leased. The kernel copies the IDs from user space using memdup_array_user(), and then fill_object_idr() allocates a temporary array of pointers using kzalloc_objs(struct drm_mode_object *, object_count) (at drivers/gpu/drm/drm_lease.c:389 in the reported tree).

Because kzalloc_objs() performs a kmalloc-family allocation with GFP_KERNEL (which lacks __GFP_NOWARN), if user space requests a sufficiently large object_count (for example, 1,000,000 mode objects), the allocation for the pointer array (1,000,000 * 8 = 8,000,000 bytes on 64-bit systems) requires order 11 pages. This exceeds MAX_PAGE_ORDER and triggers a warning in the page allocator at historical warning-site line drm_lease.c:389 in the reported tree:

WARNING: mm/page_alloc.c:5340 at alloc_order_allowed mm/page_alloc.c:5340
[inline]
WARNING: mm/page_alloc.c:5340 at __alloc_frozen_pages_noprof+0xe4/0x640
mm/page_alloc.c:5397
Call trace:
 alloc_order_allowed mm/page_alloc.c:5340 [inline]
 __alloc_frozen_pages_noprof+0xe4/0x640 mm/page_alloc.c:5397
 alloc_pages_mpol+0x1fc/0x3d0 mm/mempolicy.c:2486
 alloc_frozen_pages_noprof+0xe0/0x214 mm/mempolicy.c:2557
 ___kmalloc_large_node+0xf0/0x160 mm/slub.c:5353
 __kmalloc_large_node_noprof+0x2c/0x8c mm/slub.c:5385
 fill_object_idr drivers/gpu/drm/drm_lease.c:389 [inline]
 drm_mode_create_lease_ioctl+0x45c/0x15c8 drivers/gpu/drm/drm_lease.c:521
 drm_ioctl_kernel+0x250/0x328 drivers/gpu/drm/drm_ioctl.c:817
 drm_ioctl+0x65c/0xa58 drivers/gpu/drm/drm_ioctl.c:914
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:597 [inline]

Furthermore, cl->object_count is an unbounded __u32, whereas helper functions such as fill_object_idr() take object_count as a signed integer. A value of cl->object_count greater than INT_MAX is converted to a signed parameter (becoming negative) rather than causing signed arithmetic overflow. In addition, an allocation size exceeding INT_MAX would trigger a WARN_ON_ONCE inside __kvmalloc_node_noprof().

Fix these issues by validating cl->object_count early in drm_mode_create_lease_ioctl() so that values exceeding INT_MAX are rejected with -EINVAL. In addition, cap object_count to DRM_MAX_LEASE_OBJECTS (1024). In DRM, only CRTCs, connectors, and planes are leaseable, and DRM core bounds each to 32 objects via 32-bit index bitmasks (totaling at most 96 leaseable objects). A limit of 1024 covers all supported leaseable objects while staying within a reasonable allocation budget. Additionally, switch the object_ids allocation to vmemdup_array_user() and the objects pointer array allocation to kvzalloc_objs(), freeing both with kvfree(). The vmalloc fallback eliminates the high-order allocation warning, while the count limit controls resource usage.

Fixes 62884cd386b8 ("drm: Add four ioctls for managing drm mode object leases [v7]")
Assisted-by Gemini:gemini-4-argon Gemini:gemini-3.8-flash syzbot
Reported-by [email protected]

To: "David Airlie" <[email protected]>
To: <[email protected]>
To: "Maarten Lankhorst" <[email protected]>
To: "Maxime Ripard" <[email protected]>
To: "Simona Vetter" <[email protected]>
To: "Thomas Zimmermann" <[email protected]>
To: "Keith Packard" <[email protected]>
Cc: <[email protected]>

---
v2:
- Define DRM_MAX_LEASE_OBJECTS (1024) and cap object_count to it to bound per-request resource usage based on supported leaseable DRM objects.
- Validate cl->object_count > INT_MAX directly in early checks.
- Update commit description to clarify conversion to signed parameter, allocation budget rationale, and historical warning site line in reported tree.

v1:
https://lore.kernel.org/all/[email protected]/T/
---

drivers/gpu/drm/drm_lease.c +28 -5
index 5d2cf724c..6f05dde38 100644
@@ -3,6 +3,9 @@
  * Copyright © 2017 Keith Packard <[email protected]>
  */
 #include <linux/file.h>
+#include <linux/limits.h>
+#include <linux/slab.h>
+#include <linux/string.h>
 #include <linux/uaccess.h>
 
 #include <drm/drm_auth.h>
@@ -69,6 +72,14 @@
 #define drm_for_each_lessee(lessee, lessor) \
 	list_for_each_entry((lessee), &(lessor)->lessees, lessee_list)
 
+/*
+ * Only CRTCs, connectors, and planes are leaseable, and DRM core bounds
+ * each to 32 objects (32-bit index bitmasks). Cap per-request object_count
+ * to a small finite limit that covers all supported leaseable objects
+ * within a reasonable allocation budget.
+ */
+#define DRM_MAX_LEASE_OBJECTS 1024
+
 static uint64_t drm_lease_idr_object;
 
 struct drm_master *drm_lease_owner(struct drm_master *master)
@@ -386,7 +397,7 @@ static int fill_object_idr(struct drm_device *dev,
 	int ret;
 	bool universal_planes = READ_ONCE(lessor_priv->universal_planes);
 
-	objects = kzalloc_objs(struct drm_mode_object *, object_count);
+	objects = kvzalloc_objs(struct drm_mode_object *, object_count);
 	if (!objects)
 		return -ENOMEM;
 
@@ -462,7 +473,7 @@ static int fill_object_idr(struct drm_device *dev,
 		if (objects[o])
 			drm_mode_object_put(objects[o]);
 	}
-	kfree(objects);
+	kvfree(objects);
 	return ret;
 }
 
@@ -496,6 +507,11 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
 		return -EINVAL;
 	}
 
+	if (cl->object_count > INT_MAX) {
+		drm_dbg_lease(dev, "too many objects: %u\n", cl->object_count);
+		return -EINVAL;
+	}
+
 	lessor = drm_file_get_master(lessor_priv);
 	/* Do not allow sub-leases */
 	if (lessor->lessor) {
@@ -509,8 +525,15 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
 	/* Handle leased objects, if any */
 	idr_init(&leases);
 	if (object_count != 0) {
-		object_ids = memdup_array_user(u64_to_user_ptr(cl->object_ids),
-					       object_count, sizeof(__u32));
+		if (object_count > DRM_MAX_LEASE_OBJECTS) {
+			drm_dbg_lease(dev, "too many objects: %zu\n", object_count);
+			ret = -EINVAL;
+			idr_destroy(&leases);
+			goto out_lessor;
+		}
+
+		object_ids = vmemdup_array_user(u64_to_user_ptr(cl->object_ids),
+						object_count, sizeof(__u32));
 		if (IS_ERR(object_ids)) {
 			ret = PTR_ERR(object_ids);
 			idr_destroy(&leases);
@@ -520,7 +543,7 @@ int drm_mode_create_lease_ioctl(struct drm_device *dev,
 		/* fill and validate the object idr */
 		ret = fill_object_idr(dev, lessor_priv, &leases,
 				      object_count, object_ids);
-		kfree(object_ids);
+		kvfree(object_ids);
 		if (ret) {
 			drm_dbg_lease(dev, "lease object lookup failed: %i\n", ret);
 			idr_destroy(&leases);

base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907

Signature · 9 lines
This is an AI-generated patch subject to moderation.
Reply with '#syz upstream' to Sign-off the patch as a human author
and send it to the upstream kernel mailing lists.
Reply with '#syz reject' to reject it ('#syz unreject' to undo).

See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
You can comment on the patch as usual, syzbot will try to address
the comments and send a new version of the patch if necessary.
syzbot engineers can be reached at [email protected].