提交 b8483052 编写于 作者: P Peter Hurley 提交者: Greg Kroah-Hartman

n_tty: Fix unsafe update of available buffer space

receive_room is used to control the amount of data the flip
buffer work can push to the read buffer. This update is unsafe:

  CPU 0                        |  CPU 1
                               |
                               | n_tty_read()
                               |   n_tty_set_room()
                               |     left = <calc of space>
n_tty_receive_buf()            |
  <push data to buffer>        |
  n_tty_set_room()             |
    left = <calc of space>     |
    tty->receive_room = left   |
                               |     tty->receive_room = left

receive_room is now updated with a stale calculation of the
available buffer space, and the subsequent work loop will likely
overwrite unread data in the input buffer.

Update receive_room atomically with the calculation of the
available buffer space.
Signed-off-by: NPeter Hurley <peter@hurleysoftware.com>
Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
上级 a6e54319
...@@ -115,13 +115,14 @@ static inline int tty_put_user(struct tty_struct *tty, unsigned char x, ...@@ -115,13 +115,14 @@ static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
} }
/** /**
* n_tty_set__room - receive space * n_tty_set_room - receive space
* @tty: terminal * @tty: terminal
* *
* Called by the driver to find out how much data it is * Sets tty->receive_room to reflect the currently available space
* permitted to feed to the line discipline without any being lost * in the input buffer, and re-schedules the flip buffer work if space
* and thus to manage flow control. Not serialized. Answers for the * just became available.
* "instant". *
* Locks: Concurrent update is protected with read_lock
*/ */
static void n_tty_set_room(struct tty_struct *tty) static void n_tty_set_room(struct tty_struct *tty)
...@@ -129,8 +130,10 @@ static void n_tty_set_room(struct tty_struct *tty) ...@@ -129,8 +130,10 @@ static void n_tty_set_room(struct tty_struct *tty)
struct n_tty_data *ldata = tty->disc_data; struct n_tty_data *ldata = tty->disc_data;
int left; int left;
int old_left; int old_left;
unsigned long flags;
raw_spin_lock_irqsave(&ldata->read_lock, flags);
/* ldata->read_cnt is not read locked ? */
if (I_PARMRK(tty)) { if (I_PARMRK(tty)) {
/* Multiply read_cnt by 3, since each byte might take up to /* Multiply read_cnt by 3, since each byte might take up to
* three times as many spaces when PARMRK is set (depending on * three times as many spaces when PARMRK is set (depending on
...@@ -150,6 +153,8 @@ static void n_tty_set_room(struct tty_struct *tty) ...@@ -150,6 +153,8 @@ static void n_tty_set_room(struct tty_struct *tty)
old_left = tty->receive_room; old_left = tty->receive_room;
tty->receive_room = left; tty->receive_room = left;
raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
/* Did this open up the receive buffer? We may need to flip */ /* Did this open up the receive buffer? We may need to flip */
if (left && !old_left) { if (left && !old_left) {
WARN_RATELIMIT(tty->port->itty == NULL, WARN_RATELIMIT(tty->port->itty == NULL,
...@@ -1872,7 +1877,6 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, ...@@ -1872,7 +1877,6 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
retval = -ERESTARTSYS; retval = -ERESTARTSYS;
break; break;
} }
/* FIXME: does n_tty_set_room need locking ? */
n_tty_set_room(tty); n_tty_set_room(tty);
timeout = schedule_timeout(timeout); timeout = schedule_timeout(timeout);
continue; continue;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册