usb: gadget: f_uac1: fix ac_header_desc in descriptor lists
PATCH 1/1 usb: gadget: f_uac1: fix ac_header_desc in descriptor lists
From: Paritosh Potukuchi <[email protected]>
ac_header_desc is a pointer to a dynamically allocated uac1_ac_header_descriptor. The descriptor lists incorrectly store &ac_header_desc, which is the address of the pointer variable rather than the descriptor itself.
usb_copy_descriptors() interprets entries in these lists as USB descriptor headers. As a result, passing &ac_header_desc causes the pointer variable itself to be interpreted as a descriptor and can lead to out-of-bounds memory accesses.
Store ac_header_desc instead of &ac_header_desc in both f_audio_desc and f_audio_ss_desc.
--- drivers/usb/gadget/function/f_uac1.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/f_uac1.c +2 -2
@@ -260,7 +260,7 @@ static struct uac_iso_endpoint_descriptor as_iso_in_desc = {static struct usb_descriptor_header *f_audio_desc[] = { (struct usb_descriptor_header *)&ac_interface_desc, - (struct usb_descriptor_header *)&ac_header_desc, + (struct usb_descriptor_header *)ac_header_desc, (struct usb_descriptor_header *)&usb_out_it_desc, (struct usb_descriptor_header *)&io_out_ot_desc,
@@ -332,7 +332,7 @@ static struct usb_ss_ep_comp_descriptor ss_as_in_ep_desc_comp = {static struct usb_descriptor_header *f_audio_ss_desc[] = { (struct usb_descriptor_header *)&ac_interface_desc, - (struct usb_descriptor_header *)&ac_header_desc, + (struct usb_descriptor_header *)ac_header_desc, (struct usb_descriptor_header *)&usb_out_it_desc, (struct usb_descriptor_header *)&io_out_ot_desc,
Signature · 1 line
2.55.0
Quoted 2 lines · Paritosh Potukuchi
Store
ac_header_descinstead of&ac_header_descin bothf_audio_descandf_audio_ss_desc.
This doesn't build. f_audio_desc[] and f_audio_ss_desc[] are file-scope arrays, so their initializers must be constant expressions. &ac_header_desc is an address constant and is fine; ac_header_desc reads the value of a mutable pointer, which is not a constant expression, so both changed entries fail with "initializer element is not constant".
It also touches the array that isn't the problem. f_audio_desc[] (FS/HS) is rebuilt entry by entry in afunc_bind() after the descriptor is allocated, so its static initializer never reaches the core. The entry that does reach the core unchanged is in f_audio_ss_desc[] (SS/SSP), which is never rebuilt -- that is where &ac_header_desc is handed to usb_copy_descriptors() and the pointer variable itself gets read as a descriptor header.
So the fix belongs at run time rather than in the static initializer:
the f_audio_ss_desc[] AC-header slot needs to be set from ac_header_desc after build_ac_header_desc() succeeds and before usb_assign_descriptors(), the same way f_audio_desc[] is already built. (Putting ac_header_desc in the static initializer would capture NULL anyway, since it is only allocated in afunc_bind().)
Thanks,
Aldo