Skip to contentSkip to conversation

vt: Fix a missing-check bug in drivers/tty/vt/vt.c

PATCH Superseded Gen Zhang 3 participants 14 messages May 21, 2019 – May 24, 2019 TTY layer and serial drivers

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Gen Zhang

author
#0 lore raw

PATCH v2 1/1 vt: Fix a missing-check bug in drivers/tty/vt/vt.c

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated a memory space via kzalloc(). And they are used in the following codes.
However, when there is a memory allocation error, kzalloc() can fail. Thus null pointer (vc_cons[currcons].d, vc and vc->vc_screenbuf) dereference may happen. And it will cause the kernel to crash. Therefore, we should check return value and handle the error. Further,the loop condition MIN_NR_CONSOLES is defined as 1 in include/uapi/linux/vt.h. So there is no need to unwind the loop.

---

drivers/tty/vt/vt.c +12 -0
index fdd12f8..b756609 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_cons[currcons].d || !vc)
+			goto err_vc;
 		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 err_vc_screenbuf;
 		vc_init(vc, vc->vc_rows, vc->vc_cols,
 			currcons || !vc->vc_sw->con_save_screen);
 	}
@@ -3375,6 +3379,14 @@ static int __init con_init(void)
 	register_console(&vt_console_driver);
 #endif
 	return 0;
+err_vc:
+	console_unlock();
+	return -ENOMEM;
+err_vc_screenbuf:
+	console_unlock();
+	kfree(vc);
+	vc_cons[currcons].d = NULL;
+	return -ENOMEM;
 }
 console_initcall(con_init);
 

---

#1 lore raw
Quoted 9 lines · Gen Zhang

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated a memory space via kzalloc(). And they are used in the following codes.
However, when there is a memory allocation error, kzalloc() can fail. Thus null pointer (vc_cons[currcons].d, vc and vc->vc_screenbuf) dereference may happen. And it will cause the kernel to crash. Therefore, we should check return value and handle the error. Further,the loop condition MIN_NR_CONSOLES is defined as 1 in include/uapi/linux/vt.h. So there is no need to unwind the loop.

But what if someone changes that define? It won't be obvious that some code did rely on it to be defined to 1.

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

Quoted 12 lines

---

drivers/tty/vt/vt.c +1 -0
index fdd12f8..b756609 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_cons[currcons].d || !vc)

Both vc_cons[currcons].d and vc are assigned the same value on the previous line. You don't have to test them both.

Quoted 12 lines
drivers/tty/vt/vt.c +3 -0
+			goto err_vc;
 		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 err_vc_screenbuf;
 		vc_init(vc, vc->vc_rows, vc->vc_cols,
 			currcons || !vc->vc_sw->con_save_screen);
 	}
@@ -3375,6 +3379,14 @@ static int __init con_init(void)
 	register_console(&vt_console_driver);

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

Quoted 10 lines
drivers/tty/vt/vt.c +8 -0
 #endif
 	return 0;
+err_vc:
+	console_unlock();
+	return -ENOMEM;
+err_vc_screenbuf:
+	console_unlock();
+	kfree(vc);
+	vc_cons[currcons].d = NULL;
+	return -ENOMEM;

As soon as you release the lock, another thread could come along and start using the memory pointed by vc_cons[currcons].d you're about to free here. This is unlikely for an initcall, but still.

You should consider this ordering instead:

err_vc_screenbuf:

	kfree(vc);
	vc_cons[currcons].d = NULL;
err_vc:
	console_unlock();
	return -ENOMEM;
Quoted 5 lines
drivers/tty/vt/vt.c +0 -0
 }
 console_initcall(con_init);
 

---

author
#2 lore raw
Quoted 12 lines · Nicolas Pitre
Quoted 9 lines · Gen Zhang

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated a memory space via kzalloc(). And they are used in the following codes.
However, when there is a memory allocation error, kzalloc() can fail. Thus null pointer (vc_cons[currcons].d, vc and vc->vc_screenbuf) dereference may happen. And it will cause the kernel to crash. Therefore, we should check return value and handle the error. Further,the loop condition MIN_NR_CONSOLES is defined as 1 in include/uapi/linux/vt.h. So there is no need to unwind the loop.

Quoted 2 lines

But what if someone changes that define? It won't be obvious that some code did rely on it to be defined to 1.

I re-examine the source code. MIN_NR_CONSOLES is only defined once and no other changes to it.

Quoted 10 lines
Quoted 10 lines

---

drivers/tty/vt/vt.c +0 -0
index fdd12f8..b756609 100644
@@ -3350,10 +3350,14 @@ static int __init con_init(void)
 
Quoted 6 lines

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

Quoted 3 lines
drivers/tty/vt/vt.c +1 -0
 	for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
 		vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
+		if (!vc_cons[currcons].d || !vc)

