Received: by 2002:ac0:a591:0:0:0:0:0 with SMTP id m17-v6csp1408515imm; Thu, 5 Jul 2018 22:48:56 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcHnZHBunetSZMiCxrL9Gx+TBUdq784BwW4A9wyriDwEIoeLWzyt71n1eqMM0176JMQhPqe X-Received: by 2002:a17:902:123:: with SMTP id 32-v6mr2245130plb.181.1530856136200; Thu, 05 Jul 2018 22:48:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530856136; cv=none; d=google.com; s=arc-20160816; b=foCZhaMtm6yyeXBT5fsOq+AkWWDTJzvS7N56mFRCGBdifgQhwx7RaeS044aaH6ULlc KvDcXUxpLGsWKPKboAS6TApdoFrROlyMezgZJfMwYum4wg5uQpR6sHgFwyKKYhfeD8Ld Gz2I8Y3n00UawwoGhNLRXeqX8maS0kyaLUGTYOB2yYg8XRoyI+Gu1Y4ySM8s1YkaoYO/ vqgSM/MKhdWB1hvb/yLD6u1gLEyRoWUd5uPFT+JgkCh2ppgy78z6yalpkiR3RI1W4sdR LFwWaEMRULhNeATBalH1sDOjVei4PpCRvNYvwMaEbj8gh4ZSymCVOzhQ5mQ4dkxY2D1f ty2Q== 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=FVKUaMhzbvBsX61Kzzo2oEx2QgLj561eGOgGqF5i3Nk=; b=TkU6DDaxNKZASOzYUcPg/i8IoPVY2qi+fX6aYUx2Wu/urAKrjjjDfF4c5rxa5jMOhS HMEkBREdqGnHX5A9vqAAx7ykOPIcRmlq1PQJ2bm3tO0FiYPbd3sRkk8qB/ueYLzBhnAb 4NGVe5jzla2e1aTZO7GCfoGjF2IhzCfSKUMlWOt0Oo2iXYHg8rU6S/2z2puUh15cm2YN CKezWCpzjrO/lzJy0ecB3hBdPM29uLga2fbYoHr5oBmcH7BWa++0SezzlBCE2nmlfQhE k4KoCDulzTP5dAg8bfvMAH1/6hQV5ypKRA5kjXCzU4pmaOJ4+80nIGHYfbLCaOcNmYth G0GA== 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 l5-v6si7722144pls.360.2018.07.05.22.48.41; Thu, 05 Jul 2018 22:48:56 -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 S932975AbeGFFrr (ORCPT + 99 others); Fri, 6 Jul 2018 01:47:47 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:58948 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932935AbeGFFrq (ORCPT ); Fri, 6 Jul 2018 01:47:46 -0400 Received: from localhost (D57D388D.static.ziggozakelijk.nl [213.125.56.141]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 171A7BA2; Fri, 6 Jul 2018 05:47:44 +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.17 10/46] n_tty: Fix stall at n_tty_receive_char_special(). Date: Fri, 6 Jul 2018 07:46:31 +0200 Message-Id: <20180706054525.092860984@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180706054524.595521988@linuxfoundation.org> References: <20180706054524.595521988@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.17-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 @@ -124,6 +124,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; @@ -978,14 +980,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)) @@ -1027,7 +1030,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') { @@ -1302,7 +1305,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++; } @@ -2411,7 +2414,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--;