5740 lines · 167.1 KB · C
RawDownload
/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com * Copyright (c) 2016 Facebook * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. */#include <linux/kernel.h>#include <linux/types.h>#include <linux/slab.h>#include <linux/bpf.h>#include <linux/bpf_verifier.h>#include <linux/filter.h>#include <net/netlink.h>#include <linux/file.h>#include <linux/vmalloc.h>#include <linux/stringify.h>#include <linux/bsearch.h>#include <linux/sort.h> #include "disasm.h" static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {#define BPF_PROG_TYPE(_id, _name) \	[_id] = & _name ## _verifier_ops,#define BPF_MAP_TYPE(_id, _ops)#include <linux/bpf_types.h>#undef BPF_PROG_TYPE#undef BPF_MAP_TYPE}; /* bpf_check() is a static code analyzer that walks eBPF program * instruction by instruction and updates register/stack state. * All paths of conditional branches are analyzed until 'bpf_exit' insn. * * The first pass is depth-first-search to check that the program is a DAG. * It rejects the following programs: * - larger than BPF_MAXINSNS insns * - if loop is present (detected via back-edge) * - unreachable insns exist (shouldn't be a forest. program = one function) * - out of bounds or malformed jumps * The second pass is all possible path descent from the 1st insn. * Since it's analyzing all pathes through the program, the length of the * analysis is limited to 64k insn, which may be hit even if total number of * insn is less then 4K, but there are too many branches that change stack/regs. * Number of 'branches to be analyzed' is limited to 1k * * On entry to each instruction, each register has a type, and the instruction * changes the types of the registers depending on instruction semantics. * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is * copied to R1. * * All registers are 64-bit. * R0 - return register * R1-R5 argument passing registers * R6-R9 callee saved registers * R10 - frame pointer read-only * * At the start of BPF program the register R1 contains a pointer to bpf_context * and has type PTR_TO_CTX. * * Verifier tracks arithmetic operations on pointers in case: *    BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), *    BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), * 1st insn copies R10 (which has FRAME_PTR) type into R1 * and 2nd arithmetic instruction is pattern matched to recognize * that it wants to construct a pointer to some element within stack. * So after 2nd insn, the register R1 has type PTR_TO_STACK * (and -20 constant is saved for further stack bounds checking). * Meaning that this reg is a pointer to stack plus known immediate constant. * * Most of the time the registers have SCALAR_VALUE type, which * means the register has some value, but it's not a valid pointer. * (like pointer plus pointer becomes SCALAR_VALUE type) * * When verifier sees load or store instructions the type of base register * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK. These are three pointer * types recognized by check_mem_access() function. * * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' * and the range of [ptr, ptr + map's value_size) is accessible. * * registers used to pass values to function calls are checked against * function argument constraints. * * ARG_PTR_TO_MAP_KEY is one of such argument constraints. * It means that the register type passed to this function must be * PTR_TO_STACK and it will be used inside the function as * 'pointer to map element key' * * For example the argument constraints for bpf_map_lookup_elem(): *   .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, *   .arg1_type = ARG_CONST_MAP_PTR, *   .arg2_type = ARG_PTR_TO_MAP_KEY, * * ret_type says that this function returns 'pointer to map elem value or null' * function expects 1st argument to be a const pointer to 'struct bpf_map' and * 2nd argument should be a pointer to stack, which will be used inside * the helper function as a pointer to map element key. * * On the kernel side the helper function looks like: * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) * { *    struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; *    void *key = (void *) (unsigned long) r2; *    void *value; * *    here kernel can access 'key' and 'map' pointers safely, knowing that *    [key, key + map->key_size) bytes are valid and were initialized on *    the stack of eBPF program. * } * * Corresponding eBPF program may look like: *    BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),  // after this insn R2 type is FRAME_PTR *    BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK *    BPF_LD_MAP_FD(BPF_REG_1, map_fd),      // after this insn R1 type is CONST_PTR_TO_MAP *    BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), * here verifier looks at prototype of map_lookup_elem() and sees: * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, * Now verifier knows that this map has key of R1->map_ptr->key_size bytes * * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, * Now verifier checks that [R2, R2 + map's key_size) are within stack limits * and were initialized prior to this call. * If it's ok, then verifier allows this BPF_CALL insn and looks at * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function * returns ether pointer to map value or NULL. * * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' * insn, the register holding that pointer in the true branch changes state to * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false * branch. See check_cond_jmp_op(). * * After the call R0 is set to return type of the function and registers R1-R5 * are set to NOT_INIT to indicate that they are no longer readable. */ /* verifier_state + insn_idx are pushed to stack when branch is encountered */struct bpf_verifier_stack_elem {	/* verifer state is 'st'	 * before processing instruction 'insn_idx'	 * and after processing instruction 'prev_insn_idx'	 */	struct bpf_verifier_state st;	int insn_idx;	int prev_insn_idx;	struct bpf_verifier_stack_elem *next;}; #define BPF_COMPLEXITY_LIMIT_INSNS	131072#define BPF_COMPLEXITY_LIMIT_STACK	1024 #define BPF_MAP_PTR_POISON ((void *)0xeB9F + POISON_POINTER_DELTA) struct bpf_call_arg_meta {	struct bpf_map *map_ptr;	bool raw_mode;	bool pkt_access;	int regno;	int access_size;}; static DEFINE_MUTEX(bpf_verifier_lock); /* log_level controls verbosity level of eBPF verifier. * bpf_verifier_log_write() is used to dump the verification trace to the log, * so the user can figure out what's wrong with the program */__printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,					   const char *fmt, ...){	struct bpf_verifer_log *log = &env->log;	unsigned int n;	va_list args; 	if (!log->level || !log->ubuf || bpf_verifier_log_full(log))		return; 	va_start(args, fmt);	n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args);	va_end(args); 	WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1,		  "verifier log line truncated - local buffer too short\n"); 	n = min(log->len_total - log->len_used - 1, n);	log->kbuf[n] = '\0'; 	if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1))		log->len_used += n;	else		log->ubuf = NULL;}EXPORT_SYMBOL_GPL(bpf_verifier_log_write);/* Historically bpf_verifier_log_write was called verbose, but the name was too * generic for symbol export. The function was renamed, but not the calls in * the verifier to avoid complicating backports. Hence the alias below. */static __printf(2, 3) void verbose(struct bpf_verifier_env *env,				   const char *fmt, ...)	__attribute__((alias("bpf_verifier_log_write"))); static bool type_is_pkt_pointer(enum bpf_reg_type type){	return type == PTR_TO_PACKET ||	       type == PTR_TO_PACKET_META;} /* string representation of 'enum bpf_reg_type' */static const char * const reg_type_str[] = {	[NOT_INIT]		= "?",	[SCALAR_VALUE]		= "inv",	[PTR_TO_CTX]		= "ctx",	[CONST_PTR_TO_MAP]	= "map_ptr",	[PTR_TO_MAP_VALUE]	= "map_value",	[PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",	[PTR_TO_STACK]		= "fp",	[PTR_TO_PACKET]		= "pkt",	[PTR_TO_PACKET_META]	= "pkt_meta",	[PTR_TO_PACKET_END]	= "pkt_end",}; static void print_liveness(struct bpf_verifier_env *env,			   enum bpf_reg_liveness live){	if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN))	    verbose(env, "_");	if (live & REG_LIVE_READ)		verbose(env, "r");	if (live & REG_LIVE_WRITTEN)		verbose(env, "w");} static struct bpf_func_state *func(struct bpf_verifier_env *env,				   const struct bpf_reg_state *reg){	struct bpf_verifier_state *cur = env->cur_state; 	return cur->frame[reg->frameno];} static void print_verifier_state(struct bpf_verifier_env *env,				 const struct bpf_func_state *state){	const struct bpf_reg_state *reg;	enum bpf_reg_type t;	int i; 	if (state->frameno)		verbose(env, " frame%d:", state->frameno);	for (i = 0; i < MAX_BPF_REG; i++) {		reg = &state->regs[i];		t = reg->type;		if (t == NOT_INIT)			continue;		verbose(env, " R%d", i);		print_liveness(env, reg->live);		verbose(env, "=%s", reg_type_str[t]);		if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&		    tnum_is_const(reg->var_off)) {			/* reg->off should be 0 for SCALAR_VALUE */			verbose(env, "%lld", reg->var_off.value + reg->off);			if (t == PTR_TO_STACK)				verbose(env, ",call_%d", func(env, reg)->callsite);		} else {			verbose(env, "(id=%d", reg->id);			if (t != SCALAR_VALUE)				verbose(env, ",off=%d", reg->off);			if (type_is_pkt_pointer(t))				verbose(env, ",r=%d", reg->range);			else if (t == CONST_PTR_TO_MAP ||				 t == PTR_TO_MAP_VALUE ||				 t == PTR_TO_MAP_VALUE_OR_NULL)				verbose(env, ",ks=%d,vs=%d",					reg->map_ptr->key_size,					reg->map_ptr->value_size);			if (tnum_is_const(reg->var_off)) {				/* Typically an immediate SCALAR_VALUE, but				 * could be a pointer whose offset is too big				 * for reg->off				 */				verbose(env, ",imm=%llx", reg->var_off.value);			} else {				if (reg->smin_value != reg->umin_value &&				    reg->smin_value != S64_MIN)					verbose(env, ",smin_value=%lld",						(long long)reg->smin_value);				if (reg->smax_value != reg->umax_value &&				    reg->smax_value != S64_MAX)					verbose(env, ",smax_value=%lld",						(long long)reg->smax_value);				if (reg->umin_value != 0)					verbose(env, ",umin_value=%llu",						(unsigned long long)reg->umin_value);				if (reg->umax_value != U64_MAX)					verbose(env, ",umax_value=%llu",						(unsigned long long)reg->umax_value);				if (!tnum_is_unknown(reg->var_off)) {					char tn_buf[48]; 					tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);					verbose(env, ",var_off=%s", tn_buf);				}			}			verbose(env, ")");		}	}	for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {		if (state->stack[i].slot_type[0] == STACK_SPILL) {			verbose(env, " fp%d",				(-i - 1) * BPF_REG_SIZE);			print_liveness(env, state->stack[i].spilled_ptr.live);			verbose(env, "=%s",				reg_type_str[state->stack[i].spilled_ptr.type]);		}		if (state->stack[i].slot_type[0] == STACK_ZERO)			verbose(env, " fp%d=0", (-i - 1) * BPF_REG_SIZE);	}	verbose(env, "\n");} static int copy_stack_state(struct bpf_func_state *dst,			    const struct bpf_func_state *src){	if (!src->stack)		return 0;	if (WARN_ON_ONCE(dst->allocated_stack < src->allocated_stack)) {		/* internal bug, make state invalid to reject the program */		memset(dst, 0, sizeof(*dst));		return -EFAULT;	}	memcpy(dst->stack, src->stack,	       sizeof(*src->stack) * (src->allocated_stack / BPF_REG_SIZE));	return 0;} /* do_check() starts with zero-sized stack in struct bpf_verifier_state to * make it consume minimal amount of memory. check_stack_write() access from * the program calls into realloc_func_state() to grow the stack size. * Note there is a non-zero 'parent' pointer inside bpf_verifier_state * which this function copies over. It points to previous bpf_verifier_state * which is never reallocated */static int realloc_func_state(struct bpf_func_state *state, int size,			      bool copy_old){	u32 old_size = state->allocated_stack;	struct bpf_stack_state *new_stack;	int slot = size / BPF_REG_SIZE; 	if (size <= old_size || !size) {		if (copy_old)			return 0;		state->allocated_stack = slot * BPF_REG_SIZE;		if (!size && old_size) {			kfree(state->stack);			state->stack = NULL;		}		return 0;	}	new_stack = kmalloc_array(slot, sizeof(struct bpf_stack_state),				  GFP_KERNEL);	if (!new_stack)		return -ENOMEM;	if (copy_old) {		if (state->stack)			memcpy(new_stack, state->stack,			       sizeof(*new_stack) * (old_size / BPF_REG_SIZE));		memset(new_stack + old_size / BPF_REG_SIZE, 0,		       sizeof(*new_stack) * (size - old_size) / BPF_REG_SIZE);	}	state->allocated_stack = slot * BPF_REG_SIZE;	kfree(state->stack);	state->stack = new_stack;	return 0;} static void free_func_state(struct bpf_func_state *state){	if (!state)		return;	kfree(state->stack);	kfree(state);} static void free_verifier_state(struct bpf_verifier_state *state,				bool free_self){	int i; 	for (i = 0; i <= state->curframe; i++) {		free_func_state(state->frame[i]);		state->frame[i] = NULL;	}	if (free_self)		kfree(state);} /* copy verifier state from src to dst growing dst stack space * when necessary to accommodate larger src stack */static int copy_func_state(struct bpf_func_state *dst,			   const struct bpf_func_state *src){	int err; 	err = realloc_func_state(dst, src->allocated_stack, false);	if (err)		return err;	memcpy(dst, src, offsetof(struct bpf_func_state, allocated_stack));	return copy_stack_state(dst, src);} static int copy_verifier_state(struct bpf_verifier_state *dst_state,			       const struct bpf_verifier_state *src){	struct bpf_func_state *dst;	int i, err; 	/* if dst has more stack frames then src frame, free them */	for (i = src->curframe + 1; i <= dst_state->curframe; i++) {		free_func_state(dst_state->frame[i]);		dst_state->frame[i] = NULL;	}	dst_state->curframe = src->curframe;	dst_state->parent = src->parent;	for (i = 0; i <= src->curframe; i++) {		dst = dst_state->frame[i];		if (!dst) {			dst = kzalloc(sizeof(*dst), GFP_KERNEL);			if (!dst)				return -ENOMEM;			dst_state->frame[i] = dst;		}		err = copy_func_state(dst, src->frame[i]);		if (err)			return err;	}	return 0;} static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx,		     int *insn_idx){	struct bpf_verifier_state *cur = env->cur_state;	struct bpf_verifier_stack_elem *elem, *head = env->head;	int err; 	if (env->head == NULL)		return -ENOENT; 	if (cur) {		err = copy_verifier_state(cur, &head->st);		if (err)			return err;	}	if (insn_idx)		*insn_idx = head->insn_idx;	if (prev_insn_idx)		*prev_insn_idx = head->prev_insn_idx;	elem = head->next;	free_verifier_state(&head->st, false);	kfree(head);	env->head = elem;	env->stack_size--;	return 0;} static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,					     int insn_idx, int prev_insn_idx){	struct bpf_verifier_state *cur = env->cur_state;	struct bpf_verifier_stack_elem *elem;	int err; 	elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);	if (!elem)		goto err; 	elem->insn_idx = insn_idx;	elem->prev_insn_idx = prev_insn_idx;	elem->next = env->head;	env->head = elem;	env->stack_size++;	err = copy_verifier_state(&elem->st, cur);	if (err)		goto err;	if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {		verbose(env, "BPF program is too complex\n");		goto err;	}	return &elem->st;err:	free_verifier_state(env->cur_state, true);

Lines 1–500 of 5740.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t