Both vc_cons[currcons].d and vc are assigned the same value on the previous line. You don't have to test them both.

Thanks for this comment!

Quoted 28 lines
Quoted 13 lines
drivers/tty/vt/vt.c +3 -0
+			goto err_vc;
 		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 err_vc_screenbuf;
 		vc_init(vc, vc->vc_rows, vc->vc_cols,
 			currcons || !vc->vc_sw->con_save_screen);
 	}
@@ -3375,6 +3379,14 @@ static int __init con_init(void)
 	register_console(&vt_console_driver);

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

Quoted 10 lines
drivers/tty/vt/vt.c +8 -0
 #endif
 	return 0;
+err_vc:
+	console_unlock();
+	return -ENOMEM;
+err_vc_screenbuf:
+	console_unlock();
+	kfree(vc);
+	vc_cons[currcons].d = NULL;
+	return -ENOMEM;

As soon as you release the lock, another thread could come along and start using the memory pointed by vc_cons[currcons].d you're about to free here. This is unlikely for an initcall, but still.

Quoted 10 lines

You should consider this ordering instead:

err_vc_screenbuf:

	kfree(vc);
	vc_cons[currcons].d = NULL;
err_vc:
	console_unlock();
	return -ENOMEM;

Thanks for your patient reply, Nicolas!
I will work on this patch and resubmit it.
Thanks
Gen

Quoted 5 lines
Quoted 5 lines
drivers/tty/vt/vt.c +0 -0
 }
 console_initcall(con_init);
 

---

#3 lore raw
Quoted 13 lines · Gen Zhang
Quoted 12 lines · Nicolas Pitre
Quoted 9 lines · Gen Zhang

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated a memory space via kzalloc(). And they are used in the following codes.
However, when there is a memory allocation error, kzalloc() can fail. Thus null pointer (vc_cons[currcons].d, vc and vc->vc_screenbuf) dereference may happen. And it will cause the kernel to crash. Therefore, we should check return value and handle the error. Further,the loop condition MIN_NR_CONSOLES is defined as 1 in include/uapi/linux/vt.h. So there is no need to unwind the loop.

Quoted 4 lines
Quoted 2 lines

But what if someone changes that define? It won't be obvious that some code did rely on it to be defined to 1.

I re-examine the source code. MIN_NR_CONSOLES is only defined once and no other changes to it.

Yes, that is true today. But if someone changes that in the future, how will that person know that you relied on it to be 1 for not needing to unwind the loop?

Nicolas

author
#4 lore raw
Quoted 20 lines · Nicolas Pitre
Quoted 13 lines · Gen Zhang
Quoted 12 lines · Nicolas Pitre
Quoted 9 lines · Gen Zhang

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated a memory space via kzalloc(). And they are used in the following codes.
However, when there is a memory allocation error, kzalloc() can fail. Thus null pointer (vc_cons[currcons].d, vc and vc->vc_screenbuf) dereference may happen. And it will cause the kernel to crash. Therefore, we should check return value and handle the error. Further,the loop condition MIN_NR_CONSOLES is defined as 1 in include/uapi/linux/vt.h. So there is no need to unwind the loop.

Quoted 4 lines
Quoted 2 lines

But what if someone changes that define? It won't be obvious that some code did rely on it to be defined to 1.

I re-examine the source code. MIN_NR_CONSOLES is only defined once and no other changes to it.

Quoted 6 lines

Yes, that is true today. But if someone changes that in the future, how will that person know that you relied on it to be 1 for not needing to unwind the loop?

Nicolas

Hi Nicolas,
Thanks for your explaination! And I got your point. And is this way proper?

err_vc_screenbuf:

        kfree(vc);
	for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++)
		vc_cons[currcons].d = NULL;
	return -ENOMEM;
err_vc:
	console_unlock();
	return -ENOMEM;

Thanks
Gen

#5 lore raw
Quoted 26 lines · Gen Zhang
Quoted 25 lines · Nicolas Pitre
Quoted 13 lines · Gen Zhang
Quoted 12 lines · Nicolas Pitre
Quoted 9 lines · Gen Zhang

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated a memory space via kzalloc(). And they are used in the following codes.
However, when there is a memory allocation error, kzalloc() can fail. Thus null pointer (vc_cons[currcons].d, vc and vc->vc_screenbuf) dereference may happen. And it will cause the kernel to crash. Therefore, we should check return value and handle the error. Further,the loop condition MIN_NR_CONSOLES is defined as 1 in include/uapi/linux/vt.h. So there is no need to unwind the loop.

Quoted 4 lines
Quoted 2 lines

But what if someone changes that define? It won't be obvious that some code did rely on it to be defined to 1.

I re-examine the source code. MIN_NR_CONSOLES is only defined once and no other changes to it.

