Skip to content

Commit ce68455

Browse files
Tetsuo Handagregkh
authored andcommitted
vt: Reject zero-sized screen buffer size.
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 caused by fb_set_var() from ioctl(FBIOPUT_VSCREENINFO) on /dev/fb0 , for gotoxy(vc, 0, 0) from reset_terminal() from vc_init() from vc_allocate() from con_install() from tty_init_dev() from tty_open() 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 or 0 row makes sense. And it seems that vc_do_resize() does not intend to allow resizing a console to 0 column or 0 row due to new_cols = (cols ? cols : vc->vc_cols); new_rows = (lines ? lines : vc->vc_rows); exception. 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(). Since we can detect cols == 0 or rows == 0 via screenbuf_size = 0 in visual_init(), we can reject kzalloc(0). Then, vc_allocate() will return an error, and con_write() will not be called on a console with 0 column or 0 row. We need to make sure that integer overflow in visual_init() won't happen. Since vc_do_resize() restricts cols <= 32767 and rows <= 32767, applying 1 <= cols <= 32767 and 1 <= rows <= 32767 restrictions to vc_allocate() will be practically fine. 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-and-tested-by: syzbot <[email protected]> Signed-off-by: Tetsuo Handa <[email protected]> Cc: stable <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent ba47d84 commit ce68455

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

drivers/tty/vt/vt.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,10 +1092,19 @@ static const struct tty_port_operations vc_port_ops = {
10921092
.destruct = vc_port_destruct,
10931093
};
10941094

1095+
/*
1096+
* Change # of rows and columns (0 means unchanged/the size of fg_console)
1097+
* [this is to be used together with some user program
1098+
* like resize that changes the hardware videomode]
1099+
*/
1100+
#define VC_MAXCOL (32767)
1101+
#define VC_MAXROW (32767)
1102+
10951103
int vc_allocate(unsigned int currcons) /* return 0 on success */
10961104
{
10971105
struct vt_notifier_param param;
10981106
struct vc_data *vc;
1107+
int err;
10991108

11001109
WARN_CONSOLE_UNLOCKED();
11011110

@@ -1125,6 +1134,11 @@ int vc_allocate(unsigned int currcons) /* return 0 on success */
11251134
if (!*vc->vc_uni_pagedir_loc)
11261135
con_set_default_unimap(vc);
11271136

1137+
err = -EINVAL;
1138+
if (vc->vc_cols > VC_MAXCOL || vc->vc_rows > VC_MAXROW ||
1139+
vc->vc_screenbuf_size > KMALLOC_MAX_SIZE || !vc->vc_screenbuf_size)
1140+
goto err_free;
1141+
err = -ENOMEM;
11281142
vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_KERNEL);
11291143
if (!vc->vc_screenbuf)
11301144
goto err_free;
@@ -1143,7 +1157,7 @@ int vc_allocate(unsigned int currcons) /* return 0 on success */
11431157
visual_deinit(vc);
11441158
kfree(vc);
11451159
vc_cons[currcons].d = NULL;
1146-
return -ENOMEM;
1160+
return err;
11471161
}
11481162

11491163
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,
11581172
return err;
11591173
}
11601174

1161-
/*
1162-
* Change # of rows and columns (0 means unchanged/the size of fg_console)
1163-
* [this is to be used together with some user program
1164-
* like resize that changes the hardware videomode]
1165-
*/
1166-
#define VC_RESIZE_MAXCOL (32767)
1167-
#define VC_RESIZE_MAXROW (32767)
1168-
11691175
/**
11701176
* vc_do_resize - resizing method for the tty
11711177
* @tty: tty being resized
@@ -1201,7 +1207,7 @@ static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
12011207
user = vc->vc_resize_user;
12021208
vc->vc_resize_user = 0;
12031209

1204-
if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
1210+
if (cols > VC_MAXCOL || lines > VC_MAXROW)
12051211
return -EINVAL;
12061212

12071213
new_cols = (cols ? cols : vc->vc_cols);
@@ -1212,7 +1218,7 @@ static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
12121218
if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
12131219
return 0;
12141220

1215-
if (new_screen_size > KMALLOC_MAX_SIZE)
1221+
if (new_screen_size > KMALLOC_MAX_SIZE || !new_screen_size)
12161222
return -EINVAL;
12171223
newscreen = kzalloc(new_screen_size, GFP_USER);
12181224
if (!newscreen)
@@ -3393,6 +3399,7 @@ static int __init con_init(void)
33933399
INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
33943400
tty_port_init(&vc->port);
33953401
visual_init(vc, currcons, 1);
3402+
/* Assuming vc->vc_{cols,rows,screenbuf_size} are sane here. */
33963403
vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
33973404
vc_init(vc, vc->vc_rows, vc->vc_cols,
33983405
currcons || !vc->vc_sw->con_save_screen);

0 commit comments

Comments
 (0)