bpf_jit_disasm.c 6.39 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Minimal BPF JIT image disassembler
 *
 * Disassembles BPF JIT compiler emitted opcodes back to asm insn's for
 * debugging or verification purposes.
 *
 * To get the disassembly of the JIT code, do the following:
 *
 *  1) `echo 2 > /proc/sys/net/core/bpf_jit_enable`
 *  2) Load a BPF filter (e.g. `tcpdump -p -n -s 0 -i eth1 host 192.168.20.0/24`)
 *  3) Run e.g. `bpf_jit_disasm -o` to read out the last JIT code
 *
 * Copyright 2013 Daniel Borkmann <borkmann@redhat.com>
 */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <bfd.h>
#include <dis-asm.h>
25 26
#include <regex.h>
#include <fcntl.h>
27 28
#include <sys/klog.h>
#include <sys/types.h>
29
#include <sys/stat.h>
30
#include <limits.h>
31 32 33

#define CMD_ACTION_SIZE_BUFFER		10
#define CMD_ACTION_READ_ALL		3
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

static void get_exec_path(char *tpath, size_t size)
{
	char *path;
	ssize_t len;

	snprintf(tpath, size, "/proc/%d/exe", (int) getpid());
	tpath[size - 1] = 0;

	path = strdup(tpath);
	assert(path);

	len = readlink(path, tpath, size);
	tpath[len] = 0;

	free(path);
}

52
static void get_asm_insns(uint8_t *image, size_t len, int opcodes)
53 54
{
	int count, i, pc = 0;
55
	char tpath[PATH_MAX];
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
	struct disassemble_info info;
	disassembler_ftype disassemble;
	bfd *bfdf;

	memset(tpath, 0, sizeof(tpath));
	get_exec_path(tpath, sizeof(tpath));

	bfdf = bfd_openr(tpath, NULL);
	assert(bfdf);
	assert(bfd_check_format(bfdf, bfd_object));

	init_disassemble_info(&info, stdout, (fprintf_ftype) fprintf);
	info.arch = bfd_get_arch(bfdf);
	info.mach = bfd_get_mach(bfdf);
	info.buffer = image;
	info.buffer_length = len;

	disassemble_init_for_target(&info);

75 76 77 78 79 80
#ifdef DISASM_FOUR_ARGS_SIGNATURE
	disassemble = disassembler(info.arch,
				   bfd_big_endian(bfdf),
				   info.mach,
				   bfdf);
#else
81
	disassemble = disassembler(bfdf);
82
#endif
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	assert(disassemble);

	do {
		printf("%4x:\t", pc);

		count = disassemble(pc, &info);

		if (opcodes) {
			printf("\n\t");
			for (i = 0; i < count; ++i)
				printf("%02x ", (uint8_t) image[pc + i]);
		}
		printf("\n");

		pc += count;
	} while(count > 0 && pc < len);

	bfd_close(bfdf);
}

103
static char *get_klog_buff(unsigned int *klen)
104
{
105 106 107 108
	int ret, len;
	char *buff;

	len = klogctl(CMD_ACTION_SIZE_BUFFER, NULL, 0);
109 110 111
	if (len < 0)
		return NULL;

112 113 114 115 116 117 118 119 120
	buff = malloc(len);
	if (!buff)
		return NULL;

	ret = klogctl(CMD_ACTION_READ_ALL, buff, len);
	if (ret < 0) {
		free(buff);
		return NULL;
	}
121 122

	*klen = ret;
123 124
	return buff;
}
125

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
static char *get_flog_buff(const char *file, unsigned int *klen)
{
	int fd, ret, len;
	struct stat fi;
	char *buff;

	fd = open(file, O_RDONLY);
	if (fd < 0)
		return NULL;

	ret = fstat(fd, &fi);
	if (ret < 0 || !S_ISREG(fi.st_mode))
		goto out;

	len = fi.st_size + 1;
	buff = malloc(len);
	if (!buff)
		goto out;

	memset(buff, 0, len);
	ret = read(fd, buff, len - 1);
	if (ret <= 0)
		goto out_free;

	close(fd);
	*klen = ret;
152
	return buff;
153 154 155 156 157 158 159 160 161 162
out_free:
	free(buff);
out:
	close(fd);
	return NULL;
}

static char *get_log_buff(const char *file, unsigned int *klen)
{
	return file ? get_flog_buff(file, klen) : get_klog_buff(klen);
163 164
}

165
static void put_log_buff(char *buff)
166 167 168 169
{
	free(buff);
}

170 171
static uint8_t *get_last_jit_image(char *haystack, size_t hlen,
				   unsigned int *ilen)
