Skip to contentSkip to conversation

vt: Fix a missing-check bug in con_init()

PATCH Superseded Gen Zhang 4 participants 11 messages May 28, 2019 – Jun 10, 2019 TTY layer and serial drivers

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Gen Zhang

author
#0 lore raw

PATCH v3 1/1 vt: Fix a missing-check bug in con_init()

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated by kzalloc(). And they are used in the following codes. However, kzalloc() returns NULL when fails, and null pointer dereference may happen. And it will cause the kernel to crash. Therefore, we should check the return value and handle the error.

Further, since the allcoation is in a loop, we should free all the allocated memory in a loop.

---

drivers/tty/vt/vt.c +14 -0
index fdd12f8..d50f68f 100644
@@ -3350,10 +3350,14 @@ 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 fail1;
 		INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
 		tty_port_init(&vc->port);
 		visual_init(vc, currcons, 1);
 		vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
+		if (!vc->vc_screenbuf)
+			goto fail2;
 		vc_init(vc, vc->vc_rows, vc->vc_cols,
 			currcons || !vc->vc_sw->con_save_screen);
 	}
@@ -3375,6 +3379,16 @@ static int __init con_init(void)
 	register_console(&vt_console_driver);
 #endif
 	return 0;
+fail1:
+	while (currcons > 0) {
+		currcons--;
+		kfree(vc_cons[currcons].d->vc_screenbuf);
+fail2:
+		kfree(vc_cons[currcons].d);
+		vc_cons[currcons].d = NULL;
+	}
+	console_unlock();
+	return -ENOMEM;
 }
 console_initcall(con_init);
 

---

author
#1 lore raw
Quoted 39 lines · Gen Zhang

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated by kzalloc(). And they are used in the following codes. However, kzalloc() returns NULL when fails, and null pointer dereference may happen. And it will cause the kernel to crash. Therefore, we should check the return value and handle the error.

Further, since the allcoation is in a loop, we should free all the allocated memory in a loop.

---

drivers/tty/vt/vt.c +8 -0
index fdd12f8..d50f68f 100644
@@ -3350,10 +3350,14 @@ 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 fail1;
 		INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
 		tty_port_init(&vc->port);
 		visual_init(vc, currcons, 1);
 		vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
+		if (!vc->vc_screenbuf)
+			goto fail2;
 		vc_init(vc, vc->vc_rows, vc->vc_cols,
 			currcons || !vc->vc_sw->con_save_screen);
 	}
@@ -3375,6 +3379,16 @@ static int __init con_init(void)
 	register_console(&vt_console_driver);
 #endif
 	return 0;
+fail1:
+	while (currcons > 0) {
+		currcons--;
+		kfree(vc_cons[currcons].d->vc_screenbuf);

drivers/tty/vt/vt.c static int __init con_init(void)

Quoted 10 lines
drivers/tty/vt/vt.c +6 -1
+fail2:
+		kfree(vc_cons[currcons].d);
+		vc_cons[currcons].d = NULL;
+	}
+	console_unlock();
+	return -ENOMEM;
 }
 console_initcall(con_init);
 
---

Can anyone look into this patch? It's already reviewed by Nicolas Pitre <[email protected]>.

Thanks
Gen

maintainer
#2 lore raw
Quoted 49 lines · Gen Zhang
Quoted 48 lines · Gen Zhang

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated by kzalloc(). And they are used in the following codes. However, kzalloc() returns NULL when fails, and null pointer dereference may happen. And it will cause the kernel to crash. Therefore, we should check the return value and handle the error.

Further, since the allcoation is in a loop, we should free all the allocated memory in a loop.

---

drivers/tty/vt/vt.c +14 -0
index fdd12f8..d50f68f 100644
@@ -3350,10 +3350,14 @@ 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 fail1;
 		INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
 		tty_port_init(&vc->port);
 		visual_init(vc, currcons, 1);
 		vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
+		if (!vc->vc_screenbuf)
+			goto fail2;
 		vc_init(vc, vc->vc_rows, vc->vc_cols,
 			currcons || !vc->vc_sw->con_save_screen);
 	}
@@ -3375,6 +3379,16 @@ static int __init con_init(void)
 	register_console(&vt_console_driver);
 #endif
 	return 0;
