From nobody Sat May 11 15:59:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1561967641; cv=none; d=zoho.com; s=zohoarc; b=AjYPC5gxhwL+07TZ79TiAE2R/l/MCitD5ecQr9YCDe//jihuHaqXy3BQfAe5Hy7nxtLdH7yFpJKqXTsoZPJ21F802zoxuKXgKhYmpNyZ09Vlp1ftZoiQkuUnOn1lf/SSNo3RSLt5quWkiHMiDvcZW+et1UBvX2Kf8Z42RQj/hXg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1561967641; h=Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:To:ARC-Authentication-Results; bh=JfogGNmPkDpDzd7ps34MjZb+f5YVVkAPAxo7EZZNYg4=; b=S8Xlhl/Ayto5IiPVTUzKeXPfCeyQiEzmb0ez3zcHvlfYOhYj1MvQ8YkEeZ+ukptoJgA5YAc72fgRmZxQj01YF4DyDedqDBwS1wQbMgGQOSFcGiCbn30ZSZRMBxvgJkV2K7JFTl7KaZQfl5xkWcl2zOF9lI1Z8O12IhNe/BL5mZg= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1561967641674227.52829189675072; Mon, 1 Jul 2019 00:54:01 -0700 (PDT) Received: from localhost ([::1]:48414 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hhr8K-0000Zl-5A for importer@patchew.org; Mon, 01 Jul 2019 03:53:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57608) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hhr7f-00005P-NH for qemu-devel@nongnu.org; Mon, 01 Jul 2019 03:53:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hhr7e-0002H1-J0 for qemu-devel@nongnu.org; Mon, 01 Jul 2019 03:53:11 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36258) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hhr7e-0002GW-BG for qemu-devel@nongnu.org; Mon, 01 Jul 2019 03:53:10 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7198B307D915; Mon, 1 Jul 2019 07:53:09 +0000 (UTC) Received: from sirius.home.kraxel.org (ovpn-116-96.ams2.redhat.com [10.36.116.96]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8A9C04DA2E; Mon, 1 Jul 2019 07:53:02 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id C379711AA3; Mon, 1 Jul 2019 09:53:01 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Mon, 1 Jul 2019 09:53:01 +0200 Message-Id: <20190701075301.14165-1-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Mon, 01 Jul 2019 07:53:09 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2] console: fix cell overflow X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dinechin@redhat.com, alxndr@bu.edu, Gerd Hoffmann , P J P Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Linux terminal behavior (coming from vt100 I think) is somewhat strange when it comes to line wraps: When a character is printed to the last char cell of a line the cursor does NOT jump to the next line but stays where it is. The line feed happens when the next character is printed. So the valid range for the cursor position is not 0 .. width-1 but 0 .. width, where x =3D=3D width represents the state where the line is full but the cursor didn't jump to the next line yet. The code for the 'clear from start of line' control sequence (ESC[1K) fails to handle this corner case correctly and may call console_clear_xy() with x =3D=3D width. That will incorrectly clear the first char cell of the next line, or in case the cursor happens to be on the last line overflow the cell buffer by one character (three bytes). Add a check to the loop to fix that. Didn't spot any other places with the same problem. But it's easy to miss that corner case, so also allocate one extra cell as precaution, so in case we have simliar issues lurking elsewhere it at least wouldn't be a buffer overflow. v2: squashed in additional checks suggested by Christophe de Dinechin. Reported-by: Alexander Oleinik Signed-off-by: Gerd Hoffmann Reviewed-by: Christophe de Dinechin --- ui/console.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ui/console.c b/ui/console.c index eb7e7e0c517a..82d1ddac9cfd 100644 --- a/ui/console.c +++ b/ui/console.c @@ -484,7 +484,7 @@ static void text_console_resize(QemuConsole *s) if (s->width < w1) w1 =3D s->width; =20 - cells =3D g_new(TextCell, s->width * s->total_height); + cells =3D g_new(TextCell, s->width * s->total_height + 1); for(y =3D 0; y < s->total_height; y++) { c =3D &cells[y * s->width]; if (w1 > 0) { @@ -541,6 +541,9 @@ static void update_xy(QemuConsole *s, int x, int y) y2 +=3D s->total_height; } if (y2 < s->height) { + if (x >=3D s->width) { + x =3D s->width - 1; + } c =3D &s->cells[y1 * s->width + x]; vga_putcharxy(s, x, y2, c->ch, &(c->t_attrib)); @@ -787,6 +790,9 @@ static void console_handle_escape(QemuConsole *s) static void console_clear_xy(QemuConsole *s, int x, int y) { int y1 =3D (s->y_base + y) % s->total_height; + if (x >=3D s->width) { + x =3D s->width - 1; + } TextCell *c =3D &s->cells[y1 * s->width + x]; c->ch =3D ' '; c->t_attrib =3D s->t_attrib_default; @@ -992,7 +998,7 @@ static void console_putchar(QemuConsole *s, int ch) break; case 1: /* clear from beginning of line */ - for (x =3D 0; x <=3D s->x; x++) { + for (x =3D 0; x <=3D s->x && x < s->width; x++) { console_clear_xy(s, x, s->y); } break; --=20 2.18.1