Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp925811ybg; Wed, 29 Jul 2020 01:01:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyMuC7C//lQ+frD8yIgYPyfHn2ZOvikpnqMtQBb4wE0Hlt7ICVfr6dJyl8JIOiLFZLUyFUZ X-Received: by 2002:a17:906:1ed3:: with SMTP id m19mr22707932ejj.396.1596009669075; Wed, 29 Jul 2020 01:01:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596009669; cv=none; d=google.com; s=arc-20160816; b=CHT5lH61KXpFtnHoSLwNARXfORwEbvX7uvW8iot+utW5H1/ZYTEmbJFHsP7SRL128t a2EY32rg+lPbqc2RS+Px4nLfPPyJXj84L+b3uouI/ZbPv9BlE71h6BHOTpWg/hjATOuX zeNRN98St5uWOV4pNGPUyICu9cxxSA5fb6xqgwNwmab1mkBzjI0kGSdlDrhR4RsJyoii GAGjy8P06HiTI9QN+PB2f1mpnDByyZBQptOeNTDqq6PMUHHvJtLqsFkh1/h4Ln2MoB5A g/ue1rQIAFf5YBqt1fBKR2anU4eYA4kC0frRxr1KAWdI91sDPqdL9LagHOz9PdBmafRl o8Yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:autocrypt:from:references :cc:to:subject; bh=K/7FOedQPg+hEYjnvRmFB6qWLf4NxIYv2PcZgTgZiYI=; b=mfXHeFwhCX+fmKLHFfnmQnQ6IfNBt2YdD2QLuA16fR626mvchBOgS4k5lC+tg8Cu2S 4T8dW1GRIdxo4pt/DTkh8w5PphFzfJSjEmJiz5xRcfd+eEFdSiyKXybPav7fuWPxfTe4 C9HYuxiyDrdqo5GNNscM+j11K+yaS6RhnITznT3tUfZk1I8dMdvuDdeKlsLlY2vPX5aY GJU5JApU4oBtKtf6uXwo6RcCMmPeYePEl0WHIy/SSn72rP0p+6geR5UBmv+tblfM/pAY gtdsOBRIPEWb4D5aSvOjZFlsOdCWxoJj631A4TNChRPg+99JaGuoSXUugT3Q9RRfrwIT SRmA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j9si551359ejx.438.2020.07.29.01.00.46; Wed, 29 Jul 2020 01:01:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727907AbgG2H7t (ORCPT + 99 others); Wed, 29 Jul 2020 03:59:49 -0400 Received: from nsfocus.com ([221.122.62.131]:58050 "HELO nsfocus.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with SMTP id S1727082AbgG2H7s (ORCPT ); Wed, 29 Jul 2020 03:59:48 -0400 Received: (qmail 3331 invoked from network); 29 Jul 2020 07:51:42 -0000 Received: from unknown (HELO ?192.168.7.10?) (221.122.62.131) by nsfocus.com with SMTP; 29 Jul 2020 07:51:42 -0000 Subject: Re: [PATCH] vgacon: fix out of bounds write to the scrollback buffer To: Jiri Slaby , b.zolnierkie@samsung.com Cc: linux-kernel@vger.kernel.org, Yang Yingliang , Kyungtae Kim , Linus Torvalds , Greg KH , Solar Designer , "Srivatsa S. Bhat" , Anthony Liguori , Security Officers , linux-distros@vs.openwall.org, dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org References: <20200729070249.20892-1-jslaby@suse.cz> From: =?UTF-8?B?5byg5LqR5rW3?= Autocrypt: addr=zhangyunhai@nsfocus.com; keydata= xsFN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PC3LXwn6OXYoiiojjO+z+aJ0AfdE1 s0iDw1oQhKCQsH6ReiLd3R1cmOovotyQREXDml136OPwEnWiL2sNH6dE/wARAQABwsFfBBgB CAAJBQJV3/m0AhsMAAoJEP4mMEaS5e9PzywP/jdR9cn0s2PNa0fQEPo7Ai6v6qy2dHp0lopa 8k/KoIpZEhgnFgy3aVL+vL+9AuaZfSdm3gwW4t4V5GbR5HilQ6Nfp0sJVpE8F/JOF1P9SLSy fIsna0tcqE79/isyF+ockZwVK5rgwJHqEIzr50TOKob2yY4AF0ZFQUSrpU/AmE9OK1EH5d88 gIki0kOYQwteL8hLTjkRlecjiBSljA9V4VZVwpXyCHUDO3sCxJQYMaiSTjGEztswoAyUy0Q+ xnpzelyw670W/y9DAgafdaN5MJldyAapUOv8yIRSlQ5M2f3ZFyjJOAozNXfqXiuHkKoXgsbW Sfh/o9HfPE5y8NCPJY1IoHRr1pUklwVNIwM77xpQxBFhBPNUbL43igdqRf6hApk4aJ/jT7pF wPKclsAKfZTkqYOksT1Qh0FURhr8S6xUe3aV9omGXIOLGMIbpHuZSbP0akdHA0nzUY6HYbN1 0T3X0bi33lOUefj2uAnhuPeReyAP02CjvkNJVfBRho3h/D56ofuPdvfAetT46d6y+tQVdoka 5tO7oLXD/f5GPuDoYSjfOiIlU4d/tIDUdyXXfml0Ez8DZk0c+3z61TNXRDV1tzXKmC1oV+6m Ql46hjmfjnRfvq72kL55kj+YzWjlM9h98+4vqknUPPYIq+lUz4hO7I3b64i5sPkBWtN7DLkm Message-ID: <55075898-bf95-1805-3358-b0d1438feaa9@nsfocus.com> Date: Wed, 29 Jul 2020 15:53:02 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20200729070249.20892-1-jslaby@suse.cz> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi All, This patch dosen't fix the issue, the check should be in the loop. The change of the VT sze is before vgacon_scrollback_update, not in the meantime. Let's consider the following situation: suppose: vgacon_scrollback_cur->size is 65440 vgacon_scrollback_cur->tail is 64960 c->vc_size_row is 160 count is 5 Reset c->vc_size_row to 200 by VT_RESIZE, then call vgacon_scrollback_update. This will pass the check, since (vgacon_scrollback_cur->tail + c->vc_size_row) is 65160 which is less then vgacon_scrollback_cur->size(65440). However, in the 3rd iteration of the loop, vgacon_scrollback_cur->tail is update to 65360, the memcpy will overflow. To avoid overflow, the check should be if ((vgacon_scrollback_cur->tail + c->vc_size_row * count) >= However, this will break the circular of the buffer, since all 5 lines will be copy at the beginning. To avoid break circular, we have to detect if wrap occurs, use a loop to copy lines before wrap, reset vgacon_scrollback_cur->tail to 0, then use another loop to copy lines after wrap. Of course the 2 loop can be combine into 2 memcpy, that will be similar to Linus's patch. Thus, I think the check should be in the loop. The 2 check in the loop seems to be redundancy, Zhang Xiao from Windriver suggest that the check after the memcpy can be remove. I think he was right, but not very sure. Thus, I suggest we discuss that too. Regards, Yunhai Zhang / NSFOCUS Security Team On 2020/7/29 15:02, Jiri Slaby wrote: > The current vgacon's scroll up implementation uses a circural buffer > in vgacon_scrollback_cur. It always advances tail to prepare it for the > next write and caps it to zero if the next ->vc_size_row bytes won't fit. > > But when we change the VT size (e.g. by VT_RESIZE) in the meantime, the new > line might not fit to the end of the scrollback buffer in the next > attempt to scroll. This leads to various crashes as > vgacon_scrollback_update writes out of the buffer: > BUG: unable to handle page fault for address: ffffc900001752a0 > #PF: supervisor write access in kernel mode > #PF: error_code(0x0002) - not-present page > RIP: 0010:mutex_unlock+0x13/0x30 > ... > Call Trace: > n_tty_write+0x1a0/0x4d0 > tty_write+0x1a0/0x2e0 > > Or to KASAN reports: > BUG: KASAN: slab-out-of-bounds in vgacon_scroll+0x57a/0x8ed > > So check whether the line fits in the buffer and wrap if needed. Do it > before the loop as console_sem is held and ->vc_size_row cannot change > during the execution of vgacon_scrollback_cur. If it does change, we > need to ensure it does not change elsewhere, not here. > > Also, we do not split the write of a line into chunks as that would > break the consumers of the buffer. They expect ->cnt, ->tail and ->size > to be in harmony and advanced by ->vc_size_row. > > I found few reports of this in the past, some with patches included, > some even 2 years old: > https://lore.kernel.org/lkml/CAEAjamsJnG-=TSOwgRbbb3B9Z-PA63oWmNPoKYWQ=Z=+X49akg@mail.gmail.com/ > https://lore.kernel.org/lkml/1589336932-35508-1-git-send-email-yangyingliang@huawei.com/ > > This fixes CVE-2020-14331. > > Big thanks to guys mentioned in the Reported-and-debugged-by lines below > who actually found the root cause. > > Signed-off-by: Jiri Slaby > Reported-and-debugged-by: 张云海 > Reported-and-debugged-by: Yang Yingliang > Reported-by: Kyungtae Kim > Fixes: 15bdab959c9b ([PATCH] vgacon: Add support for soft scrollback) > Cc: Linus Torvalds > Cc: Greg KH > Cc: Solar Designer > Cc: "Srivatsa S. Bhat" > Cc: Anthony Liguori > Cc: Security Officers > Cc: linux-distros@vs.openwall.org > Cc: Yang Yingliang > Cc: Bartlomiej Zolnierkiewicz > Cc: dri-devel@lists.freedesktop.org > Cc: linux-fbdev@vger.kernel.org > --- > drivers/video/console/vgacon.c | 5 +++++ > 1 file changed, 5 insertions(+) > > diff --git a/drivers/video/console/vgacon.c b/drivers/video/console/vgacon.c > index f0f3d573f848..13194bb246f8 100644 > --- a/drivers/video/console/vgacon.c > +++ b/drivers/video/console/vgacon.c > @@ -250,6 +250,11 @@ static void vgacon_scrollback_update(struct vc_data *c, int t, int count) > > p = (void *) (c->vc_origin + t * c->vc_size_row); > > + /* vc_size_row might have changed by VT_RESIZE in the meantime */ > + if ((vgacon_scrollback_cur->tail + c->vc_size_row) >= > + vgacon_scrollback_cur->size) > + vgacon_scrollback_cur->tail = 0; > + > while (count--) { > scr_memcpyw(vgacon_scrollback_cur->data + > vgacon_scrollback_cur->tail, > -- 张云海 绿盟科技 安全研究部 研究员 地址:北京市海淀区北洼路4号益泰大厦三层 邮编:100089 电话:(010)68438880-8510 传真:(010)68437328 手机:13691192782 邮箱:zhangyunhai@nsfocus.com 网站:http://www.nsfocus.com