From patchwork Mon Aug 1 17:39:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 12933938 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6477C00144 for ; Mon, 1 Aug 2022 17:39:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232740AbiHARja (ORCPT ); Mon, 1 Aug 2022 13:39:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231368AbiHARj3 (ORCPT ); Mon, 1 Aug 2022 13:39:29 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACEBE63C7 for ; Mon, 1 Aug 2022 10:39:28 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-324f98aed9eso23566987b3.16 for ; Mon, 01 Aug 2022 10:39:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:mime-version:message-id:date:from:to:cc; bh=iyO7DTFE82DSnWeayUyzGKWvvGkvn6Eh0cYt/6e5Cs0=; b=qgNa+3GaFLLZzferNwNnjur+sDDZKll9HnViv8v3zoS8w/ArdCQP1UbSnCyBdkKxxH 2epbED74qWvocG7A2bpKwgDvNPV/pmEWQkXBHXiA6XlVrfI2QS5G5mknrq6W/h+pukRz 7lIKf55a42k4ZIXJuGXY61IpSLuLSfkoyGnpObx/wFMstrtFqsBwTQN3NYEQ2Jvc/wdr IAeNhXEWHiSD/rg6AYiybrVxHXNLfYmFgYR44juvqa3v0EfGQFaL8Bri90Q6GlJESRnK i1RO0hgcNZnrjn5b0wDV0SL/aquA7wgSXZumFk1AqQfHy1VG/6HnUEFzq3S0ghpVWmPd xtvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:mime-version:message-id:date:x-gm-message-state :from:to:cc; bh=iyO7DTFE82DSnWeayUyzGKWvvGkvn6Eh0cYt/6e5Cs0=; b=Pojrz9+bHhNZBXnGl4c72SsZYH/tLVhfeQZz2L+XLPlXvoEyCEWHRreXdOjouj+zAt wiMLQ/DsFs5XhNgMRg7H3csPngpBGlGTLxpN7SUSgDa5HKZH9UMCeNHrq+UcK3VUJykO mXdpWNQD7ZhlAwphpYKIvejFPBwI2G++25jvb60xTAhSwI8Sbr5vzzGqtPPnzYddLEoE M8TTjld9HdNF8lZbXD9qDVwpySOjiFeBWTF3Mh6bLInpVlUIqI3SaSH30KR/QFOBVHTr dKhWA9Ay01YHxjr0RGS1LG94a9Nxtiwyp02D2Q5Q8ZritDCyL/bjJMkkNlggwndAeCVW rBrg== X-Gm-Message-State: ACgBeo2I4mY/ZpZM5WYO7j0lZwZSl0ZoYr/601MlNBV2zEzFuPtt+0CP KPSUFjJ7KyoijU5DkL7890wy9w+MZ9/6VtjWana2L2WarFc0+DGfzL++4FTgDIWuOOYJw4gdpue +2ZAk+BZzh2g9+AjVFXo9RkbV1W9piSZCMB5QR40CMPIURQcVTA== X-Google-Smtp-Source: AA6agR7eW0j4LHeVUOcIxwZaIS4m4/tsDM3UkBrUQn3cZXASx9EoT1P/7/sZue8FAnA9TKH5MqPC7Ls= X-Received: from sdf.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5935]) (user=sdf job=sendgmr) by 2002:a81:6d45:0:b0:31f:498b:294 with SMTP id i66-20020a816d45000000b0031f498b0294mr14230980ywc.214.1659375567927; Mon, 01 Aug 2022 10:39:27 -0700 (PDT) Date: Mon, 1 Aug 2022 10:39:25 -0700 Message-Id: <20220801173926.2441748-1-sdf@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.37.1.455.g008518b4e5-goog Subject: [PATCH bpf-next v2 1/2] bpf: use proper target btf when exporting attach_btf_obj_id From: Stanislav Fomichev To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, Martin KaFai Lau Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net When attaching to program, the program itself might not be attached to anything (and, hence, might not have attach_btf), so we can't unconditionally use 'prog->aux->dst_prog->aux->attach_btf'. Instead, use bpf_prog_get_target_btf to pick proper target btf: * when attached to dst_prog, use dst_prog->aux->btf * when attached to kernel btf, use prog->aux->attach_btf Fixes: b79c9fc9551b ("bpf: implement BPF_PROG_QUERY for BPF_LSM_CGROUP") Acked-by: Hao Luo Acked-by: Martin KaFai Lau Signed-off-by: Stanislav Fomichev --- kernel/bpf/syscall.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 83c7136c5788..7dc3f8003631 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3886,6 +3886,7 @@ static int bpf_prog_get_info_by_fd(struct file *file, union bpf_attr __user *uattr) { struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); + struct btf *attach_btf = bpf_prog_get_target_btf(prog); struct bpf_prog_info info; u32 info_len = attr->info.info_len; struct bpf_prog_kstats stats; @@ -4088,10 +4089,8 @@ static int bpf_prog_get_info_by_fd(struct file *file, if (prog->aux->btf) info.btf_id = btf_obj_id(prog->aux->btf); info.attach_btf_id = prog->aux->attach_btf_id; - if (prog->aux->attach_btf) - info.attach_btf_obj_id = btf_obj_id(prog->aux->attach_btf); - else if (prog->aux->dst_prog) - info.attach_btf_obj_id = btf_obj_id(prog->aux->dst_prog->aux->attach_btf); + if (attach_btf) + info.attach_btf_obj_id = btf_obj_id(attach_btf); ulen = info.nr_func_info; info.nr_func_info = prog->aux->func_info_cnt; From patchwork Mon Aug 1 17:39:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 12933939 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8F63EC19F2B for ; Mon, 1 Aug 2022 17:39:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231368AbiHARjb (ORCPT ); Mon, 1 Aug 2022 13:39:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232289AbiHARja (ORCPT ); Mon, 1 Aug 2022 13:39:30 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E817E140B4 for ; Mon, 1 Aug 2022 10:39:29 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id q82-20020a632a55000000b0041bafd16728so2520793pgq.3 for ; Mon, 01 Aug 2022 10:39:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=XV+9Gk8Glt4UiO1lSe6vSmqpZhOdzkUwOwlhq1X7BnQ=; b=ILz0JyNzO9kpMnfHbXU7p2cu7gk5gfq0fQGlD+cfCx8EThUFgYytS6lS1Tu4yekioD FMrE5h4UnkaWNlDoEpl0erBGCvZUINVDnSIx1vCtzc0oTFZ182sHJzscQ4E+u8z2ZDTx I3CvvvllgTTICjCJ/UzQG2Scqk86fP/1lB5GcJTvjog+5aJmak48XXULcDOlv+OMUMh/ 7TF9n3Ngjlkqhva3kT51JkqWYFrjI0vD826x5QTAxlAduRdhcNkynFIJ82EWrZOY2li+ tuTPNQWrF/ePDl/Xcqi3kSuUdydBs1RhfgC1DjJDbHK0oNIKUoThEVY09AIpidAvowLd +q+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=XV+9Gk8Glt4UiO1lSe6vSmqpZhOdzkUwOwlhq1X7BnQ=; b=wn+w8JNF1M1uJgN73kQDl0jcKOBvNYfl+FHzNfsLdS5OgO/u9OtX0XpV8xtc6S7+yu IL/jiconJweohQ1Jf2d9nW75d4z+y+50qPkjJHcPvfF8IPqTKJTycXlpfJx4e0NSpczH 2KRm8DR4GnKCzAKQZg0OSpC/FZEhoy0+/vrDQEZ7ZpIa+jBrXF/hzsCkBMnGwT6pekBk dpH/p6wcJbbhZKyld+X49bsudKL43/qpAW0+8fsgKGpNV8nKcd0zOpLcQJ5L2IR0/d/L bEAUmUA+aJsM8lAv84tpHlzxkbrbW/igIdxa29c1rNV9PNGF/k/Q5Wlfd6ZWCz/f61Hf eGqA== X-Gm-Message-State: ACgBeo0nzpMNauJsmrYtGE1GikwJ+ARwl3d/qlVmBItbLT/nzTo6KG6r INP4BcQFRT/l2VPQAk6nPtxiD/K6GMg3VvqV37bzV8czJPp21ZUeS8FFNje82oYQS4B3ngzbVf1 OpmWEzvoqumLpvXT9punl2HUbJKaIHZEEapsSEpFsdO5KcrwzOQ== X-Google-Smtp-Source: AA6agR5qj7BQTiCl0tHCGKlBs/JfKz4IAuQPIXoGAqSx8cc/amAU8KU5LYTM69VQYdQto9mz1iH/YUM= X-Received: from sdf.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5935]) (user=sdf job=sendgmr) by 2002:a17:902:aa4b:b0:164:11ad:af0f with SMTP id c11-20020a170902aa4b00b0016411adaf0fmr17465336plr.54.1659375569302; Mon, 01 Aug 2022 10:39:29 -0700 (PDT) Date: Mon, 1 Aug 2022 10:39:26 -0700 In-Reply-To: <20220801173926.2441748-1-sdf@google.com> Message-Id: <20220801173926.2441748-2-sdf@google.com> Mime-Version: 1.0 References: <20220801173926.2441748-1-sdf@google.com> X-Mailer: git-send-email 2.37.1.455.g008518b4e5-goog Subject: [PATCH bpf-next v2 2/2] selftests/bpf: Excercise bpf_obj_get_info_by_fd for bpf2bpf From: Stanislav Fomichev To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Apparently, no existing selftest covers it. Add a new one where we load cgroup/bind4 program and attach fentry to it. Calling bpf_obj_get_info_by_fd on the fentry program should return non-zero btf_id/btf_obj_id instead of crashing the kernel. v2: - use ret instead of err in find_prog_btf_id (Hao) - remove verifier log (Hao) - drop if conditional from ASSERT_OK(bpf_obj_get_info_by_fd(...)) (Hao) Signed-off-by: Stanislav Fomichev Acked-by: Hao Luo --- .../selftests/bpf/prog_tests/attach_to_bpf.c | 97 +++++++++++++++++++ .../selftests/bpf/progs/attach_to_bpf.c | 12 +++ 2 files changed, 109 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c create mode 100644 tools/testing/selftests/bpf/progs/attach_to_bpf.c diff --git a/tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c b/tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c new file mode 100644 index 000000000000..eb06f522c0b3 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/attach_to_bpf.c @@ -0,0 +1,97 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include +#include "attach_to_bpf.skel.h" + +static int find_prog_btf_id(const char *name, __u32 attach_prog_fd) +{ + struct bpf_prog_info info = {}; + __u32 info_len = sizeof(info); + struct btf *btf; + int ret; + + ret = bpf_obj_get_info_by_fd(attach_prog_fd, &info, &info_len); + if (ret) + return ret; + + if (!info.btf_id) + return -EINVAL; + + btf = btf__load_from_kernel_by_id(info.btf_id); + ret = libbpf_get_error(btf); + if (ret) + return ret; + + ret = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC); + btf__free(btf); + return ret; +} + +int load_fentry(int attach_prog_fd, int attach_btf_id) +{ + LIBBPF_OPTS(bpf_prog_load_opts, opts, + .expected_attach_type = BPF_TRACE_FENTRY, + .attach_prog_fd = attach_prog_fd, + .attach_btf_id = attach_btf_id, + ); + struct bpf_insn insns[] = { + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_EXIT_INSN(), + }; + + return bpf_prog_load(BPF_PROG_TYPE_TRACING, + "bind4_fentry", + "GPL", + insns, + ARRAY_SIZE(insns), + &opts); +} + +void test_attach_to_bpf(void) +{ + struct attach_to_bpf *skel = NULL; + struct bpf_prog_info info = {}; + __u32 info_len = sizeof(info); + int cgroup_fd = -1; + int fentry_fd = -1; + int btf_id; + + cgroup_fd = test__join_cgroup("/attach_to_bpf"); + if (!ASSERT_GE(cgroup_fd, 0, "cgroup_fd")) + return; + + skel = attach_to_bpf__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel")) + goto cleanup; + + skel->links.bind4 = bpf_program__attach_cgroup(skel->progs.bind4, cgroup_fd); + if (!ASSERT_OK_PTR(skel, "bpf_program__attach_cgroup")) + goto cleanup; + + btf_id = find_prog_btf_id("bind4", bpf_program__fd(skel->progs.bind4)); + if (!ASSERT_GE(btf_id, 0, "find_prog_btf_id")) + goto cleanup; + + fentry_fd = load_fentry(bpf_program__fd(skel->progs.bind4), btf_id); + if (!ASSERT_GE(fentry_fd, 0, "load_fentry")) + goto cleanup; + + /* Make sure bpf_obj_get_info_by_fd works correctly when attaching + * to another BPF program. + */ + + ASSERT_OK(bpf_obj_get_info_by_fd(fentry_fd, &info, &info_len), + "bpf_obj_get_info_by_fd"); + + ASSERT_EQ(info.btf_id, 0, "info.btf_id"); + ASSERT_GT(info.attach_btf_id, 0, "info.attach_btf_id"); + ASSERT_GT(info.attach_btf_obj_id, 0, "info.attach_btf_obj_id"); + +cleanup: + close(cgroup_fd); + close(fentry_fd); + attach_to_bpf__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/attach_to_bpf.c b/tools/testing/selftests/bpf/progs/attach_to_bpf.c new file mode 100644 index 000000000000..3f111fe96f8f --- /dev/null +++ b/tools/testing/selftests/bpf/progs/attach_to_bpf.c @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +SEC("cgroup/bind4") +int bind4(struct bpf_sock_addr *ctx) +{ + return 1; +} + +char _license[] SEC("license") = "GPL";