History

Merge tag 'bpf-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf

Pull bpf fixes from Alexei Starovoitov: "Most of the diff stat comes from Xu Kuohai's fix to emit ENDBR/BTI, since all JITs had to be touched to move constant blinding out and pass bpf_verifier_env in. - Fix use-after-free in arena_vm_close on fork (Alexei Starovoitov) - Dissociate struct_ops program with map if map_update fails (Amery Hung) - Fix out-of-range and off-by-one bugs in arm64 JIT (Daniel Borkmann) - Fix precedence bug in convert_bpf_ld_abs alignment check (Daniel Borkmann) - Fix arg tracking for imprecise/multi-offset in BPF_ST/STX insns (Eduard Zingerman) - Copy token from main to subprogs to fix missing kallsyms (Eduard Zingerman) - Prevent double close and leak of btf objects in libbpf (Jiri Olsa) - Fix af_unix null-ptr-deref in sockmap (Michal Luczaj) - Fix NULL deref in map_kptr_match_type for scalar regs (Mykyta Yatsenko) - Avoid unnecessary IPIs. Remove redundant bpf_flush_icache() in arm64 and riscv JITs (Puranjay Mohan) - Fix out of bounds access. Validate node_id in arena_alloc_pages() (Puranjay Mohan) - Reject BPF-to-BPF calls and callbacks in arm32 JIT (Puranjay Mohan) - Refactor all JITs to pass bpf_verifier_env to emit ENDBR/BTI for indirect jump targets on x86-64, arm64 JITs (Xu Kuohai) - Allow UTF-8 literals in bpf_bprintf_prepare() (Yihan Ding)" * tag 'bpf-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf: (32 commits) bpf, arm32: Reject BPF-to-BPF calls and callbacks in the JIT bpf: Dissociate struct_ops program with map if map_update fails bpf: Validate node_id in arena_alloc_pages() libbpf: Prevent double close and leak of btf objects selftests/bpf: cover UTF-8 trace_printk output bpf: allow UTF-8 literals in bpf_bprintf_prepare() selftests/bpf: Reject scalar store into kptr slot bpf: Fix NULL deref in map_kptr_match_type for scalar regs bpf: Fix precedence bug in convert_bpf_ld_abs alignment check bpf, arm64: Emit BTI for indirect jump target bpf, x86: Emit ENDBR for indirect jump targets bpf: Add helper to detect indirect jump targets bpf: Pass bpf_verifier_env to JIT bpf: Move constants blinding out of arch-specific JITs bpf, sockmap: Take state lock for af_unix iter bpf, sockmap: Fix af_unix null-ptr-deref in proto update selftests/bpf: Extend bpf_iter_unix to attempt deadlocking bpf, sockmap: Fix af_unix iter deadlock bpf, sockmap: Annotate af_unix sock:: Sk_state data-races selftests/bpf: verify kallsyms entries for token-loaded subprograms ...

Linus Torvalds committed 7 months ago

Commit eb0d6d92 parents 12bffae e1d486446 files · +1173 −707Browse files

Showing the changes to kernel (8 of 46 files). Show all

Unified · Split

8 files
  1. kernel/bpf/arena.c +20 −3
  2. kernel/bpf/bpf_struct_ops.c +4 −3
  3. kernel/bpf/core.c +99 −39
  4. kernel/bpf/fixups.c +138 −25
  5. kernel/bpf/helpers.c +16 −1
  6. kernel/bpf/liveness.c +62 −52
  7. kernel/bpf/syscall.c +0 −4
  8. kernel/bpf/verifier.c +19 −7
