libbpf: disassociate section handler on explicit bpf_program__set_type() call
authorAndrii Nakryiko <andrii@kernel.org>
Mon, 27 Mar 2023 18:52:00 +0000 (11:52 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 30 Mar 2023 00:22:01 +0000 (17:22 -0700)
If user explicitly overrides programs's type with
bpf_program__set_type() API call, we need to disassociate whatever
SEC_DEF handler libbpf determined initially based on program's SEC()
definition, as it's not goind to be valid anymore and could lead to
crashes and/or confusing failures.

Also, fix up bpf_prog_test_load() helper in selftests/bpf, which is
force-setting program type (even if that's completely unnecessary; this
is quite a legacy piece of code), and thus should expect auto-attach to
not work, yet one of the tests explicitly relies on auto-attach for
testing.

Instead, force-set program type only if it differs from the desired one.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20230327185202.1929145-2-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/lib/bpf/libbpf.c
tools/testing/selftests/bpf/testing_helpers.c

index 15737d7b5a28890fb61056302c2d47ad975cc490..49cd304ae3bc520cca9d9b11645bf33ee7679659 100644 (file)
@@ -8468,6 +8468,7 @@ int bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
                return libbpf_err(-EBUSY);
 
        prog->type = type;
+       prog->sec_def = NULL;
        return 0;
 }
 
index 6c44153755e6644a98326d06459c16884cffa564..ecfea13f938b457545084cd33104df6e9cffa4ae 100644 (file)
@@ -195,7 +195,7 @@ int bpf_prog_test_load(const char *file, enum bpf_prog_type type,
                goto err_out;
        }
 
-       if (type != BPF_PROG_TYPE_UNSPEC)
+       if (type != BPF_PROG_TYPE_UNSPEC && bpf_program__type(prog) != type)
                bpf_program__set_type(prog, type);
 
        flags = bpf_program__flags(prog) | BPF_F_TEST_RND_HI32;