1092 lines · 28.7 KB · C
RawDownload
/* * 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> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * * Andi Kleen - Fix a few bad bugs and races. * Kris Katterjohn - Added many additional checks in bpf_check_classic() */ #include <linux/filter.h>#include <linux/skbuff.h>#include <linux/vmalloc.h>#include <linux/random.h>#include <linux/moduleloader.h>#include <linux/bpf.h>#include <linux/frame.h> #include <asm/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 ARG1	regs[BPF_REG_ARG1]#define CTX	regs[BPF_REG_CTX]#define IMM	insn->imm /* 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)		ptr = skb_mac_header(skb) + k - SKF_LL_OFF; 	if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))		return ptr; 	return NULL;} struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags){	gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __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, PAGE_KERNEL);	if (fp == NULL)		return NULL; 	kmemcheck_annotate_bitfield(fp, meta); 	aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);	if (aux == NULL) {		vfree(fp);		return NULL;	} 	fp->pages = size / PAGE_SIZE;	fp->aux = aux;	fp->aux->prog = fp; 	return fp;}EXPORT_SYMBOL_GPL(bpf_prog_alloc); struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,				  gfp_t gfp_extra_flags){	gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |			  gfp_extra_flags;	struct bpf_prog *fp; 	BUG_ON(fp_old == NULL); 	size = round_up(size, PAGE_SIZE);	if (size <= fp_old->pages * PAGE_SIZE)		return fp_old; 	fp = __vmalloc(size, gfp_flags, PAGE_KERNEL);	if (fp != NULL) {		kmemcheck_annotate_bitfield(fp, meta); 		memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);		fp->pages = size / PAGE_SIZE;		fp->aux->prog = fp; 		/* We keep fp->aux from fp_old around in the new		 * reallocated structure.		 */		fp_old->aux = NULL;		__bpf_prog_free(fp_old);	} 	return fp;} void __bpf_prog_free(struct bpf_prog *fp){	kfree(fp->aux);	vfree(fp);} static bool bpf_is_jmp_and_has_target(const struct bpf_insn *insn){	return BPF_CLASS(insn->code) == BPF_JMP  &&	       /* Call and Exit are both special jumps with no		* target inside the BPF instruction image.		*/	       BPF_OP(insn->code) != BPF_CALL &&	       BPF_OP(insn->code) != BPF_EXIT;} static void bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta){	struct bpf_insn *insn = prog->insnsi;	u32 i, insn_cnt = prog->len; 	for (i = 0; i < insn_cnt; i++, insn++) {		if (!bpf_is_jmp_and_has_target(insn))			continue; 		/* Adjust offset of jmps if we cross boundaries. */		if (i < pos && i + insn->off + 1 > pos)			insn->off += delta;		else if (i > pos + delta && i + insn->off + 1 <= pos + delta)			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;	struct bpf_prog *prog_adj; 	/* 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; 	/* 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 NULL; 	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; 	memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1,		sizeof(*patch) * insn_rest);	memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len); 	bpf_adj_branches(prog_adj, off, insn_delta); 	return prog_adj;} #ifdef CONFIG_BPF_JITstruct bpf_binary_header *bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,		     unsigned int alignment,		     bpf_jit_fill_hole_t bpf_fill_ill_insns){	struct bpf_binary_header *hdr;	unsigned int size, hole, start; 	/* Most of BPF filters are really small, but if some of them	 * fill a page, allow at least 128 extra bytes to insert a	 * random section of illegal instructions.	 */	size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);	hdr = module_alloc(size);	if (hdr == NULL)		return NULL; 	/* Fill space with illegal/arch-dep instructions. */	bpf_fill_ill_insns(hdr, size); 	hdr->pages = size / PAGE_SIZE;	hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)),		     PAGE_SIZE - sizeof(*hdr));	start = (get_random_int() % hole) & ~(alignment - 1); 	/* Leave a random number of instructions before BPF code. */	*image_ptr = &hdr->image[start]; 	return hdr;} void bpf_jit_binary_free(struct bpf_binary_header *hdr){	module_memfree(hdr);} int bpf_jit_harden __read_mostly; static int bpf_jit_blind_insn(const struct bpf_insn *from,			      const struct bpf_insn *aux,			      struct bpf_insn *to_buff){	struct bpf_insn *to = to_buff;	u32 imm_rnd = get_random_int();	s16 off; 	BUILD_BUG_ON(BPF_REG_AX  + 1 != MAX_BPF_JIT_REG);	BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG); 	if (from->imm == 0 &&	    (from->code == (BPF_ALU   | BPF_MOV | BPF_K) ||	     from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {		*to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg);		goto out;	} 	switch (from->code) {	case BPF_ALU | BPF_ADD | BPF_K:	case BPF_ALU | BPF_SUB | BPF_K:	case BPF_ALU | BPF_AND | BPF_K:	case BPF_ALU | BPF_OR  | BPF_K:	case BPF_ALU | BPF_XOR | BPF_K:	case BPF_ALU | BPF_MUL | BPF_K:	case BPF_ALU | BPF_MOV | BPF_K:	case BPF_ALU | BPF_DIV | BPF_K:	case BPF_ALU | BPF_MOD | BPF_K:		*to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);		*to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);		*to++ = BPF_ALU32_REG(from->code, from->dst_reg, BPF_REG_AX);		break; 	case BPF_ALU64 | BPF_ADD | BPF_K:	case BPF_ALU64 | BPF_SUB | BPF_K:	case BPF_ALU64 | BPF_AND | BPF_K:	case BPF_ALU64 | BPF_OR  | BPF_K:	case BPF_ALU64 | BPF_XOR | BPF_K:	case BPF_ALU64 | BPF_MUL | BPF_K:	case BPF_ALU64 | BPF_MOV | BPF_K:	case BPF_ALU64 | BPF_DIV | BPF_K:	case BPF_ALU64 | BPF_MOD | BPF_K:		*to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);		*to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);		*to++ = BPF_ALU64_REG(from->code, from->dst_reg, BPF_REG_AX);		break; 	case BPF_JMP | BPF_JEQ  | BPF_K:	case BPF_JMP | BPF_JNE  | BPF_K:	case BPF_JMP | BPF_JGT  | BPF_K:	case BPF_JMP | BPF_JGE  | BPF_K:	case BPF_JMP | BPF_JSGT | BPF_K:	case BPF_JMP | BPF_JSGE | BPF_K:	case BPF_JMP | BPF_JSET | BPF_K:		/* Accommodate for extra offset in case of a backjump. */		off = from->off;		if (off < 0)			off -= 2;		*to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);		*to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);		*to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off);		break; 	case BPF_LD | BPF_ABS | BPF_W:	case BPF_LD | BPF_ABS | BPF_H:	case BPF_LD | BPF_ABS | BPF_B:		*to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);		*to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);		*to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);		break; 	case BPF_LD | BPF_IND | BPF_W:	case BPF_LD | BPF_IND | BPF_H:	case BPF_LD | BPF_IND | BPF_B:		*to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);		*to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);		*to++ = BPF_ALU32_REG(BPF_ADD, BPF_REG_AX, from->src_reg);		*to++ = BPF_LD_IND(from->code, BPF_REG_AX, 0);		break; 	case BPF_LD | BPF_IMM | BPF_DW:		*to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm);		*to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);		*to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);		*to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX);		break;	case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */		*to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm);		*to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);		*to++ = BPF_ALU64_REG(BPF_OR,  aux[0].dst_reg, BPF_REG_AX);		break; 	case BPF_ST | BPF_MEM | BPF_DW:	case BPF_ST | BPF_MEM | BPF_W:	case BPF_ST | BPF_MEM | BPF_H:	case BPF_ST | BPF_MEM | BPF_B:		*to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm);		*to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd);		*to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off);		break;	}out:	return to - to_buff;} static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,					      gfp_t gfp_extra_flags){	gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |			  gfp_extra_flags;	struct bpf_prog *fp; 	fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags, PAGE_KERNEL);	if (fp != NULL) {		kmemcheck_annotate_bitfield(fp, meta); 		/* aux->prog still points to the fp_other one, so		 * when promoting the clone to the real program,		 * this still needs to be adapted.		 */		memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE);	} 	return fp;} static void bpf_prog_clone_free(struct bpf_prog *fp){	/* aux was stolen by the other clone, so we cannot free	 * it from this path! It will be freed eventually by the	 * other program on release.	 *	 * At this point, we don't need a deferred release since	 * clone is guaranteed to not be locked.	 */	fp->aux = NULL;	__bpf_prog_free(fp);} void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other){	/* We have to repoint aux->prog to self, as we don't	 * know whether fp here is the clone or the original.	 */	fp->aux->prog = fp;	bpf_prog_clone_free(fp_other);} struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog){	struct bpf_insn insn_buff[16], aux[2];	struct bpf_prog *clone, *tmp;	int insn_delta, insn_cnt;	struct bpf_insn *insn;	int i, rewritten; 	if (!bpf_jit_blinding_enabled())		return prog; 	clone = bpf_prog_clone_create(prog, GFP_USER);	if (!clone)		return ERR_PTR(-ENOMEM); 	insn_cnt = clone->len;	insn = clone->insnsi; 	for (i = 0; i < insn_cnt; i++, insn++) {		/* We temporarily need to hold the original ld64 insn		 * so that we can still access the first part in the		 * second blinding run.		 */		if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) &&		    insn[1].code == 0)			memcpy(aux, insn, sizeof(aux)); 		rewritten = bpf_jit_blind_insn(insn, aux, insn_buff);		if (!rewritten)			continue; 		tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);		if (!tmp) {			/* Patching may have repointed aux->prog during			 * realloc from the original one, so we need to			 * fix it up here on error.			 */			bpf_jit_prog_release_other(prog, clone);			return ERR_PTR(-ENOMEM);		} 		clone = tmp;		insn_delta = rewritten - 1; 		/* Walk new program and skip insns we just inserted. */		insn = clone->insnsi + i + insn_delta;		insn_cnt += insn_delta;		i        += insn_delta;	} 	return clone;}#endif /* CONFIG_BPF_JIT */ /* Base function for offset calculation. Needs to go into .text section, * therefore keeping it non-static as well; will also be used by JITs * anyway later on, so do not let the compiler omit it. */noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5){	return 0;}EXPORT_SYMBOL_GPL(__bpf_call_base); /** *	__bpf_prog_run - run eBPF program on a given context *	@ctx: is the data we are operating on *	@insn: is the array of eBPF instructions * * Decode and execute eBPF instructions. */static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn){	u64 stack[MAX_BPF_STACK / sizeof(u64)];	u64 regs[MAX_BPF_REG], tmp;	static const void *jumptable[256] = {		[0 ... 255] = &&default_label,		/* Now overwrite non-defaults ... */		/* 32 bit ALU operations */		[BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,		[BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,		[BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,		[BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,		[BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,		[BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,		[BPF_ALU | BPF_OR | BPF_X]  = &&ALU_OR_X,		[BPF_ALU | BPF_OR | BPF_K]  = &&ALU_OR_K,		[BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,		[BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,		[BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,		[BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,		[BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,		[BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,		[BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,		[BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,		[BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,		[BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,		[BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,		[BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,		[BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,		[BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,		[BPF_ALU | BPF_NEG] = &&ALU_NEG,		[BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,		[BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,

Lines 1–500 of 1092.