Skip to contentSkip to conversation

CDC_WDM: fix hangs and error reporting in multithreaded cases

PATCH New Oliver Neukum 1 participant 8 messages Sep 23, 2020 USB

lore
Share & tools
lore
#0 lore raw

cover CDC_WDM: fix hangs and error reporting in multithreaded cases

Testing has shown

1. race conditions in the multithreaded use case
2. a vulnerability against malicious hardware
3. a minor race against disconnect() in flush()

in the CDC-WDM driver. All have a common cause in the interactions of the flush() method with other parts of the driver. Hence they require a common rework of the flush() method.

Long and exhaustive discussion and testing of these changes, however, revealed that they in turn introduce problems with error reporting and long periods of uninterruptible sleep. Those require further changes to flush() and the implementation of fsync().

The issue dates back to the introduction of the driver so the fixes also should go into stable.

#1 lore raw

PATCH 1/7 CDC-WDM: fix hangs in flush() in multithreaded cases

In a multithreaded scenario an arbitrary number of threads can be in wdm_write() and in wdm_flush(). Hence whenever WDM_IN_USE is reset, all possible waiters need to be notified. This is true whether this is due to IO completing or to an error starting IO.

---
 drivers/usb/class/cdc-wdm.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/usb/class/cdc-wdm.c +7 -4
index e3db6fbeadef..adb3fc307083 100644
@@ -151,7 +151,7 @@ static void wdm_out_callback(struct urb *urb)
 	kfree(desc->outbuf);
 	desc->outbuf = NULL;
 	clear_bit(WDM_IN_USE, &desc->flags);
-	wake_up(&desc->wait);
+	wake_up_all(&desc->wait);
 }
 
 static void wdm_in_callback(struct urb *urb)
@@ -424,6 +424,7 @@ static ssize_t wdm_write
 	if (rv < 0) {
 		desc->outbuf = NULL;
 		clear_bit(WDM_IN_USE, &desc->flags);
+		wake_up_all(&desc->wait); /* for flush() */
 		dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
 		rv = usb_translate_errors(rv);
 		goto out_free_mem_pm;
@@ -586,6 +587,7 @@ static ssize_t wdm_read
 static int wdm_flush(struct file *file, fl_owner_t id)
 {
 	struct wdm_device *desc = file->private_data;
+	int rv;
 
 	wait_event(desc->wait,
 			/*
@@ -600,11 +602,12 @@ static int wdm_flush(struct file *file, fl_owner_t id)
 	/* cannot dereference desc->intf if WDM_DISCONNECTING */
 	if (test_bit(WDM_DISCONNECTING, &desc->flags))
 		return -ENODEV;
-	if (desc->werr < 0)
+	rv = desc->werr;
+	if (rv < 0)
 		dev_err(&desc->intf->dev, "Error in flush path: %d\n",
-			desc->werr);
+			rv);
 
-	return usb_translate_errors(desc->werr);
+	return usb_translate_errors(rv);
 }
 
 static __poll_t wdm_poll(struct file *file, struct poll_table_struct *wait)
Signature · 1 line
2.16.4
#2 lore raw

PATCH 2/7 CDC-WDM: introduce a timeout in flush()

In reply to Oliver Neukum

Malicious or defective hardware may cease communication while flush() is running. In last consequence we need a timeout, as hardware that remains silent must be ignored.

Making the wait for IO interruptible would not solve the issue. While it would avoid a hang, it would not allow any progress and we would end up with an unclosable fd.

The 30 seconds are coming out of thin air.

---
 drivers/usb/class/cdc-wdm.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/usb/class/cdc-wdm.c +9 -2
index adb3fc307083..b3a3f249a915 100644
@@ -58,6 +58,9 @@ MODULE_DEVICE_TABLE (usb, wdm_ids);
 
 #define WDM_MAX			16
 
+/* flush() is uninterruptible, but we cannot wait forever */
+#define WDM_FLUSH_TIMEOUT	(30 * HZ)
+
 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
 #define WDM_DEFAULT_BUFSIZE	256
 
@@ -589,7 +592,7 @@ static int wdm_flush(struct file *file, fl_owner_t id)
 	struct wdm_device *desc = file->private_data;
 	int rv;
 
-	wait_event(desc->wait,
+	rv = wait_event_timeout(desc->wait,
 			/*
 			 * needs both flags. We cannot do with one
 			 * because resetting it would cause a race
@@ -597,11 +600,15 @@ static int wdm_flush(struct file *file, fl_owner_t id)
 			 * a disconnect
 			 */
 			!test_bit(WDM_IN_USE, &desc->flags) ||
-			test_bit(WDM_DISCONNECTING, &desc->flags));
+			test_bit(WDM_DISCONNECTING, &desc->flags),
+			WDM_FLUSH_TIMEOUT);
 
 	/* cannot dereference desc->intf if WDM_DISCONNECTING */
 	if (test_bit(WDM_DISCONNECTING, &desc->flags))
 		return -ENODEV;
+	if (!rv)
+		return -EIO;
+
 	rv = desc->werr;
 	if (rv < 0)
 		dev_err(&desc->intf->dev, "Error in flush path: %d\n",
Signature · 1 line
2.16.4
#3 lore raw

PATCH 3/7 CDC-WDM: remove use of intf->dev after potential disconnect

In reply to Oliver Neukum

After a disconnect intf->dev is not a valid pointer any longer. As flush() uses it only for logging purposes logging is not worth it. Remove the dev_err()

---
 drivers/usb/class/cdc-wdm.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/usb/class/cdc-wdm.c +5 -4
index b3a3f249a915..1ca2c85a977d 100644
@@ -603,16 +603,17 @@ static int wdm_flush(struct file *file, fl_owner_t id)
 			test_bit(WDM_DISCONNECTING, &desc->flags),
 			WDM_FLUSH_TIMEOUT);
 
-	/* cannot dereference desc->intf if WDM_DISCONNECTING */
+	/*
+	 * to report the correct error.
+	 * This is best effort
+	 * We are inevitably racing with the hardware.
+	 */
 	if (test_bit(WDM_DISCONNECTING, &desc->flags))
 		return -ENODEV;
 	if (!rv)
 		return -EIO;
 
 	rv = desc->werr;
-	if (rv < 0)
-		dev_err(&desc->intf->dev, "Error in flush path: %d\n",
-			rv);
 
 	return usb_translate_errors(rv);
 }
