mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jiri Slaby <jirislaby@kernel.org>
To: Jiasheng Jiang <jiasheng@iscas.ac.cn>,
	gregkh@linuxfoundation.org, jcmvbkbc@gmail.com, dsterba@suse.com,
	johan@kernel.org, dankamongmen@gmail.com,
	penguin-kernel@I-love.SAKURA.ne.jp, igormtorrente@gmail.com
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH] tty: vt: Check for NULL pointer after calling kzalloc
Date: Fri, 21 Jan 2022 08:01:28 +0100	[thread overview]
Message-ID: <716113c0-13d6-6fbc-f1a0-9fea6091bb90@kernel.org> (raw)
In-Reply-To: <20220121065320.2283544-1-jiasheng@iscas.ac.cn>

On 21. 01. 22, 7:53, Jiasheng Jiang wrote:
> As the potential failure of the allocation, the kzalloc() will return
> NULL pointer.
> Therefore, it should be better to check it in order to avoid the
> dereference of the NULL pointer.
> When it fails, we should free all the allocated memory and return error
> number.
> To make the code more clear, I use the 'err_free', like how
> vc_allocate() deals with the allocation failure.

https://lore.kernel.org/all/X+CRTqDQwVYSZQBi@kroah.com/

> Fixes: a5f4f52e8211 ("vt: use kzalloc() instead of the bootmem allocator")
> Signed-off-by: Jiasheng Jiang <jiasheng@iscas.ac.cn>
> ---
>   drivers/tty/vt/vt.c | 19 +++++++++++++++++++
>   1 file changed, 19 insertions(+)
> 
> diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
> index f8c87c4d7399..343fa6fffc18 100644
> --- a/drivers/tty/vt/vt.c
> +++ b/drivers/tty/vt/vt.c
> @@ -3519,11 +3519,17 @@ 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_free;
> +
>   		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);
> +		if (!vc->vc_screenbuf)
> +			goto err_free_vc;
> +
>   		vc_init(vc, vc->vc_rows, vc->vc_cols,
>   			currcons || !vc->vc_sw->con_save_screen);
>   	}
> @@ -3545,6 +3551,19 @@ static int __init con_init(void)
>   	register_console(&vt_console_driver);
>   #endif
>   	return 0;
> +err_free_vc:
> +	visual_deinit(vc);
> +	kfree(vc);
> +	vc_cons[currcons].d = NULL;
> +err_free:
> +	for (i = 0; i < currcons; i++) {
> +		kfree(vc_cons[currcons].d->vc_screenbuf);
> +		visual_deinit(vc_cons[currcons].d);
> +		kfree(vc_cons[currcons].d);
> +		vc_cons[currcons].d = NULL;
> +	}
> +	console_unlock();
> +	return -ENOMEM;
>   }
>   console_initcall(con_init);
>   


-- 
js

  reply	other threads:[~2022-01-21  7:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-21  6:53 Jiasheng Jiang
2022-01-21  7:01 ` Jiri Slaby [this message]
2022-01-21  7:31 ` Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=716113c0-13d6-6fbc-f1a0-9fea6091bb90@kernel.org \
    --to=jirislaby@kernel.org \
    --cc=dankamongmen@gmail.com \
    --cc=dsterba@suse.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=igormtorrente@gmail.com \
    --cc=jcmvbkbc@gmail.com \
    --cc=jiasheng@iscas.ac.cn \
    --cc=johan@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=penguin-kernel@I-love.SAKURA.ne.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®