7 #include <sys/resource.h>
10 #include <linux/filter.h>
11 #include <linux/unistd.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
),
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
)
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
)
67 err("broken sample loaded successfully!?\n");
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
);
78 static void check_ones(const char *buf
, size_t len
, const char *msg
)
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
)
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
);
100 err("verifier did not NULL terminate the log\n");
103 if (exp_len
&& len
!= exp_len
) {
104 err("incorrect log length expected:%zd have:%zd\n",
109 if (strchr(log
, 1)) {
110 err("verifier leaked a byte through\n");
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");
123 static void test_log_bad(char *log
, size_t log_len
, int log_level
)
127 ret
= load(log
, log_len
, log_level
);
128 check_ret(ret
, EINVAL
);
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
];
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
,
176 printf("test_verifier_log: OK\n");