Skip to content

Commit 779ec92

Browse files
laoarintel-lab-lkp
authored andcommitted
selftests/bpf: Add selftest for allow_ptr_leaks
- Without prev commit $ tools/testing/selftests/bpf/test_progs --name=tc_bpf torvalds#232/1 tc_bpf/tc_bpf_root:OK test_tc_bpf_non_root:PASS:set_cap_bpf_cap_net_admin 0 nsec test_tc_bpf_non_root:PASS:disable_cap_sys_admin 0 nsec 0: R1=ctx(off=0,imm=0) R10=fp0 ; if ((long)(iph + 1) > (long)skb->data_end) 0: (61) r2 = *(u32 *)(r1 +80) ; R1=ctx(off=0,imm=0) R2_w=pkt_end(off=0,imm=0) ; struct iphdr *iph = (void *)(long)skb->data + sizeof(struct ethhdr); 1: (61) r1 = *(u32 *)(r1 +76) ; R1_w=pkt(off=0,r=0,imm=0) ; if ((long)(iph + 1) > (long)skb->data_end) 2: (07) r1 += 34 ; R1_w=pkt(off=34,r=0,imm=0) 3: (b4) w0 = 1 ; R0_w=1 4: (2d) if r1 > r2 goto pc+1 R2 pointer comparison prohibited processed 5 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0 test_tc_bpf_non_root:FAIL:test_tc_bpf__open_and_load unexpected error: -13 torvalds#233/2 tc_bpf_non_root:FAIL - With prev commit $ tools/testing/selftests/bpf/test_progs --name=tc_bpf torvalds#232/1 tc_bpf/tc_bpf_root:OK torvalds#232/2 tc_bpf/tc_bpf_non_root:OK torvalds#232 tc_bpf:OK Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
1 parent aa16a15 commit 779ec92

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

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

+35-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <test_progs.h>
44
#include <linux/pkt_cls.h>
55

6+
#include "cap_helpers.h"
67
#include "test_tc_bpf.skel.h"
78

89
#define LO_IFINDEX 1
@@ -327,7 +328,7 @@ static int test_tc_bpf_api(struct bpf_tc_hook *hook, int fd)
327328
return 0;
328329
}
329330

330-
void test_tc_bpf(void)
331+
void tc_bpf_root(void)
331332
{
332333
DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = LO_IFINDEX,
333334
.attach_point = BPF_TC_INGRESS);
@@ -393,3 +394,36 @@ void test_tc_bpf(void)
393394
}
394395
test_tc_bpf__destroy(skel);
395396
}
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+
}

tools/testing/selftests/bpf/progs/test_tc_bpf.c

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <linux/bpf.h>
44
#include <bpf/bpf_helpers.h>
5+
#include <linux/if_ether.h>
6+
#include <linux/ip.h>
57

68
/* Dummy prog to test TC-BPF API */
79

@@ -10,3 +12,14 @@ int cls(struct __sk_buff *skb)
1012
{
1113
return 0;
1214
}
15+
16+
/* Prog to verify tc-bpf without cap_sys_admin and cap_perfmon */
17+
SEC("tcx/ingress")
18+
int pkt_ptr(struct __sk_buff *skb)
19+
{
20+
struct iphdr *iph = (void *)(long)skb->data + sizeof(struct ethhdr);
21+
22+
if ((long)(iph + 1) > (long)skb->data_end)
23+
return 1;
24+
return 0;
25+
}

0 commit comments

Comments
 (0)