Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp898007ybg; Wed, 29 Jul 2020 00:04:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyOub0BOCjZPSmsXscGZAlV0s87z9Tdobztxd+LeFwd+7LB8625JDmFu4vuCocqWpUe6XE7 X-Received: by 2002:a05:6402:3ca:: with SMTP id t10mr14772997edw.298.1596006246948; Wed, 29 Jul 2020 00:04:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596006246; cv=none; d=google.com; s=arc-20160816; b=Mo0GTbEPw5p0+58nBI7KsbW8oMv05ChPOc9fqLVbNhBSbJtPP2FfFP4/cY5/UkztDd r9dqhyPMJPyDqDzCajbUIHAqr/C3816D5LhPUkPe2knm7ObMB+Gz6oydovdVD8jFc11R bURqOPReU4/I7NH+QuQS0qzMEjal1ov7wZro8B1UNjeEKpI31S0gNQs1LIilTzDspxOr wbF2OhMElwiSCXHMGC3pZ7CrWkX8hipBhfHzf8hNlVk+HXv8ca9FCdZvpsIFBt6T18F3 Rvup8ZNh4Y+9kb0TIyYnQsAcxa+WdOF8LSCkOvoRDoSay4WtgzVZWYXrgeqFgMJ0dCjQ G71w== 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:mime-version :message-id:date:subject:cc:to:from; bh=PRpBMNx3IqXDmKWos2S0t69husKBDOLFXIHFQ11lLDU=; b=LRsrnPBVlMUnTmBHwgLGWkjYcSrbpi/DeQ4A/6C7WeZxJAgOi9jmnj5DVmm+BJ1s0l SHKQU0tM97Z3iZydJ3iDBp7Zx2B8OmF/IDA3B3+btnZ/YbFNMFQoS3tVVV4kWlVtLaxP 4uFa/MTzs1N2yeNQhbIkoSutkbRa1Gjxf1/5iyWr9BO7ph4QLKK/MktpaR49vMDGgpMz QlDtXJPWw1NH5N9bp2UJDX7zDp84YzE6+7O4MdJxdLm7wU16geq1OTvQ1EgD5isgeIOI bLJcwhAty7kBzI72cYSyt3z3dVNzBsM6jXuVYHBc4Txt/vlLIdnUc4cSZPPQoegjpLJp YvgQ== 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 ce7si721332edb.85.2020.07.29.00.03.44; Wed, 29 Jul 2020 00:04:06 -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 S1727804AbgG2HCy (ORCPT + 99 others); Wed, 29 Jul 2020 03:02:54 -0400 Received: from mx2.suse.de ([195.135.220.15]:37728 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726314AbgG2HCx (ORCPT ); Wed, 29 Jul 2020 03:02:53 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id E8C71AB55; Wed, 29 Jul 2020 07:03:02 +0000 (UTC) From: Jiri Slaby To: b.zolnierkie@samsung.com Cc: linux-kernel@vger.kernel.org, Jiri Slaby , =?UTF-8?q?=E5=BC=A0=E4=BA=91=E6=B5=B7?= , 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 Subject: [PATCH] vgacon: fix out of bounds write to the scrollback buffer Date: Wed, 29 Jul 2020 09:02:49 +0200 Message-Id: <20200729070249.20892-1-jslaby@suse.cz> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 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 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, -- 2.28.0