| 0/0 |
2026/02/06 12:22 |
flow |
moderation |
4m
Results: map[Actionable:true Confident:true Explanation:The bug report is **actionable** and **self-consistent**.
### Analysis of the report:
1. **Three Stack Traces**: The report contains all three required stack traces:
* **Bad Access Stack**: Shows a use-after-free in `raw_event_queue_add` (called via `gadget_disconnect`) when trying to acquire a spinlock.
* **Allocation Stack**: Shows the object (`struct raw_dev`) was allocated in `raw_open` via `dev_new`.
* **Free Stack**: Shows the object was freed in `raw_release` via `kref_put`.
2. **Consistency**:
* All three stacks involve the same object type (`struct raw_dev`) and the same source file (`drivers/usb/gadget/legacy/raw_gadget.c`).
* The buggy address (`ffff8880524608b8`) is at an offset (~2232 bytes) that corresponds to the `queue` member (specifically its `lock` field) within `struct raw_dev`, as confirmed by the struct layout.
* The crash happens in a workqueue (`usb_hub_wq`), which is consistent with how `dummy_hcd` (the emulated USB controller) handles port events like disconnects.
3. **Root Cause Hypothesis**:
The bug is a race condition between the closing of the `raw_gadget` file descriptor and a USB disconnect event.
* When the file descriptor is closed, `raw_release` is called. It unregisters the gadget driver and drops the reference count of the `raw_dev` object.
* Simultaneously, a disconnect event (triggered here by a port power cycle in `dummy_hcd`) calls the `gadget_disconnect` callback.
* If `dummy_hcd` or the UDC core does not properly synchronize the callback with the unregistration process, the callback may be invoked or continue running after `raw_release` has already freed the `raw_dev` structure.
* In this specific case, `gadget_disconnect` attempts to access `dev->queue.lock`, leading to a use-after-free because `dev` has been freed by `raw_release`.
4. **Actionability**:
The report provides sufficient information to identify the affected subsystem (`usb/gadget/legacy/raw_gadget.c`), the involved structures, and the likely racing execution paths. A developer can investigate why the synchronization between `usb_gadget_unregister_driver` and the `dummy_hcd` callbacks failed to prevent this access.
Conclusion: The report is actionable and provides a clear path for debugging the race condition.
]
|
| 1/1 |
2026/02/06 12:22 |
action |
kernel-checkouter |
0m
Results: map[KernelSrc:/usr/local/google/home/dvyukov/syzkaller/agent/workdir/cache/src/cb24f0835974de80148d21636f6b653d93682021]
|
| 2/1 |
2026/02/06 12:22 |
action |
kernel-builder |
0m
Results: map[KernelObj:/usr/local/google/home/dvyukov/syzkaller/agent/workdir/cache/build/8b6cc6bea4e701bb6874027e4ebf14f08b115ae5]
|
| 3/1 |
2026/02/06 12:22 |
action |
codesearch-prepare |
0m
Results: map[Index:codesearch-index]
|
| 4/1 |
2026/02/06 12:22 |
agent |
expert |
4m
Model:
Results: map[Actionable:true Confident:true]
Instruction:
You are an experienced Linux kernel developer tasked with determining if the given kernel bug
report is actionable or not. Actionable means that it contains enough info to root cause
the underlying bug, and that the report is self-consistent and makes sense, rather than
e.g. a one-off nonsensical crash induced by a previous memory corruption.
The bug report is about a use-after-free bug generated by KASAN tool.
It should contain 3 stack traces: the bad memory access stack, the heap block allocation stack,
and the heap block free stack. If the report does not contain 3 stacks, it's not actionable.
All 3 stack traces should be related to the same object type,
and usually be in the same kernel subsystem (at least leaf stack frames).
An example of an actionable and consistent report would be: first access stack relates
to an access to a field of struct Foo, allocation/free stacks relate to allocation/free
of the struct Foo.
In inconsistent/nonsensical reports an access may be to a struct Foo, but allocation
stack allocates a different structure in a different subsystem.
Look for other suspicious signals/inconsistencies that can make this report hard to
debug/understand.
In the final reply explain why you think the report is self-consistent and actionable,
or why it's inconsistent and/or not actionable.
Use the provided tools to confirm any assumptions, variables/fields being accessed, etc.
In particular, don't make assumptions about the kernel source code,
use codesearch tools to read the actual source code.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt:
The bug report is:
usb 5-1: device descriptor read/64, error -71
usb usb5-port1: attempt power cycle
==================================================================
BUG: KASAN: slab-use-after-free in __raw_spin_lock_irqsave include/linux/spinlock_api_smp.h:110 [inline]
BUG: KASAN: slab-use-after-free in _raw_spin_lock_irqsave+0x40/0x60 kernel/locking/spinlock.c:162
Read of size 1 at addr ffff8880524608b8 by task kworker/1:10/5982
CPU: 1 UID: 0 PID: 5982 Comm: kworker/1:10 Not tainted syzkaller #0 PREEMPT_{RT,(full)}
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/25/2025
Workqueue: usb_hub_wq hub_event
Call Trace:
<TASK>
dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120
print_address_description mm/kasan/report.c:378 [inline]
print_report+0xca/0x240 mm/kasan/report.c:482
kasan_report+0x118/0x150 mm/kasan/report.c:595
__kasan_check_byte+0x2a/0x40 mm/kasan/common.c:574
kasan_check_byte include/linux/kasan.h:402 [inline]
lock_acquire+0x84/0x340 kernel/locking/lockdep.c:5842
__raw_spin_lock_irqsave include/linux/spinlock_api_smp.h:110 [inline]
_raw_spin_lock_irqsave+0x40/0x60 kernel/locking/spinlock.c:162
rtlock_slowlock kernel/locking/rtmutex.c:1894 [inline]
rtlock_lock kernel/locking/spinlock_rt.c:43 [inline]
__rt_spin_lock kernel/locking/spinlock_rt.c:49 [inline]
rt_spin_lock+0x14a/0x3e0 kernel/locking/spinlock_rt.c:57
spin_lock include/linux/spinlock_rt.h:44 [inline]
raw_event_queue_add drivers/usb/gadget/legacy/raw_gadget.c:68 [inline]
raw_queue_event+0x39/0x2e0 drivers/usb/gadget/legacy/raw_gadget.c:240
gadget_disconnect+0x51/0xf0 drivers/usb/gadget/legacy/raw_gadget.c:388
set_link_state+0xc0a/0x1220 drivers/usb/gadget/udc/dummy_hcd.c:471
dummy_hub_control+0xafc/0x1a30 drivers/usb/gadget/udc/dummy_hcd.c:-1
rh_call_control drivers/usb/core/hcd.c:656 [inline]
rh_urb_enqueue drivers/usb/core/hcd.c:821 [inline]
usb_hcd_submit_urb+0xdbe/0x1b60 drivers/usb/core/hcd.c:1542
usb_start_wait_urb+0x115/0x4f0 drivers/usb/core/message.c:59
usb_internal_control_msg drivers/usb/core/message.c:103 [inline]
usb_control_msg+0x232/0x3e0 drivers/usb/core/message.c:154
usb_clear_port_feature drivers/usb/core/hub.c:456 [inline]
usb_hub_set_port_power drivers/usb/core/hub.c:894 [inline]
hub_port_connect drivers/usb/core/hub.c:5607 [inline]
hub_port_connect_change drivers/usb/core/hub.c:5707 [inline]
port_event drivers/usb/core/hub.c:5871 [inline]
hub_event+0x2d55/0x4f30 drivers/usb/core/hub.c:5953
process_one_work kernel/workqueue.c:3257 [inline]
process_scheduled_works+0xad1/0x1770 kernel/workqueue.c:3340
worker_thread+0x8a0/0xda0 kernel/workqueue.c:3421
kthread+0x711/0x8a0 kernel/kthread.c:463
ret_from_fork+0x510/0xa50 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:246
</TASK>
Allocated by task 6293:
kasan_save_stack mm/kasan/common.c:57 [inline]
kasan_save_track+0x3e/0x80 mm/kasan/common.c:78
poison_kmalloc_redzone mm/kasan/common.c:398 [inline]
__kasan_kmalloc+0x93/0xb0 mm/kasan/common.c:415
kasan_kmalloc include/linux/kasan.h:263 [inline]
__kmalloc_cache_noprof+0x1fb/0x6d0 mm/slub.c:5776
kmalloc_noprof include/linux/slab.h:957 [inline]
kzalloc_noprof include/linux/slab.h:1094 [inline]
dev_new drivers/usb/gadget/legacy/raw_gadget.c:193 [inline]
raw_open+0x8d/0x530 drivers/usb/gadget/legacy/raw_gadget.c:435
misc_open+0x2de/0x350 drivers/char/misc.c:163
chrdev_open+0x4cf/0x5e0 fs/char_dev.c:414
do_dentry_open+0x7d0/0x1270 fs/open.c:962
vfs_open+0x3b/0x350 fs/open.c:1094
do_open fs/namei.c:4628 [inline]
path_openat+0x342a/0x3df0 fs/namei.c:4787
do_filp_open+0x1fa/0x410 fs/namei.c:4814
do_sys_openat2+0x121/0x200 fs/open.c:1430
do_sys_open fs/open.c:1436 [inline]
__do_sys_openat fs/open.c:1452 [inline]
__se_sys_openat fs/open.c:1447 [inline]
__x64_sys_openat+0x138/0x170 fs/open.c:1447
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xec/0xf80 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
Freed by task 6293:
kasan_save_stack mm/kasan/common.c:57 [inline]
kasan_save_track+0x3e/0x80 mm/kasan/common.c:78
kasan_save_free_info+0x46/0x50 mm/kasan/generic.c:584
poison_slab_object mm/kasan/common.c:253 [inline]
__kasan_slab_free+0x5c/0x80 mm/kasan/common.c:285
kasan_slab_free include/linux/kasan.h:235 [inline]
slab_free_hook mm/slub.c:2540 [inline]
slab_free mm/slub.c:6670 [inline]
kfree+0x1bd/0x900 mm/slub.c:6878
kref_put include/linux/kref.h:65 [inline]
raw_release+0x191/0x260 drivers/usb/gadget/legacy/raw_gadget.c:474
__fput+0x45b/0xa80 fs/file_table.c:468
task_work_run+0x1d4/0x260 kernel/task_work.c:233
exit_task_work include/linux/task_work.h:40 [inline]
do_exit+0x694/0x22f0 kernel/exit.c:971
do_group_exit+0x21c/0x2d0 kernel/exit.c:1112
get_signal+0x125d/0x1310 kernel/signal.c:3034
arch_do_signal_or_restart+0x9a/0x7a0 arch/x86/kernel/signal.c:337
__exit_to_user_mode_loop kernel/entry/common.c:41 [inline]
exit_to_user_mode_loop+0x87/0x4e0 kernel/entry/common.c:75
__exit_to_user_mode_prepare include/linux/irq-entry-common.h:226 [inline]
syscall_exit_to_user_mode_prepare include/linux/irq-entry-common.h:256 [inline]
syscall_exit_to_user_mode_work include/linux/entry-common.h:159 [inline]
syscall_exit_to_user_mode include/linux/entry-common.h:194 [inline]
do_syscall_64+0x2c1/0xf80 arch/x86/entry/syscall_64.c:100
entry_SYSCALL_64_after_hwframe+0x77/0x7f
The buggy address belongs to the object at ffff888052460000
which belongs to the cache kmalloc-4k of size 4096
The buggy address is located 2232 bytes inside of
freed 4096-byte region [ffff888052460000, ffff888052461000)
The buggy address belongs to the physical page:
page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x52460
head: order:3 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0
flags: 0x80000000000040(head|node=0|zone=1)
page_type: f5(slab)
raw: 0080000000000040 ffff88813ff27140 dead000000000122 0000000000000000
raw: 0000000000000000 0000000000040004 00000000f5000000 0000000000000000
head: 0080000000000040 ffff88813ff27140 dead000000000122 0000000000000000
head: 0000000000000000 0000000000040004 00000000f5000000 0000000000000000
head: 0080000000000003 ffffea0001491801 00000000ffffffff 00000000ffffffff
head: 0000000000000000 0000000000000000 00000000ffffffff 0000000000000008
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 3, migratetype Unmovable, gfp_mask 0xd20c0(__GFP_IO|__GFP_FS|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC), pid 6293, tgid 6292 (syz.4.91), ts 187223965101, free_ts 187217645183
set_page_owner include/linux/page_owner.h:32 [inline]
post_alloc_hook+0x234/0x290 mm/page_alloc.c:1857
prep_new_page mm/page_alloc.c:1865 [inline]
get_page_from_freelist+0x28c0/0x2960 mm/page_alloc.c:3915
__alloc_frozen_pages_noprof+0x181/0x370 mm/page_alloc.c:5210
alloc_pages_mpol+0xd1/0x380 mm/mempolicy.c:2486
alloc_slab_page mm/slub.c:3075 [inline]
allocate_slab+0x86/0x3b0 mm/slub.c:3248
new_slab mm/slub.c:3302 [inline]
___slab_alloc+0xb10/0x13e0 mm/slub.c:4656
__slab_alloc+0xc6/0x1f0 mm/slub.c:4779
__slab_alloc_node mm/slub.c:4855 [inline]
slab_alloc_node mm/slub.c:5251 [inline]
__kmalloc_cache_noprof+0x101/0x6d0 mm/slub.c:5771
kmalloc_noprof include/linux/slab.h:957 [inline]
kzalloc_noprof include/linux/slab.h:1094 [inline]
dev_new drivers/usb/gadget/legacy/raw_gadget.c:193 [inline]
raw_open+0x8d/0x530 drivers/usb/gadget/legacy/raw_gadget.c:435
misc_open+0x2de/0x350 drivers/char/misc.c:163
chrdev_open+0x4cf/0x5e0 fs/char_dev.c:414
do_dentry_open+0x7d0/0x1270 fs/open.c:962
vfs_open+0x3b/0x350 fs/open.c:1094
do_open fs/namei.c:4628 [inline]
path_openat+0x342a/0x3df0 fs/namei.c:4787
do_filp_open+0x1fa/0x410 fs/namei.c:4814
do_sys_openat2+0x121/0x200 fs/open.c:1430
page last free pid 6292 tgid 6292 stack trace:
reset_page_owner include/linux/page_owner.h:25 [inline]
free_pages_prepare mm/page_alloc.c:1406 [inline]
__free_frozen_pages+0xfe1/0x1170 mm/page_alloc.c:2943
discard_slab mm/slub.c:3346 [inline]
__put_partials+0x149/0x170 mm/slub.c:3886
__slab_free+0x2af/0x330 mm/slub.c:5952
qlink_free mm/kasan/quarantine.c:163 [inline]
qlist_free_all+0x97/0x100 mm/kasan/quarantine.c:179
kasan_quarantine_reduce+0x148/0x160 mm/kasan/quarantine.c:286
__kasan_slab_alloc+0x22/0x80 mm/kasan/common.c:350
kasan_slab_alloc include/linux/kasan.h:253 [inline]
slab_post_alloc_hook mm/slub.c:4953 [inline]
slab_alloc_node mm/slub.c:5263 [inline]
kmem_cache_alloc_noprof+0x18d/0x6c0 mm/slub.c:5270
getname_flags+0xb8/0x540 fs/namei.c:146
user_path_at+0x24/0x60 fs/namei.c:3566
__do_sys_chdir fs/open.c:558 [inline]
__se_sys_chdir+0x91/0x280 fs/open.c:552
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xec/0xf80 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
Memory state around the buggy address:
ffff888052460780: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
ffff888052460800: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
>ffff888052460880: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
^
ffff888052460900: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
ffff888052460980: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
==================================================================
Reply: The bug report is **actionable** and **self-consistent**.
### Analysis of the report:
1. **Three Stack Traces**: The report contains all three required stack traces:
* **Bad Access Stack**: Shows a use-after-free in `raw_event_queue_add` (called via `gadget_disconnect`) when trying to acquire a spinlock.
* **Allocation Stack**: Shows the object (`struct raw_dev`) was allocated in `raw_open` via `dev_new`.
* **Free Stack**: Shows the object was freed in `raw_release` via `kref_put`.
2. **Consistency**:
* All three stacks involve the same object type (`struct raw_dev`) and the same source file (`drivers/usb/gadget/legacy/raw_gadget.c`).
* The buggy address (`ffff8880524608b8`) is at an offset (~2232 bytes) that corresponds to the `queue` member (specifically its `lock` field) within `struct raw_dev`, as confirmed by the struct layout.
* The crash happens in a workqueue (`usb_hub_wq`), which is consistent with how `dummy_hcd` (the emulated USB controller) handles port events like disconnects.
3. **Root Cause Hypothesis**:
The bug is a race condition between the closing of the `raw_gadget` file descriptor and a USB disconnect event.
* When the file descriptor is closed, `raw_release` is called. It unregisters the gadget driver and drops the reference count of the `raw_dev` object.
* Simultaneously, a disconnect event (triggered here by a port power cycle in `dummy_hcd`) calls the `gadget_disconnect` callback.
* If `dummy_hcd` or the UDC core does not properly synchronize the callback with the unregistration process, the callback may be invoked or continue running after `raw_release` has already freed the `raw_dev` structure.
* In this specific case, `gadget_disconnect` attempts to access `dev->queue.lock`, leading to a use-after-free because `dev` has been freed by `raw_release`.
4. **Actionability**:
The report provides sufficient information to identify the affected subsystem (`usb/gadget/legacy/raw_gadget.c`), the involved structures, and the likely racing execution paths. A developer can investigate why the synchronization between `usb_gadget_unregister_driver` and the `dummy_hcd` callbacks failed to prevent this access.
Conclusion: The report is actionable and provides a clear path for debugging the race condition.
|
| 5/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 7712
output: 27
thoughts: 165
Thoughts: **Analyzing a `slab-use-after-free` Vulnerability in the USB Gadget Legacy Driver**
Okay, so I've got a `slab-use-after-free` bug on my hands, specifically in `__raw_spin_lock_irqsave`. The memory access is happening at `ffff8880524608b8`, and that's a key piece of information. The allocation was done by `dev_new` within the `raw_gadget.c` file, specifically line 193. The interesting part is that `raw_open` is the call site. The corresponding deallocation is happening at line 474 in `raw_gadget.c`, done by `raw_release`.
Now, I need to understand the relationship between `raw_open`, `dev_new`, `raw_release` and the spinlock protection. My intuition says there's a concurrency issue. Someone's not synchronizing correctly.
My next step is to dive right into the code. Let's start by getting a handle on the codebase. I'll list out all of the files in `drivers/usb/gadget/legacy/`. Knowing the full context will help me get a feel for how these components play together. Then, I can focus on the specific functions involved. I need to trace the data flow and understand when this object is allocated, used, and freed in relation to the spinlock operations. Gotta find that critical section.
|
| 6/2 |
2026/02/06 12:22 |
tool |
codesearch-dir-index |
0m
Args: map[Dir:drivers/usb/gadget/legacy/]
Results: map[Files:[acm_ms.c audio.c cdc2.c dbgp.c ether.c g_ffs.c gmidi.c hid.c inode.c mass_storage.c multi.c ncm.c nokia.c printer.c raw_gadget.c serial.c tcm_usb_gadget.c webcam.c zero.c] Subdirs:<nil>]
|
| 7/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 8377
output: 47
thoughts: 41
Thoughts: Here's my analysis of the provided text, summarized in the first person, assuming an expert user:
**Diving into `raw_gadget.c`**
Alright, so the focus is clearly on `raw_gadget.c`. That's where I need to start. I'll need to drill down into the allocation process to understand the gadgetās core functionality. My instinct says I should zero in on `dev_new` and `raw_open`. Those are typically where the initial device structures and resources are set up. I expect to find some interesting stuff happening in there ā memory allocation, data structure initialization, and likely, some device-specific setup routines. Let's see if this hunch holds, and then I'll move on to anything else that seems relevant after that.
|
| 8/2 |
2026/02/06 12:22 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/legacy/raw_gadget.c IncludeLines:true Name:dev_new]
Results: map[SourceCode: 189: static struct raw_dev *dev_new(void)
190: {
191: struct raw_dev *dev;
192:
193: dev = kzalloc(sizeof(*dev), GFP_KERNEL);
194: if (!dev)
195: return NULL;
196: /* Matches kref_put() in raw_release(). */
197: kref_init(&dev->count);
198: spin_lock_init(&dev->lock);
199: init_completion(&dev->ep0_done);
200: raw_event_queue_init(&dev->queue);
201: dev->driver_id_number = -1;
202: return dev;
203: }
SourceFile:drivers/usb/gadget/legacy/raw_gadget.c]
|
| 9/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 8886
output: 47
thoughts: 20
Thoughts: Okay, here's my take on those thoughts, framed as an expert's internal monologue:
**Allocation and Release: A Quick Glance**
Alright, so the focus is on device management, specifically the low-level allocation and release of some kind of `struct raw_dev`. This suggests we're dealing with direct device access ā likely at a lower kernel level, perhaps interacting with raw block devices or something similar. The allocation of this structure is the first piece of the puzzle. What's it holding? Important, but not the focus of this thought.
Now, let's pivot to the `raw_release` function. This is critical. The allocation is one thing, but how the resource is freed and managed is where the devil often resides. We need to look closely at what `raw_release` is doing to ensure it's handling memory deallocation correctly, releasing any acquired resources (like interrupts or DMA channels), and cleaning up any state that the `struct raw_dev` might have. Is it idempotent? Is it properly synchronized to avoid race conditions? I need to understand how the system guarantees proper release of the resource and prevent memory leaks.
|
| 10/2 |
2026/02/06 12:22 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/legacy/raw_gadget.c IncludeLines:true Name:raw_release]
Results: map[SourceCode: 444: static int raw_release(struct inode *inode, struct file *fd)
445: {
446: int ret = 0;
447: struct raw_dev *dev = fd->private_data;
448: unsigned long flags;
449: bool unregister = false;
450:
451: spin_lock_irqsave(&dev->lock, flags);
452: dev->state = STATE_DEV_CLOSED;
453: if (!dev->gadget) {
454: spin_unlock_irqrestore(&dev->lock, flags);
455: goto out_put;
456: }
457: if (dev->gadget_registered)
458: unregister = true;
459: dev->gadget_registered = false;
460: spin_unlock_irqrestore(&dev->lock, flags);
461:
462: if (unregister) {
463: ret = usb_gadget_unregister_driver(&dev->driver);
464: if (ret != 0)
465: dev_err(dev->dev,
466: "usb_gadget_unregister_driver() failed with %d\n",
467: ret);
468: /* Matches kref_get() in raw_ioctl_run(). */
469: kref_put(&dev->count, dev_free);
470: }
471:
472: out_put:
473: /* Matches dev_new() in raw_open(). */
474: kref_put(&dev->count, dev_free);
475: return ret;
476: }
SourceFile:drivers/usb/gadget/legacy/raw_gadget.c]
|
| 11/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 9713
output: 48
thoughts: 126
Thoughts: **Analyzing a USB Gadget Disconnect Issue**
Okay, so I'm tracking down this UAF, and it looks like it's happening during a USB disconnect, specifically within `raw_event_queue_add`. I'm seeing the call stack: `raw_event_queue_add` is called from `raw_queue_event`, which in turn is called from `gadget_disconnect`. This is very interesting, and the function signatures are:
```c
gadget_disconnect+0x51/0xf0 drivers/usb/gadget/legacy/raw_gadget.c:388
set_link_state+0xc0a/0x1220 drivers/usb/gadget/udc/dummy_hcd.c:471
```
So, the disconnect is triggering something that eventually results in this UAF. I need to take a closer look at `gadget_disconnect` and `raw_queue_event`. I'm guessing there's a race condition or some timing issue related to the event queue. I'll start with those two functions and then see where the pointers are being mangled.
|
| 12/2 |
2026/02/06 12:22 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/legacy/raw_gadget.c IncludeLines:true Name:gadget_disconnect]
Results: map[SourceCode: 382: static void gadget_disconnect(struct usb_gadget *gadget)
383: {
384: struct raw_dev *dev = get_gadget_data(gadget);
385: int ret;
386:
387: dev_dbg(&gadget->dev, "gadget disconnected\n");
388: ret = raw_queue_event(dev, USB_RAW_EVENT_DISCONNECT, 0, NULL);
389: if (ret < 0)
390: dev_err(&gadget->dev, "failed to queue disconnect event\n");
391: }
SourceFile:drivers/usb/gadget/legacy/raw_gadget.c]
|
| 13/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 10347
output: 49
thoughts: 8
Thoughts: Okay, here's my attempt at summarizing the thought process, tailored for an expert audience, and written in the first person:
**Analyzing the `raw_queue_event`**
Right, so I'm looking at this `raw_queue_event`. The term itself suggests it's a fundamental unit, likely the unprocessed or minimally processed data that feeds into a queuing system. My immediate instinct is to understand the *shape* of this event. What are the key fields? Are they structured, perhaps JSON, or more free-form text? What metadata is included? I need to quickly assess its internal schema; this will inform how I need to parse or deserialize the data.
I'm thinking about the potential sources of this `raw_queue_event`. What kind of system is generating it? Is it coming from a database change log, an API request handler, or some other application process? The provenance is critical. Knowing the originating system gives me valuable context. For example, a database event will have different characteristics to an incoming API request.
Next, I'm already anticipating possible transformations and filtering that will be applied down the pipeline. What transformations will be needed to bring this event to a final state, if I'm able to view a final state? Does the format need to be converted to a specific format? Any normalization requirements? I'm wondering if this "raw" form implies a need for validation. Is there a defined schema that this event should conform to, and if so, how's the validation implemented? I'm going to start mentally sketching the potential steps in the processing workflow and identifying possible bottlenecks or points of failure.
Finally, I'm thinking about the implications. What's the *goal* of consuming this `raw_queue_event`? What is the downstream purpose? Is it for real-time analytics, asynchronous processing, data warehousing, or something else entirely? The end use drives the level of scrutiny and transformation necessary. I also need to consider what error handling or retry strategies might be required if processing the event fails. Overall, I want to anticipate the potential challenges associated with the ingestion and processing of this data.
|
| 14/2 |
2026/02/06 12:22 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/legacy/raw_gadget.c IncludeLines:true Name:raw_queue_event]
Results: map[SourceCode: 234: static int raw_queue_event(struct raw_dev *dev,
235: enum usb_raw_event_type type, size_t length, const void *data)
236: {
237: int ret = 0;
238: unsigned long flags;
239:
240: ret = raw_event_queue_add(&dev->queue, type, length, data);
241: if (ret < 0) {
242: spin_lock_irqsave(&dev->lock, flags);
243: dev->state = STATE_DEV_FAILED;
244: spin_unlock_irqrestore(&dev->lock, flags);
245: }
246: return ret;
247: }
SourceFile:drivers/usb/gadget/legacy/raw_gadget.c]
|
| 15/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 11104
output: 51
thoughts: 10
Thoughts: Okay, here's my attempt at summarizing the thought process, tailored for an expert audience, and written in the first person:
**Delving into Event Queues**
Alright, let's break this down. When I see mention of `raw_event_queue_add`, my mind immediately goes into overdrive considering the implications. This isn't just about *adding* an event to a queue, but about the *raw* nature of it. It suggests a low-level operation, dealing with the nitty-gritty details of event management, likely before any higher-level abstractions are applied.
I'm instantly thinking about the potential for:
* **Concurrency Issues:** Is this function thread-safe? Do I need to worry about race conditions when multiple threads or processes might be trying to add or read from the same queue? Are there appropriate locking mechanisms in place? I hope there's some design that avoids too much contention.
* **Data Structures:** What's the underlying data structure used for this raw queue? Is it a simple FIFO buffer, a priority queue, a circular buffer, or something more sophisticated? Performance characteristics depend entirely on how events are handled. Is the event size fixed or dynamic?
* **Event Representation:** What's the actual format of the event data being added? Is it a simple data structure, a pointer to more complex data, or some kind of opaque handle? The event representation impacts memory management and the efficiency of event processing. I hope some serialization strategy is applied when necessary.
* **Error Handling:** How does this function handle errors? Does it return an error code, throw an exception, or silently fail? Are there mechanisms for detecting queue overflow or other exceptional conditions? A robust design needs comprehensive error handling.
* **Integration with the System:** How does this raw queue fit into the larger event-processing architecture? Is it a building block for a higher-level event system, or is it a standalone component? Where does the queue interface come from? Does it use a pub/sub pattern? What does the "raw" part of "raw" actually imply?
Essentially, I'm trying to mentally unpack all the implicit assumptions and potential complexities that come with this single function call. It's a key piece of the puzzle, and I need to understand it thoroughly to grasp the system's overall event handling strategy. I'm wondering if this is part of some driver code, an OS kernel component, or maybe a library used by a more extensive application.
|
| 16/2 |
2026/02/06 12:22 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/legacy/raw_gadget.c IncludeLines:true Name:raw_event_queue_add]
Results: map[SourceCode: 62: static int raw_event_queue_add(struct raw_event_queue *queue,
63: enum usb_raw_event_type type, size_t length, const void *data)
64: {
65: unsigned long flags;
66: struct usb_raw_event *event;
67:
68: spin_lock_irqsave(&queue->lock, flags);
69: if (queue->size >= RAW_EVENT_QUEUE_SIZE) {
70: spin_unlock_irqrestore(&queue->lock, flags);
71: return -ENOMEM;
72: }
73: event = kmalloc(sizeof(*event) + length, GFP_ATOMIC);
74: if (!event) {
75: spin_unlock_irqrestore(&queue->lock, flags);
76: return -ENOMEM;
77: }
78: event->type = type;
79: event->length = length;
80: if (event->length)
81: memcpy(&event->data[0], data, length);
82: queue->events[queue->size] = event;
83: queue->size++;
84: up(&queue->sema);
85: spin_unlock_irqrestore(&queue->lock, flags);
86: return 0;
87: }
SourceFile:drivers/usb/gadget/legacy/raw_gadget.c]
|
| 17/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 12099
output: 49
thoughts: 413
Thoughts: **Analyzing a Potential Use-After-Free in Raw Gadget Driver**
Okay, so I've got a UAF, a Use-After-Free, at `spin_lock_irqsave(&queue->lock, flags);` within the `raw_event_queue_add` function. The `queue` in question is `&dev->queue`. This strongly suggests that the `dev`, the `struct raw_dev`, has already been freed. This warrants a deep dive.
My immediate thought is, what's releasing this `dev`? I remember `raw_release` calls `usb_gadget_unregister_driver(&dev->driver)`. If that function returns successfully, then there's a `kref_put(&dev->count, dev_free)` call. But wait, `raw_release` actually has *two* `kref_put` calls if `unregister` is true. One is right after the `usb_gadget_unregister_driver` call (line 469), and the second is further down, at line 474. It looks like:
```c
462: if (unregister) {
463: ret = usb_gadget_unregister_driver(&dev->driver);
464: if (ret != 0)
465: dev_err(dev->dev,
466: "usb_gadget_unregister_driver() failed with %d\n",
467: ret);
468: /* Matches kref_get() in raw_ioctl_run(). */
469: kref_put(&dev->count, dev_free);
470: }
471:
472: out_put:
473: /* Matches dev_new() in raw_open(). */
474: kref_put(&dev->count, dev_free);
```
Let's trace the reference counting. `raw_open` calls `dev_new`, which presumably sets the `kref` count to 1. Then, I believe `raw_ioctl_run` increments it. I need to confirm that `raw_ioctl_run` is incrementing it. I'm going to check the `raw_ioctl_run` function next to understand how the reference count is handled in that context. This is likely the key to pinpointing the race condition and the reason for the UAF.
|
| 18/2 |
2026/02/06 12:22 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/legacy/raw_gadget.c IncludeLines:true Name:raw_ioctl_run]
Results: map[SourceCode: 579: static int raw_ioctl_run(struct raw_dev *dev, unsigned long value)
580: {
581: int ret = 0;
582: unsigned long flags;
583:
584: if (value)
585: return -EINVAL;
586:
587: spin_lock_irqsave(&dev->lock, flags);
588: if (dev->state != STATE_DEV_INITIALIZED) {
589: dev_dbg(dev->dev, "fail, device is not initialized\n");
590: ret = -EINVAL;
591: goto out_unlock;
592: }
593: dev->state = STATE_DEV_REGISTERING;
594: spin_unlock_irqrestore(&dev->lock, flags);
595:
596: ret = usb_gadget_register_driver(&dev->driver);
597:
598: spin_lock_irqsave(&dev->lock, flags);
599: if (ret) {
600: dev_err(dev->dev,
601: "fail, usb_gadget_register_driver returned %d\n", ret);
602: dev->state = STATE_DEV_FAILED;
603: goto out_unlock;
604: }
605: dev->gadget_registered = true;
606: dev->state = STATE_DEV_RUNNING;
607: /* Matches kref_put() in raw_release(). */
608: kref_get(&dev->count);
609:
610: out_unlock:
611: spin_unlock_irqrestore(&dev->lock, flags);
612: return ret;
613: }
SourceFile:drivers/usb/gadget/legacy/raw_gadget.c]
|
| 19/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 13667
output: 49
thoughts: 704
Thoughts: **Analyzing a Potential Race Condition in the USB Gadget Driver**
Okay, so I've been digging into this, and here's what's happening in my head. Initially, I confirmed that `raw_ioctl_run` does indeed increment the reference count with `kref_get`. Now, the problem centers around the interaction between `raw_release` and potential disconnect events. `raw_release` is crucial here because it calls `usb_gadget_unregister_driver`. My expectation is that this should gracefully stop the gadget and, importantly, disconnect it, which is the job of the `gadget_disconnect` callback.
The crux of the matter is that `usb_gadget_unregister_driver` *might* call `gadget_disconnect` synchronously or asynchronously. The stack trace is throwing up a flag here: it shows `gadget_disconnect` originating from the `hub_event` workqueue, which gets triggered by `usb_control_msg`, specifically calls to `usb_clear_port_feature` or `usb_hub_set_port_power`. This seems to represent an actual physical disconnect event, or one simulated through `dummy_hcd`.
So, `raw_release` is triggered when the file descriptor is closed, setting `dev->state` to `STATE_DEV_CLOSED`. Here's where the race comes in: If `raw_release` is executing and calling `kref_put` *concurrently* with a disconnect event, we have a clear race condition.
I need to confirm this. I stepped back to look at `usb_gadget_unregister_driver`. That function is in `drivers/usb/gadget/udc/core.c`. Ah, the stack trace indicates that `gadget_disconnect` is, in fact, being called from `dummy_hcd` via `set_link_state` which calls `gadget_disconnect`.
The stack trace clearly illustrates that `dummy_hcd` is calling `gadget_disconnect`, and it's happening in the context of `usb_hub_wq`.
The smoking gun is here: the backtrace shows that the `raw_dev` (our gadget device) was already freed by `raw_release`. This looks like a classic use-after-free scenario. `raw_release` finishes, freeing `dev`. But, `dummy_hcd` *still* has a reference to the gadget and is attempting to call `gadget_disconnect` on this freed memory.
However, a nagging thought is that `raw_gadget` registers a `usb_gadget_driver`. Theoretically, when `raw_release` calls `usb_gadget_unregister_driver`, it *should* ensure that no further callbacks are made on that driver. This implies that there is a break in the assumptions I have made.
I am going to circle back to `raw_ioctl_run` now to see how `dev->driver` is actually initialized, to make sure I am correct.
|
| 20/2 |
2026/02/06 12:22 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/legacy/raw_gadget.c IncludeLines:true Name:raw_ioctl_init]
Results: map[SourceCode: 480: static int raw_ioctl_init(struct raw_dev *dev, unsigned long value)
481: {
482: int ret = 0;
483: int driver_id_number;
484: struct usb_raw_init arg;
485: char *udc_driver_name;
486: char *udc_device_name;
487: char *driver_driver_name;
488: unsigned long flags;
489:
490: if (copy_from_user(&arg, (void __user *)value, sizeof(arg)))
491: return -EFAULT;
492:
493: switch (arg.speed) {
494: case USB_SPEED_UNKNOWN:
495: arg.speed = USB_SPEED_HIGH;
496: break;
497: case USB_SPEED_LOW:
498: case USB_SPEED_FULL:
499: case USB_SPEED_HIGH:
500: case USB_SPEED_SUPER:
501: break;
502: default:
503: return -EINVAL;
504: }
505:
506: driver_id_number = ida_alloc(&driver_id_numbers, GFP_KERNEL);
507: if (driver_id_number < 0)
508: return driver_id_number;
509:
510: driver_driver_name = kmalloc(DRIVER_DRIVER_NAME_LENGTH_MAX, GFP_KERNEL);
511: if (!driver_driver_name) {
512: ret = -ENOMEM;
513: goto out_free_driver_id_number;
514: }
515: snprintf(driver_driver_name, DRIVER_DRIVER_NAME_LENGTH_MAX,
516: DRIVER_NAME ".%d", driver_id_number);
517:
518: udc_driver_name = kmalloc(UDC_NAME_LENGTH_MAX, GFP_KERNEL);
519: if (!udc_driver_name) {
520: ret = -ENOMEM;
521: goto out_free_driver_driver_name;
522: }
523: ret = strscpy(udc_driver_name, &arg.driver_name[0],
524: UDC_NAME_LENGTH_MAX);
525: if (ret < 0)
526: goto out_free_udc_driver_name;
527: ret = 0;
528:
529: udc_device_name = kmalloc(UDC_NAME_LENGTH_MAX, GFP_KERNEL);
530: if (!udc_device_name) {
531: ret = -ENOMEM;
532: goto out_free_udc_driver_name;
533: }
534: ret = strscpy(udc_device_name, &arg.device_name[0],
535: UDC_NAME_LENGTH_MAX);
536: if (ret < 0)
537: goto out_free_udc_device_name;
538: ret = 0;
539:
540: spin_lock_irqsave(&dev->lock, flags);
541: if (dev->state != STATE_DEV_OPENED) {
542: dev_dbg(dev->dev, "fail, device is not opened\n");
543: ret = -EINVAL;
544: goto out_unlock;
545: }
546: dev->udc_name = udc_driver_name;
547:
548: dev->driver.function = DRIVER_DESC;
549: dev->driver.max_speed = arg.speed;
550: dev->driver.setup = gadget_setup;
551: dev->driver.disconnect = gadget_disconnect;
552: dev->driver.bind = gadget_bind;
553: dev->driver.unbind = gadget_unbind;
554: dev->driver.suspend = gadget_suspend;
555: dev->driver.resume = gadget_resume;
556: dev->driver.reset = gadget_reset;
557: dev->driver.driver.name = driver_driver_name;
558: dev->driver.udc_name = udc_device_name;
559: dev->driver.match_existing_only = 1;
560: dev->driver_id_number = driver_id_number;
561:
562: dev->state = STATE_DEV_INITIALIZED;
563: spin_unlock_irqrestore(&dev->lock, flags);
564: return ret;
565:
566: out_unlock:
567: spin_unlock_irqrestore(&dev->lock, flags);
568: out_free_udc_device_name:
569: kfree(udc_device_name);
570: out_free_udc_driver_name:
571: kfree(udc_driver_name);
572: out_free_driver_driver_name:
573: kfree(driver_driver_name);
574: out_free_driver_id_number:
575: ida_free(&driver_id_numbers, driver_id_number);
576: return ret;
577: }
SourceFile:drivers/usb/gadget/legacy/raw_gadget.c]
|
| 21/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 16642
output: 50
thoughts: 127
Thoughts: **Analyzing USB Gadget Driver Unregistration**
Okay, let's break this down. In the initialization phase, specifically within `raw_ioctl_init`, the `disconnect` function pointer for the device's driver, `dev->driver.disconnect`, is being set to `gadget_disconnect`. That's standard setup, so far, so good.
Now, looking at the release path, particularly within the `raw_release` function, lines 462 and 463 are critical: we have a conditional unregistration. If `unregister` is true, we call `usb_gadget_unregister_driver` to take things down. My concern is, as always, making sure this is done cleanly.
My real focus is on ensuring `usb_gadget_unregister_driver` properly unbinds the driver and, more importantly, stops any further callbacks from being invoked. We need to prevent any possible race conditions or use-after-free scenarios. Let's delve into the core functionality. I'm going to pull up the source code for `usb_gadget_unregister_driver`, which resides in `drivers/usb/gadget/udc/core.c`, and confirm that the unbinding and callback management are handled correctly. That's where the devil is, always.
|
| 22/2 |
2026/02/06 12:22 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/udc/core.c IncludeLines:true Name:usb_gadget_unregister_driver]
Results: map[SourceCode:1746: int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1747: {
1748: if (!driver || !driver->unbind)
1749: return -EINVAL;
1750:
1751: driver_unregister(&driver->driver);
1752: return 0;
1753: }
SourceFile:drivers/usb/gadget/udc/core.c]
|
| 23/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 17235
output: 50
thoughts: 568
Thoughts: **Investigating a Gadget Disconnect Issue**
Okay, so I'm tracking down this odd behavior where `gadget_disconnect` is being called even after the driver has supposedly been unregistered. The code flow starts with a call to `driver_unregister(&driver->driver)`, which should handle the device driver unregistration. But I see this happening within the `raw_release` function, which makes me think this is the expected behavior. The core of this is the `usb_gadget_unregister_driver`.
Specifically, `raw_gadget` seems to use `usb_gadget_register_driver` and `usb_gadget_unregister_driver`, which I expect will be the standard calls. Later, the driver unregisters.
The stack trace gives me a decent picture: `gadget_disconnect` is called from `hub_event`, which is a workqueue. This is happening through `set_link_state` and ultimately through the `dummy_hcd`, which acts as both a Host Controller Driver (HCD) and a USB Device Controller (UDC). The `dummy_hcd` uses `dummy_hub_control`, and that's the link. So when the `hub_event` attempts to clear the port feature (perhaps a power cycle), it calls into `dummy_hub_control`, which calls `set_link_state`. The `set_link_state` call can then call `gadget_disconnect` if the link state transitions to disconnected.
The real question is: why is `gadget_disconnect` being called *after* the driver unregistration via `usb_gadget_unregister_driver`? Shouldn't that be a synchronous operation? The driver should be unregistered, right?
My initial hunch is that `usb_gadget_unregister_driver` might not be waiting for all of its callbacks to finish before returning. Or, there could be something fishy in the `dummy_hcd` implementation itself. Perhaps there's an issue with the timing or a race condition between the unregistration and the hub event.
I'll need to dig deeper into the `set_link_state` function within the `dummy_hcd` driver to confirm. The code itself should be straightforward enough to work through.
|
| 24/2 |
2026/02/06 12:22 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/udc/dummy_hcd.c IncludeLines:true Name:set_link_state]
Results: map[SourceCode: 432: /* caller must hold lock */
433: static void set_link_state(struct dummy_hcd *dum_hcd)
434: __must_hold(&dum->lock)
435: {
436: struct dummy *dum = dum_hcd->dum;
437: unsigned int power_bit;
438:
439: dum_hcd->active = 0;
440: if (dum->pullup)
441: if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
442: dum->gadget.speed != USB_SPEED_SUPER) ||
443: (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
444: dum->gadget.speed == USB_SPEED_SUPER))
445: return;
446:
447: set_link_state_by_speed(dum_hcd);
448: power_bit = (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 ?
449: USB_SS_PORT_STAT_POWER : USB_PORT_STAT_POWER);
450:
451: if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
452: dum_hcd->active)
453: dum_hcd->resuming = 0;
454:
455: /* Currently !connected or in reset */
456: if ((dum_hcd->port_status & power_bit) == 0 ||
457: (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
458: unsigned int disconnect = power_bit &
459: dum_hcd->old_status & (~dum_hcd->port_status);
460: unsigned int reset = USB_PORT_STAT_RESET &
461: (~dum_hcd->old_status) & dum_hcd->port_status;
462:
463: /* Report reset and disconnect events to the driver */
464: if (dum->ints_enabled && (disconnect || reset)) {
465: stop_activity(dum);
466: ++dum->callback_usage;
467: spin_unlock(&dum->lock);
468: if (reset)
469: usb_gadget_udc_reset(&dum->gadget, dum->driver);
470: else
471: dum->driver->disconnect(&dum->gadget);
472: spin_lock(&dum->lock);
473: --dum->callback_usage;
474: }
475: } else if (dum_hcd->active != dum_hcd->old_active &&
476: dum->ints_enabled) {
477: ++dum->callback_usage;
478: spin_unlock(&dum->lock);
479: if (dum_hcd->old_active && dum->driver->suspend)
480: dum->driver->suspend(&dum->gadget);
481: else if (!dum_hcd->old_active && dum->driver->resume)
482: dum->driver->resume(&dum->gadget);
483: spin_lock(&dum->lock);
484: --dum->callback_usage;
485: }
486:
487: dum_hcd->old_status = dum_hcd->port_status;
488: dum_hcd->old_active = dum_hcd->active;
489: }
SourceFile:drivers/usb/gadget/udc/dummy_hcd.c]
|
| 25/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 19390
output: 51
thoughts: 98
Thoughts: **Analyzing the USB Gadget Driver Disconnect**
Okay, so I'm looking at this `set_link_state` function. Line 471 is interesting because it calls `dum->driver->disconnect(&dum->gadget)`. Right, so `dum->driver` refers to the `usb_gadget_driver` ā that's the driver controlling this USB gadget. My immediate question is: what's going to happen when the underlying UDC driver is eventually stopped? That happens through `usb_gadget_unregister_driver`, which then calls `udc_stop`. For the specific case of the `dummy_hcd`, I need to carefully examine what the `udc_stop` function actually does. I'm going to dig into the `udc_stop` function implementation for the dummy_hcd and see what is actually happening there. This is where I think the important action will be.
|
| 26/2 |
2026/02/06 12:22 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/udc/dummy_hcd.c IncludeLines:true Name:dummy_udc_stop]
Results: map[SourceCode:1028: static int dummy_udc_stop(struct usb_gadget *g)
1029: {
1030: struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
1031: struct dummy *dum = dum_hcd->dum;
1032:
1033: spin_lock_irq(&dum->lock);
1034: dum->ints_enabled = 0;
1035: stop_activity(dum);
1036: dum->driver = NULL;
1037: spin_unlock_irq(&dum->lock);
1038:
1039: return 0;
1040: }
SourceFile:drivers/usb/gadget/udc/dummy_hcd.c]
|
| 27/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 19967
output: 22
thoughts: 1656
Thoughts: **Analyzing a Potential Race Condition in the USB Gadget Driver**
Okay, so I'm digging into this code and I think I've spotted a race condition, specifically related to the interaction between the UDC and HCD sides of the `dummy_hcd` driver. It seems to revolve around the handling of driver unregistration and link state changes.
Here's my thought process: The crux is how `dummy_udc_stop` (called during driver unregistration) sets `dum->driver = NULL` and `dum->ints_enabled = 0`. `set_link_state`, on the other hand, *might* be running concurrently from the HCD side via `dummy_hub_control`, which itself gets called from `usb_hcd_submit_urb`. `set_link_state` checks `dum->ints_enabled`, and if true, calls `dum->driver->disconnect`.
The first thought is a simple race where `set_link_state` checks `dum->ints_enabled`, which is true, and is about to call `disconnect` but `dummy_udc_stop` sets `dum->driver = NULL` *before* the call. The lock seems to solve this by gating all of the access to the UDC/HCD information.
But wait, `dummy_udc_stop` is called from `usb_gadget_unregister_driver` via the Linux kernel driver model. And the `raw_release` function is calling `usb_gadget_unregister_driver` *before* freeing the `raw_dev` structure.
This is where it gets interesting: `gadget_disconnect` which is called from within the `hub_event` workqueue, *might* still be operating on a structure (`raw_dev`) which is about to be freed. The `gadget_disconnect` function in `raw_gadget.c` gets the `dev` pointer using `get_gadget_data(gadget)`. The data pointer is set by `gadget_bind` and cleared by `gadget_unbind`. However, the current stack trace tells us that `dev` is *not* `NULL` in the call to `raw_queue_event` within `gadget_disconnect`.
The stack trace reveals that `gadget_disconnect` is called from `hub_event` workqueue, itself triggered by `usb_control_msg` from `hub_port_connect`. If `raw_release` is running, then even though `usb_gadget_unregister_driver` may have been called, some other thread is still interacting with the gadget. This is because there's a disconnect event queued on the HCD side.
So, if `gadget_unbind` has already been called, then `get_gadget_data(gadget)` should return `NULL`. Yet, the stack trace shows that it does not return `NULL`, but a valid pointer to freed memory! I have found the bug.
If `gadget_disconnect` is called, that means that `dum->driver` was *not* NULL in the HCD context at the time the function was called. `dummy_udc_stop` will set `dum->driver` to `NULL`.
It seems that the HCD and UDC driver interactions within the `dummy_hcd` driver and `raw_gadget.c` present a problem where information can live for too long, past when it should have been deallocated.
|
| 28/2 |
2026/02/06 12:22 |
tool |
grepper |
0m
Args: map[Expression:usb_gadget_remove_driver]
Results: map[Output:drivers/usb/gadget/udc/dummy_hcd.c=899=static int dummy_pullup(struct usb_gadget *_gadget, int value)
--
drivers/usb/gadget/udc/dummy_hcd.c-914- * This seems to be the best place to emulate the call to
drivers/usb/gadget/udc/dummy_hcd.c:915: * synchronize_irq() that's in usb_gadget_remove_driver().
drivers/usb/gadget/udc/dummy_hcd.c-916- * Doing it in dummy_udc_stop() would be too late since it
]
|
| 29/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 22561
output: 50
thoughts: 34
Thoughts: **Digging into the Gadget Driver Removal**
Okay, so I'm seeing a reference to `usb_gadget_remove_driver` in a comment within `dummy_hcd.c`. That definitely piques my interest. It looks like the code is handling driver removal at a low level in this "dummy" host controller driver. Since I'm familiar with this architecture and the typical USB device driver lifecycle, the presence of that function in the context of removal suggests it's a key piece of the puzzle for cleanly detaching a gadget driver. I need to understand its exact implementation in this case to be sure of the mechanics being used. Time to track down that definition and see precisely what's going on.
|
| 30/2 |
2026/02/06 12:22 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:drivers/usb/gadget/udc/core.c IncludeLines:true Name:usb_gadget_remove_driver]
Results: map[SourceCode: SourceFile:]
|
| 31/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 22817
output: 50
thoughts: 50
Thoughts: Okay, here's my interpretation of those thoughts, presented as my own mental process:
**USB Gadget Driver Investigation**
Hmm, I think I might be onto something. Could the key function I'm looking for be `usb_gadget_udc_stop`? It certainly sounds like it could be related to stopping the USB Device Controller (UDC) operation, which is where I need to focus my efforts.
Let's dive into the code. I'll take a look at `udc_bind_to_driver` within `drivers/usb/gadget/udc/core.c`. That file seems like the central hub for UDC operations. Hopefully, this function provides insight into how a UDC is initialized, bound to a driver, andācruciallyāhow it might be stopped or unbound. Understanding that process is likely to lead me closer to finding the point of control I require. I need to understand the relationship between the driver binding and any stop or unbinding calls.
|
| 32/2 |
2026/02/06 12:22 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:drivers/usb/gadget/udc/core.c IncludeLines:true Name:udc_bind_to_driver]
Results: map[SourceCode: SourceFile:]
|
| 33/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 23150
output: 19
thoughts: 15
Thoughts: Okay, here's my summary, presented as a stream of thought:
**Finding the Call Sites of `udc_stop`**
Alright, let's get down to it. My first move here is clear. I need to pinpoint every single instance where `udc_stop` is actually *used* in the code. I need to find all the call sites. Thatās the key. Knowing where this function is invoked will give me the context I need to understand its purpose and, likely, identify any potential issues or areas for optimization. This initial search is going to be crucial for forming a solid understanding of its role within the larger system. No shortcuts, just a comprehensive sweep of the codebase for every occurrence.
|
| 34/2 |
2026/02/06 12:22 |
tool |
grepper |
0m
Args: map[Expression:->udc_stop]
Results: map[Output:drivers/usb/gadget/udc/core.c=1250=static inline void usb_gadget_udc_stop_locked(struct usb_udc *udc)
--
drivers/usb/gadget/udc/core.c-1257-
drivers/usb/gadget/udc/core.c:1258: udc->gadget->ops->udc_stop(udc->gadget);
drivers/usb/gadget/udc/core.c-1259- udc->started = false;
]
|
| 35/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 23481
output: 53
thoughts: 22
Thoughts: Here's my analysis of the provided snippet, rendered as your own thoughts:
**Analyzing `usb_gadget_udc_stop_locked` and its Callers**
Okay, so I need to dig into the `usb_gadget_udc_stop_locked` function. It's obviously a critical piece of the USB gadget driver framework, specifically handling the stopping of the USB Device Controller (UDC) in a gadget context. I want to understand its purpose in detail and, crucially, how it's *used*. The key to any good system analysis is understanding call chains and where a function sits within the bigger picture. Therefore, I need to identify *all* of its callers. Knowing what calls `usb_gadget_udc_stop_locked` will paint a complete picture of its impact on system operation and where potential issues might exist. I'll need to trace its flow to be sure there are no obvious problems or areas that warrant further investigation. Let's see what we can find.
|
| 36/2 |
2026/02/06 12:22 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/udc/core.c IncludeLines:true Name:usb_gadget_udc_stop_locked]
Results: map[SourceCode:1237: /**
1238: * usb_gadget_udc_stop_locked - tells usb device controller we don't need it anymore
1239: * @udc: The UDC to be stopped
1240: *
1241: * This call is issued by the UDC Class driver after calling
1242: * gadget driver's unbind() method.
1243: *
1244: * The details are implementation specific, but it can go as
1245: * far as powering off UDC completely and disable its data
1246: * line pullups.
1247: *
1248: * Caller should acquire connect lock before invoking this function.
1249: */
1250: static inline void usb_gadget_udc_stop_locked(struct usb_udc *udc)
1251: __must_hold(&udc->connect_lock)
1252: {
1253: if (!udc->started) {
1254: dev_err(&udc->dev, "UDC had already stopped\n");
1255: return;
1256: }
1257:
1258: udc->gadget->ops->udc_stop(udc->gadget);
1259: udc->started = false;
1260: }
SourceFile:drivers/usb/gadget/udc/core.c]
|
| 37/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 24170
output: 51
thoughts: 12
Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and presented as if it were my own internal monologue:
**Halting the UDC, Methodically**
Right, so we're at the point where we need to stop the USB Gadget UDC. Thinking about this, the natural next step, the crucial command is `usb_gadget_udc_stop`. This is the point in the routine where the UDC gets disabled and any current USB communication is ceased. I need to ensure that whatever other processes are running have been appropriately handled and there are no unexpected residual side-effects that could cause future problems after this point. I'm going to review the current state of any attached devices to the UDC before proceeding. Time to ensure that everything is in a clean, de-initialized state. We need to be clean.
|
| 38/2 |
2026/02/06 12:22 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:drivers/usb/gadget/udc/core.c IncludeLines:true Name:usb_gadget_udc_stop]
Results: map[SourceCode: SourceFile:]
|
| 39/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 24435
output: 29
thoughts: 19
Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person:
**Finding Callers of `usb_gadget_udc_stop_locked`**
Alright, so the task at hand is to pinpoint all the places where the `usb_gadget_udc_stop_locked` function is being invoked. This is a critical step in understanding the control flow and potential dependencies within the USB Gadget UDC (USB Device Controller) subsystem. Knowing who calls this function is fundamental for any code analysis, debugging, or modification related to device state transitions and power management. I need to trace back from this function and see what's triggering its execution. I'm thinking I'll need to use my usual set of tools: a grep across the relevant kernel source trees is a must, but that alone probably won't be enough. I'll need to consider indirect calls through function pointers or macros, so a more sophisticated approach might be required to ensure I don't miss anything. Cross-referencing with other UDC-related modules and drivers is essential to understand the higher-level context. I'll need to carefully examine any interrupt handlers or tasklets that might be asynchronously calling this function. Once I have the call sites, I'll review the calling context, analyzing how this function is used for state change or power down, and what conditions trigger each call.
|
| 40/2 |
2026/02/06 12:22 |
tool |
codesearch-find-references |
0m
Args: map[Name:usb_gadget_udc_stop_locked]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:gadget_bind_driver SourceFile:drivers/usb/gadget/udc/core.c SourceLine:1655] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:gadget_unbind_driver SourceFile:drivers/usb/gadget/udc/core.c SourceLine:1694] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:soft_connect_store SourceFile:drivers/usb/gadget/udc/core.c SourceLine:1791]] TruncatedOutput:false]
|
| 41/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 24970
output: 48
thoughts: 13
Thoughts: Okay, let's dive into this. Here's my take on those thoughts, as if they're my own:
**Analyzing `gadget_unbind_driver`**
Alright, time to dissect `gadget_unbind_driver`. I need to understand its role in this whole dance. My immediate thought is, "What's the context? What's the triggering mechanism?" Is this called during a device disconnect, a driver unload, a configuration change, or something else entirely? The name itself screams "disconnection," but I need to be precise.
I'm mentally running through the typical USB gadget driver lifecycle. This function *has* to be responsible for releasing resources. Okay, so that likely means freeing any allocated memory, unregistering any endpoints, and likely stopping any active transfers associated with the driver. Iām thinking about potential race conditions. We need to consider locking mechanisms to ensure a clean unbind, preventing any data corruption or use-after-free scenarios. The timing is crucial. Does this happen atomically, or are there multiple stages involved? If so, what are the guarantees between these stages?
I need to quickly assess how it interacts with other related functions. `gadget_bind_driver` is its obvious partner. I'm going to look at the flow of control within the kernel to confirm my initial hypothesis. I'll need to check the function signature, scrutinize any calls to other relevant functions, and understand the implications of any return values. Ultimately, my goal is to confirm my intuition and map out its dependencies.
|
| 42/2 |
2026/02/06 12:22 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/udc/core.c IncludeLines:true Name:gadget_unbind_driver]
Results: map[SourceCode:1674: static void gadget_unbind_driver(struct device *dev)
1675: {
1676: struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1677: struct usb_udc *udc = gadget->udc;
1678: struct usb_gadget_driver *driver = udc->driver;
1679:
1680: dev_dbg(&udc->dev, "unbinding gadget driver [%s]\n", driver->function);
1681:
1682: udc->allow_connect = false;
1683: cancel_work_sync(&udc->vbus_work);
1684: mutex_lock(&udc->connect_lock);
1685: usb_gadget_disconnect_locked(gadget);
1686: usb_gadget_disable_async_callbacks(udc);
1687: if (gadget->irq)
1688: synchronize_irq(gadget->irq);
1689: mutex_unlock(&udc->connect_lock);
1690:
1691: udc->driver->unbind(gadget);
1692:
1693: mutex_lock(&udc->connect_lock);
1694: usb_gadget_udc_stop_locked(udc);
1695: mutex_unlock(&udc->connect_lock);
1696:
1697: mutex_lock(&udc_lock);
1698: driver->is_bound = false;
1699: udc->driver = NULL;
1700: mutex_unlock(&udc_lock);
1701:
1702: kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
1703: }
SourceFile:drivers/usb/gadget/udc/core.c]
|
| 43/2 |
2026/02/06 12:22 |
llm |
expert |
0m
Model:
Tokens:
input: 25879
output: 51
thoughts: 3249
Thoughts: **Analyzing a Potential Use-After-Free Vulnerability in `dummy_hcd`**
Okay, so I've been tracing the unbinding process within the `dummy_hcd` USB gadget driver, trying to understand a possible use-after-free issue. My main concern is how the host-side hub events interact with the gadget-side unregistration.
The core of the problem seems to lie in the timing between `usb_gadget_unregister_driver` and the host-side hub's handling of port events. When a gadget driver unregisters, it goes through a series of steps: First, `usb_gadget_disconnect_locked` runs, then `usb_gadget_disable_async_callbacks` is called, next `udc->driver->unbind(gadget)` is executed and finally `usb_gadget_udc_stop_locked(udc)` gets called. Crucially, the unbind function *should* null out the driver pointer, right? But the stack trace is suggesting the unregistration of the driver is not actually atomic, and events from the host side can still try to interact with the now-unregistered driver.
Looking at the `dummy_hcd` code, `dummy_pullup` calls `set_link_state`, which can trigger `gadget_disconnect`. This is called within the unregistration sequence itself. My first thought was this was fine as `dev` and the `gadget` were still valid, however, the stack trace shows `gadget_disconnect` being called from a `hub_event` workqueue, which is the host side of the `dummy_hcd`. This means this `hub_event` could be running concurrently with unregistration, potentially accessing freed memory.
The `hub_event` workqueue is triggered by the host-side hub. Specifically, `dummy_hub_control` in `dummy_hcd.c` handles host-side requests. This function calls `set_link_state`, which can then call `gadget_disconnect`. This is where the race condition likely lives. The `dummy_udc_stop` function sets `dum->driver = NULL`. The important part is that `set_link_state` checks `dum->ints_enabled`, but it drops the `dum->lock` *before* calling the driver's `disconnect` function. Meanwhile, `dummy_udc_stop` *also* takes the same lock.
The unbind process involves calling `usb_gadget_unregister_driver` which calls `gadget_unbind_driver`, eventually calling `dummy_udc_stop` to clear out the gadget driver state. However, if a host-side event comes in *after* `dummy_udc_stop` but *before* `raw_release` frees the device, we could get a Use-After-Free. Because `gadget_disconnect` dereferences the `dev` pointer, it would crash if `dev` was already freed.
The key to the vulnerability is the race between the unregistration and the hub event processing. Specifically, if the host-side event, handled by the `hub_event` workqueue, runs *after* `dummy_udc_stop` but *before* the device is freed, then the host-side processing *will* try to access freed memory, resulting in the Use-After-Free.
This appears to mean the `usb_gadget_unregister_driver` sequence does not properly synchronize with the host-side events within the `dummy_hcd`, making the host-side handling of events a source of potential UAF vulnerabilities.
|
| 44/2 |
2026/02/06 12:23 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/udc/dummy_hcd.c IncludeLines:true Name:dummy_udc_start]
Results: map[SourceCode: 996: static int dummy_udc_start(struct usb_gadget *g,
997: struct usb_gadget_driver *driver)
998: {
999: struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
1000: struct dummy *dum = dum_hcd->dum;
1001:
1002: switch (g->speed) {
1003: /* All the speeds we support */
1004: case USB_SPEED_LOW:
1005: case USB_SPEED_FULL:
1006: case USB_SPEED_HIGH:
1007: case USB_SPEED_SUPER:
1008: break;
1009: default:
1010: dev_err(dummy_dev(dum_hcd), "Unsupported driver max speed %d\n",
1011: driver->max_speed);
1012: return -EINVAL;
1013: }
1014:
1015: /*
1016: * DEVICE side init ... the layer above hardware, which
1017: * can't enumerate without help from the driver we're binding.
1018: */
1019:
1020: spin_lock_irq(&dum->lock);
1021: dum->devstatus = 0;
1022: dum->driver = driver;
1023: spin_unlock_irq(&dum->lock);
1024:
1025: return 0;
1026: }
SourceFile:drivers/usb/gadget/udc/dummy_hcd.c]
|
| 45/2 |
2026/02/06 12:23 |
llm |
expert |
0m
Model:
Tokens:
input: 30419
output: 17
thoughts: 770
Thoughts: **Analyzing a Potential Use-After-Free in USB Gadget Driver Unregistration**
Okay, let's break this down. The UAF report points to a read in `raw_gadget.c` after what appears to be a disconnect, but there's a problem, because line 68, where the fault occurs, involves a spinlock on `dev->queue`. This `dev` is a `struct raw_dev`, which, as the report points out, has been freed. The core of the issue seems to be how the `raw_dev` structure is handled during driver unregistration.
Now, if `gadget_disconnect` is called, it means `dum->driver->disconnect` was invoked at some point. This is important: it confirms that `dum->driver` was *not* NULL when `gadget_disconnect` executed. So, it hadn't been zeroed out before the disconnect happened.
The next piece is that `raw_release` must have completed *before* `gadget_disconnect` happens, as `gadget_disconnect` is part of the clean up process. If `raw_release` has finished, `usb_gadget_unregister_driver` must have returned. And since `usb_gadget_unregister_driver` returned, it follows that `dummy_udc_stop` must have finished successfully. However, `dummy_udc_stop` *should* set `dum->driver = NULL`. So, something feels off...
Wait. `dummy_udc_stop` is called from `gadget_unbind_driver`. Okay, that's where things start to come together. `gadget_unbind_driver` is triggered via `driver_unregister`. Let's confirm how the unregistration is meant to work. Checking the `usb_gadget_unregister_driver` code:
```c
1746: int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1747: {
1748: if (!driver || !driver->unbind)
1749: return -EINVAL;
1750:
1751: driver_unregister(&driver->driver);
1752: return 0;
1753: }
```
This *should* be synchronous. `driver_unregister` calls `bus_remove_driver`, which in turn calls `driver_detach`. So, the expectation is that the unregistration process cleans up. However, the `dummy_hcd` driver seems a bit special. Looking in `dummy_hcd.c`, I see:
```c
464: if (dum->ints_enabled && (disconnect || reset)) {
465: stop_activity(dum);
466: ++dum->callback_usage;
467: spin_unlock(&dum->lock);
468: if (reset)
469: usb_gadget_udc_reset(&dum->gadget, dum->driver);
470: else
471: dum->driver->disconnect(&dum->gadget);
472: spin_lock(&dum->lock);
473: --dum->callback_usage;
474: }
```
Aha! `dum->callback_usage` is incremented. Could this be a race? Does the code wait for `dum->callback_usage` to become zero? If so, this could explain the UAF, because the `raw_dev` could be freed before the disconnect callback completes. Let me search the `dummy_hcd.c` for any places where code *waits* for `dum->callback_usage` to hit zero.
|
| 46/2 |
2026/02/06 12:23 |
tool |
grepper |
0m
Args: map[Expression:callback_usage]
Results: map[Output:drivers/usb/gadget/udc/dummy_hcd.c=264=struct dummy {
--
drivers/usb/gadget/udc/dummy_hcd.c-271- int address;
drivers/usb/gadget/udc/dummy_hcd.c:272: int callback_usage;
drivers/usb/gadget/udc/dummy_hcd.c-273- struct usb_gadget gadget;
--
drivers/usb/gadget/udc/dummy_hcd.c=433=static void set_link_state(struct dummy_hcd *dum_hcd)
--
drivers/usb/gadget/udc/dummy_hcd.c-465- stop_activity(dum);
drivers/usb/gadget/udc/dummy_hcd.c:466: ++dum->callback_usage;
drivers/usb/gadget/udc/dummy_hcd.c-467- spin_unlock(&dum->lock);
--
drivers/usb/gadget/udc/dummy_hcd.c-472- spin_lock(&dum->lock);
drivers/usb/gadget/udc/dummy_hcd.c:473: --dum->callback_usage;
drivers/usb/gadget/udc/dummy_hcd.c-474- }
--
drivers/usb/gadget/udc/dummy_hcd.c-476- dum->ints_enabled) {
drivers/usb/gadget/udc/dummy_hcd.c:477: ++dum->callback_usage;
drivers/usb/gadget/udc/dummy_hcd.c-478- spin_unlock(&dum->lock);
--
drivers/usb/gadget/udc/dummy_hcd.c-483- spin_lock(&dum->lock);
drivers/usb/gadget/udc/dummy_hcd.c:484: --dum->callback_usage;
drivers/usb/gadget/udc/dummy_hcd.c-485- }
--
drivers/usb/gadget/udc/dummy_hcd.c=899=static int dummy_pullup(struct usb_gadget *_gadget, int value)
--
drivers/usb/gadget/udc/dummy_hcd.c-919- */
drivers/usb/gadget/udc/dummy_hcd.c:920: while (dum->callback_usage > 0) {
drivers/usb/gadget/udc/dummy_hcd.c-921- spin_unlock_irqrestore(&dum->lock, flags);
--
drivers/usb/gadget/udc/dummy_hcd.c=1788=static enum hrtimer_restart dummy_timer(struct hrtimer *t)
--
drivers/usb/gadget/udc/dummy_hcd.c-1924- if (value > 0) {
drivers/usb/gadget/udc/dummy_hcd.c:1925: ++dum->callback_usage;
drivers/usb/gadget/udc/dummy_hcd.c-1926- spin_unlock(&dum->lock);
--
drivers/usb/gadget/udc/dummy_hcd.c-1929- spin_lock(&dum->lock);
drivers/usb/gadget/udc/dummy_hcd.c:1930: --dum->callback_usage;
drivers/usb/gadget/udc/dummy_hcd.c-1931-
]
|
| 47/2 |
2026/02/06 12:23 |
llm |
expert |
0m
Model:
Tokens:
input: 32942
output: 20
thoughts: 2972
Thoughts: **Analyzing a Potential Use-After-Free in USB Gadget Driver Unregistration**
Okay, so I'm digging into this UAF issue that's popping up when unregistering a USB gadget driver, specifically within the `raw_gadget` framework. The initial thought process was: the driver unregistration sequence seems to involve several steps, and there's a delicate dance between `dummy_pullup`, `gadget_disconnect`, and `dummy_udc_stop`. `dummy_pullup` is designed to wait for any outstanding disconnect calls (via `callback_usage`), ensuring that the system doesn't proceed with unregistration before disconnects are handled. The UAF must be happening in `raw_event_queue_add`, which means `gadget_disconnect` is still being called.
My primary concern was around the timing: could `gadget_disconnect` be called after `dummy_pullup(0)` returns, potentially leading to a race? It seemed like the critical path involved the interplay between `set_link_state`, which can trigger `gadget_disconnect`, and the unregistration process that eventually calls `dummy_udc_stop`. In `set_link_state` it is checking for `dum->ints_enabled` to determine whether `gadget_disconnect` is called.
The `dummy_udc_stop` function sets `dum->ints_enabled = 0` and `dum->driver = NULL`. The key problem is the UAF, meaning that `dum->driver` is pointing to the freed memory of `struct raw_dev`. It can't be NULL since it would result in a null pointer dereference rather than a UAF.
Then, I was investigating the fact that the allocation and free are in the same task (6293), and the crash happened in kworker/1:10/5982, making it a race. I was then considering the possibility that there might be two `dummy_hcd` structures, one for high-speed and one for super-speed, but that these share the `dum` structure. This led to the hypothesis of the `set_link_state` being called on the incorrect `dummy_hcd`. But it must call `dum->driver` since otherwise there would be no call to `disconnect`.
But I think I've overlooked something critical in the state transitions. `raw_release` calls `usb_gadget_unregister_driver`, which should call `dummy_udc_stop`, setting `dum->driver = NULL` and `dum->ints_enabled = 0`. `set_link_state` checks `dum->ints_enabled`, so there should be no disconnect call.
Okay, I think I see the issue. `dummy_udc_stop` *must* be called eventually, and it does set the pointer to `NULL`.
A potential scenario might be this: `set_link_state` is called after the driver has been unregistered, but before `dummy_udc_stop` has completely finished and set `dum->driver` to `NULL`. In this case, `dum->driver` would still be valid (but pointing to the freed `struct raw_dev`) when `set_link_state` is called. This aligns with a UAF, because the memory is still allocated, it's just been freed and then subsequently re-used.
Now, I think I'm honing in on the timing window where this can happen. If `set_link_state` is running, then it's in the process of freeing the `struct raw_dev` and calling `dum->driver->disconnect`. If `dummy_udc_stop` runs after the `struct raw_dev` has been freed, then there is a UAF, since the pointer still exists after the object is freed. The lock would ensure that only one function can free the object, so there's no race.
|
| 48/2 |
2026/02/06 12:23 |
tool |
grepper |
0m
Error:
Args: map[Expression:ints_enabled = 1]
Results:
|
| 49/2 |
2026/02/06 12:23 |
llm |
expert |
0m
Model:
Tokens:
input: 36788
output: 17
thoughts: 13
Thoughts: Okay, let's dive into this code.
**Investigating the Modification Points of `ints_enabled`**
Right, my first instinct is always to trace back the variables. I need to understand where `ints_enabled` is being altered. If I'm trying to debug something related to its behavior, knowing its modification points is absolutely crucial. This is basic debugging 101, but the more experience you get the more efficient you get at it. So, let's start with a thorough search of the codebase to pin down every single instance where `ints_enabled` is being assigned a new value. That search will drive the rest of my investigation. From there, I'll be able to work backwards from any issues I find to the root cause.
|
| 50/2 |
2026/02/06 12:23 |
tool |
grepper |
0m
Args: map[Expression:ints_enabled]
Results: map[Output:Documentation/driver-api/device-io.rst=87=continuing execution::
--
Documentation/driver-api/device-io.rst-102- RD_REG_WORD(®->ictrl);
Documentation/driver-api/device-io.rst:103: ha->flags.ints_enabled = 0;
Documentation/driver-api/device-io.rst-104- }
--
arch/hexagon/include/uapi/asm/registers.h=26=struct pt_regs {
--
arch/hexagon/include/uapi/asm/registers.h-207- (((regs)->hvmer.vmest & (HVM_VMEST_UM_MSK << HVM_VMEST_UM_SFT)) != 0)
arch/hexagon/include/uapi/asm/registers.h:208:#define ints_enabled(regs) \
arch/hexagon/include/uapi/asm/registers.h-209- (((regs)->hvmer.vmest & (HVM_VMEST_IE_MSK << HVM_VMEST_IE_SFT)) != 0)
--
arch/hexagon/kernel/vm_events.c=22=void show_regs(struct pt_regs *regs)
--
arch/hexagon/kernel/vm_events.c-72- printk(KERN_EMERG "psp: \t0x%08lx badva: 0x%08lx int_enabled: %d\n",
arch/hexagon/kernel/vm_events.c:73: pt_psp(regs), pt_badva(regs), ints_enabled(regs));
arch/hexagon/kernel/vm_events.c-74-}
--
drivers/usb/gadget/udc/dummy_hcd.c=264=struct dummy {
--
drivers/usb/gadget/udc/dummy_hcd.c-277- u16 devstatus;
drivers/usb/gadget/udc/dummy_hcd.c:278: unsigned ints_enabled:1;
drivers/usb/gadget/udc/dummy_hcd.c-279- unsigned udc_suspended:1;
--
drivers/usb/gadget/udc/dummy_hcd.c=433=static void set_link_state(struct dummy_hcd *dum_hcd)
--
drivers/usb/gadget/udc/dummy_hcd.c-463- /* Report reset and disconnect events to the driver */
drivers/usb/gadget/udc/dummy_hcd.c:464: if (dum->ints_enabled && (disconnect || reset)) {
drivers/usb/gadget/udc/dummy_hcd.c-465- stop_activity(dum);
--
drivers/usb/gadget/udc/dummy_hcd.c-475- } else if (dum_hcd->active != dum_hcd->old_active &&
drivers/usb/gadget/udc/dummy_hcd.c:476: dum->ints_enabled) {
drivers/usb/gadget/udc/dummy_hcd.c-477- ++dum->callback_usage;
--
drivers/usb/gadget/udc/dummy_hcd.c=942=static void dummy_udc_async_callbacks(struct usb_gadget *_gadget, bool enable)
--
drivers/usb/gadget/udc/dummy_hcd.c-946- spin_lock_irq(&dum->lock);
drivers/usb/gadget/udc/dummy_hcd.c:947: dum->ints_enabled = enable;
drivers/usb/gadget/udc/dummy_hcd.c-948- spin_unlock_irq(&dum->lock);
--
drivers/usb/gadget/udc/dummy_hcd.c=1028=static int dummy_udc_stop(struct usb_gadget *g)
--
drivers/usb/gadget/udc/dummy_hcd.c-1033- spin_lock_irq(&dum->lock);
drivers/usb/gadget/udc/dummy_hcd.c:1034: dum->ints_enabled = 0;
drivers/usb/gadget/udc/dummy_hcd.c-1035- stop_activity(dum);
--
drivers/usb/gadget/udc/dummy_hcd.c=1576=static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
--
drivers/usb/gadget/udc/dummy_hcd.c-1582- return NULL;
drivers/usb/gadget/udc/dummy_hcd.c:1583: if (!dum->ints_enabled)
drivers/usb/gadget/udc/dummy_hcd.c-1584- return NULL;
]
|
| 51/2 |
2026/02/06 12:23 |
llm |
expert |
0m
Model:
Tokens:
input: 38195
output: 42
thoughts: 6108
Thoughts: **Analysis of a USB Gadget Driver Unregister Race Condition**
Okay, here's the breakdown of my thought process in analyzing this potential use-after-free vulnerability:
My focus starts with the `dummy_udc_async_callbacks` function and its link to enabling and disabling asynchronous callbacks. I immediately connect this to the `usb_gadget_enable_async_callbacks` and `usb_gadget_disable_async_callbacks` calls, tracing these to their usages in binding and unbinding the driver. I note that `dummy_udc_async_callbacks` sets and unsets `dum->ints_enabled`, which controls whether disconnect events trigger hub control functions. I quickly see that `gadget_unbind_driver` is critical because it disables these callbacks.
I then focus on the `set_link_state` function. I realize it is the key function to watch for. My concern becomes around when it calls `disconnect`, and under what conditions. The check on `dum->ints_enabled` becomes important. I try to understand the flow when the `disconnect` is called and what happens before and after.
I notice that `dummy_hub_control` and by extension, `set_link_state` is on the path of `usb_hcd_submit_urb` which will then be called from `ClearPortFeature(POWER)` by the hub event workqueue. I now suspect this could be important.
I then think about the unregistration path, namely `usb_gadget_unregister_driver` and `gadget_unbind_driver`. I know that `gadget_unbind_driver` will call `usb_gadget_disable_async_callbacks`, and `usb_gadget_disconnect_locked`, which triggers calls to the lower level UDC calls like `dummy_pullup` which, in turn will call `set_link_state`. The order of these function calls is crucial for the race condition.
I start to analyze the data-flow of `ints_enabled` to understand where it is set and unset. `ints_enabled` is set to 1 in the binding and set to zero in the unbinding. This means that if it's set to zero, then `disconnect` will not be called. Now I start to think that this might be safe.
However, I note that `dummy_hub_control` doesn't take the `udc->connect_lock` which means it might be running asynchronously.
This raises a red flag! If `dummy_hub_control` is running, then it calls `set_link_state` and checks `dum->ints_enabled`. If `ints_enabled` is set to one during that check (the unbind hasn't happened yet), and before it calls `disconnect`, this is a very important race condition. If it is, then `set_link_state` then calls the `disconnect` function on a driver that is in the process of being unregistered. This means the driver object might be freed after it checks `ints_enabled`, but before it can get a chance to call the registered `disconnect` function.
I analyze the various paths to unregister the driver. The `unregister` variable will become true if the gadget is registered. If it is, it will attempt to unregister it with `usb_gadget_unregister_driver`.
I dig deeper into the code, I find that `raw_release` calls `usb_gadget_unregister_driver` if the gadget is registered, but it can avoid it if the gadget is not set.
The `dev->gadget` member becomes important. If it is null in `raw_release` then the unregister is skipped. It is set in `gadget_bind` and cleared in `gadget_unbind`. This means that if `raw_release` is called, it might not unregister the driver.
I confirm my suspicions by reviewing the stack trace, which points the UAF is in `raw_event_queue_add`, specifically within `spin_lock_irqsave`. This happens when `dev` is already freed, and thus is the core issue.
This indicates that `gadget_disconnect` is still being called, but `dev` has been freed, suggesting an incomplete unregistration.
This leads me to `dummy_hcd.c`, and the key here is that it has *two* `dummy_hcd` structures, `hs_hcd` and `ss_hcd` and that they both share the same `dum` data structure.
I realize that even though I am unregistering with one UDC, the other one is still potentially active.
The key observation is: `dummy_udc_stop` sets `dum->driver = NULL`. *However*, `dummy_udc_start` *doesn'
|
| 52/2 |
2026/02/06 12:24 |
tool |
codesearch-struct-layout |
0m
Args: map[ContextFile:drivers/usb/gadget/legacy/raw_gadget.c Name:raw_dev]
Results: map[Fields:[map[Name:count OffsetBits:0 SizeBits:32] map[Name:lock OffsetBits:64 SizeBits:1024] map[Name:udc_name OffsetBits:1088 SizeBits:64] map[Name:driver OffsetBits:1152 SizeBits:1856] map[Name:dev OffsetBits:3008 SizeBits:64] map[Name:driver_id_number OffsetBits:3072 SizeBits:32] map[Name:state OffsetBits:3104 SizeBits:32] map[Name:gadget_registered OffsetBits:3136 SizeBits:8] map[Name:gadget OffsetBits:3200 SizeBits:64] map[Name:req OffsetBits:3264 SizeBits:64] map[Name:ep0_in_pending OffsetBits:3328 SizeBits:8] map[Name:ep0_out_pending OffsetBits:3336 SizeBits:8] map[Name:ep0_urb_queued OffsetBits:3344 SizeBits:8] map[Name:ep0_status OffsetBits:3392 SizeBits:64] map[Name:eps OffsetBits:3456 SizeBits:13440] map[Name:eps_num OffsetBits:16896 SizeBits:32] map[Name:ep0_done OffsetBits:16960 SizeBits:704] map[Name:queue OffsetBits:17664 SizeBits:2816]]]
|
| 53/2 |
2026/02/06 12:24 |
llm |
expert |
0m
Model:
Tokens:
input: 45782
output: 44
thoughts: 64
Thoughts: **Analyzing Memory Access**
Okay, so I'm looking at a specific memory interaction. I've got `queue` sitting at an offset of 17664 bits, which translates to 2208 bytes. The access itself is happening at byte offset 2232.
Right, so to understand the offset from the base, I'll calculate the difference: 2232 minus 2208 gives us 24 bytes. This looks like a relative offset within the `queue` structure itself.
Now, let's dive into that structure. I need to take a look at the definition of `struct raw_event_queue` to figure out what that 24-byte offset signifies. I should be able to deduce exactly what member is being accessed based on its size and the layout within the structure. This is critical for understanding the nature of the memory read or write. Time to cross-reference the source code.
|
| 54/2 |
2026/02/06 12:24 |
tool |
codesearch-struct-layout |
0m
Args: map[ContextFile:drivers/usb/gadget/legacy/raw_gadget.c Name:raw_event_queue]
Results: map[Fields:[map[Name:lock OffsetBits:0 SizeBits:1024] map[Name:sema OffsetBits:1024 SizeBits:704] map[Name:events OffsetBits:1728 SizeBits:1024] map[Name:size OffsetBits:2752 SizeBits:32]]]
|
| 55/2 |
2026/02/06 12:24 |
llm |
expert |
0m
Model:
Tokens:
input: 46189
output: 51
thoughts: 1028
Thoughts: **Analyzing a Potential Race Condition in USB Gadget Driver Code**
Okay, let's break this down. First, I'm noting that the `lock` within the `raw_event_queue` structure, specifically a `spinlock_t`, is located at offset 0. This spinlock is pretty hefty on a PREEMPT_RT system, consuming a significant amount of memory (128 bytes). The access in question is at offset 24 bytes within the `queue` struct, which falls within the `lock` field. Therefore, `raw_event_queue_add` is indeed accessing `queue->lock`.
The fact that this manifests as a Use-After-Free (UAF) rather than a NULL pointer dereference is crucial. It tells me that `dev` was not NULL, meaning `get_gadget_data(gadget)` returned a valid pointer. This suggests `gadget_unbind` either hadn't been called or hadn't completed yet, even though `raw_release` has finished and freed `dev`. This implies that `usb_gadget_unregister_driver` must have returned successfully.
If `usb_gadget_unregister_driver` returned, it means `gadget_unbind_driver` should have completed. `gadget_unbind_driver` calls `udc->driver->unbind(gadget)`, which is `gadget_unbind`, and `gadget_unbind` then sets `driver_data` to NULL.
Aha! Here's where it gets interesting. I've realized that `gadget_unbind_driver` calls `unbind` WITHOUT holding `udc->connect_lock`. However, `dummy_hub_control` and `set_link_state` also don't use the `udc->connect_lock`; they utilize `dum->lock` instead.
If `set_link_state` is running on one CPU, it acquires `dum->lock`, increments `callback_usage`, releases `dum->lock`, and then calls `disconnect`. And `disconnect` then calls `raw_queue_event(dev, ...)`. Concurrently, `raw_release` calls `usb_gadget_unregister_driver`, which in turn calls `gadget_unbind_driver`. `gadget_unbind_driver` then calls `usb_gadget_disconnect_locked`, and then `dummy_pullup(0)`. `dummy_pullup(0)` takes `dum->lock`, and notices that `callback_usage > 0`, and waits.
So, `dummy_pullup(0)` is waiting for `disconnect` to complete. Once `disconnect` completes, `set_link_state` re-acquires `dum->lock` and decrements `callback_usage`. After that, `dummy_pullup(0)` can proceed and return. Then, `gadget_unbind_driver` calls `udc->driver->unbind(gadget)`, and also `dummy_udc_stop`. Finally, `usb_gadget_unregister_driver` returns, and `raw_release` frees `dev`. But this sequence seems safe!
Wait! I must be missing something in the stack trace. Examining it, I see that `dummy_hub_control` is called from `usb_hcd_submit_urb`, which, in `dummy_hcd.c`, is called from a thread, specifically the `hub_event` workqueue, and is NOT called from interrupt context.
Could `gadget_disconnect` be called TWICE? And if `set_link_state` is called from both `dummy_pullup(0)` AND from `dummy_hub_control`? This seems unlikely, since they both take `dum->lock`.
Aha! Now I see it! In `raw_gadget.c`, `gadget_disconnect` calls `raw_queue_event`, which calls `raw_event_queue_add`. This adds `up(&queue->sema)`. Does anything wait on this semaphore? Yes, `raw_ioctl_event_fetch` (probably).
Okay, time to analyze `raw_ioctl_event_fetch`.
|
| 56/2 |
2026/02/06 12:24 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/legacy/raw_gadget.c IncludeLines:true Name:raw_event_queue_fetch]
Results: map[SourceCode: 89: static struct usb_raw_event *raw_event_queue_fetch(
90: struct raw_event_queue *queue)
91: {
92: int ret;
93: unsigned long flags;
94: struct usb_raw_event *event;
95:
96: /*
97: * This function can be called concurrently. We first check that
98: * there's at least one event queued by decrementing the semaphore,
99: * and then take the lock to protect queue struct fields.
100: */
101: ret = down_interruptible(&queue->sema);
102: if (ret)
103: return ERR_PTR(ret);
104: spin_lock_irqsave(&queue->lock, flags);
105: /*
106: * queue->size must have the same value as queue->sema counter (before
107: * the down_interruptible() call above), so this check is a fail-safe.
108: */
109: if (WARN_ON(!queue->size)) {
110: spin_unlock_irqrestore(&queue->lock, flags);
111: return ERR_PTR(-ENODEV);
112: }
113: event = queue->events[0];
114: queue->size--;
115: memmove(&queue->events[0], &queue->events[1],
116: queue->size * sizeof(queue->events[0]));
117: spin_unlock_irqrestore(&queue->lock, flags);
118: return event;
119: }
SourceFile:drivers/usb/gadget/legacy/raw_gadget.c]
|
| 57/2 |
2026/02/06 12:24 |
llm |
expert |
0m
Model:
Tokens:
input: 48648
output: 50
thoughts: 3702
Thoughts: **Analyzing the UAF in `raw_gadget` and `dummy_hcd`**
Okay, so I'm diving into this UAF issue. At first glance, the UAF isn't stemming directly from the `STATE_DEV_CLOSED` or `STATE_DEV_FAILED` transitions in `raw_gadget.c` as initially suspected. The clue must be in the `raw_release` function, specifically with the `kref_put` calls. Double calls to `kref_put` if `unregister` is true are suspicious. If `raw_ioctl_run` has been called, `dev->count` is initialized as 2, and then in `raw_release` the count is decremented to 0 with those `kref_put` calls. This frees the device. However, `usb_gadget_unregister_driver` is called before this happens, so the timing's crucial.
The unregistration process should be synchronous. The report has led me to review `dummy_hcd.c`, and the `dum->driver = NULL` assignment in `dummy_udc_stop` might be critical. If `gadget_disconnect` is called after `gadget_unbind` has run in `raw_gadget`, it's possible that `get_gadget_data(gadget)` still returns the previous `dev` if the unbind has not fully completed.
The key observation is `gadget_disconnect` in `raw_gadget.c` does not check for a NULL `dev`. If `dev` is NULL, then `raw_queue_event` is called, which then accesses `&dev->queue` which would be an offset from NULL, that would cause an issue. However, the address in the report, ffff8880524608b8, is a valid heap address, so `dev` cannot be NULL. `get_gadget_data(gadget)` still returns the old `dev` at this point. That tells me that `gadget_unbind` has not fully completed.
But if `raw_release` has finished and freed `dev`, then `usb_gadget_unregister_driver` must have returned. This means `gadget_unbind_driver` must have finished, so `gadget_unbind` must have as well. Unless there's a race? Let me look at that in `raw_release`.
Okay, there's a very subtle race in `raw_release`. The code checks if `dev->gadget` is NULL, and if it is, the unregistration is skipped. However, that check happens after `dev->state` is set to CLOSED. Now I'm checking `dev->gadget_registered` is true, but that only becomes true *after* `usb_gadget_register_driver` has been called. If `usb_gadget_register_driver` returns an error, `dev->gadget_registered` might never get set, so `unregister` remains false and it won't call `usb_gadget_unregister_driver`, but the kref is still dropped at line 474!
So, the next thing that I'm looking at in `raw_gadget.c` is the `kref_get` in `raw_ioctl_run`. That reference should be dropped in `raw_release`. I'm going through the `raw_release` code, and I see again that there is that `kref_put`. If it's true, it unregisters the driver.
The potential issue is that if `raw_release` is running while `raw_queue_event` is happening, it sets `dev->state` to `STATE_DEV_CLOSED`, but it doesn't check it before calling `raw_event_queue_add`, which means `dev->state` is not checked. But I still can't explain the `dev` being freed. So now I'm back to `kref_put` at line 474, and the other at line 469.
If `dev->count` is 2, and both are called, `dev` is freed.
Now I am going back to `dummy_hcd.c`. The `set_link_state` and `dummy_hub_control` are the entrypoints to the `dummy_hcd` code. The key is to look at the interactions between `dummy_udc_stop` and `set_link_state`. The race that can occur between lines 467 and 471 is very subtle, and if it does occur then there is a UAF on `dum->driver`.
If `usb_gadget_unregister_driver` is called, it calls
|
| 58/2 |
2026/02/06 12:25 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/udc/core.c IncludeLines:true Name:usb_gadget_disconnect_locked]
Results: map[SourceCode: 758: static int usb_gadget_disconnect_locked(struct usb_gadget *gadget)
759: __must_hold(&gadget->udc->connect_lock)
760: {
761: int ret = 0;
762:
763: if (!gadget->ops->pullup) {
764: ret = -EOPNOTSUPP;
765: goto out;
766: }
767:
768: if (!gadget->connected)
769: goto out;
770:
771: if (gadget->deactivated || !gadget->udc->started) {
772: /*
773: * If gadget is deactivated we only save new state.
774: * Gadget will stay disconnected after activation.
775: */
776: gadget->connected = false;
777: goto out;
778: }
779:
780: ret = gadget->ops->pullup(gadget, 0);
781: if (!ret)
782: gadget->connected = 0;
783:
784: mutex_lock(&udc_lock);
785: if (gadget->udc->driver)
786: gadget->udc->driver->disconnect(gadget);
787: mutex_unlock(&udc_lock);
788:
789: out:
790: trace_usb_gadget_disconnect(gadget, ret);
791:
792: return ret;
793: }
SourceFile:drivers/usb/gadget/udc/core.c]
|
| 59/2 |
2026/02/06 12:25 |
llm |
expert |
0m
Model:
Tokens:
input: 53928
output: 51
thoughts: 3036
Thoughts: **Analyzing a Potential Use-After-Free Vulnerability in the USB Gadget Driver**
Okay, let's break this down. First, it looks like there's a problem stemming from the interaction between `raw_gadget` and `dummy_hcd` drivers, specifically when a device is being disconnected. The core of the issue seems to revolve around the `gadget->connected` flag and the timing of disconnect callbacks. I'm seeing a potential race condition where a disconnect callback might execute on a freed `raw_dev` structure.
The initial thought process checks the standard flow. If `gadget->connected` is false, it *should* short-circuit, avoiding the call to `pullup(0)` and, consequently, `dummy_pullup(0)`, which would wait on `callback_usage`. But the key is that if `gadget->connected` *is* false, the disconnect path *shouldn't* be active. However, `set_link_state` in the `dummy_hcd` driver doesn't seem to check this crucial flag.
My focus is the fact that `set_link_state` within `dummy_hcd` calls `disconnect` directly without checking `gadget->connected`. This bypasses the normal checks and can lead to a situation where the driver might be attempting to operate on an already disconnected and potentially freed device structure. In the scenario where the gadget unregisters, the UDC core calls `usb_gadget_disconnect_locked`, which *should* handle the disconnect, but if there's a simultaneous disconnect triggered by `dummy_hcd`, the race is on.
Now, let's dissect the control flow: `hub_event` starts by clearing port power. Then, `dummy_hub_control` calls `set_link_state`. The interesting part is that `set_link_state` will call the `dum->driver->disconnect` callback *after* releasing a spinlock. Meanwhile, `raw_release` might simultaneously call `usb_gadget_unregister_driver` and `usb_gadget_disconnect_locked`. Crucially, `usb_gadget_disconnect_locked` checks `gadget->connected`, which could be *false* because it may have already been disconnected. The key vulnerability arises because `raw_gadget` doesn't protect against the `dev` structure being freed.
Let's delve into when `gadget->connected` is set. The state of `gadget->connected` is determined by `usb_gadget_connect_locked` and set to true when the gadget is connected. This is called in `dummy_hcd` in `usb_gadget_connect_locked` from the `gadget_bind_driver` function which calls the function `udc_start`. The function `udc_start` sets a variable that should enable the `gadget->connected` flag.
Wait, the key is the behavior of the UDC core and `dummy_hcd` relative to `gadget->connected`. My initial assumption was that the core driver was managing it, which is correct, and I'm starting to see that in combination with the `dummy_hcd` calls to `disconnect` that are independent.
A quick review of `usb_gadget_connect_locked` shows that it *also* checks for `gadget->deactivated` and if `udc->started` is false, it sets `gadget->connected = true` but *doesn't* call `pullup`. This is fine because `udc_start` is called before the call to `connect`, so `udc->started` will always be true when `usb_gadget_connect_locked` is called. So in that situation, a call to pullup is guaranteed, and `gadget->connected` is correctly set.
Ok, here's where it gets interesting. `usb_gadget_disconnect_locked` in the UDC core calls `pullup(0)` and then calls the driver `disconnect` function. In a concurrent state, this could lead to the race I am trying to explain.
There is a simple explanation - `raw_gadget` does not take a reference to `dev`. `raw_gadget` relies on the synchronous return of `usb_gadget_unregister_driver`. The `dummy_hcd` driver however, has a race because it drops a spinlock to call the callback. This will potentially lead to the problem of a use after free vulnerability.
In the described sequence, `set_link_state` and `usb_gadget_unregister_driver` can overlap. This allows the UAF due to the non-checking of the `dev` pointer being used in `gadget_disconnect`. If `gadget->connected` is
|
| 60/2 |
2026/02/06 12:25 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/udc/core.c IncludeLines:true Name:usb_gadget_udc_reset]
Results: map[SourceCode:1188: /**
1189: * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
1190: * @gadget: The gadget which bus reset occurs
1191: * @driver: The gadget driver we want to notify
1192: *
1193: * If the udc driver has bus reset handler, it needs to call this when the bus
1194: * reset occurs, it notifies the gadget driver that the bus reset occurs as
1195: * well as updates gadget state.
1196: */
1197: void usb_gadget_udc_reset(struct usb_gadget *gadget,
1198: struct usb_gadget_driver *driver)
1199: {
1200: driver->reset(gadget);
1201: usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
1202: }
SourceFile:drivers/usb/gadget/udc/core.c]
|
| 61/2 |
2026/02/06 12:25 |
llm |
expert |
0m
Model:
Tokens:
input: 58324
output: 50
thoughts: 1750
Thoughts: **Debugging the USB Gadget Driver: A Deep Dive**
Okay, let's break this down step by step. I'm starting by looking for the root cause of the issue, and I'm currently focused on a potential use-after-free (UAF) vulnerability. I initially suspected an issue with `gadget->connected` not being properly managed, but that seems not to be the case. I've ruled out that path.
The clue that's leading me is how `set_link_state` and `usb_gadget_disconnect` are being called, or rather, not called. Specifically, `set_link_state` calls `dum->driver->disconnect`, but it doesn't utilize `usb_gadget_disconnect`. This means that the `gadget->connected` flag can be set to true and then not be unset properly. The call to `usb_gadget_disconnect_locked` *should* then call `pullup(0)` if `gadget->connected` is still 1, but this seems to not be the issue.
I then started looking at the sequence in `gadget_unbind_driver`. I see that `usb_gadget_disconnect_locked` *is* called, but the following call to `usb_gadget_disable_async_callbacks` seems important. That call does a lot of work that I need to keep in mind. Specifically, it calls `dummy_udc_async_callbacks(false)`, which sets `dum->ints_enabled = 0`. That flag gets used in `set_link_state`, so this interaction between the two routines is one to watch. Though this appears to be benign given what is known.
Now, the UAF is in `raw_event_queue_add`, which is called from `raw_queue_event`. The crucial point is that `dev` (a `struct raw_dev`) is being used after it's been freed. Iām thinking through the possible race conditions that could lead to this. The fact that `dev` was freed tells me that `raw_release` must have finished, so that part is correct.
I am now asking myself, can `raw_release` finish, but `usb_gadget_unregister_driver` *not* be called? The free stack seems to show that `raw_release` was indeed called. I'm now looking at the code in `raw_release`. I'm seeing that there are two `kref_put` calls and a conditional `usb_gadget_unregister_driver` call. If `dev->count` is 1 and `unregister` is true, then `dev` gets freed at line 469. If `raw_ioctl_run` *wasn't* called, then `count` is 1 and `unregister` is false, and `dev` gets freed at line 474. If `raw_ioctl_run` was called, `count` is 2, and `dev` is freed by the second `kref_put`. So `dev` is always freed *after* `usb_gadget_unregister_driver` returns.
I think I see a potential problem in `raw_gadget.c` in `gadget_setup`, `gadget_suspend`, `gadget_resume` and `gadget_reset`. All of these call `raw_queue_event(dev, ...)`. Any call after `dev` has been freed leads to UAF. But these functions shouldn't be called after `usb_gadget_unregister_driver` has returned.
Then I see it! The `hub_event` workqueue is calling `dummy_hub_control`. That calls `set_link_state`. `set_link_state` then calls `gadget_disconnect`. If `usb_gadget_unregister_driver` has already returned, `dum->driver` should be NULL. If `dum->driver` is NULL, `set_link_state` *shouldn't* call the driver.
Bingo! I've found it: In `dummy_hcd.c`, `dummy_udc_stop` sets `dum->driver = NULL`. But `set_link_state` *uses* `dum->driver`. So, if `set_link_state` is called AFTER `dummy_udc_stop` has finished, it will dereference a NULL pointer and crash! This is the likely UAF. It could also lead to other issues.
However, I'm noting that a NULL dereference is not, strictly speaking, a UAF; it is a dereference.
Okay, let me refine my understanding. What if `dummy_udc_stop` *isn't* called? When would that happen
|
| 62/2 |
2026/02/06 12:26 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:drivers/usb/gadget/udc/core.c IncludeLines:true Name:udc_bind_to_driver]
Results: map[SourceCode: SourceFile:]
|
| 63/2 |
2026/02/06 12:26 |
llm |
expert |
0m
Model:
Tokens:
input: 61170
output: 17
thoughts: 12
Thoughts: Okay, here's my summarized thought process, geared towards an expert:
**Locating the `bind` Calls**
Alright, the task at hand is to understand how `bind` is being used. My immediate approach is to dive directly into the code and **locate every instance where `bind` is actually *called***. This is the fundamental first step. I need a comprehensive overview of its usage patterns. I'm not just interested in *where* it's defined, but *how* it's being invoked within the system. This gives me the raw data I need to then look for potential problems, optimizations, or unexpected behaviors.
|
| 64/2 |
2026/02/06 12:26 |
tool |
grepper |
0m
Args:
Results: map[Output:crypto/af_alg.c=148=static int alg_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int addr_len)
--
crypto/af_alg.c-183-
crypto/af_alg.c:184: private = type->bind(sa->salg_name, sa->salg_feat, sa->salg_mask);
crypto/af_alg.c-185- if (IS_ERR(private)) {
--
drivers/acpi/glue.c=352=void acpi_device_notify(struct device *dev)
--
drivers/acpi/glue.c-388- if (adev->handler && adev->handler->bind)
drivers/acpi/glue.c:389: adev->handler->bind(dev);
drivers/acpi/glue.c-390-
--
drivers/base/component.c=227=static int try_to_bring_up_aggregate_device(struct aggregate_device *adev,
--
drivers/base/component.c-248- /* Found all components */
drivers/base/component.c:249: ret = adev->ops->bind(adev->parent);
drivers/base/component.c-250- if (ret < 0) {
--
drivers/base/component.c=633=static int component_bind(struct component *component, struct aggregate_device *adev,
--
drivers/base/component.c-658-
drivers/base/component.c:659: ret = component->ops->bind(component->dev, adev->parent, data);
drivers/base/component.c-660- if (!ret) {
--
drivers/block/drbd/drbd_receiver.c=400=static struct socket *drbd_try_connect(struct drbd_connection *connection)
--
drivers/block/drbd/drbd_receiver.c-452- what = "bind before connect";
drivers/block/drbd/drbd_receiver.c:453: err = sock->ops->bind(sock, (struct sockaddr_unsized *) &src_in6, my_addr_len);
drivers/block/drbd/drbd_receiver.c-454- if (err < 0)
--
drivers/block/drbd/drbd_receiver.c=507=static int prepare_listen_socket(struct drbd_connection *connection, struct accept_wait_data *ad)
--
drivers/block/drbd/drbd_receiver.c-539- what = "bind before listen";
drivers/block/drbd/drbd_receiver.c:540: err = s_listen->ops->bind(s_listen, (struct sockaddr_unsized *)&my_addr, my_addr_len);
drivers/block/drbd/drbd_receiver.c-541- if (err < 0)
--
drivers/char/agp/alpha-agp.c=84=static int alpha_core_agp_insert_memory(struct agp_memory *mem, off_t pg_start,
--
drivers/char/agp/alpha-agp.c-98-
drivers/char/agp/alpha-agp.c:99: status = agp->ops->bind(agp, pg_start, mem);
drivers/char/agp/alpha-agp.c-100- mb();
--
drivers/gpu/drm/msm/msm_drv.c=107=static int msm_drm_init(struct device *dev, const struct drm_driver *drv,
--
drivers/gpu/drm/msm/msm_drv.c-155- if (gpu_ops)
drivers/gpu/drm/msm/msm_drv.c:156: ret = gpu_ops->bind(dev, dev, NULL);
drivers/gpu/drm/msm/msm_drv.c-157- else
--
drivers/gpu/drm/nouveau/nvkm/core/object.c=136=nvkm_object_bind(struct nvkm_object *object, struct nvkm_gpuobj *gpuobj,
--
drivers/gpu/drm/nouveau/nvkm/core/object.c-139- if (object->func->bind)
drivers/gpu/drm/nouveau/nvkm/core/object.c:140: return object->func->bind(object, gpuobj, align, pgpuobj);
drivers/gpu/drm/nouveau/nvkm/core/object.c-141- return -ENODEV;
--
drivers/gpu/drm/nouveau/nvkm/engine/disp/chan.c=79=nvkm_disp_chan_child_new(const struct nvkm_oclass *oclass, void *argv, u32 argc,
--
drivers/gpu/drm/nouveau/nvkm/engine/disp/chan.c-98-
drivers/gpu/drm/nouveau/nvkm/engine/disp/chan.c:99: object->hash = chan->func->bind(chan, object->oproxy.object, oclass->handle);
drivers/gpu/drm/nouveau/nvkm/engine/disp/chan.c-100- if (object->hash < 0)
--
drivers/gpu/drm/nouveau/nvkm/engine/dma/user.c=47=nvkm_dmaobj_bind(struct nvkm_object *base, struct nvkm_gpuobj *gpuobj,
--
drivers/gpu/drm/nouveau/nvkm/engine/dma/user.c-50- struct nvkm_dmaobj *dmaobj = nvkm_dmaobj(base);
drivers/gpu/drm/nouveau/nvkm/engine/dma/user.c:51: return dmaobj->func->bind(dmaobj, gpuobj, align, pgpuobj);
drivers/gpu/drm/nouveau/nvkm/engine/dma/user.c-52-}
--
drivers/gpu/drm/nouveau/nvkm/engine/fifo/chan.c=41=nvkm_chan_cctx_bind(struct nvkm_chan *chan, struct nvkm_engn *engn, struct nvkm_cctx *cctx)
--
drivers/gpu/drm/nouveau/nvkm/engine/fifo/chan.c-61- /* Update context pointer. */
drivers/gpu/drm/nouveau/nvkm/engine/fifo/chan.c:62: engn->func->bind(engn, cctx, chan);
drivers/gpu/drm/nouveau/nvkm/engine/fifo/chan.c-63-
--
drivers/gpu/drm/nouveau/nvkm/engine/fifo/uchan.c=286=nvkm_uchan_init(struct nvkm_object *object)
--
drivers/gpu/drm/nouveau/nvkm/engine/fifo/uchan.c-293- if (chan->func->bind)
drivers/gpu/drm/nouveau/nvkm/engine/fifo/uchan.c:294: chan->func->bind(chan);
drivers/gpu/drm/nouveau/nvkm/engine/fifo/uchan.c-295-
--
drivers/gpu/drm/vmwgfx/vmwgfx_resource.c=372=static int vmw_resource_do_validate(struct vmw_resource *res,
--
drivers/gpu/drm/vmwgfx/vmwgfx_resource.c-388- (!func->needs_guest_memory && val_buf->bo))) {
drivers/gpu/drm/vmwgfx/vmwgfx_resource.c:389: ret = func->bind(res, val_buf);
drivers/gpu/drm/vmwgfx/vmwgfx_resource.c-390- if (unlikely(ret != 0))
--
drivers/infiniband/hw/erdma/erdma_cm.c=989=static int kernel_bindconnect(struct socket *s, struct sockaddr *laddr,
--
drivers/infiniband/hw/erdma/erdma_cm.c-995- sock_set_reuseaddr(s->sk);
drivers/infiniband/hw/erdma/erdma_cm.c:996: ret = s->ops->bind(s, (struct sockaddr_unsized *)laddr, laddrlen);
drivers/infiniband/hw/erdma/erdma_cm.c-997- if (ret)
--
drivers/infiniband/hw/erdma/erdma_cm.c=1296=int erdma_create_listen(struct iw_cm_id *id, int backlog)
--
drivers/infiniband/hw/erdma/erdma_cm.c-1317-
drivers/infiniband/hw/erdma/erdma_cm.c:1318: ret = s->ops->bind(s, (struct sockaddr_unsized *)laddr,
drivers/infiniband/hw/erdma/erdma_cm.c-1319- sizeof(struct sockaddr_in));
--
drivers/infiniband/sw/siw/siw_cm.c=1374=static int kernel_bindconnect(struct socket *s, struct sockaddr *laddr,
--
drivers/infiniband/sw/siw/siw_cm.c-1391-
drivers/infiniband/sw/siw/siw_cm.c:1392: rv = s->ops->bind(s, (struct sockaddr_unsized *)laddr, size);
drivers/infiniband/sw/siw/siw_cm.c-1393- if (rv < 0)
--
drivers/infiniband/sw/siw/siw_cm.c=1808=int siw_create_listen(struct iw_cm_id *id, int backlog)
--
drivers/infiniband/sw/siw/siw_cm.c-1842- }
drivers/infiniband/sw/siw/siw_cm.c:1843: rv = s->ops->bind(s, (struct sockaddr_unsized *)laddr,
drivers/infiniband/sw/siw/siw_cm.c-1844- sizeof(struct sockaddr_in));
--
drivers/infiniband/sw/siw/siw_cm.c-1866- }
drivers/infiniband/sw/siw/siw_cm.c:1867: rv = s->ops->bind(s, (struct sockaddr_unsized *)laddr,
drivers/infiniband/sw/siw/siw_cm.c-1868- sizeof(struct sockaddr_in6));
--
drivers/isdn/mISDN/l1oip_core.c=633=l1oip_socket_thread(void *data)
--
drivers/isdn/mISDN/l1oip_core.c-678- /* bind to incoming port */
drivers/isdn/mISDN/l1oip_core.c:679: if (socket->ops->bind(socket, (struct sockaddr_unsized *)&hc->sin_local,
drivers/isdn/mISDN/l1oip_core.c-680- sizeof(hc->sin_local))) {
--
drivers/net/ethernet/mellanox/mlxsw/spectrum_span.c=1499=mlxsw_sp_span_trigger_entry_create(struct mlxsw_sp_span *span,
--
drivers/net/ethernet/mellanox/mlxsw/spectrum_span.c-1520-
drivers/net/ethernet/mellanox/mlxsw/spectrum_span.c:1521: err = trigger_entry->ops->bind(trigger_entry);
drivers/net/ethernet/mellanox/mlxsw/spectrum_span.c-1522- if (err)
--
drivers/net/usb/usbnet.c=1722=usbnet_probe(struct usb_interface *udev, const struct usb_device_id *prod)
--
drivers/net/usb/usbnet.c-1802- if (info->bind) {
drivers/net/usb/usbnet.c:1803: status = info->bind(dev, udev);
drivers/net/usb/usbnet.c-1804- if (status < 0)
--
drivers/pci/endpoint/pci-epf-core.c=59=int pci_epf_bind(struct pci_epf *epf)
--
drivers/pci/endpoint/pci-epf-core.c-122- epf_vf->sec_epc = epf->sec_epc;
drivers/pci/endpoint/pci-epf-core.c:123: ret = epf_vf->driver->ops->bind(epf_vf);
drivers/pci/endpoint/pci-epf-core.c-124- if (ret)
--
drivers/pci/endpoint/pci-epf-core.c-128-
drivers/pci/endpoint/pci-epf-core.c:129: ret = epf->driver->ops->bind(epf);
drivers/pci/endpoint/pci-epf-core.c-130- if (ret)
--
drivers/pci/tsm.c=342=int pci_tsm_bind(struct pci_dev *pdev, struct kvm *kvm, u32 tdi_id)
--
drivers/pci/tsm.c-367-
drivers/pci/tsm.c:368: tdi = to_pci_tsm_ops(pdev->tsm)->bind(pdev, kvm, tdi_id);
drivers/pci/tsm.c-369- if (IS_ERR(tdi))
--
drivers/usb/atm/usbatm.c=1008=int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
--
drivers/usb/atm/usbatm.c-1057- bind:
drivers/usb/atm/usbatm.c:1058: if (driver->bind && (error = driver->bind(instance, intf, id)) < 0) {
drivers/usb/atm/usbatm.c-1059- dev_err(dev, "%s: bind failed: %d!\n", __func__, error);
--
drivers/usb/gadget/composite.c=310=int usb_add_function(struct usb_configuration *config,
--
drivers/usb/gadget/composite.c-332- if (function->bind) {
drivers/usb/gadget/composite.c:333: value = function->bind(config, function);
drivers/usb/gadget/composite.c-334- if (value < 0) {
--
drivers/usb/gadget/composite.c=2527=static int composite_bind(struct usb_gadget *gadget,
--
drivers/usb/gadget/composite.c-2551- */
drivers/usb/gadget/composite.c:2552: status = composite->bind(cdev);
drivers/usb/gadget/composite.c-2553- if (status < 0)
--
drivers/usb/gadget/udc/core.c=1608=static int gadget_bind_driver(struct device *dev)
--
drivers/usb/gadget/udc/core.c-1628-
drivers/usb/gadget/udc/core.c:1629: ret = driver->bind(udc->gadget, driver);
drivers/usb/gadget/udc/core.c-1630- if (ret)
--
fs/dlm/lowcomms.c=1568=static int dlm_connect(struct connection *con)
--
fs/dlm/lowcomms.c-1591-
fs/dlm/lowcomms.c:1592: result = dlm_proto_ops->bind(sock);
fs/dlm/lowcomms.c-1593- if (result < 0) {
--
fs/ocfs2/cluster/tcp.c=1549=static void o2net_start_connect(struct work_struct *work)
--
fs/ocfs2/cluster/tcp.c-1617-
fs/ocfs2/cluster/tcp.c:1618: ret = sock->ops->bind(sock, (struct sockaddr_unsized *)&myaddr,
fs/ocfs2/cluster/tcp.c-1619- sizeof(myaddr));
--
fs/ocfs2/cluster/tcp.c=1978=static int o2net_open_listening_sock(__be32 addr, __be16 port)
--
fs/ocfs2/cluster/tcp.c-2004- sock->sk->sk_reuse = SK_CAN_REUSE;
fs/ocfs2/cluster/tcp.c:2005: ret = sock->ops->bind(sock, (struct sockaddr_unsized *)&sin, sizeof(sin));
fs/ocfs2/cluster/tcp.c-2006- if (ret < 0) {
--
kernel/cgroup/cgroup.c=1854=int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
--
kernel/cgroup/cgroup.c-1953- if (ss->bind)
kernel/cgroup/cgroup.c:1954: ss->bind(css);
kernel/cgroup/cgroup.c-1955- } while_each_subsys_mask();
--
kernel/cgroup/cgroup.c=6348=int __init cgroup_init(void)
--
kernel/cgroup/cgroup.c-6425- if (ss->bind)
kernel/cgroup/cgroup.c:6426: ss->bind(init_css_set.subsys[ssid]);
kernel/cgroup/cgroup.c-6427-
--
net/ieee802154/socket.c=99=static int ieee802154_sock_bind(struct socket *sock, struct sockaddr_unsized *uaddr,
--
net/ieee802154/socket.c-104- if (sk->sk_prot->bind)
net/ieee802154/socket.c:105: return sk->sk_prot->bind(sk, uaddr, addr_len);
net/ieee802154/socket.c-106-
--
net/ipv4/af_inet.c=444=int inet_bind_sk(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len)
--
net/ipv4/af_inet.c-450- if (sk->sk_prot->bind) {
net/ipv4/af_inet.c:451: return sk->sk_prot->bind(sk, uaddr, addr_len);
net/ipv4/af_inet.c-452- }
--
net/ipv6/af_inet6.c=441=int inet6_bind_sk(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len)
--
net/ipv6/af_inet6.c-450- if (prot->bind)
net/ipv6/af_inet6.c:451: return prot->bind(sk, uaddr, addr_len);
net/ipv6/af_inet6.c-452-
--
net/netlink/genetlink.c=1812=static int genl_bind(struct net *net, int group)
--
net/netlink/genetlink.c-1842- if (family->bind)
net/netlink/genetlink.c:1843: family->bind(i);
net/netlink/genetlink.c-1844-
--
net/phonet/socket.c=156=static int pn_socket_bind(struct socket *sock, struct sockaddr_unsized *addr, int len)
--
net/phonet/socket.c-165- if (sk->sk_prot->bind)
net/phonet/socket.c:166: return sk->sk_prot->bind(sk, addr, len);
net/phonet/socket.c-167-
--
net/socket.c=1866=int __sys_bind_socket(struct socket *sock, struct sockaddr_storage *address,
--
net/socket.c-1873- if (!err)
net/socket.c:1874: err = READ_ONCE(sock->ops)->bind(sock,
net/socket.c-1875- (struct sockaddr_unsized *)address,
--
net/socket.c=3548=int kernel_bind(struct socket *sock, struct sockaddr_unsized *addr, int addrlen)
--
net/socket.c-3553-
net/socket.c:3554: return READ_ONCE(sock->ops)->bind(sock, (struct sockaddr_unsized *)&address,
net/socket.c-3555- addrlen);
--
tools/testing/selftests/bpf/prog_tests/sock_addr.c=2380=static int test_xmsg(struct sock_addr_test *test)
--
tools/testing/selftests/bpf/prog_tests/sock_addr.c-2406-
tools/testing/selftests/bpf/prog_tests/sock_addr.c:2407: err = test->ops->bind(client, (struct sockaddr *)&src_addr,
tools/testing/selftests/bpf/prog_tests/sock_addr.c-2408- src_addr_len);
]
|
| 65/2 |
2026/02/06 12:26 |
llm |
expert |
0m
Model:
Tokens:
input: 66864
output: 48
thoughts: 74
Thoughts: Here's my analysis of what's going on:
**Deep Dive into USB Gadget Driver Binding**
Okay, so I'm looking at the `drivers/usb/gadget/udc/core.c` file and specifically line 1629. That `ret = driver->bind(udc->gadget, driver);` call in `gadget_bind_driver` is what I'm interested in right now. I know this is where the core binding operation happens, so it's the right place to start. Now let's jump over and examine the `gadget_bind_driver` function in `core.c` itself. I need to understand the control flow.
|
| 66/2 |
2026/02/06 12:26 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/usb/gadget/udc/core.c IncludeLines:true Name:gadget_bind_driver]
Results: map[SourceCode:1608: static int gadget_bind_driver(struct device *dev)
1609: {
1610: struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1611: struct usb_udc *udc = gadget->udc;
1612: struct usb_gadget_driver *driver = container_of(dev->driver,
1613: struct usb_gadget_driver, driver);
1614: int ret = 0;
1615:
1616: mutex_lock(&udc_lock);
1617: if (driver->is_bound) {
1618: mutex_unlock(&udc_lock);
1619: return -ENXIO; /* Driver binds to only one gadget */
1620: }
1621: driver->is_bound = true;
1622: udc->driver = driver;
1623: mutex_unlock(&udc_lock);
1624:
1625: dev_dbg(&udc->dev, "binding gadget driver [%s]\n", driver->function);
1626:
1627: usb_gadget_udc_set_speed(udc, driver->max_speed);
1628:
1629: ret = driver->bind(udc->gadget, driver);
1630: if (ret)
1631: goto err_bind;
1632:
1633: mutex_lock(&udc->connect_lock);
1634: ret = usb_gadget_udc_start_locked(udc);
1635: if (ret) {
1636: mutex_unlock(&udc->connect_lock);
1637: goto err_start;
1638: }
1639: usb_gadget_enable_async_callbacks(udc);
1640: udc->allow_connect = true;
1641: ret = usb_udc_connect_control_locked(udc);
1642: if (ret)
1643: goto err_connect_control;
1644:
1645: mutex_unlock(&udc->connect_lock);
1646:
1647: kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
1648: return 0;
1649:
1650: err_connect_control:
1651: udc->allow_connect = false;
1652: usb_gadget_disable_async_callbacks(udc);
1653: if (gadget->irq)
1654: synchronize_irq(gadget->irq);
1655: usb_gadget_udc_stop_locked(udc);
1656: mutex_unlock(&udc->connect_lock);
1657:
1658: err_start:
1659: driver->unbind(udc->gadget);
1660:
1661: err_bind:
1662: if (ret != -EISNAM)
1663: dev_err(&udc->dev, "failed to start %s: %d\n",
1664: driver->function, ret);
1665:
1666: mutex_lock(&udc_lock);
1667: udc->driver = NULL;
1668: driver->is_bound = false;
1669: mutex_unlock(&udc_lock);
1670:
1671: return ret;
1672: }
SourceFile:drivers/usb/gadget/udc/core.c]
|
| 67/2 |
2026/02/06 12:26 |
llm |
expert |
0m
Model:
Tokens:
input: 68142
output: 37
thoughts: 3708
Thoughts: **UAF in USB Gadget Raw Driver: Detailed Analysis**
Okay, here's the breakdown of my thought process in diagnosing this USB gadget driver issue. It looks like we have a classic Use-After-Free (UAF) vulnerability. The root cause seems to be a race condition between the unregistration of the raw gadget driver and the execution of callbacks within the dummy HCD (Host Controller Driver).
First, I traced the execution flow. If `gadget_bind_driver` fails *after* a partial initialization, the `driver->unbind` call is made. The `raw_gadget`'s `gadget_unbind` is setting `dev->gadget = NULL`. So far so good. I can see that `dev->gadget` is initialized to `gadget` in `gadget_bind`.
Then `gadget_bind_driver` either returns an error from `usb_gadget_udc_start_locked` or `usb_udc_connect_control_locked`. In this failure case, the driver `unbind` call is made. The process enters an error flow, but critically, `dev->gadget_registered` is *not* set. This is important.
If `gadget_unbind` is called, `dev->gadget` is set to NULL. So, when the file descriptor associated with the device is released in `raw_release`, it skips the unregistration part of the flow. Meaning that `raw_release` calls `kref_put` and *frees* `dev`.
The race arises because `dummy_hcd.c`, specifically `set_link_state` which can trigger the UAF, may still have a pointer to the now-freed `dev`! This `set_link_state` path can call the `disconnect` callback on the driver, but the driver object itself is now invalid. If the driver is unregistered after `set_link_state` and before `raw_release` the `kref_put` function will result in a UAF.
So, `set_link_state` can call `dum->driver->disconnect` after the driver has been unbound. And `raw_release` will free it.
But `dummy_hcd` sets `dum->driver = driver` in `dummy_udc_start`. Then it is set to `NULL` in `dummy_udc_stop`.
However, the critical piece is that *if* `usb_gadget_udc_start_locked` fails, or if `usb_udc_connect_control_locked` fails, then `usb_gadget_udc_stop_locked` *is* called, but the callback-waiting synchronizations in `dummy_hcd.c` are not correct in the error path! Specifically, `dummy_udc_stop` sets `dum->ints_enabled = 0`, but doesn't wait for the `callback_usage` variable in all error paths. This is the race window. `dummy_pullup(0)` does the waiting correctly.
The UAF happens within `raw_event_queue_add`, which is called through the `raw_queue_event` function which is called through the `gadget_disconnect` function.
This is the exact sequence:
1. `raw_ioctl_run` calls `usb_gadget_register_driver`.
2. `gadget_bind_driver` calls `driver->bind` -> `gadget_bind`. `dev->gadget` is set.
3. `gadget_bind_driver` calls `udc_start` -> `dummy_udc_start`. `dum->driver` is set.
4. `hub_event` triggers `set_link_state`.
5. `set_link_state` takes `dum->lock`, sees `ints_enabled` (not yet 0), increments `callback_usage`, drops `dum->lock`.
6. `gadget_bind_driver` continues, but fails at `usb_udc_connect_control_locked`.
7. `gadget_bind_driver` calls `usb_gadget_disable_async_callbacks` -> `ints_enabled = 0`.
8. `gadget_bind_driver` calls `usb_gadget_udc_stop_locked` -> `dummy_udc_stop`. `dum->driver = NULL`.
9. `gadget_bind_driver` calls `driver->unbind` -> `gadget_unbind`. `dev->gadget = NULL`.
10. `gadget_bind_driver` returns error.
11. `raw_ioctl_run` returns error.
|
| 68/2 |
2026/02/06 12:26 |
tool |
codesearch-struct-layout |
0m
Args: map[ContextFile:kernel/locking/rtmutex.c Name:rt_mutex_base]
Results: map[Fields:[map[Name:wait_lock OffsetBits:0 SizeBits:512] map[Name:waiters OffsetBits:512 SizeBits:128] map[Name:owner OffsetBits:640 SizeBits:64]]]
|
| 69/2 |
2026/02/06 12:26 |
llm |
expert |
0m
Model:
Tokens:
input: 72986
output: 37
thoughts: 14
Thoughts: Here's my thought process, summarized as you requested:
**A Real-Time Lock Deep Dive**
Okay, so I'm looking at `spinlock_rt`, and the gears in my head are turning. Wait a second... I see it using `rtlock_base`. That connection is significant. I need to refresh my understanding of how `rtlock_base` is implemented, what its guarantees are, and how that influences `spinlock_rt`'s real-time characteristics. It's not just a simple spinlock; there's a real foundation of RT-aware mechanisms at play. I'll need to double check the intended performance characteristics; are we getting the desired low-latency guarantees, or is there potential for unbounded wait times? Are there any priority inversion issues that need to be carefully considered? I need to analyze how the locking and unlocking primitives work in conjunction with the real-time scheduler. Let me remember what the relevant RT kernel documentation says. This is something to investigate more deeply.
|
| 70/2 |
2026/02/06 12:26 |
tool |
codesearch-struct-layout |
0m
Error:
Args: map[ContextFile:include/linux/spinlock_rt.h Name:rtlock_base]
Results:
|
| 71/2 |
2026/02/06 12:26 |
llm |
expert |
0m
Model:
Tokens:
input: 73268
output: 19
thoughts: 184
Thoughts: Okay, here's my take on those thoughts, framed as my own:
**Actionable Insights on a USB Gadget Driver Issue**
Right, so that 24-byte offset... it's definitely within the spinlock. That's a good starting point. This report is looking quite solid; the information provided is robust.
Let's do a quick final stack check. The crucial calls are here: `gadget_disconnect` (at drivers/usb/gadget/legacy/raw_gadget.c:388) for access, `dev_new` (at drivers/usb/gadget/legacy/raw_gadget.c:193) for allocation, and `raw_release` (at drivers/usb/gadget/legacy/raw_gadget.c:474) for the freeing. Importantly, they're all within the same file and dealing with the same underlying object. That streamlines the investigation significantly.
The crashing task being a kworker is very typical for something like `hub_event`, which makes perfect sense. The allocator/free-er being a user process (syzkaller, in this case) also fits the pattern for the `open` and `release` operations.
Overall, this report looks very actionable. We have a clear path to follow here. Time to dig in!
|
| 72/2 |
2026/02/06 12:27 |
tool |
set-results |
0m
Args: map[Actionable:true Confident:true]
Results: map[Actionable:true Confident:true]
|
| 73/2 |
2026/02/06 12:27 |
llm |
expert |
0m
Model:
Tokens:
input: 73775
output: 601
thoughts: 236
Thoughts: **Analyzing the Bug Report: A Clear UAF in the Raw Gadget Driver**
Okay, so I've just finished going over this bug report, and I'm pretty confident in my assessment. This one's actionable, and the reporter clearly laid out all the necessary information. It's a textbook Use-After-Free (UAF) situation, and thankfully, they provided all three critical stack traces: the bad access, the allocation, and the free. That really streamlined the analysis.
The common thread is the `struct raw_dev` object within the `raw_gadget` driver. All three stacks tie back to this same object type, which makes debugging much simpler. From what I can tell, the UAF is happening in `gadget_disconnect`. This is a callback from the USB Gadget core/UDC driver. So, the bug is happening when the USB gadget core is trying to disconnect a USB device.
The allocation and deallocation of the `raw_dev` object happen in `raw_open` and `raw_release` respectively. From what I'm seeing, there's a race condition. It seems `raw_release` is unregistering and freeing the `raw_dev` object, and concurrently the `dummy_hcd` driver is calling `gadget_disconnect`. Because `gadget_disconnect` is called *after* the object has been freed in this race condition, it results in a UAF.
The report also mentions the PREEMPT_RT kernel, which is useful information and explains the calls to `rtlock_lock` via `spin_lock`.
And finally, the offsets and addresses in the report are consistent with my understanding of the `raw_dev` structure, which solidifies the analysis. Overall, this is a well-documented bug report and the root cause appears clear.
|