Skip to content

Commit 92dc0b1

Browse files
jhovoldgregkh
authored andcommitted
staging: greybus: uart: fix tty use after free
User space can hold a tty open indefinitely and tty drivers must not release the underlying structures until the last user is gone. Switch to using the tty-port reference counter to manage the life time of the greybus tty state to avoid use after free after a disconnect. Fixes: a18e151 ("greybus: more uart work") Cc: [email protected] # 4.9 Reviewed-by: Alex Elder <[email protected]> Signed-off-by: Johan Hovold <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 6880fa6 commit 92dc0b1

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

drivers/staging/greybus/uart.c

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,17 @@ static void gb_tty_port_shutdown(struct tty_port *port)
761761
gbphy_runtime_put_autosuspend(gb_tty->gbphy_dev);
762762
}
763763

764+
static void gb_tty_port_destruct(struct tty_port *port)
765+
{
766+
struct gb_tty *gb_tty = container_of(port, struct gb_tty, port);
767+
768+
if (gb_tty->minor != GB_NUM_MINORS)
769+
release_minor(gb_tty);
770+
kfifo_free(&gb_tty->write_fifo);
771+
kfree(gb_tty->buffer);
772+
kfree(gb_tty);
773+
}
774+
764775
static const struct tty_operations gb_ops = {
765776
.install = gb_tty_install,
766777
.open = gb_tty_open,
@@ -786,6 +797,7 @@ static const struct tty_port_operations gb_port_ops = {
786797
.dtr_rts = gb_tty_dtr_rts,
787798
.activate = gb_tty_port_activate,
788799
.shutdown = gb_tty_port_shutdown,
800+
.destruct = gb_tty_port_destruct,
789801
};
790802

791803
static int gb_uart_probe(struct gbphy_device *gbphy_dev,
@@ -798,39 +810,43 @@ static int gb_uart_probe(struct gbphy_device *gbphy_dev,
798810
int retval;
799811
int minor;
800812

801-
gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
802-
if (!gb_tty)
803-
return -ENOMEM;
804-
805813
connection = gb_connection_create(gbphy_dev->bundle,
806814
le16_to_cpu(gbphy_dev->cport_desc->id),
807815
gb_uart_request_handler);
808-
if (IS_ERR(connection)) {
809-
retval = PTR_ERR(connection);
810-
goto exit_tty_free;
811-
}
816+
if (IS_ERR(connection))
817+
return PTR_ERR(connection);
812818

813819
max_payload = gb_operation_get_payload_size_max(connection);
814820
if (max_payload < sizeof(struct gb_uart_send_data_request)) {
815821
retval = -EINVAL;
816822
goto exit_connection_destroy;
817823
}
818824

825+
gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
826+
if (!gb_tty) {
827+
retval = -ENOMEM;
828+
goto exit_connection_destroy;
829+
}
830+
831+
tty_port_init(&gb_tty->port);
832+
gb_tty->port.ops = &gb_port_ops;
833+
gb_tty->minor = GB_NUM_MINORS;
834+
819835
gb_tty->buffer_payload_max = max_payload -
820836
sizeof(struct gb_uart_send_data_request);
821837

822838
gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
823839
if (!gb_tty->buffer) {
824840
retval = -ENOMEM;
825-
goto exit_connection_destroy;
841+
goto exit_put_port;
826842
}
827843

828844
INIT_WORK(&gb_tty->tx_work, gb_uart_tx_write_work);
829845

830846
retval = kfifo_alloc(&gb_tty->write_fifo, GB_UART_WRITE_FIFO_SIZE,
831847
GFP_KERNEL);
832848
if (retval)
833-
goto exit_buf_free;
849+
goto exit_put_port;
834850

835851
gb_tty->credits = GB_UART_FIRMWARE_CREDITS;
836852
init_completion(&gb_tty->credits_complete);
@@ -844,7 +860,7 @@ static int gb_uart_probe(struct gbphy_device *gbphy_dev,
844860
} else {
845861
retval = minor;
846862
}
847-
goto exit_kfifo_free;
863+
goto exit_put_port;
848864
}
849865

850866
gb_tty->minor = minor;
@@ -853,17 +869,14 @@ static int gb_uart_probe(struct gbphy_device *gbphy_dev,
853869
init_waitqueue_head(&gb_tty->wioctl);
854870
mutex_init(&gb_tty->mutex);
855871

856-
tty_port_init(&gb_tty->port);
857-
gb_tty->port.ops = &gb_port_ops;
858-
859872
gb_tty->connection = connection;
860873
gb_tty->gbphy_dev = gbphy_dev;
861874
gb_connection_set_data(connection, gb_tty);
862875
gb_gbphy_set_data(gbphy_dev, gb_tty);
863876

864877
retval = gb_connection_enable_tx(connection);
865878
if (retval)
866-
goto exit_release_minor;
879+
goto exit_put_port;
867880

868881
send_control(gb_tty, gb_tty->ctrlout);
869882

@@ -890,16 +903,10 @@ static int gb_uart_probe(struct gbphy_device *gbphy_dev,
890903

891904
exit_connection_disable:
892905
gb_connection_disable(connection);
893-
exit_release_minor:
894-
release_minor(gb_tty);
895-
exit_kfifo_free:
896-
kfifo_free(&gb_tty->write_fifo);
897-
exit_buf_free:
898-
kfree(gb_tty->buffer);
906+
exit_put_port:
907+
tty_port_put(&gb_tty->port);
899908
exit_connection_destroy:
900909
gb_connection_destroy(connection);
901-
exit_tty_free:
902-
kfree(gb_tty);
903910

904911
return retval;
905912
}
@@ -930,15 +937,10 @@ static void gb_uart_remove(struct gbphy_device *gbphy_dev)
930937
gb_connection_disable_rx(connection);
931938
tty_unregister_device(gb_tty_driver, gb_tty->minor);
932939

933-
/* FIXME - free transmit / receive buffers */
934-
935940
gb_connection_disable(connection);
936-
tty_port_destroy(&gb_tty->port);
937941
gb_connection_destroy(connection);
938-
release_minor(gb_tty);
939-
kfifo_free(&gb_tty->write_fifo);
940-
kfree(gb_tty->buffer);
941-
kfree(gb_tty);
942+
943+
tty_port_put(&gb_tty->port);
942944
}
943945

944946
static int gb_tty_init(void)

0 commit comments

Comments
 (0)