+fail1:
+	while (currcons > 0) {
+		currcons--;
+		kfree(vc_cons[currcons].d->vc_screenbuf);
+fail2:
+		kfree(vc_cons[currcons].d);
+		vc_cons[currcons].d = NULL;
+	}
+	console_unlock();
+	return -ENOMEM;
 }
 console_initcall(con_init);
 
Quoted 3 lines
Quoted 1 line

---

Can anyone look into this patch? It's already reviewed by Nicolas Pitre <[email protected]>.

It's in my queue. But note, given the previous history of your patches, it's really low on my piority list at the moment :(

greg k-h

author
#3 lore raw
Quoted 50 lines · Greg KH
Quoted 49 lines · Gen Zhang
Quoted 48 lines · Gen Zhang

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated by kzalloc(). And they are used in the following codes. However, kzalloc() returns NULL when fails, and null pointer dereference may happen. And it will cause the kernel to crash. Therefore, we should check the return value and handle the error.

Further, since the allcoation is in a loop, we should free all the allocated memory in a loop.

---

drivers/tty/vt/vt.c +14 -0
index fdd12f8..d50f68f 100644
@@ -3350,10 +3350,14 @@ 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 fail1;
 		INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
 		tty_port_init(&vc->port);
 		visual_init(vc, currcons, 1);
 		vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
+		if (!vc->vc_screenbuf)
+			goto fail2;
 		vc_init(vc, vc->vc_rows, vc->vc_cols,
 			currcons || !vc->vc_sw->con_save_screen);
 	}
@@ -3375,6 +3379,16 @@ static int __init con_init(void)
 	register_console(&vt_console_driver);
 #endif
 	return 0;
+fail1:
+	while (currcons > 0) {
+		currcons--;
+		kfree(vc_cons[currcons].d->vc_screenbuf);
+fail2:
+		kfree(vc_cons[currcons].d);
+		vc_cons[currcons].d = NULL;
+	}
+	console_unlock();
+	return -ENOMEM;
 }
 console_initcall(con_init);
 
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

---

Can anyone look into this patch? It's already reviewed by Nicolas Pitre <[email protected]>.

It's in my queue. But note, given the previous history of your patches, it's really low on my piority list at the moment :(

greg k-h

What? All the patches were revised iteratively according to the maintainers' or reviewers' advice. I don't think you should look down the patches from me. It seems not fair enough. :(

Thanks
Gen

author
#4 lore raw
In reply to Greg KH
Quoted 50 lines · Greg KH
Quoted 49 lines · Gen Zhang
Quoted 48 lines · Gen Zhang

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated by kzalloc(). And they are used in the following codes. However, kzalloc() returns NULL when fails, and null pointer dereference may happen. And it will cause the kernel to crash. Therefore, we should check the return value and handle the error.

Further, since the allcoation is in a loop, we should free all the allocated memory in a loop.

---

drivers/tty/vt/vt.c +14 -0
index fdd12f8..d50f68f 100644
@@ -3350,10 +3350,14 @@ 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 fail1;
 		INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
 		tty_port_init(&vc->port);
 		visual_init(vc, currcons, 1);
 		vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
+		if (!vc->vc_screenbuf)
+			goto fail2;
 		vc_init(vc, vc->vc_rows, vc->vc_cols,
 			currcons || !vc->vc_sw->con_save_screen);
 	}
@@ -3375,6 +3379,16 @@ static int __init con_init(void)
 	register_console(&vt_console_driver);
 #endif
 	return 0;
+fail1:
+	while (currcons > 0) {
+		currcons--;
+		kfree(vc_cons[currcons].d->vc_screenbuf);
+fail2:
+		kfree(vc_cons[currcons].d);
+		vc_cons[currcons].d = NULL;
+	}
+	console_unlock();
+	return -ENOMEM;
 }
 console_initcall(con_init);
 
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

---

Can anyone look into this patch? It's already reviewed by Nicolas Pitre <[email protected]>.

It's in my queue. But note, given the previous history of your patches, it's really low on my piority list at the moment :(

greg k-h

Anyway, I should be honored to be remembered by Greg K-H. :-)

Thanks
Gen

maintainer
#5 lore raw
In reply to Gen Zhang
Quoted 36 lines · Gen Zhang
Quoted 35 lines · Gen Zhang

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated by kzalloc(). And they are used in the following codes. However, kzalloc() returns NULL when fails, and null pointer dereference may happen. And it will cause the kernel to crash. Therefore, we should check the return value and handle the error.

