usb: gadget: f_fs: fix NULL pointer dereference in FUNCTIONFS_ENDPOINT_DESC
Subsystems (2)
cover usb: gadget: f_fs: fix NULL pointer dereference in FUNCTIONFS_ENDPOINT_DESC
From: Yuchao Zhang <[email protected]>
Hi,
This patch addresses a kernel NULL pointer dereference panic in FunctionFS when calling the FUNCTIONFS_ENDPOINT_DESC ioctl.
In FunctionFS, user space can legitimately supply only a subset of speed descriptors (e.g., only full-speed descriptors via FUNCTIONFS_HAS_FS_DESC). When the USB gadget is subsequently connected to a high-speed or super-speed host:
1. composite.c:config_ep_by_speed() detects that the function lacks descriptors for the current speed and gracefully falls back to full-speed descriptors (f->fs_descriptors).
2. The endpoint is enabled via usb_ep_enable(), and epfile->ep becomes valid.
3. However, when user space calls ioctl(fd, FUNCTIONFS_ENDPOINT_DESC), ffs_epfile_ioctl() selects the descriptor index solely based on epfile->ffs->gadget->speed (desc_idx = 1 for HIGH, 2 for SUPER/SUPER_PLUS).
4. Because user space only supplied full-speed descriptors, epfile->ep->descs[desc_idx] is NULL.
5. The subsequent memcpy(&desc1, desc, desc->bLength) immediately dereferences the NULL pointer, causing an instant kernel crash:
[ 2.154836] Call trace: [ 2.154836] ffs_epfile_ioctl+0xe14/0x2c40 [usb_f_fs] (P) [ 2.155003] __arm64_sys_ioctl+0x128/0x1a4 [ 2.155298] invoke_syscall.constprop.0+0x64/0x260 [ 2.155374] el0_svc_common.constprop.0+0xa0/0x240 [ 2.155441] do_el0_svc+0x3c/0x60 [ 2.155494] el0_svc+0x38/0x98 [ 2.155547] el0t_64_sync_handler+0xa0/0xe4 [ 2.155606] el0t_64_sync+0x198/0x19c [ 2.156463] Kernel panic - not syncing: Oops: Fatal exception
The fix restores the speed fallback loop mirroring config_ep_by_speed() and historical f_fs behavior, defensively checks epfile->ep->ep->desc as a fallback, and returns -EINVAL safely if no valid descriptor is found.
Testing with QEMU ARM64 using dummy_hcd confirms that after applying this patch, the ioctl succeeds and returns the fallback descriptor cleanly without any crash.
zjamg (1):
usb: gadget: f_fs: Fix NULL pointer dereference in
FUNCTIONFS_ENDPOINT_DESC
drivers/usb/gadget/function/f_fs.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
Signature · 1 line
2.53.0
PATCH RESEND 1/1 usb: gadget: f_fs: Fix NULL pointer dereference in FUNCTIONFS_ENDPOINT_DESC
From: Yuchao Zhang <[email protected]>
When user space sets up FunctionFS with only full-speed descriptors (e.g., flags = FUNCTIONFS_HAS_FS_DESC) and the gadget operates at a higher speed (such as USB_SPEED_HIGH or USB_SPEED_SUPER), config_ep_by_speed() detects that the function lacks descriptors for the current speed, logs a warning, and falls back to full-speed descriptors. The endpoint is then successfully enabled via usb_ep_enable(), and epfile->ep becomes valid.
However, when user space subsequently issues the FUNCTIONFS_ENDPOINT_DESC ioctl to query the endpoint descriptor, ffs_epfile_ioctl() selects the descriptor index solely based on gadget->speed:
switch (epfile->ffs->gadget->speed) {
case USB_SPEED_SUPER:
case USB_SPEED_SUPER_PLUS:
desc_idx = 2;
break;
case USB_SPEED_HIGH:
desc_idx = 1;
break;
default:
desc_idx = 0;
}
desc = epfile->ep->descs[desc_idx];
memcpy(&desc1, desc, desc->bLength);Because user space only supplied full-speed descriptors, epfile->ep->descs[1] is NULL. Dereferencing desc->bLength triggers an immediate kernel NULL pointer dereference panic.
Fix this by falling back through lower speed descriptors if the current speed descriptor was not supplied, matching the fallback logic in config_ep_by_speed() and historical f_fs behavior. If still missing, fall back to epfile->ep->ep->desc (the active descriptor), and return -EINVAL safely if no valid descriptor is found. In addition, mark desc as const to match struct usb_ep::desc and avoid discarding qualifiers.
--- drivers/usb/gadget/function/f_fs.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/f_fs.c +13 -2
@@ -1877,8 +1877,9 @@ static long ffs_epfile_ioctl(struct file *file, unsigned code,break ;case FUNCTIONFS_ENDPOINT_DESC : { +const struct usb_endpoint_descriptor *desc; +struct usb_endpoint_descriptor desc1;int desc_idx; -struct usb_endpoint_descriptor desc1, *desc;switch (epfile->ffs->gadget->speed) {case USB_SPEED_SUPER :
@@ -1892,7 +1893,17 @@ static long ffs_epfile_ioctl(struct file *file, unsigned code, desc_idx =0 ; } - desc = epfile->ep->descs[desc_idx]; +do { + desc = epfile->ep->descs[desc_idx]; + }while (!desc && --desc_idx >=0 ); + +if (!desc) + desc = epfile->ep->ep->desc; +if (!desc) { + ret = -EINVAL ; +break ; + } +memcpy (&desc1, desc, desc->bLength);spin_unlock_irq (&epfile->ffs->eps_lock);
Signature · 1 line
2.53.0
Re: [RESEND PATCH 1/1] usb: gadget: f_fs: Fix NULL pointer dereference in FUNCTIONFS_ENDPOINT_DESC
Quoted 5 lines · zjamg
From: Yuchao Zhang <[email protected]>
Fixes c559a3534109 ("usb: gadget: f_fs: add ioctl returning ep descriptor")Signed-off-by Yuchao Zhang <[email protected]>
Quoted 20 lines
--- drivers/usb/gadget/function/f_fs.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)drivers/usb/gadget/function/f_fs.c +2
-1@@ -1877,8 +1877,9 @@ static long ffs_epfile_ioctl(struct file *file, unsigned code,break ;case FUNCTIONFS_ENDPOINT_DESC : { +const struct usb_endpoint_descriptor *desc; +struct usb_endpoint_descriptor desc1;int desc_idx; -struct usb_endpoint_descriptor desc1, *desc;switch (epfile->ffs->gadget->speed) {case USB_SPEED_SUPER :
drivers/usb/gadget/function/f_fs.c static long ffs_epfile_ioctl(struct file *file, unsigned code, · line 1901
Quoted 10 lines
drivers/usb/gadget/function/f_fs.c +6
-1@@ -1892,7 +1893,17 @@ static long ffs_epfile_ioctl(struct file *file, unsigned code, desc_idx =0 ; } - desc = epfile->ep->descs[desc_idx]; +do { + desc = epfile->ep->descs[desc_idx]; + }while (!desc && --desc_idx >=0 ); + +if (!desc) + desc = epfile->ep->ep->desc;
IIUC, this will never be non-NULL if all the descriptors are NULL. Then again, it probably won’t hurt, so dosen’t matter one way or another.
Quoted 8 lines
drivers/usb/gadget/function/f_fs.c +5
-0+if (!desc) { + ret = -EINVAL ; +break ; + } +memcpy (&desc1, desc, desc->bLength);spin_unlock_irq (&epfile->ffs->eps_lock);
Signature · 3 lines
Best regards ミハウ “𝓶𝓲𝓷𝓪86” ナザレヴィツ «If at first you don’t succeed, give up skydiving»