History

bpf: Split subprogram and kfunc collection

add_subprog_and_kfunc() combines two operations with different ordering requirements. Subprogram discovery must precede validation of func_info and line_info, while kfunc descriptors are only needed by the verifier after its initial program setup is complete. Split the helper into add_subprogs() and add_kfuncs() so each operation can be placed according to its actual dependencies. Keep both calls adjacent and in their existing phase for now, and add short comments describing their roles. No functional change is intended for valid programs.

Kumar Kartikeya Dwivedi committed 3 months ago

Commit 41f36ff1 parent 04962af1 file · +32 −9Browse files

Showing the changes to kernel (1 of 1 file). Show all

Unified · Split

kernel/bpf/verifier.c+32−9
@@ -2837,7 +2837,7 @@ int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset) · 2836 unmodified lines
28372837 return 0;
28382838 }
28392839
2840−static int add_subprog_and_kfunc(struct bpf_verifier_env *env)
2840+static int add_subprogs(struct bpf_verifier_env *env)
28412841 {
28422842 struct bpf_subprog_info *subprog = env->subprog_info;
28432843 int i, ret, insn_cnt = env->prog->len, ex_cb_insn;
@@ -2849,8 +2849,7 @@ static int add_subprog_and_kfunc(struct bpf_verifier_env *env) · 5 unmodified lines
28492849 return ret;
28502850
28512851 for (i = 0; i < insn_cnt; i++, insn++) {
2852− if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn) &&
2853− !bpf_pseudo_kfunc_call(insn))
2852+ if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn))
28542853 continue;
28552854
28562855 if (!env->bpf_capable) {
@@ -2858,11 +2857,7 @@ static int add_subprog_and_kfunc(struct bpf_verifier_env *env) · 1 unmodified line
28582857 return -EPERM;
28592858 }
28602859
2861− if (bpf_pseudo_func(insn) || bpf_pseudo_call(insn))
2862− ret = add_subprog(env, i + insn->imm + 1);
2863− else
2864− ret = bpf_add_kfunc_call(env, insn->imm, insn->off);
2865−
2860+ ret = add_subprog(env, i + insn->imm + 1);
28662861 if (ret < 0)
28672862 return ret;
28682863 }
@@ -2900,6 +2895,28 @@ static int add_subprog_and_kfunc(struct bpf_verifier_env *env) · 31 unmodified lines
29002895 return 0;
29012896 }
29022897
2898+static int add_kfuncs(struct bpf_verifier_env *env)
2899+{
2900+ struct bpf_insn *insn = env->prog->insnsi;
2901+ int i, ret, insn_cnt = env->prog->len;
2902+
2903+ for (i = 0; i < insn_cnt; i++, insn++) {
2904+ if (!bpf_pseudo_kfunc_call(insn))
2905+ continue;
2906+
2907+ if (!env->bpf_capable) {
2908+ verbose(env, "loading/calling other bpf or kernel functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n");
2909+ return -EPERM;
2910+ }
2911+
2912+ ret = bpf_add_kfunc_call(env, insn->imm, insn->off);
2913+ if (ret < 0)
2914+ return ret;
2915+ }
2916+
2917+ return 0;
2918+}
2919+
29032920 static int check_subprogs(struct bpf_verifier_env *env)
29042921 {
29052922 int i, subprog_start, subprog_end, off, cur_subprog = 0;
@@ -20140,7 +20157,13 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, · 17234 unmodified lines
2014020157 if (ret < 0)
2014120158 goto skip_full_check;
2014220159
20143− ret = add_subprog_and_kfunc(env);
20160+ /* Discover all subprograms before validating their layout and BTF. */
20161+ ret = add_subprogs(env);
20162+ if (ret < 0)
20163+ goto skip_full_check;
20164+
20165+ /* Collect the kfunc descriptors used during verification. */
20166+ ret = add_kfuncs(env);
2014420167 if (ret < 0)
2014520168 goto skip_full_check;
2014620169