Received: by 10.192.165.148 with SMTP id m20csp4168259imm; Mon, 30 Apr 2018 13:04:37 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrRm7opkKhrErR/f9njW+wrin6Z9mlhDxJEF9CUtu8Hoo1D6CCPDjFplf5PgGq08Iki9xPN X-Received: by 2002:a17:902:708a:: with SMTP id z10-v6mr11583473plk.283.1525118677364; Mon, 30 Apr 2018 13:04:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525118677; cv=none; d=google.com; s=arc-20160816; b=JJsaZFXM258zyu3LGKw/EOYd7eoaWSWo0G5SQXD4Tk/aBQe+pdphutiff251zBfWXv bIvTIBWuWnrkKcr8mY8XrogqRXXgMOQVzTewjVzvhpftGjjOvMAiGWd8f5oQzEGIwLDF 3rDJuFKtiZGZEVynA7/XyLHFX4dVL3xb9ZZHqf32LX8yyQewDugJ4jLwINKRGEAr76Kw EylJWqhI5JJxGabrl2vfs0zQk2hn1retgXVwxeer/HNSrJp8/rSkW2HL3Efd9ZD/g+cX Rye9yu1wJZbpsUUTeoqCcAQkYhTXz+B6a5MX+m3lmac5hgiZ5iMdDy5AtHU5v8iLkjrA TF+Q== 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:dmarc-filter :arc-authentication-results; bh=Kw+n2UISrW/jaVgI2BwNqyohNgQM5v2p7dXBY0+FRuQ=; b=MfkmqtpmsczGVhexw2VRJvocnkHdFW0yEPfTi9Cf02wt+4tIOYyd0l4m7lQyMXBILE XlCe7GS3cjceC+Dm5UnIp1p2hh58dWHQdQt8ZgOjL/rxE3EPyA16/1B6KviLA1rpJgg3 uBZK/0EMqpYAQJyGcvggHxjnFMlMbl/Dq8e0N1tkFjGcjtY7O97AC+MAM77PlCHpU5fn 4KrJ1GaHeFemrHPnFs7DM0QUU9EMhqoFlkxNesEKRByVEHGEUrwRyAXhhJlGvTea6leg FMbL6JNH54fs0+9/8YmmyTpDy/xM5/v2OnVp1nvcFKZwOme711UCyNXSR4MkocucnCAq eZ6Q== 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 y63si8299350pfg.121.2018.04.30.13.04.23; Mon, 30 Apr 2018 13:04:37 -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 S1756220AbeD3UEK (ORCPT + 99 others); Mon, 30 Apr 2018 16:04:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:34188 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755989AbeD3T1c (ORCPT ); Mon, 30 Apr 2018 15:27:32 -0400 Received: from localhost (unknown [104.132.1.102]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2C7F722DC1; Mon, 30 Apr 2018 19:27:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2C7F722DC1 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linuxfoundation.org Authentication-Results: mail.kernel.org; spf=fail smtp.mailfrom=gregkh@linuxfoundation.org From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+40b7287c2dc987c48c81@syzkaller.appspotmail.com, Tetsuo Handa , Jiri Slaby , Dmitry Vyukov , Johannes Weiner , Alan Cox , Christoph Hellwig , Michal Hocko Subject: [PATCH 4.14 32/91] tty: Avoid possible error pointer dereference at tty_ldisc_restore(). Date: Mon, 30 Apr 2018 12:24:14 -0700 Message-Id: <20180430184005.847588872@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180430184004.216234025@linuxfoundation.org> References: <20180430184004.216234025@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 598c2d41ff44889dd8eced4f117403e472158d85 upstream. syzbot is reporting crashes [1] triggered by memory allocation failure at tty_ldisc_get() from tty_ldisc_restore(). While syzbot stops at WARN_ON() due to panic_on_warn == true, panic_on_warn == false will after all trigger an OOPS by dereferencing old->ops->num if IS_ERR(old) == true. We can simplify tty_ldisc_restore() as three calls (old->ops->num, N_TTY, N_NULL) to tty_ldisc_failto() in addition to avoiding possible error pointer dereference. If someone reports kernel panic triggered by forcing all memory allocations for tty_ldisc_restore() to fail, we can consider adding __GFP_NOFAIL for tty_ldisc_restore() case. [1] https://syzkaller.appspot.com/bug?id=6ac359c61e71d22e06db7f8f88243feb11d927e7 Reported-by: syzbot+40b7287c2dc987c48c81@syzkaller.appspotmail.com Signed-off-by: Tetsuo Handa Cc: Greg Kroah-Hartman Cc: Jiri Slaby Cc: Dmitry Vyukov Cc: Johannes Weiner Cc: Alan Cox Cc: Christoph Hellwig Cc: Michal Hocko Cc: stable Signed-off-by: Greg Kroah-Hartman --- drivers/tty/tty_ldisc.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) --- a/drivers/tty/tty_ldisc.c +++ b/drivers/tty/tty_ldisc.c @@ -526,19 +526,16 @@ static int tty_ldisc_failto(struct tty_s static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) { /* There is an outstanding reference here so this is safe */ - old = tty_ldisc_get(tty, old->ops->num); - WARN_ON(IS_ERR(old)); - tty->ldisc = old; - tty_set_termios_ldisc(tty, old->ops->num); - if (tty_ldisc_open(tty, old) < 0) { - tty_ldisc_put(old); + if (tty_ldisc_failto(tty, old->ops->num) < 0) { + const char *name = tty_name(tty); + + pr_warn("Falling back ldisc for %s.\n", name); /* The traditional behaviour is to fall back to N_TTY, we want to avoid falling back to N_NULL unless we have no choice to avoid the risk of breaking anything */ if (tty_ldisc_failto(tty, N_TTY) < 0 && tty_ldisc_failto(tty, N_NULL) < 0) - panic("Couldn't open N_NULL ldisc for %s.", - tty_name(tty)); + panic("Couldn't open N_NULL ldisc for %s.", name); } }