Skip to content

Commit a9789ef

Browse files
borkmanndavem330
authored andcommitted
bpf: properly reset caller saved regs after helper call and ld_abs/ind
Currently, after performing helper calls, we clear all caller saved registers, that is r0 - r5 and fill r0 depending on struct bpf_func_proto specification. The way we reset these regs can affect pruning decisions in later paths, since we only reset register's imm to 0 and type to NOT_INIT. However, we leave out clearing of other variables such as id, min_value, max_value, etc, which can later on lead to pruning mismatches due to stale data. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 1ad2f58 commit a9789ef

1 file changed

Lines changed: 16 additions & 21 deletions

File tree

‎kernel/bpf/verifier.c‎

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -463,19 +463,22 @@ static const int caller_saved[CALLER_SAVED_REGS] = {
463463
BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
464464
};
465465

466+
static void mark_reg_not_init(struct bpf_reg_state *regs, u32 regno)
467+
{
468+
BUG_ON(regno >= MAX_BPF_REG);
469+
470+
memset(&regs[regno], 0, sizeof(regs[regno]));
471+
regs[regno].type = NOT_INIT;
472+
regs[regno].min_value = BPF_REGISTER_MIN_RANGE;
473+
regs[regno].max_value = BPF_REGISTER_MAX_RANGE;
474+
}
475+
466476
static void init_reg_state(struct bpf_reg_state *regs)
467477
{
468478
int i;
469479

470-
for (i = 0; i < MAX_BPF_REG; i++) {
471-
regs[i].type = NOT_INIT;
472-
regs[i].imm = 0;
473-
regs[i].min_value = BPF_REGISTER_MIN_RANGE;
474-
regs[i].max_value = BPF_REGISTER_MAX_RANGE;
475-
regs[i].min_align = 0;
476-
regs[i].aux_off = 0;
477-
regs[i].aux_off_align = 0;
478-
}
480+
for (i = 0; i < MAX_BPF_REG; i++)
481+
mark_reg_not_init(regs, i);
479482

480483
/* frame pointer */
481484
regs[BPF_REG_FP].type = FRAME_PTR;
@@ -1346,7 +1349,6 @@ static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
13461349
struct bpf_verifier_state *state = &env->cur_state;
13471350
const struct bpf_func_proto *fn = NULL;
13481351
struct bpf_reg_state *regs = state->regs;
1349-
struct bpf_reg_state *reg;
13501352
struct bpf_call_arg_meta meta;
13511353
bool changes_data;
13521354
int i, err;
@@ -1413,11 +1415,8 @@ static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
14131415
}
14141416

14151417
/* reset caller saved regs */
1416-
for (i = 0; i < CALLER_SAVED_REGS; i++) {
1417-
reg = regs + caller_saved[i];
1418-
reg->type = NOT_INIT;
1419-
reg->imm = 0;
1420-
}
1418+
for (i = 0; i < CALLER_SAVED_REGS; i++)
1419+
mark_reg_not_init(regs, caller_saved[i]);
14211420

14221421
/* update return register */
14231422
if (fn->ret_type == RET_INTEGER) {
@@ -2445,7 +2444,6 @@ static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
24452444
{
24462445
struct bpf_reg_state *regs = env->cur_state.regs;
24472446
u8 mode = BPF_MODE(insn->code);
2448-
struct bpf_reg_state *reg;
24492447
int i, err;
24502448

24512449
if (!may_access_skb(env->prog->type)) {
@@ -2478,11 +2476,8 @@ static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
24782476
}
24792477

24802478
/* reset caller saved regs to unreadable */
2481-
for (i = 0; i < CALLER_SAVED_REGS; i++) {
2482-
reg = regs + caller_saved[i];
2483-
reg->type = NOT_INIT;
2484-
reg->imm = 0;
2485-
}
2479+
for (i = 0; i < CALLER_SAVED_REGS; i++)
2480+
mark_reg_not_init(regs, caller_saved[i]);
24862481

24872482
/* mark destination R0 register as readable, since it contains
24882483
* the value fetched from the packet

0 commit comments

Comments
 (0)