Skip to content

Commit 095ddb5

Browse files
fomichevMartin KaFai Lau
authored and
Martin KaFai Lau
committed
selftests/bpf: Add sockopt case to verify prog_type
Make sure only sockopt programs can be attached to the setsockopt and getsockopt hooks. Signed-off-by: Stanislav Fomichev <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent d70b266 commit 095ddb5

File tree

1 file changed

+38
-2
lines changed
  • tools/testing/selftests/bpf/prog_tests

1 file changed

+38
-2
lines changed

tools/testing/selftests/bpf/prog_tests/sockopt.c

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ enum sockopt_test_error {
2424
static struct sockopt_test {
2525
const char *descr;
2626
const struct bpf_insn insns[64];
27+
enum bpf_prog_type prog_type;
2728
enum bpf_attach_type attach_type;
2829
enum bpf_attach_type expected_attach_type;
2930

@@ -928,9 +929,40 @@ static struct sockopt_test {
928929

929930
.error = EPERM_SETSOCKOPT,
930931
},
932+
933+
/* ==================== prog_type ==================== */
934+
935+
{
936+
.descr = "can attach only BPF_CGROUP_SETSOCKOP",
937+
.insns = {
938+
/* return 1 */
939+
BPF_MOV64_IMM(BPF_REG_0, 1),
940+
BPF_EXIT_INSN(),
941+
942+
},
943+
.prog_type = BPF_PROG_TYPE_CGROUP_SKB,
944+
.attach_type = BPF_CGROUP_SETSOCKOPT,
945+
.expected_attach_type = 0,
946+
.error = DENY_ATTACH,
947+
},
948+
949+
{
950+
.descr = "can attach only BPF_CGROUP_GETSOCKOP",
951+
.insns = {
952+
/* return 1 */
953+
BPF_MOV64_IMM(BPF_REG_0, 1),
954+
BPF_EXIT_INSN(),
955+
956+
},
957+
.prog_type = BPF_PROG_TYPE_CGROUP_SKB,
958+
.attach_type = BPF_CGROUP_GETSOCKOPT,
959+
.expected_attach_type = 0,
960+
.error = DENY_ATTACH,
961+
},
931962
};
932963

933964
static int load_prog(const struct bpf_insn *insns,
965+
enum bpf_prog_type prog_type,
934966
enum bpf_attach_type expected_attach_type)
935967
{
936968
LIBBPF_OPTS(bpf_prog_load_opts, opts,
@@ -947,7 +979,7 @@ static int load_prog(const struct bpf_insn *insns,
947979
}
948980
insns_cnt++;
949981

950-
fd = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCKOPT, NULL, "GPL", insns, insns_cnt, &opts);
982+
fd = bpf_prog_load(prog_type, NULL, "GPL", insns, insns_cnt, &opts);
951983
if (verbose && fd < 0)
952984
fprintf(stderr, "%s\n", bpf_log_buf);
953985

@@ -1039,11 +1071,15 @@ static int call_getsockopt(bool use_io_uring, int fd, int level, int optname,
10391071
static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring,
10401072
bool use_link)
10411073
{
1074+
int prog_type = BPF_PROG_TYPE_CGROUP_SOCKOPT;
10421075
int sock_fd, err, prog_fd, link_fd = -1;
10431076
void *optval = NULL;
10441077
int ret = 0;
10451078

1046-
prog_fd = load_prog(test->insns, test->expected_attach_type);
1079+
if (test->prog_type)
1080+
prog_type = test->prog_type;
1081+
1082+
prog_fd = load_prog(test->insns, prog_type, test->expected_attach_type);
10471083
if (prog_fd < 0) {
10481084
if (test->error == DENY_LOAD)
10491085
return 0;

0 commit comments

Comments
 (0)