Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp2670501ybl; Thu, 29 Aug 2019 11:14:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqx6AcXYYAxgUCg6mz8lkguaQuQy1VXmwTgaKJhRecw2G69h96PEJFhZ94YO4M6Tzyzvd4c1 X-Received: by 2002:a62:388f:: with SMTP id f137mr13279074pfa.131.1567102481572; Thu, 29 Aug 2019 11:14:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567102481; cv=none; d=google.com; s=arc-20160816; b=P6AlRYJex47k+lN8gORd6IAbWwFJmBo9qv93LAUv4sqoyJIyyMH4n7j0DTJmQXMbJW LUWRRpUO9M3cVtTCNcgCvfgbmIG5nFPQmWTUeqMSU8Yuk5nAuT0MN1FefdRIdFsfZzlM JL+KGLfomtP5OvpqffllJrLpxguVrdYPvMc3Yca7cXrrTRgDdGdTXywQ4yjJ1cmfRtY2 JrERoVzLkN0352INfSX1PClW1TIIdVRfFjZnZr8A6gqoPFfXV7qRXA49ggGMqNHrNBSu DAK5ns/181cNubPq+DeR3O2vwqRB8R3njUz8kFseEjo/XdRyLqapjo+2yLZBNQqJ9mJY znFw== 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=vda5M3suSkl0Ip/uz6EwjXD0SkFWbiBgi3C+Y57/ILA=; b=r8Khd7tCKh4bUAyUNnVDH2jEeW43RPOsXXlEN6pPbf6bLZ7JSjRunRfHUSJrxbrbCp vKMks1tYoLjtPrkxpy45E0nSGydmWOoHhdtMbCkJRJrDA/kfJPum6KLm/WXDTd8mm3qP 4CbjzY88u5agCyJZXje1FqNbLds0brfnrpYeiT7whizzqe1rQZ/sNxfLHm2Fs8NZOO7U F5ZxRkKVLnQHYELeszFJbBUnijZONzJklDo6ybqyrOyaJmAcpgPV7X+TpsHZVhF+dJER 9eUD6iXjngkyxnrKtLywqyyfNF9S1yASNwWBxjqGA6Uj5Ots7oD8BVs4VKoJLxCpq/sP YYSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0BfqQvxB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id 200si3493682pfy.246.2019.08.29.11.14.26; Thu, 29 Aug 2019 11:14:41 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0BfqQvxB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1728170AbfH2SNW (ORCPT + 99 others); Thu, 29 Aug 2019 14:13:22 -0400 Received: from mail.kernel.org ([198.145.29.99]:55022 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726661AbfH2SNQ (ORCPT ); Thu, 29 Aug 2019 14:13:16 -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 35F7C22CF5; Thu, 29 Aug 2019 18:13:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567102395; bh=qx5FyH+qCOmLk8ayVVq6fVEn6qvhREhtmNVzQ1RfpQ8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0BfqQvxBlOilp5K2g5vfuPX/dhEroln8AUEaIQBbieGHYBWiCoiw77sDD1BUpCiS6 2KvoFLo8qPO2UPNZDnw4Q43vuwcjDitoHN5vWO4yYN7Cq4Km5Mz9hpPUGm65dLC4pd KLGZgP4lLg5SJIFf/w0PCDJan8ndq1ruoGWUoSiM= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Andrii Nakryiko , Andrey Ignatov , Alexei Starovoitov , Sasha Levin , netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH AUTOSEL 5.2 02/76] libbpf: fix erroneous multi-closing of BTF FD Date: Thu, 29 Aug 2019 14:11:57 -0400 Message-Id: <20190829181311.7562-2-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190829181311.7562-1-sashal@kernel.org> References: <20190829181311.7562-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 5d01ab7bac467edfc530e6ccf953921def935c62 ] Libbpf stores associated BTF FD per each instance of bpf_program. When program is unloaded, that FD is closed. This is wrong, because leads to a race and possibly closing of unrelated files, if application simultaneously opens new files while bpf_programs are unloaded. It's also unnecessary, because struct btf "owns" that FD, and btf__free(), called from bpf_object__close() will close it. Thus the fix is to never have per-program BTF FD and fetch it from obj->btf, when necessary. Fixes: 2993e0515bb4 ("tools/bpf: add support to read .BTF.ext sections") Reported-by: Andrey Ignatov Signed-off-by: Andrii Nakryiko Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- tools/lib/bpf/libbpf.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 3865a5d272514..e308fcf16cdd0 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -178,7 +178,6 @@ struct bpf_program { bpf_program_clear_priv_t clear_priv; enum bpf_attach_type expected_attach_type; - int btf_fd; void *func_info; __u32 func_info_rec_size; __u32 func_info_cnt; @@ -305,7 +304,6 @@ void bpf_program__unload(struct bpf_program *prog) prog->instances.nr = -1; zfree(&prog->instances.fds); - zclose(prog->btf_fd); zfree(&prog->func_info); zfree(&prog->line_info); } @@ -382,7 +380,6 @@ bpf_program__init(void *data, size_t size, char *section_name, int idx, prog->instances.fds = NULL; prog->instances.nr = -1; prog->type = BPF_PROG_TYPE_UNSPEC; - prog->btf_fd = -1; return 0; errout: @@ -1883,9 +1880,6 @@ bpf_program_reloc_btf_ext(struct bpf_program *prog, struct bpf_object *obj, prog->line_info_rec_size = btf_ext__line_info_rec_size(obj->btf_ext); } - if (!insn_offset) - prog->btf_fd = btf__fd(obj->btf); - return 0; } @@ -2060,7 +2054,7 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, char *cp, errmsg[STRERR_BUFSIZE]; int log_buf_size = BPF_LOG_BUF_SIZE; char *log_buf; - int ret; + int btf_fd, ret; memset(&load_attr, 0, sizeof(struct bpf_load_program_attr)); load_attr.prog_type = prog->type; @@ -2072,7 +2066,8 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, load_attr.license = license; load_attr.kern_version = kern_version; load_attr.prog_ifindex = prog->prog_ifindex; - load_attr.prog_btf_fd = prog->btf_fd >= 0 ? prog->btf_fd : 0; + btf_fd = bpf_object__btf_fd(prog->obj); + load_attr.prog_btf_fd = btf_fd >= 0 ? btf_fd : 0; load_attr.func_info = prog->func_info; load_attr.func_info_rec_size = prog->func_info_rec_size; load_attr.func_info_cnt = prog->func_info_cnt; -- 2.20.1