Skip to contentSkip to conversation

tty: vt: Check for NULL pointer after calling kzalloc

PATCH In review Jiasheng Jiang 3 participants 3 messages Jan 21, 2022 TTY layer and serial drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tty: vt: Check for NULL pointer after calling kzalloc

As the potential failure of the allocation, the kzalloc() will return NULL pointer.
Therefore, it should be better to check it in order to avoid the dereference of the NULL pointer.
When it fails, we should free all the allocated memory and return error number.
To make the code more clear, I use the 'err_free', like how vc_allocate() deals with the allocation failure.

Fixes a5f4f52e8211 ("vt: use kzalloc() instead of the bootmem allocator")
---
 drivers/tty/vt/vt.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/tty/vt/vt.c +19 -0
index f8c87c4d7399..343fa6fffc18 100644
@@ -3519,11 +3519,17 @@ 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)
+			goto err_free;
+
 		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);
+		if (!vc->vc_screenbuf)
+			goto err_free_vc;
+
 		vc_init(vc, vc->vc_rows, vc->vc_cols,
 			currcons || !vc->vc_sw->con_save_screen);
 	}
@@ -3545,6 +3551,19 @@ static int __init con_init(void)
 	register_console(&vt_console_driver);
 #endif
 	return 0;
+err_free_vc:
+	visual_deinit(vc);
+	kfree(vc);
+	vc_cons[currcons].d = NULL;
+err_free:
+	for (i = 0; i < currcons; i++) {
+		kfree(vc_cons[currcons].d->vc_screenbuf);
+		visual_deinit(vc_cons[currcons].d);
+		kfree(vc_cons[currcons].d);
+		vc_cons[currcons].d = NULL;
+	}
+	console_unlock();
+	return -ENOMEM;
 }
 console_initcall(con_init);
 
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 8 lines · Jiasheng Jiang

As the potential failure of the allocation, the kzalloc() will return NULL pointer.
Therefore, it should be better to check it in order to avoid the dereference of the NULL pointer.
When it fails, we should free all the allocated memory and return error number.
To make the code more clear, I use the 'err_free', like how vc_allocate() deals with the allocation failure.

https://lore.kernel.org/all/[email protected]/

Quoted 48 lines
Fixes a5f4f52e8211 ("vt: use kzalloc() instead of the bootmem allocator")
---
  drivers/tty/vt/vt.c | 19 +++++++++++++++++++
  1 file changed, 19 insertions(+)
drivers/tty/vt/vt.c +19 -0
index f8c87c4d7399..343fa6fffc18 100644
@@ -3519,11 +3519,17 @@ 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)
+			goto err_free;
+
  		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);
+		if (!vc->vc_screenbuf)
+			goto err_free_vc;
+
  		vc_init(vc, vc->vc_rows, vc->vc_cols,
  			currcons || !vc->vc_sw->con_save_screen);
  	}
@@ -3545,6 +3551,19 @@ static int __init con_init(void)
  	register_console(&vt_console_driver);
  #endif
  	return 0;
+err_free_vc:
+	visual_deinit(vc);
+	kfree(vc);
+	vc_cons[currcons].d = NULL;
+err_free:
+	for (i = 0; i < currcons; i++) {
+		kfree(vc_cons[currcons].d->vc_screenbuf);
+		visual_deinit(vc_cons[currcons].d);
+		kfree(vc_cons[currcons].d);
+		vc_cons[currcons].d = NULL;
+	}
+	console_unlock();
+	return -ENOMEM;
  }
  console_initcall(con_init);
 
Signature · 1 line
js
maintainer
#2 lore raw
In reply to Jiasheng Jiang
Quoted 9 lines · Jiasheng Jiang

As the potential failure of the allocation, the kzalloc() will return NULL pointer.
Therefore, it should be better to check it in order to avoid the dereference of the NULL pointer.
When it fails, we should free all the allocated memory and return error number.
To make the code more clear, I use the 'err_free', like how vc_allocate() deals with the allocation failure.

Quoted 5 lines
Fixes a5f4f52e8211 ("vt: use kzalloc() instead of the bootmem allocator")
---
 drivers/tty/vt/vt.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

Please note that your patches are being ignored by my email systems (I saw this because Jiri responded to it.) I recommend at this point in time that all other kernel subsystem maintainers also ignore them until you get some more experience and provide changes that work correctly and are actually tested.

good luck,

greg k-h