Received: by 2002:ac0:a591:0:0:0:0:0 with SMTP id m17-v6csp1419581imm; Thu, 5 Jul 2018 23:04:35 -0700 (PDT) X-Google-Smtp-Source: AAOMgpf/cueU2aIVcnrNSlnI3CsbNfAyKEKg959URz7H6fBPa5li7eHYkrIPx20AYt44JiZsap1u X-Received: by 2002:a62:444d:: with SMTP id r74-v6mr9227881pfa.96.1530857075721; Thu, 05 Jul 2018 23:04:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530857075; cv=none; d=google.com; s=arc-20160816; b=q36xA3rA7EwLXLq4g9+QBKmCdpq+qcxN9Zc1lzWHloAC5ogBV8sb+6ILD9SuA2iUnY nSrz4Q9fybHa+7skL2C/bHrWL1XzE5A6CyG31Za/tNA33qu+7+8udR/1fZ8DYD+1GH7W mL9kx1kcOw4B9z4zcC4LL11fpunA1JK/4AMrzN8FzcIjrrET3cTSw9FpSHJ3p+H4Nfx1 Il9QZSOZb9is88pEhTD3lWvUpxpfF1NwrgVjVaVBL1sJqDl1Eux49nR2MfdE6WPXdnQC BR4OmkLiKa+eQ63Do4F7iiRjXGU1AsuEn5L4CoIeFr0IyNR6dPVBhxh8dk/ZGYnR8I+K n6Tg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=56Hw9V1cNtTCu65QYfF4LNe+7XL5dBoHGh6ILP/4K9c=; b=dzrqy3J9qOJYBMRVMQtyesQGwxBaJ91lscOG2BbygtzHgbhtjI87Gu2dRKl1VNIKXf nYknhAN/57euZtQlf+n7oyBvS4HHAZQ5vTZYPPzvdIpDXLF9wsNVQC2IiS3JnliVS1zw RQqzhOC64ah7I9V7NFEWmcZwtDYr82qb8ftq3SewXJ2pYJxTlV/3c7y8PPSO/Hl8EyV2 6t1pQilL/wrUc1BIAvG6dBV1ABpFu4C5FiSXSJDePizioNOHbbur+qibE0+KfYCMmZgp pnjJ6U8Js6MyEHVHyHP+tTIzAYQwoZMkGZRnsTxifufTsXUOugumjQaYAb5lfJRLXAoE 1PBA== 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 l13-v6si7486138pff.261.2018.07.05.23.04.21; Thu, 05 Jul 2018 23:04:35 -0700 (PDT) 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 S934057AbeGFGCu (ORCPT + 99 others); Fri, 6 Jul 2018 02:02:50 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:60492 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933826AbeGFFuN (ORCPT ); Fri, 6 Jul 2018 01:50:13 -0400 Received: from localhost (D57D388D.static.ziggozakelijk.nl [213.125.56.141]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 73850BC6; Fri, 6 Jul 2018 05:50:12 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tetsuo Handa , syzbot , Peter Hurley Subject: [PATCH 4.14 09/61] n_tty: Fix stall at n_tty_receive_char_special(). Date: Fri, 6 Jul 2018 07:46:33 +0200 Message-Id: <20180706054712.689504017@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180706054712.332416244@linuxfoundation.org> References: <20180706054712.332416244@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable 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: stable Cc: Peter Hurley Signed-off-by: Greg Kroah-Hartman --- 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 @@ -126,6 +126,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; @@ -980,14 +982,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)) @@ -1029,7 +1032,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') { @@ -1304,7 +1307,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++; } @@ -2413,7 +2416,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--;