selftests/bpf: Migrate ATTACH_REJECT test cases

Migrate test case from bpf/test_sock_addr.c ensuring that program
attachment fails when using an inappropriate attach type.

Signed-off-by: Jordan Rife <jrife@google.com>
Link: https://lore.kernel.org/r/20240510190246.3247730-12-jrife@google.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Jordan Rife 2024-05-10 14:02:28 -05:00 committed by Alexei Starovoitov
parent b0f3af0bff
commit cded71f595
2 changed files with 102 additions and 146 deletions

View File

@ -438,13 +438,19 @@ static void prog_name##_destroy(void *skel) \
}
BPF_SKEL_FUNCS(bind4_prog, bind_v4_prog);
BPF_SKEL_FUNCS_RAW(bind4_prog, bind_v4_prog);
BPF_SKEL_FUNCS(bind6_prog, bind_v6_prog);
BPF_SKEL_FUNCS_RAW(bind6_prog, bind_v6_prog);
BPF_SKEL_FUNCS(connect4_prog, connect_v4_prog);
BPF_SKEL_FUNCS_RAW(connect4_prog, connect_v4_prog);
BPF_SKEL_FUNCS(connect6_prog, connect_v6_prog);
BPF_SKEL_FUNCS_RAW(connect6_prog, connect_v6_prog);
BPF_SKEL_FUNCS(connect_unix_prog, connect_unix_prog);
BPF_SKEL_FUNCS(sendmsg4_prog, sendmsg_v4_prog);
BPF_SKEL_FUNCS_RAW(sendmsg4_prog, sendmsg_v4_prog);
BPF_SKEL_FUNCS(sendmsg4_prog, sendmsg_v4_deny_prog);
BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_prog);
BPF_SKEL_FUNCS_RAW(sendmsg6_prog, sendmsg_v6_prog);
BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_deny_prog);
BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_preserve_dst_prog);
BPF_SKEL_FUNCS(sendmsg6_prog, sendmsg_v6_v4mapped_prog);
@ -506,6 +512,22 @@ static struct sock_addr_test tests[] = {
NULL,
LOAD_REJECT,
},
{
SOCK_ADDR_TEST_BIND,
"bind4: attach prog with wrong attach type",
bind_v4_prog_load_raw,
bind_v4_prog_destroy_raw,
BPF_CGROUP_INET6_BIND,
&user_ops,
AF_INET,
SOCK_STREAM,
NULL,
0,
NULL,
0,
NULL,
ATTACH_REJECT,
},
{
SOCK_ADDR_TEST_BIND,
"bind6: bind (stream)",
@ -554,6 +576,22 @@ static struct sock_addr_test tests[] = {
NULL,
LOAD_REJECT,
},
{
SOCK_ADDR_TEST_BIND,
"bind6: attach prog with wrong attach type",
bind_v6_prog_load_raw,
bind_v6_prog_destroy_raw,
BPF_CGROUP_INET4_BIND,
&user_ops,
AF_INET,
SOCK_STREAM,
NULL,
0,
NULL,
0,
NULL,
ATTACH_REJECT,
},
/* bind - kernel calls */
{
@ -670,6 +708,22 @@ static struct sock_addr_test tests[] = {
NULL,
LOAD_REJECT,
},
{
SOCK_ADDR_TEST_CONNECT,
"connect4: attach prog with wrong attach type",
connect_v4_prog_load_raw,
connect_v4_prog_destroy_raw,
BPF_CGROUP_INET6_CONNECT,
&user_ops,
AF_INET,
SOCK_STREAM,
NULL,
0,
NULL,
0,
NULL,
ATTACH_REJECT,
},
{
SOCK_ADDR_TEST_CONNECT,
"connect6: connect (stream)",
@ -718,6 +772,22 @@ static struct sock_addr_test tests[] = {
NULL,
LOAD_REJECT,
},
{
SOCK_ADDR_TEST_CONNECT,
"connect6: attach prog with wrong attach type",
connect_v6_prog_load_raw,
connect_v6_prog_destroy_raw,
BPF_CGROUP_INET4_CONNECT,
&user_ops,
AF_INET,
SOCK_STREAM,
NULL,
0,
NULL,
0,
NULL,
ATTACH_REJECT,
},
{
SOCK_ADDR_TEST_CONNECT,
"connect_unix: connect (stream)",
@ -866,6 +936,22 @@ static struct sock_addr_test tests[] = {
NULL,
LOAD_REJECT,
},
{
SOCK_ADDR_TEST_SENDMSG,
"sendmsg4: attach prog with wrong attach type",
sendmsg_v4_prog_load_raw,
sendmsg_v4_prog_destroy_raw,
BPF_CGROUP_UDP6_SENDMSG,
&user_ops,
AF_INET,
SOCK_DGRAM,
NULL,
0,
NULL,
0,
NULL,
ATTACH_REJECT,
},
{
SOCK_ADDR_TEST_SENDMSG,
"sendmsg6: sendmsg (dgram)",
@ -962,6 +1048,22 @@ static struct sock_addr_test tests[] = {
NULL,
LOAD_REJECT,
},
{
SOCK_ADDR_TEST_SENDMSG,
"sendmsg6: attach prog with wrong attach type",
sendmsg_v6_prog_load_raw,
sendmsg_v6_prog_destroy_raw,
BPF_CGROUP_UDP4_SENDMSG,
&user_ops,
AF_INET6,
SOCK_DGRAM,
NULL,
0,
NULL,
0,
NULL,
ATTACH_REJECT,
},
{
SOCK_ADDR_TEST_SENDMSG,
"sendmsg_unix: sendmsg (dgram)",

View File

@ -88,89 +88,11 @@ struct sock_addr_test {
} expected_result;
};
static int bind4_prog_load(const struct sock_addr_test *test);
static int bind6_prog_load(const struct sock_addr_test *test);
static int connect4_prog_load(const struct sock_addr_test *test);
static int connect6_prog_load(const struct sock_addr_test *test);
static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test);
static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test);
static struct sock_addr_test tests[] = {
/* bind */
{
"bind4: attach prog with wrong attach type",
bind4_prog_load,
BPF_CGROUP_INET4_BIND,
BPF_CGROUP_INET6_BIND,
AF_INET,
SOCK_STREAM,
NULL,
0,
NULL,
0,
NULL,
ATTACH_REJECT,
},
{
"bind6: attach prog with wrong attach type",
bind6_prog_load,
BPF_CGROUP_INET6_BIND,
BPF_CGROUP_INET4_BIND,
AF_INET,
SOCK_STREAM,
NULL,
0,
NULL,
0,
NULL,
ATTACH_REJECT,
},
/* connect */
{
"connect4: attach prog with wrong attach type",
connect4_prog_load,
BPF_CGROUP_INET4_CONNECT,
BPF_CGROUP_INET6_CONNECT,
AF_INET,
SOCK_STREAM,
NULL,
0,
NULL,
0,
NULL,
ATTACH_REJECT,
},
{
"connect6: attach prog with wrong attach type",
connect6_prog_load,
BPF_CGROUP_INET6_CONNECT,
BPF_CGROUP_INET4_CONNECT,
AF_INET,
SOCK_STREAM,
NULL,
0,
NULL,
0,
NULL,
ATTACH_REJECT,
},
/* sendmsg */
{
"sendmsg4: attach prog with wrong attach type",
sendmsg4_rw_asm_prog_load,
BPF_CGROUP_UDP4_SENDMSG,
BPF_CGROUP_UDP6_SENDMSG,
AF_INET,
SOCK_DGRAM,
NULL,
0,
NULL,
0,
NULL,
ATTACH_REJECT,
},
{
"sendmsg4: rewrite IP & port (asm)",
sendmsg4_rw_asm_prog_load,
@ -185,20 +107,6 @@ static struct sock_addr_test tests[] = {
SRC4_REWRITE_IP,
SUCCESS,
},
{
"sendmsg6: attach prog with wrong attach type",
sendmsg6_rw_asm_prog_load,
BPF_CGROUP_UDP6_SENDMSG,
BPF_CGROUP_UDP4_SENDMSG,
AF_INET6,
SOCK_DGRAM,
NULL,
0,
NULL,
0,
NULL,
ATTACH_REJECT,
},
{
"sendmsg6: rewrite IP & port (asm)",
sendmsg6_rw_asm_prog_load,
@ -234,60 +142,6 @@ static int load_insns(const struct sock_addr_test *test,
return ret;
}
static int load_path(const struct sock_addr_test *test, const char *path)
{
struct bpf_object *obj;
struct bpf_program *prog;
int err;
obj = bpf_object__open_file(path, NULL);
err = libbpf_get_error(obj);
if (err) {
log_err(">>> Opening BPF object (%s) error.\n", path);
return -1;
}
prog = bpf_object__next_program(obj, NULL);
if (!prog)
goto err_out;
bpf_program__set_type(prog, BPF_PROG_TYPE_CGROUP_SOCK_ADDR);
bpf_program__set_expected_attach_type(prog, test->expected_attach_type);
bpf_program__set_flags(prog, testing_prog_flags());
err = bpf_object__load(obj);
if (err) {
if (test->expected_result != LOAD_REJECT)
log_err(">>> Loading program (%s) error.\n", path);
goto err_out;
}
return bpf_program__fd(prog);
err_out:
bpf_object__close(obj);
return -1;
}
static int bind4_prog_load(const struct sock_addr_test *test)
{
return load_path(test, BIND4_PROG_PATH);
}
static int bind6_prog_load(const struct sock_addr_test *test)
{
return load_path(test, BIND6_PROG_PATH);
}
static int connect4_prog_load(const struct sock_addr_test *test)
{
return load_path(test, CONNECT4_PROG_PATH);
}
static int connect6_prog_load(const struct sock_addr_test *test)
{
return load_path(test, CONNECT6_PROG_PATH);
}
static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test)
{
struct sockaddr_in dst4_rw_addr;