tty: fix saved termios reset race
Subsystems (8)
Johan Hovold
maintainerPATCH 1/1 tty: fix saved termios reset race
Resetting saved termios state on device registration is needed where a minor number can be reused for an entirely different device and where the old settings may prevent the port from even being opened (e.g. when CLOCAL is not set).
Not all TTY drivers guarantee that the minor number is no longer in use when registering devices however, something which can lead to a use-after-free when closing a TTY (and saving its termios) races with re-registration.
Add a new TTY_DRIVER_RESET_SAVED_TERMIOS flag to request that any saved termios state is reset on registration and only set it for drivers that make sure that the minor number is no longer in use.
--- drivers/staging/greybus/uart.c | 3 ++- drivers/tty/tty_io.c | 18 ++++++++++-------- drivers/usb/class/cdc-acm.c | 2 +- drivers/usb/serial/usb-serial.c | 3 ++- include/linux/tty_driver.h | 6 ++++++ 5 files changed, 21 insertions(+), 11 deletions(-)
drivers/staging/greybus/uart.c +2 -1
@@ -962,7 +962,8 @@ static int gb_tty_init(void)int retval =0 ; gb_tty_driver =tty_alloc_driver (GB_NUM_MINORS ,TTY_DRIVER_REAL_RAW | -TTY_DRIVER_DYNAMIC_DEV ); +TTY_DRIVER_DYNAMIC_DEV | +TTY_DRIVER_RESET_SAVED_TERMIOS );if (IS_ERR (gb_tty_driver)) {pr_err ("Can not allocate tty driver\n" ); retval =PTR_ERR (gb_tty_driver);
drivers/tty/tty_io.c +10 -8
@@ -3256,14 +3256,16 @@ struct device *tty_register_device_attr(struct tty_driver *driver, goto err_put;if (!(driver->flags &TTY_DRIVER_DYNAMIC_ALLOC )) { -/* -* Free any saved termios data so that the termios state is -* reset when reusing a minor number. -*/ - tp = driver->termios[index]; -if (tp) { - driver->termios[index] =NULL ; -kfree (tp); +if (driver->flags &&TTY_DRIVER_RESET_SAVED_TERMIOS ) { +/* +* Free any saved termios data so that the termios state is +* reset when reusing a minor number. +*/ + tp = driver->termios[index]; +if (tp) { + driver->termios[index] =NULL ; +kfree (tp); + } } retval =tty_cdev_add (driver, devt, index,1 );
drivers/usb/class/cdc-acm.c +1 -1
@@ -2150,7 +2150,7 @@ static int __init acm_init(void) {int retval; acm_tty_driver =tty_alloc_driver (ACM_TTY_MINORS ,TTY_DRIVER_REAL_RAW | -TTY_DRIVER_DYNAMIC_DEV ); +TTY_DRIVER_DYNAMIC_DEV |TTY_DRIVER_RESET_SAVED_TERMIOS );if (IS_ERR (acm_tty_driver))return PTR_ERR (acm_tty_driver); acm_tty_driver->driver_name = "acm",
drivers/usb/serial/usb-serial.c +2 -1
@@ -1372,7 +1372,8 @@ static int __init usb_serial_init(void)int result; usb_serial_tty_driver =tty_alloc_driver (USB_SERIAL_TTY_MINORS , -TTY_DRIVER_REAL_RAW |TTY_DRIVER_DYNAMIC_DEV ); +TTY_DRIVER_REAL_RAW |TTY_DRIVER_DYNAMIC_DEV | +TTY_DRIVER_RESET_SAVED_TERMIOS );if (IS_ERR (usb_serial_tty_driver))return PTR_ERR (usb_serial_tty_driver);
include/linux/tty_driver.h +6 -0
@@ -73,6 +73,11 @@ struct serial_struct;* @TTY_DRIVER_NO_WORKQUEUE: * Do not create workqueue when tty_register_driver(). Whenever set, flip * buffer workqueue can be set by tty_port_link_wq() for every port. +* +* @TTY_DRIVER_RESET_SAVED_TERMIOS +* Reset any saved termios settings on device registration when reusing a +* minor number. Must only be set by drivers that guarantee that the minor +* number is no longer in use. */ enum tty_driver_flag {TTY_DRIVER_INSTALLED =BIT (0 ),
@@ -84,6 +89,7 @@ enum tty_driver_flag {TTY_DRIVER_DYNAMIC_ALLOC =BIT (6 ),TTY_DRIVER_UNNUMBERED_NODE =BIT (7 ),TTY_DRIVER_NO_WORKQUEUE =BIT (8 ), + TTY_DRIVER_RESET_SAVED_TERMIOS = BIT(9), }; enum tty_driver_type {
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] tty: logical AND bypasses TTY_DRIVER_RESET_SAVED_TERMIOS check --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] tty: logical AND bypasses TTY_DRIVER_RESET_SAVED_TERMIOS check --
commit c7c5b67aff9403896b61f084f64a1630c60063f9
Author: Johan Hovold <[email protected]>
tty: fix saved termios reset race
This commit adds a new flag, TTY_DRIVER_RESET_SAVED_TERMIOS, to selectively reset termios state only for drivers that ensure the minor number is no longer in use. This aims to prevent a use-after-free race during device re-registration.
Quoted 7 lines
drivers/tty/tty_io.c +0
-0@@ -3256,14 +3256,16 @@ struct device *tty_register_device_attr(struct tty_driver *driver, goto err_put;
drivers/tty/tty_io.c struct device *tty_register_device_attr(struct tty_driver *driver, · line 3259
Quoted 10 lines
drivers/tty/tty_io.c +1
-8if (!(driver->flags &TTY_DRIVER_DYNAMIC_ALLOC )) { -/* -* Free any saved termios data so that the termios state is -* reset when reusing a minor number. -*/ - tp = driver->termios[index]; -if (tp) { - driver->termios[index] =NULL ; -kfree (tp); +if (driver->flags &&TTY_DRIVER_RESET_SAVED_TERMIOS ) {
[Severity: Critical]
Does this code use a logical AND where a bitwise AND is needed?
Since TTY_DRIVER_RESET_SAVED_TERMIOS is a non-zero constant, using && means this condition evaluates to true for any driver with at least one flag set in driver->flags. This appears to bypass the intended safety filter in tty_register_device_attr() and would still apply the termios reset globally to those drivers, leaving the use-after-free vulnerability unfixed.
Quoted 12 lines
drivers/tty/tty_io.c +9
-0+/* +* Free any saved termios data so that the termios state is +* reset when reusing a minor number. +*/ + tp = driver->termios[index]; +if (tp) { + driver->termios[index] =NULL ; +kfree (tp); + } } retval =tty_cdev_add (driver, devt, index,1 );
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Johan Hovold
maintainerQuoted 21 lines · Johan Hovold
Resetting saved termios state on device registration is needed where a minor number can be reused for an entirely different device and where the old settings may prevent the port from even being opened (e.g. when CLOCAL is not set).
Not all TTY drivers guarantee that the minor number is no longer in use when registering devices however, something which can lead to a use-after-free when closing a TTY (and saving its termios) races with re-registration.
Add a new
TTY_DRIVER_RESET_SAVED_TERMIOSflag to request that any saved termios state is reset on registration and only set it for drivers that make sure that the minor number is no longer in use.Fixes 93857edd9829 ("tty: reset termios state on device registration")Reported-by Chengfeng Ye <[email protected]>Cc [email protected] # 4.12Signed-off-by Johan Hovold <[email protected]>---
drivers/tty/tty_io.c struct device *tty_register_device_attr(struct tty_driver *driver, · line 3259
Quoted 10 lines
drivers/tty/tty_io.c +1
-8if (!(driver->flags &TTY_DRIVER_DYNAMIC_ALLOC )) { -/* -* Free any saved termios data so that the termios state is -* reset when reusing a minor number. -*/ - tp = driver->termios[index]; -if (tp) { - driver->termios[index] =NULL ; -kfree (tp); +if (driver->flags &&TTY_DRIVER_RESET_SAVED_TERMIOS ) {
This was of course meant to be a bitwise & as Sashiko noted.
I'll wait a bit before sending a v2.
Johan