Linux-USB Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH v4 1/2] USB: cdc-acm: fix close_delay and closing_wait units in TIOCSSERIAL
@ 2020-03-12 13:31 Anthony Mallet
  2020-03-12 13:31 ` [PATCH v4 2/2] USB: cdc-acm: fix rounding error " Anthony Mallet
  0 siblings, 1 reply; 4+ messages in thread
From: Anthony Mallet @ 2020-03-12 13:31 UTC (permalink / raw)
  To: Oliver Neukum, linux-usb; +Cc: Anthony Mallet

close_delay and closing_wait are specified in hundredth of a second but stored
internally in jiffies. Use the jiffies_to_msecs() and msecs_to_jiffies()
functions to convert from each other.

Signed-off-by: Anthony Mallet <anthony.mallet@laas.fr>
---
Changed in v2: none
Changed in v3: version this patch series
Changes in v4: none (diffed against e6e6ec4 from github.com/torvalds/linux.git)
---
 drivers/usb/class/cdc-acm.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
index 62f4fb9b362f..da619176deca 100644
--- a/drivers/usb/class/cdc-acm.c
+++ b/drivers/usb/class/cdc-acm.c
@@ -896,10 +896,10 @@ static int get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
 
 	ss->xmit_fifo_size = acm->writesize;
 	ss->baud_base = le32_to_cpu(acm->line.dwDTERate);
-	ss->close_delay	= acm->port.close_delay / 10;
+	ss->close_delay	= jiffies_to_msecs(acm->port.close_delay) / 10;
 	ss->closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
 				ASYNC_CLOSING_WAIT_NONE :
-				acm->port.closing_wait / 10;
+				jiffies_to_msecs(acm->port.closing_wait) / 10;
 	return 0;
 }
 
@@ -909,9 +909,10 @@ static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss)
 	unsigned int closing_wait, close_delay;
 	int retval = 0;
 
-	close_delay = ss->close_delay * 10;
+	close_delay = msecs_to_jiffies(ss->close_delay * 10);
 	closing_wait = ss->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
-			ASYNC_CLOSING_WAIT_NONE : ss->closing_wait * 10;
+			ASYNC_CLOSING_WAIT_NONE :
+			msecs_to_jiffies(ss->closing_wait * 10);
 
 	mutex_lock(&acm->port.mutex);
 
-- 
2.17.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v4 2/2] USB: cdc-acm: fix rounding error in TIOCSSERIAL
  2020-03-12 13:31 [PATCH v4 1/2] USB: cdc-acm: fix close_delay and closing_wait units in TIOCSSERIAL Anthony Mallet
@ 2020-03-12 13:31 ` Anthony Mallet
  2020-03-27 12:37   ` Matthias Reichl
  0 siblings, 1 reply; 4+ messages in thread
From: Anthony Mallet @ 2020-03-12 13:31 UTC (permalink / raw)
  To: Oliver Neukum, linux-usb; +Cc: Anthony Mallet

By default, tty_port_init() initializes those parameters to a multiple
of HZ. For instance in line 69 of tty_port.c:
   port->close_delay = (50 * HZ) / 100;
https://github.com/torvalds/linux/blob/master/drivers/tty/tty_port.c#L69

With e.g. CONFIG_HZ = 250 (as this is the case for Ubuntu 18.04
linux-image-4.15.0-37-generic), the default setting for close_delay is
thus 125.

When ioctl(fd, TIOCGSERIAL, &s) is executed, the setting returned in
user space is '12' (125/10). When ioctl(fd, TIOCSSERIAL, &s) is then
executed with the same setting '12', the value is interpreted as '120'
which is different from the current setting and a EPERM error may be
raised by set_serial_info() if !CAP_SYS_ADMIN.
https://github.com/torvalds/linux/blob/master/drivers/usb/class/cdc-acm.c#L919

Fixes: ba2d8ce9db0a6 ("cdc-acm: implement TIOCSSERIAL to avoid blocking close(2)")
Signed-off-by: Anthony Mallet <anthony.mallet@laas.fr>
---
Changed in v2: fix typo (extra closing brace)
Changed in v3: version this patch series
Changed in v4: diffed against e6e6ec4 from github.com/torvalds/linux.git
---
 drivers/usb/class/cdc-acm.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
index da619176deca..a41a3d27016c 100644
--- a/drivers/usb/class/cdc-acm.c
+++ b/drivers/usb/class/cdc-acm.c
@@ -907,6 +907,7 @@ static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss)
 {
 	struct acm *acm = tty->driver_data;
 	unsigned int closing_wait, close_delay;
+	unsigned int old_closing_wait, old_close_delay;
 	int retval = 0;
 
 	close_delay = msecs_to_jiffies(ss->close_delay * 10);
@@ -914,18 +915,24 @@ static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss)
 			ASYNC_CLOSING_WAIT_NONE :
 			msecs_to_jiffies(ss->closing_wait * 10);
 
+	/* we must redo the rounding here, so that the values match */
+	old_close_delay	= jiffies_to_msecs(acm->port.close_delay) / 10;
+	old_closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
+				ASYNC_CLOSING_WAIT_NONE :
+				jiffies_to_msecs(acm->port.closing_wait) / 10;
+
 	mutex_lock(&acm->port.mutex);
 
-	if (!capable(CAP_SYS_ADMIN)) {
-		if ((close_delay != acm->port.close_delay) ||
-		    (closing_wait != acm->port.closing_wait))
+	if ((ss->close_delay != old_close_delay) ||
+            (ss->closing_wait != old_closing_wait)) {
+		if (!capable(CAP_SYS_ADMIN))
 			retval = -EPERM;
-		else
-			retval = -EOPNOTSUPP;
-	} else {
-		acm->port.close_delay  = close_delay;
-		acm->port.closing_wait = closing_wait;
-	}
+		else {
+			acm->port.close_delay  = close_delay;
+			acm->port.closing_wait = closing_wait;
+		}
+	} else
+		retval = -EOPNOTSUPP;
 
 	mutex_unlock(&acm->port.mutex);
 	return retval;
-- 
2.17.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4 2/2] USB: cdc-acm: fix rounding error in TIOCSSERIAL
  2020-03-12 13:31 ` [PATCH v4 2/2] USB: cdc-acm: fix rounding error " Anthony Mallet
