selftests/bpf: Add verifier tests for bpf_lookup_*_key() and bpf_key_put()
authorRoberto Sassu <roberto.sassu@huawei.com>
Tue, 20 Sep 2022 07:59:48 +0000 (09:59 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 22 Sep 2022 00:32:49 +0000 (17:32 -0700)
Add verifier tests for bpf_lookup_*_key() and bpf_key_put(), to ensure that
acquired key references stored in the bpf_key structure are released, that
a non-NULL bpf_key pointer is passed to bpf_key_put(), and that key
references are not leaked.

Also, slightly modify test_verifier.c, to find the BTF ID of the attach
point for the LSM program type (currently, it is done only for TRACING).

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20220920075951.929132-11-roberto.sassu@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/config
tools/testing/selftests/bpf/test_verifier.c
tools/testing/selftests/bpf/verifier/ref_tracking.c

index 0fdd11e6b742a402adf372d93175feeeb0809e33..add5a5a919b4d34a9a10cf1e72598c03c7d98464 100644 (file)
@@ -30,6 +30,7 @@ CONFIG_IPV6_GRE=y
 CONFIG_IPV6_SEG6_BPF=y
 CONFIG_IPV6_SIT=y
 CONFIG_IPV6_TUNNEL=y
+CONFIG_KEYS=y
 CONFIG_LIRC=y
 CONFIG_LWTUNNEL=y
 CONFIG_MPLS=y
index f9d553fbf68a3f451afe448494c4e10a013f4db3..2dbcbf363c181af18aa08fc064e820ff36c94dbb 100644 (file)
@@ -1498,7 +1498,8 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
                opts.log_level = DEFAULT_LIBBPF_LOG_LEVEL;
        opts.prog_flags = pflags;
 
-       if (prog_type == BPF_PROG_TYPE_TRACING && test->kfunc) {
+       if ((prog_type == BPF_PROG_TYPE_TRACING ||
+            prog_type == BPF_PROG_TYPE_LSM) && test->kfunc) {
                int attach_btf_id;
 
                attach_btf_id = libbpf_find_vmlinux_btf_id(test->kfunc,
index 57a83d763ec1780f6c7f63116fb801ea3ee5c3b4..f18ce867271fc03cec84f4ed3732be8ae2d98925 100644 (file)
        .errstr = "Unreleased reference",
        .result = REJECT,
 },
+{
+       "reference tracking: acquire/release user key reference",
+       .insns = {
+       BPF_MOV64_IMM(BPF_REG_1, -3),
+       BPF_MOV64_IMM(BPF_REG_2, 0),
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .prog_type = BPF_PROG_TYPE_LSM,
+       .kfunc = "bpf",
+       .expected_attach_type = BPF_LSM_MAC,
+       .flags = BPF_F_SLEEPABLE,
+       .fixup_kfunc_btf_id = {
+               { "bpf_lookup_user_key", 2 },
+               { "bpf_key_put", 5 },
+       },
+       .result = ACCEPT,
+},
+{
+       "reference tracking: acquire/release system key reference",
+       .insns = {
+       BPF_MOV64_IMM(BPF_REG_1, 1),
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .prog_type = BPF_PROG_TYPE_LSM,
+       .kfunc = "bpf",
+       .expected_attach_type = BPF_LSM_MAC,
+       .flags = BPF_F_SLEEPABLE,
+       .fixup_kfunc_btf_id = {
+               { "bpf_lookup_system_key", 1 },
+               { "bpf_key_put", 4 },
+       },
+       .result = ACCEPT,
+},
+{
+       "reference tracking: release user key reference without check",
+       .insns = {
+       BPF_MOV64_IMM(BPF_REG_1, -3),
+       BPF_MOV64_IMM(BPF_REG_2, 0),
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .prog_type = BPF_PROG_TYPE_LSM,
+       .kfunc = "bpf",
+       .expected_attach_type = BPF_LSM_MAC,
+       .flags = BPF_F_SLEEPABLE,
+       .errstr = "arg#0 pointer type STRUCT bpf_key must point to scalar, or struct with scalar",
+       .fixup_kfunc_btf_id = {
+               { "bpf_lookup_user_key", 2 },
+               { "bpf_key_put", 4 },
+       },
+       .result = REJECT,
+},
+{
+       "reference tracking: release system key reference without check",
+       .insns = {
+       BPF_MOV64_IMM(BPF_REG_1, 1),
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .prog_type = BPF_PROG_TYPE_LSM,
+       .kfunc = "bpf",
+       .expected_attach_type = BPF_LSM_MAC,
+       .flags = BPF_F_SLEEPABLE,
+       .errstr = "arg#0 pointer type STRUCT bpf_key must point to scalar, or struct with scalar",
+       .fixup_kfunc_btf_id = {
+               { "bpf_lookup_system_key", 1 },
+               { "bpf_key_put", 3 },
+       },
+       .result = REJECT,
+},
+{
+       "reference tracking: release with NULL key pointer",
+       .insns = {
+       BPF_MOV64_IMM(BPF_REG_1, 0),
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .prog_type = BPF_PROG_TYPE_LSM,
+       .kfunc = "bpf",
+       .expected_attach_type = BPF_LSM_MAC,
+       .flags = BPF_F_SLEEPABLE,
+       .errstr = "arg#0 pointer type STRUCT bpf_key must point to scalar, or struct with scalar",
+       .fixup_kfunc_btf_id = {
+               { "bpf_key_put", 1 },
+       },
+       .result = REJECT,
+},
+{
+       "reference tracking: leak potential reference to user key",
+       .insns = {
+       BPF_MOV64_IMM(BPF_REG_1, -3),
+       BPF_MOV64_IMM(BPF_REG_2, 0),
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .prog_type = BPF_PROG_TYPE_LSM,
+       .kfunc = "bpf",
+       .expected_attach_type = BPF_LSM_MAC,
+       .flags = BPF_F_SLEEPABLE,
+       .errstr = "Unreleased reference",
+       .fixup_kfunc_btf_id = {
+               { "bpf_lookup_user_key", 2 },
+       },
+       .result = REJECT,
+},
+{
+       "reference tracking: leak potential reference to system key",
+       .insns = {
+       BPF_MOV64_IMM(BPF_REG_1, 1),
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .prog_type = BPF_PROG_TYPE_LSM,
+       .kfunc = "bpf",
+       .expected_attach_type = BPF_LSM_MAC,
+       .flags = BPF_F_SLEEPABLE,
+       .errstr = "Unreleased reference",
+       .fixup_kfunc_btf_id = {
+               { "bpf_lookup_system_key", 1 },
+       },
+       .result = REJECT,
+},
 {
        "reference tracking: release reference without check",
        .insns = {