fbdev: Detect integer underflow at "struct fbcon_ops"->clear_margins.
authorTetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Wed, 15 Jul 2020 01:51:02 +0000 (10:51 +0900)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 21 Aug 2020 09:01:49 +0000 (11:01 +0200)
[ Upstream commit 033724d6864245a11f8e04c066002e6ad22b3fd0 ]

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 <syzbot+e5fd3e65515b48c02a30@syzkaller.appspotmail.com>
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Acked-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Cc: stable <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20200715015102.3814-1-penguin-kernel@I-love.SAKURA.ne.jp
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/video/console/bitblit.c
drivers/video/console/fbcon_ccw.c
drivers/video/console/fbcon_cw.c
drivers/video/console/fbcon_ud.c

index dbfe4eecf12e56d328478dff9a84064c591bfe85..05d1d36a56654b410048d5381e06b7a510e64ca1 100644 (file)
@@ -216,7 +216,7 @@ static void bit_clear_margins(struct vc_data *vc, struct fb_info *info,
        region.color = 0;
        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_data *vc, struct fb_info *info,
                info->fbops->fb_fillrect(info, &region);
        }
 
-       if (bh) {
+       if ((int) bh > 0) {
                region.dx = info->var.xoffset;
                region.dy = info->var.yoffset + bs;
                region.width = rs;
index 5a3cbf6dff4d944ff5a0ac3fb1fd62c2fba3106a..34da8bba9273a5370795e7f121dc581a34a7e9bd 100644 (file)
@@ -201,7 +201,7 @@ static void ccw_clear_margins(struct vc_data *vc, struct fb_info *info,
        region.color = 0;
        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_data *vc, struct fb_info *info,
                info->fbops->fb_fillrect(info, &region);
        }
 
-       if (bh) {
+       if ((int) bh > 0) {
                region.dx = info->var.xoffset + bs;
                region.dy = 0;
                 region.height = info->var.yres_virtual;
index e7ee44db4e98b1318cf8e9f679843354e6b84862..0b552b3fc22abd1659370d60b87a4d988b748c42 100644 (file)
@@ -184,7 +184,7 @@ static void cw_clear_margins(struct vc_data *vc, struct fb_info *info,
        region.color = 0;
        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_data *vc, struct fb_info *info,
                info->fbops->fb_fillrect(info, &region);
        }
 
-       if (bh) {
+       if ((int) bh > 0) {
                region.dx = info->var.xoffset;
                region.dy = info->var.yoffset;
                 region.height = info->var.yres;
index 19e3714abfe8fb765eaaea2ebeb9a21c5c2725de..7f62efe2da526dea950efeddf5ae349336c43dff 100644 (file)
@@ -231,7 +231,7 @@ static void ud_clear_margins(struct vc_data *vc, struct fb_info *info,
        region.color = 0;
        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_data *vc, struct fb_info *info,
                info->fbops->fb_fillrect(info, &region);
        }
 
-       if (bh) {
+       if ((int) bh > 0) {
                region.dy = info->var.yoffset;
                region.dx = info->var.xoffset;
                 region.height  = bh;