Skip to contentSkip to conversation

usb: class: cdc-wdm: fix reordering issue in read code path

PATCH In review Oliver Neukum 3 participants 5 messages Mar 4 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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 afba937e540c9 ("USB: CDC WDM driver")
---
 drivers/usb/class/cdc-wdm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/class/cdc-wdm.c +3 -1
index f2d94cfc70af..7556c0dac908 100644
@@ -225,7 +225,8 @@ static void wdm_in_callback(struct urb *urb)
 		/* we may already be in overflow */
 		if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
 			memmove(desc->ubuf + desc->length, desc->inbuf, length);
-			desc->length += length;
+			smp_wmb(); /* against wdm_read() */
+			WRITE_ONCE(desc->length, desc->length + length);
 		}
 	}
 skip_error:
@@ -533,6 +534,7 @@ static ssize_t wdm_read
 		return -ERESTARTSYS;
 
 	cntr = READ_ONCE(desc->length);
+	smp_rmb(); /* against wdm_in_callback() */
 	if (cntr == 0) {
 		desc->read = 0;
 retry:
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Oliver Neukum

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 afba937e540c9 ("USB: CDC WDM driver")

Looks good for the immediate fix.

As a long-term solution, refactoring this to use kfifo would be much better. The race condition I reported is just one example of how this ad-hoc lockless implementation can fail. Hand-rolling lockless algorithms is highly error-prone and likely hides other subtle bugs.

Quoted 30 lines
---
 drivers/usb/class/cdc-wdm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/class/cdc-wdm.c +3 -2
index f2d94cfc70af..7556c0dac908 100644
@@ -225,7 +225,8 @@ static void wdm_in_callback(struct urb *urb)
                /* we may already be in overflow */
                if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
                        memmove(desc->ubuf + desc->length, desc->inbuf, length);
-                       desc->length += length;
+                       smp_wmb(); /* against wdm_read() */
+                       WRITE_ONCE(desc->length, desc->length + length);
                }
        }
 skip_error:
@@ -533,6 +534,7 @@ static ssize_t wdm_read
                return -ERESTARTSYS;
 
        cntr = READ_ONCE(desc->length);
+       smp_rmb(); /* against wdm_in_callback() */
        if (cntr == 0) {
                desc->read = 0;
 retry:
--

2.53.0

#3 lore raw
In reply to Oliver Neukum
Quoted 21 lines · Oliver Neukum

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 afba937e540c9 ("USB: CDC WDM driver")
---
 drivers/usb/class/cdc-wdm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/class/cdc-wdm.c +0 -0
index f2d94cfc70af..7556c0dac908 100644

drivers/usb/class/cdc-wdm.c static void wdm_in_callback(struct urb *urb) · line 229

Quoted 6 lines
drivers/usb/class/cdc-wdm.c +2 -1
@@ -225,7 +225,8 @@ static void wdm_in_callback(struct urb *urb)
                /* we may already be in overflow */
                if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
                        memmove(desc->ubuf + desc->length, desc->inbuf, length);
-                       desc->length += length;
+                       smp_wmb(); /* against wdm_read() */
+                       WRITE_ONCE(desc->length, desc->length + length);

smp_store_release(&desc->length, desc->length + length);

drivers/usb/class/cdc-wdm.c static ssize_t wdm_read · line 537

Quoted 8 lines
drivers/usb/class/cdc-wdm.c +1 -0
                }
        }
 skip_error:
@@ -533,6 +534,7 @@ static ssize_t wdm_read
                return -ERESTARTSYS;
 
        cntr = READ_ONCE(desc->length);
+       smp_rmb(); /* against wdm_in_callback() */

cntr = smp_load_acquire(&desc->length);

I just realized one minor detail.

Instead of using smp_wmb() + WRITE_ONCE() and smp_rmb() + READ_ONCE(), we could use smp_store_release() and smp_load_acquire().

It perfectly matches the acquire/release semantics here and might bring slight performance benefits in some scenarios.

Feel free to send a v2 if you like the idea. But no pressure at all—I am totally fine with the current v1 as well, so it is entirely up to you.

Quoted 6 lines
drivers/usb/class/cdc-wdm.c +0 -1
        if (cntr == 0) {
                desc->read = 0;
 retry:
--

2.53.0

#4 lore raw

On 04.03.26 15:54, Gui-Dong Han wrote:

Hi,

thank you for the suggestion.

Quoted 5 lines

Instead of using smp_wmb() + WRITE_ONCE() and smp_rmb() + READ_ONCE(), we could use smp_store_release() and smp_load_acquire().

It perfectly matches the acquire/release semantics here and might bring slight performance benefits in some scenarios.

CDC-WDM is not really critical to performance. Hence I'd like as obvious and simple a fix as possible.

Quoted 3 lines

Feel free to send a v2 if you like the idea. But no pressure at all—I am totally fine with the current v1 as well, so it is entirely up to you.

The clean fix is to go to kfifo, isn't it? This is the best long term solution IMHO. First I'll review reordering issue with set_bit() and related operations.

In defense of the driver, if I may: It is older than the kfifo API.

Regards
Oliver

maintainer
#2 lore raw
In reply to Gui-Dong Han
Quoted 16 lines · Gui-Dong Han
Quoted 11 lines · Oliver Neukum

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 afba937e540c9 ("USB: CDC WDM driver")
Quoted 6 lines

Looks good for the immediate fix.

As a long-term solution, refactoring this to use kfifo would be much better. The race condition I reported is just one example of how this ad-hoc lockless implementation can fail. Hand-rolling lockless algorithms is highly error-prone and likely hides other subtle bugs.

Again, great, please submit patches to do this. It was done "recently" for the tty layer, so perhaps take a look at that work to see how it can be done, and the issues involved. I'd be interested if it changes the throughput and code size any (probably not on throughput given the slow speeds of USB, but code size might be more interesting...)

thanks,

greg k-h