3394 lines · 86.0 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* * Linux Socket Filter - Kernel level socket filtering * * Based on the design of the Berkeley Packet Filter. The new * internal format has been designed by PLUMgrid: * *	Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com * * Authors: * *	Jay Schulist <jschlst@samba.org> *	Alexei Starovoitov <ast@plumgrid.com> *	Daniel Borkmann <dborkman@redhat.com> * * Andi Kleen - Fix a few bad bugs and races. * Kris Katterjohn - Added many additional checks in bpf_check_classic() */ #include <uapi/linux/btf.h>#include <crypto/sha1.h>#include <linux/filter.h>#include <linux/skbuff.h>#include <linux/vmalloc.h>#include <linux/prandom.h>#include <linux/bpf.h>#include <linux/btf.h>#include <linux/hex.h>#include <linux/objtool.h>#include <linux/overflow.h>#include <linux/rbtree_latch.h>#include <linux/kallsyms.h>#include <linux/rcupdate.h>#include <linux/perf_event.h>#include <linux/extable.h>#include <linux/log2.h>#include <linux/bpf_verifier.h>#include <linux/nodemask.h>#include <linux/nospec.h>#include <linux/bpf_mem_alloc.h>#include <linux/memcontrol.h>#include <linux/execmem.h>#include <crypto/sha2.h> #include <asm/barrier.h>#include <linux/unaligned.h> /* Registers */#define BPF_R0	regs[BPF_REG_0]#define BPF_R1	regs[BPF_REG_1]#define BPF_R2	regs[BPF_REG_2]#define BPF_R3	regs[BPF_REG_3]#define BPF_R4	regs[BPF_REG_4]#define BPF_R5	regs[BPF_REG_5]#define BPF_R6	regs[BPF_REG_6]#define BPF_R7	regs[BPF_REG_7]#define BPF_R8	regs[BPF_REG_8]#define BPF_R9	regs[BPF_REG_9]#define BPF_R10	regs[BPF_REG_10] /* Named registers */#define DST	regs[insn->dst_reg]#define SRC	regs[insn->src_reg]#define FP	regs[BPF_REG_FP]#define AX	regs[BPF_REG_AX]#define ARG1	regs[BPF_REG_ARG1]#define CTX	regs[BPF_REG_CTX]#define OFF	insn->off#define IMM	insn->imm struct bpf_mem_alloc bpf_global_ma;bool bpf_global_ma_set; /* No hurry in this branch * * Exported for the bpf jit load helper. */void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size){	u8 *ptr = NULL; 	if (k >= SKF_NET_OFF) {		ptr = skb_network_header(skb) + k - SKF_NET_OFF;	} else if (k >= SKF_LL_OFF) {		if (unlikely(!skb_mac_header_was_set(skb)))			return NULL;		ptr = skb_mac_header(skb) + k - SKF_LL_OFF;	}	if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))		return ptr; 	return NULL;} /* tell bpf programs that include vmlinux.h kernel's PAGE_SIZE */enum page_size_enum {	__PAGE_SIZE = PAGE_SIZE}; struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags){	gfp_t gfp_flags = bpf_memcg_flags(GFP_KERNEL | __GFP_ZERO | gfp_extra_flags);	struct bpf_prog_aux *aux;	struct bpf_prog *fp; 	size = round_up(size, __PAGE_SIZE);	fp = __vmalloc(size, gfp_flags);	if (fp == NULL)		return NULL; 	aux = kzalloc_obj(*aux, bpf_memcg_flags(GFP_KERNEL | gfp_extra_flags));	if (aux == NULL) {		vfree(fp);		return NULL;	}	fp->active = __alloc_percpu_gfp(sizeof(u8[BPF_NR_CONTEXTS]), 4,					bpf_memcg_flags(GFP_KERNEL | gfp_extra_flags));	if (!fp->active) {		vfree(fp);		kfree(aux);		return NULL;	} 	fp->pages = size / PAGE_SIZE;	fp->aux = aux;	fp->aux->main_prog_aux = aux;	fp->aux->prog = fp;	fp->jit_requested = ebpf_jit_enabled();	fp->blinding_requested = bpf_jit_blinding_enabled(fp);#ifdef CONFIG_CGROUP_BPF	aux->cgroup_atype = CGROUP_BPF_ATTACH_TYPE_INVALID;#endif 	INIT_LIST_HEAD_RCU(&fp->aux->ksym.lnode);#ifdef CONFIG_FINEIBT	INIT_LIST_HEAD_RCU(&fp->aux->ksym_prefix.lnode);#endif	mutex_init(&fp->aux->used_maps_mutex);	mutex_init(&fp->aux->ext_mutex);	mutex_init(&fp->aux->dst_mutex);	mutex_init(&fp->aux->st_ops_assoc_mutex); #ifdef CONFIG_BPF_SYSCALL	bpf_prog_stream_init(fp);#endif 	return fp;} struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags){	gfp_t gfp_flags = bpf_memcg_flags(GFP_KERNEL | __GFP_ZERO | gfp_extra_flags);	struct bpf_prog *prog;	int cpu; 	prog = bpf_prog_alloc_no_stats(size, gfp_extra_flags);	if (!prog)		return NULL; 	prog->stats = alloc_percpu_gfp(struct bpf_prog_stats, gfp_flags);	if (!prog->stats) {		free_percpu(prog->active);		kfree(prog->aux);		vfree(prog);		return NULL;	} 	for_each_possible_cpu(cpu) {		struct bpf_prog_stats *pstats; 		pstats = per_cpu_ptr(prog->stats, cpu);		u64_stats_init(&pstats->syncp);	}	return prog;}EXPORT_SYMBOL_GPL(bpf_prog_alloc); int bpf_prog_alloc_jited_linfo(struct bpf_prog *prog){	if (!prog->aux->nr_linfo || !prog->jit_requested)		return 0; 	prog->aux->jited_linfo = kvzalloc_objs(*prog->aux->jited_linfo,					       prog->aux->nr_linfo,					       bpf_memcg_flags(GFP_KERNEL | __GFP_NOWARN));	if (!prog->aux->jited_linfo)		return -ENOMEM; 	return 0;} void bpf_prog_jit_attempt_done(struct bpf_prog *prog){	if (prog->aux->jited_linfo &&	    (!prog->jited || !prog->aux->jited_linfo[0])) {		kvfree(prog->aux->jited_linfo);		prog->aux->jited_linfo = NULL;	} 	kfree(prog->aux->kfunc_tab);	prog->aux->kfunc_tab = NULL;} /* The jit engine is responsible to provide an array * for insn_off to the jited_off mapping (insn_to_jit_off). * * The idx to this array is the insn_off.  Hence, the insn_off * here is relative to the prog itself instead of the main prog. * This array has one entry for each xlated bpf insn. * * jited_off is the byte off to the end of the jited insn. * * Hence, with * insn_start: *      The first bpf insn off of the prog.  The insn off *      here is relative to the main prog. *      e.g. if prog is a subprog, insn_start > 0 * linfo_idx: *      The prog's idx to prog->aux->linfo and jited_linfo * * jited_linfo[linfo_idx] = prog->bpf_func * * For i > linfo_idx, * * jited_linfo[i] = prog->bpf_func + *	insn_to_jit_off[linfo[i].insn_off - insn_start - 1] */void bpf_prog_fill_jited_linfo(struct bpf_prog *prog,			       const u32 *insn_to_jit_off){	u32 linfo_idx, insn_start, insn_end, nr_linfo, i;	const struct bpf_line_info *linfo;	void **jited_linfo; 	if (!prog->aux->jited_linfo || prog->aux->func_idx > prog->aux->func_cnt)		/* Userspace did not provide linfo */		return; 	linfo_idx = prog->aux->linfo_idx;	linfo = &prog->aux->linfo[linfo_idx];	insn_start = linfo[0].insn_off;	insn_end = insn_start + prog->len; 	jited_linfo = &prog->aux->jited_linfo[linfo_idx];	jited_linfo[0] = prog->bpf_func; 	nr_linfo = prog->aux->nr_linfo - linfo_idx; 	for (i = 1; i < nr_linfo && linfo[i].insn_off < insn_end; i++)		/* The verifier ensures that linfo[i].insn_off is		 * strictly increasing		 */		jited_linfo[i] = prog->bpf_func +			insn_to_jit_off[linfo[i].insn_off - insn_start - 1];} struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,				  gfp_t gfp_extra_flags){	gfp_t gfp_flags = bpf_memcg_flags(GFP_KERNEL | __GFP_ZERO | gfp_extra_flags);	struct bpf_prog *fp;	u32 pages; 	size = round_up(size, PAGE_SIZE);	pages = size / PAGE_SIZE;	if (pages <= fp_old->pages)		return fp_old; 	fp = __vmalloc(size, gfp_flags);	if (fp) {		memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);		fp->pages = pages;		fp->aux->prog = fp; 		/* We keep fp->aux from fp_old around in the new		 * reallocated structure.		 */		fp_old->aux = NULL;		fp_old->stats = NULL;		fp_old->active = NULL;		__bpf_prog_free(fp_old);	} 	return fp;} void __bpf_prog_free(struct bpf_prog *fp){	if (fp->aux) {		mutex_destroy(&fp->aux->used_maps_mutex);		mutex_destroy(&fp->aux->dst_mutex);		mutex_destroy(&fp->aux->st_ops_assoc_mutex);		kfree(fp->aux->poke_tab);		kfree(fp->aux);	}	free_percpu(fp->stats);	free_percpu(fp->active);	vfree(fp);} int bpf_prog_calc_tag(struct bpf_prog *fp){	size_t size = bpf_prog_insn_size(fp);	struct bpf_insn *dst;	bool was_ld_map;	u32 i; 	dst = vmalloc(size);	if (!dst)		return -ENOMEM; 	/* We need to take out the map fd for the digest calculation	 * since they are unstable from user space side.	 */	for (i = 0, was_ld_map = false; i < fp->len; i++) {		dst[i] = fp->insnsi[i];		if (!was_ld_map &&		    dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) &&		    (dst[i].src_reg == BPF_PSEUDO_MAP_FD ||		     dst[i].src_reg == BPF_PSEUDO_MAP_VALUE)) {			was_ld_map = true;			dst[i].imm = 0;		} else if (was_ld_map &&			   dst[i].code == 0 &&			   dst[i].dst_reg == 0 &&			   dst[i].src_reg == 0 &&			   dst[i].off == 0) {			was_ld_map = false;			dst[i].imm = 0;		} else {			was_ld_map = false;		}	}	sha256((u8 *)dst, size, fp->digest);	vfree(dst);	return 0;} static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, s32 end_old,				s32 end_new, s32 curr, const bool probe_pass){	const s64 imm_min = S32_MIN, imm_max = S32_MAX;	s32 delta = end_new - end_old;	s64 imm = insn->imm; 	if (curr < pos && curr + imm + 1 >= end_old)		imm += delta;	else if (curr >= end_new && curr + imm + 1 < end_new)		imm -= delta;	if (imm < imm_min || imm > imm_max)		return -ERANGE;	if (!probe_pass)		insn->imm = imm;	return 0;} static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, s32 end_old,				s32 end_new, s32 curr, const bool probe_pass){	s64 off_min, off_max, off;	s32 delta = end_new - end_old; 	if (insn->code == (BPF_JMP32 | BPF_JA)) {		off = insn->imm;		off_min = S32_MIN;		off_max = S32_MAX;	} else {		off = insn->off;		off_min = S16_MIN;		off_max = S16_MAX;	} 	if (curr < pos && curr + off + 1 >= end_old)		off += delta;	else if (curr >= end_new && curr + off + 1 < end_new)		off -= delta;	if (off < off_min || off > off_max)		return -ERANGE;	if (!probe_pass) {		if (insn->code == (BPF_JMP32 | BPF_JA))			insn->imm = off;		else			insn->off = off;	}	return 0;} static int bpf_adj_branches(struct bpf_prog *prog, u32 pos, s32 end_old,			    s32 end_new, const bool probe_pass){	u32 i, insn_cnt = prog->len + (probe_pass ? end_new - end_old : 0);	struct bpf_insn *insn = prog->insnsi;	int ret = 0; 	for (i = 0; i < insn_cnt; i++, insn++) {		u8 code; 		/* In the probing pass we still operate on the original,		 * unpatched image in order to check overflows before we		 * do any other adjustments. Therefore skip the patchlet.		 */		if (probe_pass && i == pos) {			i = end_new;			insn = prog->insnsi + end_old;		}		if (bpf_pseudo_func(insn)) {			ret = bpf_adj_delta_to_imm(insn, pos, end_old,						   end_new, i, probe_pass);			if (ret)				return ret;			continue;		}		code = insn->code;		if ((BPF_CLASS(code) != BPF_JMP &&		     BPF_CLASS(code) != BPF_JMP32) ||		    BPF_OP(code) == BPF_EXIT)			continue;		/* Adjust offset of jmps if we cross patch boundaries. */		if (BPF_OP(code) == BPF_CALL) {			if (insn->src_reg != BPF_PSEUDO_CALL)				continue;			ret = bpf_adj_delta_to_imm(insn, pos, end_old,						   end_new, i, probe_pass);		} else {			ret = bpf_adj_delta_to_off(insn, pos, end_old,						   end_new, i, probe_pass);		}		if (ret)			break;	} 	return ret;} static void bpf_adj_linfo(struct bpf_prog *prog, u32 off, u32 delta){	struct bpf_line_info *linfo;	u32 i, nr_linfo; 	nr_linfo = prog->aux->nr_linfo;	if (!nr_linfo || !delta)		return; 	linfo = prog->aux->linfo; 	for (i = 0; i < nr_linfo; i++)		if (off < linfo[i].insn_off)			break; 	/* Push all off < linfo[i].insn_off by delta */	for (; i < nr_linfo; i++)		linfo[i].insn_off += delta;} struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,				       const struct bpf_insn *patch, u32 len){	u32 insn_adj_cnt, insn_rest, insn_delta = len - 1;	const u32 cnt_max = S16_MAX;	struct bpf_prog *prog_adj;	int err; 	/* Since our patchlet doesn't expand the image, we're done. */	if (insn_delta == 0) {		memcpy(prog->insnsi + off, patch, sizeof(*patch));		return prog;	} 	insn_adj_cnt = prog->len + insn_delta; 	/* Reject anything that would potentially let the insn->off	 * target overflow when we have excessive program expansions.	 * We need to probe here before we do any reallocation where	 * we afterwards may not fail anymore.	 */	if (insn_adj_cnt > cnt_max &&	    (err = bpf_adj_branches(prog, off, off + 1, off + len, true)))		return ERR_PTR(err); 	/* Several new instructions need to be inserted. Make room	 * for them. Likely, there's no need for a new allocation as	 * last page could have large enough tailroom.	 */	prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),				    GFP_USER);	if (!prog_adj)		return ERR_PTR(-ENOMEM); 	prog_adj->len = insn_adj_cnt; 	/* Patching happens in 3 steps:	 *	 * 1) Move over tail of insnsi from next instruction onwards,	 *    so we can patch the single target insn with one or more	 *    new ones (patching is always from 1 to n insns, n > 0).	 * 2) Inject new instructions at the target location.	 * 3) Adjust branch offsets if necessary.	 */	insn_rest = insn_adj_cnt - off - len; 

Lines 1–500 of 3394.