Further, since the allcoation is in a loop, we should free all the allocated memory in a loop.

---

drivers/tty/vt/vt.c +4 -0
index fdd12f8..d50f68f 100644
@@ -3350,10 +3350,14 @@ 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 fail1;
 		INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
 		tty_port_init(&vc->port);
 		visual_init(vc, currcons, 1);
 		vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
+		if (!vc->vc_screenbuf)
+			goto fail2;
 		vc_init(vc, vc->vc_rows, vc->vc_cols,
 			currcons || !vc->vc_sw->con_save_screen);
 	}
@@ -3375,6 +3379,16 @@ static int __init con_init(void)
 	register_console(&vt_console_driver);
 #endif
 	return 0;
Quoted 8 lines

drivers/tty/vt/vt.c static int __init con_init(void) · line 3389

Quoted 8 lines
drivers/tty/vt/vt.c +8 -0
+fail1:
+	while (currcons > 0) {
+		currcons--;
+		kfree(vc_cons[currcons].d->vc_screenbuf);
+fail2:
+		kfree(vc_cons[currcons].d);
+		vc_cons[currcons].d = NULL;
+	}

Wait, will that even work? You can jump into the middle of a while loop?

Ugh, that's beyond ugly. And please provide "real" names for the labels, "fail1" and "fail2" do not tell anything here.

Also, have you actually be able to trigger this?

thanks,

greg k-h

author
#6 lore raw
Quoted 40 lines · Greg KH
Quoted 39 lines · Gen Zhang
Quoted 38 lines · Gen Zhang

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated by kzalloc(). And they are used in the following codes. However, kzalloc() returns NULL when fails, and null pointer dereference may happen. And it will cause the kernel to crash. Therefore, we should check the return value and handle the error.

Further, since the allcoation is in a loop, we should free all the allocated memory in a loop.

---

drivers/tty/vt/vt.c +7 -0
index fdd12f8..d50f68f 100644
@@ -3350,10 +3350,14 @@ 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 fail1;
 		INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
 		tty_port_init(&vc->port);
 		visual_init(vc, currcons, 1);
 		vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
+		if (!vc->vc_screenbuf)
+			goto fail2;
 		vc_init(vc, vc->vc_rows, vc->vc_cols,
 			currcons || !vc->vc_sw->con_save_screen);
 	}
@@ -3375,6 +3379,16 @@ static int __init con_init(void)
 	register_console(&vt_console_driver);
 #endif
 	return 0;
+fail1:
+	while (currcons > 0) {
+		currcons--;
Quoted 8 lines
Quoted 5 lines

drivers/tty/vt/vt.c static int __init con_init(void) · line 3389

Quoted 5 lines
drivers/tty/vt/vt.c +5 -0
+		kfree(vc_cons[currcons].d->vc_screenbuf);
+fail2:
+		kfree(vc_cons[currcons].d);
+		vc_cons[currcons].d = NULL;
+	}

Wait, will that even work? You can jump into the middle of a while loop?

I felt like the same when I saw reviewer's advice to write in this way. But I tested, and it did work.

Quoted 3 lines

Ugh, that's beyond ugly. And please provide "real" names for the labels, "fail1" and "fail2" do not tell anything here.

Sure, I can revise that if needed.

Thanks
Gen

Quoted 6 lines

Also, have you actually be able to trigger this?

thanks,

greg k-h

#7 lore raw
In reply to Greg KH
Quoted 40 lines · Greg KH
Quoted 39 lines · Gen Zhang
Quoted 38 lines · Gen Zhang

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated by kzalloc(). And they are used in the following codes. However, kzalloc() returns NULL when fails, and null pointer dereference may happen. And it will cause the kernel to crash. Therefore, we should check the return value and handle the error.

Further, since the allcoation is in a loop, we should free all the allocated memory in a loop.

---

drivers/tty/vt/vt.c +7 -0
index fdd12f8..d50f68f 100644
@@ -3350,10 +3350,14 @@ 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 fail1;
 		INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
 		tty_port_init(&vc->port);
 		visual_init(vc, currcons, 1);
 		vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
+		if (!vc->vc_screenbuf)
+			goto fail2;
 		vc_init(vc, vc->vc_rows, vc->vc_cols,
 			currcons || !vc->vc_sw->con_save_screen);
 	}
