Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

serdev: ttyport: add missing receive_buf sanity checks

The receive_buf tty-port callback should return the number of bytes
accepted and must specifically never return a negative errno (or a value
larger than the buffer size) to the tty layer.

A serdev driver not providing a receive_buf callback would currently
cause the flush_to_ldisc() worker to spin in a tight loop when the tty
buffer pointers are incremented with -EINVAL (-22) after data has been
received.

A serdev driver occasionally returning a negative errno (or a too large
byte count) could cause information leaks or crashes when accessing
memory outside the tty buffers in consecutive callbacks.

Fixes: cd6484e1830b ("serdev: Introduce new bus for serial attached devices")
Cc: stable <stable@vger.kernel.org> # 4.11
Signed-off-by: Johan Hovold <johan@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Johan Hovold and committed by
Greg Kroah-Hartman
eb281683 0ff3ab70

+12 -1
+12 -1
drivers/tty/serdev/serdev-ttyport.c
··· 27 27 { 28 28 struct serdev_controller *ctrl = port->client_data; 29 29 struct serport *serport = serdev_controller_get_drvdata(ctrl); 30 + int ret; 30 31 31 32 if (!test_bit(SERPORT_ACTIVE, &serport->flags)) 32 33 return 0; 33 34 34 - return serdev_controller_receive_buf(ctrl, cp, count); 35 + ret = serdev_controller_receive_buf(ctrl, cp, count); 36 + 37 + dev_WARN_ONCE(&ctrl->dev, ret < 0 || ret > count, 38 + "receive_buf returns %d (count = %zu)\n", 39 + ret, count); 40 + if (ret < 0) 41 + return 0; 42 + else if (ret > count) 43 + return count; 44 + 45 + return ret; 35 46 } 36 47 37 48 static void ttyport_write_wakeup(struct tty_port *port)