Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp1357756ybj; Thu, 7 May 2020 22:40:42 -0700 (PDT) X-Google-Smtp-Source: APiQypLuf2IL+0rIKyFdY/QkexDSoP1xL2v7TZyCuXiW8Hf+AQgnEpfrTNFceL0D4fDrT1nQ2aGk X-Received: by 2002:a17:906:8549:: with SMTP id h9mr431236ejy.145.1588916442737; Thu, 07 May 2020 22:40:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588916442; cv=none; d=google.com; s=arc-20160816; b=aedeLXqzM1Q7t3D/YR5/wFIQ6nkOKL5Je44ZanzlTTrSsaw4apd+A9tbVeFPqIE9WR ROW/3ZXBZuAP5ggooxJXjzp/USE3aeS2XGHJi1sRY3P6RLJ+5Hxokev8Z5ySE23XWx5e zoEzfh1zQBNCwUzv5TQckx58W9q6Z1BMANFJl8HwiTF52t0C87Aba+j7nrx26H5kx36H mESiVzhescW+aCdFyhysUXhAqnleOcePQjPxgYsTROUoQ2vQqSgeHKB7xkzf/7WTPf7J 2/Mnw9l1hcopV8f8i4X8+I2THH3y5EWGmZRvrb/iInp6y7D5Yi3hltnffaKomZCvCZDA KaLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=EMaoPBnsp496f8fnle4PLwTyhTwnaVEb2fZ0BoIlE/s=; b=J/ZcRyx9vjAmQ7WXXI4YZxrQWBIxkSiAYZiXnCQO9fUIc0eHFH/LTeaasOQ50CMgN7 /VrKkyodI0I7/SVGA04WU3pmhyovBZGg+4QUKF7PD2hmb1vYG+qTzbYG15bZ5veG6e5l Thwx2Ln3y/a9PfegPoDbNT8aG5aEIPd9Kie+7ugcM1+6hhhyLljp7m2IdSShZM1bUQqT ylrpUDfBg21gqGiccVs2DDYi9020Xkbz042nBQEkAE5DeiK7z74b6+Xbvq9PtDdXMMzd b7EOxmK70KyExJJaXImQu/Sg3soDgTcqfuBrzh0zxBxCEsJsrRFn/qpGzWXSulol9yo+ lfug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="jZ8/3BzU"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j13si381003edt.371.2020.05.07.22.40.19; Thu, 07 May 2020 22:40:42 -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=@google.com header.s=20161025 header.b="jZ8/3BzU"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726948AbgEHFh4 (ORCPT + 99 others); Fri, 8 May 2020 01:37:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726946AbgEHFgl (ORCPT ); Fri, 8 May 2020 01:36:41 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72A7CC05BD0B for ; Thu, 7 May 2020 22:36:41 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id e2so739330ybm.19 for ; Thu, 07 May 2020 22:36:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=EMaoPBnsp496f8fnle4PLwTyhTwnaVEb2fZ0BoIlE/s=; b=jZ8/3BzUau6j8ZEvbsjxAuGI7hb6DTIvN/+4pZeSwdomRiSNh85BVtF9D8gUSNVzGW 0eWncZr8l/llXRWz7VqOOZykXeBtJ3SKKEDwFOtudoKFQnaFGvJxJmPF2nVefWSW6T1N ujEs8CCMrw76bYxjsNxUwbOVzos4CsPvDRMCd5/PW/oSuWhkpIA3lRzQjco0kInXDbPm 8LmrpY+BMC3pKeUMPrR8rm5su1lRDsLumbV0h+zp0IpapZChomkqsjjuZcY5Cf2QXSOm iDcH2pbz97F5r2NX7V3MQ5S2ooUU3tb01J5+66RrtZtTIz6KF+NJhOkHR6wRY5hgklGo l52A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=EMaoPBnsp496f8fnle4PLwTyhTwnaVEb2fZ0BoIlE/s=; b=EyolPbXUU6+GldUeXyeYECzu4l6eJKdw3rv9zzY7Z4Mdc+MnQz/Zcr2WUZLQ5GQQUR IQfbViavJtkSWd06PRfb5aFINKgLUoY6Qg1BWSDGgJn6xBkl/DXuxxPw53aUm2lCbxEa 47cHyPxl1jofpRu4aCJ1dfYFYaaUAM1Ig8JepOYZVlSvQwMgMqh3ATJmAkxFkcaRh+8z v3PlliGxMDAfoypzgpp1aCcZhxWGMdIuMf6dGsQnu4HjCyebwkQhc8COoPEGCi68H7cr NwDgNbTVhUx/Z1Sn5M7LXurNQBfd7xb/UeJYzNpSENHqI29nPMhuNrmjpYUlyBPyU9s+ Ivtw== X-Gm-Message-State: AGi0PuZ1XnQeRqczXzvjW5N0g7ab8dVx8Rd3BzvcZwyXOVfr+jfDh7zy 5U43owsyw2x1JkfIcqEJpTt2SSAlUA2A X-Received: by 2002:a25:51c1:: with SMTP id f184mr1837043ybb.448.1588916200664; Thu, 07 May 2020 22:36:40 -0700 (PDT) Date: Thu, 7 May 2020 22:36:19 -0700 In-Reply-To: <20200508053629.210324-1-irogers@google.com> Message-Id: <20200508053629.210324-5-irogers@google.com> Mime-Version: 1.0 References: <20200508053629.210324-1-irogers@google.com> X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog Subject: [RFC PATCH v3 04/14] libbpf: Fix memory leak and possible double-free in hashmap__clear From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , John Fastabend , KP Singh , Kajol Jain , Andi Kleen , John Garry , Jin Yao , Kan Liang , Cong Wang , Kim Phillips , linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, Vince Weaver , Stephane Eranian , Alston Tang , Ian Rogers Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrii Nakryiko Fix memory leak in hashmap_clear() not freeing hashmap_entry structs for each of the remaining entries. Also NULL-out bucket list to prevent possible double-free between hashmap__clear() and hashmap__free(). Running test_progs-asan flavor clearly showed this problem. Reported-by: Alston Tang Signed-off-by: Andrii Nakryiko Signed-off-by: Alexei Starovoitov Link: https://lore.kernel.org/bpf/20200429012111.277390-5-andriin@fb.com Signed-off-by: Ian Rogers --- tools/lib/bpf/hashmap.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/lib/bpf/hashmap.c b/tools/lib/bpf/hashmap.c index 54c30c802070..cffb96202e0d 100644 --- a/tools/lib/bpf/hashmap.c +++ b/tools/lib/bpf/hashmap.c @@ -59,7 +59,14 @@ struct hashmap *hashmap__new(hashmap_hash_fn hash_fn, void hashmap__clear(struct hashmap *map) { + struct hashmap_entry *cur, *tmp; + int bkt; + + hashmap__for_each_entry_safe(map, cur, tmp, bkt) { + free(cur); + } free(map->buckets); + map->buckets = NULL; map->cap = map->cap_bits = map->sz = 0; } -- 2.26.2.645.ge9eca65c58-goog