USB: cdc-acm: fix rounding error in TIOCSSERIAL

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>
Cc: stable <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20200312133101.7096-2-anthony.mallet@laas.fr
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by Anthony Mallet and committed by Greg Kroah-Hartman b401f8c4 633e2b2d

Changed files
+16 -9
drivers
usb
class
+16 -9
drivers/usb/class/cdc-acm.c
··· 907 907 { 908 908 struct acm *acm = tty->driver_data; 909 909 unsigned int closing_wait, close_delay; 910 + unsigned int old_closing_wait, old_close_delay; 910 911 int retval = 0; 911 912 912 913 close_delay = msecs_to_jiffies(ss->close_delay * 10); ··· 915 914 ASYNC_CLOSING_WAIT_NONE : 916 915 msecs_to_jiffies(ss->closing_wait * 10); 917 916 917 + /* we must redo the rounding here, so that the values match */ 918 + old_close_delay = jiffies_to_msecs(acm->port.close_delay) / 10; 919 + old_closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ? 920 + ASYNC_CLOSING_WAIT_NONE : 921 + jiffies_to_msecs(acm->port.closing_wait) / 10; 922 + 918 923 mutex_lock(&acm->port.mutex); 919 924 920 - if (!capable(CAP_SYS_ADMIN)) { 921 - if ((close_delay != acm->port.close_delay) || 922 - (closing_wait != acm->port.closing_wait)) 925 + if ((ss->close_delay != old_close_delay) || 926 + (ss->closing_wait != old_closing_wait)) { 927 + if (!capable(CAP_SYS_ADMIN)) 923 928 retval = -EPERM; 924 - else 925 - retval = -EOPNOTSUPP; 926 - } else { 927 - acm->port.close_delay = close_delay; 928 - acm->port.closing_wait = closing_wait; 929 - } 929 + else { 930 + acm->port.close_delay = close_delay; 931 + acm->port.closing_wait = closing_wait; 932 + } 933 + } else 934 + retval = -EOPNOTSUPP; 930 935 931 936 mutex_unlock(&acm->port.mutex); 932 937 return retval;