From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 22D21C433E1 for ; Tue, 16 Jun 2020 13:10:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ED74120FC3 for ; Tue, 16 Jun 2020 13:10:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728798AbgFPNKe (ORCPT ); Tue, 16 Jun 2020 09:10:34 -0400 Received: from www62.your-server.de ([213.133.104.62]:45184 "EHLO www62.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726306AbgFPNKe (ORCPT ); Tue, 16 Jun 2020 09:10:34 -0400 Received: from sslproxy05.your-server.de ([78.46.172.2]) by www62.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89_1) (envelope-from ) id 1jlBM0-0004aN-8w; Tue, 16 Jun 2020 15:10:16 +0200 Received: from [178.196.57.75] (helo=pc-9.home) by sslproxy05.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jlBLz-0004ar-ON; Tue, 16 Jun 2020 15:10:15 +0200 Subject: Re: [PATCH] [bpf] xdp_redirect_cpu_user: Fix null pointer dereference To: Gaurav Singh , Alexei Starovoitov , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , John Fastabend , KP Singh , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , "open list:BPF (Safe dynamic programs and tools)" , "open list:BPF (Safe dynamic programs and tools)" , open list References: <20200614190434.31321-1-gaurav1086@gmail.com> From: Daniel Borkmann Message-ID: Date: Tue, 16 Jun 2020 15:10:14 +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: <20200614190434.31321-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/25844/Mon Jun 15 15:06:22 2020) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On 6/14/20 9:04 PM, Gaurav Singh wrote: > Memset() on the pointer right after malloc() can cause > a null pointer dereference if it failed to allocate memory. > Fix this by replacing malloc/memset with a single calloc(). > > Signed-off-by: Gaurav Singh Squashed all three same fixes into one and pushed to bpf, thanks! > @@ -222,11 +219,9 @@ static struct datarec *alloc_record_per_cpu(void) > static struct stats_record *alloc_stats_record(void) > { > struct stats_record *rec; > - int i, size; > + int i; > > - size = sizeof(*rec) + n_cpus * sizeof(struct record); > - rec = malloc(size); > - memset(rec, 0, size); > + rec = calloc(n_cpus + 1, sizeof(struct record)); For the record, this one is buggy, so I fixed it up as well. > if (!rec) { > fprintf(stderr, "Mem alloc error\n"); > exit(EXIT_FAIL_MEM); > Thanks, Daniel