Skip to content

Commit 9242b5f

Browse files
Alexei Starovoitovborkmann
authored andcommitted
bpf: add self-check logic to liveness analysis
Introduce REG_LIVE_DONE to check the liveness propagation and prepare the states for merging. See algorithm description in clean_live_states(). Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Jakub Kicinski <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 19e2dbb commit 9242b5f

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

include/linux/bpf_verifier.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enum bpf_reg_liveness {
3838
REG_LIVE_NONE = 0, /* reg hasn't been read or written this branch */
3939
REG_LIVE_READ, /* reg was read, so we're sensitive to initial value */
4040
REG_LIVE_WRITTEN, /* reg was written first, screening off later reads */
41+
REG_LIVE_DONE = 4, /* liveness won't be updating this register anymore */
4142
};
4243

4344
struct bpf_reg_state {

kernel/bpf/verifier.c

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,14 @@ static char slot_type_char[] = {
397397
static void print_liveness(struct bpf_verifier_env *env,
398398
enum bpf_reg_liveness live)
399399
{
400-
if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN))
400+
if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE))
401401
verbose(env, "_");
402402
if (live & REG_LIVE_READ)
403403
verbose(env, "r");
404404
if (live & REG_LIVE_WRITTEN)
405405
verbose(env, "w");
406+
if (live & REG_LIVE_DONE)
407+
verbose(env, "D");
406408
}
407409

408410
static struct bpf_func_state *func(struct bpf_verifier_env *env,
@@ -1132,6 +1134,12 @@ static int mark_reg_read(struct bpf_verifier_env *env,
11321134
/* if read wasn't screened by an earlier write ... */
11331135
if (writes && state->live & REG_LIVE_WRITTEN)
11341136
break;
1137+
if (parent->live & REG_LIVE_DONE) {
1138+
verbose(env, "verifier BUG type %s var_off %lld off %d\n",
1139+
reg_type_str[parent->type],
1140+
parent->var_off.value, parent->off);
1141+
return -EFAULT;
1142+
}
11351143
/* ... then we depend on parent's value */
11361144
parent->live |= REG_LIVE_READ;
11371145
state = parent;
@@ -5078,6 +5086,102 @@ static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
50785086
return false;
50795087
}
50805088

5089+
static void clean_func_state(struct bpf_verifier_env *env,
5090+
struct bpf_func_state *st)
5091+
{
5092+
enum bpf_reg_liveness live;
5093+
int i, j;
5094+
5095+
for (i = 0; i < BPF_REG_FP; i++) {
5096+
live = st->regs[i].live;
5097+
/* liveness must not touch this register anymore */
5098+
st->regs[i].live |= REG_LIVE_DONE;
5099+
if (!(live & REG_LIVE_READ))
5100+
/* since the register is unused, clear its state
5101+
* to make further comparison simpler
5102+
*/
5103+
__mark_reg_not_init(&st->regs[i]);
5104+
}
5105+
5106+
for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) {
5107+
live = st->stack[i].spilled_ptr.live;
5108+
/* liveness must not touch this stack slot anymore */
5109+
st->stack[i].spilled_ptr.live |= REG_LIVE_DONE;
5110+
if (!(live & REG_LIVE_READ)) {
5111+
__mark_reg_not_init(&st->stack[i].spilled_ptr);
5112+
for (j = 0; j < BPF_REG_SIZE; j++)
5113+
st->stack[i].slot_type[j] = STACK_INVALID;
5114+
}
5115+
}
5116+
}
5117+
5118+
static void clean_verifier_state(struct bpf_verifier_env *env,
5119+
struct bpf_verifier_state *st)
5120+
{
5121+
int i;
5122+
5123+
if (st->frame[0]->regs[0].live & REG_LIVE_DONE)
5124+
/* all regs in this state in all frames were already marked */
5125+
return;
5126+
5127+
for (i = 0; i <= st->curframe; i++)
5128+
clean_func_state(env, st->frame[i]);
5129+
}
5130+
5131+
/* the parentage chains form a tree.
5132+
* the verifier states are added to state lists at given insn and
5133+
* pushed into state stack for future exploration.
5134+
* when the verifier reaches bpf_exit insn some of the verifer states
5135+
* stored in the state lists have their final liveness state already,
5136+
* but a lot of states will get revised from liveness point of view when
5137+
* the verifier explores other branches.
5138+
* Example:
5139+
* 1: r0 = 1
5140+
* 2: if r1 == 100 goto pc+1
5141+
* 3: r0 = 2
5142+
* 4: exit
5143+
* when the verifier reaches exit insn the register r0 in the state list of
5144+
* insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch
5145+
* of insn 2 and goes exploring further. At the insn 4 it will walk the
5146+
* parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ.
5147+
*
5148+
* Since the verifier pushes the branch states as it sees them while exploring
5149+
* the program the condition of walking the branch instruction for the second
5150+
* time means that all states below this branch were already explored and
5151+
* their final liveness markes are already propagated.
5152+
* Hence when the verifier completes the search of state list in is_state_visited()
5153+
* we can call this clean_live_states() function to mark all liveness states
5154+
* as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state'
5155+
* will not be used.
5156+
* This function also clears the registers and stack for states that !READ
5157+
* to simplify state merging.
5158+
*
5159+
* Important note here that walking the same branch instruction in the callee
5160+
* doesn't meant that the states are DONE. The verifier has to compare
5161+
* the callsites
5162+
*/
5163+
static void clean_live_states(struct bpf_verifier_env *env, int insn,
5164+
struct bpf_verifier_state *cur)
5165+
{
5166+
struct bpf_verifier_state_list *sl;
5167+
int i;
5168+
5169+
sl = env->explored_states[insn];
5170+
if (!sl)
5171+
return;
5172+
5173+
while (sl != STATE_LIST_MARK) {
5174+
if (sl->state.curframe != cur->curframe)
5175+
goto next;
5176+
for (i = 0; i <= cur->curframe; i++)
5177+
if (sl->state.frame[i]->callsite != cur->frame[i]->callsite)
5178+
goto next;
5179+
clean_verifier_state(env, &sl->state);
5180+
next:
5181+
sl = sl->next;
5182+
}
5183+
}
5184+
50815185
/* Returns true if (rold safe implies rcur safe) */
50825186
static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
50835187
struct idpair *idmap)
@@ -5396,6 +5500,8 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
53965500
*/
53975501
return 0;
53985502

5503+
clean_live_states(env, insn_idx, cur);
5504+
53995505
while (sl != STATE_LIST_MARK) {
54005506
if (states_equal(env, &sl->state, cur)) {
54015507
/* reached equivalent register/stack state,

0 commit comments

Comments
 (0)