diff options
Diffstat (limited to 'kernel/bpf/core.c')
-rw-r--r-- | kernel/bpf/core.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 2c2bfaacce66..8ee520f0ec70 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c | |||
@@ -20,9 +20,12 @@ | |||
20 | * Andi Kleen - Fix a few bad bugs and races. | 20 | * Andi Kleen - Fix a few bad bugs and races. |
21 | * Kris Katterjohn - Added many additional checks in bpf_check_classic() | 21 | * Kris Katterjohn - Added many additional checks in bpf_check_classic() |
22 | */ | 22 | */ |
23 | |||
23 | #include <linux/filter.h> | 24 | #include <linux/filter.h> |
24 | #include <linux/skbuff.h> | 25 | #include <linux/skbuff.h> |
25 | #include <linux/vmalloc.h> | 26 | #include <linux/vmalloc.h> |
27 | #include <linux/random.h> | ||
28 | #include <linux/moduleloader.h> | ||
26 | #include <asm/unaligned.h> | 29 | #include <asm/unaligned.h> |
27 | 30 | ||
28 | /* Registers */ | 31 | /* Registers */ |
@@ -125,6 +128,42 @@ void __bpf_prog_free(struct bpf_prog *fp) | |||
125 | } | 128 | } |
126 | EXPORT_SYMBOL_GPL(__bpf_prog_free); | 129 | EXPORT_SYMBOL_GPL(__bpf_prog_free); |
127 | 130 | ||
131 | struct bpf_binary_header * | ||
132 | bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, | ||
133 | unsigned int alignment, | ||
134 | bpf_jit_fill_hole_t bpf_fill_ill_insns) | ||
135 | { | ||
136 | struct bpf_binary_header *hdr; | ||
137 | unsigned int size, hole, start; | ||
138 | |||
139 | /* Most of BPF filters are really small, but if some of them | ||
140 | * fill a page, allow at least 128 extra bytes to insert a | ||
141 | * random section of illegal instructions. | ||
142 | */ | ||
143 | size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE); | ||
144 | hdr = module_alloc(size); | ||
145 | if (hdr == NULL) | ||
146 | return NULL; | ||
147 | |||
148 | /* Fill space with illegal/arch-dep instructions. */ | ||
149 | bpf_fill_ill_insns(hdr, size); | ||
150 | |||
151 | hdr->pages = size / PAGE_SIZE; | ||
152 | hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)), | ||
153 | PAGE_SIZE - sizeof(*hdr)); | ||
154 | start = (prandom_u32() % hole) & ~(alignment - 1); | ||
155 | |||
156 | /* Leave a random number of instructions before BPF code. */ | ||
157 | *image_ptr = &hdr->image[start]; | ||
158 | |||
159 | return hdr; | ||
160 | } | ||
161 | |||
162 | void bpf_jit_binary_free(struct bpf_binary_header *hdr) | ||
163 | { | ||
164 | module_free(NULL, hdr); | ||
165 | } | ||
166 | |||
128 | /* Base function for offset calculation. Needs to go into .text section, | 167 | /* Base function for offset calculation. Needs to go into .text section, |
129 | * therefore keeping it non-static as well; will also be used by JITs | 168 | * therefore keeping it non-static as well; will also be used by JITs |
130 | * anyway later on, so do not let the compiler omit it. | 169 | * anyway later on, so do not let the compiler omit it. |