Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp1334613ybg; Thu, 11 Jun 2020 07:15:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy3wK/opTmuVmDQLk30puRYJdEfTpEtEnOTz08B4N7GX9/j9X5pGyDmGfA1HU9z5d3Mqj/s X-Received: by 2002:a17:906:3951:: with SMTP id g17mr8876925eje.414.1591884950902; Thu, 11 Jun 2020 07:15:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591884950; cv=none; d=google.com; s=arc-20160816; b=nqWnBzCBFCdx+QfvF/ZT6rk6TpqDwMd4apeDdJ/MCkFcP7jTnmTOYWqFYoAtxTlsoC X7xC1WNqJik9w6nYMI9teNcqa9QtCoKFOkN/xjcAHiILomDGzgk1lp+NcV60oItS2Ecl NzrXGM6i8Z5NjbXzxKzjV02gJul+mHDWoqAPWPBpUPlzYEUPx5fDUY4DUR4MVoGwWHED fgrwqP5AYmCnKmY34QDwSKkcMBwc7FszJhtglidbzixXE7oAJjzE/xUcE+1r5Y+wjfBS VJlt82ZTJYKQtNMq+B8sCMoKLM1hXwX+UkOOn0t+dNNEKBRcZ5W/DhLakr+gKWhSiU1q MWMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:to:subject; bh=G58M9yguwYjDrwyaJ+I6pYeg1OsZEBLV7+3ntCmtqy4=; b=zLyflI9l4R3ty7Qu1qkt3jrzo7wFTgtHszTpoKphIswfauGeSlsYFWvb+E7tnrbsMS bgqBMcZeJ2ttBXQ4LpRJ4SiaIn1d16hyCNmknTyp/FR+OB85+eiBXGq+ffCSGT+xqjwn Ju/4y+NKoikAm1tfA/+Qf+Bwvnhw2fhu9eJUuSQF5pzRMm3FkZpKOgwFQa9R82I352vl ALSFA+F/MBzXnAd4Hta0/NnUcDDysLbtDncuKCVzF9cBSU0X9AQ1mjtkd4AB5QRiLFoO 1lmNO9IOMPl2CnLOuaydjlBpHg+nTJwlCeQqpMS3heQR0naZMapLWb1Nub+45H0M3VBw g++A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dn12si2719057ejc.287.2020.06.11.07.15.26; Thu, 11 Jun 2020 07:15:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728039AbgFKONL (ORCPT + 99 others); Thu, 11 Jun 2020 10:13:11 -0400 Received: from www62.your-server.de ([213.133.104.62]:55442 "EHLO www62.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727768AbgFKONJ (ORCPT ); Thu, 11 Jun 2020 10:13:09 -0400 Received: from sslproxy02.your-server.de ([78.47.166.47]) by www62.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89_1) (envelope-from ) id 1jjNwr-0006SL-06; Thu, 11 Jun 2020 16:12:53 +0200 Received: from [178.196.57.75] (helo=pc-9.home) by sslproxy02.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jjNwq-000TE2-HS; Thu, 11 Jun 2020 16:12:52 +0200 Subject: Re: [PATCH] xdp_rxq_info_user: Add null check after malloc To: Gaurav Singh , Alexei Starovoitov , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , John Fastabend , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , KP Singh , "open list:XDP (eXpress Data Path)" , "open list:XDP (eXpress Data Path)" , open list References: <20200610030145.17263-1-gaurav1086@gmail.com> From: Daniel Borkmann Message-ID: Date: Thu, 11 Jun 2020 16:12:51 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <20200610030145.17263-1-gaurav1086@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.102.3/25840/Thu Jun 11 14:52:31 2020) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 6/10/20 5:01 AM, Gaurav Singh wrote: > Signed-off-by: Gaurav Singh > > 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