All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg KH <gregkh@linuxfoundation.org>
To: Xiaoming Ni <nixiaoming@huawei.com>
Cc: penberg@cs.helsinki.fi, jslaby@suse.com, nico@fluxnic.net,
	textshell@uchuujin.de, sam@ravnborg.org, daniel.vetter@ffwll.ch,
	mpatocka@redhat.com, ghalat@redhat.com,
	linux-kernel@vger.kernel.org, yangyingliang@huawei.com,
	yuehaibing@huawei.com, zengweilin@huawei.com
Subject: Re: [PATCH] tty:vt: Add check the return value of kzalloc to avoid oops
Date: Thu, 19 Sep 2019 11:29:33 +0200	[thread overview]
Message-ID: <20190919092933.GA2684163@kroah.com> (raw)
In-Reply-To: <1568884695-56789-1-git-send-email-nixiaoming@huawei.com>

On Thu, Sep 19, 2019 at 05:18:15PM +0800, Xiaoming Ni wrote:
> Using kzalloc() to allocate memory in function con_init(), but not
> checking the return value, there is a risk of null pointer references
> oops.
> 
> Signed-off-by: Xiaoming Ni <nixiaoming@huawei.com>

We keep having this be "reported" :(

> ---
>  drivers/tty/vt/vt.c | 18 ++++++++++++++++++
>  1 file changed, 18 insertions(+)
> 
> diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
> index 34aa39d..db83e52 100644
> --- a/drivers/tty/vt/vt.c
> +++ b/drivers/tty/vt/vt.c
> @@ -3357,15 +3357,33 @@ 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 (unlikely(!vc)) {
> +			pr_warn("%s:failed to allocate memory for the %u vc\n",
> +					__func__, currcons);
> +			break;
> +		}

At init, this really can not happen.  Have you see it ever happen?

>  		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 (unlikely(!vc->vc_screenbuf)) {

Never use likely/unlikely unless you can actually measure the speed
difference.  For something like this, the compiler will always get it
right without you having to do anything.

And again, how can this fail?  Have you seen it fail?

> +			pr_warn("%s:failed to allocate memory for the %u vc_screenbuf\n",
> +					__func__, currcons);
> +			visual_deinit(vc);
> +			tty_port_destroy(&vc->port);
> +			kfree(vc);
> +			vc_cons[currcons].d = NULL;
> +			break;
> +		}
>  		vc_init(vc, vc->vc_rows, vc->vc_cols,
>  			currcons || !vc->vc_sw->con_save_screen);
>  	}
>  	currcons = fg_console = 0;
>  	master_display_fg = vc = vc_cons[currcons].d;
> +	if (unlikely(!vc)) {

Again, never use likely/unlikely unless you can measure it.

thanks,

greg k-h

  reply	other threads:[~2019-09-19  9:29 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-19  9:18 [PATCH] tty:vt: Add check the return value of kzalloc to avoid oops Xiaoming Ni
2019-09-19  9:29 ` Greg KH [this message]
2019-09-19 15:16   ` Mikulas Patocka
2019-09-20  2:29   ` Nixiaoming
2019-09-20  6:04     ` Greg KH
2019-09-20  2:56   ` Nicolas Pitre
2019-09-20  6:04     ` Greg KH
2019-09-21  7:26       ` Xiaoming Ni
2019-09-23  3:50         ` Nicolas Pitre
2019-09-25  8:37           ` Xiaoming Ni

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=20190919092933.GA2684163@kroah.com \
    --to=gregkh@linuxfoundation.org \
    --cc=daniel.vetter@ffwll.ch \
    --cc=ghalat@redhat.com \
    --cc=jslaby@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mpatocka@redhat.com \
    --cc=nico@fluxnic.net \
    --cc=nixiaoming@huawei.com \
    --cc=penberg@cs.helsinki.fi \
    --cc=sam@ravnborg.org \
    --cc=textshell@uchuujin.de \
    --cc=yangyingliang@huawei.com \
    --cc=yuehaibing@huawei.com \
    --cc=zengweilin@huawei.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.