9 #include <linux/filter.h>
10 #include <linux/unistd.h>
14 #include "bpf_rlimit.h"
16 #define LOG_SIZE (1 << 20)
18 #define err(str...) printf("ERROR: " str)
20 static const struct bpf_insn code_sample
[] = {
21 /* We need a few instructions to pass the min log length */
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_MOV64_IMM(BPF_REG_0
, 0),
38 BPF_RAW_INSN(BPF_JMP
| BPF_CALL
, 0, 0, 0,
39 BPF_FUNC_map_lookup_elem
),
43 static inline __u64
ptr_to_u64(const void *ptr
)
45 return (__u64
) (unsigned long) ptr
;
48 static int load(char *log
, size_t log_len
, int log_level
)
52 bzero(&attr
, sizeof(attr
));
53 attr
.prog_type
= BPF_PROG_TYPE_SOCKET_FILTER
;
54 attr
.insn_cnt
= (__u32
)(sizeof(code_sample
) / sizeof(struct bpf_insn
));
55 attr
.insns
= ptr_to_u64(code_sample
);
56 attr
.license
= ptr_to_u64("GPL");
57 attr
.log_buf
= ptr_to_u64(log
);
58 attr
.log_size
= log_len
;
59 attr
.log_level
= log_level
;
61 return syscall(__NR_bpf
, BPF_PROG_LOAD
, &attr
, sizeof(attr
));
64 static void check_ret(int ret
, int exp_errno
)
68 err("broken sample loaded successfully!?\n");
72 if (!ret
|| errno
!= exp_errno
) {
73 err("Program load returned: ret:%d/errno:%d, expected ret:%d/errno:%d\n",
74 ret
, errno
, -1, exp_errno
);
79 static void check_ones(const char *buf
, size_t len
, const char *msg
)
88 static void test_log_good(char *log
, size_t buf_len
, size_t log_len
,
89 size_t exp_len
, int exp_errno
, const char *full_log
)
94 memset(log
, 1, buf_len
);
96 ret
= load(log
, log_len
, 1);
97 check_ret(ret
, exp_errno
);
99 len
= strnlen(log
, buf_len
);
100 if (len
== buf_len
) {
101 err("verifier did not NULL terminate the log\n");
104 if (exp_len
&& len
!= exp_len
) {
105 err("incorrect log length expected:%zd have:%zd\n",
110 if (strchr(log
, 1)) {
111 err("verifier leaked a byte through\n");
115 check_ones(log
+ len
+ 1, buf_len
- len
- 1,
116 "verifier wrote bytes past NULL termination\n");
118 if (memcmp(full_log
, log
, LOG_SIZE
)) {
119 err("log did not match expected output\n");
124 static void test_log_bad(char *log
, size_t log_len
, int log_level
)
128 ret
= load(log
, log_len
, log_level
);
129 check_ret(ret
, EINVAL
);
131 check_ones(log
, LOG_SIZE
,
132 "verifier touched log with bad parameters\n");
135 int main(int argc
, char **argv
)
137 char full_log
[LOG_SIZE
];
142 memset(log
, 1, LOG_SIZE
);
144 /* Test incorrect attr */
145 printf("Test log_level 0...\n");
146 test_log_bad(log
, LOG_SIZE
, 0);
148 printf("Test log_size < 128...\n");
149 test_log_bad(log
, 15, 1);
151 printf("Test log_buff = NULL...\n");
152 test_log_bad(NULL
, LOG_SIZE
, 1);
154 /* Test with log big enough */
155 printf("Test oversized buffer...\n");
156 test_log_good(full_log
, LOG_SIZE
, LOG_SIZE
, 0, EACCES
, full_log
);
158 want_len
= strlen(full_log
);
160 printf("Test exact buffer...\n");
161 test_log_good(log
, LOG_SIZE
, want_len
+ 2, want_len
, EACCES
, full_log
);
163 printf("Test undersized buffers...\n");
164 for (i
= 0; i
< 64; i
++) {
165 full_log
[want_len
- i
+ 1] = 1;
166 full_log
[want_len
- i
] = 0;
168 test_log_good(log
, LOG_SIZE
, want_len
+ 1 - i
, want_len
- i
,
172 printf("test_verifier_log: OK\n");