From patchwork Mon Jul 5 12:43:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martynas Pumputis X-Patchwork-Id: 470750 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=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham 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 0CDC3C07E99 for ; Mon, 5 Jul 2021 12:43:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E2F9A61220 for ; Mon, 5 Jul 2021 12:43:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231149AbhGEMqB (ORCPT ); Mon, 5 Jul 2021 08:46:01 -0400 Received: from out4-smtp.messagingengine.com ([66.111.4.28]:49515 "EHLO out4-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230247AbhGEMqA (ORCPT ); Mon, 5 Jul 2021 08:46:00 -0400 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 3F9A25C00F9; Mon, 5 Jul 2021 08:43:23 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Mon, 05 Jul 2021 08:43:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :message-id:mime-version:subject:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=AlpY3EVeFaaGGaHMg uyLzl1R1b77+miegqGsG3nafAs=; b=Qm18D2fSvVp9kFpRGwlIB+8JAgBxzKa24 WJMbjdllsTwpkfyZb2KZgUHbZOasDNHdQh32vDpoGlYrMftApe+hBviWpv1CcNRM zr7J+8qkS9XqV94FUwWylGX5/ct9JG/JokPzEhyXCJTtMkCbrSCjfUyPNlmh/97l 2UMgY1BVFSKRomf6RJmYUC8r9pyWtvYvl3Ntsy+HmOAqJsHNH3s2GNElr0MZZynK EjOMTvB4kkIAxzc7k18z0b4CbOvrunVM5DLryyXX1k6i3xS9GVyGaBVNAtmD0N7l htaR2d5ejNKB2W6kRa7ThWvhAKGdTh897hp67P4+fip+GsJ5ms+IA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrfeejgedgheegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgggfestdekredtre dttdenucfhrhhomhepofgrrhhthihnrghsucfruhhmphhuthhishcuoehmsehlrghmsggu rgdrlhhtqeenucggtffrrghtthgvrhhnpeeuhfefvdehleeiudehvdeviefftdelheeiud euheejjedtkeduvddtffehueeljeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgr mhepmhgrihhlfhhrohhmpehmsehlrghmsggurgdrlhht X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 5 Jul 2021 08:43:21 -0400 (EDT) From: Martynas Pumputis To: netdev@vger.kernel.org Cc: haliu@redhat.com, stephen@networkplumber.org, dsahern@gmail.com, m@lambda.lt Subject: [PATCH iproute2] libbpf: fix attach of prog with multiple sections Date: Mon, 5 Jul 2021 14:43:07 +0200 Message-Id: <20210705124307.201303-1-m@lambda.lt> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org When BPF programs which consists of multiple executable sections via iproute2+libbpf (configured with LIBBPF_FORCE=on), we noticed that a wrong section can be attached to a device. E.g.: # tc qdisc replace dev lxc_health clsact # tc filter replace dev lxc_health ingress prio 1 \ handle 1 bpf da obj bpf_lxc.o sec from-container # tc filter show dev lxc_health ingress filter protocol all pref 1 bpf chain 0 filter protocol all pref 1 bpf chain 0 handle 0x1 bpf_lxc.o:[__send_drop_notify] <-- WRONG SECTION direct-action not_in_hw id 38 tag 7d891814eda6809e jited After taking a closer look into load_bpf_object() in lib/bpf_libbpf.c, we noticed that the filter used in the program iterator does not check whether a program section name matches a requested section name (cfg->section). This can lead to a wrong prog FD being used to attach the program. Fixes: 6d61a2b55799 ("lib: add libbpf support") Signed-off-by: Martynas Pumputis Acked-by: Hangbin Liu --- lib/bpf_libbpf.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/bpf_libbpf.c b/lib/bpf_libbpf.c index d05737a4..f76b90d2 100644 --- a/lib/bpf_libbpf.c +++ b/lib/bpf_libbpf.c @@ -267,10 +267,12 @@ static int load_bpf_object(struct bpf_cfg_in *cfg) } bpf_object__for_each_program(p, obj) { + bool prog_to_attach = !prog && cfg->section && + !strcmp(get_bpf_program__section_name(p), cfg->section); + /* Only load the programs that will either be subsequently * attached or inserted into a tail call map */ - if (find_legacy_tail_calls(p, obj) < 0 && cfg->section && - strcmp(get_bpf_program__section_name(p), cfg->section)) { + if (find_legacy_tail_calls(p, obj) < 0 && !prog_to_attach) { ret = bpf_program__set_autoload(p, false); if (ret) return -EINVAL; @@ -279,7 +281,8 @@ static int load_bpf_object(struct bpf_cfg_in *cfg) bpf_program__set_type(p, cfg->type); bpf_program__set_ifindex(p, cfg->ifindex); - if (!prog) + + if (prog_to_attach) prog = p; }