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

From: Alan Stern

Date: Mon Nov 16 2026 - 13:17:40 EST


On Sun, Nov 15, 2026 at 10:05:49PM -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>
> ---
> I used -EHOSTUNREACH, the same error checkintf() returns for a device
> that is not configured. Please tell me if another error is preferred.

-EHOSTUNREACH is fine, as far as I know.

> drivers/usb/core/devio.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
> index 101cb9425480..6463ad2b1fc5 100644
> --- a/drivers/usb/core/devio.c
> +++ b/drivers/usb/core/devio.c
> @@ -1533,6 +1533,8 @@ static int proc_setintf(struct usb_dev_state *ps, void __user *arg)
>
> if (copy_from_user(&setintf, arg, sizeof(setintf)))
> return -EFAULT;
> + if (ps->suspend_allowed)
> + return -EHOSTUNREACH;

How about putting this before the copy_from_user(), and adding a comment
about avoiding possible races against an unlocked reset occurring
during a device resume?

Alan Stern

> ret = checkintf(ps, setintf.interface);
> if (ret)
> return ret;
>
> base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
> --
> 2.34.1
>