8344 lines · 238.1 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-only/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com * Copyright (c) 2016 Facebook * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io */#include <uapi/linux/btf.h>#include <linux/kernel.h>#include <linux/types.h>#include <linux/slab.h>#include <linux/bpf.h>#include <linux/btf.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 <linux/perf_event.h>#include <linux/ctype.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, PTR_TO_SOCKET. These are * four 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. * * The following reference types represent a potential reference to a kernel * resource which, after first being allocated, must be checked and freed by * the BPF program: * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET * * When the verifier sees a helper call return a reference type, it allocates a * pointer id for the reference and stores it in the current function state. * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type * passes through a NULL-check conditional. For the branch wherein the state is * changed to CONST_IMM, the verifier releases the reference. * * For each helper function that allocates a reference, such as * bpf_sk_lookup_tcp(), there is a corresponding release function, such as * bpf_sk_release(). When a reference type passes into the release function, * the verifier also releases the reference. If any unchecked or unreleased * reference remains at the end of the program, the verifier rejects it. */ /* 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_STACK	1024#define BPF_COMPLEXITY_LIMIT_STATES	64 #define BPF_MAP_PTR_UNPRIV	1UL#define BPF_MAP_PTR_POISON	((void *)((0xeB9FUL << 1) +	\					  POISON_POINTER_DELTA))#define BPF_MAP_PTR(X)		((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux){	return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON;} static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux){	return aux->map_state & BPF_MAP_PTR_UNPRIV;} static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux,			      const struct bpf_map *map, bool unpriv){	BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV);	unpriv |= bpf_map_ptr_unpriv(aux);	aux->map_state = (unsigned long)map |			 (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL);} struct bpf_call_arg_meta {	struct bpf_map *map_ptr;	bool raw_mode;	bool pkt_access;	int regno;	int access_size;	s64 msize_smax_value;	u64 msize_umax_value;	int ref_obj_id;	int func_id;}; static DEFINE_MUTEX(bpf_verifier_lock); static const struct bpf_line_info *find_linfo(const struct bpf_verifier_env *env, u32 insn_off){	const struct bpf_line_info *linfo;	const struct bpf_prog *prog;	u32 i, nr_linfo; 	prog = env->prog;	nr_linfo = prog->aux->nr_linfo; 	if (!nr_linfo || insn_off >= prog->len)		return NULL; 	linfo = prog->aux->linfo;	for (i = 1; i < nr_linfo; i++)		if (insn_off < linfo[i].insn_off)			break; 	return &linfo[i - 1];} void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt,		       va_list args){	unsigned int n; 	n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, 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;} /* 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, ...){	va_list args; 	if (!bpf_verifier_log_needed(&env->log))		return; 	va_start(args, fmt);	bpf_verifier_vlog(&env->log, fmt, args);	va_end(args);}EXPORT_SYMBOL_GPL(bpf_verifier_log_write); __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...){	struct bpf_verifier_env *env = private_data;	va_list args; 	if (!bpf_verifier_log_needed(&env->log))		return; 	va_start(args, fmt);	bpf_verifier_vlog(&env->log, fmt, args);	va_end(args);} static const char *ltrim(const char *s){	while (isspace(*s))		s++; 	return s;} __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env,					 u32 insn_off,					 const char *prefix_fmt, ...){	const struct bpf_line_info *linfo; 	if (!bpf_verifier_log_needed(&env->log))		return; 	linfo = find_linfo(env, insn_off);	if (!linfo || linfo == env->prev_linfo)		return; 	if (prefix_fmt) {		va_list args; 		va_start(args, prefix_fmt);		bpf_verifier_vlog(&env->log, prefix_fmt, args);		va_end(args);	} 	verbose(env, "%s\n",		ltrim(btf_name_by_offset(env->prog->aux->btf,					 linfo->line_off))); 	env->prev_linfo = linfo;} static bool type_is_pkt_pointer(enum bpf_reg_type type){	return type == PTR_TO_PACKET ||	       type == PTR_TO_PACKET_META;} static bool type_is_sk_pointer(enum bpf_reg_type type){	return type == PTR_TO_SOCKET ||		type == PTR_TO_SOCK_COMMON ||		type == PTR_TO_TCP_SOCK;} static bool reg_type_may_be_null(enum bpf_reg_type type){	return type == PTR_TO_MAP_VALUE_OR_NULL ||	       type == PTR_TO_SOCKET_OR_NULL ||	       type == PTR_TO_SOCK_COMMON_OR_NULL ||	       type == PTR_TO_TCP_SOCK_OR_NULL;} static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg){	return reg->type == PTR_TO_MAP_VALUE &&		map_value_has_spin_lock(reg->map_ptr);} static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type){	return type == PTR_TO_SOCKET ||		type == PTR_TO_SOCKET_OR_NULL ||		type == PTR_TO_TCP_SOCK ||		type == PTR_TO_TCP_SOCK_OR_NULL;} static bool arg_type_may_be_refcounted(enum bpf_arg_type type){	return type == ARG_PTR_TO_SOCK_COMMON;} /* Determine whether the function releases some resources allocated by another * function call. The first reference type argument will be assumed to be * released by release_reference(). */static bool is_release_function(enum bpf_func_id func_id){	return func_id == BPF_FUNC_sk_release;} static bool is_acquire_function(enum bpf_func_id func_id){	return func_id == BPF_FUNC_sk_lookup_tcp ||		func_id == BPF_FUNC_sk_lookup_udp ||		func_id == BPF_FUNC_skc_lookup_tcp;} static bool is_ptr_cast_function(enum bpf_func_id func_id){	return func_id == BPF_FUNC_tcp_sock ||		func_id == BPF_FUNC_sk_fullsock;} /* 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",	[PTR_TO_FLOW_KEYS]	= "flow_keys",	[PTR_TO_SOCKET]		= "sock",	[PTR_TO_SOCKET_OR_NULL] = "sock_or_null",	[PTR_TO_SOCK_COMMON]	= "sock_common",	[PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null",	[PTR_TO_TCP_SOCK]	= "tcp_sock",	[PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null",	[PTR_TO_TP_BUFFER]	= "tp_buffer",}; static char slot_type_char[] = {	[STACK_INVALID]	= '?',	[STACK_SPILL]	= 'r',	[STACK_MISC]	= 'm',	[STACK_ZERO]	= '0',}; static void print_liveness(struct bpf_verifier_env *env,			   enum bpf_reg_liveness live){	if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE))	    verbose(env, "_");	if (live & REG_LIVE_READ)		verbose(env, "r");	if (live & REG_LIVE_WRITTEN)		verbose(env, "w");	if (live & REG_LIVE_DONE)		verbose(env, "D");} 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 (reg_type_may_be_refcounted_or_null(t))				verbose(env, ",ref_obj_id=%d", reg->ref_obj_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++) {		char types_buf[BPF_REG_SIZE + 1];

Lines 1–500 of 8344.