Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp592439ybt; Fri, 10 Jul 2020 07:38:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxMwUL1IV1fHTpJez73RPFcUilvqEzdRHKzZdRe1rHx4KREWQa9H2ruMB6sQX4e7FaUIX8Q X-Received: by 2002:a05:6402:796:: with SMTP id d22mr81713359edy.78.1594391891747; Fri, 10 Jul 2020 07:38:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594391891; cv=none; d=google.com; s=arc-20160816; b=eNpzh/AgYPZnEqgx9V+nBffLrwgyIFY1Zqf0W+PsgeKTEbzfLfZtiDOV4+0vST4Hv2 C9rUPbwqoLhNV31MvTzUIYTODsxVopVKS8waivKC+ibNcS79KfA27pZ6Txgx8fKKxFYh N4vMc1WcS0eUgdflD3aR6NsFXfOHLWLDAXyCkbfst+ycuVsiMOp/aOY8bUZMwkXsdG5V A+tkkKBix0o39ecu9WjaZ5tzveEeN2XoGPtyOW3vwdnPLzZHx7Wdxm67EN6K3QR+Q5xo /Xx7PkBzNlRVCDemYUc0XpaE9cFz37E/CWLPeP/W6Jyk8kskSaRkQ8IuGBk5O9Bbaqe0 dp3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=cJS1GnD9yhbkPHmB1aEZvZFBmTXQnsNWDrzWPvie0d8=; b=QI5yK8cLE/uGmHRPHFMOdnvcGDrjJ4LOzhNllToSEfzVB7MwxthHfMtlPOrHqVBLc2 /qrf5y0d8DPbk+xNmjZ2qlOwRHQuNmXSejPF2Rzu23Jed3wNyXX227YSPVlh2DgIiHO+ h5Cf8NxBkGtbRXVxcZoyFdxeX9i1ZdxYpkcFfwPMzrOJ7/dbSnb5Ypjxh8MF9tTWhJ69 DNzN+V0JPsFMUfYJxul0zakdBsaph8MqpDVGq4aNmXBAKBhriEBP8QyN1UxdufwPRaue scHvDwM6Cb9+xEoYICdymIxqpuRtJ+dH6F0lu3LccvHZ0XLYD+K72L7kXeHVjsqETEk8 nslA== 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 ce16si4012835ejb.436.2020.07.10.07.37.47; Fri, 10 Jul 2020 07:38:11 -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 S1728133AbgGJOhC (ORCPT + 99 others); Fri, 10 Jul 2020 10:37:02 -0400 Received: from www262.sakura.ne.jp ([202.181.97.72]:59992 "EHLO www262.sakura.ne.jp" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726908AbgGJOhC (ORCPT ); Fri, 10 Jul 2020 10:37:02 -0400 Received: from fsav305.sakura.ne.jp (fsav305.sakura.ne.jp [153.120.85.136]) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTP id 06AEaVwI076432; Fri, 10 Jul 2020 23:36:31 +0900 (JST) (envelope-from penguin-kernel@I-love.SAKURA.ne.jp) Received: from www262.sakura.ne.jp (202.181.97.72) by fsav305.sakura.ne.jp (F-Secure/fsigk_smtp/550/fsav305.sakura.ne.jp); Fri, 10 Jul 2020 23:36:31 +0900 (JST) X-Virus-Status: clean(F-Secure/fsigk_smtp/550/fsav305.sakura.ne.jp) Received: from localhost.localdomain (M106072142033.v4.enabler.ne.jp [106.72.142.33]) (authenticated bits=0) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTPSA id 06AEaLY2076229 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Fri, 10 Jul 2020 23:36:30 +0900 (JST) (envelope-from penguin-kernel@I-love.SAKURA.ne.jp) From: Tetsuo Handa To: Greg Kroah-Hartman , Jiri Slaby Cc: Dmitry Vyukov , linux-kernel@vger.kernel.org, Tetsuo Handa , syzbot Subject: [PATCH v2] vt: Reject zero-sized screen buffer size. Date: Fri, 10 Jul 2020 23:34:55 +0900 Message-Id: <20200710143455.3438-1-penguin-kernel@I-love.SAKURA.ne.jp> X-Mailer: git-send-email 2.18.4 In-Reply-To: <20200710113658.GA1238355@kroah.com> References: <20200710113658.GA1238355@kroah.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org syzbot is reporting general protection fault in do_con_write() [1] caused by vc->vc_screenbuf == ZERO_SIZE_PTR caused by vc->vc_screenbuf_size == 0 caused by vc->vc_cols == vc->vc_rows == vc->vc_size_row == 0 being passed to ioctl(FBIOPUT_VSCREENINFO) request on /dev/fb0 , for gotoxy(vc, 0, 0) from reset_terminal() from vc_init() from vc_allocate() on such console causes vc->vc_pos == 0x10000000e due to ((unsigned long) ZERO_SIZE_PTR) + -1U * 0 + (-1U << 1). I don't think that a console with 0 column and/or 0 row makes sense, and I think that we can reject such bogus arguments in fb_set_var() from ioctl(FBIOPUT_VSCREENINFO). Regardless, I think that it is safer to also check vc->vc_screenbuf_size when allocating vc->vc_screenbuf from vc_allocate() from con_install() from tty_init_dev() from tty_open(). Theoretically, cols and rows can be any range as long as 0 < cols * rows * 2 <= KMALLOC_MAX_SIZE is satisfied (e.g. cols == 1048576 && rows == 2 is possible) because of vc->vc_size_row = vc->vc_cols << 1; vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row; in visual_init() and kzalloc(vc->vc_screenbuf_size) in vc_allocate(). But since vc_do_resize() requires cols <= 32767 and rows <= 32767, applying 1 <= cols <= 32767 and 1 <= rows <= 32767 requirements to vc_allocate() will be practically fine. (cols != 0 && rows != 0 is implicitly checked via screenbuf_size != 0.) This patch does not touch con_init(), for returning -EINVAL there does not help when we are not returning -ENOMEM. [1] https://syzkaller.appspot.com/bug?extid=017265e8553724e514e8 Reported-by: syzbot Signed-off-by: Tetsuo Handa --- drivers/tty/vt/vt.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c index 48a8199f7845..42d8c67a481f 100644 --- a/drivers/tty/vt/vt.c +++ b/drivers/tty/vt/vt.c @@ -1092,10 +1092,19 @@ static const struct tty_port_operations vc_port_ops = { .destruct = vc_port_destruct, }; +/* + * Change # of rows and columns (0 means unchanged/the size of fg_console) + * [this is to be used together with some user program + * like resize that changes the hardware videomode] + */ +#define VC_MAXCOL (32767) +#define VC_MAXROW (32767) + int vc_allocate(unsigned int currcons) /* return 0 on success */ { struct vt_notifier_param param; struct vc_data *vc; + int err; WARN_CONSOLE_UNLOCKED(); @@ -1125,6 +1134,11 @@ int vc_allocate(unsigned int currcons) /* return 0 on success */ if (!*vc->vc_uni_pagedir_loc) con_set_default_unimap(vc); + err = -EINVAL; + if (vc->vc_cols > VC_MAXCOL || vc->vc_rows > VC_MAXROW || + vc->vc_screenbuf_size > KMALLOC_MAX_SIZE || !vc->vc_screenbuf_size) + goto err_free; + err = -ENOMEM; vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_KERNEL); if (!vc->vc_screenbuf) goto err_free; @@ -1143,7 +1157,7 @@ int vc_allocate(unsigned int currcons) /* return 0 on success */ visual_deinit(vc); kfree(vc); vc_cons[currcons].d = NULL; - return -ENOMEM; + return err; } static inline int resize_screen(struct vc_data *vc, int width, int height, @@ -1158,14 +1172,6 @@ static inline int resize_screen(struct vc_data *vc, int width, int height, return err; } -/* - * Change # of rows and columns (0 means unchanged/the size of fg_console) - * [this is to be used together with some user program - * like resize that changes the hardware videomode] - */ -#define VC_RESIZE_MAXCOL (32767) -#define VC_RESIZE_MAXROW (32767) - /** * vc_do_resize - resizing method for the tty * @tty: tty being resized @@ -1201,7 +1207,7 @@ static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc, user = vc->vc_resize_user; vc->vc_resize_user = 0; - if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW) + if (cols > VC_MAXCOL || lines > VC_MAXROW) return -EINVAL; new_cols = (cols ? cols : vc->vc_cols); @@ -1212,7 +1218,7 @@ static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc, if (new_cols == vc->vc_cols && new_rows == vc->vc_rows) return 0; - if (new_screen_size > KMALLOC_MAX_SIZE) + if (new_screen_size > KMALLOC_MAX_SIZE || !new_screen_size) return -EINVAL; newscreen = kzalloc(new_screen_size, GFP_USER); if (!newscreen) @@ -3393,6 +3399,7 @@ static int __init con_init(void) INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK); tty_port_init(&vc->port); visual_init(vc, currcons, 1); + /* Assuming vc->vc_{cols,rows,screenbuf_size} are sane here. */ vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT); vc_init(vc, vc->vc_rows, vc->vc_cols, currcons || !vc->vc_sw->con_save_screen); -- 2.18.4