syzbot
memory leak in build_fu_desc
Status:
upstream: reported C repro on 2026/12/03 09:16
Subsystems:
usb
Labels:
prio:normal
[Documentation on labels]
Reported-by: syzbot+da5ce3f4a00c8cbe0d4e@syzkaller.appspotmail.com
First crash: 3h04m, last: 3h04m
✨ AI-generated root cause summary:
In drivers/usb/gadget/function/f_uac2.c, in_feature_unit_desc and out_feature_unit_desc are stored as global static pointers rather than per-instance fields of struct f_uac2. When multiple uac2 function instances are bound (such as two uac2 functions in one USB gadget configuration or across multiple gadgets), subsequent calls to afunc_bind() allocate new descriptors via build_fu_desc() and overwrite these global pointers without freeing the previous allocations. As a result, the earlier instances' uac2_feature_unit_descriptor objects become unreferenced and are leaked when afunc_unbind() later frees only the most recently stored pointers. Additionally, sharing these unsynchronized global pointers across instances allows interleaved or concurrent bind/unbind operations across gadgets to trigger NULL-pointer dereferences, use-after-free, or double-free errors.
▶
▼
✨ Full AI root cause analysis
Root Cause Analysis
The memory leak is caused by storing dynamically allocated USB Audio Class 2.0 feature unit descriptors in global static pointers rather than per-instance state in drivers/usb/gadget/function/f_uac2.c.
Involved Definitions and State
Trigger Mechanism and Lifecycle Violation
- In the reproducer, two
uac2 function instances (uac2.0 and uac2.usb0) are created and linked into the same gadget configuration (configs/c.1), and the gadget is bound by writing a UDC name to UDC.
gadget_dev_desc_UDC_store() -> usb_gadget_register_driver_owner() -> gadget_bind_driver() -> configfs_composite_bind() (drivers/usb/gadget/configfs.c:1801-1808) iterates over cfg->func_list and calls usb_add_function(c, f) -> afunc_bind() for each uac2 function:
- First
afunc_bind() (uac2.0):
- Calls
build_fu_desc(uac2_opts->c_chmask) (drivers/usb/gadget/function/f_uac2.c:1062), allocating a 32-byte uac2_feature_unit_descriptor via kzalloc() (f_uac2.c:773) and storing it in the global out_feature_unit_desc.
- Calls
build_fu_desc(uac2_opts->p_chmask) (drivers/usb/gadget/function/f_uac2.c:1067), allocating a second 32-byte uac2_feature_unit_descriptor and storing it in the global in_feature_unit_desc.
afunc_bind() completes initialization (bUnitID, bSourceID, bmaControls, iFeature — all visible in the kmemleak hex dumps: 12 24 06 05 01 0f ... 0c and 12 24 06 06 02 0f ... 0b) and returns 0.
- Second
afunc_bind() (uac2.usb0):
- Calls
build_fu_desc() at lines 1062 and 1067 again and unconditionally overwrites the global out_feature_unit_desc and in_feature_unit_desc pointers with the new allocations.
- The descriptor pointers allocated during the first
afunc_bind() are overwritten without being freed, leaving them completely unreferenced.
- When the gadget is subsequently unbound (by writing
"\n" to UDC, or on err_purge_funcs if binding fails), afunc_unbind() (drivers/usb/gadget/function/f_uac2.c:2266-2279) frees out_feature_unit_desc and in_feature_unit_desc and sets both to NULL. Only the latest instance's descriptors are freed; the earlier instance's allocations remain leaked (BUG: memory leak).
Related Defects in the Same Mechanism
- Error path leak in
afunc_bind(): Lines 1206-1246 return ret directly if set_ep_max_packet_size_bint() fails, bypassing goto err_free_fu; (f_uac2.c:1349-1354) and leaking both descriptors.
- Cross-gadget shared global state: Because
gadget_dev_desc_UDC_store() only acquires the per-gadget gi->lock (drivers/usb/gadget/configfs.c:287), and USB control requests (afunc_setup() -> in_rq_cur(), in_rq_range(), out_rq_cur(), out_rq_cur_complete()) run asynchronously without holding gi->lock, binding/unbinding multiple f_uac2 instances across separate gadgets/UDCs can also cause afunc_unbind() / err_free_fu to free and NULL the global pointers while another instance is still binding or handling control requests (USB_IN_FU_ID / USB_OUT_FU_ID), or race on kfree() before setting the globals to NULL.
Sample crash report:
* Struck through repros no longer work on HEAD.