Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp7487rwn; Wed, 7 Sep 2022 11:33:20 -0700 (PDT) X-Google-Smtp-Source: AA6agR73xtDRwLs7VOR6fijRvRHOWKiBQDKW0xP+5iaF5l2lTK6ZXbHrzBWezppg10/7fkPHhDwS X-Received: by 2002:a17:907:1ded:b0:76f:af8f:942e with SMTP id og45-20020a1709071ded00b0076faf8f942emr3181973ejc.120.1662575600126; Wed, 07 Sep 2022 11:33:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662575600; cv=none; d=google.com; s=arc-20160816; b=R70ZHbRAiYrJHCRT6wBdgPcuVX3731j/LHJYmJqAwey7ZiFvk+88ndklFtq6TxPhF6 Arhya3lmvt2ePZYRkZoSbtPXwwNyyyXBsN7sqLtGZ2v+cVcLo9sVP6jBahBo7ACaAFUx Me267qGPJEUOZQ1P/EmyTP4loOgURb3kLvf9Xw1pw5x+ObSVKN3FFLDxEgjekgSIBCiF 3lyvHBIldTYpssuAW4JP46WTsnD84exqry6vbxCcI0EZrQriTb/u2VrEqJO96RjW//ok Dk2Xf/F5A6y38K0V0a10fYB6365FMcSQ2ifr1z3IJ3jYEa5O4v8njFtY6zdfokpHFibH Q/LA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=YpBQotoGZNVTm0xUrupXAeoFQOusEenuOy1efqUfeig=; b=w6YnFGJeXAphQdmLxkl8QcIo1XhBB5FvZgsv/JumxlyOQZ9gFjfvDpgphePgEQSiSU wTUifP47b8D6ooVY9dfj9HbWoL2NcdMtx88vOozJXnCVuO1Eo1523sn3FAfFGWwzzMWd WC5X9UIB7SwnXo23eeu7eafBex0djGROfXqQJiMrmV+ztjiqdKxGJg7aEz6IVDLtL3BQ rtfYRRhoqtly30G7cUV3PhPh0dVOV3urDy3BWhoogOblRwc189d3z/I/+wnhj7mi/gxE u2Ro9qb2kfwgK7jN57HzRJl/zkpYZHOCMQhf5Uqas4z8ojMQsZrZtom5Dcvb5eoX0jpP rSPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=j4ISLYTi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ek10-20020a056402370a00b0044d7a93e44dsi8870938edb.579.2022.09.07.11.32.54; Wed, 07 Sep 2022 11:33:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=j4ISLYTi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230423AbiIGSJn (ORCPT + 99 others); Wed, 7 Sep 2022 14:09:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230408AbiIGSJg (ORCPT ); Wed, 7 Sep 2022 14:09:36 -0400 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 443A06361; Wed, 7 Sep 2022 11:09:25 -0700 (PDT) Received: by mail-ej1-x632.google.com with SMTP id fg1so10098230ejc.2; Wed, 07 Sep 2022 11:09:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=YpBQotoGZNVTm0xUrupXAeoFQOusEenuOy1efqUfeig=; b=j4ISLYTikdQ6qUZEdNQO2K8UEEV/jYQxSH/ugTlxA2Rp9XhkXab+fAt7e6NWqpI1y4 NZdbJAPREKqWPbgOdFfTmUxUKEWIxKY75WRayDWkTFTb/IOiMQZEvXYwAG+I6kOhlF0z 1hiLgTn/N5yntMm+bCRHA8eo+HSHADrD9KWweabXH2mxexPciowOEQ3WjD268z8MArfY HCXpPbnHmho/9a++dlI2H9JK7j9H3baAIA/9WKbCq0sjYfuVSso09o2yQkFavQFdoYVy 8WUB3Dfjak5wiGiiF51FUZz74pY6Yb1jRNM+q5eWlAp4C8E2Xk7raE5okQc6gOFDEK13 0hMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=YpBQotoGZNVTm0xUrupXAeoFQOusEenuOy1efqUfeig=; b=5XlNalnMmSKth7Yr7Ct51iyVxuMBMNJFA3yvf7IfEZHrhLSYL9g+KpqbSAaQEnsIDW UHJdsaEKmExKv09SrrUQBWf3TVr0qznEHgl3SEVftBSpaNaAysJVTXtW6ldgUKOdjpw+ O8kaYmQXVhsvMCL25nOCa31QY0LY+K3NIzv+zm3HzS7KPgQ/Ly9UL3Gc5MIFpFkkKtFv ri6thBzw/MvK5yv+hYkTv1KfzHoqJWb+bv3VODhB2w+BcWpq3fjnP7f4fSAxiZu2T+9S 9I8FNzZMpr+DJk5Y1sJ1wZKqy3SBym5gXF/D+1kZPAvK67ba/KpJw1SJS6vOTjnLlIYK yrxg== X-Gm-Message-State: ACgBeo1cxAq6FpiJZuFOKKE+5V70Kl0JLS2PaZ3Fr/Wi64xRNV6wIANR vQPL0Ye4UHz7iTQq8o4yWxK53TJlJKGQBMzrJF4= X-Received: by 2002:a17:907:2c74:b0:741:657a:89de with SMTP id ib20-20020a1709072c7400b00741657a89demr3156554ejc.58.1662574163526; Wed, 07 Sep 2022 11:09:23 -0700 (PDT) MIME-Version: 1.0 References: <20220906151303.2780789-1-benjamin.tissoires@redhat.com> <20220906151303.2780789-5-benjamin.tissoires@redhat.com> In-Reply-To: From: Alexei Starovoitov Date: Wed, 7 Sep 2022 11:09:12 -0700 Message-ID: Subject: Re: [PATCH bpf-next v11 4/7] selftests/bpf: add test for accessing ctx from syscall program type To: Kumar Kartikeya Dwivedi Cc: Benjamin Tissoires , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Shuah Khan , LKML , Network Development , bpf , "open list:KERNEL SELFTEST FRAMEWORK" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 7, 2022 at 10:46 AM Kumar Kartikeya Dwivedi wrote: > > On Tue, 6 Sept 2022 at 17:13, Benjamin Tissoires > wrote: > > > > We need to also export the kfunc set to the syscall program type, > > and then add a couple of eBPF programs that are testing those calls. > > > > The first one checks for valid access, and the second one is OK > > from a static analysis point of view but fails at run time because > > we are trying to access outside of the allocated memory. > > > > Signed-off-by: Benjamin Tissoires > > > > --- > > CI is failing for test_progs-no_alu32: > https://github.com/kernel-patches/bpf/runs/8220916615?check_suite_focus=true > > > > > changes in v11: > > - use new way of declaring tests > > > > changes in v10: > > - use new definitions for tests in an array > > - add a new kfunc syscall_test_null_fail test > > > > no changes in v9 > > > > no changes in v8 > > > > changes in v7: > > - add 1 more case to ensure we can read the entire sizeof(ctx) > > - add a test case for when the context is NULL > > > > new in v6 > > --- > > net/bpf/test_run.c | 1 + > > .../selftests/bpf/prog_tests/kfunc_call.c | 143 +++++++++++++++++- > > .../selftests/bpf/progs/kfunc_call_fail.c | 39 +++++ > > .../selftests/bpf/progs/kfunc_call_test.c | 38 +++++ > > 4 files changed, 214 insertions(+), 7 deletions(-) > > create mode 100644 tools/testing/selftests/bpf/progs/kfunc_call_fail.c > > > > diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c > > index 25d8ecf105aa..f16baf977a21 100644 > > --- a/net/bpf/test_run.c > > +++ b/net/bpf/test_run.c > > @@ -1634,6 +1634,7 @@ static int __init bpf_prog_test_run_init(void) > > > > ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set); > > ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_prog_test_kfunc_set); > > + ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_prog_test_kfunc_set); > > return ret ?: register_btf_id_dtor_kfuncs(bpf_prog_test_dtor_kfunc, > > ARRAY_SIZE(bpf_prog_test_dtor_kfunc), > > THIS_MODULE); > > diff --git a/tools/testing/selftests/bpf/prog_tests/kfunc_call.c b/tools/testing/selftests/bpf/prog_tests/kfunc_call.c > > index 9dfbe5355a2d..d5881c3331a8 100644 > > --- a/tools/testing/selftests/bpf/prog_tests/kfunc_call.c > > +++ b/tools/testing/selftests/bpf/prog_tests/kfunc_call.c > > @@ -2,6 +2,7 @@ > > /* Copyright (c) 2021 Facebook */ > > #include > > #include > > +#include "kfunc_call_fail.skel.h" > > #include "kfunc_call_test.skel.h" > > #include "kfunc_call_test.lskel.h" > > #include "kfunc_call_test_subprog.skel.h" > > @@ -10,37 +11,96 @@ > > > > #include "cap_helpers.h" > > > > +static size_t log_buf_sz = 1048576; /* 1 MB */ > > +static char obj_log_buf[1048576]; > > + > > +enum kfunc_test_type { > > + tc_test = 0, > > + syscall_test, > > + syscall_null_ctx_test, > > +}; > > + > > struct kfunc_test_params { > > const char *prog_name; > > unsigned long lskel_prog_desc_offset; > > int retval; > > + enum kfunc_test_type test_type; > > + const char *expected_err_msg; > > }; > > > > -#define TC_TEST(name, __retval) \ > > +#define __BPF_TEST_SUCCESS(name, __retval, type) \ > > { \ > > .prog_name = #name, \ > > .lskel_prog_desc_offset = offsetof(struct kfunc_call_test_lskel, progs.name), \ > > .retval = __retval, \ > > + .test_type = type, \ > > + .expected_err_msg = NULL, \ > > + } > > + > > +#define __BPF_TEST_FAIL(name, __retval, type, error_msg) \ > > + { \ > > + .prog_name = #name, \ > > + .lskel_prog_desc_offset = 0 /* unused when test is failing */, \ > > + .retval = __retval, \ > > + .test_type = type, \ > > + .expected_err_msg = error_msg, \ > > } > > > > +#define TC_TEST(name, retval) __BPF_TEST_SUCCESS(name, retval, tc_test) > > +#define SYSCALL_TEST(name, retval) __BPF_TEST_SUCCESS(name, retval, syscall_test) > > +#define SYSCALL_NULL_CTX_TEST(name, retval) __BPF_TEST_SUCCESS(name, retval, syscall_null_ctx_test) > > + > > +#define SYSCALL_NULL_CTX_FAIL(name, retval, error_msg) \ > > + __BPF_TEST_FAIL(name, retval, syscall_null_ctx_test, error_msg) > > + > > static struct kfunc_test_params kfunc_tests[] = { > > + /* failure cases: > > + * if retval is 0 -> the program will fail to load and the error message is an error > > + * if retval is not 0 -> the program can be loaded but running it will gives the > > + * provided return value. The error message is thus the one > > + * from a successful load > > + */ > > + SYSCALL_NULL_CTX_FAIL(kfunc_syscall_test_fail, -EINVAL, "processed 4 insns"), > > + SYSCALL_NULL_CTX_FAIL(kfunc_syscall_test_null_fail, -EINVAL, "processed 4 insns"), > > + > > + /* success cases */ > > TC_TEST(kfunc_call_test1, 12), > > TC_TEST(kfunc_call_test2, 3), > > TC_TEST(kfunc_call_test_ref_btf_id, 0), > > + SYSCALL_TEST(kfunc_syscall_test, 0), > > + SYSCALL_NULL_CTX_TEST(kfunc_syscall_test_null, 0), > > +}; > > + > > +struct syscall_test_args { > > + __u8 data[16]; > > + size_t size; > > }; > > > > static void verify_success(struct kfunc_test_params *param) > > { > > struct kfunc_call_test_lskel *lskel = NULL; > > + LIBBPF_OPTS(bpf_test_run_opts, topts); > > struct bpf_prog_desc *lskel_prog; > > struct kfunc_call_test *skel; > > struct bpf_program *prog; > > int prog_fd, err; > > - LIBBPF_OPTS(bpf_test_run_opts, topts, > > - .data_in = &pkt_v4, > > - .data_size_in = sizeof(pkt_v4), > > - .repeat = 1, > > - ); > > + struct syscall_test_args args = { > > + .size = 10, > > + }; > > + > > + switch (param->test_type) { > > + case syscall_test: > > + topts.ctx_in = &args; > > + topts.ctx_size_in = sizeof(args); > > + /* fallthrough */ > > + case syscall_null_ctx_test: > > + break; > > + case tc_test: > > + topts.data_in = &pkt_v4; > > + topts.data_size_in = sizeof(pkt_v4); > > + topts.repeat = 1; > > + break; > > + } > > > > /* first test with normal libbpf */ > > skel = kfunc_call_test__open_and_load(); > > @@ -79,6 +139,72 @@ static void verify_success(struct kfunc_test_params *param) > > kfunc_call_test_lskel__destroy(lskel); > > } > > > > +static void verify_fail(struct kfunc_test_params *param) > > +{ > > + LIBBPF_OPTS(bpf_object_open_opts, opts); > > + LIBBPF_OPTS(bpf_test_run_opts, topts); > > + struct bpf_program *prog; > > + struct kfunc_call_fail *skel; > > + int prog_fd, err; > > + struct syscall_test_args args = { > > + .size = 10, > > + }; > > + > > + opts.kernel_log_buf = obj_log_buf; > > + opts.kernel_log_size = log_buf_sz; > > + opts.kernel_log_level = 1; > > + > > + switch (param->test_type) { > > + case syscall_test: > > + topts.ctx_in = &args; > > + topts.ctx_size_in = sizeof(args); > > + /* fallthrough */ > > + case syscall_null_ctx_test: > > + break; > > + case tc_test: > > + topts.data_in = &pkt_v4; > > + topts.data_size_in = sizeof(pkt_v4); > > + break; > > + topts.repeat = 1; > > + } > > + > > + skel = kfunc_call_fail__open_opts(&opts); > > + if (!ASSERT_OK_PTR(skel, "kfunc_call_fail__open_opts")) > > + goto cleanup; > > + > > + prog = bpf_object__find_program_by_name(skel->obj, param->prog_name); > > + if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name")) > > + goto cleanup; > > + > > + bpf_program__set_autoload(prog, true); > > + > > + err = kfunc_call_fail__load(skel); > > + if (!param->retval) { > > + /* the verifier is supposed to complain and refuses to load */ > > + if (!ASSERT_ERR(err, "unexpected load success")) > > + goto out_err; > > + > > + } else { > > + /* the program is loaded but must dynamically fail */ > > + if (!ASSERT_OK(err, "unexpected load error")) > > + goto out_err; > > + > > + prog_fd = bpf_program__fd(prog); > > + err = bpf_prog_test_run_opts(prog_fd, &topts); > > + if (!ASSERT_EQ(err, param->retval, param->prog_name)) > > + goto out_err; > > + } > > + > > +out_err: > > + if (!ASSERT_OK_PTR(strstr(obj_log_buf, param->expected_err_msg), "expected_err_msg")) { > > + fprintf(stderr, "Expected err_msg: %s\n", param->expected_err_msg); > > + fprintf(stderr, "Verifier output: %s\n", obj_log_buf); > > + } > > + > > +cleanup: > > + kfunc_call_fail__destroy(skel); > > +} > > + > > static void test_main(void) > > { > > int i; > > @@ -87,7 +213,10 @@ static void test_main(void) > > if (!test__start_subtest(kfunc_tests[i].prog_name)) > > continue; > > > > - verify_success(&kfunc_tests[i]); > > + if (!kfunc_tests[i].expected_err_msg) > > + verify_success(&kfunc_tests[i]); > > + else > > + verify_fail(&kfunc_tests[i]); > > } > > } > > > > diff --git a/tools/testing/selftests/bpf/progs/kfunc_call_fail.c b/tools/testing/selftests/bpf/progs/kfunc_call_fail.c > > new file mode 100644 > > index 000000000000..4168027f2ab1 > > --- /dev/null > > +++ b/tools/testing/selftests/bpf/progs/kfunc_call_fail.c > > @@ -0,0 +1,39 @@ > > +// SPDX-License-Identifier: GPL-2.0 > > +/* Copyright (c) 2021 Facebook */ > > +#include > > +#include > > + > > +extern void bpf_kfunc_call_test_mem_len_pass1(void *mem, int len) __ksym; > > + > > +struct syscall_test_args { > > + __u8 data[16]; > > + size_t size; > > +}; > > + > > +SEC("?syscall") > > +int kfunc_syscall_test_fail(struct syscall_test_args *args) > > +{ > > + bpf_kfunc_call_test_mem_len_pass1(&args->data, sizeof(*args) + 1); > > + > > + return 0; > > +} > > + > > +SEC("?syscall") > > +int kfunc_syscall_test_null_fail(struct syscall_test_args *args) > > +{ > > + /* Must be called with args as a NULL pointer > > + * we do not check for it to have the verifier consider that > > + * the pointer might not be null, and so we can load it. > > + * > > + * So the following can not be added: > > + * > > + * if (args) > > + * return -22; > > + */ > > + > > + bpf_kfunc_call_test_mem_len_pass1(args, sizeof(*args)); > > + > > + return 0; > > +} > > + > > +char _license[] SEC("license") = "GPL"; > > diff --git a/tools/testing/selftests/bpf/progs/kfunc_call_test.c b/tools/testing/selftests/bpf/progs/kfunc_call_test.c > > index 5aecbb9fdc68..94c05267e5e7 100644 > > --- a/tools/testing/selftests/bpf/progs/kfunc_call_test.c > > +++ b/tools/testing/selftests/bpf/progs/kfunc_call_test.c > > @@ -92,4 +92,42 @@ int kfunc_call_test_pass(struct __sk_buff *skb) > > return 0; > > } > > > > +struct syscall_test_args { > > + __u8 data[16]; > > + size_t size; > > +}; > > + > > +SEC("syscall") > > +int kfunc_syscall_test(struct syscall_test_args *args) > > +{ > > + const int size = args->size; > > + > > + if (size > sizeof(args->data)) > > + return -7; /* -E2BIG */ > > + > > Looks like it is due to this. Verifier is confused because: > r7 = args->data; > r1 = r7; > > then it does r1 <<= 32; r1 >>=32; clearing upper 32 bits, so both r1 > and r7 lose the id association which propagates the bounds of r1 > learnt from comparison of it with sizeof(args->data); > > > + bpf_kfunc_call_test_mem_len_pass1(&args->data, sizeof(args->data)); > > + bpf_kfunc_call_test_mem_len_pass1(&args->data, sizeof(*args)); > > Later llvm assigns r7 to r2 for this call's 2nd arg. At this point the > verifier still thinks r7 is unbounded, while to make a call with mem, > len pair you need non-negative min value. > > Easiest way might be to just do args->size & sizeof(args->data), as > the verifier log says. You might still keep the error above. > Others may have better ideas/insights. I just did s/const int size/const long size/ to fix the issues. Also fixed commit in patch 3 that talks about max_ctx_offset and did: - BTF_KFUNC_SET_MAX_CNT = 64, + BTF_KFUNC_SET_MAX_CNT = 256, and applied.