Skip to contentSkip to conversation

usblp: fix race between disconnect() and read()

PATCH In review Oliver Neukum 2 participants 4 messages Sep 17, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usblp: fix race between disconnect() and read()

read() needs to check whether the device has been disconnected before it tries to talk to the device.

---
 drivers/usb/class/usblp.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/usb/class/usblp.c +5 -0
index 084c48c5848f..67cbd42421be 100644
@@ -827,6 +827,11 @@ static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, lo
 	if (rv < 0)
 		return rv;
 
+	if (!usblp->present) {
+		count = -ENODEV;
+		goto done;
+	}
+
 	if ((avail = usblp->rstatus) < 0) {
 		printk(KERN_ERR "usblp%d: error %d reading from printer\n",
 		    usblp->minor, (int)avail);
Signature · 1 line
2.16.4
maintainer
#1 lore raw
Quoted 14 lines · Oliver Neukum

read() needs to check whether the device has been disconnected before it tries to talk to the device.

---
 drivers/usb/class/usblp.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/usb/class/usblp.c +0 -0
index 084c48c5848f..67cbd42421be 100644

drivers/usb/class/usblp.c static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, lo · line 834

Quoted 8 lines
drivers/usb/class/usblp.c +5 -0
@@ -827,6 +827,11 @@ static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, lo
 	if (rv < 0)
 		return rv;
 
+	if (!usblp->present) {
+		count = -ENODEV;
+		goto done;
+	}
+

What prevents ->present from not being changed right after this test?

thanks,

greg k-h

#2 lore raw

Am Donnerstag, den 17.09.2020, 13:43 +0200 schrieb Greg KH:

Quoted 18 lines · Oliver Neukum
Quoted 17 lines

read() needs to check whether the device has been disconnected before it tries to talk to the device.

---
 drivers/usb/class/usblp.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/usb/class/usblp.c +0 -0
index 084c48c5848f..67cbd42421be 100644
@@ -827,6 +827,11 @@ static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, lo
 	if (rv < 0)
 		return rv;
 
Quoted 7 lines

drivers/usb/class/usblp.c static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, lo · line 834

Quoted 5 lines
drivers/usb/class/usblp.c +5 -0
+	if (!usblp->present) {
+		count = -ENODEV;
+		goto done;
+	}
+

What prevents ->present from not being changed right after this test?

Hi,

the mutex taken in

rv = usblp_rwait_and_lock(usblp, !!(file->f_flags & O_NONBLOCK));

right above it. Yes, this driver is a mess. But short of adding a ton of comments or rewriting the whole thing I have no idea what to do about that.

Regards
Oliver

maintainer
#3 lore raw
Quoted 27 lines · Oliver Neukum

Am Donnerstag, den 17.09.2020, 13:43 +0200 schrieb Greg KH:

Quoted 18 lines · Oliver Neukum
Quoted 17 lines

read() needs to check whether the device has been disconnected before it tries to talk to the device.

---
 drivers/usb/class/usblp.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/usb/class/usblp.c +0 -0
index 084c48c5848f..67cbd42421be 100644
@@ -827,6 +827,11 @@ static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, lo
 	if (rv < 0)
 		return rv;
 
Quoted 7 lines

drivers/usb/class/usblp.c static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, lo · line 834

Quoted 5 lines
drivers/usb/class/usblp.c +5 -0
+	if (!usblp->present) {
+		count = -ENODEV;
+		goto done;
+	}
+

What prevents ->present from not being changed right after this test?

Quoted 5 lines

Hi,

the mutex taken in

rv = usblp_rwait_and_lock(usblp, !!(file->f_flags & O_NONBLOCK));

Ah, missed that, thanks.

greg k-h