On Sat, Sep 19, 2020 at 4:50 AM Toke Høiland-Jørgensen <toke@xxxxxxxxxx> wrote: > > From: Toke Høiland-Jørgensen <toke@xxxxxxxxxx> > > This adds a selftest that ensures that modify_return tracing programs > cannot be attached to freplace programs. The security_ prefix is added to > the freplace program because that would otherwise let it pass the check for > modify_return. > > Signed-off-by: Toke Høiland-Jørgensen <toke@xxxxxxxxxx> > --- Acked-by: Andrii Nakryiko <andriin@xxxxxx> > .../selftests/bpf/prog_tests/fexit_bpf2bpf.c | 68 ++++++++++++++++++++ > .../selftests/bpf/progs/fmod_ret_freplace.c | 14 ++++ > .../selftests/bpf/progs/freplace_get_constant.c | 2 - > 3 files changed, 83 insertions(+), 1 deletion(-) > create mode 100644 tools/testing/selftests/bpf/progs/fmod_ret_freplace.c > > diff --git a/tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c b/tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c > index 27677e015730..6339d125ef9a 100644 > --- a/tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c > +++ b/tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c > @@ -233,6 +233,72 @@ static void test_func_replace_multi(void) > prog_name, true, test_second_attach); > } > > +static void test_fmod_ret_freplace(void) > +{ > + const char *tgt_name = "./test_pkt_access.o"; > + const char *freplace_name = "./freplace_get_constant.o"; > + const char *fmod_ret_name = "./fmod_ret_freplace.o"; > + struct bpf_link *freplace_link = NULL, *fmod_link = NULL; > + struct bpf_object *freplace_obj = NULL, *pkt_obj, *fmod_obj = NULL; > + struct bpf_program *prog; > + __u32 duration = 0; > + int err, pkt_fd; > + > + err = bpf_prog_load(tgt_name, BPF_PROG_TYPE_UNSPEC, > + &pkt_obj, &pkt_fd); > + /* the target prog should load fine */ > + if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n", > + tgt_name, err, errno)) > + return; > + DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, > + .attach_prog_fd = pkt_fd, > + ); this is variable declaration, it has to be together with all the variables above > + > + freplace_obj = bpf_object__open_file(freplace_name, &opts); > + if (CHECK(IS_ERR_OR_NULL(freplace_obj), "freplace_obj_open", > + "failed to open %s: %ld\n", freplace_name, > + PTR_ERR(freplace_obj))) > + goto out; > + > + err = bpf_object__load(freplace_obj); > + if (CHECK(err, "freplace_obj_load", "err %d\n", err)) > + goto out; > + > + prog = bpf_program__next(NULL, freplace_obj); > + freplace_link = bpf_program__attach_trace(prog); > + if (CHECK(IS_ERR(freplace_link), "freplace_attach_trace", "failed to link\n")) > + goto out; > + > + opts.attach_prog_fd = bpf_program__fd(prog); > + fmod_obj = bpf_object__open_file(fmod_ret_name, &opts); > + if (CHECK(IS_ERR_OR_NULL(fmod_obj), "fmod_obj_open", > + "failed to open %s: %ld\n", fmod_ret_name, > + PTR_ERR(fmod_obj))) > + goto out; > + > + err = bpf_object__load(fmod_obj); > + if (CHECK(err, "fmod_obj_load", "err %d\n", err)) > + goto out; > + > + prog = bpf_program__next(NULL, fmod_obj); > + fmod_link = bpf_program__attach_trace(prog); > + if (CHECK(!IS_ERR(fmod_link), "fmod_attach_trace", > + "linking fmod_ret to freplace should fail\n")) > + goto out; > + > +out: > + if (!IS_ERR_OR_NULL(freplace_link)) > + bpf_link__destroy(freplace_link); > + if (!IS_ERR_OR_NULL(fmod_link)) > + bpf_link__destroy(fmod_link); > + if (!IS_ERR_OR_NULL(freplace_obj)) > + bpf_object__close(freplace_obj); > + if (!IS_ERR_OR_NULL(fmod_obj)) > + bpf_object__close(fmod_obj); no need for all IS_ERR_OR_NULL checks > + bpf_object__close(pkt_obj); > +} > + > + [...]