Skip to contentSkip to conversation

tty:vt: Add NULL check to return value of kzalloc()

PATCH Superseded Wang YanQing 2 participants 2 messages Oct 28, 2013 TTY layer and serial drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tty:vt: Add NULL check to return value of kzalloc()

In extreme situation kzalloc in con_init may
return NULL, so we should check it to prevent
OOPS!

---
I don't think a system without memory to initialize vt could do some useful things, but this patch could make debug easier and improve kernel code's robustness in theory.

 drivers/tty/vt/vc_screen.c |  2 +-
 drivers/tty/vt/vt.c        | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
drivers/tty/vt/vc_screen.c +1 -1
index 14a2b5f..2bc3987bc 100644
@@ -648,6 +648,6 @@ int __init vcs_init(void)
 	device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
 	device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");
 	for (i = 0; i < MIN_NR_CONSOLES; i++)
-		vcs_make_sysfs(i);
+		if (vc_cons[i].d) vcs_make_sysfs(i);
 	return 0;
 }
drivers/tty/vt/vt.c +12 -0
index 9a8e8c5..ef8b1aa 100644
@@ -2887,6 +2887,8 @@ static int __init con_init(void)
 
 	for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
 		vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
+		if (vc == NULL)
+			break;
 		INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
 		tty_port_init(&vc->port);
 		visual_init(vc, currcons, 1);
@@ -2894,8 +2896,18 @@ static int __init con_init(void)
 		vc_init(vc, vc->vc_rows, vc->vc_cols,
 			currcons || !vc->vc_sw->con_save_screen);
 	}
+	if (currcons < MIN_NR_CONSOLES)
+		printk(KERN_WARNING "Console: vt %d-%d initialization failed!\n",
+			currcons, MIN_NR_CONSOLES - 1);
+
 	currcons = fg_console = 0;
 	master_display_fg = vc = vc_cons[currcons].d;
+
+	if (vc == NULL) {
+		printk(KERN_WARNING "Console: no avaiable vt!\n");
+		console_unlock();
+		return 0;
+	}
 	set_origin(vc);
 	save_screen(vc);
 	gotoxy(vc, vc->vc_x, vc->vc_y);
Signature · 1 line
1.7.12.4.dirty
maintainer
#1 lore raw
Quoted 6 lines · Wang YanQing

In extreme situation kzalloc in con_init may
return NULL, so we should check it to prevent
OOPS!

I need a name for Reported-by: as well, not just an email.

Quoted 5 lines

---
I don't think a system without memory to initialize vt could do some useful things, but this patch could make debug easier and improve kernel code's robustness in theory.

Please always run your patches through the scripts/checkpatch.pl tool and fix the problems found there so that maintainers don't have to point out the same thing...

greg k-h