Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2010542imm; Tue, 10 Jul 2018 11:32:46 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdBrY5iog+1Ej0/Sp9mFpXVANgrUPYghJk5g905BltGwCDwRPvd7frBKhIOlmA0Q9ALgbZz X-Received: by 2002:a63:4811:: with SMTP id v17-v6mr18810181pga.379.1531247566477; Tue, 10 Jul 2018 11:32:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531247566; cv=none; d=google.com; s=arc-20160816; b=p63fwoEKvHRC2T5DnwT/1kZ9rO/d2cTKLIf7ZUCNM6lS28PIjmH80KfotoH9Ga71hI TXSzmlJqCCjasrHDT1GjUmVm/xtQ6LUSLI21L0dfIzPZuRfXQNVH+wrvzYMxiJZfztwq w/9PeQSiIzN12yEgMf4J1LFr1LaX4Ixz5B1Zm3pcmuvW64SMScKyifzVgyn+FiEemx3t fKV8qyCq/0GDPPely6ieFXLAvvqSHM/JG+iYeAgnkhyDecK6JTfWizXgEqI6MzhAnI5n ohIFECEvUJmvWSyvl2YQErFALmOwYj0FLR18AT5V1UucFqP+EaPPjMm1i9TEZQVlRZj+ 0u5w== 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=DMuKGUhn2pZ0bLogJyvNpwzZ+ghX5pytO4oa+ETN+yU=; b=d6zjQY8U3xUAdLSmo6roKl5HiHfZzs0JLJ51X8oFS9Oj2eOTFouwDZLFOwQEaBelSf ke3cU1YN65S3fVYMs1lPxSeGmgD4K0/P5FIzC9rSn2pY163kugaMG90yKJlvuFsSoqIy wx/uJiZvTI2HAUyfz1tfSVkANbqVXoukl75gQA16QGpAUDQ71aO9AmtWRn/L+2iSlitk zy++HYiXjveQfiE/25QrgI84LqPuFl59tyTjbFm5jCMLGO3USmbgKrAQnw7EC5IAZ/0V FpqJYY+JFUUs7oVKnBTzJC9MuusIbrNbh6VdQH3Go5Xy9PzuvT2yBMPshOe0tl1tByru dIBg== 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 l63-v6si17188801plb.106.2018.07.10.11.32.31; Tue, 10 Jul 2018 11:32:46 -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 S2388140AbeGJSak (ORCPT + 99 others); Tue, 10 Jul 2018 14:30:40 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:45614 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388129AbeGJSaj (ORCPT ); Tue, 10 Jul 2018 14:30:39 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id B5321EC2; Tue, 10 Jul 2018 18:30:27 +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.9 05/52] n_tty: Fix stall at n_tty_receive_char_special(). Date: Tue, 10 Jul 2018 20:24:33 +0200 Message-Id: <20180710182449.631406449@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180710182449.285532226@linuxfoundation.org> References: <20180710182449.285532226@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.9-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--;