From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 899BBC43457 for ; Sun, 18 Oct 2020 19:51:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5A22020714 for ; Sun, 18 Oct 2020 19:51:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603050710; bh=Xerhk7RgVOtG1WU9Ag6tCmB6KN9b6/1DMXofnjiU4rY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=10M37aayvUTL1IkFdaOU+YnRB5gwMx2HvTfY9NpbdyjnhoXGv7cOUJwguWLevdLp2 B/twMhbR/rhigm8QkpFhKO4xYe5Qk3IZmG1EldAAyHC6+z/W0Yj6UIAIkdQ7oXksFx 7YvcC9SHQeNF0LRP1sOFWppR8LY+OskiK4dJUMo0= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729445AbgJRTvt (ORCPT ); Sun, 18 Oct 2020 15:51:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:33188 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729304AbgJRTVO (ORCPT ); Sun, 18 Oct 2020 15:21:14 -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 14C55222B9; Sun, 18 Oct 2020 19:21:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603048873; bh=Xerhk7RgVOtG1WU9Ag6tCmB6KN9b6/1DMXofnjiU4rY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Il5M2COmmJFwjY31aJk3WN/kDjyX48MbfSNk+gjDpswDG5LQNhcr6kKxsGJhBJQr4 /x0eTP9hdnOQpfedpjZmQ+oeSAn6HKqF+yG5sd85NuW9ItQJ+1XtnhdfpUZOwY6WPk YbUZrt4MUkFrz2JtP6++RjNBuOeLzCXfM3nmBLHo= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Hangbin Liu , Andrii Nakryiko , Alexei Starovoitov , Andrii Nakryiko , Sasha Levin Subject: [PATCH AUTOSEL 5.8 038/101] libbpf: Close map fd if init map slots failed Date: Sun, 18 Oct 2020 15:19:23 -0400 Message-Id: <20201018192026.4053674-38-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201018192026.4053674-1-sashal@kernel.org> References: <20201018192026.4053674-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Hangbin Liu [ Upstream commit a0f2b7acb4b1d29127ff99c714233b973afd1411 ] Previously we forgot to close the map fd if bpf_map_update_elem() failed during map slot init, which will leak map fd. Let's move map slot initialization to new function init_map_slots() to simplify the code. And close the map fd if init slot failed. Reported-by: Andrii Nakryiko Signed-off-by: Hangbin Liu Signed-off-by: Alexei Starovoitov Acked-by: Andrii Nakryiko Link: https://lore.kernel.org/bpf/20201006021345.3817033-2-liuhangbin@gmail.com Signed-off-by: Sasha Levin --- tools/lib/bpf/libbpf.c | 55 ++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 236c91aff48f8..bafc0524a83a7 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -3677,6 +3677,36 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map) return 0; } +static int init_map_slots(struct bpf_map *map) +{ + const struct bpf_map *targ_map; + unsigned int i; + int fd, err; + + for (i = 0; i < map->init_slots_sz; i++) { + if (!map->init_slots[i]) + continue; + + targ_map = map->init_slots[i]; + fd = bpf_map__fd(targ_map); + err = bpf_map_update_elem(map->fd, &i, &fd, 0); + if (err) { + err = -errno; + pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n", + map->name, i, targ_map->name, + fd, err); + return err; + } + pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n", + map->name, i, targ_map->name, fd); + } + + zfree(&map->init_slots); + map->init_slots_sz = 0; + + return 0; +} + static int bpf_object__create_maps(struct bpf_object *obj) { @@ -3719,28 +3749,11 @@ bpf_object__create_maps(struct bpf_object *obj) } if (map->init_slots_sz) { - for (j = 0; j < map->init_slots_sz; j++) { - const struct bpf_map *targ_map; - int fd; - - if (!map->init_slots[j]) - continue; - - targ_map = map->init_slots[j]; - fd = bpf_map__fd(targ_map); - err = bpf_map_update_elem(map->fd, &j, &fd, 0); - if (err) { - err = -errno; - pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n", - map->name, j, targ_map->name, - fd, err); - goto err_out; - } - pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n", - map->name, j, targ_map->name, fd); + err = init_map_slots(map); + if (err < 0) { + zclose(map->fd); + goto err_out; } - zfree(&map->init_slots); - map->init_slots_sz = 0; } if (map->pin_path && !map->pinned) { -- 2.25.1