Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp119450ybg; Mon, 8 Jun 2020 18:09:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzbrONsB7S9VWkA5y0nm7C4a/ABjlprIoPFNrqisLOCeP5yE17MqFJolX2vMKNLhJt/sh1/ X-Received: by 2002:a17:906:3c8:: with SMTP id c8mr23600428eja.129.1591664953617; Mon, 08 Jun 2020 18:09:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591664953; cv=none; d=google.com; s=arc-20160816; b=v3gfS2eR9/a5RT6aiZ+56WHVWd1pf/ITi+za8zU5FA8ZriLW9ViXLc/cL9xJC6BNW8 opgfttC25dyxAa31gR/UPLRCoQ1xulrOxxgDe9aG2OJsbP3JJDgt+1Xr/lclYpUWF8H2 eK8jSIgNLQ/PsiP/ZwOYcy4DzbBf4vy2DLqqgGBqZN3EsRj303oO0MQWjA6T4+PenyK1 3l+ac0axfoqJtrvgK0xKYV1rPrHgdCQvq8MmVyN7DnYGDI23bQ9D2liKcWmESYH9We2u f9tSFvqvY5QHmDzk+KN8+6T1wdx7fQoEknjft+XQBkgFs09XOjyl8/tOtQHwXu1i31SD ER7g== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=5NbtiMejWtiyOZfCZcUoH4dAuruQD+q+loZChCx9228=; b=eHoZUELx8C55sAjnL3qevlMyrjOBknOA//0ZvAqwQWYDpSmlRylqBkO1qXJr/ehOZP 3tWEyWkZGwWfw9MDcfzJZKM1BoXu82WVYdsHu5PGmz+oi6XBI+KNIAiY94/QLbS9d1u8 BBREC3Tw+spwct4Xk2lmqi/iD7ZdnPiELIL+QrRwlj+Yg1hH6Dvj80P/kd0DQELrUylw YtI6I3kEyCVENHbFidzTTtJUgqzA2ouNNZME8u9sicNTPprzOovOqWjjPZtipBUDRQaF sWAJCYRWiOgTTJgXp+NfWyLeJvImaRc8y6X69LJrBW2sATQw7M5HcqNIsrAXJhQ9skbP +0DA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=To50O7H1; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r22si9666737ejs.584.2020.06.08.18.08.50; Mon, 08 Jun 2020 18:09:13 -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=@kernel.org header.s=default header.b=To50O7H1; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727774AbgFHXHC (ORCPT + 99 others); Mon, 8 Jun 2020 19:07:02 -0400 Received: from mail.kernel.org ([198.145.29.99]:49966 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727036AbgFHXGi (ORCPT ); Mon, 8 Jun 2020 19:06:38 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6349120774; Mon, 8 Jun 2020 23:06:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1591657598; bh=h6qgXN4fgHCA3UiZlx2CBU9FEsMck4TG+MvMhPBdHmg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=To50O7H1PqWbNIUyxLRobz+R59ApnaJDF2hwj2LgWMkrUj+QaYcfaK9XEa/SUIPtV BTDnW1b7+JcKAqmCiJjNFr2G78qEmIFu9Otgt8X9pgcRNJyXBkalmNCSVxn6o/HWtd mt3+hy4tyDLXZh5NFdVyTgeQtgnmANoO4Vn9vz5A= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Andrii Nakryiko , Alston Tang , Alexei Starovoitov , Sasha Levin , netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH AUTOSEL 5.7 024/274] libbpf: Fix memory leak and possible double-free in hashmap__clear Date: Mon, 8 Jun 2020 19:01:57 -0400 Message-Id: <20200608230607.3361041-24-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200608230607.3361041-1-sashal@kernel.org> References: <20200608230607.3361041-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrii Nakryiko [ Upstream commit 229bf8bf4d910510bc1a2fd0b89bd467cd71050d ] 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: Sasha Levin --- 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.25.1