Back to home page

OSCL-LXR

 
 

    


0001 #include <errno.h>
0002 #include <stdlib.h>
0003 #include <stdio.h>
0004 #include <string.h>
0005 #include <unistd.h>
0006 #include <sys/time.h>
0007 
0008 #include <linux/bpf.h>
0009 #include <linux/filter.h>
0010 #include <linux/unistd.h>
0011 
0012 #include <bpf/bpf.h>
0013 
0014 #define LOG_SIZE (1 << 20)
0015 
0016 #define err(str...) printf("ERROR: " str)
0017 
0018 static const struct bpf_insn code_sample[] = {
0019     /* We need a few instructions to pass the min log length */
0020     BPF_MOV64_IMM(BPF_REG_0, 0),
0021     BPF_MOV64_IMM(BPF_REG_0, 0),
0022     BPF_MOV64_IMM(BPF_REG_0, 0),
0023     BPF_MOV64_IMM(BPF_REG_0, 0),
0024     BPF_MOV64_IMM(BPF_REG_0, 0),
0025     BPF_MOV64_IMM(BPF_REG_0, 0),
0026     BPF_MOV64_IMM(BPF_REG_0, 0),
0027     BPF_MOV64_IMM(BPF_REG_0, 0),
0028     BPF_MOV64_IMM(BPF_REG_0, 0),
0029     BPF_MOV64_IMM(BPF_REG_0, 0),
0030     BPF_MOV64_IMM(BPF_REG_0, 0),
0031     BPF_MOV64_IMM(BPF_REG_0, 0),
0032     BPF_MOV64_IMM(BPF_REG_0, 0),
0033     BPF_MOV64_IMM(BPF_REG_0, 0),
0034     BPF_MOV64_IMM(BPF_REG_0, 0),
0035     BPF_MOV64_IMM(BPF_REG_0, 0),
0036     BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
0037              BPF_FUNC_map_lookup_elem),
0038     BPF_EXIT_INSN(),
0039 };
0040 
0041 static inline __u64 ptr_to_u64(const void *ptr)
0042 {
0043     return (__u64) (unsigned long) ptr;
0044 }
0045 
0046 static int load(char *log, size_t log_len, int log_level)
0047 {
0048     union bpf_attr attr;
0049 
0050     bzero(&attr, sizeof(attr));
0051     attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
0052     attr.insn_cnt = (__u32)(sizeof(code_sample) / sizeof(struct bpf_insn));
0053     attr.insns = ptr_to_u64(code_sample);
0054     attr.license = ptr_to_u64("GPL");
0055     attr.log_buf = ptr_to_u64(log);
0056     attr.log_size = log_len;
0057     attr.log_level = log_level;
0058 
0059     return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
0060 }
0061 
0062 static void check_ret(int ret, int exp_errno)
0063 {
0064     if (ret > 0) {
0065         close(ret);
0066         err("broken sample loaded successfully!?\n");
0067         exit(1);
0068     }
0069 
0070     if (!ret || errno != exp_errno) {
0071         err("Program load returned: ret:%d/errno:%d, expected ret:%d/errno:%d\n",
0072             ret, errno, -1, exp_errno);
0073         exit(1);
0074     }
0075 }
0076 
0077 static void check_ones(const char *buf, size_t len, const char *msg)
0078 {
0079     while (len--)
0080         if (buf[len] != 1) {
0081             err("%s", msg);
0082             exit(1);
0083         }
0084 }
0085 
0086 static void test_log_good(char *log, size_t buf_len, size_t log_len,
0087               size_t exp_len, int exp_errno, const char *full_log)
0088 {
0089     size_t len;
0090     int ret;
0091 
0092     memset(log, 1, buf_len);
0093 
0094     ret = load(log, log_len, 1);
0095     check_ret(ret, exp_errno);
0096 
0097     len = strnlen(log, buf_len);
0098     if (len == buf_len) {
0099         err("verifier did not NULL terminate the log\n");
0100         exit(1);
0101     }
0102     if (exp_len && len != exp_len) {
0103         err("incorrect log length expected:%zd have:%zd\n",
0104             exp_len, len);
0105         exit(1);
0106     }
0107 
0108     if (strchr(log, 1)) {
0109         err("verifier leaked a byte through\n");
0110         exit(1);
0111     }
0112 
0113     check_ones(log + len + 1, buf_len - len - 1,
0114            "verifier wrote bytes past NULL termination\n");
0115 
0116     if (memcmp(full_log, log, LOG_SIZE)) {
0117         err("log did not match expected output\n");
0118         exit(1);
0119     }
0120 }
0121 
0122 static void test_log_bad(char *log, size_t log_len, int log_level)
0123 {
0124     int ret;
0125 
0126     ret = load(log, log_len, log_level);
0127     check_ret(ret, EINVAL);
0128     if (log)
0129         check_ones(log, LOG_SIZE,
0130                "verifier touched log with bad parameters\n");
0131 }
0132 
0133 int main(int argc, char **argv)
0134 {
0135     char full_log[LOG_SIZE];
0136     char log[LOG_SIZE];
0137     size_t want_len;
0138     int i;
0139 
0140     memset(log, 1, LOG_SIZE);
0141 
0142     /* Use libbpf 1.0 API mode */
0143     libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
0144 
0145     /* Test incorrect attr */
0146     printf("Test log_level 0...\n");
0147     test_log_bad(log, LOG_SIZE, 0);
0148 
0149     printf("Test log_size < 128...\n");
0150     test_log_bad(log, 15, 1);
0151 
0152     printf("Test log_buff = NULL...\n");
0153     test_log_bad(NULL, LOG_SIZE, 1);
0154 
0155     /* Test with log big enough */
0156     printf("Test oversized buffer...\n");
0157     test_log_good(full_log, LOG_SIZE, LOG_SIZE, 0, EACCES, full_log);
0158 
0159     want_len = strlen(full_log);
0160 
0161     printf("Test exact buffer...\n");
0162     test_log_good(log, LOG_SIZE, want_len + 2, want_len, EACCES, full_log);
0163 
0164     printf("Test undersized buffers...\n");
0165     for (i = 0; i < 64; i++) {
0166         full_log[want_len - i + 1] = 1;
0167         full_log[want_len - i] = 0;
0168 
0169         test_log_good(log, LOG_SIZE, want_len + 1 - i, want_len - i,
0170                   ENOSPC, full_log);
0171     }
0172 
0173     printf("test_verifier_log: OK\n");
0174     return 0;
0175 }