Skip to contentSkip to conversation

bpf: Preserve const register type on const OR alu ops

PATCH Applied Alexei Starovoitov 3 participants 3 messages Dec 3, 2016 – Dec 5, 2016

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Preserve const register type on const OR alu ops

From: Gianluca Borello <[email protected]>

Occasionally, clang (e.g. version 3.8.1) translates a sum between two constant operands using a BPF_OR instead of a BPF_ADD. The verifier is currently not handling this scenario, and the destination register type becomes UNKNOWN_VALUE even if it's still storing a constant. As a result, the destination register cannot be used as argument to a helper function expecting a ARG_CONST_STACK_*, limiting some use cases.

Modify the verifier to handle this case, and add a few tests to make sure all combinations are supported, and stack boundaries are still verified even with BPF_OR.

---
 kernel/bpf/verifier.c                       |  9 ++++-
 tools/testing/selftests/bpf/.gitignore      |  1 +
 tools/testing/selftests/bpf/test_verifier.c | 60 +++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +7 -2
index 0e742210750e..38d05da84a49 100644
@@ -1481,14 +1481,19 @@ static int evaluate_reg_imm_alu(struct bpf_verifier_env *env,
 	struct bpf_reg_state *src_reg = &regs[insn->src_reg];
 	u8 opcode = BPF_OP(insn->code);
 
-	/* dst_reg->type == CONST_IMM here, simulate execution of 'add' insn.
-	 * Don't care about overflow or negative values, just add them
+	/* dst_reg->type == CONST_IMM here, simulate execution of 'add'/'or'
+	 * insn. Don't care about overflow or negative values, just add them
 	 */
 	if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_K)
 		dst_reg->imm += insn->imm;
 	else if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_X &&
 		 src_reg->type == CONST_IMM)
 		dst_reg->imm += src_reg->imm;
+	else if (opcode == BPF_OR && BPF_SRC(insn->code) == BPF_K)
+		dst_reg->imm |= insn->imm;
+	else if (opcode == BPF_OR && BPF_SRC(insn->code) == BPF_X &&
+		 src_reg->type == CONST_IMM)
+		dst_reg->imm |= src_reg->imm;
 	else
 		mark_reg_unknown_value(regs, insn->dst_reg);
 	return 0;
tools/testing/selftests/bpf/.gitignore +1 -0
index 3c59f96e3ed8..071431bedde8 100644
@@ -1,2 +1,3 @@
 test_verifier
 test_maps
+test_lru_map
tools/testing/selftests/bpf/test_verifier.c +60 -0

66 lines not on this page · raw message

Signature · 1 line
2.8.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Alexei Starovoitov

From: Gianluca Borello <[email protected]>

Occasionally, clang (e.g. version 3.8.1) translates a sum between two constant operands using a BPF_OR instead of a BPF_ADD. The verifier is currently not handling this scenario, and the destination register type becomes UNKNOWN_VALUE even if it's still storing a constant. As a result, the destination register cannot be used as argument to a helper function expecting a ARG_CONST_STACK_*, limiting some use cases.

Quoted 6 lines

Modify the verifier to handle this case, and add a few tests to make sure all combinations are supported, and stack boundaries are still verified even with BPF_OR.

maintainer Applied
#2 lore raw
In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>
Date: Sat, 3 Dec 2016 12:31:33 -0800

Quoted 9 lines

From: Gianluca Borello <[email protected]>

Occasionally, clang (e.g. version 3.8.1) translates a sum between two constant operands using a BPF_OR instead of a BPF_ADD. The verifier is currently not handling this scenario, and the destination register type becomes UNKNOWN_VALUE even if it's still storing a constant. As a result, the destination register cannot be used as argument to a helper function expecting a ARG_CONST_STACK_*, limiting some use cases.

Quoted 6 lines

Modify the verifier to handle this case, and add a few tests to make sure all combinations are supported, and stack boundaries are still verified even with BPF_OR.

Applied, but...

tools/testing/selftests/bpf/.gitignore line 3

Quoted 8 lines
tools/testing/selftests/bpf/.gitignore +1 -0
index 3c59f96e3ed8..071431bedde8 100644
@@ -1,2 +1,3 @@
 test_verifier
 test_maps
+test_lru_map

This had nothing to do with this change, nor was it mentioned in the commit message.

Please do not do this in the future.