Blob Blame History Raw
From: Jiri Slaby <jslaby@suse.cz>
Date: Tue, 28 Jul 2020 10:47:06 +0200
Subject: vgacon: fix out of bounds write to the scrollback buffer
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Patch-mainline: submitted 2020/07/28
References: bsc#1174205 CVE-2020-14331

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 c->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. 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

So check whether the line fits in the buffer and wrap if needed.

Fix CVE-2020-14331.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Reported-and-debugged-by: 张云海 <zhangyunhai@nsfocus.com>
Fixes: 15bdab959c9b ([PATCH] vgacon: Add support for soft scrollback)
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Greg KH <greg@kroah.com>
Cc: Security Officers <security@kernel.org>
Cc: linux-distros@vs.openwall.org
---
 drivers/video/console/vgacon.c |    5 +++++
 1 file changed, 5 insertions(+)

--- a/drivers/video/console/vgacon.c
+++ b/drivers/video/console/vgacon.c
@@ -245,6 +245,11 @@ static void vgacon_scrollback_update(str
 
 	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,