netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andriin@fb.com>
To: <bpf@vger.kernel.org>, <netdev@vger.kernel.org>, <ast@fb.com>,
	<daniel@iogearbox.net>
Cc: <andrii.nakryiko@gmail.com>, <kernel-team@fb.com>,
	Andrii Nakryiko <andriin@fb.com>, Alston Tang <alston64@fb.com>
Subject: [PATCH bpf-next 4/6] libbpf: fix memory leak and possible double-free in hashmap__clear
Date: Mon, 27 Apr 2020 21:46:26 -0700	[thread overview]
Message-ID: <20200428044628.3772114-5-andriin@fb.com> (raw)
In-Reply-To: <20200428044628.3772114-1-andriin@fb.com>

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.

Cc: Alston Tang <alston64@fb.com>
Reported-by: Alston Tang <alston64@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 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.24.1


  parent reply	other threads:[~2020-04-28  4:51 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-28  4:46 [PATCH bpf-next 0/6] Add ASAN to selftest and fix found problems Andrii Nakryiko
2020-04-28  4:46 ` [PATCH bpf-next 1/6] selftests/bpf: ensure test flavors use correct skeletons Andrii Nakryiko
2020-04-28  4:46 ` [PATCH bpf-next 2/6] selftests/bpf: add test_progs-asan flavor with AddressSantizer Andrii Nakryiko
2020-04-28 16:44   ` Alexei Starovoitov
2020-04-28 18:35     ` Andrii Nakryiko
2020-04-28 20:41       ` Alexei Starovoitov
2020-04-28 22:13         ` Andrii Nakryiko
2020-04-28  4:46 ` [PATCH bpf-next 3/6] selftests/bpf: convert test_hashmap into test_progs test Andrii Nakryiko
2020-04-28  4:46 ` Andrii Nakryiko [this message]
2020-04-28  4:46 ` [PATCH bpf-next 5/6] selftests/bpf: fix memory leak in test selector Andrii Nakryiko
2020-04-28  4:46 ` [PATCH bpf-next 6/6] selftests/bpf: fix memory leak in extract_build_id() Andrii Nakryiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200428044628.3772114-5-andriin@fb.com \
    --to=andriin@fb.com \
    --cc=alston64@fb.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@fb.com \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).