Re: [PATCH v2] usb: usbfs: reject SETINTERFACE while suspend is allowed

From: Alan Stern

Date: Tue Nov 17 2026 - 15:37:34 EST


On Mon, Nov 16, 2026 at 06:38:58PM -0800, Cheng Yongkang wrote:
> After USBDEVFS_ALLOW_SUSPEND, a usbfs file no longer holds a runtime PM
> reference and the device can be runtime suspended. When it is resumed
> through reset-resume, finish_port_resume() sets USB_STATE_CONFIGURED
> before usb_reset_and_verify_device() restores the altsettings. The
> restore loop also runs when a driver bound to another interface calls
> usb_reset_device() from its resume handler. The resume path does not
> take the device lock, so a SETINTERFACE issued in that window passes
> checkintf() and its usb_set_interface() runs concurrently with the
> usb_set_interface() calls of the restore loop.
>
> Fail SETINTERFACE with -EHOSTUNREACH while suspend is allowed. A
> program has to call USBDEVFS_FORBID_SUSPEND or USBDEVFS_WAIT_FOR_RESUME
> first, which resumes the device and keeps it from suspending until the
> file allows suspend again.
>
> Fixes: 7794f486ed0b ("usbfs: Add ioctls for runtime power management")
> Suggested-by: Alan Stern <stern@xxxxxxxxxxxxxxxxxxx>
> Signed-off-by: Cheng Yongkang <teel4res@xxxxxxxxx>
> ---

Acked-by: Alan Stern <stern@xxxxxxxxxxxxxxxxxxx>

> Changes in v2:
> - Move the check before copy_from_user() and add a comment (Alan).
>
> v1: https://lore.kernel.org/linux-usb/20261116060549.370140-1-teel4res@xxxxxxxxx/
>
> drivers/usb/core/devio.c | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
> index 101cb9425480..8a032f4b547b 100644
> --- a/drivers/usb/core/devio.c
> +++ b/drivers/usb/core/devio.c
> @@ -1531,6 +1531,13 @@ static int proc_setintf(struct usb_dev_state *ps, void __user *arg)
> struct usbdevfs_setinterface setintf;
> int ret;
>
> + /*
> + * Avoid racing with a reset during device resume, which restores
> + * the altsettings without holding the device lock. The device can
> + * only be suspended while suspend is allowed.
> + */
> + if (ps->suspend_allowed)
> + return -EHOSTUNREACH;
> if (copy_from_user(&setintf, arg, sizeof(setintf)))
> return -EFAULT;
> ret = checkintf(ps, setintf.interface);
>
> base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
> --
> 2.34.1