Re: [PATCH] vt: Fix a missing-check bug in drivers/tty/vt/vt.c file of Linux 5.0.14

From: Gen Zhang
Date: Mon May 13 2019 - 05:39:23 EST


On Mon, May 13, 2019 at 09:36:19AM +0200, Greg KH wrote:
> > Signed-off-by: Gen Zhang <blackgod016574@xxxxxxxxx>
> > ---
> > --- drivers/tty/vt/vt.c
> > +++ drivers/tty/vt/vt.c
> > @@ -3349,10 +3349,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;
>
> What about the other memory that was allocated? You never free that.
>
> > 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;
>
> Same here, you are now leaking memory.
>
> Did you test this patch out with a kmalloc function that can fail? If
> not, please try to do so.
>
> thanks,
>
> greg k-h
Hi, Greg
1. I re-examined the source code.
For vc_cons[currcons].d and vc allocation fail, we may need to free
vc->vc_screenbuf from the previous loop. So kfree(vc->vc_screenbuf)
need to be added to err_vc;
As for vc->vc_screenbuf allocation fail, I don't think there is other
memory need to be freed. Because in function con_init, there's no other
allocation operations except this two kzalloc functions. And in
err_vc_screenbuf, vc_cons[currcons].d and vc is freed in the patch.

2. I tried to test this patch with a compiled kernel in QEMU but
failed. Testing this is out of my skills. So is there any other ways
to test this patch?
Thanks
Gen