aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeter Hurley <peter@hurleysoftware.com>2013-06-15 07:28:30 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2013-06-17 15:55:30 -0400
commitb84830527645dfe7b7a5cc03518e3c791b4ee9e0 (patch)
treeece65ac8c7dd14703870d6417d99f7f856eac4e8
parenta6e54319a7499bf754efb3a2cb2f5d4901ccbcff (diff)
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: Peter Hurley <peter@hurleysoftware.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--drivers/tty/n_tty.c18
1 files changed, 11 insertions, 7 deletions
diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
index fa5cb4654c4f..064616542cb5 100644
--- a/drivers/tty/n_tty.c
+++ b/drivers/tty/n_tty.c
@@ -115,13 +115,14 @@ static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
115} 115}
116 116
117/** 117/**
118 * n_tty_set__room - receive space 118 * n_tty_set_room - receive space
119 * @tty: terminal 119 * @tty: terminal
120 * 120 *
121 * Called by the driver to find out how much data it is 121 * Sets tty->receive_room to reflect the currently available space
122 * permitted to feed to the line discipline without any being lost 122 * in the input buffer, and re-schedules the flip buffer work if space
123 * and thus to manage flow control. Not serialized. Answers for the 123 * just became available.
124 * "instant". 124 *
125 * Locks: Concurrent update is protected with read_lock
125 */ 126 */
126 127
127static void n_tty_set_room(struct tty_struct *tty) 128static void n_tty_set_room(struct tty_struct *tty)
@@ -129,8 +130,10 @@ static void n_tty_set_room(struct tty_struct *tty)
129 struct n_tty_data *ldata = tty->disc_data; 130 struct n_tty_data *ldata = tty->disc_data;
130 int left; 131 int left;
131 int old_left; 132 int old_left;
133 unsigned long flags;
134
135 raw_spin_lock_irqsave(&ldata->read_lock, flags);
132 136
133 /* ldata->read_cnt is not read locked ? */
134 if (I_PARMRK(tty)) { 137 if (I_PARMRK(tty)) {
135 /* Multiply read_cnt by 3, since each byte might take up to 138 /* Multiply read_cnt by 3, since each byte might take up to
136 * three times as many spaces when PARMRK is set (depending on 139 * 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 old_left = tty->receive_room; 153 old_left = tty->receive_room;
151 tty->receive_room = left; 154 tty->receive_room = left;
152 155
156 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
157
153 /* Did this open up the receive buffer? We may need to flip */ 158 /* Did this open up the receive buffer? We may need to flip */
154 if (left && !old_left) { 159 if (left && !old_left) {
155 WARN_RATELIMIT(tty->port->itty == NULL, 160 WARN_RATELIMIT(tty->port->itty == NULL,
@@ -1872,7 +1877,6 @@ do_it_again:
1872 retval = -ERESTARTSYS; 1877 retval = -ERESTARTSYS;
1873 break; 1878 break;
1874 } 1879 }
1875 /* FIXME: does n_tty_set_room need locking ? */
1876 n_tty_set_room(tty); 1880 n_tty_set_room(tty);
1877 timeout = schedule_timeout(timeout); 1881 timeout = schedule_timeout(timeout);
1878 continue; 1882 continue;