@ 2020-03-27 12:37   ` Matthias Reichl
  2020-03-27 12:53     ` Greg Kroah-Hartman
  0 siblings, 1 reply; 4+ messages in thread
From: Matthias Reichl @ 2020-03-27 12:37 UTC (permalink / raw)
  To: Anthony Mallet, Greg Kroah-Hartman; +Cc: Oliver Neukum, linux-usb

On Thu, Mar 12, 2020 at 02:31:01PM +0100, Anthony Mallet wrote:
> By default, tty_port_init() initializes those parameters to a multiple
> of HZ. For instance in line 69 of tty_port.c:
>    port->close_delay = (50 * HZ) / 100;
> https://github.com/torvalds/linux/blob/master/drivers/tty/tty_port.c#L69
> 
> With e.g. CONFIG_HZ = 250 (as this is the case for Ubuntu 18.04
> linux-image-4.15.0-37-generic), the default setting for close_delay is
> thus 125.
> 
> When ioctl(fd, TIOCGSERIAL, &s) is executed, the setting returned in
> user space is '12' (125/10). When ioctl(fd, TIOCSSERIAL, &s) is then
> executed with the same setting '12', the value is interpreted as '120'
> which is different from the current setting and a EPERM error may be
> raised by set_serial_info() if !CAP_SYS_ADMIN.
> https://github.com/torvalds/linux/blob/master/drivers/usb/class/cdc-acm.c#L919
> 
> Fixes: ba2d8ce9db0a6 ("cdc-acm: implement TIOCSSERIAL to avoid blocking close(2)")
> Signed-off-by: Anthony Mallet <anthony.mallet@laas.fr>
> ---
> Changed in v2: fix typo (extra closing brace)
> Changed in v3: version this patch series
> Changed in v4: diffed against e6e6ec4 from github.com/torvalds/linux.git
> ---
>  drivers/usb/class/cdc-acm.c | 27 +++++++++++++++++----------
>  1 file changed, 17 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
> index da619176deca..a41a3d27016c 100644
> --- a/drivers/usb/class/cdc-acm.c
> +++ b/drivers/usb/class/cdc-acm.c
> @@ -907,6 +907,7 @@ static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss)
>  {
>  	struct acm *acm = tty->driver_data;
>  	unsigned int closing_wait, close_delay;
> +	unsigned int old_closing_wait, old_close_delay;
>  	int retval = 0;
>  
>  	close_delay = msecs_to_jiffies(ss->close_delay * 10);
> @@ -914,18 +915,24 @@ static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss)
>  			ASYNC_CLOSING_WAIT_NONE :
>  			msecs_to_jiffies(ss->closing_wait * 10);
>  
> +	/* we must redo the rounding here, so that the values match */
> +	old_close_delay	= jiffies_to_msecs(acm->port.close_delay) / 10;
> +	old_closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
> +				ASYNC_CLOSING_WAIT_NONE :
> +				jiffies_to_msecs(acm->port.closing_wait) / 10;
> +
>  	mutex_lock(&acm->port.mutex);
>  
> -	if (!capable(CAP_SYS_ADMIN)) {
> -		if ((close_delay != acm->port.close_delay) ||
> -		    (closing_wait != acm->port.closing_wait))
> +	if ((ss->close_delay != old_close_delay) ||
> +            (ss->closing_wait != old_closing_wait)) {
> +		if (!capable(CAP_SYS_ADMIN))
>  			retval = -EPERM;
> -		else
> -			retval = -EOPNOTSUPP;
> -	} else {
> -		acm->port.close_delay  = close_delay;
> -		acm->port.closing_wait = closing_wait;
> -	}
> +		else {
> +			acm->port.close_delay  = close_delay;
> +			acm->port.closing_wait = closing_wait;
> +		}
> +	} else
> +		retval = -EOPNOTSUPP;

