bpf_jit_disasm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. #include <stdint.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <assert.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <bfd.h>
  23. #include <dis-asm.h>
  24. #include <regex.h>
  25. #include <fcntl.h>
  26. #include <sys/klog.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <limits.h>
  30. #define CMD_ACTION_SIZE_BUFFER 10
  31. #define CMD_ACTION_READ_ALL 3
  32. static void get_exec_path(char *tpath, size_t size)
  33. {
  34. char *path;
  35. ssize_t len;
  36. snprintf(tpath, size, "/proc/%d/exe", (int) getpid());
  37. tpath[size - 1] = 0;
  38. path = strdup(tpath);
  39. assert(path);
  40. len = readlink(path, tpath, size);
  41. tpath[len] = 0;
  42. free(path);
  43. }
  44. static void get_asm_insns(uint8_t *image, size_t len, int opcodes)
  45. {
  46. int count, i, pc = 0;
  47. char tpath[PATH_MAX];
  48. struct disassemble_info info;
  49. disassembler_ftype disassemble;
  50. bfd *bfdf;
  51. memset(tpath, 0, sizeof(tpath));
  52. get_exec_path(tpath, sizeof(tpath));
  53. bfdf = bfd_openr(tpath, NULL);
  54. assert(bfdf);
  55. assert(bfd_check_format(bfdf, bfd_object));
  56. init_disassemble_info(&info, stdout, (fprintf_ftype) fprintf);
  57. info.arch = bfd_get_arch(bfdf);
  58. info.mach = bfd_get_mach(bfdf);
  59. info.buffer = image;
  60. info.buffer_length = len;
  61. disassemble_init_for_target(&info);
  62. #ifdef DISASM_FOUR_ARGS_SIGNATURE
  63. disassemble = disassembler(info.arch,
  64. bfd_big_endian(bfdf),
  65. info.mach,
  66. bfdf);
  67. #else
  68. disassemble = disassembler(bfdf);
  69. #endif
  70. assert(disassemble);
  71. do {
  72. printf("%4x:\t", pc);
  73. count = disassemble(pc, &info);
  74. if (opcodes) {
  75. printf("\n\t");
  76. for (i = 0; i < count; ++i)
  77. printf("%02x ", (uint8_t) image[pc + i]);
  78. }
  79. printf("\n");
  80. pc += count;
  81. } while(count > 0 && pc < len);
  82. bfd_close(bfdf);
  83. }
  84. static char *get_klog_buff(unsigned int *klen)
  85. {
  86. int ret, len;
  87. char *buff;
  88. len = klogctl(CMD_ACTION_SIZE_BUFFER, NULL, 0);
  89. if (len < 0)
  90. return NULL;
  91. buff = malloc(len);
  92. if (!buff)
  93. return NULL;
  94. ret = klogctl(CMD_ACTION_READ_ALL, buff, len);
  95. if (ret < 0) {
  96. free(buff);
  97. return NULL;
  98. }
  99. *klen = ret;
  100. return buff;
  101. }
  102. static char *get_flog_buff(const char *file, unsigned int *klen)
  103. {
  104. int fd, ret, len;
  105. struct stat fi;
  106. char *buff;
  107. fd = open(file, O_RDONLY);
  108. if (fd < 0)
  109. return NULL;
  110. ret = fstat(fd, &fi);
  111. if (ret < 0 || !S_ISREG(fi.st_mode))
  112. goto out;
  113. len = fi.st_size + 1;
  114. buff = malloc(len);
  115. if (!buff)
  116. goto out;
  117. memset(buff, 0, len);
  118. ret = read(fd, buff, len - 1);
  119. if (ret <= 0)
  120. goto out_free;
  121. close(fd);
  122. *klen = ret;
  123. return buff;
  124. out_free:
  125. free(buff);
  126. out:
  127. close(fd);
  128. return NULL;
  129. }
  130. static char *get_log_buff(const char *file, unsigned int *klen)
  131. {
  132. return file ? get_flog_buff(file, klen) : get_klog_buff(klen);
  133. }
  134. static void put_log_buff(char *buff)
  135. {
  136. free(buff);
  137. }
  138. static uint8_t *get_last_jit_image(char *haystack, size_t hlen,
  139. unsigned int *ilen)
  140. {
  141. char *ptr, *pptr, *tmp;
  142. off_t off = 0;
  143. unsigned int proglen;
  144. int ret, flen, pass, ulen = 0;
  145. regmatch_t pmatch[1];
  146. unsigned long base;
  147. regex_t regex;
  148. uint8_t *image;
  149. if (hlen == 0)
  150. return NULL;
  151. ret = regcomp(&regex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ "
  152. "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED);
  153. assert(ret == 0);
  154. ptr = haystack;
  155. memset(pmatch, 0, sizeof(pmatch));
  156. while (1) {
  157. ret = regexec(&regex, ptr, 1, pmatch, 0);
  158. if (ret == 0) {
  159. ptr += pmatch[0].rm_eo;
  160. off += pmatch[0].rm_eo;
  161. assert(off < hlen);
  162. } else
  163. break;
  164. }
  165. ptr = haystack + off - (pmatch[0].rm_eo - pmatch[0].rm_so);
  166. ret = sscanf(ptr, "flen=%d proglen=%u pass=%d image=%lx",
  167. &flen, &proglen, &pass, &base);
  168. if (ret != 4) {
  169. regfree(&regex);
  170. return NULL;
  171. }
  172. if (proglen > 1000000) {
  173. printf("proglen of %d too big, stopping\n", proglen);
  174. return NULL;
  175. }
  176. image = malloc(proglen);
  177. if (!image) {
  178. printf("Out of memory\n");
  179. return NULL;
  180. }
  181. memset(image, 0, proglen);
  182. tmp = ptr = haystack + off;
  183. while ((ptr = strtok(tmp, "\n")) != NULL && ulen < proglen) {
  184. tmp = NULL;
  185. if (!strstr(ptr, "JIT code"))
  186. continue;
  187. pptr = ptr;
  188. while ((ptr = strstr(pptr, ":")))
  189. pptr = ptr + 1;
  190. ptr = pptr;
  191. do {
  192. image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16);
  193. if (ptr == pptr) {
  194. ulen--;
  195. break;
  196. }
  197. if (ulen >= proglen)
  198. break;
  199. ptr = pptr;
  200. } while (1);
  201. }
  202. assert(ulen == proglen);
  203. printf("%u bytes emitted from JIT compiler (pass:%d, flen:%d)\n",
  204. proglen, pass, flen);
  205. printf("%lx + <x>:\n", base);
  206. regfree(&regex);
  207. *ilen = ulen;
  208. return image;
  209. }
  210. static void usage(void)
  211. {
  212. printf("Usage: bpf_jit_disasm [...]\n");
  213. printf(" -o Also display related opcodes (default: off).\n");
  214. printf(" -O <file> Write binary image of code to file, don't disassemble to stdout.\n");
  215. printf(" -f <file> Read last image dump from file or stdin (default: klog).\n");
  216. printf(" -h Display this help.\n");
  217. }
  218. int main(int argc, char **argv)
  219. {
  220. unsigned int len, klen, opt, opcodes = 0;
  221. char *kbuff, *file = NULL;
  222. char *ofile = NULL;
  223. int ofd;
  224. ssize_t nr;
  225. uint8_t *pos;
  226. uint8_t *image = NULL;
  227. while ((opt = getopt(argc, argv, "of:O:")) != -1) {
  228. switch (opt) {
  229. case 'o':
  230. opcodes = 1;
  231. break;
  232. case 'O':
  233. ofile = optarg;
  234. break;
  235. case 'f':
  236. file = optarg;
  237. break;
  238. default:
  239. usage();
  240. return -1;
  241. }
  242. }
  243. bfd_init();
  244. kbuff = get_log_buff(file, &klen);
  245. if (!kbuff) {
  246. fprintf(stderr, "Could not retrieve log buffer!\n");
  247. return -1;
  248. }
  249. image = get_last_jit_image(kbuff, klen, &len);
  250. if (!image) {
  251. fprintf(stderr, "No JIT image found!\n");
  252. goto done;
  253. }
  254. if (!ofile) {
  255. get_asm_insns(image, len, opcodes);
  256. goto done;
  257. }
  258. ofd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE);
  259. if (ofd < 0) {
  260. fprintf(stderr, "Could not open file %s for writing: ", ofile);
  261. perror(NULL);
  262. goto done;
  263. }
  264. pos = image;
  265. do {
  266. nr = write(ofd, pos, len);
  267. if (nr < 0) {
  268. fprintf(stderr, "Could not write data to %s: ", ofile);
  269. perror(NULL);
  270. goto done;
  271. }
  272. len -= nr;
  273. pos += nr;
  274. } while (len);
  275. close(ofd);
  276. done:
  277. put_log_buff(kbuff);
  278. free(image);
  279. return 0;
  280. }