Skip to content

Commit 1ef22b6

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpf: maintain bitmasks across all active frames in __mark_chain_precision
Teach __mark_chain_precision logic to maintain register/stack masks across all active frames when going from child state to parent state. Currently this should be mostly no-op, as precision backtracking usually bails out when encountering subprog entry/exit. It's not very apparent from the diff due to increased indentation, but the logic remains the same, except everything is done on specific `fr` frame index. Calls to bt_clear_reg() and bt_clear_slot() are replaced with frame-specific bt_clear_frame_reg() and bt_clear_frame_slot(), where frame index is passed explicitly, instead of using current frame number. We also adjust logging to emit affected frame number. And we also add better logging of human-readable register and stack slot masks, similar to previous patch. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/r/20230505043317.3629845-6-andrii@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent d9439c2 commit 1ef22b6

2 files changed

Lines changed: 62 additions & 56 deletions

File tree

kernel/bpf/verifier.c

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3736,7 +3736,7 @@ static int __mark_chain_precision(struct bpf_verifier_env *env, int frame, int r
37363736
struct bpf_func_state *func;
37373737
struct bpf_reg_state *reg;
37383738
bool skip_first = true;
3739-
int i, err;
3739+
int i, fr, err;
37403740

37413741
if (!env->bpf_capable)
37423742
return 0;
@@ -3845,56 +3845,62 @@ static int __mark_chain_precision(struct bpf_verifier_env *env, int frame, int r
38453845
if (!st)
38463846
break;
38473847

3848-
func = st->frame[frame];
3849-
bitmap_from_u64(mask, bt_reg_mask(bt));
3850-
for_each_set_bit(i, mask, 32) {
3851-
reg = &func->regs[i];
3852-
if (reg->type != SCALAR_VALUE) {
3853-
bt_clear_reg(bt, i);
3854-
continue;
3848+
for (fr = bt->frame; fr >= 0; fr--) {
3849+
func = st->frame[fr];
3850+
bitmap_from_u64(mask, bt_frame_reg_mask(bt, fr));
3851+
for_each_set_bit(i, mask, 32) {
3852+
reg = &func->regs[i];
3853+
if (reg->type != SCALAR_VALUE) {
3854+
bt_clear_frame_reg(bt, fr, i);
3855+
continue;
3856+
}
3857+
if (reg->precise)
3858+
bt_clear_frame_reg(bt, fr, i);
3859+
else
3860+
reg->precise = true;
38553861
}
3856-
if (reg->precise)
3857-
bt_clear_reg(bt, i);
3858-
else
3859-
reg->precise = true;
3860-
}
38613862

3862-
bitmap_from_u64(mask, bt_stack_mask(bt));
3863-
for_each_set_bit(i, mask, 64) {
3864-
if (i >= func->allocated_stack / BPF_REG_SIZE) {
3865-
/* the sequence of instructions:
3866-
* 2: (bf) r3 = r10
3867-
* 3: (7b) *(u64 *)(r3 -8) = r0
3868-
* 4: (79) r4 = *(u64 *)(r10 -8)
3869-
* doesn't contain jmps. It's backtracked
3870-
* as a single block.
3871-
* During backtracking insn 3 is not recognized as
3872-
* stack access, so at the end of backtracking
3873-
* stack slot fp-8 is still marked in stack_mask.
3874-
* However the parent state may not have accessed
3875-
* fp-8 and it's "unallocated" stack space.
3876-
* In such case fallback to conservative.
3877-
*/
3878-
mark_all_scalars_precise(env, st);
3879-
bt_reset(bt);
3880-
return 0;
3881-
}
3863+
bitmap_from_u64(mask, bt_frame_stack_mask(bt, fr));
3864+
for_each_set_bit(i, mask, 64) {
3865+
if (i >= func->allocated_stack / BPF_REG_SIZE) {
3866+
/* the sequence of instructions:
3867+
* 2: (bf) r3 = r10
3868+
* 3: (7b) *(u64 *)(r3 -8) = r0
3869+
* 4: (79) r4 = *(u64 *)(r10 -8)
3870+
* doesn't contain jmps. It's backtracked
3871+
* as a single block.
3872+
* During backtracking insn 3 is not recognized as
3873+
* stack access, so at the end of backtracking
3874+
* stack slot fp-8 is still marked in stack_mask.
3875+
* However the parent state may not have accessed
3876+
* fp-8 and it's "unallocated" stack space.
3877+
* In such case fallback to conservative.
3878+
*/
3879+
mark_all_scalars_precise(env, st);
3880+
bt_reset(bt);
3881+
return 0;
3882+
}
38823883

3883-
if (!is_spilled_scalar_reg(&func->stack[i])) {
3884-
bt_clear_slot(bt, i);
3885-
continue;
3884+
if (!is_spilled_scalar_reg(&func->stack[i])) {
3885+
bt_clear_frame_slot(bt, fr, i);
3886+
continue;
3887+
}
3888+
reg = &func->stack[i].spilled_ptr;
3889+
if (reg->precise)
3890+
bt_clear_frame_slot(bt, fr, i);
3891+
else
3892+
reg->precise = true;
3893+
}
3894+
if (env->log.level & BPF_LOG_LEVEL2) {
3895+
fmt_reg_mask(env->tmp_str_buf, TMP_STR_BUF_LEN,
3896+
bt_frame_reg_mask(bt, fr));
3897+
verbose(env, "mark_precise: frame%d: parent state regs=%s ",
3898+
fr, env->tmp_str_buf);
3899+
fmt_stack_mask(env->tmp_str_buf, TMP_STR_BUF_LEN,
3900+
bt_frame_stack_mask(bt, fr));
3901+
verbose(env, "stack=%s: ", env->tmp_str_buf);
3902+
print_verifier_state(env, func, true);
38863903
}
3887-
reg = &func->stack[i].spilled_ptr;
3888-
if (reg->precise)
3889-
bt_clear_slot(bt, i);
3890-
else
3891-
reg->precise = true;
3892-
}
3893-
if (env->log.level & BPF_LOG_LEVEL2) {
3894-
verbose(env, "parent %s regs=%x stack=%llx marks:",
3895-
!bt_empty(bt) ? "didn't have" : "already had",
3896-
bt_reg_mask(bt), bt_stack_mask(bt));
3897-
print_verifier_state(env, func, true);
38983904
}
38993905

39003906
if (bt_empty(bt))

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
mark_precise: frame0: regs=r2 stack= before 23\
4545
mark_precise: frame0: regs=r2 stack= before 22\
4646
mark_precise: frame0: regs=r2 stack= before 20\
47-
parent didn't have regs=4 stack=0 marks:\
47+
mark_precise: frame0: parent state regs=r2 stack=:\
4848
mark_precise: frame0: last_idx 19 first_idx 10\
4949
mark_precise: frame0: regs=r2 stack= before 19\
5050
mark_precise: frame0: regs=r9 stack= before 18\
@@ -55,7 +55,7 @@
5555
mark_precise: frame0: regs=r9 stack= before 12\
5656
mark_precise: frame0: regs=r9 stack= before 11\
5757
mark_precise: frame0: regs=r9 stack= before 10\
58-
parent already had regs=0 stack=0 marks:",
58+
mark_precise: frame0: parent state regs= stack=:",
5959
},
6060
{
6161
"precise: test 2",
@@ -104,15 +104,15 @@
104104
mark_precise: frame0: regs=r2 stack= before 24\
105105
mark_precise: frame0: regs=r2 stack= before 23\
106106
mark_precise: frame0: regs=r2 stack= before 22\
107-
parent didn't have regs=4 stack=0 marks:\
107+
mark_precise: frame0: parent state regs=r2 stack=:\
108108
mark_precise: frame0: last_idx 20 first_idx 20\
109109
mark_precise: frame0: regs=r2 stack= before 20\
110-
parent didn't have regs=4 stack=0 marks:\
110+
mark_precise: frame0: parent state regs=r2 stack=:\
111111
mark_precise: frame0: last_idx 19 first_idx 17\
112112
mark_precise: frame0: regs=r2 stack= before 19\
113113
mark_precise: frame0: regs=r9 stack= before 18\
114114
mark_precise: frame0: regs=r8,r9 stack= before 17\
115-
parent already had regs=0 stack=0 marks:",
115+
mark_precise: frame0: parent state regs= stack=:",
116116
},
117117
{
118118
"precise: cross frame pruning",
@@ -153,14 +153,14 @@
153153
.prog_type = BPF_PROG_TYPE_XDP,
154154
.flags = BPF_F_TEST_STATE_FREQ,
155155
.errstr = "mark_precise: frame0: last_idx 5 first_idx 5\
156-
parent didn't have regs=10 stack=0 marks:\
156+
mark_precise: frame0: parent state regs=r4 stack=:\
157157
mark_precise: frame0: last_idx 4 first_idx 2\
158158
mark_precise: frame0: regs=r4 stack= before 4\
159159
mark_precise: frame0: regs=r4 stack= before 3\
160160
mark_precise: frame0: regs= stack=-8 before 2\
161161
mark_precise: frame0: falling back to forcing all scalars precise\
162162
mark_precise: frame0: last_idx 5 first_idx 5\
163-
parent didn't have regs=1 stack=0 marks:",
163+
mark_precise: frame0: parent state regs=r0 stack=:",
164164
.result = VERBOSE_ACCEPT,
165165
.retval = -1,
166166
},
@@ -179,7 +179,7 @@
179179
.prog_type = BPF_PROG_TYPE_XDP,
180180
.flags = BPF_F_TEST_STATE_FREQ,
181181
.errstr = "mark_precise: frame0: last_idx 6 first_idx 6\
182-
parent didn't have regs=10 stack=0 marks:\
182+
mark_precise: frame0: parent state regs=r4 stack=:\
183183
mark_precise: frame0: last_idx 5 first_idx 3\
184184
mark_precise: frame0: regs=r4 stack= before 5\
185185
mark_precise: frame0: regs=r4 stack= before 4\
@@ -188,7 +188,7 @@
188188
force_precise: frame0: forcing r0 to be precise\
189189
force_precise: frame0: forcing r0 to be precise\
190190
mark_precise: frame0: last_idx 6 first_idx 6\
191-
parent didn't have regs=1 stack=0 marks:\
191+
mark_precise: frame0: parent state regs=r0 stack=:\
192192
mark_precise: frame0: last_idx 5 first_idx 3\
193193
mark_precise: frame0: regs=r0 stack= before 5",
194194
.result = VERBOSE_ACCEPT,

0 commit comments

Comments
 (0)