Commit 8f3084c4 authored by Johan Hovold's avatar Johan Hovold Committed by Jiri Slaby

USB: console: fix potential use after free

commit 32a4bf2e upstream.

Use tty kref to release the fake tty in usb_console_setup to avoid use
after free if the underlying serial driver has acquired a reference.

Note that using the tty destructor release_one_tty requires some more
state to be initialised.

Fixes: 4a90f09b ("tty: usb-serial krefs")
Signed-off-by: default avatarJohan Hovold <johan@kernel.org>
Signed-off-by: default avatarJiri Slaby <jslaby@suse.cz>
parent 56dfc960
...@@ -47,6 +47,8 @@ static struct console usbcons; ...@@ -47,6 +47,8 @@ static struct console usbcons;
* ------------------------------------------------------------ * ------------------------------------------------------------
*/ */
static const struct tty_operations usb_console_fake_tty_ops = {
};
/* /*
* The parsing of the command line works exactly like the * The parsing of the command line works exactly like the
...@@ -139,15 +141,18 @@ static int usb_console_setup(struct console *co, char *options) ...@@ -139,15 +141,18 @@ static int usb_console_setup(struct console *co, char *options)
goto reset_open_count; goto reset_open_count;
} }
kref_init(&tty->kref); kref_init(&tty->kref);
tty_port_tty_set(&port->port, tty);
tty->driver = usb_serial_tty_driver; tty->driver = usb_serial_tty_driver;
tty->index = co->index; tty->index = co->index;
init_ldsem(&tty->ldisc_sem); init_ldsem(&tty->ldisc_sem);
INIT_LIST_HEAD(&tty->tty_files);
kref_get(&tty->driver->kref);
tty->ops = &usb_console_fake_tty_ops;
if (tty_init_termios(tty)) { if (tty_init_termios(tty)) {
retval = -ENOMEM; retval = -ENOMEM;
dev_err(&port->dev, "no more memory\n"); dev_err(&port->dev, "no more memory\n");
goto free_tty; goto put_tty;
} }
tty_port_tty_set(&port->port, tty);
} }
/* only call the device specific open if this /* only call the device specific open if this
...@@ -165,7 +170,7 @@ static int usb_console_setup(struct console *co, char *options) ...@@ -165,7 +170,7 @@ static int usb_console_setup(struct console *co, char *options)
serial->type->set_termios(tty, port, &dummy); serial->type->set_termios(tty, port, &dummy);
tty_port_tty_set(&port->port, NULL); tty_port_tty_set(&port->port, NULL);
kfree(tty); tty_kref_put(tty);
} }
set_bit(ASYNCB_INITIALIZED, &port->port.flags); set_bit(ASYNCB_INITIALIZED, &port->port.flags);
} }
...@@ -181,8 +186,8 @@ static int usb_console_setup(struct console *co, char *options) ...@@ -181,8 +186,8 @@ static int usb_console_setup(struct console *co, char *options)
fail: fail:
tty_port_tty_set(&port->port, NULL); tty_port_tty_set(&port->port, NULL);
free_tty: put_tty:
kfree(tty); tty_kref_put(tty);
reset_open_count: reset_open_count:
port->port.count = 0; port->port.count = 0;
usb_autopm_put_interface(serial->interface); usb_autopm_put_interface(serial->interface);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment