Skip to content

Commit 48464c1

Browse files
mfijalkogregkh
authored andcommitted
bpf: Limit caller's stack depth 256 for subprogs with tailcalls
[ Upstream commit 7f6e431 ] Protect against potential stack overflow that might happen when bpf2bpf calls get combined with tailcalls. Limit the caller's stack depth for such case down to 256 so that the worst case scenario would result in 8k stack size (32 which is tailcall limit * 256 = 8k). Suggested-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 62c4333 commit 48464c1

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

include/linux/bpf_verifier.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ struct bpf_subprog_info {
358358
u32 start; /* insn idx of function entry point */
359359
u32 linfo_idx; /* The idx to the main_prog->aux->linfo */
360360
u16 stack_depth; /* max. stack depth used by this function */
361+
bool has_tail_call;
361362
};
362363

363364
/* single container for all structs

kernel/bpf/verifier.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,10 @@ static int check_subprogs(struct bpf_verifier_env *env)
14891489
for (i = 0; i < insn_cnt; i++) {
14901490
u8 code = insn[i].code;
14911491

1492+
if (code == (BPF_JMP | BPF_CALL) &&
1493+
insn[i].imm == BPF_FUNC_tail_call &&
1494+
insn[i].src_reg != BPF_PSEUDO_CALL)
1495+
subprog[cur_subprog].has_tail_call = true;
14921496
if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32)
14931497
goto next;
14941498
if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL)
@@ -2974,6 +2978,31 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
29742978
int ret_prog[MAX_CALL_FRAMES];
29752979

29762980
process_func:
2981+
/* protect against potential stack overflow that might happen when
2982+
* bpf2bpf calls get combined with tailcalls. Limit the caller's stack
2983+
* depth for such case down to 256 so that the worst case scenario
2984+
* would result in 8k stack size (32 which is tailcall limit * 256 =
2985+
* 8k).
2986+
*
2987+
* To get the idea what might happen, see an example:
2988+
* func1 -> sub rsp, 128
2989+
* subfunc1 -> sub rsp, 256
2990+
* tailcall1 -> add rsp, 256
2991+
* func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320)
2992+
* subfunc2 -> sub rsp, 64
2993+
* subfunc22 -> sub rsp, 128
2994+
* tailcall2 -> add rsp, 128
2995+
* func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416)
2996+
*
2997+
* tailcall will unwind the current stack frame but it will not get rid
2998+
* of caller's stack as shown on the example above.
2999+
*/
3000+
if (idx && subprog[idx].has_tail_call && depth >= 256) {
3001+
verbose(env,
3002+
"tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n",
3003+
depth);
3004+
return -EACCES;
3005+
}
29773006
/* round up to 32-bytes, since this is granularity
29783007
* of interpreter stack size
29793008
*/

0 commit comments

Comments
 (0)