Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / tools / testing / selftests / bpf / test_verifier_log.c
blobe9626cf5607ad060b070680d25986a270c5cd59c
1 #include <errno.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <sys/time.h>
7 #include <sys/resource.h>
9 #include <linux/bpf.h>
10 #include <linux/filter.h>
11 #include <linux/unistd.h>
13 #include <bpf/bpf.h>
15 #define LOG_SIZE (1 << 20)
17 #define err(str...) printf("ERROR: " str)
19 static const struct bpf_insn code_sample[] = {
20 /* We need a few instructions to pass the min log length */
21 BPF_MOV64_IMM(BPF_REG_0, 0),
22 BPF_MOV64_IMM(BPF_REG_0, 0),
23 BPF_MOV64_IMM(BPF_REG_0, 0),
24 BPF_MOV64_IMM(BPF_REG_0, 0),
25 BPF_MOV64_IMM(BPF_REG_0, 0),
26 BPF_MOV64_IMM(BPF_REG_0, 0),
27 BPF_MOV64_IMM(BPF_REG_0, 0),
28 BPF_MOV64_IMM(BPF_REG_0, 0),
29 BPF_MOV64_IMM(BPF_REG_0, 0),
30 BPF_MOV64_IMM(BPF_REG_0, 0),
31 BPF_MOV64_IMM(BPF_REG_0, 0),
32 BPF_MOV64_IMM(BPF_REG_0, 0),
33 BPF_MOV64_IMM(BPF_REG_0, 0),
34 BPF_MOV64_IMM(BPF_REG_0, 0),
35 BPF_MOV64_IMM(BPF_REG_0, 0),
36 BPF_MOV64_IMM(BPF_REG_0, 0),
37 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
38 BPF_FUNC_map_lookup_elem),
39 BPF_EXIT_INSN(),
42 static inline __u64 ptr_to_u64(const void *ptr)
44 return (__u64) (unsigned long) ptr;
47 static int load(char *log, size_t log_len, int log_level)
49 union bpf_attr attr;
51 bzero(&attr, sizeof(attr));
52 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
53 attr.insn_cnt = (__u32)(sizeof(code_sample) / sizeof(struct bpf_insn));
54 attr.insns = ptr_to_u64(code_sample);
55 attr.license = ptr_to_u64("GPL");
56 attr.log_buf = ptr_to_u64(log);
57 attr.log_size = log_len;
58 attr.log_level = log_level;
60 return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
63 static void check_ret(int ret, int exp_errno)
65 if (ret > 0) {
66 close(ret);
67 err("broken sample loaded successfully!?\n");
68 exit(1);
71 if (!ret || errno != exp_errno) {
72 err("Program load returned: ret:%d/errno:%d, expected ret:%d/errno:%d\n",
73 ret, errno, -1, exp_errno);
74 exit(1);
78 static void check_ones(const char *buf, size_t len, const char *msg)
80 while (len--)
81 if (buf[len] != 1) {
82 err("%s", msg);
83 exit(1);
87 static void test_log_good(char *log, size_t buf_len, size_t log_len,
88 size_t exp_len, int exp_errno, const char *full_log)
90 size_t len;
91 int ret;
93 memset(log, 1, buf_len);
95 ret = load(log, log_len, 1);
96 check_ret(ret, exp_errno);
98 len = strnlen(log, buf_len);
99 if (len == buf_len) {
100 err("verifier did not NULL terminate the log\n");
101 exit(1);
103 if (exp_len && len != exp_len) {
104 err("incorrect log length expected:%zd have:%zd\n",
105 exp_len, len);
106 exit(1);
109 if (strchr(log, 1)) {
110 err("verifier leaked a byte through\n");
111 exit(1);
114 check_ones(log + len + 1, buf_len - len - 1,
115 "verifier wrote bytes past NULL termination\n");
117 if (memcmp(full_log, log, LOG_SIZE)) {
118 err("log did not match expected output\n");
119 exit(1);
123 static void test_log_bad(char *log, size_t log_len, int log_level)
125 int ret;
127 ret = load(log, log_len, log_level);
128 check_ret(ret, EINVAL);
129 if (log)
130 check_ones(log, LOG_SIZE,
131 "verifier touched log with bad parameters\n");
134 int main(int argc, char **argv)
136 struct rlimit limit = { RLIM_INFINITY, RLIM_INFINITY };
137 char full_log[LOG_SIZE];
138 char log[LOG_SIZE];
139 size_t want_len;
140 int i;
142 /* allow unlimited locked memory to have more consistent error code */
143 if (setrlimit(RLIMIT_MEMLOCK, &limit) < 0)
144 perror("Unable to lift memlock rlimit");
146 memset(log, 1, LOG_SIZE);
148 /* Test incorrect attr */
149 printf("Test log_level 0...\n");
150 test_log_bad(log, LOG_SIZE, 0);
152 printf("Test log_size < 128...\n");
153 test_log_bad(log, 15, 1);
155 printf("Test log_buff = NULL...\n");
156 test_log_bad(NULL, LOG_SIZE, 1);
158 /* Test with log big enough */
159 printf("Test oversized buffer...\n");
160 test_log_good(full_log, LOG_SIZE, LOG_SIZE, 0, EACCES, full_log);
162 want_len = strlen(full_log);
164 printf("Test exact buffer...\n");
165 test_log_good(log, LOG_SIZE, want_len + 2, want_len, EACCES, full_log);
167 printf("Test undersized buffers...\n");
168 for (i = 0; i < 64; i++) {
169 full_log[want_len - i + 1] = 1;
170 full_log[want_len - i] = 0;
172 test_log_good(log, LOG_SIZE, want_len + 1 - i, want_len - i,
173 ENOSPC, full_log);
176 printf("test_verifier_log: OK\n");
177 return 0;