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