Swapping the order of the capability / close settings checks introduced
a rather unexpected regression: when running with CAP_SYS_ADMIN we now
get -EOPNOTSUPP when the close settings are unchanged.

This will break programs / scripts that set these settings to fixed
values.

eg (tested on vanilla 5.4.28) changing the settings works, but
setting the same value again fails:

# setserial -a /dev/ttyACM0
/dev/ttyACM0, Line 0, UART: unknown, Port: 0x0000, IRQ: 0
	Baud_base: 9600, close_delay: 50, divisor: 0
	closing_wait: 3000
	Flags: spd_normal

# setserial /dev/ttyACM0 closing_wait 2000
# setserial /dev/ttyACM0 closing_wait 2000
Cannot set serial info: Operation not supported

From the commit message it's not clear why this behaviour was changed
so I suspect this might be a bug.

so long,

Hias

>  
>  	mutex_unlock(&acm->port.mutex);
>  	return retval;
> -- 
> 2.17.1
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4 2/2] USB: cdc-acm: fix rounding error in TIOCSSERIAL
  2020-03-27 12:37   ` Matthias Reichl
@ 2020-03-27 12:53     ` Greg Kroah-Hartman
  0 siblings, 0 replies; 4+ messages in thread
From: Greg Kroah-Hartman @ 2020-03-27 12:53 UTC (permalink / raw)
  To: Matthias Reichl; +Cc: Anthony Mallet, Oliver Neukum, linux-usb