@@ -3375,6 +3379,16 @@ static int __init con_init(void)
 	register_console(&vt_console_driver);
 #endif
 	return 0;
+fail1:
+	while (currcons > 0) {
+		currcons--;
Quoted 8 lines
Quoted 5 lines

drivers/tty/vt/vt.c static int __init con_init(void) · line 3389

Quoted 5 lines
drivers/tty/vt/vt.c +5 -0
+		kfree(vc_cons[currcons].d->vc_screenbuf);
+fail2:
+		kfree(vc_cons[currcons].d);
+		vc_cons[currcons].d = NULL;
+	}

Wait, will that even work? You can jump into the middle of a while loop?

Absolutely.

Quoted 1 line

Ugh, that's beyond ugly.

That was me who suggested to do it like that. To me, this is nicer than the proposed alternatives. For an error path that is rather unlikely to happen, I think this is a very concise and eleguant way to do it.

Quoted 2 lines

And please provide "real" names for the labels, "fail1" and "fail2" do not tell anything here.

That I agree with.

Nicolas

author
#8 lore raw
Quoted 59 lines · Nicolas Pitre
Quoted 40 lines · Greg KH
Quoted 39 lines · Gen Zhang
Quoted 38 lines · Gen Zhang

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated by kzalloc(). And they are used in the following codes. However, kzalloc() returns NULL when fails, and null pointer dereference may happen. And it will cause the kernel to crash. Therefore, we should check the return value and handle the error.

Further, since the allcoation is in a loop, we should free all the allocated memory in a loop.

---

drivers/tty/vt/vt.c +7 -0
index fdd12f8..d50f68f 100644
@@ -3350,10 +3350,14 @@ 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 fail1;
 		INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
 		tty_port_init(&vc->port);
 		visual_init(vc, currcons, 1);
 		vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
+		if (!vc->vc_screenbuf)
+			goto fail2;
 		vc_init(vc, vc->vc_rows, vc->vc_cols,
 			currcons || !vc->vc_sw->con_save_screen);
 	}
@@ -3375,6 +3379,16 @@ static int __init con_init(void)
 	register_console(&vt_console_driver);
 #endif
 	return 0;
+fail1:
+	while (currcons > 0) {
+		currcons--;
Quoted 8 lines
Quoted 5 lines

drivers/tty/vt/vt.c static int __init con_init(void) · line 3389

Quoted 5 lines
drivers/tty/vt/vt.c +5 -0
+		kfree(vc_cons[currcons].d->vc_screenbuf);
+fail2:
+		kfree(vc_cons[currcons].d);
+		vc_cons[currcons].d = NULL;
+	}

Wait, will that even work? You can jump into the middle of a while loop?

Absolutely.

Quoted 1 line

Ugh, that's beyond ugly.

That was me who suggested to do it like that. To me, this is nicer than the proposed alternatives. For an error path that is rather unlikely to happen, I think this is a very concise and eleguant way to do it.

Quoted 7 lines
Quoted 2 lines

And please provide "real" names for the labels, "fail1" and "fail2" do not tell anything here.

That I agree with.

Nicolas

Thanks for your comments. Then am I supposed to revise the patch and send a v4 version?

Thanks
Gen

#9 lore raw
Quoted 6 lines · Gen Zhang
Quoted 5 lines · Nicolas Pitre
Quoted 2 lines · Greg KH

And please provide "real" names for the labels, "fail1" and "fail2" do not tell anything here.

Quoted 6 lines
Quoted 4 lines

That I agree with.

Nicolas

Thanks for your comments. Then am I supposed to revise the patch and send a v4 version?

Yes.

Nicolas

#10 lore raw
In reply to Nicolas Pitre
Quoted 12 lines · Nicolas Pitre
Quoted 11 lines
Quoted 8 lines

drivers/tty/vt/vt.c static int __init con_init(void) · line 3389

Quoted 8 lines
drivers/tty/vt/vt.c +8 -0
+fail1:
+	while (currcons > 0) {
+		currcons--;
+		kfree(vc_cons[currcons].d->vc_screenbuf);
+fail2:
+		kfree(vc_cons[currcons].d);
+		vc_cons[currcons].d = NULL;
+	}

Wait, will that even work? You can jump into the middle of a while loop?

Quoted 1 line

Absolutely.

In C99, exceptions are only blocks with variable-sized declarations (like "int a[b]").

thanks,

Signature · 2 lines
js
suse labs