at commit 0fa99358
| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Linux Socket Filter - Kernel level socket filtering |
| 4 | * |
| 5 | * Based on the design of the Berkeley Packet Filter. The new |
| 6 | * internal format has been designed by PLUMgrid: |
| 7 | * |
| 8 | * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com |
| 9 | * |
| 10 | * Authors: |
| 11 | * |
| 12 | * Jay Schulist <jschlst@samba.org> |
| 13 | * Alexei Starovoitov <ast@plumgrid.com> |
| 14 | * Daniel Borkmann <dborkman@redhat.com> |
| 15 | * |
| 16 | * Andi Kleen - Fix a few bad bugs and races. |
| 17 | * Kris Katterjohn - Added many additional checks in bpf_check_classic() |
| 18 | */ |
| 19 | |
| 20 | #include <uapi/linux/btf.h> |
| 21 | #include <linux/filter.h> |
| 22 | #include <linux/sched/signal.h> |
| 23 | #include <linux/skbuff.h> |
| 24 | #include <linux/static_call.h> |
| 25 | #include <linux/vmalloc.h> |
| 26 | #include <linux/prandom.h> |
| 27 | #include <linux/bpf.h> |
| 28 | #include <linux/btf.h> |
| 29 | #include <linux/hex.h> |
| 30 | #include <linux/objtool.h> |
| 31 | #include <linux/overflow.h> |
| 32 | #include <linux/rbtree_latch.h> |
| 33 | #include <linux/kallsyms.h> |
| 34 | #include <linux/rcupdate.h> |
| 35 | #include <linux/perf_event.h> |
| 36 | #include <linux/extable.h> |
| 37 | #include <linux/log2.h> |
| 38 | #include <linux/bpf_verifier.h> |
| 39 | #include <linux/nodemask.h> |
| 40 | #include <linux/nospec.h> |
| 41 | #include <linux/bpf_mem_alloc.h> |
| 42 | #include <linux/memcontrol.h> |
| 43 | #include <linux/execmem.h> |
| 44 | #include <crypto/sha2.h> |
| 45 | |
| 46 | #include <asm/barrier.h> |
| 47 | #include <linux/unaligned.h> |
| 48 | |
| 49 | /* Registers */ |
| 50 | #define BPF_R0 regs[BPF_REG_0] |
| 51 | #define BPF_R1 regs[BPF_REG_1] |
| 52 | #define BPF_R2 regs[BPF_REG_2] |
| 53 | #define BPF_R3 regs[BPF_REG_3] |
| 54 | #define BPF_R4 regs[BPF_REG_4] |
| 55 | #define BPF_R5 regs[BPF_REG_5] |
| 56 | #define BPF_R6 regs[BPF_REG_6] |
| 57 | #define BPF_R7 regs[BPF_REG_7] |
| 58 | #define BPF_R8 regs[BPF_REG_8] |
| 59 | #define BPF_R9 regs[BPF_REG_9] |
| 60 | #define BPF_R10 regs[BPF_REG_10] |
| 61 | |
| 62 | /* Named registers */ |
| 63 | #define DST regs[insn->dst_reg] |
| 64 | #define SRC regs[insn->src_reg] |
| 65 | #define FP regs[BPF_REG_FP] |
| 66 | #define AX regs[BPF_REG_AX] |
| 67 | #define ARG1 regs[BPF_REG_ARG1] |
| 68 | #define CTX regs[BPF_REG_CTX] |
| 69 | #define OFF insn->off |
| 70 | #define IMM insn->imm |
| 71 | |
| 72 | struct bpf_mem_alloc bpf_global_ma; |
| 73 | bool bpf_global_ma_set; |
| 74 | |
| 75 | /* No hurry in this branch |
| 76 | * |
| 77 | * Exported for the bpf jit load helper. |
| 78 | */ |
| 79 | void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size) |
| 80 | { |
| 81 | u8 *ptr = NULL; |
| 82 | |
| 83 | if (k >= SKF_NET_OFF) { |
| 84 | ptr = skb_network_header(skb) + k - SKF_NET_OFF; |
| 85 | } else if (k >= SKF_LL_OFF) { |
| 86 | if (unlikely(!skb_mac_header_was_set(skb))) |
| 87 | return NULL; |
| 88 | ptr = skb_mac_header(skb) + k - SKF_LL_OFF; |
| 89 | } |
| 90 | if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb)) |
| 91 | return ptr; |
| 92 | |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | /* tell bpf programs that include vmlinux.h kernel's PAGE_SIZE */ |
| 97 | enum page_size_enum { |
| 98 | __PAGE_SIZE = PAGE_SIZE |
| 99 | }; |
| 100 | |
| 101 | struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags) |
| 102 | { |
| 103 | gfp_t gfp_flags = bpf_memcg_flags(GFP_KERNEL | __GFP_ZERO | gfp_extra_flags); |
| 104 | struct bpf_prog_aux *aux; |
| 105 | struct bpf_prog *fp; |
| 106 | |
| 107 | size = round_up(size, __PAGE_SIZE); |
| 108 | fp = __vmalloc(size, gfp_flags); |
| 109 | if (fp == NULL) |
| 110 | return NULL; |
| 111 | |
| 112 | aux = kzalloc_obj(*aux, bpf_memcg_flags(GFP_KERNEL | gfp_extra_flags)); |
| 113 | if (aux == NULL) { |
| 114 | vfree(fp); |
| 115 | return NULL; |
| 116 | } |
| 117 | fp->active = __alloc_percpu_gfp(sizeof(u8[BPF_NR_CONTEXTS]), 4, |
| 118 | bpf_memcg_flags(GFP_KERNEL | gfp_extra_flags)); |
| 119 | if (!fp->active) { |
| 120 | vfree(fp); |
| 121 | kfree(aux); |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | fp->pages = size / PAGE_SIZE; |
| 126 | fp->aux = aux; |
| 127 | fp->aux->main_prog_aux = aux; |
| 128 | fp->aux->prog = fp; |
| 129 | fp->jit_requested = ebpf_jit_enabled(); |
| 130 | fp->jit_required = IS_ENABLED(CONFIG_BPF_JIT_ALWAYS_ON); |
| 131 | fp->blinding_requested = bpf_jit_blinding_enabled(fp); |
| 132 | #ifdef CONFIG_CGROUP_BPF |
| 133 | aux->cgroup_atype = CGROUP_BPF_ATTACH_TYPE_INVALID; |
| 134 | #endif |
| 135 | |
| 136 | INIT_LIST_HEAD_RCU(&fp->aux->ksym.lnode); |
| 137 | #ifdef CONFIG_FINEIBT |
| 138 | INIT_LIST_HEAD_RCU(&fp->aux->ksym_prefix.lnode); |
| 139 | #endif |
| 140 | mutex_init(&fp->aux->used_maps_mutex); |
| 141 | mutex_init(&fp->aux->ext_mutex); |
| 142 | mutex_init(&fp->aux->dst_mutex); |
| 143 | mutex_init(&fp->aux->st_ops_assoc_mutex); |
| 144 | |
| 145 | #ifdef CONFIG_BPF_SYSCALL |
| 146 | bpf_prog_stream_init(fp); |
| 147 | #endif |
| 148 | |
| 149 | return fp; |
| 150 | } |
| 151 | |
| 152 | struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags) |
| 153 | { |
| 154 | gfp_t gfp_flags = bpf_memcg_flags(GFP_KERNEL | __GFP_ZERO | gfp_extra_flags); |
| 155 | struct bpf_prog *prog; |
| 156 | int cpu; |
| 157 | |
| 158 | prog = bpf_prog_alloc_no_stats(size, gfp_extra_flags); |
| 159 | if (!prog) |
| 160 | return NULL; |
| 161 | |
| 162 | prog->stats = alloc_percpu_gfp(struct bpf_prog_stats, gfp_flags); |
| 163 | if (!prog->stats) { |
| 164 | free_percpu(prog->active); |
| 165 | kfree(prog->aux); |
| 166 | vfree(prog); |
| 167 | return NULL; |
| 168 | } |
| 169 | |
| 170 | for_each_possible_cpu(cpu) { |
| 171 | struct bpf_prog_stats *pstats; |
| 172 | |
| 173 | pstats = per_cpu_ptr(prog->stats, cpu); |
| 174 | u64_stats_init(&pstats->syncp); |
| 175 | } |
| 176 | return prog; |
| 177 | } |
| 178 | EXPORT_SYMBOL_GPL(bpf_prog_alloc); |
| 179 | |
| 180 | int bpf_prog_alloc_jited_linfo(struct bpf_prog *prog) |
| 181 | { |
| 182 | if (!prog->aux->nr_linfo || !prog->jit_requested) |
| 183 | return 0; |
| 184 | |
| 185 | prog->aux->jited_linfo = kvzalloc_objs(*prog->aux->jited_linfo, |
| 186 | prog->aux->nr_linfo, |
| 187 | bpf_memcg_flags(GFP_KERNEL | __GFP_NOWARN)); |
| 188 | if (!prog->aux->jited_linfo) |
| 189 | return -ENOMEM; |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | void bpf_prog_jit_attempt_done(struct bpf_prog *prog) |
| 195 | { |
| 196 | if (prog->aux->jited_linfo && |
| 197 | (!prog->jited || !prog->aux->jited_linfo[0])) { |
| 198 | kvfree(prog->aux->jited_linfo); |
| 199 | prog->aux->jited_linfo = NULL; |
| 200 | } |
| 201 | |
| 202 | kfree(prog->aux->kfunc_tab); |
| 203 | prog->aux->kfunc_tab = NULL; |
| 204 | } |
| 205 | |
| 206 | /* The jit engine is responsible to provide an array |
| 207 | * for insn_off to the jited_off mapping (insn_to_jit_off). |
| 208 | * |
| 209 | * The idx to this array is the insn_off. Hence, the insn_off |
| 210 | * here is relative to the prog itself instead of the main prog. |
| 211 | * This array has one entry for each xlated bpf insn. |
| 212 | * |
| 213 | * jited_off is the byte off to the end of the jited insn. |
| 214 | * |
| 215 | * Hence, with |
| 216 | * insn_start: |
| 217 | * The first bpf insn off of the prog. The insn off |
| 218 | * here is relative to the main prog. |
| 219 | * e.g. if prog is a subprog, insn_start > 0 |
| 220 | * linfo_idx: |
| 221 | * The prog's idx to prog->aux->linfo and jited_linfo |
| 222 | * |
| 223 | * jited_linfo[linfo_idx] = prog->bpf_func |
| 224 | * |
| 225 | * For i > linfo_idx, |
| 226 | * |
| 227 | * jited_linfo[i] = prog->bpf_func + |
| 228 | * insn_to_jit_off[linfo[i].insn_off - insn_start - 1] |
| 229 | */ |
| 230 | void bpf_prog_fill_jited_linfo(struct bpf_prog *prog, |
| 231 | const u32 *insn_to_jit_off) |
| 232 | { |
| 233 | u32 linfo_idx, insn_start, insn_end, nr_linfo, i; |
| 234 | const struct bpf_line_info *linfo; |
| 235 | void **jited_linfo; |
| 236 | |
| 237 | if (!prog->aux->jited_linfo || prog->aux->func_idx > prog->aux->func_cnt) |
| 238 | /* Userspace did not provide linfo */ |
| 239 | return; |
| 240 | |
| 241 | linfo_idx = prog->aux->linfo_idx; |
| 242 | linfo = &prog->aux->linfo[linfo_idx]; |
| 243 | insn_start = linfo[0].insn_off; |
| 244 | insn_end = insn_start + prog->len; |
| 245 | |
| 246 | jited_linfo = &prog->aux->jited_linfo[linfo_idx]; |
| 247 | jited_linfo[0] = prog->bpf_func; |
| 248 | |
| 249 | nr_linfo = prog->aux->nr_linfo - linfo_idx; |
| 250 | |
| 251 | for (i = 1; i < nr_linfo && linfo[i].insn_off < insn_end; i++) |
| 252 | /* The verifier ensures that linfo[i].insn_off is |
| 253 | * strictly increasing |
| 254 | */ |
| 255 | jited_linfo[i] = prog->bpf_func + |
| 256 | insn_to_jit_off[linfo[i].insn_off - insn_start - 1]; |
| 257 | } |
| 258 | |
| 259 | struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size, |
| 260 | gfp_t gfp_extra_flags) |
| 261 | { |
| 262 | gfp_t gfp_flags = bpf_memcg_flags(GFP_KERNEL | __GFP_ZERO | gfp_extra_flags); |
| 263 | struct bpf_prog *fp; |
| 264 | u32 pages; |
| 265 | |
| 266 | size = round_up(size, PAGE_SIZE); |
| 267 | pages = size / PAGE_SIZE; |
| 268 | if (pages <= fp_old->pages) |
| 269 | return fp_old; |
| 270 | |
| 271 | fp = __vmalloc(size, gfp_flags); |
| 272 | if (fp) { |
| 273 | memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE); |
| 274 | fp->pages = pages; |
| 275 | fp->aux->prog = fp; |
| 276 | |
| 277 | /* We keep fp->aux from fp_old around in the new |
| 278 | * reallocated structure. |
| 279 | */ |
| 280 | fp_old->aux = NULL; |
| 281 | fp_old->stats = NULL; |
| 282 | fp_old->active = NULL; |
| 283 | __bpf_prog_free(fp_old); |
| 284 | } |
| 285 | |
| 286 | return fp; |
| 287 | } |
| 288 | |
| 289 | void __bpf_prog_free(struct bpf_prog *fp) |
| 290 | { |
| 291 | if (fp->aux) { |
| 292 | mutex_destroy(&fp->aux->used_maps_mutex); |
| 293 | mutex_destroy(&fp->aux->dst_mutex); |
| 294 | mutex_destroy(&fp->aux->st_ops_assoc_mutex); |
| 295 | kfree(fp->aux->poke_tab); |
| 296 | kfree(fp->aux); |
| 297 | } |
| 298 | free_percpu(fp->stats); |
| 299 | free_percpu(fp->active); |
| 300 | vfree(fp); |
| 301 | } |
| 302 | |
| 303 | int bpf_prog_calc_tag(struct bpf_prog *fp) |
| 304 | { |
| 305 | size_t size = bpf_prog_insn_size(fp); |
| 306 | struct bpf_insn *dst; |
| 307 | bool was_ld_map; |
| 308 | u32 i; |
| 309 | |
| 310 | dst = __vmalloc(size, GFP_KERNEL_ACCOUNT); |
| 311 | if (!dst) |
| 312 | return -ENOMEM; |
| 313 | |
| 314 | /* We need to take out the map fd for the digest calculation |
| 315 | * since they are unstable from user space side. |
| 316 | */ |
| 317 | for (i = 0, was_ld_map = false; i < fp->len; i++) { |
| 318 | dst[i] = fp->insnsi[i]; |
| 319 | if (!was_ld_map && |
| 320 | dst[i].code == (BPF_LD | BPF_IMM | BPF_DW) && |
| 321 | (dst[i].src_reg == BPF_PSEUDO_MAP_FD || |
| 322 | dst[i].src_reg == BPF_PSEUDO_MAP_VALUE)) { |
| 323 | was_ld_map = true; |
| 324 | dst[i].imm = 0; |
| 325 | } else if (was_ld_map && |
| 326 | dst[i].code == 0 && |
| 327 | dst[i].dst_reg == 0 && |
| 328 | dst[i].src_reg == 0 && |
| 329 | dst[i].off == 0) { |
| 330 | was_ld_map = false; |
| 331 | dst[i].imm = 0; |
| 332 | } else { |
| 333 | was_ld_map = false; |
| 334 | } |
| 335 | } |
| 336 | sha256((u8 *)dst, size, fp->digest); |
| 337 | vfree(dst); |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, s32 end_old, |
| 342 | s32 end_new, s32 curr, const bool probe_pass) |
| 343 | { |
| 344 | const s64 imm_min = S32_MIN, imm_max = S32_MAX; |
| 345 | s32 delta = end_new - end_old; |
| 346 | s64 imm = insn->imm; |
| 347 | |
| 348 | if (curr < pos && curr + imm + 1 >= end_old) |
| 349 | imm += delta; |
| 350 | else if (curr >= end_new && curr + imm + 1 < end_new) |
| 351 | imm -= delta; |
| 352 | if (imm < imm_min || imm > imm_max) |
| 353 | return -ERANGE; |
| 354 | if (!probe_pass) |
| 355 | insn->imm = imm; |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static int bpf_adj_delta_to_off(struct bpf_insn *insn, u32 pos, s32 end_old, |
| 360 | s32 end_new, s32 curr, const bool probe_pass) |
| 361 | { |
| 362 | s64 off_min, off_max, off; |
| 363 | s32 delta = end_new - end_old; |
| 364 | |
| 365 | if (insn->code == (BPF_JMP32 | BPF_JA)) { |
| 366 | off = insn->imm; |
| 367 | off_min = S32_MIN; |
| 368 | off_max = S32_MAX; |
| 369 | } else { |
| 370 | off = insn->off; |
| 371 | off_min = S16_MIN; |
| 372 | off_max = S16_MAX; |
| 373 | } |
| 374 | |
| 375 | if (curr < pos && curr + off + 1 >= end_old) |
| 376 | off += delta; |
| 377 | else if (curr >= end_new && curr + off + 1 < end_new) |
| 378 | off -= delta; |
| 379 | if (off < off_min || off > off_max) |
| 380 | return -ERANGE; |
| 381 | if (!probe_pass) { |
| 382 | if (insn->code == (BPF_JMP32 | BPF_JA)) |
| 383 | insn->imm = off; |
| 384 | else |
| 385 | insn->off = off; |
| 386 | } |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static int bpf_adj_branches(struct bpf_prog *prog, u32 pos, s32 end_old, |
| 391 | s32 end_new, const bool probe_pass) |
| 392 | { |
| 393 | u32 i, insn_cnt = prog->len + (probe_pass ? end_new - end_old : 0); |
| 394 | struct bpf_insn *insn = prog->insnsi; |
| 395 | int ret = 0; |
| 396 | |
| 397 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 398 | u8 code; |
| 399 | |
| 400 | /* In the probing pass we still operate on the original, |
| 401 | * unpatched image in order to check overflows before we |
| 402 | * do any other adjustments. Therefore skip the patchlet. |
| 403 | */ |
| 404 | if (probe_pass && i == pos) { |
| 405 | i = end_new; |
| 406 | insn = prog->insnsi + end_old; |
| 407 | } |
| 408 | if (bpf_pseudo_func(insn)) { |
| 409 | ret = bpf_adj_delta_to_imm(insn, pos, end_old, |
| 410 | end_new, i, probe_pass); |
| 411 | if (ret) |
| 412 | return ret; |
| 413 | continue; |
| 414 | } |
| 415 | code = insn->code; |
| 416 | if ((BPF_CLASS(code) != BPF_JMP && |
| 417 | BPF_CLASS(code) != BPF_JMP32) || |
| 418 | BPF_OP(code) == BPF_EXIT) |
| 419 | continue; |
| 420 | /* Adjust offset of jmps if we cross patch boundaries. */ |
| 421 | if (BPF_OP(code) == BPF_CALL) { |
| 422 | if (insn->src_reg != BPF_PSEUDO_CALL) |
| 423 | continue; |
| 424 | ret = bpf_adj_delta_to_imm(insn, pos, end_old, |
| 425 | end_new, i, probe_pass); |
| 426 | } else { |
| 427 | ret = bpf_adj_delta_to_off(insn, pos, end_old, |
| 428 | end_new, i, probe_pass); |
| 429 | } |
| 430 | if (ret) |
| 431 | break; |
| 432 | } |
| 433 | |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | static void bpf_adj_linfo(struct bpf_prog *prog, u32 off, u32 delta) |
| 438 | { |
| 439 | struct bpf_line_info *linfo; |
| 440 | u32 i, nr_linfo; |
| 441 | |
| 442 | nr_linfo = prog->aux->nr_linfo; |
| 443 | if (!nr_linfo || !delta) |
| 444 | return; |
| 445 | |
| 446 | linfo = prog->aux->linfo; |
| 447 | |
| 448 | for (i = 0; i < nr_linfo; i++) |
| 449 | if (off < linfo[i].insn_off) |
| 450 | break; |
| 451 | |
| 452 | /* Push all off < linfo[i].insn_off by delta */ |
| 453 | for (; i < nr_linfo; i++) |
| 454 | linfo[i].insn_off += delta; |
| 455 | } |
| 456 | |
| 457 | struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off, |
| 458 | const struct bpf_insn *patch, u32 len) |
| 459 | { |
| 460 | u32 insn_adj_cnt, insn_rest, insn_delta = len - 1; |
| 461 | const u32 cnt_max = S16_MAX; |
| 462 | struct bpf_prog *prog_adj; |
| 463 | int err; |
| 464 | |
| 465 | /* Since our patchlet doesn't expand the image, we're done. */ |
| 466 | if (insn_delta == 0) { |
| 467 | memcpy(prog->insnsi + off, patch, sizeof(*patch)); |
| 468 | return prog; |
| 469 | } |
| 470 | |
| 471 | insn_adj_cnt = prog->len + insn_delta; |
| 472 | |
| 473 | /* Reject anything that would potentially let the insn->off |
| 474 | * target overflow when we have excessive program expansions. |
| 475 | * We need to probe here before we do any reallocation where |
| 476 | * we afterwards may not fail anymore. |
| 477 | */ |
| 478 | if (insn_adj_cnt > cnt_max && |
| 479 | (err = bpf_adj_branches(prog, off, off + 1, off + len, true))) |
| 480 | return ERR_PTR(err); |
| 481 | |
| 482 | /* Several new instructions need to be inserted. Make room |
| 483 | * for them. Likely, there's no need for a new allocation as |
| 484 | * last page could have large enough tailroom. |
| 485 | */ |
| 486 | prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt), |
| 487 | GFP_USER); |
| 488 | if (!prog_adj) |
| 489 | return ERR_PTR(-ENOMEM); |
| 490 | |
| 491 | prog_adj->len = insn_adj_cnt; |
| 492 | |
| 493 | /* Patching happens in 3 steps: |
| 494 | * |
| 495 | * 1) Move over tail of insnsi from next instruction onwards, |
| 496 | * so we can patch the single target insn with one or more |
| 497 | * new ones (patching is always from 1 to n insns, n > 0). |
| 498 | * 2) Inject new instructions at the target location. |
| 499 | * 3) Adjust branch offsets if necessary. |
| 500 | */ |
| 501 | insn_rest = insn_adj_cnt - off - len; |
| 502 | |
| 503 | memmove(prog_adj->insnsi + off + len, prog_adj->insnsi + off + 1, |
| 504 | sizeof(*patch) * insn_rest); |
| 505 | memcpy(prog_adj->insnsi + off, patch, sizeof(*patch) * len); |
| 506 | |
| 507 | /* We are guaranteed to not fail at this point, otherwise |
| 508 | * the ship has sailed to reverse to the original state. An |
| 509 | * overflow cannot happen at this point. |
| 510 | */ |
| 511 | BUG_ON(bpf_adj_branches(prog_adj, off, off + 1, off + len, false)); |
| 512 | |
| 513 | bpf_adj_linfo(prog_adj, off, insn_delta); |
| 514 | |
| 515 | return prog_adj; |
| 516 | } |
| 517 | |
| 518 | int bpf_remove_insns(struct bpf_prog *prog, u32 off, u32 cnt) |
| 519 | { |
| 520 | int err; |
| 521 | |
| 522 | /* Branch offsets can't overflow when program is shrinking, no need |
| 523 | * to call bpf_adj_branches(..., true) here |
| 524 | */ |
| 525 | memmove(prog->insnsi + off, prog->insnsi + off + cnt, |
| 526 | sizeof(struct bpf_insn) * (prog->len - off - cnt)); |
| 527 | prog->len -= cnt; |
| 528 | |
| 529 | err = bpf_adj_branches(prog, off, off + cnt, off, false); |
| 530 | WARN_ON_ONCE(err); |
| 531 | return err; |
| 532 | } |
| 533 | |
| 534 | static void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp) |
| 535 | { |
| 536 | int i; |
| 537 | |
| 538 | for (i = 0; i < fp->aux->real_func_cnt; i++) |
| 539 | bpf_prog_kallsyms_del(fp->aux->func[i]); |
| 540 | } |
| 541 | |
| 542 | void bpf_prog_kallsyms_del_all(struct bpf_prog *fp) |
| 543 | { |
| 544 | bpf_prog_kallsyms_del_subprogs(fp); |
| 545 | bpf_prog_kallsyms_del(fp); |
| 546 | } |
| 547 | |
| 548 | #ifdef CONFIG_BPF_JIT |
| 549 | /* All BPF JIT sysctl knobs here. */ |
| 550 | int bpf_jit_enable __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON); |
| 551 | int bpf_jit_kallsyms __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON); |
| 552 | int bpf_jit_harden __read_mostly; |
| 553 | long bpf_jit_limit __read_mostly; |
| 554 | long bpf_jit_limit_max __read_mostly; |
| 555 | |
| 556 | static void |
| 557 | bpf_prog_ksym_set_addr(struct bpf_prog *prog) |
| 558 | { |
| 559 | WARN_ON_ONCE(!bpf_prog_ebpf_jited(prog)); |
| 560 | |
| 561 | prog->aux->ksym.start = (unsigned long) prog->bpf_func; |
| 562 | prog->aux->ksym.end = prog->aux->ksym.start + prog->jited_len; |
| 563 | } |
| 564 | |
| 565 | static void |
| 566 | bpf_prog_ksym_set_name(struct bpf_prog *prog) |
| 567 | { |
| 568 | char *sym = prog->aux->ksym.name; |
| 569 | const char *end = sym + KSYM_NAME_LEN; |
| 570 | const struct btf_type *type; |
| 571 | const char *func_name; |
| 572 | |
| 573 | BUILD_BUG_ON(sizeof("bpf_prog_") + |
| 574 | sizeof(prog->tag) * 2 + |
| 575 | /* name has been null terminated. |
| 576 | * We should need +1 for the '_' preceding |
| 577 | * the name. However, the null character |
| 578 | * is double counted between the name and the |
| 579 | * sizeof("bpf_prog_") above, so we omit |
| 580 | * the +1 here. |
| 581 | */ |
| 582 | sizeof(prog->aux->name) > KSYM_NAME_LEN); |
| 583 | |
| 584 | sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_"); |
| 585 | sym = bin2hex(sym, prog->tag, sizeof(prog->tag)); |
| 586 | |
| 587 | /* prog->aux->name will be ignored if full btf name is available */ |
| 588 | if (prog->aux->func_info_cnt && prog->aux->func_idx < prog->aux->func_info_cnt) { |
| 589 | type = btf_type_by_id(prog->aux->btf, |
| 590 | prog->aux->func_info[prog->aux->func_idx].type_id); |
| 591 | func_name = btf_name_by_offset(prog->aux->btf, type->name_off); |
| 592 | snprintf(sym, (size_t)(end - sym), "_%s", func_name); |
| 593 | return; |
| 594 | } |
| 595 | |
| 596 | if (prog->aux->name[0]) |
| 597 | snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name); |
| 598 | else |
| 599 | *sym = 0; |
| 600 | } |
| 601 | |
| 602 | static unsigned long bpf_get_ksym_start(struct latch_tree_node *n) |
| 603 | { |
| 604 | return container_of(n, struct bpf_ksym, tnode)->start; |
| 605 | } |
| 606 | |
| 607 | static __always_inline bool bpf_tree_less(struct latch_tree_node *a, |
| 608 | struct latch_tree_node *b) |
| 609 | { |
| 610 | return bpf_get_ksym_start(a) < bpf_get_ksym_start(b); |
| 611 | } |
| 612 | |
| 613 | static __always_inline int bpf_tree_comp(void *key, struct latch_tree_node *n) |
| 614 | { |
| 615 | unsigned long val = (unsigned long)key; |
| 616 | const struct bpf_ksym *ksym; |
| 617 | |
| 618 | ksym = container_of(n, struct bpf_ksym, tnode); |
| 619 | |
| 620 | if (val < ksym->start) |
| 621 | return -1; |
| 622 | /* Ensure that we detect return addresses as part of the program, when |
| 623 | * the final instruction is a call for a program part of the stack |
| 624 | * trace. Therefore, do val > ksym->end instead of val >= ksym->end. |
| 625 | */ |
| 626 | if (val > ksym->end) |
| 627 | return 1; |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | static const struct latch_tree_ops bpf_tree_ops = { |
| 633 | .less = bpf_tree_less, |
| 634 | .comp = bpf_tree_comp, |
| 635 | }; |
| 636 | |
| 637 | static DEFINE_SPINLOCK(bpf_lock); |
| 638 | static LIST_HEAD(bpf_kallsyms); |
| 639 | static struct latch_tree_root bpf_tree __cacheline_aligned; |
| 640 | |
| 641 | void bpf_ksym_add(struct bpf_ksym *ksym) |
| 642 | { |
| 643 | spin_lock_bh(&bpf_lock); |
| 644 | WARN_ON_ONCE(!list_empty(&ksym->lnode)); |
| 645 | list_add_tail_rcu(&ksym->lnode, &bpf_kallsyms); |
| 646 | latch_tree_insert(&ksym->tnode, &bpf_tree, &bpf_tree_ops); |
| 647 | spin_unlock_bh(&bpf_lock); |
| 648 | } |
| 649 | |
| 650 | static void __bpf_ksym_del(struct bpf_ksym *ksym) |
| 651 | { |
| 652 | if (list_empty(&ksym->lnode)) |
| 653 | return; |
| 654 | |
| 655 | latch_tree_erase(&ksym->tnode, &bpf_tree, &bpf_tree_ops); |
| 656 | list_del_rcu(&ksym->lnode); |
| 657 | } |
| 658 | |
| 659 | void bpf_ksym_del(struct bpf_ksym *ksym) |
| 660 | { |
| 661 | spin_lock_bh(&bpf_lock); |
| 662 | __bpf_ksym_del(ksym); |
| 663 | spin_unlock_bh(&bpf_lock); |
| 664 | } |
| 665 | |
| 666 | static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp) |
| 667 | { |
| 668 | return fp->jited && !bpf_prog_was_classic(fp); |
| 669 | } |
| 670 | |
| 671 | void bpf_prog_kallsyms_add(struct bpf_prog *fp) |
| 672 | { |
| 673 | if (!bpf_prog_kallsyms_candidate(fp) || |
| 674 | !bpf_token_capable(fp->aux->token, CAP_BPF)) |
| 675 | return; |
| 676 | |
| 677 | bpf_prog_ksym_set_addr(fp); |
| 678 | bpf_prog_ksym_set_name(fp); |
| 679 | fp->aux->ksym.prog = true; |
| 680 | |
| 681 | bpf_ksym_add(&fp->aux->ksym); |
| 682 | |
| 683 | #ifdef CONFIG_FINEIBT |
| 684 | /* |
| 685 | * When FineIBT, code in the __cfi_foo() symbols can get executed |
| 686 | * and hence unwinder needs help. |
| 687 | */ |
| 688 | if (cfi_mode != CFI_FINEIBT) |
| 689 | return; |
| 690 | |
| 691 | snprintf(fp->aux->ksym_prefix.name, KSYM_NAME_LEN, |
| 692 | "__cfi_%s", fp->aux->ksym.name); |
| 693 | |
| 694 | fp->aux->ksym_prefix.start = (unsigned long) fp->bpf_func - 16; |
| 695 | fp->aux->ksym_prefix.end = (unsigned long) fp->bpf_func; |
| 696 | |
| 697 | bpf_ksym_add(&fp->aux->ksym_prefix); |
| 698 | #endif |
| 699 | } |
| 700 | |
| 701 | void bpf_prog_kallsyms_del(struct bpf_prog *fp) |
| 702 | { |
| 703 | if (!bpf_prog_kallsyms_candidate(fp)) |
| 704 | return; |
| 705 | |
| 706 | bpf_ksym_del(&fp->aux->ksym); |
| 707 | #ifdef CONFIG_FINEIBT |
| 708 | if (cfi_mode != CFI_FINEIBT) |
| 709 | return; |
| 710 | bpf_ksym_del(&fp->aux->ksym_prefix); |
| 711 | #endif |
| 712 | } |
| 713 | |
| 714 | static struct bpf_ksym *bpf_ksym_find(unsigned long addr) |
| 715 | { |
| 716 | struct latch_tree_node *n; |
| 717 | |
| 718 | n = latch_tree_find((void *)addr, &bpf_tree, &bpf_tree_ops); |
| 719 | return n ? container_of(n, struct bpf_ksym, tnode) : NULL; |
| 720 | } |
| 721 | |
| 722 | int bpf_address_lookup(unsigned long addr, unsigned long *size, |
| 723 | unsigned long *off, char *sym) |
| 724 | { |
| 725 | struct bpf_ksym *ksym; |
| 726 | int ret = 0; |
| 727 | |
| 728 | rcu_read_lock(); |
| 729 | ksym = bpf_ksym_find(addr); |
| 730 | if (ksym) { |
| 731 | unsigned long symbol_start = ksym->start; |
| 732 | unsigned long symbol_end = ksym->end; |
| 733 | |
| 734 | ret = strscpy(sym, ksym->name, KSYM_NAME_LEN); |
| 735 | |
| 736 | if (size) |
| 737 | *size = symbol_end - symbol_start; |
| 738 | if (off) |
| 739 | *off = addr - symbol_start; |
| 740 | } |
| 741 | rcu_read_unlock(); |
| 742 | |
| 743 | return ret; |
| 744 | } |
| 745 | |
| 746 | bool is_bpf_text_address(unsigned long addr) |
| 747 | { |
| 748 | bool ret; |
| 749 | |
| 750 | rcu_read_lock(); |
| 751 | ret = bpf_ksym_find(addr) != NULL; |
| 752 | rcu_read_unlock(); |
| 753 | |
| 754 | return ret; |
| 755 | } |
| 756 | |
| 757 | struct bpf_prog *bpf_prog_ksym_find(unsigned long addr) |
| 758 | { |
| 759 | struct bpf_ksym *ksym; |
| 760 | |
| 761 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 762 | ksym = bpf_ksym_find(addr); |
| 763 | |
| 764 | return ksym && ksym->prog ? |
| 765 | container_of(ksym, struct bpf_prog_aux, ksym)->prog : |
| 766 | NULL; |
| 767 | } |
| 768 | |
| 769 | bool bpf_has_frame_pointer(unsigned long ip) |
| 770 | { |
| 771 | struct bpf_ksym *ksym; |
| 772 | unsigned long offset; |
| 773 | |
| 774 | guard(rcu)(); |
| 775 | |
| 776 | ksym = bpf_ksym_find(ip); |
| 777 | if (!ksym || !ksym->fp_start || !ksym->fp_end) |
| 778 | return false; |
| 779 | |
| 780 | offset = ip - ksym->start; |
| 781 | |
| 782 | return offset >= ksym->fp_start && offset < ksym->fp_end; |
| 783 | } |
| 784 | |
| 785 | const struct exception_table_entry *search_bpf_extables(unsigned long addr) |
| 786 | { |
| 787 | const struct exception_table_entry *e = NULL; |
| 788 | struct bpf_prog *prog; |
| 789 | |
| 790 | rcu_read_lock(); |
| 791 | prog = bpf_prog_ksym_find(addr); |
| 792 | if (!prog) |
| 793 | goto out; |
| 794 | if (!prog->aux->num_exentries) |
| 795 | goto out; |
| 796 | |
| 797 | e = search_extable(prog->aux->extable, prog->aux->num_exentries, addr); |
| 798 | out: |
| 799 | rcu_read_unlock(); |
| 800 | return e; |
| 801 | } |
| 802 | |
| 803 | int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type, |
| 804 | char *sym) |
| 805 | { |
| 806 | struct bpf_ksym *ksym; |
| 807 | unsigned int it = 0; |
| 808 | int ret = -ERANGE; |
| 809 | |
| 810 | if (!bpf_jit_kallsyms_enabled()) |
| 811 | return ret; |
| 812 | |
| 813 | rcu_read_lock(); |
| 814 | list_for_each_entry_rcu(ksym, &bpf_kallsyms, lnode) { |
| 815 | if (it++ != symnum) |
| 816 | continue; |
| 817 | |
| 818 | strscpy(sym, ksym->name, KSYM_NAME_LEN); |
| 819 | |
| 820 | *value = ksym->start; |
| 821 | *type = BPF_SYM_ELF_TYPE; |
| 822 | |
| 823 | ret = 0; |
| 824 | break; |
| 825 | } |
| 826 | rcu_read_unlock(); |
| 827 | |
| 828 | return ret; |
| 829 | } |
| 830 | |
| 831 | int bpf_jit_add_poke_descriptor(struct bpf_prog *prog, |
| 832 | struct bpf_jit_poke_descriptor *poke) |
| 833 | { |
| 834 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; |
| 835 | static const u32 poke_tab_max = 1024; |
| 836 | u32 slot = prog->aux->size_poke_tab; |
| 837 | u32 size = slot + 1; |
| 838 | |
| 839 | if (size > poke_tab_max) |
| 840 | return -ENOSPC; |
| 841 | if (poke->tailcall_target || poke->tailcall_target_stable || |
| 842 | poke->tailcall_bypass || poke->adj_off || poke->bypass_addr) |
| 843 | return -EINVAL; |
| 844 | |
| 845 | switch (poke->reason) { |
| 846 | case BPF_POKE_REASON_TAIL_CALL: |
| 847 | if (!poke->tail_call.map) |
| 848 | return -EINVAL; |
| 849 | break; |
| 850 | default: |
| 851 | return -EINVAL; |
| 852 | } |
| 853 | |
| 854 | tab = krealloc_array(tab, size, sizeof(*poke), GFP_KERNEL); |
| 855 | if (!tab) |
| 856 | return -ENOMEM; |
| 857 | |
| 858 | memcpy(&tab[slot], poke, sizeof(*poke)); |
| 859 | prog->aux->size_poke_tab = size; |
| 860 | prog->aux->poke_tab = tab; |
| 861 | |
| 862 | return slot; |
| 863 | } |
| 864 | |
| 865 | /* |
| 866 | * BPF program pack allocator. |
| 867 | * |
| 868 | * Most BPF programs are pretty small. Allocating a hole page for each |
| 869 | * program is sometime a waste. Many small bpf program also adds pressure |
| 870 | * to instruction TLB. To solve this issue, we introduce a BPF program pack |
| 871 | * allocator. The prog_pack allocator uses HPAGE_PMD_SIZE page (2MB on x86) |
| 872 | * to host BPF programs. |
| 873 | */ |
| 874 | #define BPF_PROG_CHUNK_SHIFT 6 |
| 875 | #define BPF_PROG_CHUNK_SIZE (1 << BPF_PROG_CHUNK_SHIFT) |
| 876 | #define BPF_PROG_CHUNK_MASK (~(BPF_PROG_CHUNK_SIZE - 1)) |
| 877 | |
| 878 | struct bpf_prog_pack { |
| 879 | struct list_head list; |
| 880 | void *ptr; |
| 881 | bool arch_flush_needed; |
| 882 | unsigned long bitmap[]; |
| 883 | }; |
| 884 | |
| 885 | void bpf_jit_fill_hole_with_zero(void *area, unsigned int size) |
| 886 | { |
| 887 | memset(area, 0, size); |
| 888 | } |
| 889 | |
| 890 | DEFINE_STATIC_CALL_NULL(bpf_arch_pred_flush, bpf_arch_pred_flush); |
| 891 | |
| 892 | /* |
| 893 | * Enabled once bpf_arch_pred_flush points at a real flush routine. Lets the |
| 894 | * pack allocator test "is a predictor flush wired up at all" with a cheap |
| 895 | * static branch instead of repeatedly querying the static call target. |
| 896 | */ |
| 897 | DEFINE_STATIC_KEY_FALSE(bpf_pred_flush_enabled); |
| 898 | |
| 899 | #define BPF_PROG_SIZE_TO_NBITS(size) (round_up(size, BPF_PROG_CHUNK_SIZE) / BPF_PROG_CHUNK_SIZE) |
| 900 | |
| 901 | static DEFINE_MUTEX(pack_mutex); |
| 902 | static LIST_HEAD(pack_list); |
| 903 | |
| 904 | /* PMD_SIZE is not available in some special config, e.g. ARCH=arm with |
| 905 | * CONFIG_MMU=n. Use PAGE_SIZE in these cases. |
| 906 | */ |
| 907 | #ifdef PMD_SIZE |
| 908 | /* PMD_SIZE is really big for some archs. It doesn't make sense to |
| 909 | * reserve too much memory in one allocation. Hardcode BPF_PROG_PACK_SIZE to |
| 910 | * 2MiB * num_possible_nodes(). On most architectures PMD_SIZE will be |
| 911 | * greater than or equal to 2MB. |
| 912 | */ |
| 913 | #define BPF_PROG_PACK_SIZE (SZ_2M * num_possible_nodes()) |
| 914 | #else |
| 915 | #define BPF_PROG_PACK_SIZE PAGE_SIZE |
| 916 | #endif |
| 917 | |
| 918 | #define BPF_PROG_CHUNK_COUNT (BPF_PROG_PACK_SIZE / BPF_PROG_CHUNK_SIZE) |
| 919 | |
| 920 | static bool bpf_jit_mem_is_rox(void) |
| 921 | { |
| 922 | return execmem_is_rox(EXECMEM_BPF); |
| 923 | } |
| 924 | |
| 925 | static struct bpf_prog_pack *alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_insns) |
| 926 | { |
| 927 | struct bpf_prog_pack *pack; |
| 928 | int err; |
| 929 | |
| 930 | pack = kzalloc_flex(*pack, bitmap, BITS_TO_LONGS(BPF_PROG_CHUNK_COUNT)); |
| 931 | if (!pack) |
| 932 | return NULL; |
| 933 | pack->ptr = bpf_jit_alloc_exec(BPF_PROG_PACK_SIZE); |
| 934 | if (!pack->ptr) |
| 935 | goto out; |
| 936 | bitmap_zero(pack->bitmap, BPF_PROG_PACK_SIZE / BPF_PROG_CHUNK_SIZE); |
| 937 | |
| 938 | if (static_branch_unlikely(&bpf_pred_flush_enabled)) |
| 939 | pack->arch_flush_needed = true; |
| 940 | if (!bpf_jit_mem_is_rox()) { |
| 941 | bpf_fill_ill_insns(pack->ptr, BPF_PROG_PACK_SIZE); |
| 942 | set_vm_flush_reset_perms(pack->ptr); |
| 943 | err = set_memory_rox((unsigned long)pack->ptr, |
| 944 | BPF_PROG_PACK_SIZE / PAGE_SIZE); |
| 945 | if (err) |
| 946 | goto out; |
| 947 | } |
| 948 | list_add_tail(&pack->list, &pack_list); |
| 949 | return pack; |
| 950 | |
| 951 | out: |
| 952 | bpf_jit_free_exec(pack->ptr); |
| 953 | kfree(pack); |
| 954 | return NULL; |
| 955 | } |
| 956 | |
| 957 | void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns, bool was_classic) |
| 958 | { |
| 959 | unsigned int nbits = BPF_PROG_SIZE_TO_NBITS(size); |
| 960 | struct bpf_prog_pack *pack, *fallback_pack = NULL; |
| 961 | unsigned long pos, fallback_pos = 0; |
| 962 | void *ptr = NULL; |
| 963 | |
| 964 | mutex_lock(&pack_mutex); |
| 965 | if (size > BPF_PROG_PACK_SIZE) { |
| 966 | /* |
| 967 | * Allocations larger than a pack get their own pages, and |
| 968 | * predictors are not flushed for such allocation. This is only |
| 969 | * safe because cBPF programs (the unprivileged attack surface) |
| 970 | * are bounded well below a pack size. |
| 971 | */ |
| 972 | if (was_classic && static_branch_unlikely(&bpf_pred_flush_enabled)) |
| 973 | pr_warn_once("BPF: Predictors not flushed for allocations greater than BPF_PROG_PACK_SIZE\n"); |
| 974 | size = round_up(size, PAGE_SIZE); |
| 975 | ptr = bpf_jit_alloc_exec(size); |
| 976 | if (ptr && !bpf_jit_mem_is_rox()) { |
| 977 | int err; |
| 978 | |
| 979 | bpf_fill_ill_insns(ptr, size); |
| 980 | set_vm_flush_reset_perms(ptr); |
| 981 | err = set_memory_rox((unsigned long)ptr, |
| 982 | size / PAGE_SIZE); |
| 983 | if (err) { |
| 984 | bpf_jit_free_exec(ptr); |
| 985 | ptr = NULL; |
| 986 | } |
| 987 | } |
| 988 | goto out; |
| 989 | } |
| 990 | list_for_each_entry(pack, &pack_list, list) { |
| 991 | pos = bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0, |
| 992 | nbits, 0); |
| 993 | if (pos >= BPF_PROG_CHUNK_COUNT) |
| 994 | continue; |
| 995 | /* Flush not enabled, use any pack */ |
| 996 | if (!static_branch_unlikely(&bpf_pred_flush_enabled)) |
| 997 | goto found_free_area; |
| 998 | /* |
| 999 | * cBPF reuse of a dirty pack triggers a flush, so prefer a |
| 1000 | * clean pack for cBPF. eBPF never flushes, so steer it to a |
| 1001 | * dirty pack and keep clean packs free for cBPF. |
| 1002 | */ |
| 1003 | if (was_classic ^ pack->arch_flush_needed) |
| 1004 | goto found_free_area; |
| 1005 | if (!fallback_pack) { |
| 1006 | fallback_pack = pack; |
| 1007 | fallback_pos = pos; |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | /* No preferred pack found */ |
| 1012 | if (fallback_pack) { |
| 1013 | pack = fallback_pack; |
| 1014 | pos = fallback_pos; |
| 1015 | goto found_free_area; |
| 1016 | } |
| 1017 | |
| 1018 | pack = alloc_new_pack(bpf_fill_ill_insns); |
| 1019 | if (!pack) |
| 1020 | goto out; |
| 1021 | |
| 1022 | pos = 0; |
| 1023 | |
| 1024 | found_free_area: |
| 1025 | /* Flush only for cBPF as it may contain a crafted gadget */ |
| 1026 | if (static_branch_unlikely(&bpf_pred_flush_enabled) && |
| 1027 | pack->arch_flush_needed && |
| 1028 | was_classic) { |
| 1029 | struct bpf_prog_pack *p; |
| 1030 | |
| 1031 | static_call_cond(bpf_arch_pred_flush)(); |
| 1032 | list_for_each_entry(p, &pack_list, list) |
| 1033 | p->arch_flush_needed = false; |
| 1034 | } |
| 1035 | bitmap_set(pack->bitmap, pos, nbits); |
| 1036 | ptr = (void *)(pack->ptr) + (pos << BPF_PROG_CHUNK_SHIFT); |
| 1037 | |
| 1038 | out: |
| 1039 | mutex_unlock(&pack_mutex); |
| 1040 | return ptr; |
| 1041 | } |
| 1042 | |
| 1043 | void bpf_prog_pack_free(void *ptr, u32 size) |
| 1044 | { |
| 1045 | struct bpf_prog_pack *pack = NULL, *tmp; |
| 1046 | unsigned int nbits; |
| 1047 | unsigned long pos; |
| 1048 | |
| 1049 | mutex_lock(&pack_mutex); |
| 1050 | if (size > BPF_PROG_PACK_SIZE) { |
| 1051 | bpf_jit_free_exec(ptr); |
| 1052 | goto out; |
| 1053 | } |
| 1054 | |
| 1055 | list_for_each_entry(tmp, &pack_list, list) { |
| 1056 | if (ptr >= tmp->ptr && (tmp->ptr + BPF_PROG_PACK_SIZE) > ptr) { |
| 1057 | pack = tmp; |
| 1058 | break; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | if (WARN_ONCE(!pack, "bpf_prog_pack bug\n")) |
| 1063 | goto out; |
| 1064 | |
| 1065 | nbits = BPF_PROG_SIZE_TO_NBITS(size); |
| 1066 | pos = ((unsigned long)ptr - (unsigned long)pack->ptr) >> BPF_PROG_CHUNK_SHIFT; |
| 1067 | |
| 1068 | WARN_ONCE(bpf_arch_text_invalidate(ptr, size), |
| 1069 | "bpf_prog_pack bug: missing bpf_arch_text_invalidate?\n"); |
| 1070 | |
| 1071 | bitmap_clear(pack->bitmap, pos, nbits); |
| 1072 | |
| 1073 | if (static_branch_unlikely(&bpf_pred_flush_enabled)) |
| 1074 | pack->arch_flush_needed = true; |
| 1075 | if (bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0, |
| 1076 | BPF_PROG_CHUNK_COUNT, 0) == 0) { |
| 1077 | list_del(&pack->list); |
| 1078 | bpf_jit_free_exec(pack->ptr); |
| 1079 | kfree(pack); |
| 1080 | } |
| 1081 | out: |
| 1082 | mutex_unlock(&pack_mutex); |
| 1083 | } |
| 1084 | |
| 1085 | static atomic_long_t bpf_jit_current; |
| 1086 | |
| 1087 | /* Can be overridden by an arch's JIT compiler if it has a custom, |
| 1088 | * dedicated BPF backend memory area, or if neither of the two |
| 1089 | * below apply. |
| 1090 | */ |
| 1091 | u64 __weak bpf_jit_alloc_exec_limit(void) |
| 1092 | { |
| 1093 | #if defined(MODULES_VADDR) |
| 1094 | return MODULES_END - MODULES_VADDR; |
| 1095 | #else |
| 1096 | return VMALLOC_END - VMALLOC_START; |
| 1097 | #endif |
| 1098 | } |
| 1099 | |
| 1100 | static int __init bpf_jit_charge_init(void) |
| 1101 | { |
| 1102 | /* Only used as heuristic here to derive limit. */ |
| 1103 | bpf_jit_limit_max = bpf_jit_alloc_exec_limit(); |
| 1104 | bpf_jit_limit = min_t(u64, round_up(bpf_jit_limit_max >> 1, |
| 1105 | PAGE_SIZE), LONG_MAX); |
| 1106 | return 0; |
| 1107 | } |
| 1108 | pure_initcall(bpf_jit_charge_init); |
| 1109 | |
| 1110 | int bpf_jit_charge_modmem(u32 size) |
| 1111 | { |
| 1112 | if (atomic_long_add_return(size, &bpf_jit_current) > READ_ONCE(bpf_jit_limit)) { |
| 1113 | if (!bpf_capable()) { |
| 1114 | atomic_long_sub(size, &bpf_jit_current); |
| 1115 | return -EPERM; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | void bpf_jit_uncharge_modmem(u32 size) |
| 1123 | { |
| 1124 | atomic_long_sub(size, &bpf_jit_current); |
| 1125 | } |
| 1126 | |
| 1127 | void *bpf_jit_alloc_exec(unsigned long size) |
| 1128 | { |
| 1129 | return execmem_alloc(EXECMEM_BPF, size); |
| 1130 | } |
| 1131 | |
| 1132 | void bpf_jit_free_exec(void *addr) |
| 1133 | { |
| 1134 | execmem_free(addr); |
| 1135 | } |
| 1136 | |
| 1137 | struct bpf_binary_header * |
| 1138 | bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr, |
| 1139 | unsigned int alignment, |
| 1140 | bpf_jit_fill_hole_t bpf_fill_ill_insns) |
| 1141 | { |
| 1142 | struct bpf_binary_header *hdr; |
| 1143 | u32 size, hole, start; |
| 1144 | |
| 1145 | WARN_ON_ONCE(!is_power_of_2(alignment) || |
| 1146 | alignment > BPF_IMAGE_ALIGNMENT); |
| 1147 | |
| 1148 | /* Most of BPF filters are really small, but if some of them |
| 1149 | * fill a page, allow at least 128 extra bytes to insert a |
| 1150 | * random section of illegal instructions. |
| 1151 | */ |
| 1152 | size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE); |
| 1153 | |
| 1154 | if (bpf_jit_charge_modmem(size)) |
| 1155 | return NULL; |
| 1156 | hdr = bpf_jit_alloc_exec(size); |
| 1157 | if (!hdr) { |
| 1158 | bpf_jit_uncharge_modmem(size); |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | |
| 1162 | /* Fill space with illegal/arch-dep instructions. */ |
| 1163 | bpf_fill_ill_insns(hdr, size); |
| 1164 | |
| 1165 | hdr->size = size; |
| 1166 | hole = min_t(unsigned int, size - (proglen + sizeof(*hdr)), |
| 1167 | PAGE_SIZE - sizeof(*hdr)); |
| 1168 | start = get_random_u32_below(hole) & ~(alignment - 1); |
| 1169 | |
| 1170 | /* Leave a random number of instructions before BPF code. */ |
| 1171 | *image_ptr = &hdr->image[start]; |
| 1172 | |
| 1173 | return hdr; |
| 1174 | } |
| 1175 | |
| 1176 | void bpf_jit_binary_free(struct bpf_binary_header *hdr) |
| 1177 | { |
| 1178 | u32 size = hdr->size; |
| 1179 | |
| 1180 | bpf_jit_free_exec(hdr); |
| 1181 | bpf_jit_uncharge_modmem(size); |
| 1182 | } |
| 1183 | |
| 1184 | /* Allocate jit binary from bpf_prog_pack allocator. |
| 1185 | * Since the allocated memory is RO+X, the JIT engine cannot write directly |
| 1186 | * to the memory. To solve this problem, a RW buffer is also allocated at |
| 1187 | * as the same time. The JIT engine should calculate offsets based on the |
| 1188 | * RO memory address, but write JITed program to the RW buffer. Once the |
| 1189 | * JIT engine finishes, it calls bpf_jit_binary_pack_finalize, which copies |
| 1190 | * the JITed program to the RO memory. |
| 1191 | */ |
| 1192 | struct bpf_binary_header * |
| 1193 | bpf_jit_binary_pack_alloc(unsigned int proglen, u8 **image_ptr, |
| 1194 | unsigned int alignment, |
| 1195 | struct bpf_binary_header **rw_header, |
| 1196 | u8 **rw_image, |
| 1197 | bpf_jit_fill_hole_t bpf_fill_ill_insns, |
| 1198 | bool was_classic) |
| 1199 | { |
| 1200 | struct bpf_binary_header *ro_header; |
| 1201 | u32 size, hole, start; |
| 1202 | |
| 1203 | WARN_ON_ONCE(!is_power_of_2(alignment) || |
| 1204 | alignment > BPF_IMAGE_ALIGNMENT); |
| 1205 | |
| 1206 | /* add 16 bytes for a random section of illegal instructions */ |
| 1207 | size = round_up(proglen + sizeof(*ro_header) + 16, BPF_PROG_CHUNK_SIZE); |
| 1208 | |
| 1209 | if (bpf_jit_charge_modmem(size)) |
| 1210 | return NULL; |
| 1211 | ro_header = bpf_prog_pack_alloc(size, bpf_fill_ill_insns, was_classic); |
| 1212 | if (!ro_header) { |
| 1213 | bpf_jit_uncharge_modmem(size); |
| 1214 | return NULL; |
| 1215 | } |
| 1216 | |
| 1217 | *rw_header = kvmalloc(size, GFP_KERNEL); |
| 1218 | if (!*rw_header) { |
| 1219 | bpf_prog_pack_free(ro_header, size); |
| 1220 | bpf_jit_uncharge_modmem(size); |
| 1221 | return NULL; |
| 1222 | } |
| 1223 | |
| 1224 | /* Fill space with illegal/arch-dep instructions. */ |
| 1225 | bpf_fill_ill_insns(*rw_header, size); |
| 1226 | (*rw_header)->size = size; |
| 1227 | |
| 1228 | hole = min_t(unsigned int, size - (proglen + sizeof(*ro_header)), |
| 1229 | BPF_PROG_CHUNK_SIZE - sizeof(*ro_header)); |
| 1230 | start = get_random_u32_below(hole) & ~(alignment - 1); |
| 1231 | |
| 1232 | *image_ptr = &ro_header->image[start]; |
| 1233 | *rw_image = &(*rw_header)->image[start]; |
| 1234 | |
| 1235 | return ro_header; |
| 1236 | } |
| 1237 | |
| 1238 | /* Copy JITed text from rw_header to its final location, the ro_header. */ |
| 1239 | int bpf_jit_binary_pack_finalize(struct bpf_binary_header *ro_header, |
| 1240 | struct bpf_binary_header *rw_header) |
| 1241 | { |
| 1242 | void *ptr; |
| 1243 | |
| 1244 | ptr = bpf_arch_text_copy(ro_header, rw_header, rw_header->size); |
| 1245 | |
| 1246 | kvfree(rw_header); |
| 1247 | |
| 1248 | if (IS_ERR(ptr)) { |
| 1249 | bpf_prog_pack_free(ro_header, ro_header->size); |
| 1250 | return PTR_ERR(ptr); |
| 1251 | } |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | /* bpf_jit_binary_pack_free is called in two different scenarios: |
| 1256 | * 1) when the program is freed after; |
| 1257 | * 2) when the JIT engine fails (before bpf_jit_binary_pack_finalize). |
| 1258 | * For case 2), we need to free both the RO memory and the RW buffer. |
| 1259 | * |
| 1260 | * bpf_jit_binary_pack_free requires proper ro_header->size. However, |
| 1261 | * bpf_jit_binary_pack_alloc does not set it. Therefore, ro_header->size |
| 1262 | * must be set with either bpf_jit_binary_pack_finalize (normal path) or |
| 1263 | * bpf_arch_text_copy (when jit fails). |
| 1264 | */ |
| 1265 | void bpf_jit_binary_pack_free(struct bpf_binary_header *ro_header, |
| 1266 | struct bpf_binary_header *rw_header) |
| 1267 | { |
| 1268 | u32 size = ro_header->size; |
| 1269 | |
| 1270 | bpf_prog_pack_free(ro_header, size); |
| 1271 | kvfree(rw_header); |
| 1272 | bpf_jit_uncharge_modmem(size); |
| 1273 | } |
| 1274 | |
| 1275 | struct bpf_binary_header * |
| 1276 | bpf_jit_binary_pack_hdr(const struct bpf_prog *fp) |
| 1277 | { |
| 1278 | unsigned long real_start = (unsigned long)fp->bpf_func; |
| 1279 | unsigned long addr; |
| 1280 | |
| 1281 | addr = real_start & BPF_PROG_CHUNK_MASK; |
| 1282 | return (void *)addr; |
| 1283 | } |
| 1284 | |
| 1285 | static inline struct bpf_binary_header * |
| 1286 | bpf_jit_binary_hdr(const struct bpf_prog *fp) |
| 1287 | { |
| 1288 | unsigned long real_start = (unsigned long)fp->bpf_func; |
| 1289 | unsigned long addr; |
| 1290 | |
| 1291 | addr = real_start & PAGE_MASK; |
| 1292 | return (void *)addr; |
| 1293 | } |
| 1294 | |
| 1295 | /* This symbol is only overridden by archs that have different |
| 1296 | * requirements than the usual eBPF JITs, f.e. when they only |
| 1297 | * implement cBPF JIT, do not set images read-only, etc. |
| 1298 | */ |
| 1299 | void __weak bpf_jit_free(struct bpf_prog *fp) |
| 1300 | { |
| 1301 | if (fp->jited) { |
| 1302 | struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp); |
| 1303 | |
| 1304 | bpf_jit_binary_free(hdr); |
| 1305 | WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp)); |
| 1306 | } |
| 1307 | |
| 1308 | bpf_prog_unlock_free(fp); |
| 1309 | } |
| 1310 | |
| 1311 | int bpf_jit_get_func_addr(const struct bpf_prog *prog, |
| 1312 | const struct bpf_insn *insn, bool extra_pass, |
| 1313 | u64 *func_addr, bool *func_addr_fixed) |
| 1314 | { |
| 1315 | s16 off = insn->off; |
| 1316 | s32 imm = insn->imm; |
| 1317 | u8 *addr; |
| 1318 | int err; |
| 1319 | |
| 1320 | *func_addr_fixed = insn->src_reg != BPF_PSEUDO_CALL; |
| 1321 | if (!*func_addr_fixed) { |
| 1322 | /* Place-holder address till the last pass has collected |
| 1323 | * all addresses for JITed subprograms in which case we |
| 1324 | * can pick them up from prog->aux. |
| 1325 | */ |
| 1326 | if (!extra_pass) |
| 1327 | addr = NULL; |
| 1328 | else if (prog->aux->func && |
| 1329 | off >= 0 && off < prog->aux->real_func_cnt) |
| 1330 | addr = (u8 *)prog->aux->func[off]->bpf_func; |
| 1331 | else |
| 1332 | return -EINVAL; |
| 1333 | } else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL && |
| 1334 | bpf_jit_supports_far_kfunc_call()) { |
| 1335 | err = bpf_get_kfunc_addr(prog, insn->imm, insn->off, &addr); |
| 1336 | if (err) |
| 1337 | return err; |
| 1338 | } else { |
| 1339 | /* Address of a BPF helper call. Since part of the core |
| 1340 | * kernel, it's always at a fixed location. __bpf_call_base |
| 1341 | * and the helper with imm relative to it are both in core |
| 1342 | * kernel. |
| 1343 | */ |
| 1344 | addr = (u8 *)__bpf_call_base + imm; |
| 1345 | } |
| 1346 | |
| 1347 | *func_addr = (unsigned long)addr; |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | const char *bpf_jit_get_prog_name(struct bpf_prog *prog) |
| 1352 | { |
| 1353 | if (prog->aux->ksym.prog) |
| 1354 | return prog->aux->ksym.name; |
| 1355 | return prog->aux->name; |
| 1356 | } |
| 1357 | |
| 1358 | static int bpf_jit_blind_insn(const struct bpf_insn *from, |
| 1359 | const struct bpf_insn *aux, |
| 1360 | struct bpf_insn *to_buff, |
| 1361 | bool emit_zext) |
| 1362 | { |
| 1363 | struct bpf_insn *to = to_buff; |
| 1364 | u32 imm_rnd = get_random_u32(); |
| 1365 | int off; |
| 1366 | |
| 1367 | BUILD_BUG_ON(BPF_REG_PARAMS + 2 != MAX_BPF_JIT_REG); |
| 1368 | BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG); |
| 1369 | |
| 1370 | /* Constraints on AX register: |
| 1371 | * |
| 1372 | * AX register is inaccessible from user space. It is mapped in |
| 1373 | * all JITs, and used here for constant blinding rewrites. It is |
| 1374 | * typically "stateless" meaning its contents are only valid within |
| 1375 | * the executed instruction, but not across several instructions. |
| 1376 | * There are a few exceptions however which are further detailed |
| 1377 | * below. |
| 1378 | * |
| 1379 | * Constant blinding is only used by JITs, not in the interpreter. |
| 1380 | * The interpreter uses AX in some occasions as a local temporary |
| 1381 | * register e.g. in DIV or MOD instructions. |
| 1382 | * |
| 1383 | * In restricted circumstances, the verifier can also use the AX |
| 1384 | * register for rewrites as long as they do not interfere with |
| 1385 | * the above cases! |
| 1386 | */ |
| 1387 | if (from->dst_reg == BPF_REG_AX || from->src_reg == BPF_REG_AX) |
| 1388 | goto out; |
| 1389 | |
| 1390 | if (from->imm == 0 && |
| 1391 | (from->code == (BPF_ALU | BPF_MOV | BPF_K) || |
| 1392 | from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) { |
| 1393 | *to++ = BPF_ALU64_REG(BPF_XOR, from->dst_reg, from->dst_reg); |
| 1394 | goto out; |
| 1395 | } |
| 1396 | |
| 1397 | switch (from->code) { |
| 1398 | case BPF_ALU | BPF_ADD | BPF_K: |
| 1399 | case BPF_ALU | BPF_SUB | BPF_K: |
| 1400 | case BPF_ALU | BPF_AND | BPF_K: |
| 1401 | case BPF_ALU | BPF_OR | BPF_K: |
| 1402 | case BPF_ALU | BPF_XOR | BPF_K: |
| 1403 | case BPF_ALU | BPF_MUL | BPF_K: |
| 1404 | case BPF_ALU | BPF_MOV | BPF_K: |
| 1405 | case BPF_ALU | BPF_DIV | BPF_K: |
| 1406 | case BPF_ALU | BPF_MOD | BPF_K: |
| 1407 | *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); |
| 1408 | *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 1409 | *to++ = BPF_ALU32_REG_OFF(from->code, from->dst_reg, BPF_REG_AX, from->off); |
| 1410 | break; |
| 1411 | |
| 1412 | case BPF_ALU64 | BPF_ADD | BPF_K: |
| 1413 | case BPF_ALU64 | BPF_SUB | BPF_K: |
| 1414 | case BPF_ALU64 | BPF_AND | BPF_K: |
| 1415 | case BPF_ALU64 | BPF_OR | BPF_K: |
| 1416 | case BPF_ALU64 | BPF_XOR | BPF_K: |
| 1417 | case BPF_ALU64 | BPF_MUL | BPF_K: |
| 1418 | case BPF_ALU64 | BPF_MOV | BPF_K: |
| 1419 | case BPF_ALU64 | BPF_DIV | BPF_K: |
| 1420 | case BPF_ALU64 | BPF_MOD | BPF_K: |
| 1421 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); |
| 1422 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 1423 | *to++ = BPF_ALU64_REG_OFF(from->code, from->dst_reg, BPF_REG_AX, from->off); |
| 1424 | break; |
| 1425 | |
| 1426 | case BPF_JMP | BPF_JEQ | BPF_K: |
| 1427 | case BPF_JMP | BPF_JNE | BPF_K: |
| 1428 | case BPF_JMP | BPF_JGT | BPF_K: |
| 1429 | case BPF_JMP | BPF_JLT | BPF_K: |
| 1430 | case BPF_JMP | BPF_JGE | BPF_K: |
| 1431 | case BPF_JMP | BPF_JLE | BPF_K: |
| 1432 | case BPF_JMP | BPF_JSGT | BPF_K: |
| 1433 | case BPF_JMP | BPF_JSLT | BPF_K: |
| 1434 | case BPF_JMP | BPF_JSGE | BPF_K: |
| 1435 | case BPF_JMP | BPF_JSLE | BPF_K: |
| 1436 | case BPF_JMP | BPF_JSET | BPF_K: |
| 1437 | /* Accommodate for extra offset in case of a backjump. */ |
| 1438 | off = from->off; |
| 1439 | if (off < 0) |
| 1440 | off -= 2; |
| 1441 | if (off < S16_MIN) |
| 1442 | return -ERANGE; |
| 1443 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); |
| 1444 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 1445 | *to++ = BPF_JMP_REG(from->code, from->dst_reg, BPF_REG_AX, off); |
| 1446 | break; |
| 1447 | |
| 1448 | case BPF_JMP32 | BPF_JEQ | BPF_K: |
| 1449 | case BPF_JMP32 | BPF_JNE | BPF_K: |
| 1450 | case BPF_JMP32 | BPF_JGT | BPF_K: |
| 1451 | case BPF_JMP32 | BPF_JLT | BPF_K: |
| 1452 | case BPF_JMP32 | BPF_JGE | BPF_K: |
| 1453 | case BPF_JMP32 | BPF_JLE | BPF_K: |
| 1454 | case BPF_JMP32 | BPF_JSGT | BPF_K: |
| 1455 | case BPF_JMP32 | BPF_JSLT | BPF_K: |
| 1456 | case BPF_JMP32 | BPF_JSGE | BPF_K: |
| 1457 | case BPF_JMP32 | BPF_JSLE | BPF_K: |
| 1458 | case BPF_JMP32 | BPF_JSET | BPF_K: |
| 1459 | /* Accommodate for extra offset in case of a backjump. */ |
| 1460 | off = from->off; |
| 1461 | if (off < 0) |
| 1462 | off -= 2; |
| 1463 | if (off < S16_MIN) |
| 1464 | return -ERANGE; |
| 1465 | *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); |
| 1466 | *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 1467 | *to++ = BPF_JMP32_REG(from->code, from->dst_reg, BPF_REG_AX, |
| 1468 | off); |
| 1469 | break; |
| 1470 | |
| 1471 | case BPF_LD | BPF_IMM | BPF_DW: |
| 1472 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[1].imm); |
| 1473 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 1474 | *to++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); |
| 1475 | *to++ = BPF_ALU64_REG(BPF_MOV, aux[0].dst_reg, BPF_REG_AX); |
| 1476 | break; |
| 1477 | case 0: /* Part 2 of BPF_LD | BPF_IMM | BPF_DW. */ |
| 1478 | *to++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ aux[0].imm); |
| 1479 | *to++ = BPF_ALU32_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 1480 | if (emit_zext) |
| 1481 | *to++ = BPF_ZEXT_REG(BPF_REG_AX); |
| 1482 | *to++ = BPF_ALU64_REG(BPF_OR, aux[0].dst_reg, BPF_REG_AX); |
| 1483 | break; |
| 1484 | |
| 1485 | case BPF_ST | BPF_MEM | BPF_DW: |
| 1486 | case BPF_ST | BPF_MEM | BPF_W: |
| 1487 | case BPF_ST | BPF_MEM | BPF_H: |
| 1488 | case BPF_ST | BPF_MEM | BPF_B: |
| 1489 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ from->imm); |
| 1490 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 1491 | *to++ = BPF_STX_MEM(from->code, from->dst_reg, BPF_REG_AX, from->off); |
| 1492 | break; |
| 1493 | |
| 1494 | case BPF_ST | BPF_PROBE_MEM32 | BPF_DW: |
| 1495 | case BPF_ST | BPF_PROBE_MEM32 | BPF_W: |
| 1496 | case BPF_ST | BPF_PROBE_MEM32 | BPF_H: |
| 1497 | case BPF_ST | BPF_PROBE_MEM32 | BPF_B: |
| 1498 | *to++ = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, imm_rnd ^ |
| 1499 | from->imm); |
| 1500 | *to++ = BPF_ALU64_IMM(BPF_XOR, BPF_REG_AX, imm_rnd); |
| 1501 | /* |
| 1502 | * Cannot use BPF_STX_MEM() macro here as it |
| 1503 | * hardcodes BPF_MEM mode, losing PROBE_MEM32 |
| 1504 | * and breaking arena addressing in the JIT. |
| 1505 | */ |
| 1506 | *to++ = (struct bpf_insn) { |
| 1507 | .code = BPF_STX | BPF_PROBE_MEM32 | |
| 1508 | BPF_SIZE(from->code), |
| 1509 | .dst_reg = from->dst_reg, |
| 1510 | .src_reg = BPF_REG_AX, |
| 1511 | .off = from->off, |
| 1512 | }; |
| 1513 | break; |
| 1514 | } |
| 1515 | out: |
| 1516 | return to - to_buff; |
| 1517 | } |
| 1518 | |
| 1519 | static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other, |
| 1520 | gfp_t gfp_extra_flags) |
| 1521 | { |
| 1522 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | gfp_extra_flags; |
| 1523 | struct bpf_prog *fp; |
| 1524 | |
| 1525 | fp = __vmalloc(fp_other->pages * PAGE_SIZE, gfp_flags); |
| 1526 | if (fp != NULL) { |
| 1527 | /* aux->prog still points to the fp_other one, so |
| 1528 | * when promoting the clone to the real program, |
| 1529 | * this still needs to be adapted. |
| 1530 | */ |
| 1531 | memcpy(fp, fp_other, fp_other->pages * PAGE_SIZE); |
| 1532 | } |
| 1533 | |
| 1534 | return fp; |
| 1535 | } |
| 1536 | |
| 1537 | static void bpf_prog_clone_free(struct bpf_prog *fp) |
| 1538 | { |
| 1539 | /* aux was stolen by the other clone, so we cannot free |
| 1540 | * it from this path! It will be freed eventually by the |
| 1541 | * other program on release. |
| 1542 | * |
| 1543 | * At this point, we don't need a deferred release since |
| 1544 | * clone is guaranteed to not be locked. |
| 1545 | */ |
| 1546 | fp->aux = NULL; |
| 1547 | fp->stats = NULL; |
| 1548 | fp->active = NULL; |
| 1549 | __bpf_prog_free(fp); |
| 1550 | } |
| 1551 | |
| 1552 | void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other) |
| 1553 | { |
| 1554 | /* We have to repoint aux->prog to self, as we don't |
| 1555 | * know whether fp here is the clone or the original. |
| 1556 | */ |
| 1557 | fp->aux->prog = fp; |
| 1558 | if (fp->aux->offload) |
| 1559 | fp->aux->offload->prog = fp; |
| 1560 | bpf_prog_clone_free(fp_other); |
| 1561 | } |
| 1562 | |
| 1563 | /* |
| 1564 | * Now this function is used only to blind the main prog and must be invoked only when |
| 1565 | * bpf_prog_need_blind() returns true. |
| 1566 | */ |
| 1567 | struct bpf_prog *bpf_jit_blind_constants(struct bpf_verifier_env *env, struct bpf_prog *prog) |
| 1568 | { |
| 1569 | struct bpf_insn insn_buff[16], aux[2]; |
| 1570 | struct bpf_prog *clone, *tmp; |
| 1571 | int insn_delta, insn_cnt; |
| 1572 | struct bpf_insn *insn; |
| 1573 | int i, rewritten; |
| 1574 | |
| 1575 | if (WARN_ON_ONCE(env && env->prog != prog)) |
| 1576 | return ERR_PTR(-EINVAL); |
| 1577 | |
| 1578 | clone = bpf_prog_clone_create(prog, GFP_USER); |
| 1579 | if (!clone) |
| 1580 | return ERR_PTR(-ENOMEM); |
| 1581 | |
| 1582 | /* make sure bpf_patch_insn_data() patches the correct prog */ |
| 1583 | if (env) |
| 1584 | env->prog = clone; |
| 1585 | |
| 1586 | insn_cnt = clone->len; |
| 1587 | insn = clone->insnsi; |
| 1588 | |
| 1589 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 1590 | if (bpf_pseudo_func(insn)) { |
| 1591 | /* ld_imm64 with an address of bpf subprog is not |
| 1592 | * a user controlled constant. Don't randomize it, |
| 1593 | * since it will conflict with jit_subprogs() logic. |
| 1594 | */ |
| 1595 | insn++; |
| 1596 | i++; |
| 1597 | continue; |
| 1598 | } |
| 1599 | |
| 1600 | /* We temporarily need to hold the original ld64 insn |
| 1601 | * so that we can still access the first part in the |
| 1602 | * second blinding run. |
| 1603 | */ |
| 1604 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW) && |
| 1605 | insn[1].code == 0) |
| 1606 | memcpy(aux, insn, sizeof(aux)); |
| 1607 | |
| 1608 | rewritten = bpf_jit_blind_insn(insn, aux, insn_buff, |
| 1609 | clone->aux->verifier_zext); |
| 1610 | if (!rewritten) |
| 1611 | continue; |
| 1612 | |
| 1613 | if (rewritten < 0) |
| 1614 | tmp = ERR_PTR(rewritten); |
| 1615 | else if (env) |
| 1616 | tmp = bpf_patch_insn_data(env, i, insn_buff, rewritten); |
| 1617 | else |
| 1618 | tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten); |
| 1619 | |
| 1620 | if (IS_ERR_OR_NULL(tmp)) { |
| 1621 | if (env) |
| 1622 | /* restore the original prog */ |
| 1623 | env->prog = prog; |
| 1624 | /* Patching may have repointed aux->prog during |
| 1625 | * realloc from the original one, so we need to |
| 1626 | * fix it up here on error. |
| 1627 | */ |
| 1628 | bpf_jit_prog_release_other(prog, clone); |
| 1629 | if (env && fatal_signal_pending(current)) |
| 1630 | return ERR_PTR(-EINTR); |
| 1631 | return IS_ERR(tmp) ? tmp : ERR_PTR(-ENOMEM); |
| 1632 | } |
| 1633 | |
| 1634 | clone = tmp; |
| 1635 | insn_delta = rewritten - 1; |
| 1636 | |
| 1637 | if (env) |
| 1638 | env->prog = clone; |
| 1639 | |
| 1640 | /* Walk new program and skip insns we just inserted. */ |
| 1641 | insn = clone->insnsi + i + insn_delta; |
| 1642 | insn_cnt += insn_delta; |
| 1643 | i += insn_delta; |
| 1644 | } |
| 1645 | |
| 1646 | clone->blinded = 1; |
| 1647 | return clone; |
| 1648 | } |
| 1649 | |
| 1650 | bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const struct bpf_prog *prog, |
| 1651 | int insn_idx) |
| 1652 | { |
| 1653 | if (!env) |
| 1654 | return false; |
| 1655 | insn_idx += prog->aux->subprog_start; |
| 1656 | return env->insn_aux_data[insn_idx].indirect_target; |
| 1657 | } |
| 1658 | |
| 1659 | u16 bpf_out_stack_arg_cnt(const struct bpf_verifier_env *env, const struct bpf_prog *prog) |
| 1660 | { |
| 1661 | const struct bpf_subprog_info *sub; |
| 1662 | |
| 1663 | if (!env) |
| 1664 | return 0; |
| 1665 | sub = &env->subprog_info[prog->aux->func_idx]; |
| 1666 | return sub->stack_arg_cnt - bpf_in_stack_arg_cnt(sub); |
| 1667 | } |
| 1668 | #endif /* CONFIG_BPF_JIT */ |
| 1669 | |
| 1670 | /* Base function for offset calculation. Needs to go into .text section, |
| 1671 | * therefore keeping it non-static as well; will also be used by JITs |
| 1672 | * anyway later on, so do not let the compiler omit it. This also needs |
| 1673 | * to go into kallsyms for correlation from e.g. bpftool, so naming |
| 1674 | * must not change. |
| 1675 | */ |
| 1676 | noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 1677 | { |
| 1678 | return 0; |
| 1679 | } |
| 1680 | EXPORT_SYMBOL_GPL(__bpf_call_base); |
| 1681 | |
| 1682 | /* All UAPI available opcodes. */ |
| 1683 | #define BPF_INSN_MAP(INSN_2, INSN_3) \ |
| 1684 | /* 32 bit ALU operations. */ \ |
| 1685 | /* Register based. */ \ |
| 1686 | INSN_3(ALU, ADD, X), \ |
| 1687 | INSN_3(ALU, SUB, X), \ |
| 1688 | INSN_3(ALU, AND, X), \ |
| 1689 | INSN_3(ALU, OR, X), \ |
| 1690 | INSN_3(ALU, LSH, X), \ |
| 1691 | INSN_3(ALU, RSH, X), \ |
| 1692 | INSN_3(ALU, XOR, X), \ |
| 1693 | INSN_3(ALU, MUL, X), \ |
| 1694 | INSN_3(ALU, MOV, X), \ |
| 1695 | INSN_3(ALU, ARSH, X), \ |
| 1696 | INSN_3(ALU, DIV, X), \ |
| 1697 | INSN_3(ALU, MOD, X), \ |
| 1698 | INSN_2(ALU, NEG), \ |
| 1699 | INSN_3(ALU, END, TO_BE), \ |
| 1700 | INSN_3(ALU, END, TO_LE), \ |
| 1701 | /* Immediate based. */ \ |
| 1702 | INSN_3(ALU, ADD, K), \ |
| 1703 | INSN_3(ALU, SUB, K), \ |
| 1704 | INSN_3(ALU, AND, K), \ |
| 1705 | INSN_3(ALU, OR, K), \ |
| 1706 | INSN_3(ALU, LSH, K), \ |
| 1707 | INSN_3(ALU, RSH, K), \ |
| 1708 | INSN_3(ALU, XOR, K), \ |
| 1709 | INSN_3(ALU, MUL, K), \ |
| 1710 | INSN_3(ALU, MOV, K), \ |
| 1711 | INSN_3(ALU, ARSH, K), \ |
| 1712 | INSN_3(ALU, DIV, K), \ |
| 1713 | INSN_3(ALU, MOD, K), \ |
| 1714 | /* 64 bit ALU operations. */ \ |
| 1715 | /* Register based. */ \ |
| 1716 | INSN_3(ALU64, ADD, X), \ |
| 1717 | INSN_3(ALU64, SUB, X), \ |
| 1718 | INSN_3(ALU64, AND, X), \ |
| 1719 | INSN_3(ALU64, OR, X), \ |
| 1720 | INSN_3(ALU64, LSH, X), \ |
| 1721 | INSN_3(ALU64, RSH, X), \ |
| 1722 | INSN_3(ALU64, XOR, X), \ |
| 1723 | INSN_3(ALU64, MUL, X), \ |
| 1724 | INSN_3(ALU64, MOV, X), \ |
| 1725 | INSN_3(ALU64, ARSH, X), \ |
| 1726 | INSN_3(ALU64, DIV, X), \ |
| 1727 | INSN_3(ALU64, MOD, X), \ |
| 1728 | INSN_2(ALU64, NEG), \ |
| 1729 | INSN_3(ALU64, END, TO_LE), \ |
| 1730 | /* Immediate based. */ \ |
| 1731 | INSN_3(ALU64, ADD, K), \ |
| 1732 | INSN_3(ALU64, SUB, K), \ |
| 1733 | INSN_3(ALU64, AND, K), \ |
| 1734 | INSN_3(ALU64, OR, K), \ |
| 1735 | INSN_3(ALU64, LSH, K), \ |
| 1736 | INSN_3(ALU64, RSH, K), \ |
| 1737 | INSN_3(ALU64, XOR, K), \ |
| 1738 | INSN_3(ALU64, MUL, K), \ |
| 1739 | INSN_3(ALU64, MOV, K), \ |
| 1740 | INSN_3(ALU64, ARSH, K), \ |
| 1741 | INSN_3(ALU64, DIV, K), \ |
| 1742 | INSN_3(ALU64, MOD, K), \ |
| 1743 | /* Call instruction. */ \ |
| 1744 | INSN_2(JMP, CALL), \ |
| 1745 | /* Exit instruction. */ \ |
| 1746 | INSN_2(JMP, EXIT), \ |
| 1747 | /* 32-bit Jump instructions. */ \ |
| 1748 | /* Register based. */ \ |
| 1749 | INSN_3(JMP32, JEQ, X), \ |
| 1750 | INSN_3(JMP32, JNE, X), \ |
| 1751 | INSN_3(JMP32, JGT, X), \ |
| 1752 | INSN_3(JMP32, JLT, X), \ |
| 1753 | INSN_3(JMP32, JGE, X), \ |
| 1754 | INSN_3(JMP32, JLE, X), \ |
| 1755 | INSN_3(JMP32, JSGT, X), \ |
| 1756 | INSN_3(JMP32, JSLT, X), \ |
| 1757 | INSN_3(JMP32, JSGE, X), \ |
| 1758 | INSN_3(JMP32, JSLE, X), \ |
| 1759 | INSN_3(JMP32, JSET, X), \ |
| 1760 | /* Immediate based. */ \ |
| 1761 | INSN_3(JMP32, JEQ, K), \ |
| 1762 | INSN_3(JMP32, JNE, K), \ |
| 1763 | INSN_3(JMP32, JGT, K), \ |
| 1764 | INSN_3(JMP32, JLT, K), \ |
| 1765 | INSN_3(JMP32, JGE, K), \ |
| 1766 | INSN_3(JMP32, JLE, K), \ |
| 1767 | INSN_3(JMP32, JSGT, K), \ |
| 1768 | INSN_3(JMP32, JSLT, K), \ |
| 1769 | INSN_3(JMP32, JSGE, K), \ |
| 1770 | INSN_3(JMP32, JSLE, K), \ |
| 1771 | INSN_3(JMP32, JSET, K), \ |
| 1772 | /* Jump instructions. */ \ |
| 1773 | /* Register based. */ \ |
| 1774 | INSN_3(JMP, JEQ, X), \ |
| 1775 | INSN_3(JMP, JNE, X), \ |
| 1776 | INSN_3(JMP, JGT, X), \ |
| 1777 | INSN_3(JMP, JLT, X), \ |
| 1778 | INSN_3(JMP, JGE, X), \ |
| 1779 | INSN_3(JMP, JLE, X), \ |
| 1780 | INSN_3(JMP, JSGT, X), \ |
| 1781 | INSN_3(JMP, JSLT, X), \ |
| 1782 | INSN_3(JMP, JSGE, X), \ |
| 1783 | INSN_3(JMP, JSLE, X), \ |
| 1784 | INSN_3(JMP, JSET, X), \ |
| 1785 | /* Immediate based. */ \ |
| 1786 | INSN_3(JMP, JEQ, K), \ |
| 1787 | INSN_3(JMP, JNE, K), \ |
| 1788 | INSN_3(JMP, JGT, K), \ |
| 1789 | INSN_3(JMP, JLT, K), \ |
| 1790 | INSN_3(JMP, JGE, K), \ |
| 1791 | INSN_3(JMP, JLE, K), \ |
| 1792 | INSN_3(JMP, JSGT, K), \ |
| 1793 | INSN_3(JMP, JSLT, K), \ |
| 1794 | INSN_3(JMP, JSGE, K), \ |
| 1795 | INSN_3(JMP, JSLE, K), \ |
| 1796 | INSN_3(JMP, JSET, K), \ |
| 1797 | INSN_2(JMP, JA), \ |
| 1798 | INSN_2(JMP32, JA), \ |
| 1799 | /* Atomic operations. */ \ |
| 1800 | INSN_3(STX, ATOMIC, B), \ |
| 1801 | INSN_3(STX, ATOMIC, H), \ |
| 1802 | INSN_3(STX, ATOMIC, W), \ |
| 1803 | INSN_3(STX, ATOMIC, DW), \ |
| 1804 | /* Store instructions. */ \ |
| 1805 | /* Register based. */ \ |
| 1806 | INSN_3(STX, MEM, B), \ |
| 1807 | INSN_3(STX, MEM, H), \ |
| 1808 | INSN_3(STX, MEM, W), \ |
| 1809 | INSN_3(STX, MEM, DW), \ |
| 1810 | /* Immediate based. */ \ |
| 1811 | INSN_3(ST, MEM, B), \ |
| 1812 | INSN_3(ST, MEM, H), \ |
| 1813 | INSN_3(ST, MEM, W), \ |
| 1814 | INSN_3(ST, MEM, DW), \ |
| 1815 | /* Load instructions. */ \ |
| 1816 | /* Register based. */ \ |
| 1817 | INSN_3(LDX, MEM, B), \ |
| 1818 | INSN_3(LDX, MEM, H), \ |
| 1819 | INSN_3(LDX, MEM, W), \ |
| 1820 | INSN_3(LDX, MEM, DW), \ |
| 1821 | INSN_3(LDX, MEMSX, B), \ |
| 1822 | INSN_3(LDX, MEMSX, H), \ |
| 1823 | INSN_3(LDX, MEMSX, W), \ |
| 1824 | /* Immediate based. */ \ |
| 1825 | INSN_3(LD, IMM, DW) |
| 1826 | |
| 1827 | bool bpf_opcode_in_insntable(u8 code) |
| 1828 | { |
| 1829 | #define BPF_INSN_2_TBL(x, y) [BPF_##x | BPF_##y] = true |
| 1830 | #define BPF_INSN_3_TBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = true |
| 1831 | static const bool public_insntable[256] = { |
| 1832 | [0 ... 255] = false, |
| 1833 | /* Now overwrite non-defaults ... */ |
| 1834 | BPF_INSN_MAP(BPF_INSN_2_TBL, BPF_INSN_3_TBL), |
| 1835 | /* UAPI exposed, but rewritten opcodes. cBPF carry-over. */ |
| 1836 | [BPF_LD | BPF_ABS | BPF_B] = true, |
| 1837 | [BPF_LD | BPF_ABS | BPF_H] = true, |
| 1838 | [BPF_LD | BPF_ABS | BPF_W] = true, |
| 1839 | [BPF_LD | BPF_IND | BPF_B] = true, |
| 1840 | [BPF_LD | BPF_IND | BPF_H] = true, |
| 1841 | [BPF_LD | BPF_IND | BPF_W] = true, |
| 1842 | [BPF_JMP | BPF_JA | BPF_X] = true, |
| 1843 | [BPF_JMP | BPF_JCOND] = true, |
| 1844 | }; |
| 1845 | #undef BPF_INSN_3_TBL |
| 1846 | #undef BPF_INSN_2_TBL |
| 1847 | return public_insntable[code]; |
| 1848 | } |
| 1849 | |
| 1850 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
| 1851 | /* Absolute value of s32 without undefined behavior for S32_MIN */ |
| 1852 | static u32 abs_s32(s32 x) |
| 1853 | { |
| 1854 | return x >= 0 ? (u32)x : -(u32)x; |
| 1855 | } |
| 1856 | |
| 1857 | static u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, |
| 1858 | const struct bpf_insn *insn); |
| 1859 | |
| 1860 | /** |
| 1861 | * ___bpf_prog_run - run eBPF program on a given context |
| 1862 | * @regs: is the array of MAX_BPF_EXT_REG eBPF pseudo-registers |
| 1863 | * @insn: is the array of eBPF instructions |
| 1864 | * |
| 1865 | * Decode and execute eBPF instructions. |
| 1866 | * |
| 1867 | * Return: whatever value is in %BPF_R0 at program exit |
| 1868 | */ |
| 1869 | static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn) |
| 1870 | { |
| 1871 | #define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y |
| 1872 | #define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z |
| 1873 | static const void * const jumptable[256] __annotate_jump_table = { |
| 1874 | [0 ... 255] = &&default_label, |
| 1875 | /* Now overwrite non-defaults ... */ |
| 1876 | BPF_INSN_MAP(BPF_INSN_2_LBL, BPF_INSN_3_LBL), |
| 1877 | /* Non-UAPI available opcodes. */ |
| 1878 | [BPF_JMP | BPF_CALL_ARGS] = &&JMP_CALL_ARGS, |
| 1879 | [BPF_JMP | BPF_TAIL_CALL] = &&JMP_TAIL_CALL, |
| 1880 | [BPF_ST | BPF_NOSPEC] = &&ST_NOSPEC, |
| 1881 | [BPF_LDX | BPF_PROBE_MEM | BPF_B] = &&LDX_PROBE_MEM_B, |
| 1882 | [BPF_LDX | BPF_PROBE_MEM | BPF_H] = &&LDX_PROBE_MEM_H, |
| 1883 | [BPF_LDX | BPF_PROBE_MEM | BPF_W] = &&LDX_PROBE_MEM_W, |
| 1884 | [BPF_LDX | BPF_PROBE_MEM | BPF_DW] = &&LDX_PROBE_MEM_DW, |
| 1885 | [BPF_LDX | BPF_PROBE_MEMSX | BPF_B] = &&LDX_PROBE_MEMSX_B, |
| 1886 | [BPF_LDX | BPF_PROBE_MEMSX | BPF_H] = &&LDX_PROBE_MEMSX_H, |
| 1887 | [BPF_LDX | BPF_PROBE_MEMSX | BPF_W] = &&LDX_PROBE_MEMSX_W, |
| 1888 | }; |
| 1889 | #undef BPF_INSN_3_LBL |
| 1890 | #undef BPF_INSN_2_LBL |
| 1891 | u32 tail_call_cnt = 0; |
| 1892 | |
| 1893 | #define CONT ({ insn++; goto select_insn; }) |
| 1894 | #define CONT_JMP ({ insn++; goto select_insn; }) |
| 1895 | |
| 1896 | select_insn: |
| 1897 | goto *jumptable[insn->code]; |
| 1898 | |
| 1899 | /* Explicitly mask the register-based shift amounts with 63 or 31 |
| 1900 | * to avoid undefined behavior. Normally this won't affect the |
| 1901 | * generated code, for example, in case of native 64 bit archs such |
| 1902 | * as x86-64 or arm64, the compiler is optimizing the AND away for |
| 1903 | * the interpreter. In case of JITs, each of the JIT backends compiles |
| 1904 | * the BPF shift operations to machine instructions which produce |
| 1905 | * implementation-defined results in such a case; the resulting |
| 1906 | * contents of the register may be arbitrary, but program behaviour |
| 1907 | * as a whole remains defined. In other words, in case of JIT backends, |
| 1908 | * the AND must /not/ be added to the emitted LSH/RSH/ARSH translation. |
| 1909 | */ |
| 1910 | /* ALU (shifts) */ |
| 1911 | #define SHT(OPCODE, OP) \ |
| 1912 | ALU64_##OPCODE##_X: \ |
| 1913 | DST = DST OP (SRC & 63); \ |
| 1914 | CONT; \ |
| 1915 | ALU_##OPCODE##_X: \ |
| 1916 | DST = (u32) DST OP ((u32) SRC & 31); \ |
| 1917 | CONT; \ |
| 1918 | ALU64_##OPCODE##_K: \ |
| 1919 | DST = DST OP IMM; \ |
| 1920 | CONT; \ |
| 1921 | ALU_##OPCODE##_K: \ |
| 1922 | DST = (u32) DST OP (u32) IMM; \ |
| 1923 | CONT; |
| 1924 | /* ALU (rest) */ |
| 1925 | #define ALU(OPCODE, OP) \ |
| 1926 | ALU64_##OPCODE##_X: \ |
| 1927 | DST = DST OP SRC; \ |
| 1928 | CONT; \ |
| 1929 | ALU_##OPCODE##_X: \ |
| 1930 | DST = (u32) DST OP (u32) SRC; \ |
| 1931 | CONT; \ |
| 1932 | ALU64_##OPCODE##_K: \ |
| 1933 | DST = DST OP IMM; \ |
| 1934 | CONT; \ |
| 1935 | ALU_##OPCODE##_K: \ |
| 1936 | DST = (u32) DST OP (u32) IMM; \ |
| 1937 | CONT; |
| 1938 | ALU(ADD, +) |
| 1939 | ALU(SUB, -) |
| 1940 | ALU(AND, &) |
| 1941 | ALU(OR, |) |
| 1942 | ALU(XOR, ^) |
| 1943 | ALU(MUL, *) |
| 1944 | SHT(LSH, <<) |
| 1945 | SHT(RSH, >>) |
| 1946 | #undef SHT |
| 1947 | #undef ALU |
| 1948 | ALU_NEG: |
| 1949 | DST = (u32) -DST; |
| 1950 | CONT; |
| 1951 | ALU64_NEG: |
| 1952 | DST = -DST; |
| 1953 | CONT; |
| 1954 | ALU_MOV_X: |
| 1955 | switch (OFF) { |
| 1956 | case 0: |
| 1957 | DST = (u32) SRC; |
| 1958 | break; |
| 1959 | case 8: |
| 1960 | DST = (u32)(s8) SRC; |
| 1961 | break; |
| 1962 | case 16: |
| 1963 | DST = (u32)(s16) SRC; |
| 1964 | break; |
| 1965 | } |
| 1966 | CONT; |
| 1967 | ALU_MOV_K: |
| 1968 | DST = (u32) IMM; |
| 1969 | CONT; |
| 1970 | ALU64_MOV_X: |
| 1971 | switch (OFF) { |
| 1972 | case 0: |
| 1973 | DST = SRC; |
| 1974 | break; |
| 1975 | case 8: |
| 1976 | DST = (s8) SRC; |
| 1977 | break; |
| 1978 | case 16: |
| 1979 | DST = (s16) SRC; |
| 1980 | break; |
| 1981 | case 32: |
| 1982 | DST = (s32) SRC; |
| 1983 | break; |
| 1984 | } |
| 1985 | CONT; |
| 1986 | ALU64_MOV_K: |
| 1987 | DST = IMM; |
| 1988 | CONT; |
| 1989 | LD_IMM_DW: |
| 1990 | DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32; |
| 1991 | insn++; |
| 1992 | CONT; |
| 1993 | ALU_ARSH_X: |
| 1994 | DST = (u64) (u32) (((s32) DST) >> (SRC & 31)); |
| 1995 | CONT; |
| 1996 | ALU_ARSH_K: |
| 1997 | DST = (u64) (u32) (((s32) DST) >> IMM); |
| 1998 | CONT; |
| 1999 | ALU64_ARSH_X: |
| 2000 | (*(s64 *) &DST) >>= (SRC & 63); |
| 2001 | CONT; |
| 2002 | ALU64_ARSH_K: |
| 2003 | (*(s64 *) &DST) >>= IMM; |
| 2004 | CONT; |
| 2005 | ALU64_MOD_X: |
| 2006 | switch (OFF) { |
| 2007 | case 0: |
| 2008 | div64_u64_rem(DST, SRC, &AX); |
| 2009 | DST = AX; |
| 2010 | break; |
| 2011 | case 1: |
| 2012 | AX = div64_s64(DST, SRC); |
| 2013 | DST = DST - AX * SRC; |
| 2014 | break; |
| 2015 | } |
| 2016 | CONT; |
| 2017 | ALU_MOD_X: |
| 2018 | switch (OFF) { |
| 2019 | case 0: |
| 2020 | AX = (u32) DST; |
| 2021 | DST = do_div(AX, (u32) SRC); |
| 2022 | break; |
| 2023 | case 1: |
| 2024 | AX = abs_s32((s32)DST); |
| 2025 | AX = do_div(AX, abs_s32((s32)SRC)); |
| 2026 | if ((s32)DST < 0) |
| 2027 | DST = (u32)-AX; |
| 2028 | else |
| 2029 | DST = (u32)AX; |
| 2030 | break; |
| 2031 | } |
| 2032 | CONT; |
| 2033 | ALU64_MOD_K: |
| 2034 | switch (OFF) { |
| 2035 | case 0: |
| 2036 | div64_u64_rem(DST, IMM, &AX); |
| 2037 | DST = AX; |
| 2038 | break; |
| 2039 | case 1: |
| 2040 | AX = div64_s64(DST, IMM); |
| 2041 | DST = DST - AX * IMM; |
| 2042 | break; |
| 2043 | } |
| 2044 | CONT; |
| 2045 | ALU_MOD_K: |
| 2046 | switch (OFF) { |
| 2047 | case 0: |
| 2048 | AX = (u32) DST; |
| 2049 | DST = do_div(AX, (u32) IMM); |
| 2050 | break; |
| 2051 | case 1: |
| 2052 | AX = abs_s32((s32)DST); |
| 2053 | AX = do_div(AX, abs_s32((s32)IMM)); |
| 2054 | if ((s32)DST < 0) |
| 2055 | DST = (u32)-AX; |
| 2056 | else |
| 2057 | DST = (u32)AX; |
| 2058 | break; |
| 2059 | } |
| 2060 | CONT; |
| 2061 | ALU64_DIV_X: |
| 2062 | switch (OFF) { |
| 2063 | case 0: |
| 2064 | DST = div64_u64(DST, SRC); |
| 2065 | break; |
| 2066 | case 1: |
| 2067 | DST = div64_s64(DST, SRC); |
| 2068 | break; |
| 2069 | } |
| 2070 | CONT; |
| 2071 | ALU_DIV_X: |
| 2072 | switch (OFF) { |
| 2073 | case 0: |
| 2074 | AX = (u32) DST; |
| 2075 | do_div(AX, (u32) SRC); |
| 2076 | DST = (u32) AX; |
| 2077 | break; |
| 2078 | case 1: |
| 2079 | AX = abs_s32((s32)DST); |
| 2080 | do_div(AX, abs_s32((s32)SRC)); |
| 2081 | if (((s32)DST < 0) == ((s32)SRC < 0)) |
| 2082 | DST = (u32)AX; |
| 2083 | else |
| 2084 | DST = (u32)-AX; |
| 2085 | break; |
| 2086 | } |
| 2087 | CONT; |
| 2088 | ALU64_DIV_K: |
| 2089 | switch (OFF) { |
| 2090 | case 0: |
| 2091 | DST = div64_u64(DST, IMM); |
| 2092 | break; |
| 2093 | case 1: |
| 2094 | DST = div64_s64(DST, IMM); |
| 2095 | break; |
| 2096 | } |
| 2097 | CONT; |
| 2098 | ALU_DIV_K: |
| 2099 | switch (OFF) { |
| 2100 | case 0: |
| 2101 | AX = (u32) DST; |
| 2102 | do_div(AX, (u32) IMM); |
| 2103 | DST = (u32) AX; |
| 2104 | break; |
| 2105 | case 1: |
| 2106 | AX = abs_s32((s32)DST); |
| 2107 | do_div(AX, abs_s32((s32)IMM)); |
| 2108 | if (((s32)DST < 0) == ((s32)IMM < 0)) |
| 2109 | DST = (u32)AX; |
| 2110 | else |
| 2111 | DST = (u32)-AX; |
| 2112 | break; |
| 2113 | } |
| 2114 | CONT; |
| 2115 | ALU_END_TO_BE: |
| 2116 | switch (IMM) { |
| 2117 | case 16: |
| 2118 | DST = (__force u16) cpu_to_be16(DST); |
| 2119 | break; |
| 2120 | case 32: |
| 2121 | DST = (__force u32) cpu_to_be32(DST); |
| 2122 | break; |
| 2123 | case 64: |
| 2124 | DST = (__force u64) cpu_to_be64(DST); |
| 2125 | break; |
| 2126 | } |
| 2127 | CONT; |
| 2128 | ALU_END_TO_LE: |
| 2129 | switch (IMM) { |
| 2130 | case 16: |
| 2131 | DST = (__force u16) cpu_to_le16(DST); |
| 2132 | break; |
| 2133 | case 32: |
| 2134 | DST = (__force u32) cpu_to_le32(DST); |
| 2135 | break; |
| 2136 | case 64: |
| 2137 | DST = (__force u64) cpu_to_le64(DST); |
| 2138 | break; |
| 2139 | } |
| 2140 | CONT; |
| 2141 | ALU64_END_TO_LE: |
| 2142 | switch (IMM) { |
| 2143 | case 16: |
| 2144 | DST = (__force u16) __swab16(DST); |
| 2145 | break; |
| 2146 | case 32: |
| 2147 | DST = (__force u32) __swab32(DST); |
| 2148 | break; |
| 2149 | case 64: |
| 2150 | DST = (__force u64) __swab64(DST); |
| 2151 | break; |
| 2152 | } |
| 2153 | CONT; |
| 2154 | |
| 2155 | /* CALL */ |
| 2156 | JMP_CALL: |
| 2157 | /* Function call scratches BPF_R1-BPF_R5 registers, |
| 2158 | * preserves BPF_R6-BPF_R9, and stores return value |
| 2159 | * into BPF_R0. |
| 2160 | */ |
| 2161 | BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3, |
| 2162 | BPF_R4, BPF_R5); |
| 2163 | CONT; |
| 2164 | |
| 2165 | JMP_CALL_ARGS: |
| 2166 | BPF_R0 = interpreters_args[insn->off](BPF_R1, BPF_R2, BPF_R3, |
| 2167 | BPF_R4, BPF_R5, |
| 2168 | insn + insn->imm + 1); |
| 2169 | CONT; |
| 2170 | |
| 2171 | JMP_TAIL_CALL: { |
| 2172 | struct bpf_map *map = (struct bpf_map *) (unsigned long) BPF_R2; |
| 2173 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 2174 | struct bpf_prog *prog; |
| 2175 | u32 index = BPF_R3; |
| 2176 | |
| 2177 | if (unlikely(index >= array->map.max_entries)) |
| 2178 | goto out; |
| 2179 | |
| 2180 | if (unlikely(tail_call_cnt >= MAX_TAIL_CALL_CNT)) |
| 2181 | goto out; |
| 2182 | |
| 2183 | prog = READ_ONCE(array->ptrs[index]); |
| 2184 | if (!prog) |
| 2185 | goto out; |
| 2186 | |
| 2187 | tail_call_cnt++; |
| 2188 | |
| 2189 | /* ARG1 at this point is guaranteed to point to CTX from |
| 2190 | * the verifier side due to the fact that the tail call is |
| 2191 | * handled like a helper, that is, bpf_tail_call_proto, |
| 2192 | * where arg1_type is ARG_PTR_TO_CTX. |
| 2193 | */ |
| 2194 | insn = prog->insnsi; |
| 2195 | goto select_insn; |
| 2196 | out: |
| 2197 | CONT; |
| 2198 | } |
| 2199 | JMP_JA: |
| 2200 | insn += insn->off; |
| 2201 | CONT; |
| 2202 | JMP32_JA: |
| 2203 | insn += insn->imm; |
| 2204 | CONT; |
| 2205 | JMP_EXIT: |
| 2206 | return BPF_R0; |
| 2207 | /* JMP */ |
| 2208 | #define COND_JMP(SIGN, OPCODE, CMP_OP) \ |
| 2209 | JMP_##OPCODE##_X: \ |
| 2210 | if ((SIGN##64) DST CMP_OP (SIGN##64) SRC) { \ |
| 2211 | insn += insn->off; \ |
| 2212 | CONT_JMP; \ |
| 2213 | } \ |
| 2214 | CONT; \ |
| 2215 | JMP32_##OPCODE##_X: \ |
| 2216 | if ((SIGN##32) DST CMP_OP (SIGN##32) SRC) { \ |
| 2217 | insn += insn->off; \ |
| 2218 | CONT_JMP; \ |
| 2219 | } \ |
| 2220 | CONT; \ |
| 2221 | JMP_##OPCODE##_K: \ |
| 2222 | if ((SIGN##64) DST CMP_OP (SIGN##64) IMM) { \ |
| 2223 | insn += insn->off; \ |
| 2224 | CONT_JMP; \ |
| 2225 | } \ |
| 2226 | CONT; \ |
| 2227 | JMP32_##OPCODE##_K: \ |
| 2228 | if ((SIGN##32) DST CMP_OP (SIGN##32) IMM) { \ |
| 2229 | insn += insn->off; \ |
| 2230 | CONT_JMP; \ |
| 2231 | } \ |
| 2232 | CONT; |
| 2233 | COND_JMP(u, JEQ, ==) |
| 2234 | COND_JMP(u, JNE, !=) |
| 2235 | COND_JMP(u, JGT, >) |
| 2236 | COND_JMP(u, JLT, <) |
| 2237 | COND_JMP(u, JGE, >=) |
| 2238 | COND_JMP(u, JLE, <=) |
| 2239 | COND_JMP(u, JSET, &) |
| 2240 | COND_JMP(s, JSGT, >) |
| 2241 | COND_JMP(s, JSLT, <) |
| 2242 | COND_JMP(s, JSGE, >=) |
| 2243 | COND_JMP(s, JSLE, <=) |
| 2244 | #undef COND_JMP |
| 2245 | /* ST, STX and LDX*/ |
| 2246 | ST_NOSPEC: |
| 2247 | /* Speculation barrier for mitigating Speculative Store Bypass, |
| 2248 | * Bounds-Check Bypass and Type Confusion. In case of arm64, we |
| 2249 | * rely on the firmware mitigation as controlled via the ssbd |
| 2250 | * kernel parameter. Whenever the mitigation is enabled, it |
| 2251 | * works for all of the kernel code with no need to provide any |
| 2252 | * additional instructions here. In case of x86, we use 'lfence' |
| 2253 | * insn for mitigation. We reuse preexisting logic from Spectre |
| 2254 | * v1 mitigation that happens to produce the required code on |
| 2255 | * x86 for v4 as well. |
| 2256 | */ |
| 2257 | barrier_nospec(); |
| 2258 | CONT; |
| 2259 | #define LDST(SIZEOP, SIZE) \ |
| 2260 | STX_MEM_##SIZEOP: \ |
| 2261 | *(SIZE *)(unsigned long) (DST + insn->off) = SRC; \ |
| 2262 | CONT; \ |
| 2263 | ST_MEM_##SIZEOP: \ |
| 2264 | *(SIZE *)(unsigned long) (DST + insn->off) = IMM; \ |
| 2265 | CONT; \ |
| 2266 | LDX_MEM_##SIZEOP: \ |
| 2267 | DST = *(SIZE *)(unsigned long) (SRC + insn->off); \ |
| 2268 | CONT; \ |
| 2269 | LDX_PROBE_MEM_##SIZEOP: \ |
| 2270 | bpf_probe_read_kernel_common(&DST, sizeof(SIZE), \ |
| 2271 | (const void *)(long) (SRC + insn->off)); \ |
| 2272 | DST = *((SIZE *)&DST); \ |
| 2273 | CONT; |
| 2274 | |
| 2275 | LDST(B, u8) |
| 2276 | LDST(H, u16) |
| 2277 | LDST(W, u32) |
| 2278 | LDST(DW, u64) |
| 2279 | #undef LDST |
| 2280 | |
| 2281 | #define LDSX(SIZEOP, SIZE) \ |
| 2282 | LDX_MEMSX_##SIZEOP: \ |
| 2283 | DST = *(SIZE *)(unsigned long) (SRC + insn->off); \ |
| 2284 | CONT; \ |
| 2285 | LDX_PROBE_MEMSX_##SIZEOP: \ |
| 2286 | bpf_probe_read_kernel_common(&DST, sizeof(SIZE), \ |
| 2287 | (const void *)(long) (SRC + insn->off)); \ |
| 2288 | DST = *((SIZE *)&DST); \ |
| 2289 | CONT; |
| 2290 | |
| 2291 | LDSX(B, s8) |
| 2292 | LDSX(H, s16) |
| 2293 | LDSX(W, s32) |
| 2294 | #undef LDSX |
| 2295 | |
| 2296 | #define ATOMIC_ALU_OP(BOP, KOP) \ |
| 2297 | case BOP: \ |
| 2298 | if (BPF_SIZE(insn->code) == BPF_W) \ |
| 2299 | atomic_##KOP((u32) SRC, (atomic_t *)(unsigned long) \ |
| 2300 | (DST + insn->off)); \ |
| 2301 | else if (BPF_SIZE(insn->code) == BPF_DW) \ |
| 2302 | atomic64_##KOP((u64) SRC, (atomic64_t *)(unsigned long) \ |
| 2303 | (DST + insn->off)); \ |
| 2304 | else \ |
| 2305 | goto default_label; \ |
| 2306 | break; \ |
| 2307 | case BOP | BPF_FETCH: \ |
| 2308 | if (BPF_SIZE(insn->code) == BPF_W) \ |
| 2309 | SRC = (u32) atomic_fetch_##KOP( \ |
| 2310 | (u32) SRC, \ |
| 2311 | (atomic_t *)(unsigned long) (DST + insn->off)); \ |
| 2312 | else if (BPF_SIZE(insn->code) == BPF_DW) \ |
| 2313 | SRC = (u64) atomic64_fetch_##KOP( \ |
| 2314 | (u64) SRC, \ |
| 2315 | (atomic64_t *)(unsigned long) (DST + insn->off)); \ |
| 2316 | else \ |
| 2317 | goto default_label; \ |
| 2318 | break; |
| 2319 | |
| 2320 | STX_ATOMIC_DW: |
| 2321 | STX_ATOMIC_W: |
| 2322 | STX_ATOMIC_H: |
| 2323 | STX_ATOMIC_B: |
| 2324 | switch (IMM) { |
| 2325 | /* Atomic read-modify-write instructions support only W and DW |
| 2326 | * size modifiers. |
| 2327 | */ |
| 2328 | ATOMIC_ALU_OP(BPF_ADD, add) |
| 2329 | ATOMIC_ALU_OP(BPF_AND, and) |
| 2330 | ATOMIC_ALU_OP(BPF_OR, or) |
| 2331 | ATOMIC_ALU_OP(BPF_XOR, xor) |
| 2332 | #undef ATOMIC_ALU_OP |
| 2333 | |
| 2334 | case BPF_XCHG: |
| 2335 | if (BPF_SIZE(insn->code) == BPF_W) |
| 2336 | SRC = (u32) atomic_xchg( |
| 2337 | (atomic_t *)(unsigned long) (DST + insn->off), |
| 2338 | (u32) SRC); |
| 2339 | else if (BPF_SIZE(insn->code) == BPF_DW) |
| 2340 | SRC = (u64) atomic64_xchg( |
| 2341 | (atomic64_t *)(unsigned long) (DST + insn->off), |
| 2342 | (u64) SRC); |
| 2343 | else |
| 2344 | goto default_label; |
| 2345 | break; |
| 2346 | case BPF_CMPXCHG: |
| 2347 | if (BPF_SIZE(insn->code) == BPF_W) |
| 2348 | BPF_R0 = (u32) atomic_cmpxchg( |
| 2349 | (atomic_t *)(unsigned long) (DST + insn->off), |
| 2350 | (u32) BPF_R0, (u32) SRC); |
| 2351 | else if (BPF_SIZE(insn->code) == BPF_DW) |
| 2352 | BPF_R0 = (u64) atomic64_cmpxchg( |
| 2353 | (atomic64_t *)(unsigned long) (DST + insn->off), |
| 2354 | (u64) BPF_R0, (u64) SRC); |
| 2355 | else |
| 2356 | goto default_label; |
| 2357 | break; |
| 2358 | /* Atomic load and store instructions support all size |
| 2359 | * modifiers. |
| 2360 | */ |
| 2361 | case BPF_LOAD_ACQ: |
| 2362 | switch (BPF_SIZE(insn->code)) { |
| 2363 | #define LOAD_ACQUIRE(SIZEOP, SIZE) \ |
| 2364 | case BPF_##SIZEOP: \ |
| 2365 | DST = (SIZE)smp_load_acquire( \ |
| 2366 | (SIZE *)(unsigned long)(SRC + insn->off)); \ |
| 2367 | break; |
| 2368 | LOAD_ACQUIRE(B, u8) |
| 2369 | LOAD_ACQUIRE(H, u16) |
| 2370 | LOAD_ACQUIRE(W, u32) |
| 2371 | #ifdef CONFIG_64BIT |
| 2372 | LOAD_ACQUIRE(DW, u64) |
| 2373 | #endif |
| 2374 | #undef LOAD_ACQUIRE |
| 2375 | default: |
| 2376 | goto default_label; |
| 2377 | } |
| 2378 | break; |
| 2379 | case BPF_STORE_REL: |
| 2380 | switch (BPF_SIZE(insn->code)) { |
| 2381 | #define STORE_RELEASE(SIZEOP, SIZE) \ |
| 2382 | case BPF_##SIZEOP: \ |
| 2383 | smp_store_release( \ |
| 2384 | (SIZE *)(unsigned long)(DST + insn->off), (SIZE)SRC); \ |
| 2385 | break; |
| 2386 | STORE_RELEASE(B, u8) |
| 2387 | STORE_RELEASE(H, u16) |
| 2388 | STORE_RELEASE(W, u32) |
| 2389 | #ifdef CONFIG_64BIT |
| 2390 | STORE_RELEASE(DW, u64) |
| 2391 | #endif |
| 2392 | #undef STORE_RELEASE |
| 2393 | default: |
| 2394 | goto default_label; |
| 2395 | } |
| 2396 | break; |
| 2397 | |
| 2398 | default: |
| 2399 | goto default_label; |
| 2400 | } |
| 2401 | CONT; |
| 2402 | |
| 2403 | default_label: |
| 2404 | /* If we ever reach this, we have a bug somewhere. Die hard here |
| 2405 | * instead of just returning 0; we could be somewhere in a subprog, |
| 2406 | * so execution could continue otherwise which we do /not/ want. |
| 2407 | * |
| 2408 | * Note, verifier whitelists all opcodes in bpf_opcode_in_insntable(). |
| 2409 | */ |
| 2410 | pr_warn("BPF interpreter: unknown opcode %02x (imm: 0x%x)\n", |
| 2411 | insn->code, insn->imm); |
| 2412 | BUG_ON(1); |
| 2413 | return 0; |
| 2414 | } |
| 2415 | |
| 2416 | #define PROG_NAME(stack_size) __bpf_prog_run##stack_size |
| 2417 | #define DEFINE_BPF_PROG_RUN(stack_size) \ |
| 2418 | static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \ |
| 2419 | { \ |
| 2420 | u64 stack[stack_size / sizeof(u64)]; \ |
| 2421 | u64 regs[MAX_BPF_EXT_REG] = {}; \ |
| 2422 | \ |
| 2423 | kmsan_unpoison_memory(stack, sizeof(stack)); \ |
| 2424 | FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \ |
| 2425 | ARG1 = (u64) (unsigned long) ctx; \ |
| 2426 | return ___bpf_prog_run(regs, insn); \ |
| 2427 | } |
| 2428 | |
| 2429 | #define PROG_NAME_ARGS(stack_size) __bpf_prog_run_args##stack_size |
| 2430 | #define DEFINE_BPF_PROG_RUN_ARGS(stack_size) \ |
| 2431 | static u64 PROG_NAME_ARGS(stack_size)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, \ |
| 2432 | const struct bpf_insn *insn) \ |
| 2433 | { \ |
| 2434 | u64 stack[stack_size / sizeof(u64)]; \ |
| 2435 | u64 regs[MAX_BPF_EXT_REG]; \ |
| 2436 | \ |
| 2437 | kmsan_unpoison_memory(stack, sizeof(stack)); \ |
| 2438 | FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)]; \ |
| 2439 | BPF_R1 = r1; \ |
| 2440 | BPF_R2 = r2; \ |
| 2441 | BPF_R3 = r3; \ |
| 2442 | BPF_R4 = r4; \ |
| 2443 | BPF_R5 = r5; \ |
| 2444 | return ___bpf_prog_run(regs, insn); \ |
| 2445 | } |
| 2446 | |
| 2447 | #define EVAL1(FN, X) FN(X) |
| 2448 | #define EVAL2(FN, X, Y...) FN(X) EVAL1(FN, Y) |
| 2449 | #define EVAL3(FN, X, Y...) FN(X) EVAL2(FN, Y) |
| 2450 | #define EVAL4(FN, X, Y...) FN(X) EVAL3(FN, Y) |
| 2451 | #define EVAL5(FN, X, Y...) FN(X) EVAL4(FN, Y) |
| 2452 | #define EVAL6(FN, X, Y...) FN(X) EVAL5(FN, Y) |
| 2453 | |
| 2454 | EVAL6(DEFINE_BPF_PROG_RUN, 32, 64, 96, 128, 160, 192); |
| 2455 | EVAL6(DEFINE_BPF_PROG_RUN, 224, 256, 288, 320, 352, 384); |
| 2456 | EVAL4(DEFINE_BPF_PROG_RUN, 416, 448, 480, 512); |
| 2457 | |
| 2458 | EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 32, 64, 96, 128, 160, 192); |
| 2459 | EVAL6(DEFINE_BPF_PROG_RUN_ARGS, 224, 256, 288, 320, 352, 384); |
| 2460 | EVAL4(DEFINE_BPF_PROG_RUN_ARGS, 416, 448, 480, 512); |
| 2461 | |
| 2462 | #define PROG_NAME_LIST(stack_size) PROG_NAME(stack_size), |
| 2463 | |
| 2464 | static unsigned int (*interpreters[])(const void *ctx, |
| 2465 | const struct bpf_insn *insn) = { |
| 2466 | EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192) |
| 2467 | EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384) |
| 2468 | EVAL4(PROG_NAME_LIST, 416, 448, 480, 512) |
| 2469 | }; |
| 2470 | #undef PROG_NAME_LIST |
| 2471 | #define PROG_NAME_LIST(stack_size) PROG_NAME_ARGS(stack_size), |
| 2472 | static __maybe_unused |
| 2473 | u64 (*interpreters_args[])(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5, |
| 2474 | const struct bpf_insn *insn) = { |
| 2475 | EVAL6(PROG_NAME_LIST, 32, 64, 96, 128, 160, 192) |
| 2476 | EVAL6(PROG_NAME_LIST, 224, 256, 288, 320, 352, 384) |
| 2477 | EVAL4(PROG_NAME_LIST, 416, 448, 480, 512) |
| 2478 | }; |
| 2479 | #undef PROG_NAME_LIST |
| 2480 | |
| 2481 | #ifdef CONFIG_BPF_SYSCALL |
| 2482 | int bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth) |
| 2483 | { |
| 2484 | stack_depth = max_t(u32, stack_depth, 1); |
| 2485 | /* Prevent out-of-bounds read to interpreters_args */ |
| 2486 | if (stack_depth > MAX_BPF_STACK) |
| 2487 | return -EINVAL; |
| 2488 | insn->off = (round_up(stack_depth, 32) / 32) - 1; |
| 2489 | insn->code = BPF_JMP | BPF_CALL_ARGS; |
| 2490 | return 0; |
| 2491 | } |
| 2492 | |
| 2493 | s32 bpf_call_args_imm(s16 idx) |
| 2494 | { |
| 2495 | if (WARN_ON_ONCE(idx < 0 || idx >= ARRAY_SIZE(interpreters_args))) |
| 2496 | return 0; |
| 2497 | return BPF_CALL_IMM(interpreters_args[idx]); |
| 2498 | } |
| 2499 | #endif |
| 2500 | #endif |
| 2501 | |
| 2502 | static unsigned int __bpf_prog_ret0_warn(const void *ctx, |
| 2503 | const struct bpf_insn *insn) |
| 2504 | { |
| 2505 | /* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON |
| 2506 | * is not working properly, so warn about it! |
| 2507 | */ |
| 2508 | WARN_ON_ONCE(1); |
| 2509 | return 0; |
| 2510 | } |
| 2511 | |
| 2512 | static bool __bpf_prog_map_compatible(struct bpf_map *map, |
| 2513 | const struct bpf_prog *fp) |
| 2514 | { |
| 2515 | enum bpf_prog_type prog_type = resolve_prog_type(fp); |
| 2516 | struct bpf_prog_aux *aux = fp->aux; |
| 2517 | enum bpf_cgroup_storage_type i; |
| 2518 | bool ret = false; |
| 2519 | u64 cookie; |
| 2520 | |
| 2521 | if (fp->kprobe_override) |
| 2522 | return ret; |
| 2523 | |
| 2524 | spin_lock(&map->owner_lock); |
| 2525 | /* There's no owner yet where we could check for compatibility. */ |
| 2526 | if (!map->owner) { |
| 2527 | map->owner = bpf_map_owner_alloc(map); |
| 2528 | if (!map->owner) |
| 2529 | goto err; |
| 2530 | map->owner->type = prog_type; |
| 2531 | map->owner->jited = fp->jited; |
| 2532 | map->owner->xdp_has_frags = aux->xdp_has_frags; |
| 2533 | map->owner->sleepable = fp->sleepable; |
| 2534 | map->owner->expected_attach_type = fp->expected_attach_type; |
| 2535 | map->owner->attach_func_proto = aux->attach_func_proto; |
| 2536 | for_each_cgroup_storage_type(i) { |
| 2537 | map->owner->storage_cookie[i] = |
| 2538 | aux->cgroup_storage[i] ? |
| 2539 | aux->cgroup_storage[i]->cookie : 0; |
| 2540 | } |
| 2541 | ret = true; |
| 2542 | } else { |
| 2543 | ret = map->owner->type == prog_type && |
| 2544 | map->owner->jited == fp->jited && |
| 2545 | map->owner->xdp_has_frags == aux->xdp_has_frags && |
| 2546 | map->owner->sleepable == fp->sleepable; |
| 2547 | if (ret && |
| 2548 | map->map_type == BPF_MAP_TYPE_PROG_ARRAY && |
| 2549 | map->owner->expected_attach_type != fp->expected_attach_type) |
| 2550 | ret = false; |
| 2551 | for_each_cgroup_storage_type(i) { |
| 2552 | if (!ret) |
| 2553 | break; |
| 2554 | cookie = aux->cgroup_storage[i] ? |
| 2555 | aux->cgroup_storage[i]->cookie : 0; |
| 2556 | ret = map->owner->storage_cookie[i] == cookie || |
| 2557 | (!cookie && !aux->tail_call_reachable); |
| 2558 | } |
| 2559 | if (ret && |
| 2560 | map->owner->attach_func_proto != aux->attach_func_proto) { |
| 2561 | switch (prog_type) { |
| 2562 | case BPF_PROG_TYPE_TRACING: |
| 2563 | case BPF_PROG_TYPE_LSM: |
| 2564 | case BPF_PROG_TYPE_EXT: |
| 2565 | case BPF_PROG_TYPE_STRUCT_OPS: |
| 2566 | ret = false; |
| 2567 | break; |
| 2568 | default: |
| 2569 | break; |
| 2570 | } |
| 2571 | } |
| 2572 | } |
| 2573 | err: |
| 2574 | spin_unlock(&map->owner_lock); |
| 2575 | return ret; |
| 2576 | } |
| 2577 | |
| 2578 | bool bpf_prog_map_compatible(struct bpf_map *map, const struct bpf_prog *fp) |
| 2579 | { |
| 2580 | /* XDP programs inserted into maps are not guaranteed to run on |
| 2581 | * a particular netdev (and can run outside driver context entirely |
| 2582 | * in the case of devmap and cpumap). Until device checks |
| 2583 | * are implemented, prohibit adding dev-bound programs to program maps. |
| 2584 | */ |
| 2585 | if (bpf_prog_is_dev_bound(fp->aux)) |
| 2586 | return false; |
| 2587 | |
| 2588 | return __bpf_prog_map_compatible(map, fp); |
| 2589 | } |
| 2590 | |
| 2591 | static int bpf_check_tail_call(const struct bpf_prog *fp) |
| 2592 | { |
| 2593 | struct bpf_prog_aux *aux = fp->aux; |
| 2594 | int i, ret = 0; |
| 2595 | |
| 2596 | mutex_lock(&aux->used_maps_mutex); |
| 2597 | for (i = 0; i < aux->used_map_cnt; i++) { |
| 2598 | struct bpf_map *map = aux->used_maps[i]; |
| 2599 | |
| 2600 | if (!map_type_contains_progs(map)) |
| 2601 | continue; |
| 2602 | |
| 2603 | if (!__bpf_prog_map_compatible(map, fp)) { |
| 2604 | ret = -EINVAL; |
| 2605 | goto out; |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | out: |
| 2610 | mutex_unlock(&aux->used_maps_mutex); |
| 2611 | return ret; |
| 2612 | } |
| 2613 | |
| 2614 | static bool bpf_prog_select_interpreter(struct bpf_prog *fp) |
| 2615 | { |
| 2616 | bool select_interpreter = false; |
| 2617 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
| 2618 | u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1); |
| 2619 | u32 idx = (round_up(stack_depth, 32) / 32) - 1; |
| 2620 | |
| 2621 | /* may_goto may cause stack size > 512, leading to idx out-of-bounds. |
| 2622 | * But for non-JITed programs, we don't need bpf_func, so no bounds |
| 2623 | * check needed. |
| 2624 | */ |
| 2625 | if (idx < ARRAY_SIZE(interpreters)) { |
| 2626 | fp->bpf_func = interpreters[idx]; |
| 2627 | select_interpreter = true; |
| 2628 | } else { |
| 2629 | fp->bpf_func = __bpf_prog_ret0_warn; |
| 2630 | } |
| 2631 | #else |
| 2632 | fp->bpf_func = __bpf_prog_ret0_warn; |
| 2633 | #endif |
| 2634 | return select_interpreter; |
| 2635 | } |
| 2636 | |
| 2637 | static struct bpf_prog *bpf_prog_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog) |
| 2638 | { |
| 2639 | #ifdef CONFIG_BPF_JIT |
| 2640 | struct bpf_prog *orig_prog; |
| 2641 | |
| 2642 | if (!bpf_prog_need_blind(prog)) |
| 2643 | return bpf_int_jit_compile(env, prog); |
| 2644 | |
| 2645 | orig_prog = prog; |
| 2646 | prog = bpf_jit_blind_constants(env, prog); |
| 2647 | /* |
| 2648 | * Fall back to the interpreter after blinding failures, except when |
| 2649 | * the loader was killed. |
| 2650 | */ |
| 2651 | if (IS_ERR(prog)) { |
| 2652 | if (PTR_ERR(prog) == -EINTR) |
| 2653 | return prog; |
| 2654 | goto out_restore; |
| 2655 | } |
| 2656 | |
| 2657 | prog = bpf_int_jit_compile(env, prog); |
| 2658 | if (prog->jited) { |
| 2659 | bpf_jit_prog_release_other(prog, orig_prog); |
| 2660 | return prog; |
| 2661 | } |
| 2662 | |
| 2663 | bpf_jit_prog_release_other(orig_prog, prog); |
| 2664 | |
| 2665 | out_restore: |
| 2666 | prog = orig_prog; |
| 2667 | #endif |
| 2668 | return prog; |
| 2669 | } |
| 2670 | |
| 2671 | struct bpf_prog *__bpf_prog_select_runtime(struct bpf_verifier_env *env, struct bpf_prog *fp, |
| 2672 | int *err) |
| 2673 | { |
| 2674 | struct bpf_prog *jit_prog; |
| 2675 | |
| 2676 | /* In case of BPF to BPF calls, verifier did all the prep |
| 2677 | * work with regards to JITing, etc. |
| 2678 | */ |
| 2679 | bool jit_needed = fp->jit_required; |
| 2680 | |
| 2681 | if (fp->bpf_func) |
| 2682 | goto finalize; |
| 2683 | |
| 2684 | if (!bpf_prog_select_interpreter(fp)) |
| 2685 | jit_needed = true; |
| 2686 | |
| 2687 | /* eBPF JITs can rewrite the program in case constant |
| 2688 | * blinding is active. However, in case of error during |
| 2689 | * blinding, bpf_int_jit_compile() must always return a |
| 2690 | * valid program, which in this case would simply not |
| 2691 | * be JITed, but falls back to the interpreter. |
| 2692 | */ |
| 2693 | if (!bpf_prog_is_offloaded(fp->aux)) { |
| 2694 | *err = bpf_prog_alloc_jited_linfo(fp); |
| 2695 | if (*err) |
| 2696 | return fp; |
| 2697 | |
| 2698 | jit_prog = bpf_prog_jit_compile(env, fp); |
| 2699 | if (IS_ERR(jit_prog)) { |
| 2700 | *err = PTR_ERR(jit_prog); |
| 2701 | return fp; |
| 2702 | } |
| 2703 | fp = jit_prog; |
| 2704 | bpf_prog_jit_attempt_done(fp); |
| 2705 | if (!fp->jited && jit_needed) { |
| 2706 | *err = -ENOTSUPP; |
| 2707 | return fp; |
| 2708 | } |
| 2709 | } else { |
| 2710 | *err = bpf_prog_offload_compile(fp); |
| 2711 | if (*err) |
| 2712 | return fp; |
| 2713 | } |
| 2714 | |
| 2715 | finalize: |
| 2716 | *err = bpf_prog_lock_ro(fp); |
| 2717 | if (*err) |
| 2718 | return fp; |
| 2719 | |
| 2720 | /* The tail call compatibility check can only be done at |
| 2721 | * this late stage as we need to determine, if we deal |
| 2722 | * with JITed or non JITed program concatenations and not |
| 2723 | * all eBPF JITs might immediately support all features. |
| 2724 | */ |
| 2725 | *err = bpf_check_tail_call(fp); |
| 2726 | |
| 2727 | return fp; |
| 2728 | } |
| 2729 | |
| 2730 | /** |
| 2731 | * bpf_prog_select_runtime - select exec runtime for BPF program |
| 2732 | * @fp: bpf_prog populated with BPF program |
| 2733 | * @err: pointer to error variable |
| 2734 | * |
| 2735 | * Try to JIT eBPF program, if JIT is not available, use interpreter. |
| 2736 | * The BPF program will be executed via bpf_prog_run() function. |
| 2737 | * |
| 2738 | * Return: the &fp argument along with &err set to 0 for success or |
| 2739 | * a negative errno code on failure |
| 2740 | */ |
| 2741 | struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err) |
| 2742 | { |
| 2743 | return __bpf_prog_select_runtime(NULL, fp, err); |
| 2744 | } |
| 2745 | EXPORT_SYMBOL_GPL(bpf_prog_select_runtime); |
| 2746 | |
| 2747 | static unsigned int __bpf_prog_ret1(const void *ctx, |
| 2748 | const struct bpf_insn *insn) |
| 2749 | { |
| 2750 | return 1; |
| 2751 | } |
| 2752 | |
| 2753 | static struct bpf_prog_dummy { |
| 2754 | struct bpf_prog prog; |
| 2755 | } dummy_bpf_prog = { |
| 2756 | .prog = { |
| 2757 | .bpf_func = __bpf_prog_ret1, |
| 2758 | }, |
| 2759 | }; |
| 2760 | |
| 2761 | struct bpf_prog_array bpf_empty_prog_array = { |
| 2762 | .items = { |
| 2763 | { .prog = NULL }, |
| 2764 | }, |
| 2765 | }; |
| 2766 | EXPORT_SYMBOL(bpf_empty_prog_array); |
| 2767 | |
| 2768 | struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags) |
| 2769 | { |
| 2770 | struct bpf_prog_array *p; |
| 2771 | |
| 2772 | if (prog_cnt) |
| 2773 | p = kzalloc_flex(*p, items, prog_cnt + 1, flags); |
| 2774 | else |
| 2775 | p = &bpf_empty_prog_array; |
| 2776 | |
| 2777 | return p; |
| 2778 | } |
| 2779 | |
| 2780 | void bpf_prog_array_free(struct bpf_prog_array *progs) |
| 2781 | { |
| 2782 | if (!progs || progs == &bpf_empty_prog_array) |
| 2783 | return; |
| 2784 | kfree_rcu(progs, rcu); |
| 2785 | } |
| 2786 | |
| 2787 | static void __bpf_prog_array_free_sleepable_cb(struct rcu_head *rcu) |
| 2788 | { |
| 2789 | struct bpf_prog_array *progs; |
| 2790 | |
| 2791 | /* |
| 2792 | * RCU Tasks Trace grace period implies RCU grace period, there is no |
| 2793 | * need to call kfree_rcu(), just call kfree() directly. |
| 2794 | */ |
| 2795 | progs = container_of(rcu, struct bpf_prog_array, rcu); |
| 2796 | kfree(progs); |
| 2797 | } |
| 2798 | |
| 2799 | void bpf_prog_array_free_sleepable(struct bpf_prog_array *progs) |
| 2800 | { |
| 2801 | if (!progs || progs == &bpf_empty_prog_array) |
| 2802 | return; |
| 2803 | call_rcu_tasks_trace(&progs->rcu, __bpf_prog_array_free_sleepable_cb); |
| 2804 | } |
| 2805 | |
| 2806 | int bpf_prog_array_length(struct bpf_prog_array *array) |
| 2807 | { |
| 2808 | struct bpf_prog_array_item *item; |
| 2809 | u32 cnt = 0; |
| 2810 | |
| 2811 | for (item = array->items; item->prog; item++) |
| 2812 | if (item->prog != &dummy_bpf_prog.prog) |
| 2813 | cnt++; |
| 2814 | return cnt; |
| 2815 | } |
| 2816 | |
| 2817 | bool bpf_prog_array_is_empty(struct bpf_prog_array *array) |
| 2818 | { |
| 2819 | struct bpf_prog_array_item *item; |
| 2820 | |
| 2821 | for (item = array->items; item->prog; item++) |
| 2822 | if (item->prog != &dummy_bpf_prog.prog) |
| 2823 | return false; |
| 2824 | return true; |
| 2825 | } |
| 2826 | |
| 2827 | static bool bpf_prog_array_copy_core(struct bpf_prog_array *array, |
| 2828 | u32 *prog_ids, |
| 2829 | u32 request_cnt) |
| 2830 | { |
| 2831 | struct bpf_prog_array_item *item; |
| 2832 | int i = 0; |
| 2833 | |
| 2834 | for (item = array->items; item->prog; item++) { |
| 2835 | if (item->prog == &dummy_bpf_prog.prog) |
| 2836 | continue; |
| 2837 | prog_ids[i] = item->prog->aux->id; |
| 2838 | if (++i == request_cnt) { |
| 2839 | item++; |
| 2840 | break; |
| 2841 | } |
| 2842 | } |
| 2843 | |
| 2844 | return !!(item->prog); |
| 2845 | } |
| 2846 | |
| 2847 | int bpf_prog_array_copy_to_user(struct bpf_prog_array *array, |
| 2848 | __u32 __user *prog_ids, u32 cnt) |
| 2849 | { |
| 2850 | unsigned long err = 0; |
| 2851 | bool nospc; |
| 2852 | u32 *ids; |
| 2853 | |
| 2854 | /* users of this function are doing: |
| 2855 | * cnt = bpf_prog_array_length(); |
| 2856 | * if (cnt > 0) |
| 2857 | * bpf_prog_array_copy_to_user(..., cnt); |
| 2858 | * so below kcalloc doesn't need extra cnt > 0 check. |
| 2859 | */ |
| 2860 | ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN); |
| 2861 | if (!ids) |
| 2862 | return -ENOMEM; |
| 2863 | nospc = bpf_prog_array_copy_core(array, ids, cnt); |
| 2864 | err = copy_to_user(prog_ids, ids, cnt * sizeof(u32)); |
| 2865 | kfree(ids); |
| 2866 | if (err) |
| 2867 | return -EFAULT; |
| 2868 | if (nospc) |
| 2869 | return -ENOSPC; |
| 2870 | return 0; |
| 2871 | } |
| 2872 | |
| 2873 | void bpf_prog_array_delete_safe(struct bpf_prog_array *array, |
| 2874 | struct bpf_prog *old_prog) |
| 2875 | { |
| 2876 | struct bpf_prog_array_item *item; |
| 2877 | |
| 2878 | for (item = array->items; item->prog; item++) |
| 2879 | if (item->prog == old_prog) { |
| 2880 | WRITE_ONCE(item->prog, &dummy_bpf_prog.prog); |
| 2881 | break; |
| 2882 | } |
| 2883 | } |
| 2884 | |
| 2885 | /** |
| 2886 | * bpf_prog_array_delete_safe_at() - Replaces the program at the given |
| 2887 | * index into the program array with |
| 2888 | * a dummy no-op program. |
| 2889 | * @array: a bpf_prog_array |
| 2890 | * @index: the index of the program to replace |
| 2891 | * |
| 2892 | * Skips over dummy programs, by not counting them, when calculating |
| 2893 | * the position of the program to replace. |
| 2894 | * |
| 2895 | * Return: |
| 2896 | * * 0 - Success |
| 2897 | * * -EINVAL - Invalid index value. Must be a non-negative integer. |
| 2898 | * * -ENOENT - Index out of range |
| 2899 | */ |
| 2900 | int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index) |
| 2901 | { |
| 2902 | return bpf_prog_array_update_at(array, index, &dummy_bpf_prog.prog); |
| 2903 | } |
| 2904 | |
| 2905 | /** |
| 2906 | * bpf_prog_array_update_at() - Updates the program at the given index |
| 2907 | * into the program array. |
| 2908 | * @array: a bpf_prog_array |
| 2909 | * @index: the index of the program to update |
| 2910 | * @prog: the program to insert into the array |
| 2911 | * |
| 2912 | * Skips over dummy programs, by not counting them, when calculating |
| 2913 | * the position of the program to update. |
| 2914 | * |
| 2915 | * Return: |
| 2916 | * * 0 - Success |
| 2917 | * * -EINVAL - Invalid index value. Must be a non-negative integer. |
| 2918 | * * -ENOENT - Index out of range |
| 2919 | */ |
| 2920 | int bpf_prog_array_update_at(struct bpf_prog_array *array, int index, |
| 2921 | struct bpf_prog *prog) |
| 2922 | { |
| 2923 | struct bpf_prog_array_item *item; |
| 2924 | |
| 2925 | if (unlikely(index < 0)) |
| 2926 | return -EINVAL; |
| 2927 | |
| 2928 | for (item = array->items; item->prog; item++) { |
| 2929 | if (item->prog == &dummy_bpf_prog.prog) |
| 2930 | continue; |
| 2931 | if (!index) { |
| 2932 | WRITE_ONCE(item->prog, prog); |
| 2933 | return 0; |
| 2934 | } |
| 2935 | index--; |
| 2936 | } |
| 2937 | return -ENOENT; |
| 2938 | } |
| 2939 | |
| 2940 | int bpf_prog_array_copy(struct bpf_prog_array *old_array, |
| 2941 | struct bpf_prog *exclude_prog, |
| 2942 | struct bpf_prog *include_prog, |
| 2943 | u64 bpf_cookie, |
| 2944 | struct bpf_prog_array **new_array) |
| 2945 | { |
| 2946 | int new_prog_cnt, carry_prog_cnt = 0; |
| 2947 | struct bpf_prog_array_item *existing, *new; |
| 2948 | struct bpf_prog_array *array; |
| 2949 | bool found_exclude = false; |
| 2950 | |
| 2951 | /* Figure out how many existing progs we need to carry over to |
| 2952 | * the new array. |
| 2953 | */ |
| 2954 | if (old_array) { |
| 2955 | existing = old_array->items; |
| 2956 | for (; existing->prog; existing++) { |
| 2957 | if (existing->prog == exclude_prog) { |
| 2958 | found_exclude = true; |
| 2959 | continue; |
| 2960 | } |
| 2961 | if (existing->prog != &dummy_bpf_prog.prog) |
| 2962 | carry_prog_cnt++; |
| 2963 | if (existing->prog == include_prog) |
| 2964 | return -EEXIST; |
| 2965 | } |
| 2966 | } |
| 2967 | |
| 2968 | if (exclude_prog && !found_exclude) |
| 2969 | return -ENOENT; |
| 2970 | |
| 2971 | /* How many progs (not NULL) will be in the new array? */ |
| 2972 | new_prog_cnt = carry_prog_cnt; |
| 2973 | if (include_prog) |
| 2974 | new_prog_cnt += 1; |
| 2975 | |
| 2976 | /* Do we have any prog (not NULL) in the new array? */ |
| 2977 | if (!new_prog_cnt) { |
| 2978 | *new_array = NULL; |
| 2979 | return 0; |
| 2980 | } |
| 2981 | |
| 2982 | /* +1 as the end of prog_array is marked with NULL */ |
| 2983 | array = bpf_prog_array_alloc(new_prog_cnt + 1, GFP_KERNEL); |
| 2984 | if (!array) |
| 2985 | return -ENOMEM; |
| 2986 | new = array->items; |
| 2987 | |
| 2988 | /* Fill in the new prog array */ |
| 2989 | if (carry_prog_cnt) { |
| 2990 | existing = old_array->items; |
| 2991 | for (; existing->prog; existing++) { |
| 2992 | if (existing->prog == exclude_prog || |
| 2993 | existing->prog == &dummy_bpf_prog.prog) |
| 2994 | continue; |
| 2995 | |
| 2996 | new->prog = existing->prog; |
| 2997 | new->bpf_cookie = existing->bpf_cookie; |
| 2998 | new++; |
| 2999 | } |
| 3000 | } |
| 3001 | if (include_prog) { |
| 3002 | new->prog = include_prog; |
| 3003 | new->bpf_cookie = bpf_cookie; |
| 3004 | new++; |
| 3005 | } |
| 3006 | new->prog = NULL; |
| 3007 | *new_array = array; |
| 3008 | return 0; |
| 3009 | } |
| 3010 | |
| 3011 | int bpf_prog_array_copy_info(struct bpf_prog_array *array, |
| 3012 | u32 *prog_ids, u32 request_cnt, |
| 3013 | u32 *prog_cnt) |
| 3014 | { |
| 3015 | u32 cnt = 0; |
| 3016 | |
| 3017 | if (array) |
| 3018 | cnt = bpf_prog_array_length(array); |
| 3019 | |
| 3020 | *prog_cnt = cnt; |
| 3021 | |
| 3022 | /* return early if user requested only program count or nothing to copy */ |
| 3023 | if (!request_cnt || !cnt) |
| 3024 | return 0; |
| 3025 | |
| 3026 | /* this function is called under trace/bpf_trace.c: bpf_event_mutex */ |
| 3027 | return bpf_prog_array_copy_core(array, prog_ids, request_cnt) ? -ENOSPC |
| 3028 | : 0; |
| 3029 | } |
| 3030 | |
| 3031 | void __bpf_free_used_maps(struct bpf_prog_aux *aux, |
| 3032 | struct bpf_map **used_maps, u32 len) |
| 3033 | { |
| 3034 | struct bpf_map *map; |
| 3035 | bool sleepable; |
| 3036 | u32 i; |
| 3037 | |
| 3038 | sleepable = aux->prog->sleepable; |
| 3039 | for (i = 0; i < len; i++) { |
| 3040 | map = used_maps[i]; |
| 3041 | if (map->ops->map_poke_untrack) |
| 3042 | map->ops->map_poke_untrack(map, aux); |
| 3043 | if (sleepable) |
| 3044 | atomic64_dec(&map->sleepable_refcnt); |
| 3045 | bpf_map_put(map); |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | static void bpf_free_used_maps(struct bpf_prog_aux *aux) |
| 3050 | { |
| 3051 | __bpf_free_used_maps(aux, aux->used_maps, aux->used_map_cnt); |
| 3052 | kfree(aux->used_maps); |
| 3053 | } |
| 3054 | |
| 3055 | void __bpf_free_used_btfs(struct btf_mod_pair *used_btfs, u32 len) |
| 3056 | { |
| 3057 | #ifdef CONFIG_BPF_SYSCALL |
| 3058 | struct btf_mod_pair *btf_mod; |
| 3059 | u32 i; |
| 3060 | |
| 3061 | for (i = 0; i < len; i++) { |
| 3062 | btf_mod = &used_btfs[i]; |
| 3063 | if (btf_mod->module) |
| 3064 | module_put(btf_mod->module); |
| 3065 | btf_put(btf_mod->btf); |
| 3066 | } |
| 3067 | #endif |
| 3068 | } |
| 3069 | |
| 3070 | static void bpf_free_used_btfs(struct bpf_prog_aux *aux) |
| 3071 | { |
| 3072 | __bpf_free_used_btfs(aux->used_btfs, aux->used_btf_cnt); |
| 3073 | kfree(aux->used_btfs); |
| 3074 | } |
| 3075 | |
| 3076 | static void bpf_prog_free_deferred(struct work_struct *work) |
| 3077 | { |
| 3078 | struct bpf_prog_aux *aux; |
| 3079 | int i; |
| 3080 | |
| 3081 | aux = container_of(work, struct bpf_prog_aux, work); |
| 3082 | #ifdef CONFIG_BPF_SYSCALL |
| 3083 | bpf_free_kfunc_btf_tab(aux->kfunc_btf_tab); |
| 3084 | bpf_prog_stream_free(aux->prog); |
| 3085 | #endif |
| 3086 | #ifdef CONFIG_CGROUP_BPF |
| 3087 | if (aux->cgroup_atype != CGROUP_BPF_ATTACH_TYPE_INVALID) |
| 3088 | bpf_cgroup_atype_put(aux->cgroup_atype); |
| 3089 | #endif |
| 3090 | bpf_free_used_maps(aux); |
| 3091 | bpf_free_used_btfs(aux); |
| 3092 | bpf_prog_disassoc_struct_ops(aux->prog); |
| 3093 | if (bpf_prog_is_dev_bound(aux)) |
| 3094 | bpf_prog_dev_bound_destroy(aux->prog); |
| 3095 | #ifdef CONFIG_PERF_EVENTS |
| 3096 | if (aux->prog->has_callchain_buf) |
| 3097 | put_callchain_buffers(); |
| 3098 | #endif |
| 3099 | if (aux->dst_trampoline) |
| 3100 | bpf_trampoline_put(aux->dst_trampoline); |
| 3101 | for (i = 0; i < aux->real_func_cnt; i++) { |
| 3102 | /* We can just unlink the subprog poke descriptor table as |
| 3103 | * it was originally linked to the main program and is also |
| 3104 | * released along with it. |
| 3105 | */ |
| 3106 | aux->func[i]->aux->poke_tab = NULL; |
| 3107 | bpf_jit_free(aux->func[i]); |
| 3108 | } |
| 3109 | if (aux->real_func_cnt) { |
| 3110 | kfree(aux->func); |
| 3111 | bpf_prog_unlock_free(aux->prog); |
| 3112 | } else { |
| 3113 | bpf_jit_free(aux->prog); |
| 3114 | } |
| 3115 | } |
| 3116 | |
| 3117 | void bpf_prog_free(struct bpf_prog *fp) |
| 3118 | { |
| 3119 | struct bpf_prog_aux *aux = fp->aux; |
| 3120 | |
| 3121 | if (aux->dst_prog) |
| 3122 | bpf_prog_put(aux->dst_prog); |
| 3123 | bpf_token_put(aux->token); |
| 3124 | INIT_WORK(&aux->work, bpf_prog_free_deferred); |
| 3125 | schedule_work(&aux->work); |
| 3126 | } |
| 3127 | EXPORT_SYMBOL_GPL(bpf_prog_free); |
| 3128 | |
| 3129 | /* RNG for unprivileged user space with separated state from prandom_u32(). */ |
| 3130 | static DEFINE_PER_CPU(struct rnd_state, bpf_user_rnd_state); |
| 3131 | |
| 3132 | void bpf_user_rnd_init_once(void) |
| 3133 | { |
| 3134 | prandom_init_once(&bpf_user_rnd_state); |
| 3135 | } |
| 3136 | |
| 3137 | BPF_CALL_0(bpf_user_rnd_u32) |
| 3138 | { |
| 3139 | /* Should someone ever have the rather unwise idea to use some |
| 3140 | * of the registers passed into this function, then note that |
| 3141 | * this function is called from native eBPF and classic-to-eBPF |
| 3142 | * transformations. Register assignments from both sides are |
| 3143 | * different, f.e. classic always sets fn(ctx, A, X) here. |
| 3144 | */ |
| 3145 | struct rnd_state *state; |
| 3146 | u32 res; |
| 3147 | |
| 3148 | state = &get_cpu_var(bpf_user_rnd_state); |
| 3149 | res = prandom_u32_state(state); |
| 3150 | put_cpu_var(bpf_user_rnd_state); |
| 3151 | |
| 3152 | return res; |
| 3153 | } |
| 3154 | |
| 3155 | BPF_CALL_0(bpf_get_raw_cpu_id) |
| 3156 | { |
| 3157 | return raw_smp_processor_id(); |
| 3158 | } |
| 3159 | |
| 3160 | /* Weak definitions of helper functions in case we don't have bpf syscall. */ |
| 3161 | const struct bpf_func_proto bpf_map_lookup_elem_proto __weak; |
| 3162 | const struct bpf_func_proto bpf_map_update_elem_proto __weak; |
| 3163 | const struct bpf_func_proto bpf_map_delete_elem_proto __weak; |
| 3164 | const struct bpf_func_proto bpf_map_push_elem_proto __weak; |
| 3165 | const struct bpf_func_proto bpf_map_pop_elem_proto __weak; |
| 3166 | const struct bpf_func_proto bpf_map_peek_elem_proto __weak; |
| 3167 | const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto __weak; |
| 3168 | const struct bpf_func_proto bpf_spin_lock_proto __weak; |
| 3169 | const struct bpf_func_proto bpf_spin_unlock_proto __weak; |
| 3170 | const struct bpf_func_proto bpf_jiffies64_proto __weak; |
| 3171 | |
| 3172 | const struct bpf_func_proto bpf_get_prandom_u32_proto __weak; |
| 3173 | const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak; |
| 3174 | const struct bpf_func_proto bpf_get_numa_node_id_proto __weak; |
| 3175 | const struct bpf_func_proto bpf_ktime_get_ns_proto __weak; |
| 3176 | const struct bpf_func_proto bpf_ktime_get_boot_ns_proto __weak; |
| 3177 | const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto __weak; |
| 3178 | const struct bpf_func_proto bpf_ktime_get_tai_ns_proto __weak; |
| 3179 | |
| 3180 | const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak; |
| 3181 | const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak; |
| 3182 | const struct bpf_func_proto bpf_get_current_comm_proto __weak; |
| 3183 | const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak; |
| 3184 | const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto __weak; |
| 3185 | const struct bpf_func_proto bpf_get_local_storage_proto __weak; |
| 3186 | const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto __weak; |
| 3187 | const struct bpf_func_proto bpf_snprintf_btf_proto __weak; |
| 3188 | const struct bpf_func_proto bpf_seq_printf_btf_proto __weak; |
| 3189 | const struct bpf_func_proto bpf_set_retval_proto __weak; |
| 3190 | const struct bpf_func_proto bpf_get_retval_proto __weak; |
| 3191 | |
| 3192 | const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void) |
| 3193 | { |
| 3194 | return NULL; |
| 3195 | } |
| 3196 | |
| 3197 | const struct bpf_func_proto * __weak bpf_get_trace_vprintk_proto(void) |
| 3198 | { |
| 3199 | return NULL; |
| 3200 | } |
| 3201 | |
| 3202 | const struct bpf_func_proto * __weak bpf_get_perf_event_read_value_proto(void) |
| 3203 | { |
| 3204 | return NULL; |
| 3205 | } |
| 3206 | |
| 3207 | u64 __weak |
| 3208 | bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, |
| 3209 | void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy) |
| 3210 | { |
| 3211 | return -ENOTSUPP; |
| 3212 | } |
| 3213 | EXPORT_SYMBOL_GPL(bpf_event_output); |
| 3214 | |
| 3215 | /* Always built-in helper functions. */ |
| 3216 | const struct bpf_func_proto bpf_tail_call_proto = { |
| 3217 | /* func is unused for tail_call, we set it to pass the |
| 3218 | * get_helper_proto check |
| 3219 | */ |
| 3220 | .func = BPF_PTR_POISON, |
| 3221 | .gpl_only = false, |
| 3222 | .ret_type = RET_VOID, |
| 3223 | .arg1_type = ARG_PTR_TO_CTX, |
| 3224 | .arg2_type = ARG_CONST_MAP_PTR, |
| 3225 | .arg3_type = ARG_ANYTHING, |
| 3226 | }; |
| 3227 | |
| 3228 | /* Stub for JITs that only support cBPF. eBPF programs are interpreted. |
| 3229 | * It is encouraged to implement bpf_int_jit_compile() instead, so that |
| 3230 | * eBPF and implicitly also cBPF can get JITed! |
| 3231 | */ |
| 3232 | struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog) |
| 3233 | { |
| 3234 | return prog; |
| 3235 | } |
| 3236 | |
| 3237 | /* Stub for JITs that support eBPF. All cBPF code gets transformed into |
| 3238 | * eBPF by the kernel and is later compiled by bpf_int_jit_compile(). |
| 3239 | */ |
| 3240 | void __weak bpf_jit_compile(struct bpf_prog *prog) |
| 3241 | { |
| 3242 | } |
| 3243 | |
| 3244 | bool __weak bpf_helper_changes_pkt_data(enum bpf_func_id func_id) |
| 3245 | { |
| 3246 | return false; |
| 3247 | } |
| 3248 | |
| 3249 | /* Return TRUE if the JIT backend wants verifier to enable sub-register usage |
| 3250 | * analysis code and wants explicit zero extension inserted by verifier. |
| 3251 | * Otherwise, return FALSE. |
| 3252 | * |
| 3253 | * The verifier inserts an explicit zero extension after BPF_CMPXCHGs even if |
| 3254 | * you don't override this. JITs that don't want these extra insns can detect |
| 3255 | * them using insn_is_zext. |
| 3256 | */ |
| 3257 | bool __weak bpf_jit_needs_zext(void) |
| 3258 | { |
| 3259 | return false; |
| 3260 | } |
| 3261 | |
| 3262 | /* By default, enable the verifier's mitigations against Spectre v1 and v4 for |
| 3263 | * all archs. The value returned must not change at runtime as there is |
| 3264 | * currently no support for reloading programs that were loaded without |
| 3265 | * mitigations. |
| 3266 | */ |
| 3267 | bool __weak bpf_jit_bypass_spec_v1(void) |
| 3268 | { |
| 3269 | return false; |
| 3270 | } |
| 3271 | |
| 3272 | bool __weak bpf_jit_bypass_spec_v4(void) |
| 3273 | { |
| 3274 | return false; |
| 3275 | } |
| 3276 | |
| 3277 | /* Return true if the JIT inlines the call to the helper corresponding to |
| 3278 | * the imm. |
| 3279 | * |
| 3280 | * The verifier will not patch the insn->imm for the call to the helper if |
| 3281 | * this returns true. |
| 3282 | */ |
| 3283 | bool __weak bpf_jit_inlines_helper_call(s32 imm) |
| 3284 | { |
| 3285 | return false; |
| 3286 | } |
| 3287 | |
| 3288 | /* Return TRUE if the JIT backend supports mixing bpf2bpf and tailcalls. */ |
| 3289 | bool __weak bpf_jit_supports_subprog_tailcalls(void) |
| 3290 | { |
| 3291 | return false; |
| 3292 | } |
| 3293 | |
| 3294 | bool __weak bpf_jit_supports_percpu_insn(void) |
| 3295 | { |
| 3296 | return false; |
| 3297 | } |
| 3298 | |
| 3299 | bool __weak bpf_jit_supports_kfunc_call(void) |
| 3300 | { |
| 3301 | return false; |
| 3302 | } |
| 3303 | |
| 3304 | bool __weak bpf_jit_supports_stack_args(void) |
| 3305 | { |
| 3306 | return false; |
| 3307 | } |
| 3308 | |
| 3309 | bool __weak bpf_jit_supports_arena_args(void) |
| 3310 | { |
| 3311 | return false; |
| 3312 | } |
| 3313 | |
| 3314 | bool __weak bpf_jit_supports_far_kfunc_call(void) |
| 3315 | { |
| 3316 | return false; |
| 3317 | } |
| 3318 | |
| 3319 | bool __weak bpf_jit_supports_arena(void) |
| 3320 | { |
| 3321 | return false; |
| 3322 | } |
| 3323 | |
| 3324 | bool __weak bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena) |
| 3325 | { |
| 3326 | return false; |
| 3327 | } |
| 3328 | |
| 3329 | bool __weak bpf_jit_supports_fsession(void) |
| 3330 | { |
| 3331 | return false; |
| 3332 | } |
| 3333 | |
| 3334 | u64 __weak bpf_arch_uaddress_limit(void) |
| 3335 | { |
| 3336 | #if defined(CONFIG_64BIT) && defined(CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE) |
| 3337 | return TASK_SIZE; |
| 3338 | #else |
| 3339 | return 0; |
| 3340 | #endif |
| 3341 | } |
| 3342 | |
| 3343 | /* Return TRUE if the JIT backend satisfies the following two conditions: |
| 3344 | * 1) JIT backend supports atomic_xchg() on pointer-sized words. |
| 3345 | * 2) Under the specific arch, the implementation of xchg() is the same |
| 3346 | * as atomic_xchg() on pointer-sized words. |
| 3347 | */ |
| 3348 | bool __weak bpf_jit_supports_ptr_xchg(void) |
| 3349 | { |
| 3350 | return false; |
| 3351 | } |
| 3352 | |
| 3353 | /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call |
| 3354 | * skb_copy_bits(), so provide a weak definition of it for NET-less config. |
| 3355 | */ |
| 3356 | int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to, |
| 3357 | int len) |
| 3358 | { |
| 3359 | return -EFAULT; |
| 3360 | } |
| 3361 | |
| 3362 | int __weak bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t, |
| 3363 | enum bpf_text_poke_type new_t, void *old_addr, |
| 3364 | void *new_addr) |
| 3365 | { |
| 3366 | return -ENOTSUPP; |
| 3367 | } |
| 3368 | |
| 3369 | void * __weak bpf_arch_text_copy(void *dst, void *src, size_t len) |
| 3370 | { |
| 3371 | return ERR_PTR(-ENOTSUPP); |
| 3372 | } |
| 3373 | |
| 3374 | int __weak bpf_arch_text_invalidate(void *dst, size_t len) |
| 3375 | { |
| 3376 | return -ENOTSUPP; |
| 3377 | } |
| 3378 | |
| 3379 | bool __weak bpf_jit_supports_exceptions(void) |
| 3380 | { |
| 3381 | return false; |
| 3382 | } |
| 3383 | |
| 3384 | bool __weak bpf_jit_supports_private_stack(void) |
| 3385 | { |
| 3386 | return false; |
| 3387 | } |
| 3388 | |
| 3389 | void __weak arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie) |
| 3390 | { |
| 3391 | } |
| 3392 | |
| 3393 | bool __weak bpf_jit_supports_timed_may_goto(void) |
| 3394 | { |
| 3395 | return false; |
| 3396 | } |
| 3397 | |
| 3398 | u64 __weak arch_bpf_timed_may_goto(void) |
| 3399 | { |
| 3400 | return 0; |
| 3401 | } |
| 3402 | |
| 3403 | static noinline void bpf_prog_report_may_goto_violation(void) |
| 3404 | { |
| 3405 | #ifdef CONFIG_BPF_SYSCALL |
| 3406 | struct bpf_stream_stage ss; |
| 3407 | struct bpf_prog *prog; |
| 3408 | |
| 3409 | prog = bpf_prog_find_from_stack(); |
| 3410 | if (!prog) |
| 3411 | return; |
| 3412 | bpf_stream_stage(ss, prog, BPF_STDERR, ({ |
| 3413 | bpf_stream_printk(ss, "ERROR: Timeout detected for may_goto instruction\n"); |
| 3414 | bpf_stream_dump_stack(ss); |
| 3415 | })); |
| 3416 | #endif |
| 3417 | } |
| 3418 | |
| 3419 | u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *p) |
| 3420 | { |
| 3421 | u64 time = ktime_get_mono_fast_ns(); |
| 3422 | |
| 3423 | /* Populate the timestamp for this stack frame, and refresh count. */ |
| 3424 | if (!p->timestamp) { |
| 3425 | p->timestamp = time; |
| 3426 | return BPF_MAX_TIMED_LOOPS; |
| 3427 | } |
| 3428 | /* Check if we've exhausted our time slice, and zero count. */ |
| 3429 | if (unlikely(time - p->timestamp >= (NSEC_PER_SEC / 4))) { |
| 3430 | bpf_prog_report_may_goto_violation(); |
| 3431 | return 0; |
| 3432 | } |
| 3433 | /* Refresh the count for the stack frame. */ |
| 3434 | return BPF_MAX_TIMED_LOOPS; |
| 3435 | } |
| 3436 | |
| 3437 | /* for configs without MMU or 32-bit */ |
| 3438 | __weak const struct bpf_map_ops arena_map_ops; |
| 3439 | __weak u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena) |
| 3440 | { |
| 3441 | return 0; |
| 3442 | } |
| 3443 | __weak u64 bpf_arena_get_kern_vm_start(struct bpf_arena *arena) |
| 3444 | { |
| 3445 | return 0; |
| 3446 | } |
| 3447 | |
| 3448 | #ifdef CONFIG_BPF_SYSCALL |
| 3449 | __weak bool bpf_arena_handle_page_fault(unsigned long addr, bool is_write, |
| 3450 | unsigned long fault_ip) |
| 3451 | { |
| 3452 | return false; |
| 3453 | } |
| 3454 | |
| 3455 | static int __init bpf_global_ma_init(void) |
| 3456 | { |
| 3457 | int ret; |
| 3458 | |
| 3459 | ret = bpf_mem_alloc_init(&bpf_global_ma, 0, false); |
| 3460 | bpf_global_ma_set = !ret; |
| 3461 | return ret; |
| 3462 | } |
| 3463 | late_initcall(bpf_global_ma_init); |
| 3464 | #endif |
| 3465 | |
| 3466 | DEFINE_STATIC_KEY_FALSE(bpf_stats_enabled_key); |
| 3467 | EXPORT_SYMBOL(bpf_stats_enabled_key); |
| 3468 | |
| 3469 | /* All definitions of tracepoints related to BPF. */ |
| 3470 | #define CREATE_TRACE_POINTS |
| 3471 | #include <linux/bpf_trace.h> |
| 3472 | |
| 3473 | EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_exception); |
| 3474 | EXPORT_TRACEPOINT_SYMBOL_GPL(xdp_bulk_tx); |
| 3475 | |
| 3476 | #ifdef CONFIG_BPF_SYSCALL |
| 3477 | |
| 3478 | void bpf_get_linfo_source(struct btf *btf, const struct bpf_line_info *linfo, |
| 3479 | struct bpf_linfo_source *src) |
| 3480 | { |
| 3481 | src->file = kbasename(btf_name_by_offset(btf, linfo->file_name_off)); |
| 3482 | src->line = btf_name_by_offset(btf, linfo->line_off); |
| 3483 | src->file_name_off = linfo->file_name_off; |
| 3484 | src->line_num = BPF_LINE_INFO_LINE_NUM(linfo->line_col); |
| 3485 | src->line_col = BPF_LINE_INFO_LINE_COL(linfo->line_col); |
| 3486 | } |
| 3487 | |
| 3488 | const struct bpf_line_info *bpf_find_linfo(const struct bpf_prog *prog, u32 insn_off) |
| 3489 | { |
| 3490 | const struct bpf_line_info *linfo; |
| 3491 | u32 nr_linfo; |
| 3492 | int l, r, m; |
| 3493 | |
| 3494 | nr_linfo = prog->aux->nr_linfo; |
| 3495 | if (!nr_linfo || insn_off >= prog->len) |
| 3496 | return NULL; |
| 3497 | |
| 3498 | linfo = prog->aux->linfo; |
| 3499 | /* Loop invariant: linfo[l].insn_off <= insns_off. |
| 3500 | * linfo[0].insn_off == 0 which always satisfies above condition. |
| 3501 | * Binary search is searching for rightmost linfo entry that satisfies |
| 3502 | * the above invariant, giving us the desired record that covers given |
| 3503 | * instruction offset. |
| 3504 | */ |
| 3505 | l = 0; |
| 3506 | r = nr_linfo - 1; |
| 3507 | while (l < r) { |
| 3508 | /* (r - l + 1) / 2 means we break a tie to the right, so if: |
| 3509 | * l=1, r=2, linfo[l].insn_off <= insn_off, linfo[r].insn_off > insn_off, |
| 3510 | * then m=2, we see that linfo[m].insn_off > insn_off, and so |
| 3511 | * r becomes 1 and we exit the loop with correct l==1. |
| 3512 | * If the tie was broken to the left, m=1 would end us up in |
| 3513 | * an endless loop where l and m stay at 1 and r stays at 2. |
| 3514 | */ |
| 3515 | m = l + (r - l + 1) / 2; |
| 3516 | if (linfo[m].insn_off <= insn_off) |
| 3517 | l = m; |
| 3518 | else |
| 3519 | r = m - 1; |
| 3520 | } |
| 3521 | |
| 3522 | return &linfo[l]; |
| 3523 | } |
| 3524 | |
| 3525 | int bpf_prog_get_file_line(struct bpf_prog *prog, unsigned long ip, const char **filep, |
| 3526 | const char **linep, int *nump) |
| 3527 | { |
| 3528 | struct bpf_linfo_source src; |
| 3529 | int idx = -1, insn_start, insn_end, len; |
| 3530 | struct bpf_line_info *linfo; |
| 3531 | void **jited_linfo; |
| 3532 | struct btf *btf; |
| 3533 | int nr_linfo; |
| 3534 | |
| 3535 | btf = prog->aux->btf; |
| 3536 | linfo = prog->aux->linfo; |
| 3537 | jited_linfo = prog->aux->jited_linfo; |
| 3538 | |
| 3539 | if (!btf || !linfo || !jited_linfo) |
| 3540 | return -EINVAL; |
| 3541 | len = prog->aux->func ? prog->aux->func[prog->aux->func_idx]->len : prog->len; |
| 3542 | |
| 3543 | linfo = &prog->aux->linfo[prog->aux->linfo_idx]; |
| 3544 | jited_linfo = &prog->aux->jited_linfo[prog->aux->linfo_idx]; |
| 3545 | |
| 3546 | insn_start = linfo[0].insn_off; |
| 3547 | insn_end = insn_start + len; |
| 3548 | nr_linfo = prog->aux->nr_linfo - prog->aux->linfo_idx; |
| 3549 | |
| 3550 | for (int i = 0; i < nr_linfo && |
| 3551 | linfo[i].insn_off >= insn_start && linfo[i].insn_off < insn_end; i++) { |
| 3552 | if (jited_linfo[i] >= (void *)ip) |
| 3553 | break; |
| 3554 | idx = i; |
| 3555 | } |
| 3556 | |
| 3557 | if (idx == -1) |
| 3558 | return -ENOENT; |
| 3559 | |
| 3560 | bpf_get_linfo_source(btf, &linfo[idx], &src); |
| 3561 | while (isspace(*src.line)) |
| 3562 | src.line++; |
| 3563 | if (filep) |
| 3564 | *filep = src.file; |
| 3565 | if (linep) |
| 3566 | *linep = src.line; |
| 3567 | if (nump) |
| 3568 | *nump = src.line_num; |
| 3569 | return 0; |
| 3570 | } |
| 3571 | |
| 3572 | struct walk_stack_ctx { |
| 3573 | struct bpf_prog *prog; |
| 3574 | }; |
| 3575 | |
| 3576 | static bool find_from_stack_cb(void *cookie, u64 ip, u64 sp, u64 bp) |
| 3577 | { |
| 3578 | struct walk_stack_ctx *ctxp = cookie; |
| 3579 | struct bpf_prog *prog; |
| 3580 | |
| 3581 | /* |
| 3582 | * The RCU read lock is held to safely traverse the latch tree, but we |
| 3583 | * don't need its protection when accessing the prog, since it has an |
| 3584 | * active stack frame on the current stack trace, and won't disappear. |
| 3585 | */ |
| 3586 | rcu_read_lock(); |
| 3587 | prog = bpf_prog_ksym_find(ip); |
| 3588 | rcu_read_unlock(); |
| 3589 | if (!prog) |
| 3590 | return true; |
| 3591 | /* Make sure we return the main prog if we found a subprog */ |
| 3592 | ctxp->prog = prog->aux->main_prog_aux->prog; |
| 3593 | return false; |
| 3594 | } |
| 3595 | |
| 3596 | struct bpf_prog *bpf_prog_find_from_stack(void) |
| 3597 | { |
| 3598 | struct walk_stack_ctx ctx = {}; |
| 3599 | |
| 3600 | arch_bpf_stack_walk(find_from_stack_cb, &ctx); |
| 3601 | return ctx.prog; |
| 3602 | } |
| 3603 | |
| 3604 | #endif |