Re: [PATCH] tty: vt: Add checks after calling kzalloc

From: Tetsuo Handa
Date: Wed Aug 31 2022 - 05:43:05 EST


Please check if your system can survive, by doing

On 2022/08/31 18:08, Jiasheng Jiang wrote:
> On Wed, Aug 31, 2022 at 03:57:42PM +0800, Jiasheng Jiang wrote:
>>> for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
>>> vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);

- vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
+ vc_cons[currcons].d = vc = NULL;

>>> + if (!vc) {
>>> + console_unlock();
>>> + return -ENOMEM;
>>> + }
>>> 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);

and/or

- vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
+ vc->vc_screenbuf = NULL;

>>> + if (!vc->vc_screenbuf) {
>>> + console_unlock();
>>> + return -ENOMEM;
>>> + }

in addition to your patch.

This is __init function of built-in module, isn't it?
Who can recover from this allocation failure? ;-)