Skip to content

Commit 62a5863

Browse files
anakryikoKernel Patches Daemon
authored andcommitted
bpf: fix mark_all_scalars_precise use in mark_chain_precision
When precision backtracking bails out due to some unsupported sequence of instructions (e.g., stack access through register other than r10), we need to mark all SCALAR registers as precise to be safe. Currently, though, we mark SCALARs precise only starting from the state we detected unsupported condition, which could be one of the parent states of the actual current state. This will leave some registers potentially not marked as precise, even though they should. So make sure we start marking scalars as precise from current state (env->cur_state). Further, we don't currently detect a situation when we end up with some stack slots marked as needing precision, but we ran out of available states to find the instructions that populate those stack slots. This is akin the `i >= func->allocated_stack / BPF_REG_SIZE` check and should be handled similarly by falling back to marking all SCALARs precise. Add this check when we run out of states. Signed-off-by: Andrii Nakryiko <[email protected]>
1 parent 0096d3f commit 62a5863

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

kernel/bpf/verifier.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3823,7 +3823,7 @@ static int __mark_chain_precision(struct bpf_verifier_env *env, int frame, int r
38233823
err = backtrack_insn(env, i, bt);
38243824
}
38253825
if (err == -ENOTSUPP) {
3826-
mark_all_scalars_precise(env, st);
3826+
mark_all_scalars_precise(env, env->cur_state);
38273827
bt_reset(bt);
38283828
return 0;
38293829
} else if (err) {
@@ -3885,7 +3885,7 @@ static int __mark_chain_precision(struct bpf_verifier_env *env, int frame, int r
38853885
* fp-8 and it's "unallocated" stack space.
38863886
* In such case fallback to conservative.
38873887
*/
3888-
mark_all_scalars_precise(env, st);
3888+
mark_all_scalars_precise(env, env->cur_state);
38893889
bt_reset(bt);
38903890
return 0;
38913891
}
@@ -3914,11 +3914,21 @@ static int __mark_chain_precision(struct bpf_verifier_env *env, int frame, int r
39143914
}
39153915

39163916
if (bt_bitcnt(bt) == 0)
3917-
break;
3917+
return 0;
39183918

39193919
last_idx = st->last_insn_idx;
39203920
first_idx = st->first_insn_idx;
39213921
}
3922+
3923+
/* if we still have requested precise regs or slots, we missed
3924+
* something (e.g., stack access through non-r10 register), so
3925+
* fallback to marking all precise
3926+
*/
3927+
if (bt_bitcnt(bt) != 0) {
3928+
mark_all_scalars_precise(env, env->cur_state);
3929+
bt_reset(bt);
3930+
}
3931+
39223932
return 0;
39233933
}
39243934

tools/testing/selftests/bpf/verifier/precise.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@
159159
mark_precise: frame0: regs(0x10)=r4 stack(0x0)= before 3\
160160
mark_precise: frame0: regs(0x0)= stack(0x1)=-8 before 2\
161161
mark_precise: frame0: falling back to forcing all scalars precise\
162+
force_precise: frame0: forcing r0 to be precise\
162163
mark_precise: frame0: last_idx 5 first_idx 5\
163-
mark_precise: frame0: parent state regs(0x1)=r0 stack(0x0)=:",
164+
mark_precise: frame0: parent state regs(0x0)= stack(0x0)=:",
164165
.result = VERBOSE_ACCEPT,
165166
.retval = -1,
166167
},
@@ -187,10 +188,10 @@
187188
mark_precise: frame0: falling back to forcing all scalars precise\
188189
force_precise: frame0: forcing r0 to be precise\
189190
force_precise: frame0: forcing r0 to be precise\
191+
force_precise: frame0: forcing r0 to be precise\
192+
force_precise: frame0: forcing r0 to be precise\
190193
mark_precise: frame0: last_idx 6 first_idx 6\
191-
mark_precise: frame0: parent state regs(0x1)=r0 stack(0x0)=:\
192-
mark_precise: frame0: last_idx 5 first_idx 3\
193-
mark_precise: frame0: regs(0x1)=r0 stack(0x0)= before 5",
194+
mark_precise: frame0: parent state regs(0x0)= stack(0x0)=:",
194195
.result = VERBOSE_ACCEPT,
195196
.retval = -1,
196197
},

0 commit comments

Comments
 (0)