kernel/bpf/arena.c+20−3
@@ -341,6 +341,16 @@ static void arena_vm_open(struct vm_area_struct *vma) · 340 unmodified lines
341341 refcount_inc(&vml->mmap_count);
342342 }
343343
344+static int arena_vm_may_split(struct vm_area_struct *vma, unsigned long addr)
345+{
346+ return -EINVAL;
347+}
348+
349+static int arena_vm_mremap(struct vm_area_struct *vma)
350+{
351+ return -EINVAL;
352+}
353+
344354 static void arena_vm_close(struct vm_area_struct *vma)
345355 {
346356 struct bpf_map *map = vma->vm_file->private_data;
@@ -417,6 +427,8 @@ out_unlock_sigsegv: · 70 unmodified lines
417427
418428 static const struct vm_operations_struct arena_vm_ops = {
419429 .open = arena_vm_open,
430+ .may_split = arena_vm_may_split,
431+ .mremap = arena_vm_mremap,
420432 .close = arena_vm_close,
421433 .fault = arena_vm_fault,
422434 };
@@ -486,10 +498,11 @@ static int arena_map_mmap(struct bpf_map *map, struct vm_area_struct *vma) · 63 unmodified lines
486498 arena->user_vm_end = vma->vm_end;
487499 /*
488500 * bpf_map_mmap() checks that it's being mmaped as VM_SHARED and
489− * clears VM_MAYEXEC. Set VM_DONTEXPAND as well to avoid
490− * potential change of user_vm_start.
501+ * clears VM_MAYEXEC. Set VM_DONTEXPAND to avoid potential change
502+ * of user_vm_start. Set VM_DONTCOPY to prevent arena VMA from
503+ * being copied into the child process on fork.
491504 */
492− vm_flags_set(vma, VM_DONTEXPAND);
505+ vm_flags_set(vma, VM_DONTEXPAND | VM_DONTCOPY);
493506 vma->vm_ops = &arena_vm_ops;
494507 return 0;
495508 }
@@ -549,6 +562,10 @@ static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt · 53 unmodified lines
549562 u32 uaddr32;
550563 int ret, i;
551564
565+ if (node_id != NUMA_NO_NODE &&
566+ ((unsigned int)node_id >= nr_node_ids || !node_online(node_id)))
567+ return 0;
568+
552569 if (page_cnt > page_cnt_max)
553570 return 0;
554571
kernel/bpf/bpf_struct_ops.c+4−3
@@ -811,9 +811,6 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, · 810 unmodified lines
811811 goto reset_unlock;
812812 }
813813
814− /* Poison pointer on error instead of return for backward compatibility */
815− bpf_prog_assoc_struct_ops(prog, &st_map->map);
816−
817814 link = kzalloc_obj(*link, GFP_USER);
818815 if (!link) {
819816 bpf_prog_put(prog);
@@ -824,6 +821,9 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, · 4 unmodified lines
824821 &bpf_struct_ops_link_lops, prog, prog->expected_attach_type);
825822 *plink++ = &link->link;
826823
824+ /* Poison pointer on error instead of return for backward compatibility */
825+ bpf_prog_assoc_struct_ops(prog, &st_map->map);
826+
827827 ksym = kzalloc_obj(*ksym, GFP_USER);
828828 if (!ksym) {
829829 err = -ENOMEM;
@@ -906,6 +906,7 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, · 76 unmodified lines
906906 reset_unlock:
907907 bpf_struct_ops_map_free_ksyms(st_map);
908908 bpf_struct_ops_map_free_image(st_map);
909+ bpf_struct_ops_map_dissoc_progs(st_map);
909910 bpf_struct_ops_map_put_progs(st_map);
910911 memset(uvalue, 0, map->value_size);
911912 memset(kvalue, 0, map->value_size);
kernel/bpf/core.c+99−39
@@ -1491,24 +1491,11 @@ void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other) · 1490 unmodified lines
14911491 bpf_prog_clone_free(fp_other);
14921492 }
14931493
1494−static void adjust_insn_arrays(struct bpf_prog *prog, u32 off, u32 len)
1495−{
1496−#ifdef CONFIG_BPF_SYSCALL
1497− struct bpf_map *map;
1498− int i;
1499−
1500− if (len <= 1)
1501− return;
1502−
1503− for (i = 0; i < prog->aux->used_map_cnt; i++) {
1504− map = prog->aux->used_maps[i];
1505− if (map->map_type == BPF_MAP_TYPE_INSN_ARRAY)
1506− bpf_insn_array_adjust(map, off, len);
1507− }
1508−#endif
1509−}
1510−
1511−struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
1494+/*
1495+ * Now this function is used only to blind the main prog and must be invoked only when
1496+ * bpf_prog_need_blind() returns true.
1497+ */
1498+struct bpf_prog *bpf_jit_blind_constants(struct bpf_verifier_env *env, struct bpf_prog *prog)
15121499 {
15131500 struct bpf_insn insn_buff[16], aux[2];
15141501 struct bpf_prog *clone, *tmp;
@@ -1516,13 +1503,17 @@ struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog) · 1 unmodified line
15161503 struct bpf_insn *insn;
15171504 int i, rewritten;
15181505
1519− if (!prog->blinding_requested || prog->blinded)
1520− return prog;
1506+ if (WARN_ON_ONCE(env && env->prog != prog))
1507+ return ERR_PTR(-EINVAL);
15211508
15221509 clone = bpf_prog_clone_create(prog, GFP_USER);
15231510 if (!clone)
15241511 return ERR_PTR(-ENOMEM);
15251512
1513+ /* make sure bpf_patch_insn_data() patches the correct prog */
1514+ if (env)
1515+ env->prog = clone;
1516+
15261517 insn_cnt = clone->len;
15271518 insn = clone->insnsi;
15281519
@@ -1550,21 +1541,28 @@ struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog) · 21 unmodified lines
15501541 if (!rewritten)
15511542 continue;
15521543
1553− tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
1554− if (IS_ERR(tmp)) {
1544+ if (env)
1545+ tmp = bpf_patch_insn_data(env, i, insn_buff, rewritten);
1546+ else
1547+ tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
1548+
1549+ if (IS_ERR_OR_NULL(tmp)) {
1550+ if (env)
1551+ /* restore the original prog */
1552+ env->prog = prog;
15551553 /* Patching may have repointed aux->prog during
15561554 * realloc from the original one, so we need to
15571555 * fix it up here on error.
15581556 */
15591557 bpf_jit_prog_release_other(prog, clone);
1560− return tmp;
1558+ return IS_ERR(tmp) ? tmp : ERR_PTR(-ENOMEM);
15611559 }
15621560
15631561 clone = tmp;
15641562 insn_delta = rewritten - 1;
15651563
1566− /* Instructions arrays must be updated using absolute xlated offsets */
1567− adjust_insn_arrays(clone, prog->aux->subprog_start + i, rewritten);
1564+ if (env)
1565+ env->prog = clone;
15681566
15691567 /* Walk new program and skip insns we just inserted. */
15701568 insn = clone->insnsi + i + insn_delta;
@@ -1575,6 +1573,15 @@ struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog) · 4 unmodified lines
15751573 clone->blinded = 1;
15761574 return clone;
15771575 }
1576+
1577+bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const struct bpf_prog *prog,
1578+ int insn_idx)
1579+{
1580+ if (!env)
1581+ return false;
1582+ insn_idx += prog->aux->subprog_start;
1583+ return env->insn_aux_data[insn_idx].indirect_target;
1584+}
15781585 #endif /* CONFIG_BPF_JIT */
15791586
15801587 /* Base function for offset calculation. Needs to go into .text section,
@@ -2533,18 +2540,55 @@ static bool bpf_prog_select_interpreter(struct bpf_prog *fp) · 952 unmodified lines
25332540 return select_interpreter;
25342541 }
25352542
2536−/**
2537− * bpf_prog_select_runtime - select exec runtime for BPF program
2538− * @fp: bpf_prog populated with BPF program
2539− * @err: pointer to error variable
2540− *
2541− * Try to JIT eBPF program, if JIT is not available, use interpreter.
2542− * The BPF program will be executed via bpf_prog_run() function.
2543− *
2544− * Return: the &fp argument along with &err set to 0 for success or
2545− * a negative errno code on failure
2546− */
2547−struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
2543+static struct bpf_prog *bpf_prog_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)
2544+{
2545+#ifdef CONFIG_BPF_JIT
2546+ struct bpf_prog *orig_prog;
2547+ struct bpf_insn_aux_data *orig_insn_aux;
2548+
2549+ if (!bpf_prog_need_blind(prog))
2550+ return bpf_int_jit_compile(env, prog);
2551+
2552+ if (env) {
2553+ /*
2554+ * If env is not NULL, we are called from the end of bpf_check(), at this
2555+ * point, only insn_aux_data is used after failure, so it should be restored
2556+ * on failure.
2557+ */
2558+ orig_insn_aux = bpf_dup_insn_aux_data(env);
2559+ if (!orig_insn_aux)
2560+ return prog;
2561+ }
2562+
2563+ orig_prog = prog;
2564+ prog = bpf_jit_blind_constants(env, prog);
2565+ /*
2566+ * If blinding was requested and we failed during blinding, we must fall
2567+ * back to the interpreter.
2568+ */
2569+ if (IS_ERR(prog))
2570+ goto out_restore;
2571+
2572+ prog = bpf_int_jit_compile(env, prog);
2573+ if (prog->jited) {
2574+ bpf_jit_prog_release_other(prog, orig_prog);
2575+ if (env)
2576+ vfree(orig_insn_aux);
2577+ return prog;
2578+ }
2579+
2580+ bpf_jit_prog_release_other(orig_prog, prog);
2581+
2582+out_restore:
2583+ prog = orig_prog;
2584+ if (env)
2585+ bpf_restore_insn_aux_data(env, orig_insn_aux);
2586+#endif
2587+ return prog;
2588+}
2589+
2590+struct bpf_prog *__bpf_prog_select_runtime(struct bpf_verifier_env *env, struct bpf_prog *fp,
2591+ int *err)
25482592 {
25492593 /* In case of BPF to BPF calls, verifier did all the prep
25502594 * work with regards to JITing, etc.
@@ -2572,7 +2616,7 @@ struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err) · 21 unmodified lines
25722616 if (*err)
25732617 return fp;
25742618
2575− fp = bpf_int_jit_compile(fp);
2619+ fp = bpf_prog_jit_compile(env, fp);
25762620 bpf_prog_jit_attempt_done(fp);
25772621 if (!fp->jited && jit_needed) {
25782622 *err = -ENOTSUPP;
@@ -2598,6 +2642,22 @@ finalize: · 19 unmodified lines
25982642
25992643 return fp;
26002644 }
2645+
2646+/**
2647+ * bpf_prog_select_runtime - select exec runtime for BPF program
2648+ * @fp: bpf_prog populated with BPF program
2649+ * @err: pointer to error variable
2650+ *
2651+ * Try to JIT eBPF program, if JIT is not available, use interpreter.
2652+ * The BPF program will be executed via bpf_prog_run() function.
2653+ *
2654+ * Return: the &fp argument along with &err set to 0 for success or
2655+ * a negative errno code on failure
2656+ */
2657+struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
2658+{
2659+ return __bpf_prog_select_runtime(NULL, fp, err);
2660+}
26012661 EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
26022662
26032663 static unsigned int __bpf_prog_ret1(const void *ctx,
@@ -3085,7 +3145,7 @@ const struct bpf_func_proto bpf_tail_call_proto = { · 481 unmodified lines
30853145 * It is encouraged to implement bpf_int_jit_compile() instead, so that
30863146 * eBPF and implicitly also cBPF can get JITed!
30873147 */
3088−struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_prog *prog)
3148+struct bpf_prog * __weak bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)
30893149 {
30903150 return prog;
30913151 }
kernel/bpf/fixups.c+138−25
@@ -183,6 +183,18 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env, · 182 unmodified lines
183183 data[i].seen = old_seen;
184184 data[i].zext_dst = insn_has_def32(insn + i);
185185 }
186+
187+ /*
188+ * The indirect_target flag of the original instruction was moved to the last of the
189+ * new instructions by the above memmove and memset, but the indirect jump target is
190+ * actually the first instruction, so move it back. This also matches with the behavior
191+ * of bpf_insn_array_adjust(), which preserves xlated_off to point to the first new
192+ * instruction.
193+ */
194+ if (data[off + cnt - 1].indirect_target) {
195+ data[off].indirect_target = 1;
196+ data[off + cnt - 1].indirect_target = 0;
197+ }
186198 }
187199
188200 static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)
@@ -232,8 +244,8 @@ static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len) · 43 unmodified lines
232244 }
233245 }
234246
235−static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
236− const struct bpf_insn *patch, u32 len)
247+struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
248+ const struct bpf_insn *patch, u32 len)
237249 {
238250 struct bpf_prog *new_prog;
239251 struct bpf_insn_aux_data *new_data = NULL;
@@ -973,7 +985,47 @@ patch_insn_buf: · 733 unmodified lines
973985 return 0;
974986 }
975987
976−int bpf_jit_subprogs(struct bpf_verifier_env *env)
988+static u32 *bpf_dup_subprog_starts(struct bpf_verifier_env *env)
989+{
990+ u32 *starts = NULL;
991+
992+ starts = kvmalloc_objs(u32, env->subprog_cnt, GFP_KERNEL_ACCOUNT);
993+ if (starts) {
994+ for (int i = 0; i < env->subprog_cnt; i++)
995+ starts[i] = env->subprog_info[i].start;
996+ }
997+ return starts;
998+}
999+
1000+static void bpf_restore_subprog_starts(struct bpf_verifier_env *env, u32 *orig_starts)
1001+{
1002+ for (int i = 0; i < env->subprog_cnt; i++)
1003+ env->subprog_info[i].start = orig_starts[i];
1004+ /* restore the start of fake 'exit' subprog as well */
1005+ env->subprog_info[env->subprog_cnt].start = env->prog->len;
1006+}
1007+
1008+struct bpf_insn_aux_data *bpf_dup_insn_aux_data(struct bpf_verifier_env *env)
1009+{
1010+ size_t size;
1011+ void *new_aux;
1012+
1013+ size = array_size(sizeof(struct bpf_insn_aux_data), env->prog->len);
1014+ new_aux = __vmalloc(size, GFP_KERNEL_ACCOUNT);
1015+ if (new_aux)
1016+ memcpy(new_aux, env->insn_aux_data, size);
1017+ return new_aux;
1018+}
1019+
1020+void bpf_restore_insn_aux_data(struct bpf_verifier_env *env,
1021+ struct bpf_insn_aux_data *orig_insn_aux)
1022+{
1023+ /* the expanded elements are zero-filled, so no special handling is required */
1024+ vfree(env->insn_aux_data);
1025+ env->insn_aux_data = orig_insn_aux;
1026+}
1027+
1028+static int jit_subprogs(struct bpf_verifier_env *env)
9771029 {
9781030 struct bpf_prog *prog = env->prog, **func, *tmp;
9791031 int i, j, subprog_start, subprog_end = 0, len, subprog;
@@ -981,10 +1033,6 @@ int bpf_jit_subprogs(struct bpf_verifier_env *env) · 1 unmodified line
9811033 struct bpf_insn *insn;
9821034 void *old_bpf_func;
9831035 int err, num_exentries;
984− int old_len, subprog_start_adjustment = 0;
985−
986− if (env->subprog_cnt <= 1)
987− return 0;
9881036
9891037 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
9901038 if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn))
@@ -1053,10 +1101,11 @@ int bpf_jit_subprogs(struct bpf_verifier_env *env) · 62 unmodified lines
10531101 goto out_free;
10541102 func[i]->is_func = 1;
10551103 func[i]->sleepable = prog->sleepable;
1104+ func[i]->blinded = prog->blinded;
10561105 func[i]->aux->func_idx = i;
10571106 /* Below members will be freed only at prog->aux */
10581107 func[i]->aux->btf = prog->aux->btf;
1059− func[i]->aux->subprog_start = subprog_start + subprog_start_adjustment;
1108+ func[i]->aux->subprog_start = subprog_start;
10601109 func[i]->aux->func_info = prog->aux->func_info;
10611110 func[i]->aux->func_info_cnt = prog->aux->func_info_cnt;
10621111 func[i]->aux->poke_tab = prog->aux->poke_tab;
@@ -1110,17 +1159,10 @@ int bpf_jit_subprogs(struct bpf_verifier_env *env) · 47 unmodified lines
11101159 func[i]->aux->exception_cb = env->subprog_info[i].is_exception_cb;
11111160 func[i]->aux->changes_pkt_data = env->subprog_info[i].changes_pkt_data;
11121161 func[i]->aux->might_sleep = env->subprog_info[i].might_sleep;
1162+ func[i]->aux->token = prog->aux->token;
11131163 if (!i)
11141164 func[i]->aux->exception_boundary = env->seen_exception;
1115−
1116− /*
1117− * To properly pass the absolute subprog start to jit
1118− * all instruction adjustments should be accumulated
1119− */
1120− old_len = func[i]->len;
1121− func[i] = bpf_int_jit_compile(func[i]);
1122− subprog_start_adjustment += func[i]->len - old_len;
1123−
1165+ func[i] = bpf_int_jit_compile(env, func[i]);
11241166 if (!func[i]->jited) {
11251167 err = -ENOTSUPP;
11261168 goto out_free;
@@ -1164,7 +1206,7 @@ int bpf_jit_subprogs(struct bpf_verifier_env *env) · 37 unmodified lines
11641206 }
11651207 for (i = 0; i < env->subprog_cnt; i++) {
11661208 old_bpf_func = func[i]->bpf_func;
1167− tmp = bpf_int_jit_compile(func[i]);
1209+ tmp = bpf_int_jit_compile(env, func[i]);
11681210 if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
11691211 verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
11701212 err = -ENOTSUPP;
@@ -1246,16 +1288,87 @@ out_free: · 75 unmodified lines
12461288 }
12471289 kfree(func);
12481290 out_undo_insn:
1291+ bpf_prog_jit_attempt_done(prog);
1292+ return err;
1293+}
1294+
1295+int bpf_jit_subprogs(struct bpf_verifier_env *env)
1296+{
1297+ int err, i;
1298+ bool blinded = false;
1299+ struct bpf_insn *insn;
1300+ struct bpf_prog *prog, *orig_prog;
1301+ struct bpf_insn_aux_data *orig_insn_aux;
1302+ u32 *orig_subprog_starts;
1303+
1304+ if (env->subprog_cnt <= 1)
1305+ return 0;
1306+
1307+ prog = orig_prog = env->prog;
1308+ if (bpf_prog_need_blind(prog)) {
1309+ orig_insn_aux = bpf_dup_insn_aux_data(env);
1310+ if (!orig_insn_aux) {
1311+ err = -ENOMEM;
1312+ goto out_cleanup;
1313+ }
1314+ orig_subprog_starts = bpf_dup_subprog_starts(env);
1315+ if (!orig_subprog_starts) {
1316+ vfree(orig_insn_aux);
1317+ err = -ENOMEM;
1318+ goto out_cleanup;
1319+ }
1320+ prog = bpf_jit_blind_constants(env, prog);
1321+ if (IS_ERR(prog)) {
1322+ err = -ENOMEM;
1323+ prog = orig_prog;
1324+ goto out_restore;
1325+ }
1326+ blinded = true;
1327+ }
1328+
1329+ err = jit_subprogs(env);
1330+ if (err)
1331+ goto out_jit_err;
1332+
1333+ if (blinded) {
1334+ bpf_jit_prog_release_other(prog, orig_prog);
1335+ kvfree(orig_subprog_starts);
1336+ vfree(orig_insn_aux);
1337+ }
1338+
1339+ return 0;
1340+
1341+out_jit_err:
1342+ if (blinded) {
1343+ bpf_jit_prog_release_other(orig_prog, prog);
1344+ /* roll back to the clean original prog */
1345+ prog = env->prog = orig_prog;
1346+ goto out_restore;
1347+ } else {
1348+ if (err != -EFAULT) {
1349+ /*
1350+ * We will fall back to interpreter mode when err is not -EFAULT, before
1351+ * that, insn->off and insn->imm should be restored to their original
1352+ * values since they were modified by jit_subprogs.
1353+ */
1354+ for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
1355+ if (!bpf_pseudo_call(insn))
1356+ continue;
1357+ insn->off = 0;
1358+ insn->imm = env->insn_aux_data[i].call_imm;
1359+ }
1360+ }
1361+ goto out_cleanup;
1362+ }
1363+
1364+out_restore:
1365+ bpf_restore_subprog_starts(env, orig_subprog_starts);
1366+ bpf_restore_insn_aux_data(env, orig_insn_aux);
1367+ kvfree(orig_subprog_starts);
1368+out_cleanup:
12491369 /* cleanup main prog to be interpreted */
12501370 prog->jit_requested = 0;
12511371 prog->blinding_requested = 0;
1252− for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
1253− if (!bpf_pseudo_call(insn))
1254− continue;
1255− insn->off = 0;
1256− insn->imm = env->insn_aux_data[i].call_imm;
1257− }
1258− bpf_prog_jit_attempt_done(prog);
12591372 return err;
12601373 }
12611374
kernel/bpf/helpers.c+16−1
@@ -845,7 +845,13 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args, · 844 unmodified lines
845845 data->buf = buffers->buf;
846846
847847 for (i = 0; i < fmt_size; i++) {
848− if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) {
848+ unsigned char c = fmt[i];
849+
850+ /*
851+ * Permit bytes >= 0x80 in plain text so UTF-8 literals can pass
852+ * through unchanged, while still rejecting ASCII control bytes.
853+ */
854+ if (isascii(c) && !isprint(c) && !isspace(c)) {
849855 err = -EINVAL;
850856 goto out;
851857 }
@@ -867,6 +873,15 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args, · 15 unmodified lines
867873 * always access fmt[i + 1], in the worst case it will be a 0
868874 */
869875 i++;
876+ c = fmt[i];
877+ /*
878+ * The format parser below only understands ASCII conversion
879+ * specifiers and modifiers, so reject non-ASCII after '%'.
880+ */
881+ if (!isascii(c)) {
882+ err = -EINVAL;
883+ goto out;
884+ }
870885
871886 /* skip optional "[0 +-][num]" width formatting field */
872887 while (fmt[i] == '0' || fmt[i] == '+' || fmt[i] == '-' ||
kernel/bpf/liveness.c+62−52
@@ -574,7 +574,7 @@ static int print_instances(struct bpf_verifier_env *env) · 573 unmodified lines
574574 *
575575 * precise {frame=N, off=V} -- known absolute frame index and byte offset
576576 * |
577− * offset-imprecise {frame=N, off=OFF_IMPRECISE}
577+ * offset-imprecise {frame=N, cnt=0}
578578 * | -- known frame identity, unknown offset
579579 * fully-imprecise {frame=ARG_IMPRECISE, mask=bitmask}
580580 * -- unknown frame identity; .mask is a
@@ -607,8 +607,6 @@ enum arg_track_state { · 26 unmodified lines
607607 ARG_IMPRECISE = -3, /* lost identity; .mask is arg bitmask */
608608 };
609609
610−#define OFF_IMPRECISE S16_MIN /* arg identity known but offset unknown */
611−
612610 /* Track callee stack slots fp-8 through fp-512 (64 slots of 8 bytes each) */
613611 #define MAX_ARG_SPILL_SLOTS 64
614612
@@ -622,28 +620,6 @@ static bool arg_is_fp(const struct arg_track *at) · 7 unmodified lines
622620 return at->frame >= 0 || at->frame == ARG_IMPRECISE;
623621 }
624622
625−/*
626− * Clear all tracked callee stack slots overlapping the byte range
627− * [off, off+sz-1] where off is a negative FP-relative offset.
628− */
629−static void clear_overlapping_stack_slots(struct arg_track *at_stack, s16 off, u32 sz)
630−{
631− struct arg_track none = { .frame = ARG_NONE };
632−
633− if (off == OFF_IMPRECISE) {
634− for (int i = 0; i < MAX_ARG_SPILL_SLOTS; i++)
635− at_stack[i] = none;
636− return;
637− }
638− for (int i = 0; i < MAX_ARG_SPILL_SLOTS; i++) {
639− int slot_start = -((i + 1) * 8);
640− int slot_end = slot_start + 8;
641−
642− if (slot_start < off + (int)sz && slot_end > off)
643− at_stack[i] = none;
644− }
645−}
646−
647623 static void verbose_arg_track(struct bpf_verifier_env *env, struct arg_track *at)
648624 {
649625 int i;
@@ -863,16 +839,13 @@ static void arg_track_alu64(struct arg_track *dst, const struct arg_track *src) · 213 unmodified lines
863839 *dst = arg_join_imprecise(*dst, *src);
864840 }
865841
866−static s16 arg_add(s16 off, s64 delta)
842+static bool arg_add(s16 off, s64 delta, s16 *out)
867843 {
868− s64 res;
869−
870− if (off == OFF_IMPRECISE)
871− return OFF_IMPRECISE;
872− res = (s64)off + delta;
873− if (res < S16_MIN + 1 || res > S16_MAX)
874− return OFF_IMPRECISE;
875− return res;
844+ s16 d = delta;
845+
846+ if (d != delta)
847+ return true;
848+ return check_add_overflow(off, d, out);
876849 }
877850
878851 static void arg_padd(struct arg_track *at, s64 delta)
@@ -882,9 +855,9 @@ static void arg_padd(struct arg_track *at, s64 delta) · 3 unmodified lines
882855 if (at->off_cnt == 0)
883856 return;
884857 for (i = 0; i < at->off_cnt; i++) {
885− s16 new_off = arg_add(at->off[i], delta);
858+ s16 new_off;
886859
887− if (new_off == OFF_IMPRECISE) {
860+ if (arg_add(at->off[i], delta, &new_off)) {
888861 at->off_cnt = 0;
889862 return;
890863 }
@@ -899,8 +872,6 @@ static void arg_padd(struct arg_track *at, s64 delta) · 8 unmodified lines
899872 */
900873 static int fp_off_to_slot(s16 off)
901874 {
902− if (off == OFF_IMPRECISE)
903− return -1;
904875 if (off >= 0 || off < -(int)(MAX_ARG_SPILL_SLOTS * 8))
905876 return -1;
906877 if (off % 8)
@@ -930,9 +901,11 @@ static struct arg_track fill_from_stack(struct bpf_insn *insn, · 23 unmodified lines
930901 return imp;
931902
932903 for (i = 0; i < cnt; i++) {
933− s16 fp_off = arg_add(at_out[reg].off[i], insn->off);
934− int slot = fp_off_to_slot(fp_off);
904+ s16 fp_off, slot;
935905
906+ if (arg_add(at_out[reg].off[i], insn->off, &fp_off))
907+ return imp;
908+ slot = fp_off_to_slot(fp_off);
936909 if (slot < 0)
937910 return imp;
938911 result = __arg_track_join(result, at_stack_out[slot]);
@@ -968,9 +941,12 @@ static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out, · 29 unmodified lines
968941 return;
969942 }
970943 for (i = 0; i < cnt; i++) {
971− s16 fp_off = arg_add(at_out[reg].off[i], insn->off);
972− int slot = fp_off_to_slot(fp_off);
944+ s16 fp_off;
945+ int slot;
973946
947+ if (arg_add(at_out[reg].off[i], insn->off, &fp_off))
948+ continue;
949+ slot = fp_off_to_slot(fp_off);
974950 if (slot < 0)
975951 continue;
976952 if (cnt == 1)
@@ -980,6 +956,32 @@ static void spill_to_stack(struct bpf_insn *insn, struct arg_track *at_out, · 3 unmodified lines
980956 }
981957 }
982958
959+/*
960+ * Clear all tracked callee stack slots overlapping the byte range
961+ * [off, off+sz-1] where off is a negative FP-relative offset.
962+ */
963+static void clear_overlapping_stack_slots(struct arg_track *at_stack, s16 off, u32 sz, int cnt)
964+{
965+ struct arg_track none = { .frame = ARG_NONE };
966+
967+ if (cnt == 0) {
968+ for (int i = 0; i < MAX_ARG_SPILL_SLOTS; i++)
969+ at_stack[i] = __arg_track_join(at_stack[i], none);
970+ return;
971+ }
972+ for (int i = 0; i < MAX_ARG_SPILL_SLOTS; i++) {
973+ int slot_start = -((i + 1) * 8);
974+ int slot_end = slot_start + 8;
975+
976+ if (slot_start < off + (int)sz && slot_end > off) {
977+ if (cnt == 1)
978+ at_stack[i] = none;
979+ else
980+ at_stack[i] = __arg_track_join(at_stack[i], none);
981+ }
982+ }
983+}
984+
983985 /*
984986 * Clear stack slots overlapping all possible FP offsets in @reg.
985987 */
@@ -990,18 +992,22 @@ static void clear_stack_for_all_offs(struct bpf_insn *insn, · 4 unmodified lines
990992 int cnt, i;
991993
992994 if (reg == BPF_REG_FP) {
993− clear_overlapping_stack_slots(at_stack_out, insn->off, sz);
995+ clear_overlapping_stack_slots(at_stack_out, insn->off, sz, 1);
994996 return;
995997 }
996998 cnt = at_out[reg].off_cnt;
997999 if (cnt == 0) {
998− clear_overlapping_stack_slots(at_stack_out, OFF_IMPRECISE, sz);
1000+ clear_overlapping_stack_slots(at_stack_out, 0, sz, cnt);
9991001 return;
10001002 }
10011003 for (i = 0; i < cnt; i++) {
1002− s16 fp_off = arg_add(at_out[reg].off[i], insn->off);
1004+ s16 fp_off;
10031005
1004− clear_overlapping_stack_slots(at_stack_out, fp_off, sz);
1006+ if (arg_add(at_out[reg].off[i], insn->off, &fp_off)) {
1007+ clear_overlapping_stack_slots(at_stack_out, 0, sz, 0);
1008+ break;
1009+ }
1010+ clear_overlapping_stack_slots(at_stack_out, fp_off, sz, cnt);
10051011 }
10061012 }
10071013
@@ -1042,6 +1048,12 @@ static void arg_track_log(struct bpf_verifier_env *env, struct bpf_insn *insn, i · 34 unmodified lines
10421048 verbose(env, "\n");
10431049 }
10441050
1051+static bool can_be_local_fp(int depth, int regno, struct arg_track *at)
1052+{
1053+ return regno == BPF_REG_FP || at->frame == depth ||
1054+ (at->frame == ARG_IMPRECISE && (at->mask & BIT(depth)));
1055+}
1056+
10451057 /*
10461058 * Pure dataflow transfer function for arg_track state.
10471059 * Updates at_out[] based on how the instruction modifies registers.
@@ -1111,8 +1123,7 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn, · 63 unmodified lines
11111123 at_out[r] = none;
11121124 } else if (class == BPF_LDX) {
11131125 u32 sz = bpf_size_to_bytes(BPF_SIZE(insn->code));
1114− bool src_is_local_fp = insn->src_reg == BPF_REG_FP || src->frame == depth ||
1115− (src->frame == ARG_IMPRECISE && (src->mask & BIT(depth)));
1126+ bool src_is_local_fp = can_be_local_fp(depth, insn->src_reg, src);
11161127
11171128 /*
11181129 * Reload from callee stack: if src is current-frame FP-derived
@@ -1147,7 +1158,7 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn, · 28 unmodified lines
11471158 bool dst_is_local_fp;
11481159
11491160 /* Track spills to current-frame FP-derived callee stack */
1150− dst_is_local_fp = insn->dst_reg == BPF_REG_FP || dst->frame == depth;
1161+ dst_is_local_fp = can_be_local_fp(depth, insn->dst_reg, dst);
11511162 if (dst_is_local_fp && BPF_MODE(insn->code) == BPF_MEM)
11521163 spill_to_stack(insn, at_out, insn->dst_reg,
11531164 at_stack_out, src, sz);
@@ -1166,7 +1177,7 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn, · 12 unmodified lines
11661177 }
11671178 } else if (class == BPF_ST && BPF_MODE(insn->code) == BPF_MEM) {
11681179 u32 sz = bpf_size_to_bytes(BPF_SIZE(insn->code));
1169− bool dst_is_local_fp = insn->dst_reg == BPF_REG_FP || dst->frame == depth;
1180+ bool dst_is_local_fp = can_be_local_fp(depth, insn->dst_reg, dst);
11701181
11711182 /* BPF_ST to FP-derived dst: clear overlapping stack slots */
11721183 if (dst_is_local_fp)
@@ -1316,8 +1327,7 @@ static int record_load_store_access(struct bpf_verifier_env *env, · 143 unmodified lines
13161327 resolved.off_cnt = ptr->off_cnt;
13171328 resolved.frame = ptr->frame;
13181329 for (oi = 0; oi < ptr->off_cnt; oi++) {
1319− resolved.off[oi] = arg_add(ptr->off[oi], insn->off);
1320− if (resolved.off[oi] == OFF_IMPRECISE) {
1330+ if (arg_add(ptr->off[oi], insn->off, &resolved.off[oi])) {
13211331 resolved.off_cnt = 0;
13221332 break;
13231333 }
kernel/bpf/syscall.c+0−4
@@ -3083,10 +3083,6 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) · 3082 unmodified lines
30833083 if (err < 0)
30843084 goto free_used_maps;
30853085
3086− prog = bpf_prog_select_runtime(prog, &err);
3087− if (err < 0)
3088− goto free_used_maps;
3089−
30903086 err = bpf_prog_mark_insn_arrays_ready(prog);
30913087 if (err < 0)
30923088 goto free_used_maps;
kernel/bpf/verifier.c+19−7
@@ -3497,6 +3497,11 @@ static int insn_stack_access_flags(int frameno, int spi) · 3496 unmodified lines
34973497 return INSN_F_STACK_ACCESS | (spi << INSN_F_SPI_SHIFT) | frameno;
34983498 }
34993499
3500+static void mark_indirect_target(struct bpf_verifier_env *env, int idx)
3501+{
3502+ env->insn_aux_data[idx].indirect_target = true;
3503+}
3504+
35003505 #define LR_FRAMENO_BITS 3
35013506 #define LR_SPI_BITS 6
35023507 #define LR_ENTRY_BITS (LR_SPI_BITS + LR_FRAMENO_BITS + 1)
@@ -4544,6 +4549,9 @@ static int map_kptr_match_type(struct bpf_verifier_env *env, · 1041 unmodified lines
45444549 int perm_flags;
45454550 const char *reg_name = "";
45464551
4552+ if (base_type(reg->type) != PTR_TO_BTF_ID)
4553+ goto bad_type;
4554+
45474555 if (btf_is_kernel(reg->btf)) {
45484556 perm_flags = PTR_MAYBE_NULL | PTR_TRUSTED | MEM_RCU;
45494557
@@ -4556,7 +4564,7 @@ static int map_kptr_match_type(struct bpf_verifier_env *env, · 6 unmodified lines
45564564 perm_flags |= MEM_PERCPU;
45574565 }
45584566
4559− if (base_type(reg->type) != PTR_TO_BTF_ID || (type_flag(reg->type) & ~perm_flags))
4567+ if (type_flag(reg->type) & ~perm_flags)
45604568 goto bad_type;
45614569
45624570 /* We need to verify reg->type and reg->btf, before accessing reg->btf */
@@ -17545,12 +17553,14 @@ static int check_indirect_jump(struct bpf_verifier_env *env, struct bpf_insn *in · 12982 unmodified lines
1754517553 }
1754617554
1754717555 for (i = 0; i < n - 1; i++) {
17556+ mark_indirect_target(env, env->gotox_tmp_buf->items[i]);
1754817557 other_branch = push_stack(env, env->gotox_tmp_buf->items[i],
1754917558 env->insn_idx, env->cur_state->speculative);
1755017559 if (IS_ERR(other_branch))
1755117560 return PTR_ERR(other_branch);
1755217561 }
1755317562 env->insn_idx = env->gotox_tmp_buf->items[n-1];
17563+ mark_indirect_target(env, env->insn_idx);
1755417564 return INSN_IDX_UPDATED;
1755517565 }
1755617566
@@ -20155,6 +20165,14 @@ skip_full_check: · 2598 unmodified lines
2015520165
2015620166 adjust_btf_func(env);
2015720167
20168+ /* extension progs temporarily inherit the attach_type of their targets
20169+ for verification purposes, so set it back to zero before returning
20170+ */
20171+ if (env->prog->type == BPF_PROG_TYPE_EXT)
20172+ env->prog->expected_attach_type = 0;
20173+
20174+ env->prog = __bpf_prog_select_runtime(env, env->prog, &ret);
20175+
2015820176 err_release_maps:
2015920177 if (ret)
2016020178 release_insn_arrays(env);
@@ -20166,12 +20184,6 @@ err_release_maps: · 5 unmodified lines
2016620184 if (!env->prog->aux->used_btfs)
2016720185 release_btfs(env);
2016820186
20169− /* extension progs temporarily inherit the attach_type of their targets
20170− for verification purposes, so set it back to zero before returning
20171− */
20172− if (env->prog->type == BPF_PROG_TYPE_EXT)
20173− env->prog->expected_attach_type = 0;
20174−
2017520187 *prog = env->prog;
2017620188
2017720189 module_put(env->attach_btf_mod);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t