usb: fix over-reported actual_length in usbip/usbfs
Subsystems (2)
cover usb: fix over-reported actual_length in usbip/usbfs
The usbip vhci receive error path gives a failed URB back with the peer-supplied actual_length, and usbfs copy_urb_data_to_user() trusts that length for non-isochronous URBs. A malicious usbip peer can make usbfs read past the transfer buffer.
Patch 1 clears actual_length on the vhci receive failure path. Patch 2 clamps the copied length to transfer_buffer_length in usbfs as a defense-in-depth measure for any HCD that violates the actual_length contract.
Cheng Yongkang (2):
usbip: clear actual_length after a failed receive
usb: usbfs: clamp the copied length to transfer_buffer_length
drivers/usb/core/devio.c | 7 +++++-- drivers/usb/usbip/vhci_rx.c | 1 + 2 files changed, 6 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/2 usbip: clear actual_length after a failed receive
vhci_recv_ret_submit() receives the transfer buffer for an IN URB. When usbip_recv_xbuff() fails, it sets urb->status to -EPROTO and gives the URB back, but leaves urb->actual_length at the value supplied by the usbip peer. A malicious peer can set actual_length larger than transfer_buffer_length; usbfs then copies that many bytes from the kmalloc(transfer_buffer_length) buffer to userspace:
BUG: KASAN: slab-out-of-bounds in _copy_to_user+0x79/0xb0 Read of size 88 at addr ... copy_urb_data_to_user+0x36f/0x480
Clear actual_length on the failure path so the failed URB never carries a peer-controlled length. The URB is completed with -EPROTO and userspace observes a zero-length, failed transfer.
--- drivers/usb/usbip/vhci_rx.c | 1 + 1 file changed, 1 insertion(+)
drivers/usb/usbip/vhci_rx.c +1 -0
@@ -79,6 +79,7 @@ static void vhci_recv_ret_submit(struct vhci_device *vdev,/* recv transfer buffer */ if (usbip_recv_xbuff (ud, urb) <0 ) { urb->status = -EPROTO ; + urb->actual_length =0 ; goto error; }
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 1/2] usbip: clear actual_length after a failed receive
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines
vhci_recv_ret_submit()receives the transfer buffer for an IN URB. Whenusbip_recv_xbuff()fails, it setsurb->statusto-EPROTOand gives the URB back, but leavesurb->actual_lengthat the value supplied by the usbip peer. A malicious peer can setactual_lengthlarger thantransfer_buffer_length; usbfs then copies that many bytes from thekmalloc(transfer_buffer_length)buffer to userspace:BUG: KASAN: slab-out-of-bounds in _copy_to_user+0x79/0xb0 Read of size 88 at addr ... copy_urb_data_to_user+0x36f/0x480
Quoted 8 lines
Clear
actual_lengthon the failure path so the failed URB never carries a peer-controlled length. The URB is completed with-EPROTOand userspace observes a zero-length, failed transfer.Fixes aabb5b833872 ("usbip: Fix error path of vhci_recv_ret_submit()")Assisted-by LLMSigned-off-by Cheng Yongkang <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Shuah Khan
maintainerRe: [PATCH 1/2] usbip: clear actual_length after a failed receive
In reply to Cheng YongkangQuoted 28 lines · Cheng Yongkang
vhci_recv_ret_submit()receives the transfer buffer for an IN URB. Whenusbip_recv_xbuff()fails, it setsurb->statusto-EPROTOand gives the URB back, but leavesurb->actual_lengthat the value supplied by the usbip peer. A malicious peer can setactual_lengthlarger thantransfer_buffer_length; usbfs then copies that many bytes from thekmalloc(transfer_buffer_length)buffer to userspace:BUG: KASAN: slab-out-of-bounds in _copy_to_user+0x79/0xb0 Read of size 88 at addr ... copy_urb_data_to_user+0x36f/0x480Clear
actual_lengthon the failure path so the failed URB never carries a peer-controlled length. The URB is completed with-EPROTOand userspace observes a zero-length, failed transfer.Fixes aabb5b833872 ("usbip: Fix error path of vhci_recv_ret_submit()")Assisted-by LLMSigned-off-by Cheng Yongkang <[email protected]>--- drivers/usb/usbip/vhci_rx.c | 1 + 1 file changed, 1 insertion(+)drivers/usb/usbip/vhci_rx.c +0
-0
drivers/usb/usbip/vhci_rx.c static void vhci_recv_ret_submit(struct vhci_device *vdev, · line 85
Quoted 7 lines
drivers/usb/usbip/vhci_rx.c +1
-0@@ -79,6 +79,7 @@ static void vhci_recv_ret_submit(struct vhci_device *vdev,/* recv transfer buffer */ if (usbip_recv_xbuff (ud, urb) <0 ) { urb->status = -EPROTO ; + urb->actual_length =0 ; goto error; }
What happens without this code change? The code path follows the error leg because of EPROTO? So how does setting actual_length to zero help?
thanks,
-- Shuah
Re: [PATCH 1/2] usbip: clear actual_length after a failed receive
Hi Shuah,
Without this change the URB is still completed to usbfs, with status -EPROTO but with the peer-supplied actual_length intact:
vhci_recv_ret_submit()
usbip_pack_pdu() -> usbip_pack_ret_submit():
urb->actual_length = rpdu->actual_length; (usbip_common.c:471)
usbip_recv_xbuff():
size = urb->actual_length; (usbip_common.c:849)
if (size > urb->transfer_buffer_length) (usbip_common.c:856)
goto error; /* rejects the packet, returns < 0 */
urb->status = -EPROTO; goto error;
error:
usb_hcd_giveback_urb(hcd, urb, urb->status); (vhci_rx.c:107)
-> urb->complete(urb) (hcd.c:1657)
-> usbfs async_completed() (devio.c:1911)usbfs copies data on reap whenever actual_length is non-zero, and does not look at the URB status:
processcompl():
if (as->userbuffer && urb->actual_length) (devio.c:2050)
copy_urb_data_to_user(as->userbuffer, urb);
copy_urb_data_to_user():
len = urb->actual_length; /* non-isochronous */ (devio.c:563)That is deliberate: a legitimate HCD can complete with a short read and a non-zero status, and userspace still gets the partial data. But here the length came from the peer and was already rejected as invalid, so it must not reach usbfs. -EPROTO alone does not stop the copy; zeroing actual_length makes the copy condition false and userspace observes a failed, zero-length transfer.
I'll fold this into the commit message for the next version.
Thanks,
Cheng Yongkang
Re: [PATCH 1/2] usbip: clear actual_length after a failed receive
Hi Shuah, Sung,
My patch 1/2 makes the same change as Sung's earlier submission:
https://lore.kernel.org/linux-usb/[email protected]/
Please drop mine; sorry for the duplicate. My explanation above of why -EPROTO alone does not stop the usbfs copy applies to Sung's patch as well.
That change does not cover the high-bit length case raised in the review of Sung's patch:
https://lore.kernel.org/linux-usb/[email protected]/
I have only checked this in the source: usbip_recv_xbuff() stores the length in an int, so a value with the top bit set takes the size <= 0 early return, and the URB is given back with that length.
I have sent the usbfs side separately. It bounds actual_length in usbfs regardless of what the HCD reports:
https://lore.kernel.org/linux-usb/[email protected]/
Thanks,
Cheng Yongkang
Shuah Khan
maintainerRe: [PATCH 1/2] usbip: clear actual_length after a failed receive
In reply to Cheng YongkangQuoted 17 lines · Cheng Yongkang
Hi Shuah,
Without this change the URB is still completed to usbfs, with status
-EPROTObut with the peer-suppliedactual_lengthintact:Quoted 1 line
vhci_recv_ret_submit()usbip_pack_pdu() -> usbip_pack_ret_submit(): urb->actual_length = rpdu->actual_length; (usbip_common.c:471) usbip_recv_xbuff(): size = urb->actual_length; (usbip_common.c:849) if (size > urb->transfer_buffer_length) (usbip_common.c:856) goto error; /* rejects the packet, returns < 0 */ urb->status = -EPROTO; goto error; error: usb_hcd_giveback_urb(hcd, urb, urb->status); (vhci_rx.c:107) -> urb->complete(urb) (hcd.c:1657) -> usbfs async_completed() (devio.c:1911)
Quoted 2 lines
usbfs copies data on reap whenever
actual_lengthis non-zero, and does not look at the URB status:
It sounds like fixing usbfs to check the URB status before copying sounds like the right fix.
Changing vhci to zero out a filed in an URB that it received, itsn't something we want to do. Why not fix this in usbfs and that would work for other invalid lengths?
thanks,
-- Shuah
Re: [PATCH 1/2] usbip: clear actual_length after a failed receive
Quoted 18 lines · Shuah Khan
Quoted 17 lines · Cheng Yongkang
Hi Shuah,
Without this change the URB is still completed to usbfs, with status
-EPROTObut with the peer-suppliedactual_lengthintact:Quoted 1 line
vhci_recv_ret_submit()usbip_pack_pdu() -> usbip_pack_ret_submit(): urb->actual_length = rpdu->actual_length; (usbip_common.c:471) usbip_recv_xbuff(): size = urb->actual_length; (usbip_common.c:849) if (size > urb->transfer_buffer_length) (usbip_common.c:856) goto error; /* rejects the packet, returns < 0 */ urb->status = -EPROTO; goto error; error: usb_hcd_giveback_urb(hcd, urb, urb->status); (vhci_rx.c:107) -> urb->complete(urb) (hcd.c:1657) -> usbfs async_completed() (devio.c:1911)
Quoted 5 lines
Quoted 2 lines
usbfs copies data on reap whenever
actual_lengthis non-zero, and does not look at the URB status:It sounds like fixing usbfs to check the URB status before copying sounds like the right fix.
No, that's not possible. A transfer may terminate with an error partway through, after some data has been sent/received. The URB status would then be a negative error code, but actual_length would still need to reflect the amount of data that usbfs has to copy back to userspace (for an IN transfer) or tell userspace was sent (for an OUT transfer).
In general, the USB core relies on HCDs to set urb->actual_length correctly even when there was an error.
Quoted 3 lines
Changing vhci to zero out a filed in an URB that it received, itsn't something we want to do. Why not fix this in usbfs and that would work for other invalid lengths?
HCDs are _supposed_ to set the actual_length field in URBs. It's not a mistake to do so.
Alan Stern
Re: [PATCH 1/2] usbip: clear actual_length after a failed receive
Quoted 19 lines · Alan Stern
Quoted 18 lines · Shuah Khan
Quoted 17 lines · Cheng Yongkang
Hi Shuah,
Without this change the URB is still completed to usbfs, with status
-EPROTObut with the peer-suppliedactual_lengthintact:Quoted 1 line
vhci_recv_ret_submit()usbip_pack_pdu() -> usbip_pack_ret_submit(): urb->actual_length = rpdu->actual_length; (usbip_common.c:471) usbip_recv_xbuff(): size = urb->actual_length; (usbip_common.c:849) if (size > urb->transfer_buffer_length) (usbip_common.c:856) goto error; /* rejects the packet, returns < 0 */ urb->status = -EPROTO; goto error; error: usb_hcd_giveback_urb(hcd, urb, urb->status); (vhci_rx.c:107) -> urb->complete(urb) (hcd.c:1657) -> usbfs async_completed() (devio.c:1911)
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
usbfs copies data on reap whenever
actual_lengthis non-zero, and does not look at the URB status:It sounds like fixing usbfs to check the URB status before copying sounds like the right fix.
Cheng, I missed that the you sent usbfs patch to bound the length. Thank you for both patches.
Quoted 10 lines
No, that's not possible. A transfer may terminate with an error partway through, after some data has been sent/received. The URB status would then be a negative error code, but
actual_lengthwould still need to reflect the amount of data that usbfs has to copy back to userspace (for an IN transfer) or tell userspace was sent (for an OUT transfer).In general, the USB core relies on HCDs to set
urb->actual_lengthcorrectly even when there was an error.
Quoted 6 lines
Quoted 3 lines
Changing vhci to zero out a filed in an URB that it received, itsn't something we want to do. Why not fix this in usbfs and that would work for other invalid lengths?
HCDs are _supposed_ to set the
actual_lengthfield in URBs. It's not a mistake to do so.
Alan, Thank you for weighing on this.
Greg, please pick this patch up.
thanks,
-- Shuah
PATCH 2/2 usb: usbfs: clamp the copied length to transfer_buffer_length
In reply to Cheng Yongkangcopy_urb_data_to_user() uses urb->actual_length as the number of bytes to copy to userspace for non-isochronous URBs. The usbfs contract is that a driver must not report more data than the transfer buffer holds, but usbfs cannot rely on every HCD or device implementation to honor it; a malicious usbip peer can already set actual_length beyond transfer_buffer_length and trigger an out-of-bounds read:
BUG: KASAN: slab-out-of-bounds in _copy_to_user+0x79/0xb0 Read of size 88 at addr ... copy_urb_data_to_user+0x36f/0x480
Clamp the non-isochronous length to transfer_buffer_length before the copy. Normal URBs are unaffected; the isochronous path already uses transfer_buffer_length.
--- drivers/usb/core/devio.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/usb/core/devio.c +5 -2
@@ -557,10 +557,13 @@ static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb) {unsigned i, len, size; -if (urb->number_of_packets >0 )/* Isochronous */ +if (urb->number_of_packets >0 ) {/* Isochronous */ len = urb->transfer_buffer_length; -else /* Non-Isoc */ + }else {/* Non-Isoc */ len = urb->actual_length; +if (len > urb->transfer_buffer_length) + len = urb->transfer_buffer_length; + }if (urb->num_sgs ==0 ) {if (copy_to_user (userbuffer, urb->transfer_buffer, len))
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 2/2] usb: usbfs: clamp the copied length to transfer_buffer_length
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] usb: usbfs: unclamped urb->actual_length reported in processcompl() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] usb: usbfs: unclamped urb->actual_length reported in processcompl() --
commit c0187fb6be71954b8fdc91f54fa38ce341728b34
Author: Cheng Yongkang <[email protected]>
usb: usbfs: clamp the copied length to transfer_buffer_length
This commit clamps the local copy length in copy_urb_data_to_user() to urb->transfer_buffer_length for non-isochronous URBs so that an HCD or usbip peer reporting urb->actual_length larger than the transfer buffer does not trigger an out-of-bounds read during copy_to_user().
Quoted 7 lines
drivers/usb/core/devio.c +0
-0@@ -557,10 +557,13 @@ static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb) {unsigned i, len, size;
drivers/usb/core/devio.c static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb) · line 566
Quoted 10 lines
drivers/usb/core/devio.c +5
-2-if (urb->number_of_packets >0 )/* Isochronous */ +if (urb->number_of_packets >0 ) {/* Isochronous */ len = urb->transfer_buffer_length; -else /* Non-Isoc */ + }else {/* Non-Isoc */ len = urb->actual_length; +if (len > urb->transfer_buffer_length) + len = urb->transfer_buffer_length; + }
[Severity: Medium]
Should urb->actual_length itself be clamped (for example on completion in async_completed()), rather than only clamping the local len variable in copy_urb_data_to_user()?
When a non-isochronous URB submitted via USBDEVFS_SUBMITURB completes with urb->actual_length > urb->transfer_buffer_length and is reaped via USBDEVFS_REAPURB or USBDEVFS_REAPURBNDELAY, copy_urb_data_to_user() copies at most urb->transfer_buffer_length bytes into the user buffer, but processcompl() and processcompl_compat() then report the unclamped urb->actual_length to userspace:
drivers/usb/core/devio.c:processcompl() {
if (as->userbuffer && urb->actual_length) {
if (copy_urb_data_to_user(as->userbuffer, urb))
goto err_out;
}
if (put_user(as->status, &userurb->status))
goto err_out;
if (put_user(urb->actual_length, &userurb->actual_length))
goto err_out;
}drivers/usb/core/devio.c:processcompl_compat() {
if (as->userbuffer && urb->actual_length) {
if (copy_urb_data_to_user(as->userbuffer, urb))
return -EFAULT;
}
if (put_user(as->status, &userurb->status))
return -EFAULT;
if (put_user(urb->actual_length, &userurb->actual_length))
return -EFAULT;
}This tells userspace that userurb->actual_length bytes were transferred when only urb->transfer_buffer_length bytes were copied into userurb->buffer, which can cause userspace callers to read uninitialized or out-of-bounds memory.
In addition, before the URB is reaped, async_completed() passes the unclamped urb->actual_length to snoop_urb_data():
drivers/usb/core/devio.c:async_completed() {
snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
as->status, COMPLETE, NULL, 0);
if (usb_urb_dir_in(urb))
snoop_urb_data(urb, urb->actual_length);
}When usbfs_snoop is enabled, can snoop_urb_data() still read past the end of urb->transfer_buffer?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Greg Kroah-Hartman
maintainerRe: [PATCH 2/2] usb: usbfs: clamp the copied length to transfer_buffer_length
In reply to Cheng YongkangQuoted 6 lines · Cheng Yongkang
copy_urb_data_to_user()usesurb->actual_lengthas the number of bytes to copy to userspace for non-isochronous URBs. The usbfs contract is that a driver must not report more data than the transfer buffer holds, but usbfs cannot rely on every HCD or device implementation to honor it; a malicious usbip peer can already setactual_lengthbeyondtransfer_buffer_lengthand trigger an out-of-bounds read:
No, every HCD must honor this.
thanks,
greg k-h