Yes, that is true today. But if someone changes that in the future, how will that person know that you relied on it to be 1 for not needing to unwind the loop?

Quoted 4 lines
Quoted 1 line

Nicolas

Hi Nicolas,
Thanks for your explaination! And I got your point. And is this way proper?

Not quite.

Quoted 8 lines

err_vc_screenbuf:

        kfree(vc);
	for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++)
		vc_cons[currcons].d = NULL;
	return -ENOMEM;
err_vc:
	console_unlock();
	return -ENOMEM;

Now imagine that MIN_NR_CONSOLES is defined to 10 instead of 1.

What happens with allocated memory if the err_vc condition is met on the 5th loop?

If err_vc_screenbuf condition is encountered on the 5th loop (curcons = 4), what is the value of vc_cons[4].d? Isn't it the same as vc that you just freed?

Nicolas

author
#6 lore raw
Quoted 4 lines · Nicolas Pitre

Now imagine that MIN_NR_CONSOLES is defined to 10 instead of 1.

What happens with allocated memory if the err_vc condition is met on the 5th loop?

Yes, vc->vc_screenbuf from the last loop is still not freed, right? I don't have idea to solve this one. Could please give some advice? Since we have to consider the err_vc condition.

Quoted 6 lines

If err_vc_screenbuf condition is encountered on the 5th loop (curcons = 4), what is the value of vc_cons[4].d? Isn't it the same as vc that you just freed?

Nicolas

Thanks for your explaination! You mean a double free situation may happen, right? But in vc_allocate() there is also such a kfree(vc) and vc_cons[currcons].d = NULL operation. This situation is really confusing me.
Thanks
Gen

#7 lore raw
Quoted 14 lines · Gen Zhang
Quoted 4 lines · Nicolas Pitre

Now imagine that MIN_NR_CONSOLES is defined to 10 instead of 1.

What happens with allocated memory if the err_vc condition is met on the 5th loop?

Yes, vc->vc_screenbuf from the last loop is still not freed, right? I don't have idea to solve this one. Could please give some advice? Since we have to consider the err_vc condition.

Quoted 5 lines

If err_vc_screenbuf condition is encountered on the 5th loop (curcons = 4), what is the value of vc_cons[4].d? Isn't it the same as vc that you just freed?

Quoted 5 lines
Quoted 1 line

Nicolas

Thanks for your explaination! You mean a double free situation may happen, right? But in vc_allocate() there is also such a kfree(vc) and vc_cons[currcons].d = NULL operation. This situation is really confusing me.

What you could do is something that looks like:

	for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
		vc_cons[currcons].d = vc = kzalloc(...);
		if (!vc)
			goto fail1;
		...
		vc->vc_screenbuf = kzalloc(...);
		if (!vc->vc_screenbuf)
			goto fail2;
		...

	return 0;

	/* free already allocated memory on error */
fail1:
	while (curcons > 0) {
		curcons--;
		kfree(vc_cons[currcons].d->vc_screenbuf);
fail2:
		kfree(vc_cons[currcons].d);
		vc_cons[currcons].d = NULL;
	}
	console_unlock();
	return -ENOMEM;

Nicolas

author
#8 lore raw
Quoted 49 lines · Nicolas Pitre
Quoted 14 lines · Gen Zhang
Quoted 4 lines · Nicolas Pitre

Now imagine that MIN_NR_CONSOLES is defined to 10 instead of 1.

What happens with allocated memory if the err_vc condition is met on the 5th loop?

Yes, vc->vc_screenbuf from the last loop is still not freed, right? I don't have idea to solve this one. Could please give some advice? Since we have to consider the err_vc condition.

Quoted 5 lines

If err_vc_screenbuf condition is encountered on the 5th loop (curcons = 4), what is the value of vc_cons[4].d? Isn't it the same as vc that you just freed?

Quoted 5 lines
Quoted 1 line

Nicolas

Thanks for your explaination! You mean a double free situation may happen, right? But in vc_allocate() there is also such a kfree(vc) and vc_cons[currcons].d = NULL operation. This situation is really confusing me.

What you could do is something that looks like:

	for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
		vc_cons[currcons].d = vc = kzalloc(...);
		if (!vc)
			goto fail1;
		...
		vc->vc_screenbuf = kzalloc(...);
		if (!vc->vc_screenbuf)
			goto fail2;
		...

	return 0;

	/* free already allocated memory on error */
fail1:
	while (curcons > 0) {
		curcons--;
		kfree(vc_cons[currcons].d->vc_screenbuf);
fail2:
		kfree(vc_cons[currcons].d);
		vc_cons[currcons].d = NULL;
	}
	console_unlock();
	return -ENOMEM;
Quoted 1 line

Nicolas

Thanks for your patient explaination, Nicolas!
I will work on it and resubmit it.
Thanks
Gen