172 173 174
{
	char *ptr, *pptr, *tmp;
	off_t off = 0;
175 176
	unsigned int proglen;
	int ret, flen, pass, ulen = 0;
177
	regmatch_t pmatch[1];
178
	unsigned long base;
179
	regex_t regex;
180
	uint8_t *image;
181 182

	if (hlen == 0)
183
		return NULL;
184 185 186 187 188 189

	ret = regcomp(&regex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ "
		      "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED);
	assert(ret == 0);

	ptr = haystack;
190 191
	memset(pmatch, 0, sizeof(pmatch));

192 193 194 195 196 197 198 199 200 201 202
	while (1) {
		ret = regexec(&regex, ptr, 1, pmatch, 0);
		if (ret == 0) {
			ptr += pmatch[0].rm_eo;
			off += pmatch[0].rm_eo;
			assert(off < hlen);
		} else
			break;
	}

	ptr = haystack + off - (pmatch[0].rm_eo - pmatch[0].rm_so);
203
	ret = sscanf(ptr, "flen=%d proglen=%u pass=%d image=%lx",
204
		     &flen, &proglen, &pass, &base);
205 206
	if (ret != 4) {
		regfree(&regex);
207 208 209 210 211
		return NULL;
	}
	if (proglen > 1000000) {
		printf("proglen of %d too big, stopping\n", proglen);
		return NULL;
212
	}
213

214 215 216 217 218 219 220
	image = malloc(proglen);
	if (!image) {
		printf("Out of memory\n");
		return NULL;
	}
	memset(image, 0, proglen);

221
	tmp = ptr = haystack + off;
222
	while ((ptr = strtok(tmp, "\n")) != NULL && ulen < proglen) {
223 224 225 226 227 228 229 230 231
		tmp = NULL;
		if (!strstr(ptr, "JIT code"))
			continue;
		pptr = ptr;
		while ((ptr = strstr(pptr, ":")))
			pptr = ptr + 1;
		ptr = pptr;
		do {
			image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16);
232
			if (ptr == pptr) {
233 234 235
				ulen--;
				break;
			}
236 237
			if (ulen >= proglen)
				break;
238 239 240 241 242
			ptr = pptr;
		} while (1);
	}

	assert(ulen == proglen);
243
	printf("%u bytes emitted from JIT compiler (pass:%d, flen:%d)\n",
244
	       proglen, pass, flen);
245
	printf("%lx + <x>:\n", base);
246 247

	regfree(&regex);
248 249
	*ilen = ulen;
	return image;
250 251
}

252 253 254 255
static void usage(void)
{
	printf("Usage: bpf_jit_disasm [...]\n");
	printf("       -o          Also display related opcodes (default: off).\n");
256
	printf("       -O <file>   Write binary image of code to file, don't disassemble to stdout.\n");
257 258 259 260
	printf("       -f <file>   Read last image dump from file or stdin (default: klog).\n");
	printf("       -h          Display this help.\n");
}

261 262
int main(int argc, char **argv)
{
263 264
	unsigned int len, klen, opt, opcodes = 0;
	char *kbuff, *file = NULL;
265 266 267 268
	char *ofile = NULL;
	int ofd;
	ssize_t nr;
	uint8_t *pos;
269
	uint8_t *image = NULL;
270

271
	while ((opt = getopt(argc, argv, "of:O:")) != -1) {
272 273
		switch (opt) {
		case 'o':
274
			opcodes = 1;
275
			break;
276 277 278
		case 'O':
			ofile = optarg;
			break;
279 280 281 282 283 284
		case 'f':
			file = optarg;
			break;
		default:
			usage();
			return -1;
285 286 287 288 289
		}
	}

	bfd_init();

290 291 292 293 294
	kbuff = get_log_buff(file, &klen);
	if (!kbuff) {
		fprintf(stderr, "Could not retrieve log buffer!\n");
		return -1;
	}
295

296 297
	image = get_last_jit_image(kbuff, klen, &len);
	if (!image) {
298
		fprintf(stderr, "No JIT image found!\n");
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
		goto done;
	}
	if (!ofile) {
		get_asm_insns(image, len, opcodes);
		goto done;
	}

	ofd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE);
	if (ofd < 0) {
		fprintf(stderr, "Could not open file %s for writing: ", ofile);
		perror(NULL);
		goto done;
	}
	pos = image;
	do {
		nr = write(ofd, pos, len);
		if (nr < 0) {
			fprintf(stderr, "Could not write data to %s: ", ofile);
			perror(NULL);
			goto done;
		}
		len -= nr;
		pos += nr;
	} while (len);
	close(ofd);
324

325
done:
326
	put_log_buff(kbuff);
327
	free(image);
328 329
	return 0;
}