Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp2236271ybt; Tue, 16 Jun 2020 00:08:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzuZkMeq3Jcc0oBWqf2Azkm5et1QiOoZwEw6ilRXL2B9oj7xf0Z9G7genQCyrmrIbvot1t6 X-Received: by 2002:aa7:c486:: with SMTP id m6mr1336910edq.234.1592291299407; Tue, 16 Jun 2020 00:08:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592291299; cv=none; d=google.com; s=arc-20160816; b=QZR1O00O6euEh61zMdmrQDTV9SnKewZgF+/omtB8C36OGX3LJv46bmZzffeO6UgcPw hj9kT4jjr+BEyfyOm9klFpd96BgxaIV5Bojo5O0U4YiughVrWrFOABEtE29e6UmQ+udd TnfCG0fH8KtLDa/WDfESk8s1YW/fVQWcmD2PaP849MXtVF3JBoyqZ+tVq8yTPcCFuQ5c qbes4+twAtIqpncRg80u1sWa5cGrpknPp9is42fse4y45oDmQi34Gzju768nedhP/Cn0 ryXOnYDAtgTsd+pK//fHRsNqynXv/G049eFThgA43tXC6ni8+fs65ZCuyan9sSwJcWez wjxA== 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:mime-version :subject:references:in-reply-to:message-id:to:from:date :dkim-signature; bh=7UYkh8B3Fgzwx1rQ3OeB4igDdNX76egtj7f9JC6lupI=; b=P8A/G2M3qHWWEE8w/ij5qOGYiB+FmTBy9Q2UCHlSbdbXHPbIizTVjLkKWTN3jzHtfG XNUcJ9Cet1j98dI0eWtjL9qk1UhCFAJMDNz/QOw0TN/ehUiGGFI9kPxK/wMvlz5nMsaL b2csIjH4jiUm8yv7q0H/SFwXNPbVIwqtOA+GYH6te2R1bm9Yy184V9DWF9RNU1IGcd7Q QCte5n1MLx4pFIiYzLvpMghIBbYBUDHnoSX8OSZK9ntpyTZ2ZkqDGjsYhDLIo8e7tJzF e0jOVH9yKT61M6rSdoWtRcq8ocXTXR1T2TGt9xluSGwdd6738aNh6Pr03VUPvPCxeVW3 PYfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=OYYMuveh; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m3si9354371edi.593.2020.06.16.00.07.56; Tue, 16 Jun 2020 00:08:19 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=OYYMuveh; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726398AbgFPHGH (ORCPT + 99 others); Tue, 16 Jun 2020 03:06:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725710AbgFPHGG (ORCPT ); Tue, 16 Jun 2020 03:06:06 -0400 Received: from mail-io1-xd41.google.com (mail-io1-xd41.google.com [IPv6:2607:f8b0:4864:20::d41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A97DDC05BD43; Tue, 16 Jun 2020 00:06:06 -0700 (PDT) Received: by mail-io1-xd41.google.com with SMTP id i25so434456iog.0; Tue, 16 Jun 2020 00:06:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:message-id:in-reply-to:references:subject:mime-version :content-transfer-encoding; bh=7UYkh8B3Fgzwx1rQ3OeB4igDdNX76egtj7f9JC6lupI=; b=OYYMuvehv/qPMmliSCDYK5QkafZl2sPnKeKmHj8GLN1GKJykOCKee0EQFmDR4rhhyr Hu4tnAFyWvABeM2j9ATpoj8htmFRFD9XufGunYw+0cZDPg6IN3Wnwq0iThzuXxF6YgrM F7IUv0QoOzG5l7kzjaF+ucbyQg3MGv9KvXrqJuzV62Et0VnBusvmq6qWozV4ULNHZUe+ fkKXal0X5pFSA5UshG6yRt3dVjTWL/6twoZliH/AfuJJ6ds+I6Xibo9+jcdqFzhDPMJV hfz0/W3ZE/BkgfnjbWOL0xK2nFN68O8DjMpJa9C7Q4GsZ9xenEKVBQ5znwUkdcNWn48w kkpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:message-id:in-reply-to:references :subject:mime-version:content-transfer-encoding; bh=7UYkh8B3Fgzwx1rQ3OeB4igDdNX76egtj7f9JC6lupI=; b=WfrX7JnNNj9wpk9oqQ6selDeM3t9FyxkylvZ5ZqBQw6gpSDVIuL5INq8kFT2Gmszpt fyR6WF71eCxz7GbJn/wBa/HoHorQdnX2tnh5Id1Aiuw9qZ2gIbN2B9Uqirx9494vMDTi q2OdEmupFzqm7ISBrmk3IG00M67VGh8C1+a2QvYitDPXodbV98R6bs/zeYf29+c/3OJz wwUcZlwQSQPpKqGuU2brquu1UVKKTTO7l8bDj85pMJPyM5FC/SVu2EIDKqwKZkJDC3ud 9NaEGRzgeI3g3wSaA8qNWTypvkJ0OcAQUARwYugDwxgeDL2mCdDIi9HLWEa3MylT2Yoc C8KQ== X-Gm-Message-State: AOAM533Ksg7EQBdOBM5OepH9klMYngxREsPpNoU+qihiesOzpvrWch+7 8gKJbHF+b2ma3FuGTifrOes= X-Received: by 2002:a5e:d90c:: with SMTP id n12mr1611376iop.144.1592291165839; Tue, 16 Jun 2020 00:06:05 -0700 (PDT) Received: from localhost ([184.63.162.180]) by smtp.gmail.com with ESMTPSA id j80sm9681504ili.65.2020.06.16.00.06.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jun 2020 00:06:05 -0700 (PDT) Date: Tue, 16 Jun 2020 00:05:58 -0700 From: John Fastabend To: Gaurav Singh , gaurav1086@gmail.com, Alexei Starovoitov , Daniel Borkmann , 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\\))" , bpf@vger.kernel.org (open list:BPF \(Safe dynamic programs and tools\)), "(open list:BPF \\(Safe dynamic programs and tools\\) open list)" Message-ID: <5ee86f561cff7_4be02ab1b668a5b4e2@john-XPS-13-9370.notmuch> In-Reply-To: <20200614184102.30992-1-gaurav1086@gmail.com> References: <20200614184102.30992-1-gaurav1086@gmail.com> Subject: RE: [PATCH] [bpf] xdp_monitor_user: Fix null pointer dereference Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 > --- > samples/bpf/xdp_monitor_user.c | 8 ++------ > 1 file changed, 2 insertions(+), 6 deletions(-) > > diff --git a/samples/bpf/xdp_monitor_user.c b/samples/bpf/xdp_monitor_user.c > index dd558cbb2309..ef53b93db573 100644 > --- a/samples/bpf/xdp_monitor_user.c > +++ b/samples/bpf/xdp_monitor_user.c > @@ -509,11 +509,8 @@ static void *alloc_rec_per_cpu(int record_size) > { > unsigned int nr_cpus = bpf_num_possible_cpus(); > void *array; > - size_t size; > > - size = record_size * nr_cpus; > - array = malloc(size); > - memset(array, 0, size); > + array = calloc(nr_cpus, record_size); > if (!array) { > fprintf(stderr, "Mem alloc error (nr_cpus:%u)\n", nr_cpus); > exit(EXIT_FAIL_MEM); > @@ -528,8 +525,7 @@ static struct stats_record *alloc_stats_record(void) > int i; > > /* Alloc main stats_record structure */ > - rec = malloc(sizeof(*rec)); > - memset(rec, 0, sizeof(*rec)); > + rec = calloc(1, sizeof(*rec)); > if (!rec) { > fprintf(stderr, "Mem alloc error\n"); > exit(EXIT_FAIL_MEM); > -- > 2.17.1 > Acked-by: John Fastabend