Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3315009imu; Sun, 11 Nov 2018 12:11:32 -0800 (PST) X-Google-Smtp-Source: AJdET5cqGHyHfd7DPmMpCwvY/GkJ2s5Z6vyIb3UlSDW6UICyLn4jzCdbDo8oSqhnKVhz//0Z58MW X-Received: by 2002:a63:2f86:: with SMTP id v128mr14521423pgv.407.1541967092884; Sun, 11 Nov 2018 12:11:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541967092; cv=none; d=google.com; s=arc-20160816; b=Y8ugA+Std/MFujplXhlgwBUsPVy5akbKeTudoiXw5cejPP2VTrFFNSBz5i1xdE+kqh BKG/U100a/REOJE/CXROa79djQZTIsgowOzcnxvugpk3cgziBwoal2CLsTap8f0vGDH9 h+3q1DGZAlmXjCLSc1lT+7VNaPbB7zZWe/Iz5jZKnX2D80IOBtbZCn+JFZaxvwMoiv7s baoZpqNAjUPG0VF0qYwOPoOY3QwtjErrZHQeIdP4v87DZI4baePHkSaGUpVJsdqV8wdn fLIObBvGHmibWtPAM5VknIjwnLFhYJrCsiZKaIINPYX8tU6eodG8etpvTYHPF2AB1yfQ jfSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=ewR6tdaT+ur7+3WWU8nH71mO6EgVMoKjFIIBX4r/vbo=; b=RQe5PdnMCwkYqvMqIq8oKbhv2DDfNyCmELaPKZ6h7j1UkMVlfMMU2FuWJHozk2FjhE qxDRmbFRam7l57M1ON2Sf4s0xZUV84ozej2xEID62sX7eitY7vXeRbQV8hEmajzeY9d5 hQYYTcJf6Dzbxy+Ci0jvKrHtNvvC4KKVGJO1JN4vEvHXdGFO9C0cMxVMsUH3chtR28OU WBuDqN7cqflBqbje5a+NIYQ4jiVFvtUM+HZt+a5UgDksIQDD85MsFEvP2OIVuTe3+LG4 ZfiqxM/fgZ3s/RSL09GtRrf0x3p1j0MnSgT7hnBXbyG9hHmKu0ixh/gVbwMgZvoNnBl4 Vvjw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n1-v6si14925333pld.378.2018.11.11.12.11.17; Sun, 11 Nov 2018 12:11:32 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731257AbeKLF77 (ORCPT + 99 others); Mon, 12 Nov 2018 00:59:59 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:52348 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727357AbeKLF77 (ORCPT ); Mon, 12 Nov 2018 00:59:59 -0500 Received: from [192.168.4.242] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gLvsv-0000l7-BH; Sun, 11 Nov 2018 19:59:05 +0000 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1gLvsU-0001eR-TA; Sun, 11 Nov 2018 19:58:38 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Peter Hurley" , "Tetsuo Handa" , "syzbot" , "Greg Kroah-Hartman" Date: Sun, 11 Nov 2018 19:49:05 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 209/366] n_tty: Fix stall at n_tty_receive_char_special(). In-Reply-To: X-SA-Exim-Connect-IP: 192.168.4.242 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.61-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Tetsuo Handa commit 3d63b7e4ae0dc5e02d28ddd2fa1f945defc68d81 upstream. syzbot is reporting stalls at n_tty_receive_char_special() [1]. This is because comparison is not working as expected since ldata->read_head can change at any moment. Mitigate this by explicitly masking with buffer size when checking condition for "while" loops. [1] https://syzkaller.appspot.com/bug?id=3d7481a346958d9469bebbeb0537d5f056bdd6e8 Signed-off-by: Tetsuo Handa Reported-by: syzbot Fixes: bc5a5e3f45d04784 ("n_tty: Don't wrap input buffer indices at buffer size") Cc: Peter Hurley Signed-off-by: Greg Kroah-Hartman Signed-off-by: Ben Hutchings --- drivers/tty/n_tty.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -127,6 +127,8 @@ struct n_tty_data { struct mutex output_lock; }; +#define MASK(x) ((x) & (N_TTY_BUF_SIZE - 1)) + static inline size_t read_cnt(struct n_tty_data *ldata) { return ldata->read_head - ldata->read_tail; @@ -1032,14 +1034,15 @@ static void eraser(unsigned char c, stru } seen_alnums = 0; - while (ldata->read_head != ldata->canon_head) { + while (MASK(ldata->read_head) != MASK(ldata->canon_head)) { head = ldata->read_head; /* erase a single possibly multibyte character */ do { head--; c = read_buf(ldata, head); - } while (is_continuation(c, tty) && head != ldata->canon_head); + } while (is_continuation(c, tty) && + MASK(head) != MASK(ldata->canon_head)); /* do not partially erase */ if (is_continuation(c, tty)) @@ -1081,7 +1084,7 @@ static void eraser(unsigned char c, stru * This info is used to go back the correct * number of columns. */ - while (tail != ldata->canon_head) { + while (MASK(tail) != MASK(ldata->canon_head)) { tail--; c = read_buf(ldata, tail); if (c == '\t') { @@ -1341,7 +1344,7 @@ n_tty_receive_char_special(struct tty_st finish_erasing(ldata); echo_char(c, tty); echo_char_raw('\n', ldata); - while (tail != ldata->read_head) { + while (MASK(tail) != MASK(ldata->read_head)) { echo_char(read_buf(ldata, tail), tty); tail++; } @@ -2506,7 +2509,7 @@ static unsigned long inq_canon(struct n_ tail = ldata->read_tail; nr = head - tail; /* Skip EOF-chars.. */ - while (head != tail) { + while (MASK(head) != MASK(tail)) { if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) && read_buf(ldata, tail) == __DISABLED_CHAR) nr--;