Signature · 1 line
2.16.4
#4 lore raw

PATCH 4/7 CDC-WDM: fix race reporting errors in flush

In reply to Oliver Neukum

In case a race was lost and multiple fds used,
an error could be reported multiple times. To fix
this a spinlock must be taken.

---
 drivers/usb/class/cdc-wdm.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/usb/class/cdc-wdm.c +3 -0
index 1ca2c85a977d..5fb855404403 100644
@@ -613,7 +613,10 @@ static int wdm_flush(struct file *file, fl_owner_t id)
 	if (!rv)
 		return -EIO;
 
+	spin_lock_irq(&desc->iuspin);
 	rv = desc->werr;
+	desc->werr = 0;
+	spin_unlock_irq(&desc->iuspin);
 
 	return usb_translate_errors(rv);
 }
Signature · 1 line
2.16.4
#5 lore raw

PATCH 5/7 CDC-WDM: correct error reporting in write()

In reply to Oliver Neukum

In case our wait was interrupted by a disconnect, we should report that.

---
 drivers/usb/class/cdc-wdm.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/usb/class/cdc-wdm.c +3 -0
index 5fb855404403..242f83118208 100644
@@ -396,6 +396,9 @@ static ssize_t wdm_write
 	if (test_bit(WDM_RESETTING, &desc->flags))
 		r = -EIO;
 
+	if (test_bit(WDM_DISCONNECTING, &desc->flags))
+		r = -ENODEV;
+
 	if (r < 0) {
 		rv = r;
 		goto out_free_mem_pm;
Signature · 1 line
2.16.4
#6 lore raw

PATCH 6/7 CDC-WDM: implement fsync

In reply to Oliver Neukum

Some users want to be very sure that data has gone out to the device. This needs fsync.

---
 drivers/usb/class/cdc-wdm.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/usb/class/cdc-wdm.c +28 -0
index 242f83118208..6ea03c12380c 100644
@@ -590,6 +590,33 @@ static ssize_t wdm_read
 	return rv;
 }
 
+/*
+ * The difference to flush is that we wait forever. If you don't like
+ * that behavior, you need to send a signal.
+ */
+
+static int wdm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
+{
+	struct wdm_device *desc = file->private_data;
+	int rv;
+
+	rv = wait_event_interruptible(desc->wait,
+			!test_bit(WDM_IN_USE, &desc->flags) ||
+			test_bit(WDM_DISCONNECTING, &desc->flags));
+
+	if (test_bit(WDM_DISCONNECTING, &desc->flags))
+		return -ENODEV;
+	if (rv < 0)
+		return -EINTR;
+
+	spin_lock_irq(&desc->iuspin);
+	rv = desc->werr;
+	desc->werr = 0;
+	spin_unlock_irq(&desc->iuspin);
+
+	return usb_translate_errors(rv);
+}
+
 static int wdm_flush(struct file *file, fl_owner_t id)
 {
 	struct wdm_device *desc = file->private_data;
@@ -746,6 +773,7 @@ static const struct file_operations wdm_fops = {
 	.owner =	THIS_MODULE,
 	.read =		wdm_read,
 	.write =	wdm_write,
+	.fsync =	wdm_fsync,
 	.open =		wdm_open,
 	.flush =	wdm_flush,
 	.release =	wdm_release,
Signature · 1 line
2.16.4
#7 lore raw

PATCH 7/7 CDC-WDM: making flush() interruptible

In reply to Oliver Neukum

There is no need for flush() to be uninterruptible. close(2) is allowed to return -EINTR.

30 seconds is quite long a time to sleep in an uninterruptible state. Change it to an interruptible sleep.

---
 drivers/usb/class/cdc-wdm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/class/cdc-wdm.c +3 -1
index 6ea03c12380c..b9cca1cb5058 100644
@@ -622,7 +622,7 @@ static int wdm_flush(struct file *file, fl_owner_t id)
 	struct wdm_device *desc = file->private_data;
 	int rv;
 
-	rv = wait_event_timeout(desc->wait,
+	rv = wait_event_interruptible_timeout(desc->wait,
 			/*
 			 * needs both flags. We cannot do with one
 			 * because resetting it would cause a race
@@ -642,6 +642,8 @@ static int wdm_flush(struct file *file, fl_owner_t id)
 		return -ENODEV;
 	if (!rv)
 		return -EIO;
+	if (rv < 0)
+		return -EINTR;
 
 	spin_lock_irq(&desc->iuspin);
 	rv = desc->werr;
Signature · 1 line
2.16.4