On Fri, Mar 27, 2020 at 01:37:24PM +0100, Matthias Reichl wrote:
> On Thu, Mar 12, 2020 at 02:31:01PM +0100, Anthony Mallet wrote:
> > By default, tty_port_init() initializes those parameters to a multiple
> > of HZ. For instance in line 69 of tty_port.c:
> >    port->close_delay = (50 * HZ) / 100;
> > https://github.com/torvalds/linux/blob/master/drivers/tty/tty_port.c#L69
> > 
> > With e.g. CONFIG_HZ = 250 (as this is the case for Ubuntu 18.04
> > linux-image-4.15.0-37-generic), the default setting for close_delay is
> > thus 125.
> > 
> > When ioctl(fd, TIOCGSERIAL, &s) is executed, the setting returned in
> > user space is '12' (125/10). When ioctl(fd, TIOCSSERIAL, &s) is then
> > executed with the same setting '12', the value is interpreted as '120'
> > which is different from the current setting and a EPERM error may be
> > raised by set_serial_info() if !CAP_SYS_ADMIN.
> > https://github.com/torvalds/linux/blob/master/drivers/usb/class/cdc-acm.c#L919
> > 
> > Fixes: ba2d8ce9db0a6 ("cdc-acm: implement TIOCSSERIAL to avoid blocking close(2)")
> > Signed-off-by: Anthony Mallet <anthony.mallet@laas.fr>
> > ---
> > Changed in v2: fix typo (extra closing brace)
> > Changed in v3: version this patch series
> > Changed in v4: diffed against e6e6ec4 from github.com/torvalds/linux.git
> > ---
> >  drivers/usb/class/cdc-acm.c | 27 +++++++++++++++++----------
> >  1 file changed, 17 insertions(+), 10 deletions(-)
> > 
> > diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
> > index da619176deca..a41a3d27016c 100644
> > --- a/drivers/usb/class/cdc-acm.c
> > +++ b/drivers/usb/class/cdc-acm.c
> > @@ -907,6 +907,7 @@ static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss)
> >  {
> >  	struct acm *acm = tty->driver_data;
> >  	unsigned int closing_wait, close_delay;
> > +	unsigned int old_closing_wait, old_close_delay;
> >  	int retval = 0;
> >  
> >  	close_delay = msecs_to_jiffies(ss->close_delay * 10);
> > @@ -914,18 +915,24 @@ static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss)
> >  			ASYNC_CLOSING_WAIT_NONE :
> >  			msecs_to_jiffies(ss->closing_wait * 10);
> >  
> > +	/* we must redo the rounding here, so that the values match */
> > +	old_close_delay	= jiffies_to_msecs(acm->port.close_delay) / 10;
> > +	old_closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
> > +				ASYNC_CLOSING_WAIT_NONE :
> > +				jiffies_to_msecs(acm->port.closing_wait) / 10;
> > +
> >  	mutex_lock(&acm->port.mutex);
> >  
> > -	if (!capable(CAP_SYS_ADMIN)) {
> > -		if ((close_delay != acm->port.close_delay) ||
> > -		    (closing_wait != acm->port.closing_wait))
> > +	if ((ss->close_delay != old_close_delay) ||
> > +            (ss->closing_wait != old_closing_wait)) {
> > +		if (!capable(CAP_SYS_ADMIN))
> >  			retval = -EPERM;
> > -		else
> > -			retval = -EOPNOTSUPP;
> > -	} else {
> > -		acm->port.close_delay  = close_delay;
> > -		acm->port.closing_wait = closing_wait;
> > -	}
> > +		else {
> > +			acm->port.close_delay  = close_delay;
> > +			acm->port.closing_wait = closing_wait;
> > +		}
> > +	} else
> > +		retval = -EOPNOTSUPP;
> 
> Swapping the order of the capability / close settings checks introduced
> a rather unexpected regression: when running with CAP_SYS_ADMIN we now
> get -EOPNOTSUPP when the close settings are unchanged.
> 
> This will break programs / scripts that set these settings to fixed
> values.
> 
> eg (tested on vanilla 5.4.28) changing the settings works, but
> setting the same value again fails:
> 
> # setserial -a /dev/ttyACM0
> /dev/ttyACM0, Line 0, UART: unknown, Port: 0x0000, IRQ: 0
> 	Baud_base: 9600, close_delay: 50, divisor: 0
> 	closing_wait: 3000
> 	Flags: spd_normal
> 
> # setserial /dev/ttyACM0 closing_wait 2000
> # setserial /dev/ttyACM0 closing_wait 2000
> Cannot set serial info: Operation not supported
> 
> >From the commit message it's not clear why this behaviour was changed
> so I suspect this might be a bug.

Looks like a bug :(

Care to send a patch to fix this up?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-12 13:31 [PATCH v4 1/2] USB: cdc-acm: fix close_delay and closing_wait units in TIOCSSERIAL Anthony Mallet
2020-03-12 13:31 ` [PATCH v4 2/2] USB: cdc-acm: fix rounding error " Anthony Mallet
2020-03-27 12:37   ` Matthias Reichl
2020-03-27 12:53     ` Greg Kroah-Hartman

Linux-USB Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-usb/0 linux-usb/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-usb linux-usb/ https://lore.kernel.org/linux-usb \
		linux-usb@vger.kernel.org
	public-inbox-index linux-usb

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-usb


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git