|
3 | 3 | #include <test_progs.h>
|
4 | 4 | #include <linux/pkt_cls.h>
|
5 | 5 |
|
| 6 | +#include "cap_helpers.h" |
6 | 7 | #include "test_tc_bpf.skel.h"
|
7 | 8 |
|
8 | 9 | #define LO_IFINDEX 1
|
@@ -327,7 +328,7 @@ static int test_tc_bpf_api(struct bpf_tc_hook *hook, int fd)
|
327 | 328 | return 0;
|
328 | 329 | }
|
329 | 330 |
|
330 |
| -void test_tc_bpf(void) |
| 331 | +void tc_bpf_root(void) |
331 | 332 | {
|
332 | 333 | DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = LO_IFINDEX,
|
333 | 334 | .attach_point = BPF_TC_INGRESS);
|
@@ -393,3 +394,36 @@ void test_tc_bpf(void)
|
393 | 394 | }
|
394 | 395 | test_tc_bpf__destroy(skel);
|
395 | 396 | }
|
| 397 | + |
| 398 | +void tc_bpf_non_root(void) |
| 399 | +{ |
| 400 | + struct test_tc_bpf *skel = NULL; |
| 401 | + __u64 caps = 0; |
| 402 | + int ret; |
| 403 | + |
| 404 | + /* In case CAP_BPF and CAP_PERFMON is not set */ |
| 405 | + ret = cap_enable_effective(1ULL << CAP_BPF | 1ULL << CAP_NET_ADMIN, &caps); |
| 406 | + if (!ASSERT_OK(ret, "set_cap_bpf_cap_net_admin")) |
| 407 | + return; |
| 408 | + ret = cap_disable_effective(1ULL << CAP_SYS_ADMIN | 1ULL << CAP_PERFMON, NULL); |
| 409 | + if (!ASSERT_OK(ret, "disable_cap_sys_admin")) |
| 410 | + goto restore_cap; |
| 411 | + |
| 412 | + skel = test_tc_bpf__open_and_load(); |
| 413 | + if (!ASSERT_OK_PTR(skel, "test_tc_bpf__open_and_load")) |
| 414 | + goto restore_cap; |
| 415 | + |
| 416 | + test_tc_bpf__destroy(skel); |
| 417 | + |
| 418 | +restore_cap: |
| 419 | + if (caps) |
| 420 | + cap_enable_effective(caps, NULL); |
| 421 | +} |
| 422 | + |
| 423 | +void test_tc_bpf(void) |
| 424 | +{ |
| 425 | + if (test__start_subtest("tc_bpf_root")) |
| 426 | + tc_bpf_root(); |
| 427 | + if (test__start_subtest("tc_bpf_non_root")) |
| 428 | + tc_bpf_non_root(); |
| 429 | +} |
0 commit comments