aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrey Ignatov <rdna@fb.com>2019-04-04 02:22:40 -0400
committerDaniel Borkmann <daniel@iogearbox.net>2019-04-05 10:50:08 -0400
commit2c6927dbdc3fbd41207e671212f53a98bbebf6ba (patch)
treefead722fca639d9f3aef274e4a207acf3e6faf82
parent088ec26d9c2da9d879ab73e3f4117f9df6c566ee (diff)
selftests/bpf: Test indirect var_off stack access in unpriv mode
Test that verifier rejects indirect stack access with variable offset in unprivileged mode and accepts same code in privileged mode. Since pointer arithmetics is prohibited in unprivileged mode verifier should reject the program even before it gets to helper call that uses variable offset, at the time when that variable offset is trying to be constructed. Example of output: # ./test_verifier ... #859/u indirect variable-offset stack access, priv vs unpriv OK #859/p indirect variable-offset stack access, priv vs unpriv OK Signed-off-by: Andrey Ignatov <rdna@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
-rw-r--r--tools/testing/selftests/bpf/verifier/var_off.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/tools/testing/selftests/bpf/verifier/var_off.c b/tools/testing/selftests/bpf/verifier/var_off.c
index 3840bd16e173..f5d5ff18ef22 100644
--- a/tools/testing/selftests/bpf/verifier/var_off.c
+++ b/tools/testing/selftests/bpf/verifier/var_off.c
@@ -115,6 +115,33 @@
115 .prog_type = BPF_PROG_TYPE_LWT_IN, 115 .prog_type = BPF_PROG_TYPE_LWT_IN,
116}, 116},
117{ 117{
118 "indirect variable-offset stack access, priv vs unpriv",
119 .insns = {
120 /* Fill the top 16 bytes of the stack. */
121 BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, 0),
122 BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
123 /* Get an unknown value. */
124 BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 0),
125 /* Make it small and 4-byte aligned. */
126 BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 4),
127 BPF_ALU64_IMM(BPF_SUB, BPF_REG_2, 16),
128 /* Add it to fp. We now have either fp-12 or fp-16, we don't know
129 * which, but either way it points to initialized stack.
130 */
131 BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_10),
132 /* Dereference it indirectly. */
133 BPF_LD_MAP_FD(BPF_REG_1, 0),
134 BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
135 BPF_MOV64_IMM(BPF_REG_0, 0),
136 BPF_EXIT_INSN(),
137 },
138 .fixup_map_hash_8b = { 6 },
139 .errstr_unpriv = "R2 stack pointer arithmetic goes out of range, prohibited for !root",
140 .result_unpriv = REJECT,
141 .result = ACCEPT,
142 .prog_type = BPF_PROG_TYPE_CGROUP_SKB,
143},
144{
118 "indirect variable-offset stack access, uninitialized", 145 "indirect variable-offset stack access, uninitialized",
119 .insns = { 146 .insns = {
120 BPF_MOV64_IMM(BPF_REG_2, 6), 147 BPF_MOV64_IMM(BPF_REG_2, 6),