author
#9 lore raw

PATCH v3 1/1 vt: Fix a missing-check bug in drivers/tty/vt/vt.c

In reply to Nicolas Pitre

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated a memory space via kzalloc(). And they are used in the following codes.
However, when there is a memory allocation error, kzalloc() can fail. Thus null pointer (vc_cons[currcons].d, vc and vc->vc_screenbuf) dereference may happen. And it will cause the kernel to crash. Therefore, we should check 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);
 

---

Reviewed-by on v3 1/1
#10 lore raw
Quoted 11 lines · Gen Zhang

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated a memory space via kzalloc(). And they are used in the following codes.
However, when there is a memory allocation error, kzalloc() can fail. Thus null pointer (vc_cons[currcons].d, vc and vc->vc_screenbuf) dereference may happen. And it will cause the kernel to crash. Therefore, we should check return value and handle the error.
Further, since the allcoation is in a loop, we should free all the allocated memory in a loop.

Quoted 40 lines

---

drivers/tty/vt/vt.c +14 -1
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
#11 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 a memory space via kzalloc(). And they are used in the following codes.
However, when there is a memory allocation error, kzalloc() can fail. Thus null pointer (vc_cons[currcons].d, vc and vc->vc_screenbuf) dereference may happen. And it will cause the kernel to crash. Therefore, we should check 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
#12 lore raw
In reply to Nicolas Pitre
Quoted 4 lines · Nicolas Pitre

As soon as you release the lock, another thread could come along and start using the memory pointed by vc_cons[currcons].d you're about to free here. This is unlikely for an initcall, but still.

Quoted 8 lines

You should consider this ordering instead:

err_vc_screenbuf:

	kfree(vc);
	vc_cons[currcons].d = NULL;
err_vc:
	console_unlock();
	return -ENOMEM;

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated a memory space via kzalloc(). And they are used in the following codes.
However, when there is a memory allocation error, kzalloc() can fail. Thus null pointer (vc_cons[currcons].d, vc and vc->vc_screenbuf) dereference may happen. And it will cause the kernel to crash. Therefore, we should check return value and handle the error. Further,the loop condition MIN_NR_CONSOLES is defined as 1 in include/uapi/linux/vt.h and it is not changed. So there is no need to unwind the loop.

---

drivers/tty/vt/vt.c +11 -1
index fdd12f8..ea47eb3 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 err_vc;
 		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 err_vc_screenbuf;
 		vc_init(vc, vc->vc_rows, vc->vc_cols,
 			currcons || !vc->vc_sw->con_save_screen);
 	}
@@ -3375,6 +3379,13 @@ static int __init con_init(void)
 	register_console(&vt_console_driver);
 #endif
 	return 0;
+err_vc_screenbuf:
+	kfree(vc);
+	vc_cons[currcons].d = NULL;
+err_vc:
+	console_unlock();
+	return -ENOMEM;
+
 }
 console_initcall(con_init);
---
#13 lore raw

Cc'ing Grzegorz.

Quoted 60 lines · Gen Zhang
Quoted 4 lines · Nicolas Pitre

As soon as you release the lock, another thread could come along and start using the memory pointed by vc_cons[currcons].d you're about to free here. This is unlikely for an initcall, but still.

Quoted 8 lines

You should consider this ordering instead:

err_vc_screenbuf:

	kfree(vc);
	vc_cons[currcons].d = NULL;
err_vc:
	console_unlock();
	return -ENOMEM;

In function con_init(), the pointer variable vc_cons[currcons].d, vc and vc->vc_screenbuf is allocated a memory space via kzalloc(). And they are used in the following codes.
However, when there is a memory allocation error, kzalloc() can fail. Thus null pointer (vc_cons[currcons].d, vc and vc->vc_screenbuf) dereference may happen. And it will cause the kernel to crash. Therefore, we should check return value and handle the error. Further,the loop condition MIN_NR_CONSOLES is defined as 1 in include/uapi/linux/vt.h and it is not changed. So there is no need to unwind the loop.

---

drivers/tty/vt/vt.c +11 -1
index fdd12f8..ea47eb3 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 err_vc;
 		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 err_vc_screenbuf;
 		vc_init(vc, vc->vc_rows, vc->vc_cols,
 			currcons || !vc->vc_sw->con_save_screen);
 	}
@@ -3375,6 +3379,13 @@ static int __init con_init(void)
 	register_console(&vt_console_driver);
 #endif
 	return 0;
+err_vc_screenbuf:
+	kfree(vc);
+	vc_cons[currcons].d = NULL;
+err_vc:
+	console_unlock();
+	return -ENOMEM;
+
 }
 console_initcall(con_init);
---
Signature · 3 lines
  Best regards,
    Oleksandr Natalenko (post-factum)
    Senior Software Maintenance Engineer