xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/bpf/test_verifier_log.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun #include <errno.h>
2*4882a593Smuzhiyun #include <stdlib.h>
3*4882a593Smuzhiyun #include <stdio.h>
4*4882a593Smuzhiyun #include <string.h>
5*4882a593Smuzhiyun #include <unistd.h>
6*4882a593Smuzhiyun #include <sys/time.h>
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/bpf.h>
9*4882a593Smuzhiyun #include <linux/filter.h>
10*4882a593Smuzhiyun #include <linux/unistd.h>
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <bpf/bpf.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include "bpf_rlimit.h"
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #define LOG_SIZE (1 << 20)
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define err(str...)	printf("ERROR: " str)
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun static const struct bpf_insn code_sample[] = {
21*4882a593Smuzhiyun 	/* We need a few instructions to pass the min log length */
22*4882a593Smuzhiyun 	BPF_MOV64_IMM(BPF_REG_0, 0),
23*4882a593Smuzhiyun 	BPF_MOV64_IMM(BPF_REG_0, 0),
24*4882a593Smuzhiyun 	BPF_MOV64_IMM(BPF_REG_0, 0),
25*4882a593Smuzhiyun 	BPF_MOV64_IMM(BPF_REG_0, 0),
26*4882a593Smuzhiyun 	BPF_MOV64_IMM(BPF_REG_0, 0),
27*4882a593Smuzhiyun 	BPF_MOV64_IMM(BPF_REG_0, 0),
28*4882a593Smuzhiyun 	BPF_MOV64_IMM(BPF_REG_0, 0),
29*4882a593Smuzhiyun 	BPF_MOV64_IMM(BPF_REG_0, 0),
30*4882a593Smuzhiyun 	BPF_MOV64_IMM(BPF_REG_0, 0),
31*4882a593Smuzhiyun 	BPF_MOV64_IMM(BPF_REG_0, 0),
32*4882a593Smuzhiyun 	BPF_MOV64_IMM(BPF_REG_0, 0),
33*4882a593Smuzhiyun 	BPF_MOV64_IMM(BPF_REG_0, 0),
34*4882a593Smuzhiyun 	BPF_MOV64_IMM(BPF_REG_0, 0),
35*4882a593Smuzhiyun 	BPF_MOV64_IMM(BPF_REG_0, 0),
36*4882a593Smuzhiyun 	BPF_MOV64_IMM(BPF_REG_0, 0),
37*4882a593Smuzhiyun 	BPF_MOV64_IMM(BPF_REG_0, 0),
38*4882a593Smuzhiyun 	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
39*4882a593Smuzhiyun 		     BPF_FUNC_map_lookup_elem),
40*4882a593Smuzhiyun 	BPF_EXIT_INSN(),
41*4882a593Smuzhiyun };
42*4882a593Smuzhiyun 
ptr_to_u64(const void * ptr)43*4882a593Smuzhiyun static inline __u64 ptr_to_u64(const void *ptr)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	return (__u64) (unsigned long) ptr;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun 
load(char * log,size_t log_len,int log_level)48*4882a593Smuzhiyun static int load(char *log, size_t log_len, int log_level)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun 	union bpf_attr attr;
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	bzero(&attr, sizeof(attr));
53*4882a593Smuzhiyun 	attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
54*4882a593Smuzhiyun 	attr.insn_cnt = (__u32)(sizeof(code_sample) / sizeof(struct bpf_insn));
55*4882a593Smuzhiyun 	attr.insns = ptr_to_u64(code_sample);
56*4882a593Smuzhiyun 	attr.license = ptr_to_u64("GPL");
57*4882a593Smuzhiyun 	attr.log_buf = ptr_to_u64(log);
58*4882a593Smuzhiyun 	attr.log_size = log_len;
59*4882a593Smuzhiyun 	attr.log_level = log_level;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun 
check_ret(int ret,int exp_errno)64*4882a593Smuzhiyun static void check_ret(int ret, int exp_errno)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	if (ret > 0) {
67*4882a593Smuzhiyun 		close(ret);
68*4882a593Smuzhiyun 		err("broken sample loaded successfully!?\n");
69*4882a593Smuzhiyun 		exit(1);
70*4882a593Smuzhiyun 	}
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	if (!ret || errno != exp_errno) {
73*4882a593Smuzhiyun 		err("Program load returned: ret:%d/errno:%d, expected ret:%d/errno:%d\n",
74*4882a593Smuzhiyun 		    ret, errno, -1, exp_errno);
75*4882a593Smuzhiyun 		exit(1);
76*4882a593Smuzhiyun 	}
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
check_ones(const char * buf,size_t len,const char * msg)79*4882a593Smuzhiyun static void check_ones(const char *buf, size_t len, const char *msg)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	while (len--)
82*4882a593Smuzhiyun 		if (buf[len] != 1) {
83*4882a593Smuzhiyun 			err("%s", msg);
84*4882a593Smuzhiyun 			exit(1);
85*4882a593Smuzhiyun 		}
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
test_log_good(char * log,size_t buf_len,size_t log_len,size_t exp_len,int exp_errno,const char * full_log)88*4882a593Smuzhiyun static void test_log_good(char *log, size_t buf_len, size_t log_len,
89*4882a593Smuzhiyun 			  size_t exp_len, int exp_errno, const char *full_log)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	size_t len;
92*4882a593Smuzhiyun 	int ret;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	memset(log, 1, buf_len);
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	ret = load(log, log_len, 1);
97*4882a593Smuzhiyun 	check_ret(ret, exp_errno);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	len = strnlen(log, buf_len);
100*4882a593Smuzhiyun 	if (len == buf_len) {
101*4882a593Smuzhiyun 		err("verifier did not NULL terminate the log\n");
102*4882a593Smuzhiyun 		exit(1);
103*4882a593Smuzhiyun 	}
104*4882a593Smuzhiyun 	if (exp_len && len != exp_len) {
105*4882a593Smuzhiyun 		err("incorrect log length expected:%zd have:%zd\n",
106*4882a593Smuzhiyun 		    exp_len, len);
107*4882a593Smuzhiyun 		exit(1);
108*4882a593Smuzhiyun 	}
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	if (strchr(log, 1)) {
111*4882a593Smuzhiyun 		err("verifier leaked a byte through\n");
112*4882a593Smuzhiyun 		exit(1);
113*4882a593Smuzhiyun 	}
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	check_ones(log + len + 1, buf_len - len - 1,
116*4882a593Smuzhiyun 		   "verifier wrote bytes past NULL termination\n");
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	if (memcmp(full_log, log, LOG_SIZE)) {
119*4882a593Smuzhiyun 		err("log did not match expected output\n");
120*4882a593Smuzhiyun 		exit(1);
121*4882a593Smuzhiyun 	}
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun 
test_log_bad(char * log,size_t log_len,int log_level)124*4882a593Smuzhiyun static void test_log_bad(char *log, size_t log_len, int log_level)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	int ret;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	ret = load(log, log_len, log_level);
129*4882a593Smuzhiyun 	check_ret(ret, EINVAL);
130*4882a593Smuzhiyun 	if (log)
131*4882a593Smuzhiyun 		check_ones(log, LOG_SIZE,
132*4882a593Smuzhiyun 			   "verifier touched log with bad parameters\n");
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
main(int argc,char ** argv)135*4882a593Smuzhiyun int main(int argc, char **argv)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun 	char full_log[LOG_SIZE];
138*4882a593Smuzhiyun 	char log[LOG_SIZE];
139*4882a593Smuzhiyun 	size_t want_len;
140*4882a593Smuzhiyun 	int i;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	memset(log, 1, LOG_SIZE);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	/* Test incorrect attr */
145*4882a593Smuzhiyun 	printf("Test log_level 0...\n");
146*4882a593Smuzhiyun 	test_log_bad(log, LOG_SIZE, 0);
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	printf("Test log_size < 128...\n");
149*4882a593Smuzhiyun 	test_log_bad(log, 15, 1);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	printf("Test log_buff = NULL...\n");
152*4882a593Smuzhiyun 	test_log_bad(NULL, LOG_SIZE, 1);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	/* Test with log big enough */
155*4882a593Smuzhiyun 	printf("Test oversized buffer...\n");
156*4882a593Smuzhiyun 	test_log_good(full_log, LOG_SIZE, LOG_SIZE, 0, EACCES, full_log);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	want_len = strlen(full_log);
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	printf("Test exact buffer...\n");
161*4882a593Smuzhiyun 	test_log_good(log, LOG_SIZE, want_len + 2, want_len, EACCES, full_log);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	printf("Test undersized buffers...\n");
164*4882a593Smuzhiyun 	for (i = 0; i < 64; i++) {
165*4882a593Smuzhiyun 		full_log[want_len - i + 1] = 1;
166*4882a593Smuzhiyun 		full_log[want_len - i] = 0;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 		test_log_good(log, LOG_SIZE, want_len + 1 - i, want_len - i,
169*4882a593Smuzhiyun 			      ENOSPC, full_log);
170*4882a593Smuzhiyun 	}
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	printf("test_verifier_log: OK\n");
173*4882a593Smuzhiyun 	return 0;
174*4882a593Smuzhiyun }
175