Skip to content

Commit 1ee33b1

Browse files
Tetsuo Handagregkh
authored andcommitted
tty: n_hdlc: make n_hdlc_tty_wakeup() asynchronous
syzbot is reporting that an unprivileged user who logged in from tty console can crash the system using a reproducer shown below [1], for n_hdlc_tty_wakeup() is synchronously calling n_hdlc_send_frames(). ---------- #include <sys/ioctl.h> #include <unistd.h> int main(int argc, char *argv[]) { const int disc = 0xd; ioctl(1, TIOCSETD, &disc); while (1) { ioctl(1, TCXONC, 0); write(1, "", 1); ioctl(1, TCXONC, 1); /* Kernel panic - not syncing: scheduling while atomic */ } } ---------- Linus suspected that "struct tty_ldisc"->ops->write_wakeup() must not sleep, and Jiri confirmed it from include/linux/tty_ldisc.h. Thus, defer n_hdlc_send_frames() from n_hdlc_tty_wakeup() to a WQ context like net/nfc/nci/uart.c does. Link: https://syzkaller.appspot.com/bug?extid=5f47a8cea6a12b77a876 [1] Reported-by: syzbot <[email protected]> Cc: stable <[email protected]> Analyzed-by: Fabio M. De Francesco <[email protected]> Suggested-by: Linus Torvalds <[email protected]> Confirmed-by: Jiri Slaby <[email protected]> Reviewed-by: Fabio M. De Francesco <[email protected]> Signed-off-by: Tetsuo Handa <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 0fcfb00 commit 1ee33b1

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

drivers/tty/n_hdlc.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ struct n_hdlc {
140140
struct n_hdlc_buf_list rx_buf_list;
141141
struct n_hdlc_buf_list tx_free_buf_list;
142142
struct n_hdlc_buf_list rx_free_buf_list;
143+
struct work_struct write_work;
144+
struct tty_struct *tty_for_write_work;
143145
};
144146

145147
/*
@@ -154,6 +156,7 @@ static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *list);
154156
/* Local functions */
155157

156158
static struct n_hdlc *n_hdlc_alloc(void);
159+
static void n_hdlc_tty_write_work(struct work_struct *work);
157160

158161
/* max frame size for memory allocations */
159162
static int maxframe = 4096;
@@ -210,6 +213,8 @@ static void n_hdlc_tty_close(struct tty_struct *tty)
210213
wake_up_interruptible(&tty->read_wait);
211214
wake_up_interruptible(&tty->write_wait);
212215

216+
cancel_work_sync(&n_hdlc->write_work);
217+
213218
n_hdlc_free_buf_list(&n_hdlc->rx_free_buf_list);
214219
n_hdlc_free_buf_list(&n_hdlc->tx_free_buf_list);
215220
n_hdlc_free_buf_list(&n_hdlc->rx_buf_list);
@@ -241,6 +246,8 @@ static int n_hdlc_tty_open(struct tty_struct *tty)
241246
return -ENFILE;
242247
}
243248

249+
INIT_WORK(&n_hdlc->write_work, n_hdlc_tty_write_work);
250+
n_hdlc->tty_for_write_work = tty;
244251
tty->disc_data = n_hdlc;
245252
tty->receive_room = 65536;
246253

@@ -334,6 +341,20 @@ static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
334341
goto check_again;
335342
} /* end of n_hdlc_send_frames() */
336343

344+
/**
345+
* n_hdlc_tty_write_work - Asynchronous callback for transmit wakeup
346+
* @work: pointer to work_struct
347+
*
348+
* Called when low level device driver can accept more send data.
349+
*/
350+
static void n_hdlc_tty_write_work(struct work_struct *work)
351+
{
352+
struct n_hdlc *n_hdlc = container_of(work, struct n_hdlc, write_work);
353+
struct tty_struct *tty = n_hdlc->tty_for_write_work;
354+
355+
n_hdlc_send_frames(n_hdlc, tty);
356+
} /* end of n_hdlc_tty_write_work() */
357+
337358
/**
338359
* n_hdlc_tty_wakeup - Callback for transmit wakeup
339360
* @tty: pointer to associated tty instance data
@@ -344,7 +365,7 @@ static void n_hdlc_tty_wakeup(struct tty_struct *tty)
344365
{
345366
struct n_hdlc *n_hdlc = tty->disc_data;
346367

347-
n_hdlc_send_frames(n_hdlc, tty);
368+
schedule_work(&n_hdlc->write_work);
348369
} /* end of n_hdlc_tty_wakeup() */
349370

350371
/**

0 commit comments

Comments
 (0)