linux-serial.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johan Hovold <johan@kernel.org>
To: Rob Herring <robh@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Jiri Slaby <jslaby@suse.com>,
	linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org,
	Johan Hovold <johan@kernel.org>, stable <stable@vger.kernel.org>
Subject: [PATCH 1/8] serdev: ttyport: add missing receive_buf sanity checks
Date: Fri,  3 Nov 2017 15:30:52 +0100	[thread overview]
Message-ID: <20171103143059.20749-2-johan@kernel.org> (raw)
In-Reply-To: <20171103143059.20749-1-johan@kernel.org>

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>
---
 drivers/tty/serdev/serdev-ttyport.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/tty/serdev/serdev-ttyport.c b/drivers/tty/serdev/serdev-ttyport.c
index 5b09ce920117..b8bc60a251c6 100644
--- a/drivers/tty/serdev/serdev-ttyport.c
+++ b/drivers/tty/serdev/serdev-ttyport.c
@@ -35,11 +35,22 @@ static int ttyport_receive_buf(struct tty_port *port, const unsigned char *cp,
 {
 	struct serdev_controller *ctrl = port->client_data;
 	struct serport *serport = serdev_controller_get_drvdata(ctrl);
+	int ret;
 
 	if (!test_bit(SERPORT_ACTIVE, &serport->flags))
 		return 0;
 
-	return serdev_controller_receive_buf(ctrl, cp, count);
+	ret = serdev_controller_receive_buf(ctrl, cp, count);
+
+	dev_WARN_ONCE(&ctrl->dev, ret < 0 || ret > count,
+				"receive_buf returns %d (count = %zu)\n",
+				ret, count);
+	if (ret < 0)
+		return 0;
+	else if (ret > count)
+		return count;
+
+	return ret;
 }
 
 static void ttyport_write_wakeup(struct tty_port *port)
-- 
2.15.0

  reply	other threads:[~2017-11-03 14:30 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-03 14:30 [PATCH 0/8] serdev: receive_buf and locking fixes Johan Hovold
2017-11-03 14:30 ` Johan Hovold [this message]
2017-11-03 14:30 ` [PATCH 2/8] serdev: fix receive_buf return value when no callback Johan Hovold
2017-11-03 14:30 ` [PATCH 3/8] serdev: document driver callbacks Johan Hovold
2017-11-03 14:30 ` [PATCH 4/8] serdev: ttyport: fix NULL-deref on hangup Johan Hovold
2017-11-03 14:30 ` [PATCH 5/8] serdev: ttyport: fix tty locking in close Johan Hovold
2017-11-03 14:30 ` [PATCH 6/8] serdev: ttyport: release tty lock sooner on open Johan Hovold
2017-11-03 14:30 ` [PATCH 7/8] serdev: ttyport: ignore carrier detect to avoid hangups Johan Hovold
2017-11-03 14:30 ` [PATCH 8/8] serdev: ttyport: do not used keyed wakeup in write_wakeup Johan Hovold
2017-11-28 15:04   ` Greg Kroah-Hartman
2017-11-28 15:16     ` Johan Hovold
2017-11-28 19:39       ` Greg Kroah-Hartman
2017-11-29  9:48         ` Johan Hovold
2017-12-15 19:26           ` Greg Kroah-Hartman
2017-12-18 11:03             ` Johan Hovold

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171103143059.20749-2-johan@kernel.org \
    --to=johan@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jslaby@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).