Skip to content

Commit 69c4e8a

Browse files
ecree-solarflaredavem330
authored andcommitted
selftests/bpf: variable offset negative tests
Variable ctx accesses and stack accesses aren't allowed, because we can't determine what type of value will be read. Signed-off-by: Edward Cree <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent f999d64 commit 69c4e8a

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5980,6 +5980,47 @@ static struct bpf_test tests[] = {
59805980
.errstr = "R0 min value is negative, either use unsigned index or do a if (index >=0) check.",
59815981
.result = REJECT,
59825982
},
5983+
{
5984+
"variable-offset ctx access",
5985+
.insns = {
5986+
/* Get an unknown value */
5987+
BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 0),
5988+
/* Make it small and 4-byte aligned */
5989+
BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 4),
5990+
/* add it to skb. We now have either &skb->len or
5991+
* &skb->pkt_type, but we don't know which
5992+
*/
5993+
BPF_ALU64_REG(BPF_ADD, BPF_REG_1, BPF_REG_2),
5994+
/* dereference it */
5995+
BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, 0),
5996+
BPF_EXIT_INSN(),
5997+
},
5998+
.errstr = "variable ctx access var_off=(0x0; 0x4)",
5999+
.result = REJECT,
6000+
.prog_type = BPF_PROG_TYPE_LWT_IN,
6001+
},
6002+
{
6003+
"variable-offset stack access",
6004+
.insns = {
6005+
/* Fill the top 8 bytes of the stack */
6006+
BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
6007+
/* Get an unknown value */
6008+
BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 0),
6009+
/* Make it small and 4-byte aligned */
6010+
BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 4),
6011+
BPF_ALU64_IMM(BPF_SUB, BPF_REG_2, 8),
6012+
/* add it to fp. We now have either fp-4 or fp-8, but
6013+
* we don't know which
6014+
*/
6015+
BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_10),
6016+
/* dereference it */
6017+
BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_2, 0),
6018+
BPF_EXIT_INSN(),
6019+
},
6020+
.errstr = "variable stack access var_off=(0xfffffffffffffff8; 0x4)",
6021+
.result = REJECT,
6022+
.prog_type = BPF_PROG_TYPE_LWT_IN,
6023+
},
59836024
};
59846025

59856026
static int probe_filter_length(const struct bpf_insn *fp)

0 commit comments

Comments
 (0)