bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Daniel Borkmann <daniel@iogearbox.net>
To: Gaurav Singh <gaurav1086@gmail.com>,
	Alexei Starovoitov <ast@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>,
	Jesper Dangaard Brouer <hawk@kernel.org>,
	John Fastabend <john.fastabend@gmail.com>,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Yonghong Song <yhs@fb.com>, Andrii Nakryiko <andriin@fb.com>,
	KP Singh <kpsingh@chromium.org>,
	"open list:XDP (eXpress Data Path)" <netdev@vger.kernel.org>,
	"open list:XDP (eXpress Data Path)" <bpf@vger.kernel.org>,
	open list <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] xdp_rxq_info_user: Add null check after malloc
Date: Thu, 11 Jun 2020 16:12:51 +0200	[thread overview]
Message-ID: <de636159-a33e-cfa9-0d8f-e9c81873588f@iogearbox.net> (raw)
In-Reply-To: <20200610030145.17263-1-gaurav1086@gmail.com>

On 6/10/20 5:01 AM, Gaurav Singh wrote:
> Signed-off-by: Gaurav Singh <gaurav1086@gmail.com>
> 
> The memset call is made right after malloc call which
> can return a NULL pointer upon failure causing a
> segmentation fault. Fix this by adding a null check
> right after malloc() and then do memset().

The SoB should come after the commit message here.

> ---
>   samples/bpf/xdp_rxq_info_user.c | 6 +++---
>   1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/samples/bpf/xdp_rxq_info_user.c b/samples/bpf/xdp_rxq_info_user.c
> index 4fe47502ebed..2d03c84a4cca 100644
> --- a/samples/bpf/xdp_rxq_info_user.c
> +++ b/samples/bpf/xdp_rxq_info_user.c
> @@ -202,11 +202,11 @@ static struct datarec *alloc_record_per_cpu(void)
>   
>   	size = sizeof(struct datarec) * nr_cpus;
>   	array = malloc(size);
> -	memset(array, 0, size);

All these below are candidates for calloc(), can we just use that instead and
simplify the below.

>   	if (!array) {
>   		fprintf(stderr, "Mem alloc error (nr_cpus:%u)\n", nr_cpus);
>   		exit(EXIT_FAIL_MEM);
>   	}
> +	memset(array, 0, size);
>   	return array;
>   }
>   
> @@ -218,11 +218,11 @@ static struct record *alloc_record_per_rxq(void)
>   
>   	size = sizeof(struct record) * nr_rxqs;
>   	array = malloc(size);
> -	memset(array, 0, size);
>   	if (!array) {
>   		fprintf(stderr, "Mem alloc error (nr_rxqs:%u)\n", nr_rxqs);
>   		exit(EXIT_FAIL_MEM);
>   	}
> +	memset(array, 0, size);
>   	return array;
>   }
>   
> @@ -233,11 +233,11 @@ static struct stats_record *alloc_stats_record(void)
>   	int i;
>   
>   	rec = malloc(sizeof(*rec));
> -	memset(rec, 0, sizeof(*rec));
>   	if (!rec) {
>   		fprintf(stderr, "Mem alloc error\n");
>   		exit(EXIT_FAIL_MEM);
>   	}
> +	memset(rec, 0, sizeof(*rec));
>   	rec->rxq = alloc_record_per_rxq();
>   	for (i = 0; i < nr_rxqs; i++)
>   		rec->rxq[i].cpu = alloc_record_per_cpu();
> 

Thanks,
Daniel

      reply	other threads:[~2020-06-11 14:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-10  3:01 [PATCH] xdp_rxq_info_user: Add null check after malloc Gaurav Singh
2020-06-11 14:12 ` Daniel Borkmann [this message]

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=de636159-a33e-cfa9-0d8f-e9c81873588f@iogearbox.net \
    --to=daniel@iogearbox.net \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=davem@davemloft.net \
    --cc=gaurav1086@gmail.com \
    --cc=hawk@kernel.org \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@chromium.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=yhs@fb.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).