Skip to content

Commit 32dd59f

Browse files
dstarke-siemensgregkh
authored andcommitted
tty: n_gsm: fix race condition in gsmld_write()
The function may be used by the user directly and also by the n_gsm internal functions. They can lead into a race condition which results in interleaved frames if both are writing at the same time. The receiving side is not able to decode those interleaved frames correctly. Add a lock around the low side tty write to avoid race conditions and frame interleaving between user originated writes and n_gsm writes. Fixes: e1eaea4 ("tty: n_gsm line discipline") Signed-off-by: Daniel Starke <daniel.starke@siemens.com> Link: https://lore.kernel.org/r/20220701061652.39604-9-daniel.starke@siemens.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4fae831 commit 32dd59f

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

drivers/tty/n_gsm.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,11 +2859,24 @@ static ssize_t gsmld_read(struct tty_struct *tty, struct file *file,
28592859
static ssize_t gsmld_write(struct tty_struct *tty, struct file *file,
28602860
const unsigned char *buf, size_t nr)
28612861
{
2862-
int space = tty_write_room(tty);
2862+
struct gsm_mux *gsm = tty->disc_data;
2863+
unsigned long flags;
2864+
int space;
2865+
int ret;
2866+
2867+
if (!gsm)
2868+
return -ENODEV;
2869+
2870+
ret = -ENOBUFS;
2871+
spin_lock_irqsave(&gsm->tx_lock, flags);
2872+
space = tty_write_room(tty);
28632873
if (space >= nr)
2864-
return tty->ops->write(tty, buf, nr);
2865-
set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2866-
return -ENOBUFS;
2874+
ret = tty->ops->write(tty, buf, nr);
2875+
else
2876+
set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2877+
spin_unlock_irqrestore(&gsm->tx_lock, flags);
2878+
2879+
return ret;
28672880
}
28682881

28692882
/**

0 commit comments

Comments
 (0)