Received: by 2002:a17:90b:8d0:0:0:0:0 with SMTP id ds16csp4881395pjb; Mon, 27 Jul 2020 07:30:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyyZXZ4Qk2zwd+/NuXeze/RxdzfY3D0SSUb3Q8O+GyRl293w+bwswdwlOS+XDf0o/JSnuPD X-Received: by 2002:a17:906:3984:: with SMTP id h4mr22537075eje.254.1595860210766; Mon, 27 Jul 2020 07:30:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595860210; cv=none; d=google.com; s=arc-20160816; b=ZkOezFzJDXbNJD9dnjCK7Gf45kFCxtZTGeYCDQtd+WqUCHmiJfgGe66yXNAEy8jows NjDpgFV1fJNrEFGNC4hRS5dCNUljVNa7bdsDqu30qJp1+BnBd5DB7IMMGGehiVRr6g+p um1Mnd8CAlqz7YpyGPngZ2XXXplBrLV9go7nyerNaY7cS1l8/JQ5HEAWgWDZFXkrZo6D Tb/lMsCZ1LjygOm6+VUIrBCp32Vj2F31PFN3AzTnPAZ4yZDyMp0BXplug2tZT4bwPcUU RQ6n8o5juJfEMFb8OWeqyEU+nLnV/epK0AtDTjNik7aGNO+k2zjppJggKAYc5zeZN40a AuBg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=3lzYqLEyiE2aozDvX8vYwSvSaO4sGPqtmcqDHeeX3Wc=; b=02WcHZQNDlIQJD5DcYkm7w7Ggw8Fo2HeAWkpn4rPszPl2JuvZJyIBJtNUJwhud/U40 zBsPclSV2BHIb2+gnAkQ91ATn1kFpjXQRbcJe45LK7p48CW1uAFL25bWSXlNwGWEwfCF fvGn15qEX25B3p7D+E3NL7Q6qYmQXaZdqtXh7KYY5Yrj8BvdUweiihtVmVgHb/tdJs88 /TvopQko7QHDg2Oxzwunmf0dC/I0DK6Q7fU23VeIj0sNP0b618YLYL4ZyxLOLAEFI9Aa q++ZhCO9pcd3rorjewiI/CL2+SmjZJAJM245+J8L7D28fgPBi7ansftEvoyC5Op8J2hS VVTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=XOinYIGH; 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 le18si1578572ejb.705.2020.07.27.07.29.48; Mon, 27 Jul 2020 07:30:10 -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; dkim=pass header.i=@kernel.org header.s=default header.b=XOinYIGH; 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 S1732625AbgG0O0d (ORCPT + 99 others); Mon, 27 Jul 2020 10:26:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:56754 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731829AbgG0O0b (ORCPT ); Mon, 27 Jul 2020 10:26:31 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 EBA1F20663; Mon, 27 Jul 2020 14:26:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1595859990; bh=/YydLRvkoKVz03blHEasLvIRqIOV/wARQ1buAMOoUk4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XOinYIGH0TxejZFEArTRZc5z1Csk2ekPT3qgsI9JRtGISvMWJdXgeYiU0gm8cmsLP eDTP+3HSn2e67GwQOOMT2em/Nfd/6zVPai1evt4NwkkMFTIfkgPPMxQ2qZF+m5txlA jvILjc/1sebIDSMtE1m9mmZkbzKU/bwRZDnKDHCk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tetsuo Handa , Daniel Vetter , syzbot Subject: [PATCH 5.7 156/179] fbdev: Detect integer underflow at "struct fbcon_ops"->clear_margins. Date: Mon, 27 Jul 2020 16:05:31 +0200 Message-Id: <20200727134940.273282559@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200727134932.659499757@linuxfoundation.org> References: <20200727134932.659499757@linuxfoundation.org> User-Agent: quilt/0.66 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 From: Tetsuo Handa commit 033724d6864245a11f8e04c066002e6ad22b3fd0 upstream. syzbot is reporting general protection fault in bitfill_aligned() [1] caused by integer underflow in bit_clear_margins(). The cause of this problem is when and how do_vc_resize() updates vc->vc_{cols,rows}. If vc_do_resize() fails (e.g. kzalloc() fails) when var.xres or var.yres is going to shrink, vc->vc_{cols,rows} will not be updated. This allows bit_clear_margins() to see info->var.xres < (vc->vc_cols * cw) or info->var.yres < (vc->vc_rows * ch). Unexpectedly large rw or bh will try to overrun the __iomem region and causes general protection fault. Also, vc_resize(vc, 0, 0) does not set vc->vc_{cols,rows} = 0 due to new_cols = (cols ? cols : vc->vc_cols); new_rows = (lines ? lines : vc->vc_rows); exception. Since cols and lines are calculated as cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres); rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres); cols /= vc->vc_font.width; rows /= vc->vc_font.height; vc_resize(vc, cols, rows); in fbcon_modechanged(), var.xres < vc->vc_font.width makes cols = 0 and var.yres < vc->vc_font.height makes rows = 0. This means that const int fd = open("/dev/fb0", O_ACCMODE); struct fb_var_screeninfo var = { }; ioctl(fd, FBIOGET_VSCREENINFO, &var); var.xres = var.yres = 1; ioctl(fd, FBIOPUT_VSCREENINFO, &var); easily reproduces integer underflow bug explained above. Of course, callers of vc_resize() are not handling vc_do_resize() failure is bad. But we can't avoid vc_resize(vc, 0, 0) which returns 0. Therefore, as a band-aid workaround, this patch checks integer underflow in "struct fbcon_ops"->clear_margins call, assuming that vc->vc_cols * vc->vc_font.width and vc->vc_rows * vc->vc_font.heigh do not cause integer overflow. [1] https://syzkaller.appspot.com/bug?id=a565882df74fa76f10d3a6fec4be31098dbb37c6 Reported-and-tested-by: syzbot Signed-off-by: Tetsuo Handa Acked-by: Daniel Vetter Cc: stable Link: https://lore.kernel.org/r/20200715015102.3814-1-penguin-kernel@I-love.SAKURA.ne.jp Signed-off-by: Greg Kroah-Hartman --- drivers/video/fbdev/core/bitblit.c | 4 ++-- drivers/video/fbdev/core/fbcon_ccw.c | 4 ++-- drivers/video/fbdev/core/fbcon_cw.c | 4 ++-- drivers/video/fbdev/core/fbcon_ud.c | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) --- a/drivers/video/fbdev/core/bitblit.c +++ b/drivers/video/fbdev/core/bitblit.c @@ -216,7 +216,7 @@ static void bit_clear_margins(struct vc_ region.color = color; region.rop = ROP_COPY; - if (rw && !bottom_only) { + if ((int) rw > 0 && !bottom_only) { region.dx = info->var.xoffset + rs; region.dy = 0; region.width = rw; @@ -224,7 +224,7 @@ static void bit_clear_margins(struct vc_ info->fbops->fb_fillrect(info, ®ion); } - if (bh) { + if ((int) bh > 0) { region.dx = info->var.xoffset; region.dy = info->var.yoffset + bs; region.width = rs; --- a/drivers/video/fbdev/core/fbcon_ccw.c +++ b/drivers/video/fbdev/core/fbcon_ccw.c @@ -201,7 +201,7 @@ static void ccw_clear_margins(struct vc_ region.color = color; region.rop = ROP_COPY; - if (rw && !bottom_only) { + if ((int) rw > 0 && !bottom_only) { region.dx = 0; region.dy = info->var.yoffset; region.height = rw; @@ -209,7 +209,7 @@ static void ccw_clear_margins(struct vc_ info->fbops->fb_fillrect(info, ®ion); } - if (bh) { + if ((int) bh > 0) { region.dx = info->var.xoffset + bs; region.dy = 0; region.height = info->var.yres_virtual; --- a/drivers/video/fbdev/core/fbcon_cw.c +++ b/drivers/video/fbdev/core/fbcon_cw.c @@ -184,7 +184,7 @@ static void cw_clear_margins(struct vc_d region.color = color; region.rop = ROP_COPY; - if (rw && !bottom_only) { + if ((int) rw > 0 && !bottom_only) { region.dx = 0; region.dy = info->var.yoffset + rs; region.height = rw; @@ -192,7 +192,7 @@ static void cw_clear_margins(struct vc_d info->fbops->fb_fillrect(info, ®ion); } - if (bh) { + if ((int) bh > 0) { region.dx = info->var.xoffset; region.dy = info->var.yoffset; region.height = info->var.yres; --- a/drivers/video/fbdev/core/fbcon_ud.c +++ b/drivers/video/fbdev/core/fbcon_ud.c @@ -231,7 +231,7 @@ static void ud_clear_margins(struct vc_d region.color = color; region.rop = ROP_COPY; - if (rw && !bottom_only) { + if ((int) rw > 0 && !bottom_only) { region.dy = 0; region.dx = info->var.xoffset; region.width = rw; @@ -239,7 +239,7 @@ static void ud_clear_margins(struct vc_d info->fbops->fb_fillrect(info, ®ion); } - if (bh) { + if ((int) bh > 0) { region.dy = info->var.yoffset; region.dx = info->var.xoffset; region.height = bh;