Skip to content

Commit b9fa9bc

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
selftests/bpf: Verify copy_register_state() preserves parent/live fields
A testcase to check that verifier.c:copy_register_state() preserves register parentage chain and livness information. Signed-off-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 71f656a commit b9fa9bc

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,39 @@
225225
.result_unpriv = ACCEPT,
226226
.insn_processed = 15,
227227
},
228+
/* The test performs a conditional 64-bit write to a stack location
229+
* fp[-8], this is followed by an unconditional 8-bit write to fp[-8],
230+
* then data is read from fp[-8]. This sequence is unsafe.
231+
*
232+
* The test would be mistakenly marked as safe w/o dst register parent
233+
* preservation in verifier.c:copy_register_state() function.
234+
*
235+
* Note the usage of BPF_F_TEST_STATE_FREQ to force creation of the
236+
* checkpoint state after conditional 64-bit assignment.
237+
*/
238+
{
239+
"write tracking and register parent chain bug",
240+
.insns = {
241+
/* r6 = ktime_get_ns() */
242+
BPF_EMIT_CALL(BPF_FUNC_ktime_get_ns),
243+
BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
244+
/* r0 = ktime_get_ns() */
245+
BPF_EMIT_CALL(BPF_FUNC_ktime_get_ns),
246+
/* if r0 > r6 goto +1 */
247+
BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_6, 1),
248+
/* *(u64 *)(r10 - 8) = 0xdeadbeef */
249+
BPF_ST_MEM(BPF_DW, BPF_REG_FP, -8, 0xdeadbeef),
250+
/* r1 = 42 */
251+
BPF_MOV64_IMM(BPF_REG_1, 42),
252+
/* *(u8 *)(r10 - 8) = r1 */
253+
BPF_STX_MEM(BPF_B, BPF_REG_FP, BPF_REG_1, -8),
254+
/* r2 = *(u64 *)(r10 - 8) */
255+
BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_FP, -8),
256+
/* exit(0) */
257+
BPF_MOV64_IMM(BPF_REG_0, 0),
258+
BPF_EXIT_INSN(),
259+
},
260+
.flags = BPF_F_TEST_STATE_FREQ,
261+
.errstr = "invalid read from stack off -8+1 size 8",
262+
.result = REJECT,
263+
},

0 commit comments

Comments
 (0)