Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2006101imm; Tue, 10 Jul 2018 11:27:58 -0700 (PDT) X-Google-Smtp-Source: AAOMgpe3i0bjPCnA9L0BZsyNmT/7G37rPNRdtwEEpRmxWdtEfN3piIhcszHS5s5+qBmLSfyxW7Cs X-Received: by 2002:a17:902:8f94:: with SMTP id z20-v6mr25481989plo.337.1531247278515; Tue, 10 Jul 2018 11:27:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531247278; cv=none; d=google.com; s=arc-20160816; b=urjLncFSIzzYSGjU2WbBuCwR1tnsm+95rWexWf5H46O2q4EqnYEHdXshOENxy17Ld/ A6/9gtcCmSrKXProfhlwIIJLJEq+WjA05k7IOcMOIYMUrxP6OJAfboTUU2pdpntQOqZB UT7pca8oYw9xLWQZIbU0xkUTBgAWOB9EEZMkYqW92LQDB2pml5y7hPRgvki/40nVRTSm VSdLNUJo+Ohw5DUIb2zS60obr8ZQzXQRbKp3E7AXBNhGEN1YzAg+xVlwInH0eOoyMWx8 yL1G59OL54waIFcbn6cZH0G2I68cO5Ex7bH6s0aKfFnN9Sjdr/ScNVQo2hLhXaalto5f kyLQ== 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=cfnAnTheoMMG5S05h1pz3kf2sz3O6Tt1bfRyH2g/MmE=; b=SjtnFtdoXiqD98jkcUnlyQDBWHFVyaI0vXk7JHloxMd68Y6TmKge5P34oakwLkVAND mHkWqSBmKs0OL8BxeLAxfWz84w/Ov8h9LVWW1MWF7CDgfDjb7E98JhhGsyNGBZmig98/ zapavDCzwhcwx/zbNkmf1pCF2M7qOuGBCJWls7fuH7mym6eugEo1CfTe4Ecd9N0OirtW kbyId0N3HRqx9wEDReDUZWJ8x3QQBvF3MwmwCWD6nW31jHqMcLvUCDh1lLPEPE0focll 6gwNXg3VG5CvBK/o/8QQ8okodfQRnwDsWCxeNyMeN/eIq3G4fJXQL9WxQhKKz8Etx0Wq C6Mw== 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 j129-v6si17961376pfc.191.2018.07.10.11.27.43; Tue, 10 Jul 2018 11:27:58 -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 S1732586AbeGJSZ4 (ORCPT + 99 others); Tue, 10 Jul 2018 14:25:56 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:43586 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732245AbeGJSZ4 (ORCPT ); Tue, 10 Jul 2018 14:25:56 -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 D2ABED71; Tue, 10 Jul 2018 18:25:45 +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 3.18 01/23] n_tty: Fix stall at n_tty_receive_char_special(). Date: Tue, 10 Jul 2018 20:24:34 +0200 Message-Id: <20180710182308.933799891@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180710182308.877332304@linuxfoundation.org> References: <20180710182308.877332304@linuxfoundation.org> User-Agent: quilt/0.65 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 3.18-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 @@ -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++; } @@ -2505,7 +2508,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--;