Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp647174ybk; Fri, 15 May 2020 09:54:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxq8NXLIdEjzDKOjnYToYaxWGmcQRVUoiYfwlT4k8/tRlLs3yPVve1ON4Cdo14qBethXifR X-Received: by 2002:aa7:d650:: with SMTP id v16mr3573702edr.267.1589561647059; Fri, 15 May 2020 09:54:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589561647; cv=none; d=google.com; s=arc-20160816; b=GjN5kDa7wlnkOQgFDDb4DLwh5WFOxBnII+BpWqduyBbQ5AjTvO/tWdDHzXEBhHOXgB 7+Xqt6A0gvunLDlLhQ1PGAUAZmLTmzyG1uEi3P+H9fsAMYhl2XOrbTXpzrvyXM8kr+LX z35S3UlZF3sfSBjfJ27v5PoHBUT4dVykDgebIbSjZE6QDmtGa538pnqHpkhhy3CQu11x jK6J/PP65QhZp1HeIHv91eHEOPaHAM7TPHv5dwmXcFsyQfo2wSMG/Zz9AzFREBtalwWE 25MqMF7/8A5mVuHfMkju+kcbEYUVx7LLI+lUm8ERNeH9Ty9SSl9c1dJsbQSxIY43Ydkl pu/A== 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=tvl7gLFsS/dAedcEqfrKtncclZsMityBUyr8g2OugcQ=; b=R/OCnWUlex3mf5KVLij9yycgZY1adEz8EvUiVF6mDRfyd1u5MgcqvWus4N07D4rD3v u4x+xRmb/2nutoT1VSPlaOGEYt3Apf9AbLiUHGfSs4SUjEwR7Z8fWyo3GnTvpglyajnF Lnylyw6QiWLM0QyFxgK/Qy43KZ9Uv6cbeRtxuw4Yj40JarGJ1beRPXkHBn+vQ8O+/Em6 KeMziRPwQWzmg7N2zUuWJ6CaSWTk11JZIbuGeOzaykJGQ83CVUCYAcJuq82lDhl9g1EC hxd8/GwzIQWW8X6n/fPdtuiJdH+GbLwTuwgANBdKCebJH9npcry4PhJD0onDKGF1fK2+ HZhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=OGopoj+E; 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 si17si1569998ejb.288.2020.05.15.09.53.43; Fri, 15 May 2020 09:54:07 -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=OGopoj+E; 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 S1726521AbgEOQuR (ORCPT + 99 others); Fri, 15 May 2020 12:50:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726023AbgEOQuP (ORCPT ); Fri, 15 May 2020 12:50:15 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33C2AC061A0C for ; Fri, 15 May 2020 09:50:15 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id l4so2874215qke.2 for ; Fri, 15 May 2020 09:50:15 -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=tvl7gLFsS/dAedcEqfrKtncclZsMityBUyr8g2OugcQ=; b=OGopoj+ElQmzcTdGxoabqS9eF87VZHL+YKcn8EL4MNkN1sf7PKD0S2cQHJu4CCOCWV /5Sq2Mu8iguP210lceBZY1OO7RJ7CkYhrHtqn0BnSF1j3XS36C+L2n4P+PjRx7R31Nch 3h/5QgSoVrdR+N2Xw2zQ87s8q5TAMqdL9PQ3bsgB8plIwGFcvz7ILoCxnI8yG2BJOoJC ueRDeyh/REt1PRHNocvBh9oMcPpFMAOXMuWkysXsgYYA29hR0F6TOtUhwG56cx1qxdwp nietz7S0Q8Bo1TU6F0n5gdf4Md+m2AuEMuP0aaUaIqLEXvF9Fv3EsaeMFxNZCP/YTICg q1+w== 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=tvl7gLFsS/dAedcEqfrKtncclZsMityBUyr8g2OugcQ=; b=De4v7x1XQotGF6gevISxyJq9DlIj6XFUSGNCF4Px3KtV8hij9UcFG2Ra9nk0ntOArb qoxZnfD3i9plgY1Xc07TSwJTAR3xbB+8vG1Y9UxdvRoQj4ijEfuSEl4/98zLUM5PJl7I 6LngxB1ri4tTN8HlUVkqzqDvkntQUG0lgfCPWgHh08n0KAYk9PwsvFMYPCv2NkeB7QSU j+z1mDwSOWghWhprs7nekJKe6cYJzmhRVNsOrpG3HpdfkK9cRHoJbwdLon4bGjqhmBzZ wgQ48ze0j9lqkwLoc6FM47yUnEZOgSArRn+QrJP5hMckABTgtOkHeIhuZx/SlZFRoYNy tgLA== X-Gm-Message-State: AOAM533kOnDl3oTu9rtk/0E7xcah2E6Yqws6fhmFJ3nOBzft+0UInMpL u/nVY3iyDOhq/aNnzmcvA2GB+8nAafGB X-Received: by 2002:a05:6214:164:: with SMTP id y4mr4179863qvs.249.1589561414334; Fri, 15 May 2020 09:50:14 -0700 (PDT) Date: Fri, 15 May 2020 09:50:01 -0700 In-Reply-To: <20200515165007.217120-1-irogers@google.com> Message-Id: <20200515165007.217120-2-irogers@google.com> Mime-Version: 1.0 References: <20200515165007.217120-1-irogers@google.com> X-Mailer: git-send-email 2.26.2.761.g0e0b3e54be-goog Subject: [PATCH v2 1/7] 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 , Adrian Hunter , Leo Yan , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Cc: 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.761.g0e0b3e54be-goog