Skip to content

Commit 8df672b

Browse files
oneukumgregkh
authored andcommitted
usb: class: cdc-wdm: fix reordering issue in read code path
Quoting the bug report: Due to compiler optimization or CPU out-of-order execution, the desc->length update can be reordered before the memmove. If this happens, wdm_read() can see the new length and call copy_to_user() on uninitialized memory. This also violates LKMM data race rules [1]. Fix it by using WRITE_ONCE and memory barriers. Fixes: afba937 ("USB: CDC WDM driver") Cc: stable <stable@kernel.org> Signed-off-by: Oliver Neukum <oneukum@suse.com> Closes: https://lore.kernel.org/linux-usb/CALbr=LbrUZn_cfp7CfR-7Z5wDTHF96qeuM=3fO2m-q4cDrnC4A@mail.gmail.com/ Reported-by: Gui-Dong Han <hanguidong02@gmail.com> Reviewed-by: Gui-Dong Han <hanguidong02@gmail.com> Link: https://patch.msgid.link/20260304130116.1721682-1-oneukum@suse.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 3cbc242 commit 8df672b

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

drivers/usb/class/cdc-wdm.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ static void wdm_in_callback(struct urb *urb)
225225
/* we may already be in overflow */
226226
if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
227227
memmove(desc->ubuf + desc->length, desc->inbuf, length);
228-
desc->length += length;
228+
smp_wmb(); /* against wdm_read() */
229+
WRITE_ONCE(desc->length, desc->length + length);
229230
}
230231
}
231232
skip_error:
@@ -533,6 +534,7 @@ static ssize_t wdm_read
533534
return -ERESTARTSYS;
534535

535536
cntr = READ_ONCE(desc->length);
537+
smp_rmb(); /* against wdm_in_callback() */
536538
if (cntr == 0) {
537539
desc->read = 0;
538540
retry:

0 commit comments

Comments
 (0)