* [PATCH v2] tty: vt: fix memory leak in vc_allocate()
@ 2026-07-31 2:45 Mingyu Wang
2026-08-03 14:23 ` Greg KH
0 siblings, 1 reply; 3+ messages in thread
From: Mingyu Wang @ 2026-07-31 2:45 UTC (permalink / raw)
To: gregkh, jirislaby; +Cc: linux-kernel, linux-serial, stable, Mingyu Wang
If the screen buffer allocation fails in vc_allocate(), the error handling
path jumps to `err_free`. However, this path fails to release the unicode
screen map attached to `vc->uni_pagedict_loc`.
During the early stages of vc_allocate(), the unicode screen map is either
newly allocated via con_set_default_unimap() or shares the default unicode
map from a previously initialized console (which increments its refcount).
If the subsequent kzalloc() for the screen buffer fails, the err_free path
frees the vc structure but leaves the attached uni_pagedict with an
elevated refcount. This results in an unreferenced object memory leak, as
the reference to the dictionary is lost and its refcount can never reach
zero.
This issue was discovered by DevGen (an automated virtual device modeling
fuzzer based on Syzkaller). During fuzzing with kernel fault injection
(failslab) enabled, the fuzzer forcefully failed the kzalloc() for the
screen buffer, exposing this error-handling path leak.
Fix this by calling con_free_unimap(vc) in the err_free path before
kfree(vc). This safely decrements the refcount and releases the dictionary
memory if this was the last reference.
Fixes: 34902b7f2754 ("tty: vt, get rid of weird source code flow")
Cc: stable@vger.kernel.org
Signed-off-by: Mingyu Wang <25181214217@stu.xidian.edu.cn>
---
Changes in v2:
- Added documentation in the commit message detailing the fuzzing
environment (DevGen/Syzkaller) and the fault injection (failslab)
mechanism used to discover the bug, as requested by Greg KH.
drivers/tty/vt/vt.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
index 8f467b22b799..4bac89ea968a 100644
--- a/drivers/tty/vt/vt.c
+++ b/drivers/tty/vt/vt.c
@@ -1134,6 +1134,7 @@ int vc_allocate(unsigned int currcons) /* return 0 on success */
return 0;
err_free:
visual_deinit(vc);
+ con_free_unimap(vc);
kfree(vc);
vc_cons[currcons].d = NULL;
return err;
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v2] tty: vt: fix memory leak in vc_allocate()
2026-07-31 2:45 [PATCH v2] tty: vt: fix memory leak in vc_allocate() Mingyu Wang
@ 2026-08-03 14:23 ` Greg KH
2026-08-03 14:39 ` Mingyu Wang
0 siblings, 1 reply; 3+ messages in thread
From: Greg KH @ 2026-08-03 14:23 UTC (permalink / raw)
To: Mingyu Wang; +Cc: jirislaby, linux-kernel, linux-serial, stable
On Fri, Jul 31, 2026 at 10:45:50AM +0800, Mingyu Wang wrote:
> If the screen buffer allocation fails in vc_allocate(), the error handling
> path jumps to `err_free`. However, this path fails to release the unicode
> screen map attached to `vc->uni_pagedict_loc`.
>
> During the early stages of vc_allocate(), the unicode screen map is either
> newly allocated via con_set_default_unimap() or shares the default unicode
> map from a previously initialized console (which increments its refcount).
> If the subsequent kzalloc() for the screen buffer fails, the err_free path
> frees the vc structure but leaves the attached uni_pagedict with an
> elevated refcount. This results in an unreferenced object memory leak, as
> the reference to the dictionary is lost and its refcount can never reach
> zero.
>
> This issue was discovered by DevGen (an automated virtual device modeling
> fuzzer based on Syzkaller). During fuzzing with kernel fault injection
> (failslab) enabled, the fuzzer forcefully failed the kzalloc() for the
> screen buffer, exposing this error-handling path leak.
>
> Fix this by calling con_free_unimap(vc) in the err_free path before
> kfree(vc). This safely decrements the refcount and releases the dictionary
> memory if this was the last reference.
>
> Fixes: 34902b7f2754 ("tty: vt, get rid of weird source code flow")
> Cc: stable@vger.kernel.org
> Signed-off-by: Mingyu Wang <25181214217@stu.xidian.edu.cn>
> ---
> Changes in v2:
> - Added documentation in the commit message detailing the fuzzing
> environment (DevGen/Syzkaller) and the fault injection (failslab)
> mechanism used to discover the bug, as requested by Greg KH.
>
> drivers/tty/vt/vt.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
> index 8f467b22b799..4bac89ea968a 100644
> --- a/drivers/tty/vt/vt.c
> +++ b/drivers/tty/vt/vt.c
> @@ -1134,6 +1134,7 @@ int vc_allocate(unsigned int currcons) /* return 0 on success */
> return 0;
> err_free:
> visual_deinit(vc);
> + con_free_unimap(vc);
Shouldn't you be checking vc->uni_pagedict_loc before calling this
function?
Right now we have callers sometimes checking this, and sometimes not,
while the function doesn't seem to care, but we should be consistant,
right? This will only need to be cleaned up if the previous check in
this function was correct.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v2] tty: vt: fix memory leak in vc_allocate()
2026-08-03 14:23 ` Greg KH
@ 2026-08-03 14:39 ` Mingyu Wang
0 siblings, 0 replies; 3+ messages in thread
From: Mingyu Wang @ 2026-08-03 14:39 UTC (permalink / raw)
To: Greg KH; +Cc: jirislaby, linux-kernel, linux-serial, stable
Hi Greg,
> Shouldn't you be checking vc->uni_pagedict_loc before calling this
> function?
>
> Right now we have callers sometimes checking this, and sometimes not,
> while the function doesn't seem to care, but we should be consistant,
> right? This will only need to be cleaned up if the previous check in
> this function was correct.
Thank you for the review.
Yes, the previous check in this function is correct. It acts as a
conditional initialization, allocating the default map only if the VC
did not already inherit one during visual_init().
While con_free_unimap() safely handles NULL internally, I completely
agree that keeping the callers consistent is better for readability
and maintenance.
I will add the explicit check for *vc->uni_pagedict_loc in the
err_free path and send a v3 shortly.
Best regards,
Mingyu Wang
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-08-03 14:39 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-31 2:45 [PATCH v2] tty: vt: fix memory leak in vc_allocate() Mingyu Wang
2026-08-03 14:23 ` Greg KH
2026-08-03 14:39 ` Mingyu Wang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox