Commit f910cefa authored by Jiong Wang's avatar Jiong Wang Committed by Daniel Borkmann

bpf: unify main prog and subprog

Currently, verifier treat main prog and subprog differently. All subprogs
detected are kept in env->subprog_starts while main prog is not kept there.
Instead, main prog is implicitly defined as the prog start at 0.

There is actually no difference between main prog and subprog, it is better
to unify them, and register all progs detected into env->subprog_starts.

This could also help simplifying some code logic.
Signed-off-by: default avatarJiong Wang <jiong.wang@netronome.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 5234ccf2
...@@ -191,7 +191,7 @@ struct bpf_verifier_env { ...@@ -191,7 +191,7 @@ struct bpf_verifier_env {
bool seen_direct_write; bool seen_direct_write;
struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */ struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */
struct bpf_verifier_log log; struct bpf_verifier_log log;
u32 subprog_starts[BPF_MAX_SUBPROGS]; u32 subprog_starts[BPF_MAX_SUBPROGS + 1];
/* computes the stack depth of each bpf function */ /* computes the stack depth of each bpf function */
u16 subprog_stack_depth[BPF_MAX_SUBPROGS + 1]; u16 subprog_stack_depth[BPF_MAX_SUBPROGS + 1];
u32 subprog_cnt; u32 subprog_cnt;
......
...@@ -768,7 +768,7 @@ static int add_subprog(struct bpf_verifier_env *env, int off) ...@@ -768,7 +768,7 @@ static int add_subprog(struct bpf_verifier_env *env, int off)
ret = find_subprog(env, off); ret = find_subprog(env, off);
if (ret >= 0) if (ret >= 0)
return 0; return 0;
if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { if (env->subprog_cnt > BPF_MAX_SUBPROGS) {
verbose(env, "too many subprograms\n"); verbose(env, "too many subprograms\n");
return -E2BIG; return -E2BIG;
} }
...@@ -784,6 +784,11 @@ static int check_subprogs(struct bpf_verifier_env *env) ...@@ -784,6 +784,11 @@ static int check_subprogs(struct bpf_verifier_env *env)
struct bpf_insn *insn = env->prog->insnsi; struct bpf_insn *insn = env->prog->insnsi;
int insn_cnt = env->prog->len; int insn_cnt = env->prog->len;
/* Add entry function. */
ret = add_subprog(env, 0);
if (ret < 0)
return ret;
/* determine subprog starts. The end is one before the next starts */ /* determine subprog starts. The end is one before the next starts */
for (i = 0; i < insn_cnt; i++) { for (i = 0; i < insn_cnt; i++) {
if (insn[i].code != (BPF_JMP | BPF_CALL)) if (insn[i].code != (BPF_JMP | BPF_CALL))
...@@ -809,10 +814,10 @@ static int check_subprogs(struct bpf_verifier_env *env) ...@@ -809,10 +814,10 @@ static int check_subprogs(struct bpf_verifier_env *env)
/* now check that all jumps are within the same subprog */ /* now check that all jumps are within the same subprog */
subprog_start = 0; subprog_start = 0;
if (env->subprog_cnt == cur_subprog) if (env->subprog_cnt == cur_subprog + 1)
subprog_end = insn_cnt; subprog_end = insn_cnt;
else else
subprog_end = env->subprog_starts[cur_subprog++]; subprog_end = env->subprog_starts[cur_subprog + 1];
for (i = 0; i < insn_cnt; i++) { for (i = 0; i < insn_cnt; i++) {
u8 code = insn[i].code; u8 code = insn[i].code;
...@@ -836,11 +841,13 @@ static int check_subprogs(struct bpf_verifier_env *env) ...@@ -836,11 +841,13 @@ static int check_subprogs(struct bpf_verifier_env *env)
verbose(env, "last insn is not an exit or jmp\n"); verbose(env, "last insn is not an exit or jmp\n");
return -EINVAL; return -EINVAL;
} }
cur_subprog++;
subprog_start = subprog_end; subprog_start = subprog_end;
if (env->subprog_cnt == cur_subprog) if (env->subprog_cnt == cur_subprog + 1)
subprog_end = insn_cnt; subprog_end = insn_cnt;
else else
subprog_end = env->subprog_starts[cur_subprog++]; subprog_end =
env->subprog_starts[cur_subprog + 1];
} }
} }
return 0; return 0;
...@@ -1508,10 +1515,10 @@ static int check_max_stack_depth(struct bpf_verifier_env *env) ...@@ -1508,10 +1515,10 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
return -EACCES; return -EACCES;
} }
continue_func: continue_func:
if (env->subprog_cnt == subprog) if (env->subprog_cnt == subprog + 1)
subprog_end = insn_cnt; subprog_end = insn_cnt;
else else
subprog_end = env->subprog_starts[subprog]; subprog_end = env->subprog_starts[subprog + 1];
for (; i < subprog_end; i++) { for (; i < subprog_end; i++) {
if (insn[i].code != (BPF_JMP | BPF_CALL)) if (insn[i].code != (BPF_JMP | BPF_CALL))
continue; continue;
...@@ -1529,7 +1536,6 @@ static int check_max_stack_depth(struct bpf_verifier_env *env) ...@@ -1529,7 +1536,6 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
i); i);
return -EFAULT; return -EFAULT;
} }
subprog++;
frame++; frame++;
if (frame >= MAX_CALL_FRAMES) { if (frame >= MAX_CALL_FRAMES) {
WARN_ONCE(1, "verifier bug. Call stack is too deep\n"); WARN_ONCE(1, "verifier bug. Call stack is too deep\n");
...@@ -1561,7 +1567,6 @@ static int get_callee_stack_depth(struct bpf_verifier_env *env, ...@@ -1561,7 +1567,6 @@ static int get_callee_stack_depth(struct bpf_verifier_env *env,
start); start);
return -EFAULT; return -EFAULT;
} }
subprog++;
return env->subprog_stack_depth[subprog]; return env->subprog_stack_depth[subprog];
} }
#endif #endif
...@@ -2099,7 +2104,7 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env, ...@@ -2099,7 +2104,7 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
case BPF_FUNC_tail_call: case BPF_FUNC_tail_call:
if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
goto error; goto error;
if (env->subprog_cnt) { if (env->subprog_cnt > 1) {
verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
return -EINVAL; return -EINVAL;
} }
...@@ -2272,7 +2277,7 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, ...@@ -2272,7 +2277,7 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
/* remember the callsite, it will be used by bpf_exit */ /* remember the callsite, it will be used by bpf_exit */
*insn_idx /* callsite */, *insn_idx /* callsite */,
state->curframe + 1 /* frameno within this callchain */, state->curframe + 1 /* frameno within this callchain */,
subprog + 1 /* subprog number within this prog */); subprog /* subprog number within this prog */);
/* copy r1 - r5 args that callee can access */ /* copy r1 - r5 args that callee can access */
for (i = BPF_REG_1; i <= BPF_REG_5; i++) for (i = BPF_REG_1; i <= BPF_REG_5; i++)
...@@ -3889,7 +3894,7 @@ static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) ...@@ -3889,7 +3894,7 @@ static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
return -EINVAL; return -EINVAL;
} }
if (env->subprog_cnt) { if (env->subprog_cnt > 1) {
/* when program has LD_ABS insn JITs and interpreter assume /* when program has LD_ABS insn JITs and interpreter assume
* that r1 == ctx == skb which is not the case for callees * that r1 == ctx == skb which is not the case for callees
* that can have arbitrary arguments. It's problematic * that can have arbitrary arguments. It's problematic
...@@ -4920,11 +4925,11 @@ static int do_check(struct bpf_verifier_env *env) ...@@ -4920,11 +4925,11 @@ static int do_check(struct bpf_verifier_env *env)
verbose(env, "processed %d insns (limit %d), stack depth ", verbose(env, "processed %d insns (limit %d), stack depth ",
insn_processed, BPF_COMPLEXITY_LIMIT_INSNS); insn_processed, BPF_COMPLEXITY_LIMIT_INSNS);
for (i = 0; i < env->subprog_cnt + 1; i++) { for (i = 0; i < env->subprog_cnt; i++) {
u32 depth = env->subprog_stack_depth[i]; u32 depth = env->subprog_stack_depth[i];
verbose(env, "%d", depth); verbose(env, "%d", depth);
if (i + 1 < env->subprog_cnt + 1) if (i + 1 < env->subprog_cnt)
verbose(env, "+"); verbose(env, "+");
} }
verbose(env, "\n"); verbose(env, "\n");
...@@ -5301,7 +5306,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) ...@@ -5301,7 +5306,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
void *old_bpf_func; void *old_bpf_func;
int err = -ENOMEM; int err = -ENOMEM;
if (env->subprog_cnt == 0) if (env->subprog_cnt <= 1)
return 0; return 0;
for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
...@@ -5317,7 +5322,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) ...@@ -5317,7 +5322,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
/* temporarily remember subprog id inside insn instead of /* temporarily remember subprog id inside insn instead of
* aux_data, since next loop will split up all insns into funcs * aux_data, since next loop will split up all insns into funcs
*/ */
insn->off = subprog + 1; insn->off = subprog;
/* remember original imm in case JIT fails and fallback /* remember original imm in case JIT fails and fallback
* to interpreter will be needed * to interpreter will be needed
*/ */
...@@ -5326,16 +5331,16 @@ static int jit_subprogs(struct bpf_verifier_env *env) ...@@ -5326,16 +5331,16 @@ static int jit_subprogs(struct bpf_verifier_env *env)
insn->imm = 1; insn->imm = 1;
} }
func = kzalloc(sizeof(prog) * (env->subprog_cnt + 1), GFP_KERNEL); func = kzalloc(sizeof(prog) * env->subprog_cnt, GFP_KERNEL);
if (!func) if (!func)
return -ENOMEM; return -ENOMEM;
for (i = 0; i <= env->subprog_cnt; i++) { for (i = 0; i < env->subprog_cnt; i++) {
subprog_start = subprog_end; subprog_start = subprog_end;
if (env->subprog_cnt == i) if (env->subprog_cnt == i + 1)
subprog_end = prog->len; subprog_end = prog->len;
else else
subprog_end = env->subprog_starts[i]; subprog_end = env->subprog_starts[i + 1];
len = subprog_end - subprog_start; len = subprog_end - subprog_start;
func[i] = bpf_prog_alloc(bpf_prog_size(len), GFP_USER); func[i] = bpf_prog_alloc(bpf_prog_size(len), GFP_USER);
...@@ -5365,7 +5370,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) ...@@ -5365,7 +5370,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
* now populate all bpf_calls with correct addresses and * now populate all bpf_calls with correct addresses and
* run last pass of JIT * run last pass of JIT
*/ */
for (i = 0; i <= env->subprog_cnt; i++) { for (i = 0; i < env->subprog_cnt; i++) {
insn = func[i]->insnsi; insn = func[i]->insnsi;
for (j = 0; j < func[i]->len; j++, insn++) { for (j = 0; j < func[i]->len; j++, insn++) {
if (insn->code != (BPF_JMP | BPF_CALL) || if (insn->code != (BPF_JMP | BPF_CALL) ||
...@@ -5378,7 +5383,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) ...@@ -5378,7 +5383,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
__bpf_call_base; __bpf_call_base;
} }
} }
for (i = 0; i <= env->subprog_cnt; i++) { for (i = 0; i < env->subprog_cnt; i++) {
old_bpf_func = func[i]->bpf_func; old_bpf_func = func[i]->bpf_func;
tmp = bpf_int_jit_compile(func[i]); tmp = bpf_int_jit_compile(func[i]);
if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
...@@ -5392,7 +5397,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) ...@@ -5392,7 +5397,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
/* finally lock prog and jit images for all functions and /* finally lock prog and jit images for all functions and
* populate kallsysm * populate kallsysm
*/ */
for (i = 0; i <= env->subprog_cnt; i++) { for (i = 0; i < env->subprog_cnt; i++) {
bpf_prog_lock_ro(func[i]); bpf_prog_lock_ro(func[i]);
bpf_prog_kallsyms_add(func[i]); bpf_prog_kallsyms_add(func[i]);
} }
...@@ -5409,7 +5414,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) ...@@ -5409,7 +5414,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
continue; continue;
insn->off = env->insn_aux_data[i].call_imm; insn->off = env->insn_aux_data[i].call_imm;
subprog = find_subprog(env, i + insn->off + 1); subprog = find_subprog(env, i + insn->off + 1);
addr = (unsigned long)func[subprog + 1]->bpf_func; addr = (unsigned long)func[subprog]->bpf_func;
addr &= PAGE_MASK; addr &= PAGE_MASK;
insn->imm = (u64 (*)(u64, u64, u64, u64, u64)) insn->imm = (u64 (*)(u64, u64, u64, u64, u64))
addr - __bpf_call_base; addr - __bpf_call_base;
...@@ -5418,10 +5423,10 @@ static int jit_subprogs(struct bpf_verifier_env *env) ...@@ -5418,10 +5423,10 @@ static int jit_subprogs(struct bpf_verifier_env *env)
prog->jited = 1; prog->jited = 1;
prog->bpf_func = func[0]->bpf_func; prog->bpf_func = func[0]->bpf_func;
prog->aux->func = func; prog->aux->func = func;
prog->aux->func_cnt = env->subprog_cnt + 1; prog->aux->func_cnt = env->subprog_cnt;
return 0; return 0;
out_free: out_free:
for (i = 0; i <= env->subprog_cnt; i++) for (i = 0; i < env->subprog_cnt; i++)
if (func[i]) if (func[i])
bpf_jit_free(func[i]); bpf_jit_free(func[i]);
kfree(func); kfree(func);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment