Skip to content

Commit 1015c27

Browse files
AlanSterngregkh
authored andcommitted
USB: core: Limit the length of unkillable synchronous timeouts
The usb_control_msg(), usb_bulk_msg(), and usb_interrupt_msg() APIs in usbcore allow unlimited timeout durations. And since they use uninterruptible waits, this leaves open the possibility of hanging a task for an indefinitely long time, with no way to kill it short of unplugging the target device. To prevent this sort of problem, enforce a maximum limit on the length of these unkillable timeouts. The limit chosen here, somewhat arbitrarily, is 60 seconds. On many systems (although not all) this is short enough to avoid triggering the kernel's hung-task detector. In addition, clear up the ambiguity of negative timeout values by treating them the same as 0, i.e., using the maximum allowed timeout. Signed-off-by: Alan Stern <stern@rowland.harvard.edu> Link: https://lore.kernel.org/linux-usb/3acfe838-6334-4f6d-be7c-4bb01704b33d@rowland.harvard.edu/ Fixes: 1da177e ("Linux-2.6.12-rc2") CC: stable@vger.kernel.org Link: https://patch.msgid.link/15fc9773-a007-47b0-a703-df89a8cf83dd@rowland.harvard.edu Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 7784caa commit 1015c27

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

drivers/usb/core/message.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ static void usb_api_blocking_completion(struct urb *urb)
4545
* Starts urb and waits for completion or timeout.
4646
* Whether or not the wait is killable depends on the flag passed in.
4747
* For example, compare usb_bulk_msg() and usb_bulk_msg_killable().
48+
*
49+
* For non-killable waits, we enforce a maximum limit on the timeout value.
4850
*/
4951
static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length,
5052
bool killable)
@@ -61,7 +63,9 @@ static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length,
6163
if (unlikely(retval))
6264
goto out;
6365

64-
expire = timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT;
66+
if (!killable && (timeout <= 0 || timeout > USB_MAX_SYNCHRONOUS_TIMEOUT))
67+
timeout = USB_MAX_SYNCHRONOUS_TIMEOUT;
68+
expire = (timeout > 0) ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT;
6569
if (killable)
6670
rc = wait_for_completion_killable_timeout(&ctx.done, expire);
6771
else
@@ -127,8 +131,7 @@ static int usb_internal_control_msg(struct usb_device *usb_dev,
127131
* @index: USB message index value
128132
* @data: pointer to the data to send
129133
* @size: length in bytes of the data to send
130-
* @timeout: time in msecs to wait for the message to complete before timing
131-
* out (if 0 the wait is forever)
134+
* @timeout: time in msecs to wait for the message to complete before timing out
132135
*
133136
* Context: task context, might sleep.
134137
*
@@ -183,8 +186,7 @@ EXPORT_SYMBOL_GPL(usb_control_msg);
183186
* @index: USB message index value
184187
* @driver_data: pointer to the data to send
185188
* @size: length in bytes of the data to send
186-
* @timeout: time in msecs to wait for the message to complete before timing
187-
* out (if 0 the wait is forever)
189+
* @timeout: time in msecs to wait for the message to complete before timing out
188190
* @memflags: the flags for memory allocation for buffers
189191
*
190192
* Context: !in_interrupt ()
@@ -242,8 +244,7 @@ EXPORT_SYMBOL_GPL(usb_control_msg_send);
242244
* @index: USB message index value
243245
* @driver_data: pointer to the data to be filled in by the message
244246
* @size: length in bytes of the data to be received
245-
* @timeout: time in msecs to wait for the message to complete before timing
246-
* out (if 0 the wait is forever)
247+
* @timeout: time in msecs to wait for the message to complete before timing out
247248
* @memflags: the flags for memory allocation for buffers
248249
*
249250
* Context: !in_interrupt ()
@@ -314,8 +315,7 @@ EXPORT_SYMBOL_GPL(usb_control_msg_recv);
314315
* @len: length in bytes of the data to send
315316
* @actual_length: pointer to a location to put the actual length transferred
316317
* in bytes
317-
* @timeout: time in msecs to wait for the message to complete before
318-
* timing out (if 0 the wait is forever)
318+
* @timeout: time in msecs to wait for the message to complete before timing out
319319
*
320320
* Context: task context, might sleep.
321321
*
@@ -347,8 +347,7 @@ EXPORT_SYMBOL_GPL(usb_interrupt_msg);
347347
* @len: length in bytes of the data to send
348348
* @actual_length: pointer to a location to put the actual length transferred
349349
* in bytes
350-
* @timeout: time in msecs to wait for the message to complete before
351-
* timing out (if 0 the wait is forever)
350+
* @timeout: time in msecs to wait for the message to complete before timing out
352351
*
353352
* Context: task context, might sleep.
354353
*
@@ -408,12 +407,12 @@ EXPORT_SYMBOL_GPL(usb_bulk_msg);
408407
* @actual_length: pointer to a location to put the actual length transferred
409408
* in bytes
410409
* @timeout: time in msecs to wait for the message to complete before
411-
* timing out (if 0 the wait is forever)
410+
* timing out (if <= 0, the wait is as long as possible)
412411
*
413412
* Context: task context, might sleep.
414413
*
415-
* This function is just like usb_blk_msg() except that it waits in a
416-
* killable state.
414+
* This function is just like usb_blk_msg(), except that it waits in a
415+
* killable state and there is no limit on the timeout length.
417416
*
418417
* Return:
419418
* If successful, 0. Otherwise a negative error number. The number of actual

include/linux/usb.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,9 @@ void usb_free_noncoherent(struct usb_device *dev, size_t size,
18621862
* SYNCHRONOUS CALL SUPPORT *
18631863
*-------------------------------------------------------------------*/
18641864

1865+
/* Maximum value allowed for timeout in synchronous routines below */
1866+
#define USB_MAX_SYNCHRONOUS_TIMEOUT 60000 /* ms */
1867+
18651868
extern int usb_control_msg(struct usb_device *dev, unsigned int pipe,
18661869
__u8 request, __u8 requesttype, __u16 value, __u16 index,
18671870
void *data, __u16 size, int timeout);

0 commit comments

Comments
 (0)