Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Minimal BPF JIT image disassembler |
| 3 | * |
| 4 | * Disassembles BPF JIT compiler emitted opcodes back to asm insn's for |
| 5 | * debugging or verification purposes. |
| 6 | * |
| 7 | * To get the disassembly of the JIT code, do the following: |
| 8 | * |
| 9 | * 1) `echo 2 > /proc/sys/net/core/bpf_jit_enable` |
| 10 | * 2) Load a BPF filter (e.g. `tcpdump -p -n -s 0 -i eth1 host 192.168.20.0/24`) |
| 11 | * 3) Run e.g. `bpf_jit_disasm -o` to read out the last JIT code |
| 12 | * |
| 13 | * Copyright 2013 Daniel Borkmann <borkmann@redhat.com> |
| 14 | * Licensed under the GNU General Public License, version 2.0 (GPLv2) |
| 15 | */ |
| 16 | |
| 17 | #include <stdint.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <assert.h> |
| 21 | #include <unistd.h> |
| 22 | #include <string.h> |
| 23 | #include <bfd.h> |
| 24 | #include <dis-asm.h> |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 25 | #include <regex.h> |
| 26 | #include <fcntl.h> |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 27 | #include <sys/klog.h> |
| 28 | #include <sys/types.h> |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 29 | #include <sys/stat.h> |
Prashant Bhole | cdc89c9 | 2017-11-02 17:09:45 +0900 | [diff] [blame] | 30 | #include <limits.h> |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 31 | |
| 32 | #define CMD_ACTION_SIZE_BUFFER 10 |
| 33 | #define CMD_ACTION_READ_ALL 3 |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 34 | |
| 35 | static void get_exec_path(char *tpath, size_t size) |
| 36 | { |
| 37 | char *path; |
| 38 | ssize_t len; |
| 39 | |
| 40 | snprintf(tpath, size, "/proc/%d/exe", (int) getpid()); |
| 41 | tpath[size - 1] = 0; |
| 42 | |
| 43 | path = strdup(tpath); |
| 44 | assert(path); |
| 45 | |
| 46 | len = readlink(path, tpath, size); |
| 47 | tpath[len] = 0; |
| 48 | |
| 49 | free(path); |
| 50 | } |
| 51 | |
Alexei Starovoitov | ed4afd4 | 2014-05-15 15:56:38 -0700 | [diff] [blame] | 52 | static void get_asm_insns(uint8_t *image, size_t len, int opcodes) |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 53 | { |
| 54 | int count, i, pc = 0; |
Prashant Bhole | cdc89c9 | 2017-11-02 17:09:45 +0900 | [diff] [blame] | 55 | char tpath[PATH_MAX]; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 56 | struct disassemble_info info; |
| 57 | disassembler_ftype disassemble; |
| 58 | bfd *bfdf; |
| 59 | |
| 60 | memset(tpath, 0, sizeof(tpath)); |
| 61 | get_exec_path(tpath, sizeof(tpath)); |
| 62 | |
| 63 | bfdf = bfd_openr(tpath, NULL); |
| 64 | assert(bfdf); |
| 65 | assert(bfd_check_format(bfdf, bfd_object)); |
| 66 | |
| 67 | init_disassemble_info(&info, stdout, (fprintf_ftype) fprintf); |
| 68 | info.arch = bfd_get_arch(bfdf); |
| 69 | info.mach = bfd_get_mach(bfdf); |
| 70 | info.buffer = image; |
| 71 | info.buffer_length = len; |
| 72 | |
| 73 | disassemble_init_for_target(&info); |
| 74 | |
| 75 | disassemble = disassembler(bfdf); |
| 76 | assert(disassemble); |
| 77 | |
| 78 | do { |
| 79 | printf("%4x:\t", pc); |
| 80 | |
| 81 | count = disassemble(pc, &info); |
| 82 | |
| 83 | if (opcodes) { |
| 84 | printf("\n\t"); |
| 85 | for (i = 0; i < count; ++i) |
| 86 | printf("%02x ", (uint8_t) image[pc + i]); |
| 87 | } |
| 88 | printf("\n"); |
| 89 | |
| 90 | pc += count; |
| 91 | } while(count > 0 && pc < len); |
| 92 | |
| 93 | bfd_close(bfdf); |
| 94 | } |
| 95 | |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 96 | static char *get_klog_buff(unsigned int *klen) |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 97 | { |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 98 | int ret, len; |
| 99 | char *buff; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 100 | |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 101 | len = klogctl(CMD_ACTION_SIZE_BUFFER, NULL, 0); |
Colin Ian King | 25a5434 | 2016-05-05 23:39:33 +0100 | [diff] [blame] | 102 | if (len < 0) |
| 103 | return NULL; |
| 104 | |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 105 | buff = malloc(len); |
| 106 | if (!buff) |
| 107 | return NULL; |
| 108 | |
| 109 | ret = klogctl(CMD_ACTION_READ_ALL, buff, len); |
| 110 | if (ret < 0) { |
| 111 | free(buff); |
| 112 | return NULL; |
| 113 | } |
| 114 | |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 115 | *klen = ret; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 116 | return buff; |
| 117 | } |
| 118 | |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 119 | static char *get_flog_buff(const char *file, unsigned int *klen) |
| 120 | { |
| 121 | int fd, ret, len; |
| 122 | struct stat fi; |
| 123 | char *buff; |
| 124 | |
| 125 | fd = open(file, O_RDONLY); |
| 126 | if (fd < 0) |
| 127 | return NULL; |
| 128 | |
| 129 | ret = fstat(fd, &fi); |
| 130 | if (ret < 0 || !S_ISREG(fi.st_mode)) |
| 131 | goto out; |
| 132 | |
| 133 | len = fi.st_size + 1; |
| 134 | buff = malloc(len); |
| 135 | if (!buff) |
| 136 | goto out; |
| 137 | |
| 138 | memset(buff, 0, len); |
| 139 | ret = read(fd, buff, len - 1); |
| 140 | if (ret <= 0) |
| 141 | goto out_free; |
| 142 | |
| 143 | close(fd); |
| 144 | *klen = ret; |
| 145 | return buff; |
| 146 | out_free: |
| 147 | free(buff); |
| 148 | out: |
| 149 | close(fd); |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | static char *get_log_buff(const char *file, unsigned int *klen) |
| 154 | { |
| 155 | return file ? get_flog_buff(file, klen) : get_klog_buff(klen); |
| 156 | } |
| 157 | |
| 158 | static void put_log_buff(char *buff) |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 159 | { |
| 160 | free(buff); |
| 161 | } |
| 162 | |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 163 | static uint8_t *get_last_jit_image(char *haystack, size_t hlen, |
| 164 | unsigned int *ilen) |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 165 | { |
| 166 | char *ptr, *pptr, *tmp; |
| 167 | off_t off = 0; |
| 168 | int ret, flen, proglen, pass, ulen = 0; |
| 169 | regmatch_t pmatch[1]; |
Alexei Starovoitov | ed4afd4 | 2014-05-15 15:56:38 -0700 | [diff] [blame] | 170 | unsigned long base; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 171 | regex_t regex; |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 172 | uint8_t *image; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 173 | |
| 174 | if (hlen == 0) |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 175 | return NULL; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 176 | |
| 177 | ret = regcomp(®ex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ " |
| 178 | "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED); |
| 179 | assert(ret == 0); |
| 180 | |
| 181 | ptr = haystack; |
Daniel Borkmann | 082739a | 2015-05-25 14:08:03 +0200 | [diff] [blame] | 182 | memset(pmatch, 0, sizeof(pmatch)); |
| 183 | |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 184 | while (1) { |
| 185 | ret = regexec(®ex, ptr, 1, pmatch, 0); |
| 186 | if (ret == 0) { |
| 187 | ptr += pmatch[0].rm_eo; |
| 188 | off += pmatch[0].rm_eo; |
| 189 | assert(off < hlen); |
| 190 | } else |
| 191 | break; |
| 192 | } |
| 193 | |
| 194 | ptr = haystack + off - (pmatch[0].rm_eo - pmatch[0].rm_so); |
| 195 | ret = sscanf(ptr, "flen=%d proglen=%d pass=%d image=%lx", |
Alexei Starovoitov | ed4afd4 | 2014-05-15 15:56:38 -0700 | [diff] [blame] | 196 | &flen, &proglen, &pass, &base); |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 197 | if (ret != 4) { |
| 198 | regfree(®ex); |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 199 | return NULL; |
| 200 | } |
| 201 | if (proglen > 1000000) { |
| 202 | printf("proglen of %d too big, stopping\n", proglen); |
| 203 | return NULL; |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 204 | } |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 205 | |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 206 | image = malloc(proglen); |
| 207 | if (!image) { |
| 208 | printf("Out of memory\n"); |
| 209 | return NULL; |
| 210 | } |
| 211 | memset(image, 0, proglen); |
| 212 | |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 213 | tmp = ptr = haystack + off; |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 214 | while ((ptr = strtok(tmp, "\n")) != NULL && ulen < proglen) { |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 215 | tmp = NULL; |
| 216 | if (!strstr(ptr, "JIT code")) |
| 217 | continue; |
| 218 | pptr = ptr; |
| 219 | while ((ptr = strstr(pptr, ":"))) |
| 220 | pptr = ptr + 1; |
| 221 | ptr = pptr; |
| 222 | do { |
| 223 | image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16); |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 224 | if (ptr == pptr) { |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 225 | ulen--; |
| 226 | break; |
| 227 | } |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 228 | if (ulen >= proglen) |
| 229 | break; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 230 | ptr = pptr; |
| 231 | } while (1); |
| 232 | } |
| 233 | |
| 234 | assert(ulen == proglen); |
| 235 | printf("%d bytes emitted from JIT compiler (pass:%d, flen:%d)\n", |
| 236 | proglen, pass, flen); |
Alexei Starovoitov | ed4afd4 | 2014-05-15 15:56:38 -0700 | [diff] [blame] | 237 | printf("%lx + <x>:\n", base); |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 238 | |
| 239 | regfree(®ex); |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 240 | *ilen = ulen; |
| 241 | return image; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 244 | static void usage(void) |
| 245 | { |
| 246 | printf("Usage: bpf_jit_disasm [...]\n"); |
| 247 | printf(" -o Also display related opcodes (default: off).\n"); |
David Daney | b6518e6 | 2017-04-11 14:30:52 -0700 | [diff] [blame] | 248 | printf(" -O <file> Write binary image of code to file, don't disassemble to stdout.\n"); |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 249 | printf(" -f <file> Read last image dump from file or stdin (default: klog).\n"); |
| 250 | printf(" -h Display this help.\n"); |
| 251 | } |
| 252 | |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 253 | int main(int argc, char **argv) |
| 254 | { |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 255 | unsigned int len, klen, opt, opcodes = 0; |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 256 | char *kbuff, *file = NULL; |
David Daney | b6518e6 | 2017-04-11 14:30:52 -0700 | [diff] [blame] | 257 | char *ofile = NULL; |
| 258 | int ofd; |
| 259 | ssize_t nr; |
| 260 | uint8_t *pos; |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 261 | uint8_t *image = NULL; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 262 | |
David Daney | b6518e6 | 2017-04-11 14:30:52 -0700 | [diff] [blame] | 263 | while ((opt = getopt(argc, argv, "of:O:")) != -1) { |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 264 | switch (opt) { |
| 265 | case 'o': |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 266 | opcodes = 1; |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 267 | break; |
David Daney | b6518e6 | 2017-04-11 14:30:52 -0700 | [diff] [blame] | 268 | case 'O': |
| 269 | ofile = optarg; |
| 270 | break; |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 271 | case 'f': |
| 272 | file = optarg; |
| 273 | break; |
| 274 | default: |
| 275 | usage(); |
| 276 | return -1; |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
| 280 | bfd_init(); |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 281 | |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 282 | kbuff = get_log_buff(file, &klen); |
| 283 | if (!kbuff) { |
| 284 | fprintf(stderr, "Could not retrieve log buffer!\n"); |
| 285 | return -1; |
| 286 | } |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 287 | |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 288 | image = get_last_jit_image(kbuff, klen, &len); |
| 289 | if (!image) { |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 290 | fprintf(stderr, "No JIT image found!\n"); |
David Daney | b6518e6 | 2017-04-11 14:30:52 -0700 | [diff] [blame] | 291 | goto done; |
| 292 | } |
| 293 | if (!ofile) { |
| 294 | get_asm_insns(image, len, opcodes); |
| 295 | goto done; |
| 296 | } |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 297 | |
David Daney | b6518e6 | 2017-04-11 14:30:52 -0700 | [diff] [blame] | 298 | ofd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE); |
| 299 | if (ofd < 0) { |
| 300 | fprintf(stderr, "Could not open file %s for writing: ", ofile); |
| 301 | perror(NULL); |
| 302 | goto done; |
| 303 | } |
| 304 | pos = image; |
| 305 | do { |
| 306 | nr = write(ofd, pos, len); |
| 307 | if (nr < 0) { |
| 308 | fprintf(stderr, "Could not write data to %s: ", ofile); |
| 309 | perror(NULL); |
| 310 | goto done; |
| 311 | } |
| 312 | len -= nr; |
| 313 | pos += nr; |
| 314 | } while (len); |
| 315 | close(ofd); |
| 316 | |
| 317 | done: |
Daniel Borkmann | a6ed383 | 2015-07-30 16:04:22 +0200 | [diff] [blame] | 318 | put_log_buff(kbuff); |
David Daney | e274da1 | 2017-06-13 16:49:35 -0700 | [diff] [blame] | 319 | free(image); |
Daniel Borkmann | e306e2c | 2013-03-20 12:11:47 +0000 | [diff] [blame] | 320 | return 0; |
| 321 | } |