From patchwork Mon Jun 6 07:52:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= X-Patchwork-Id: 12870101 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 CEC50CCA473 for ; Mon, 6 Jun 2022 07:53:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231544AbiFFHxG (ORCPT ); Mon, 6 Jun 2022 03:53:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231531AbiFFHxG (ORCPT ); Mon, 6 Jun 2022 03:53:06 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1E5CB1183D for ; Mon, 6 Jun 2022 00:53:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1654501984; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=4wjxbsl1erlBKFkxTTfcWwvSHQv8SIMTKUXWHhXdDJ8=; b=Lngp2n5nXfr13MJz5jWAOsYtTiU93ronkKa/MomzpWIYniWbMVFuIOQkVXKFoaAz7G2S8w FRrfOXcRnYuQTfjmq1Qim9V4mQnxOWeSCYPwBpyeUm4y/O41JBWnswaHdPdag9Y50S88PG FK/yUH3UAGQDL+sEyZmaKdM6qFUcQ+M= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-541-mOaeRsc3NrqkrQhcDeflcg-1; Mon, 06 Jun 2022 03:53:03 -0400 X-MC-Unique: mOaeRsc3NrqkrQhcDeflcg-1 Received: by mail-ed1-f70.google.com with SMTP id a4-20020a056402168400b0042dc5b94da6so10022727edv.10 for ; Mon, 06 Jun 2022 00:53:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=4wjxbsl1erlBKFkxTTfcWwvSHQv8SIMTKUXWHhXdDJ8=; b=4lSI//rqJ3Mn/uykMtb6v/Ba4P/HmUFbN/XEJkj4CcnOdXV+9j3JkCBdLVqyZcfM5f 4vRotWTM/wGJr5XKcZldyCl28h9RxHYarVL4bd3y+IWgACCM+jB2mzWs/GAwedfDgqR5 Dhv1RAjfibm/2Qt6h9EyZ1d1zZmtKFZ5XJywhTagrkVibw0Z4vzdpKcarfJn536B/mR7 MGWfPQaiWnFqLXcktKXPzZnp82rA81WDgm3PQkn4/b89bS+DoyTsLXrCDLrATZ6PEumF MTlUFmspqcQN9P4VPdPX/GXOY2+Y+S+KUGxb3l9/dvwN5sDmmxFwQDpXxdig1zyKPgXn DvPw== X-Gm-Message-State: AOAM5315ftQHYNICjRbCadl1+UAFqblqS5GgTdeKIlDakNNT5ljGuxbP YOrx3oCK1OwJowyL2kknV41EZHftKC+PbdSOzOkNTLDiqs3bR519Wt0oAzcl1HCetKlr+kIWcoN jydgnkX0q7Ba+s8B5 X-Received: by 2002:a17:907:6d12:b0:711:d524:8c88 with SMTP id sa18-20020a1709076d1200b00711d5248c88mr1390606ejc.615.1654501981259; Mon, 06 Jun 2022 00:53:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyYPfybuQj+QM3Mn4wjMpYzMx3MzTHUG2uQ2hgZMAQK8TkvY3grAtN65X/JyUOvJRQgesi6bA== X-Received: by 2002:a17:907:6d12:b0:711:d524:8c88 with SMTP id sa18-20020a1709076d1200b00711d5248c88mr1390566ejc.615.1654501980372; Mon, 06 Jun 2022 00:53:00 -0700 (PDT) Received: from alrua-x1.borgediget.toke.dk ([2a0c:4d80:42:443::2]) by smtp.gmail.com with ESMTPSA id w6-20020a05640234c600b004307c8e1c3fsm3676350edc.34.2022.06.06.00.52.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jun 2022 00:52:59 -0700 (PDT) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id 1B0ED40559F; Mon, 6 Jun 2022 09:52:57 +0200 (CEST) From: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Kumar Kartikeya Dwivedi Cc: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= , Simon Sundberg , netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH bpf v2 1/2] bpf: Fix calling global functions from BPF_PROG_TYPE_EXT programs Date: Mon, 6 Jun 2022 09:52:51 +0200 Message-Id: <20220606075253.28422-1-toke@redhat.com> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net The verifier allows programs to call global functions as long as their argument types match, using BTF to check the function arguments. One of the allowed argument types to such global functions is PTR_TO_CTX; however the check for this fails on BPF_PROG_TYPE_EXT functions because the verifier uses the wrong type to fetch the vmlinux BTF ID for the program context type. This failure is seen when an XDP program is loaded using libxdp (which loads it as BPF_PROG_TYPE_EXT and attaches it to a global XDP type program). Fix the issue by passing in the target program type instead of the BPF_PROG_TYPE_EXT type to bpf_prog_get_ctx() when checking function argument compatibility. The first Fixes tag refers to the latest commit that touched the code in question, while the second one points to the code that first introduced the global function call verification. v2: - Use resolve_prog_type() Fixes: 3363bd0cfbb8 ("bpf: Extend kfunc with PTR_TO_CTX, PTR_TO_MEM argument support") Fixes: 51c39bb1d5d1 ("bpf: Introduce function-by-function verification") Reported-by: Simon Sundberg Signed-off-by: Toke Høiland-Jørgensen --- kernel/bpf/btf.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 7bccaa4646e5..63d0ac7dfe2f 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -6054,6 +6054,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, struct bpf_reg_state *regs, bool ptr_to_mem_ok) { + enum bpf_prog_type prog_type = resolve_prog_type(env->prog); struct bpf_verifier_log *log = &env->log; u32 i, nargs, ref_id, ref_obj_id = 0; bool is_kfunc = btf_is_kernel(btf); @@ -6171,7 +6172,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, return -EINVAL; } /* rest of the arguments can be anything, like normal kfunc */ - } else if (btf_get_prog_ctx_type(log, btf, t, env->prog->type, i)) { + } else if (btf_get_prog_ctx_type(log, btf, t, prog_type, i)) { /* If function expects ctx type in BTF check that caller * is passing PTR_TO_CTX. */ From patchwork Mon Jun 6 07:52:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= X-Patchwork-Id: 12870102 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 B9D62C433EF for ; Mon, 6 Jun 2022 07:53:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231545AbiFFHxH (ORCPT ); Mon, 6 Jun 2022 03:53:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231530AbiFFHxG (ORCPT ); Mon, 6 Jun 2022 03:53:06 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 145FF11816 for ; Mon, 6 Jun 2022 00:53:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1654501984; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=efmsQ2arxZifYajSmsr75p7XJe4C+xw36T3Z4hPU4g4=; b=f7jar+M22j1Du49MApBbM3EafLLpVzE0abqOWggEMB6FGRWmuy9Q9K4a6WsP2EtjDLb+4r WxMvhCi94V9uDdw1SrnbOZLTueC18hEWev6/tehA4FrA2Tzh8oaZa5b7dPZIQ3Q4UZxbNO dEuRgfFiTi9cl85Cv7uZmOel0rTDrkA= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-185-nKohALySPg-t38VPRhKN3g-1; Mon, 06 Jun 2022 03:53:02 -0400 X-MC-Unique: nKohALySPg-t38VPRhKN3g-1 Received: by mail-ej1-f72.google.com with SMTP id t15-20020a1709066bcf00b0070dedeacb2cso2906000ejs.9 for ; Mon, 06 Jun 2022 00:53:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=efmsQ2arxZifYajSmsr75p7XJe4C+xw36T3Z4hPU4g4=; b=OdONKphIUdpPezC84+sOnQsL68RXlBawALKKDAIboTrBk002/+p2r7JFTrhEfi47wM cmfjw1qJGcjA4QpGxPRz3jR4W4L/XaRraY3hdgdP+KxtCp9m08JFpQ4Md7Obl2GPWXpY 2XLciHy1u9SXXynApoiS1rH5pI87PqgHnzsfZwAfAKYLbqZ+cmzQ+KA1isSl+S6A+257 g9BbC+5GZnz2yQv+VkBHpOLH6l+oWvBzFmBuwSeiNcZdWUQqbAqs/Ss/ItJ481wO5cvd fsc83zLkUI9hlZZnbXKJeGIIhqaldGfCZx57AnOsW0qSbIK7WHU80sEUEAu2V0Lb6NKO 5xXg== X-Gm-Message-State: AOAM532Op9ehLx/BfJAsqq8kdMO2UnXk0tKG/2rn2V/Yz2+dW0JLIfLO 0LDviFz4Sthh6zh+AloIZiGwOoRtuDLF9Q/HHiJBO2aaKhSOyLDazBuwcHLBq+iUpZXNdkrtssf fTlK6iIES7hvwPbH+ X-Received: by 2002:a05:6402:5114:b0:42f:b5f3:1f96 with SMTP id m20-20020a056402511400b0042fb5f31f96mr13720315edd.260.1654501980963; Mon, 06 Jun 2022 00:53:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxmnCNxtltGYWawiS61eUdc/EIKyqZ4BcsfNsSbSw7Z/52LixKveyxGp66T7W5LCa1zwgdktg== X-Received: by 2002:a05:6402:5114:b0:42f:b5f3:1f96 with SMTP id m20-20020a056402511400b0042fb5f31f96mr13720288edd.260.1654501980669; Mon, 06 Jun 2022 00:53:00 -0700 (PDT) Received: from alrua-x1.borgediget.toke.dk ([45.145.92.2]) by smtp.gmail.com with ESMTPSA id p23-20020a170906499700b0070f36b8cb39sm3568239eju.103.2022.06.06.00.52.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jun 2022 00:52:59 -0700 (PDT) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id 9DADA4055A1; Mon, 6 Jun 2022 09:52:57 +0200 (CEST) From: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh Cc: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= , Shuah Khan , netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH bpf v2 2/2] selftests/bpf: Add selftest for calling global functions from freplace Date: Mon, 6 Jun 2022 09:52:52 +0200 Message-Id: <20220606075253.28422-2-toke@redhat.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220606075253.28422-1-toke@redhat.com> References: <20220606075253.28422-1-toke@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Add a selftest that calls a global function with a context object parameter from an freplace function to check that the program context type is correctly converted to the freplace target when fetching the context type from the kernel BTF. v2: - Trim includes - Get rid of global function - Use __noinline Signed-off-by: Toke Høiland-Jørgensen --- .../selftests/bpf/prog_tests/fexit_bpf2bpf.c | 14 ++++++++++++++ .../selftests/bpf/progs/freplace_global_func.c | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/freplace_global_func.c diff --git a/tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c b/tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c index d9aad15e0d24..02bb8cbf9194 100644 --- a/tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +++ b/tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c @@ -395,6 +395,18 @@ static void test_func_map_prog_compatibility(void) "./test_attach_probe.o"); } +static void test_func_replace_global_func(void) +{ + const char *prog_name[] = { + "freplace/test_pkt_access", + }; + + test_fexit_bpf2bpf_common("./freplace_global_func.o", + "./test_pkt_access.o", + ARRAY_SIZE(prog_name), + prog_name, false, NULL); +} + /* NOTE: affect other tests, must run in serial mode */ void serial_test_fexit_bpf2bpf(void) { @@ -416,4 +428,6 @@ void serial_test_fexit_bpf2bpf(void) test_func_replace_multi(); if (test__start_subtest("fmod_ret_freplace")) test_fmod_ret_freplace(); + if (test__start_subtest("func_replace_global_func")) + test_func_replace_global_func(); } diff --git a/tools/testing/selftests/bpf/progs/freplace_global_func.c b/tools/testing/selftests/bpf/progs/freplace_global_func.c new file mode 100644 index 000000000000..96cb61a6ce87 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/freplace_global_func.c @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include + +__noinline +int test_ctx_global_func(struct __sk_buff *skb) +{ + volatile int retval = 1; + return retval; +} + +SEC("freplace/test_pkt_access") +int new_test_pkt_access(struct __sk_buff *skb) +{ + return test_ctx_global_func(skb); +} + +char _license[] SEC("license") = "GPL";