Potential id mismatch when trying to change gem
Subsystems (2)
cover Potential id mismatch when trying to change gem
Kernel fuzzing found a potential WARN when trying to change handle to 0.
To reproduce: gcc reproducer code, modprobe xe, then run a.out.
Reproducer code:
// autogenerated by syzkaller (https://github.com/google/syzkaller)
//#define _GNU_SOURCE
//as git considers hashes to be comments, i had to comment them out. //#include <endian.h>
//#include <stdint.h>
//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
//#include <sys/syscall.h>
//#include <sys/types.h>
//#include <unistd.h>
uint64_t r[2] = {0xffffffffffffffff, 0x0};
int main(void)
{
syscall(__NR_mmap, /*addr=*/0x1ffffffff000ul, /*len=*/0x1000ul, /*prot=*/0ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul, /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/7ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul);
syscall(__NR_mmap, /*addr=*/0x200001000000ul, /*len=*/0x1000ul, /*prot=*/0ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul);
const char* reason;
(void)reason;
intptr_t res = 0;
if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {}// openat$xe_render arguments: [
// fd: const = 0xffffffffffffff9c (8 bytes)
// file: ptr[in, buffer] {
// buffer: {2f 64 65 76 2f 64 72 69 2f 72 65 6e 64 65 72 44 31 32 38 00} (length 0x14)
// }
// flags: open_flags = 0x2000 (4 bytes)
// mode: const = 0x0 (2 bytes)
// ]
// returns fd_xe
memcpy((void*)0x200000000040, "/dev/dri/renderD128\000", 20);
res = syscall(__NR_openat, /*fd=*/0xffffffffffffff9cul, /*file=*/0x200000000040ul, /*flags=FASYNC*/0x2000, /*mode=*/0);
if (res != -1)
r[0] = res;
// ioctl$DRM_IOCTL_XE_GEM_CREATE arguments: [
// fd: fd_xe (resource)
// cmd: const = 0xc0386441 (4 bytes)
// arg: ptr[inout, drm_xe_gem_create] {
// drm_xe_gem_create {
// extensions: nil
// size: int64 = 0x8000 (8 bytes)
// placement: int32 = 0x2 (4 bytes)
// flags: xe_gem_create_flags = 0x1 (4 bytes)
// vm_id: xe_vm_id (resource)
// handle: xe_gem_handle (resource)
// cpu_caching: xe_gem_create_cpu_caching = 0x2 (2 bytes)
// pad: buffer: {00 00 00 00 00 00} (length 0x6)
// reserved: buffer: {00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00} (length 0x10)// }
// }
// ]
*(uint64_t*)0x200000000180 = 0;
*(uint64_t*)0x200000000188 = 0x8000;
*(uint32_t*)0x200000000190 = 2;
*(uint32_t*)0x200000000194 = 1;
*(uint32_t*)0x200000000198 = 0;
*(uint16_t*)0x2000000001a0 = 2;memset((void*)0x2000000001a2, 0, 6);
memset((void*)0x2000000001a8, 0, 16);
res = syscall(__NR_ioctl, /*fd=*/r[0], /*cmd=*/0xc0386441, /*arg=*/0x200000000180ul);
if (res != -1)
r[1] = *(uint32_t*)0x20000000019c;
// ioctl$DRM_IOCTL_PRIME_HANDLE_TO_FD$xe arguments: [
// fd: fd_xe (resource)
// cmd: const = 0xc00c642d (4 bytes)
// arg: ptr[inout, drm_prime_handle_xe$handle_to_fd] {
// drm_prime_handle_xe$handle_to_fd {
// handle: xe_gem_handle (resource)
// flags: dup_flags = 0x80000 (4 bytes)
// fd: xe_prime_fd (resource)// }
// }
// ]
*(uint32_t*)0x2000000001c0 = r[1];
*(uint32_t*)0x2000000001c4 = 0x80000;
syscall(__NR_ioctl, /*fd=*/r[0], /*cmd=*/0xc00c642d, /*arg=*/0x2000000001c0ul);
// ioctl$DRM_IOCTL_GEM_CLOSE$xe arguments: [
// fd: fd_xe (resource)
// cmd: const = 0x400864d2 (4 bytes)
// arg: ptr[in, drm_gem_close_xe] {// drm_gem_close_xe {
// handle: xe_gem_handle (resource)
// pad: const = 0x0 (4 bytes)
// }
// }
// ]
*(uint32_t*)0x200000000000 = r[1];
*(uint32_t*)0x200000000004 = 0; syscall(__NR_ioctl, /*fd=*/r[0], /*cmd=*/0x400864d2, /*arg=*/0x200000000000ul); return 0; }
Warning:
[ 24.293816] WARNING: CPU: 0 PID: 1221 at drivers/gpu/drm/drm_prime.c:223 drm_prime_destroy_file_private+0x43/0x60 [ 24.296087] Modules linked in: [ 24.296730] CPU: 0 UID: 0 PID: 1221 Comm: syz.3.17 Not tainted 6.17.0-xe-fuzz-gdba1fd9754c6-dirty #1 PREEMPT(voluntary) [ 24.298658] Hardware name: QEMU Ubuntu 24.04 PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 [ 24.300461] RIP: 0010:drm_prime_destroy_file_private+0x43/0x60 [ 24.301258] Code: 00 00 fc ff df 48 89 fa 48 c1 ea 03 80 3c 02 00 75 21 48 8b 83 90 00 00 00 48 85 c0 75 06 5b e9 d3 97 e7 fe e8 ce 97 e7 fe 90 <0f> 0b 90 5b e9 c4 97 e7 fe e8 4f 18 22 ff eb d8 66 66 2e 0f 1f 84 [ 24.303214] RSP: 0018:ffff8881044e7d20 EFLAGS: 00010293 [ 24.303752] RAX: 0000000000000000 RBX: ffff888104a3d380 RCX: ffffffff8b4bfb82 [ 24.304470] RDX: ffff888104731bc0 RSI: 0000000000000000 RDI: ffff888104a3d410 [ 24.305190] RBP: ffff888105180000 R08: 0000000000000001 R09: 0000000000000000 [ 24.305897] R10: 0000000000000001 R11: 00000000000002c2 R12: ffff888104a3d2b0 [ 24.306592] R13: ffffffff8b5ddd60 R14: 0000000000000020 R15: dffffc0000000000 [ 24.307296] FS: 0000555579200500(0000) GS:ffff88818c6c9000(0000) knlGS:0000000000000000 [ 24.308093] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 24.308667] CR2: 0000001b2cf63fff CR3: 000000010e141005 CR4: 0000000000f70ef0 [ 24.309370] PKRU: 00000000 [ 24.309654] Call Trace: [ 24.309912] <TASK> [ 24.310134] drm_file_free.part.0+0x727/0xba0 [ 24.310576] ? __pfx_drm_release_noglobal+0x10/0x10 [ 24.311072] drm_close_helper.isra.0+0x11b/0x160 [ 24.311536] drm_release_noglobal+0x85/0xf0 [ 24.311965] __fput+0x3ff/0xb70 [ 24.312292] task_work_run+0x14d/0x240 [ 24.312679] ? __pfx_task_work_run+0x10/0x10 [ 24.313114] ? __pfx___do_sys_close_range+0x10/0x10 [ 24.313604] exit_to_user_mode_loop+0xca/0xd0 [ 24.314062] do_syscall_64+0x26c/0x380 [ 24.314444] entry_SYSCALL_64_after_hwframe+0x77/0x7f [ 24.314962] RIP: 0033:0x7fb973d5fba9 [ 24.315324] Code: ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 40 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 a8 ff ff ff f7 d8 64 89 01 48 [ 24.317116] RSP: 002b:00007fff6fae9a98 EFLAGS: 00000246 ORIG_RAX: 00000000000001b4 [ 24.317863] RAX: 0000000000000000 RBX: 0000000000005e36 RCX: 00007fb973d5fba9 [ 24.318559] RDX: 0000000000000000 RSI: 000000000000001e RDI: 0000000000000003 [ 24.319264] RBP: 0000000000000000 R08: 0000000000000001 R09: 000000046fae9d8f [ 24.319972] R10: 0000001b2c320000 R11: 0000000000000246 R12: 00007fb973fa5fac [ 24.320672] R13: 00007fb973fa5fa0 R14: ffffffffffffffff R15: 0000000000000003 [ 24.321374] </TASK> [ 24.321603] irq event stamp: 4197 [ 24.321950] hardirqs last enabled at (4207): [<ffffffff8a140fa6>] __up_console_sem+0x76/0x80 [ 24.322786] hardirqs last disabled at (4216): [<ffffffff8a140f8b>] __up_console_sem+0x5b/0x80 [ 24.323623] softirqs last enabled at (3882): [<ffffffff89fb7776>] handle_softirqs+0x526/0x770 [ 24.324462] softirqs last disabled at (3873): [<ffffffff89fb7aee>] __irq_exit_rcu+0xbe/0xf0 [ 24.325281] ---[ end trace 0000000000000000 ]--- [ 24.293816] WARNING: CPU: 0 PID: 1221 at drivers/gpu/drm/drm_prime.c:223 drm_prime_destroy_file_private+0x43/0x60 [ 24.296087] Modules linked in: [ 24.296730] CPU: 0 UID: 0 PID: 1221 Comm: syz.3.17 Not tainted 6.17.0-xe-fuzz-gdba1fd9754c6-dirty #1 PREEMPT(voluntary) [ 24.298658] Hardware name: QEMU Ubuntu 24.04 PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 [ 24.300461] RIP: 0010:drm_prime_destroy_file_private+0x43/0x60 [ 24.301258] Code: 00 00 fc ff df 48 89 fa 48 c1 ea 03 80 3c 02 00 75 21 48 8b 83 90 00 00 00 48 85 c0 75 06 5b e9 d3 97 e7 fe e8 ce 97 e7 fe 90 <0f> 0b 90 5b e9 c4 97 e7 fe e8 4f 18 22 ff eb d8 66 66 2e 0f 1f 84 [ 24.303214] RSP: 0018:ffff8881044e7d20 EFLAGS: 00010293 [ 24.303752] RAX: 0000000000000000 RBX: ffff888104a3d380 RCX: ffffffff8b4bfb82 [ 24.304470] RDX: ffff888104731bc0 RSI: 0000000000000000 RDI: ffff888104a3d410 [ 24.305190] RBP: ffff888105180000 R08: 0000000000000001 R09: 0000000000000000 [ 24.305897] R10: 0000000000000001 R11: 00000000000002c2 R12: ffff888104a3d2b0 [ 24.306592] R13: ffffffff8b5ddd60 R14: 0000000000000020 R15: dffffc0000000000 [ 24.307296] FS: 0000555579200500(0000) GS:ffff88818c6c9000(0000) knlGS:0000000000000000 [ 24.308093] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 24.308667] CR2: 0000001b2cf63fff CR3: 000000010e141005 CR4: 0000000000f70ef0 [ 24.309370] PKRU: 00000000 [ 24.309654] Call Trace: [ 24.309912] <TASK> [ 24.310134] drm_file_free.part.0+0x727/0xba0 [ 24.310576] ? __pfx_drm_release_noglobal+0x10/0x10 [ 24.311072] drm_close_helper.isra.0+0x11b/0x160 [ 24.311536] drm_release_noglobal+0x85/0xf0 [ 24.311965] __fput+0x3ff/0xb70 [ 24.312292] task_work_run+0x14d/0x240 [ 24.312679] ? __pfx_task_work_run+0x10/0x10 [ 24.313114] ? __pfx___do_sys_close_range+0x10/0x10 [ 24.313604] exit_to_user_mode_loop+0xca/0xd0 [ 24.314062] do_syscall_64+0x26c/0x380 [ 24.314444] entry_SYSCALL_64_after_hwframe+0x77/0x7f [ 24.314962] RIP: 0033:0x7fb973d5fba9 [ 24.315324] Code: ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 40 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 a8 ff ff ff f7 d8 64 89 01 48 [ 24.317116] RSP: 002b:00007fff6fae9a98 EFLAGS: 00000246 ORIG_RAX: 00000000000001b4 [ 24.317863] RAX: 0000000000000000 RBX: 0000000000005e36 RCX: 00007fb973d5fba9 [ 24.318559] RDX: 0000000000000000 RSI: 000000000000001e RDI: 0000000000000003 [ 24.319264] RBP: 0000000000000000 R08: 0000000000000001 R09: 000000046fae9d8f [ 24.319972] R10: 0000001b2c320000 R11: 0000000000000246 R12: 00007fb973fa5fac [ 24.320672] R13: 00007fb973fa5fa0 R14: ffffffffffffffff R15: 0000000000000003 [ 24.321374] </TASK> [ 24.321603] irq event stamp: 4197 [ 24.321950] hardirqs last enabled at (4207): [<ffffffff8a140fa6>] __up_console_sem+0x76/0x80 [ 24.322786] hardirqs last disabled at (4216): [<ffffffff8a140f8b>] __up_console_sem+0x5b/0x80 [ 24.323623] softirqs last enabled at (3882): [<ffffffff89fb7776>] handle_softirqs+0x526/0x770 [ 24.324462] softirqs last disabled at (3873): [<ffffffff89fb7aee>] __irq_exit_rcu+0xbe/0xf0 [ 24.325281] ---[ end trace 0000000000000000 ]---
---
Jan Sokolowski (1):
drm: disallow setting 0 as new handle in DRM_IOCTL_GEM_CHANGE_HANDLE
drivers/gpu/drm/drm_gem.c | 4 ++++ 1 file changed, 4 insertions(+)
Signature · 1 line
2.43.0
PATCH 1/1 drm: disallow setting 0 as new handle in DRM_IOCTL_GEM_CHANGE_HANDLE
drm_file's object_idr uses 1 as base value, which can cause id mismatch when trying to use DRM_IOCTL_GEM_CHANGE_HANDLE to change id from 1 to 0.
Disallow 0 as new handle in that ioctl.
--- drivers/gpu/drm/drm_gem.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/gpu/drm/drm_gem.c +4 -0
@@ -975,6 +975,10 @@ int drm_gem_change_handle_ioctl(struct drm_device *dev, void *data,if (args->handle == args->new_handle)return 0 ; +/* As the idr base is 1, trying to set handle 0 will create id mismatch */ +if (args->new_handle ==0 ) +return 0 ; +mutex_lock (&file_priv->prime.lock);spin_lock (&file_priv->table_lock);
Signature · 1 line
2.43.0
Re: [PATCH 1/1] drm: disallow setting 0 as new handle in DRM_IOCTL_GEM_CHANGE_HANDLE
Quoted 26 lines · Jan Sokolowski
drm_file's
object_idruses 1 as base value, which can cause id mismatch when trying to useDRM_IOCTL_GEM_CHANGE_HANDLEto change id from 1 to 0.Disallow 0 as new handle in that ioctl.
Fixes 53096728b891 ("drm: Add DRM prime interface to reassign GEM handle")Signed-off-by Jan Sokolowski <[email protected]>Cc David Francis <[email protected]>Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Christian König" <[email protected]>Cc Felix Kuehling <[email protected]>Cc Lucas De Marchi <[email protected]>--- drivers/gpu/drm/drm_gem.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/gpu/drm/drm_gem.c +0
-0
drivers/gpu/drm/drm_gem.c int drm_gem_change_handle_ioctl(struct drm_device *dev, void *data, · line 980
Quoted 6 lines
drivers/gpu/drm/drm_gem.c +3
-0@@ -975,6 +975,10 @@ int drm_gem_change_handle_ioctl(struct drm_device *dev, void *data,if (args->handle == args->new_handle)return 0 ; +/* As the idr base is 1, trying to set handle 0 will create id mismatch */ +if (args->new_handle ==0 ) +return 0 ;
That would need to return -EINVAl or some other error code.
But I'm wondering why that is necessary at all? Doesn't idr_alloc() return an error when you try to allocate handle 0?
Regards,
Christian.
Quoted 4 lines
drivers/gpu/drm/drm_gem.c +1
-0+mutex_lock (&file_priv->prime.lock);spin_lock (&file_priv->table_lock);
RE: [PATCH 1/1] drm: disallow setting 0 as new handle in DRM_IOCTL_GEM_CHANGE_HANDLE
Quoted 46 lines
-----Original Message-----
From: Christian König <[email protected]>
Sent: Tuesday, November 25, 2025 1:54 PM
To: Sokolowski, Jan <[email protected]>; dri- [email protected]
Cc: David Francis <[email protected]>; Maarten Lankhorst <[email protected]>; Maxime Ripard <[email protected]>; Thomas Zimmermann <[email protected]>; David Airlie <[email protected]>; Simona Vetter <[email protected]>; Felix Kuehling <[email protected]>; De Marchi, Lucas <[email protected]>
Subject: Re: [PATCH 1/1] drm: disallow setting 0 as new handle inDRM_IOCTL_GEM_CHANGE_HANDLEQuoted 7 lines · Jan Sokolowski
drm_file's
object_idruses 1 as base value, which can cause id mismatch when trying to useDRM_IOCTL_GEM_CHANGE_HANDLEto change id from 1 to 0.Disallow 0 as new handle in that ioctl.
Fixes 53096728b891 ("drm: Add DRM prime interface to reassign GEMhandle")
drivers/gpu/drm/drm_gem.c int drm_gem_change_handle_ioctl(struct
Quoted 19 lines
Signed-off-by Jan Sokolowski <[email protected]>Cc David Francis <[email protected]>Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Christian König" <[email protected]>Cc Felix Kuehling <[email protected]>Cc Lucas De Marchi <[email protected]>--- drivers/gpu/drm/drm_gem.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/gpu/drm/drm_gem.c +0
-0@@ -975,6 +975,10 @@ int drm_gem_change_handle_ioctl(struct
drm_device*dev, void *data,Quoted 3 lines
drivers/gpu/drm/drm_gem.c +0
-0if (args->handle == args->new_handle)return 0 ;
Quoted 6 lines
drivers/gpu/drm/drm_gem.c int drm_gem_change_handle_ioctl(struct · line 978
Quoted 1 line
drivers/gpu/drm/drm_gem.c +1
-0+/* As the idr base is 1, trying to set handle 0 will create id mismatch */
drivers/gpu/drm/drm_gem.c int drm_gem_change_handle_ioctl(struct drm_device *dev, void *data, · line 980
Quoted 2 lines
drivers/gpu/drm/drm_gem.c +2
-0+if (args->new_handle ==0 ) +return 0 ;That would need to return -EINVAl or some other error code.
Ok, will change that in next version.
Quoted 3 lines
But I'm wondering why that is necessary at all? Doesn't
idr_alloc()return an error when you try to allocate handle 0?
It appears that idr_alloc doesn't return any errors in this scenario, otherwise we'd goto out_unlock straight away.
Quoted 8 lines
Regards,
Christian.Quoted 4 lines
drivers/gpu/drm/drm_gem.c +1
-0+mutex_lock (&file_priv->prime.lock);spin_lock (&file_priv->table_lock);
Re: [PATCH 1/1] drm: disallow setting 0 as new handle in DRM_IOCTL_GEM_CHANGE_HANDLE
Quoted 53 lines · Jan
Quoted 46 lines
-----Original Message-----
From: Christian König <[email protected]>
Sent: Tuesday, November 25, 2025 1:54 PM
To: Sokolowski, Jan <[email protected]>; dri- [email protected]
Cc: David Francis <[email protected]>; Maarten Lankhorst <[email protected]>; Maxime Ripard <[email protected]>; Thomas Zimmermann <[email protected]>; David Airlie <[email protected]>; Simona Vetter <[email protected]>; Felix Kuehling <[email protected]>; De Marchi, Lucas <[email protected]>
Subject: Re: [PATCH 1/1] drm: disallow setting 0 as new handle inDRM_IOCTL_GEM_CHANGE_HANDLEQuoted 7 lines · Jan Sokolowski
drm_file's
object_idruses 1 as base value, which can cause id mismatch when trying to useDRM_IOCTL_GEM_CHANGE_HANDLEto change id from 1 to 0.Disallow 0 as new handle in that ioctl.
Fixes 53096728b891 ("drm: Add DRM prime interface to reassign GEMhandle")
drivers/gpu/drm/drm_gem.c int drm_gem_change_handle_ioctl(struct
Quoted 19 lines
Signed-off-by Jan Sokolowski <[email protected]>Cc David Francis <[email protected]>Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Christian König" <[email protected]>Cc Felix Kuehling <[email protected]>Cc Lucas De Marchi <[email protected]>--- drivers/gpu/drm/drm_gem.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/gpu/drm/drm_gem.c +0
-0@@ -975,6 +975,10 @@ int drm_gem_change_handle_ioctl(struct
drm_device*dev, void *data,Quoted 3 lines
drivers/gpu/drm/drm_gem.c +0
-0if (args->handle == args->new_handle)return 0 ;Quoted 6 lines
drivers/gpu/drm/drm_gem.c int drm_gem_change_handle_ioctl(struct · line 978
Quoted 1 line
drivers/gpu/drm/drm_gem.c +1
-0+/* As the idr base is 1, trying to set handle 0 will create id mismatch */
drivers/gpu/drm/drm_gem.c int drm_gem_change_handle_ioctl(struct drm_device *dev, void *data, · line 980
Quoted 2 lines
drivers/gpu/drm/drm_gem.c +2
-0+if (args->new_handle ==0 ) +return 0 ;That would need to return -EINVAl or some other error code.
Quoted 7 lines
Ok, will change that in next version.
Quoted 3 lines
But I'm wondering why that is necessary at all? Doesn't
idr_alloc()return an error when you try to allocate handle 0?It appears that
idr_allocdoesn't return any errors in this scenario, otherwise we'd gotoout_unlockstraight away.
Mhm, that is a bit misleading. We intentionally initialized the idr so that it starts at 1. Maybe idr_alloc() should be fixed instead.
But that is clearly a much larger change, with the return code fixed feel free to add Reviewed-by: Christian König <[email protected]> to this patch here.
Adding a CC: stable tag sound appropriate as well.
Regards,
Christian.
Quoted 10 lines
Quoted 9 lines
Regards,
Christian.Quoted 4 lines
drivers/gpu/drm/drm_gem.c +1
-0+mutex_lock (&file_priv->prime.lock);spin_lock (&file_priv->table_lock);
RE: [PATCH 1/1] drm: disallow setting 0 as new handle in DRM_IOCTL_GEM_CHANGE_HANDLE
Quoted 74 lines
-----Original Message-----
From: Christian König <[email protected]>
Sent: Tuesday, November 25, 2025 2:23 PM
To: Sokolowski, Jan <[email protected]>; dri- [email protected]
Cc: David Francis <[email protected]>; Maarten Lankhorst <[email protected]>; Maxime Ripard <[email protected]>; Thomas Zimmermann <[email protected]>; David Airlie <[email protected]>; Simona Vetter <[email protected]>; Felix Kuehling <[email protected]>; De Marchi, Lucas <[email protected]>
Subject: Re: [PATCH 1/1] drm: disallow setting 0 as new handle inDRM_IOCTL_GEM_CHANGE_HANDLEQuoted 46 lines · Jan
Quoted 46 lines
-----Original Message-----
From: Christian König <[email protected]>
Sent: Tuesday, November 25, 2025 1:54 PM
To: Sokolowski, Jan <[email protected]>; dri- [email protected]
Cc: David Francis <[email protected]>; Maarten Lankhorst <[email protected]>; Maxime Ripard <[email protected]>; Thomas Zimmermann <[email protected]>; David Airlie <[email protected]>; Simona Vetter <[email protected]>; Felix Kuehling <[email protected]>; De Marchi, Lucas <[email protected]>
Subject: Re: [PATCH 1/1] drm: disallow setting 0 as new handle inDRM_IOCTL_GEM_CHANGE_HANDLEQuoted 7 lines · Jan Sokolowski
drm_file's
object_idruses 1 as base value, which can cause id mismatch when trying to useDRM_IOCTL_GEM_CHANGE_HANDLEto change id from 1 to 0.Disallow 0 as new handle in that ioctl.
Fixes 53096728b891 ("drm: Add DRM prime interface to reassign GEMhandle")
drivers/gpu/drm/drm_gem.c int drm_gem_change_handle_ioctl(struct
Quoted 19 lines
Signed-off-by Jan Sokolowski <[email protected]>Cc David Francis <[email protected]>Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Christian König" <[email protected]>Cc Felix Kuehling <[email protected]>Cc Lucas De Marchi <[email protected]>--- drivers/gpu/drm/drm_gem.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/gpu/drm/drm_gem.c +0
-0@@ -975,6 +975,10 @@ int drm_gem_change_handle_ioctl(struct
drm_device*dev, void *data,Quoted 3 lines
drivers/gpu/drm/drm_gem.c +0
-0if (args->handle == args->new_handle)return 0 ;Quoted 11 lines
Quoted 6 lines
drivers/gpu/drm/drm_gem.c int drm_gem_change_handle_ioctl(struct · line 978
Quoted 1 line
drivers/gpu/drm/drm_gem.c +1
-0+/* As the idr base is 1, trying to set handle 0 will create id mismatch */
drivers/gpu/drm/drm_gem.c int drm_gem_change_handle_ioctl(struct drm_device *dev, void *data, · line 980
Quoted 2 lines
drivers/gpu/drm/drm_gem.c +2
-0+if (args->new_handle ==0 ) +return 0 ;That would need to return -EINVAl or some other error code.
Ok, will change that in next version.
Quoted 2 lines
But I'm wondering why that is necessary at all? Doesn't
idr_alloc()return
Quoted 8 lines
an
Quoted 3 lines
Quoted 1 line
error when you try to allocate handle 0?
It appears that
idr_allocdoesn't return any errors in this scenario,otherwise we'd goto
out_unlockstraight away.Mhm, that is a bit misleading. We intentionally initialized the idr so that it starts at 1. Maybe
idr_alloc()should be fixed instead.
I've checked what idr_alloc does, and it looks like it'll return 2 in this case, which is the next reserved id (0 is reserved, so it goes to 1) + base (1). Maybe the solution would be, if idr_alloc returns a value other than args->new_handle, then
a) bail out
b) new_handle = idr_alloc, and inform user that the assignment was changed to another handle.
As idr is used in many other places, I don't think that's feasible or easy to fix that.
Quoted 20 lines
But that is clearly a much larger change, with the return code fixed feel free to add Reviewed-by: Christian König <[email protected]> to this patch here.
Adding a CC: stable tag sound appropriate as well.
Regards,
Christian.Quoted 10 lines
Quoted 9 lines
Regards,
Christian.Quoted 4 lines
drivers/gpu/drm/drm_gem.c +1
-0+mutex_lock (&file_priv->prime.lock);spin_lock (&file_priv->table_lock);
Re: [PATCH 1/1] drm: disallow setting 0 as new handle in DRM_IOCTL_GEM_CHANGE_HANDLE
Quoted 77 lines · Jan
Quoted 77 lines
-----Original Message-----
From: Christian König <[email protected]>
Sent: Tuesday, November 25, 2025 2:23 PM
To: Sokolowski, Jan <[email protected]>; dri- [email protected]
Cc: David Francis <[email protected]>; Maarten Lankhorst <[email protected]>; Maxime Ripard <[email protected]>; Thomas Zimmermann <[email protected]>; David Airlie <[email protected]>; Simona Vetter <[email protected]>; Felix Kuehling <[email protected]>; De Marchi, Lucas <[email protected]>
Subject: Re: [PATCH 1/1] drm: disallow setting 0 as new handle inDRM_IOCTL_GEM_CHANGE_HANDLEQuoted 46 lines · Jan
Quoted 46 lines
-----Original Message-----
From: Christian König <[email protected]>
Sent: Tuesday, November 25, 2025 1:54 PM
To: Sokolowski, Jan <[email protected]>; dri- [email protected]
Cc: David Francis <[email protected]>; Maarten Lankhorst <[email protected]>; Maxime Ripard <[email protected]>; Thomas Zimmermann <[email protected]>; David Airlie <[email protected]>; Simona Vetter <[email protected]>; Felix Kuehling <[email protected]>; De Marchi, Lucas <[email protected]>
Subject: Re: [PATCH 1/1] drm: disallow setting 0 as new handle inDRM_IOCTL_GEM_CHANGE_HANDLEQuoted 7 lines · Jan Sokolowski
drm_file's
object_idruses 1 as base value, which can cause id mismatch when trying to useDRM_IOCTL_GEM_CHANGE_HANDLEto change id from 1 to 0.Disallow 0 as new handle in that ioctl.
Fixes 53096728b891 ("drm: Add DRM prime interface to reassign GEMhandle")
drivers/gpu/drm/drm_gem.c int drm_gem_change_handle_ioctl(struct
Quoted 19 lines
Signed-off-by Jan Sokolowski <[email protected]>Cc David Francis <[email protected]>Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Christian König" <[email protected]>Cc Felix Kuehling <[email protected]>Cc Lucas De Marchi <[email protected]>--- drivers/gpu/drm/drm_gem.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/gpu/drm/drm_gem.c +0
-0@@ -975,6 +975,10 @@ int drm_gem_change_handle_ioctl(struct
drm_device*dev, void *data,Quoted 3 lines
drivers/gpu/drm/drm_gem.c +0
-0if (args->handle == args->new_handle)return 0 ;Quoted 11 lines
Quoted 6 lines
drivers/gpu/drm/drm_gem.c int drm_gem_change_handle_ioctl(struct · line 978
Quoted 1 line
drivers/gpu/drm/drm_gem.c +1
-0+/* As the idr base is 1, trying to set handle 0 will create id mismatch */
drivers/gpu/drm/drm_gem.c int drm_gem_change_handle_ioctl(struct drm_device *dev, void *data, · line 980
Quoted 2 lines
drivers/gpu/drm/drm_gem.c +2
-0+if (args->new_handle ==0 ) +return 0 ;That would need to return -EINVAl or some other error code.
Ok, will change that in next version.
Quoted 2 lines
But I'm wondering why that is necessary at all? Doesn't
idr_alloc()returnan
Quoted 2 lines
Quoted 1 line
error when you try to allocate handle 0?
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
It appears that
idr_allocdoesn't return any errors in this scenario,otherwise we'd goto
out_unlockstraight away.Mhm, that is a bit misleading. We intentionally initialized the idr so that it starts at 1. Maybe
idr_alloc()should be fixed instead.I've checked what
idr_allocdoes, and it looks like it'll return 2 in this case,
Well that would absolute clearly be a bug in idr.
The start parameter is documented as the minimum (inclusive) value returned and the end parameter is documented as the maximum (exclusive) value returned.
So if you ask for value 0 and get 2 instead that is clearly not the documented behavior.
Something fishy is going on here. Please investigate what is actually happening and why.
My educated guess is that the base is just ignored by idr_alloc(), that's a bit surprising but at least not otherwise documented.
Regards,
Christian.
Quoted 28 lines
which is the next reserved id (0 is reserved, so it goes to 1) + base (1). Maybe the solution would be, if
idr_allocreturns a value other thanargs->new_handle, then
a) bail out
b)new_handle=idr_alloc, and inform user that the assignment was changed to another handle.As idr is used in many other places, I don't think that's feasible or easy to fix that.
Quoted 20 lines
But that is clearly a much larger change, with the return code fixed feel free to add Reviewed-by: Christian König <[email protected]> to this patch here.
Adding a CC: stable tag sound appropriate as well.
Regards,
Christian.Quoted 10 lines
Quoted 9 lines
Regards,
Christian.Quoted 4 lines
drivers/gpu/drm/drm_gem.c +1
-0+mutex_lock (&file_priv->prime.lock);spin_lock (&file_priv->table_lock);
RE: [PATCH 1/1] drm: disallow setting 0 as new handle in DRM_IOCTL_GEM_CHANGE_HANDLE
Quoted 100 lines
-----Original Message-----
From: Christian König <[email protected]>
Sent: Tuesday, November 25, 2025 3:11 PM
To: Sokolowski, Jan <[email protected]>; dri- [email protected]
Cc: David Francis <[email protected]>; Maarten Lankhorst <[email protected]>; Maxime Ripard <[email protected]>; Thomas Zimmermann <[email protected]>; David Airlie <[email protected]>; Simona Vetter <[email protected]>; Felix Kuehling <[email protected]>; De Marchi, Lucas <[email protected]>
Subject: Re: [PATCH 1/1] drm: disallow setting 0 as new handle inDRM_IOCTL_GEM_CHANGE_HANDLEQuoted 17 lines · Jan
Quoted 17 lines
-----Original Message-----
From: Christian König <[email protected]>
Sent: Tuesday, November 25, 2025 2:23 PM
To: Sokolowski, Jan <[email protected]>; dri- [email protected]
Cc: David Francis <[email protected]>; Maarten Lankhorst <[email protected]>; Maxime Ripard <[email protected]>; Thomas Zimmermann <[email protected]>; David Airlie <[email protected]>; Simona Vetter <[email protected]>; Felix Kuehling <[email protected]>; De Marchi, Lucas <[email protected]>
Subject: Re: [PATCH 1/1] drm: disallow setting 0 as new handle inDRM_IOCTL_GEM_CHANGE_HANDLEOn 11/25/25 14:02, Sokolowski, Jan wrote:
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
-----Original Message-----
From: Christian König <[email protected]>
Sent: Tuesday, November 25, 2025 1:54 PM
To: Sokolowski, Jan <[email protected]>; dri- [email protected]
Cc: David Francis <[email protected]>; Maarten Lankhorst <[email protected]>; Maxime Ripard <[email protected]>; Thomas ZimmermannQuoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
David Airlie <[email protected]>; Simona Vetter <[email protected]>;
Felix
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Kuehling <[email protected]>; De Marchi, Lucas <[email protected]>
Subject: Re: [PATCH 1/1] drm: disallow setting 0 as new handle inDRM_IOCTL_GEM_CHANGE_HANDLEQuoted 7 lines · Jan Sokolowski
drm_file's
object_idruses 1 as base value, which can cause id mismatch when trying to useDRM_IOCTL_GEM_CHANGE_HANDLEto change id from 1 to 0.Disallow 0 as new handle in that ioctl.
Fixes 53096728b891 ("drm: Add DRM prime interface to reassign GEMhandle")
Quoted 14 lines
Signed-off-by Jan Sokolowski <[email protected]>Cc David Francis <[email protected]>Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Christian König" <[email protected]>Cc Felix Kuehling <[email protected]>Cc Lucas De Marchi <[email protected]>--- drivers/gpu/drm/drm_gem.c | 4 ++++ 1 file changed, 4 insertions(+)Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +0
-0
b/drivers/gpu/drm/drm_gem.cQuoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
drivers/gpu/drm/drm_gem.c int drm_gem_change_handle_ioctl(struct
Quoted 4 lines
index
68168d58a7c8..2a49a8e396fa100644drivers/gpu/drm/drm_gem.c +0
-0@@ -975,6 +975,10 @@ int drm_gem_change_handle_ioctl(struct
drm_device*dev, void *data,drivers/gpu/drm/drm_gem.c int drm_gem_change_handle_ioctl(struct drm_device *dev, void *data,
Quoted 4 lines
drivers/gpu/drm/drm_gem.c +1
-0if (args->handle == args->new_handle)return 0 ; +/* As the idr base is 1, trying to set handle 0 will create id mismatch
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 5 lines
*/
drivers/gpu/drm/drm_gem.c int drm_gem_change_handle_ioctl(struct drm_device *dev, void *data, · line 980
Quoted 2 lines
drivers/gpu/drm/drm_gem.c +2
-0+if (args->new_handle ==0 ) +return 0 ;That would need to return -EINVAl or some other error code.
Ok, will change that in next version.
Quoted 2 lines
But I'm wondering why that is necessary at all? Doesn't
idr_alloc()return
Quoted 4 lines
Quoted 4 lines
an
Quoted 3 lines
Quoted 1 line
error when you try to allocate handle 0?
It appears that
idr_allocdoesn't return any errors in this scenario,
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
otherwise we'd goto
out_unlockstraight away.Mhm, that is a bit misleading. We intentionally initialized the idr so that it starts at 1. Maybe
idr_alloc()should be fixed instead.I've checked what
idr_allocdoes, and it looks like it'll return 2 in this case,Well that would absolute clearly be a bug in idr.
I sent a RFC patch to linux kernel at vger ([RFC PATCH 0/1] IDR fix for potential id mismatch), hopefully that's the proper place for that patch.
I'll wait for it to be reviewed before sending any further changes to drm_gem.c (as that patch would fix the warn anyway)
Thanks for the review!
Quoted 48 lines
The start parameter is documented as the minimum (inclusive) value returned and the end parameter is documented as the maximum (exclusive) value returned.
So if you ask for value 0 and get 2 instead that is clearly not the documented behavior.
Something fishy is going on here. Please investigate what is actually happening and why.
My educated guess is that the base is just ignored by
idr_alloc(), that's a bit surprising but at least not otherwise documented.Regards,
Christian.Quoted 5 lines
which is the next reserved id (0 is reserved, so it goes to 1) + base (1). Maybe the solution would be, if
idr_allocreturns a value other than args-new_handle, then
a) bail out
b)new_handle=idr_alloc, and inform user that the assignment waschanged to another handle.
Quoted 2 lines
As idr is used in many other places, I don't think that's feasible or easy to
fix that.
Quoted 3 lines
Quoted 3 lines
But that is clearly a much larger change, with the return code fixed feel
free
Quoted 19 lines
Quoted 18 lines
to add Reviewed-by: Christian König <[email protected]> to this patch here.
Adding a CC: stable tag sound appropriate as well.
Regards,
Christian.Quoted 10 lines
Quoted 9 lines
Regards,
Christian.Quoted 4 lines
drivers/gpu/drm/drm_gem.c +1
-0+mutex_lock (&file_priv->prime.lock);spin_lock (&file_priv->table_lock);