Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp460210pxx; Wed, 28 Oct 2020 08:51:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzNraa6hQnPWt3ahhVdw0fC94z0jJ9YYymQx41LZe20RwUGF7XJHL9DSGzTqwXB3pTf1fRZ X-Received: by 2002:a50:cf45:: with SMTP id d5mr8183617edk.225.1603900271221; Wed, 28 Oct 2020 08:51:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603900271; cv=none; d=google.com; s=arc-20160816; b=w4o52L9j3qQl5Lo1jGGwJgk0gH8haE1wQkTB7xoiWTNcCTOUmiluN+AHwHG1AL6+hl nTyAdMPzMOfBOMYb7hYwv8DmzC1S2ZAn8MvIloFFS+vO6CMuUdma7OQuJZz6wuVyBiJR vuC5pr/JI858k2XOR6Mhc7ZWGeJ3w49Rqx0+ut/r/Jc5sLX9tBxLJ95Mkz4whksPGTkF 9rFu7HKNNH1udMV2yCuc1dzphgEN7B5i5RxA3BHZfcHRaOZkHIO6+9LK0TTqCyHdzJDJ ETltwbssri2/1zOP2fcqFIRismJSstgdFhxU1AFFxUZa4s84tWathmLHD9GPppiI9MHU xdOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=mS8qPpsg15Q5mqDUq771D9xfyUGVhyoXMA7AiyIKCgg=; b=L46Gb3GoCLP5EYASeZOA8Pu2WN6mOhDAcbJAZo5BgHH676p0LCK8ngI4COBRCQxK09 TDgeX88d56JYLSU3Rfp9LcKt8SlWV7uOlNzslsdAa35IWiZZ4NExBGBHVb5laaM7JMjo 76broSjKiIQBdlcq+kqoAfhBBFznKZJ4J4FDf8ETZ8Urp6EuK78ZujSOB+j0q0/7j8JS 3ZXM/QlxuMa5VEZ5IZmN0oBsBKqnz1cM+9LYFdgpQ83xgyLa6Bv4cCc9FbqC2tzOy2tp LvjffEaaFJ94kdM1E5/X9bryXXXgb/flbLu49zG3D6IgHYB8RWUXjWBFzBz5dqinGIPE kDcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=w33NU+q5; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bt10si1135660edb.503.2020.10.28.08.50.48; Wed, 28 Oct 2020 08:51:11 -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=w33NU+q5; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1813260AbgJ0Qtp (ORCPT + 99 others); Tue, 27 Oct 2020 12:49:45 -0400 Received: from mail.kernel.org ([198.145.29.99]:50690 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1794823AbgJ0PNs (ORCPT ); Tue, 27 Oct 2020 11:13:48 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3AAF42224A; Tue, 27 Oct 2020 15:13:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603811627; bh=xTXZ3LFLfbNBi2+C9NOvr+Ye26ESR4F400hGWDKrExw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=w33NU+q51VNDlF6jwjGInPLFY0fVWWs2mJnmJ4NXkW7c9tb03u9y4xBm7onN4zJjO 9IuFp7SwXxqMHh5vNohAH+tSrFcJBsIoRcJEZd8puXpZY8oeNymdO6FlzSaCdLsS4P T95nimQsofHV4TLvkdKjh30vXQu8qP4rNAzxyHdc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andrii Nakryiko , Hangbin Liu , Alexei Starovoitov , Andrii Nakryiko , Sasha Levin Subject: [PATCH 5.8 561/633] libbpf: Close map fd if init map slots failed Date: Tue, 27 Oct 2020 14:55:04 +0100 Message-Id: <20201027135549.120457645@linuxfoundation.org> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201027135522.655719020@linuxfoundation.org> References: <20201027135522.655719020@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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 e4d304247c1ba..3e71c2f69afe8 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