Skip to content

Commit aad2eea

Browse files
joestringerborkmann
authored andcommitted
bpf: Simplify ptr_min_max_vals adjustment
An upcoming commit will add another two pointer types that need very similar behaviour, so generalise this function now. Signed-off-by: Joe Stringer <joe@wand.net.nz> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent f3709f6 commit aad2eea

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

kernel/bpf/verifier.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,20 +2669,18 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
26692669
return -EACCES;
26702670
}
26712671

2672-
if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
2673-
verbose(env, "R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n",
2674-
dst);
2675-
return -EACCES;
2676-
}
2677-
if (ptr_reg->type == CONST_PTR_TO_MAP) {
2678-
verbose(env, "R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n",
2679-
dst);
2672+
switch (ptr_reg->type) {
2673+
case PTR_TO_MAP_VALUE_OR_NULL:
2674+
verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",
2675+
dst, reg_type_str[ptr_reg->type]);
26802676
return -EACCES;
2681-
}
2682-
if (ptr_reg->type == PTR_TO_PACKET_END) {
2683-
verbose(env, "R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n",
2684-
dst);
2677+
case CONST_PTR_TO_MAP:
2678+
case PTR_TO_PACKET_END:
2679+
verbose(env, "R%d pointer arithmetic on %s prohibited\n",
2680+
dst, reg_type_str[ptr_reg->type]);
26852681
return -EACCES;
2682+
default:
2683+
break;
26862684
}
26872685

26882686
/* In case of 'scalar += pointer', dst_reg inherits pointer type and id.

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3638,7 +3638,7 @@ static struct bpf_test tests[] = {
36383638
BPF_MOV64_IMM(BPF_REG_0, 0),
36393639
BPF_EXIT_INSN(),
36403640
},
3641-
.errstr = "R3 pointer arithmetic on PTR_TO_PACKET_END",
3641+
.errstr = "R3 pointer arithmetic on pkt_end",
36423642
.result = REJECT,
36433643
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
36443644
},
@@ -4896,7 +4896,7 @@ static struct bpf_test tests[] = {
48964896
BPF_EXIT_INSN(),
48974897
},
48984898
.fixup_map1 = { 4 },
4899-
.errstr = "R4 pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL",
4899+
.errstr = "R4 pointer arithmetic on map_value_or_null",
49004900
.result = REJECT,
49014901
.prog_type = BPF_PROG_TYPE_SCHED_CLS
49024902
},
@@ -4917,7 +4917,7 @@ static struct bpf_test tests[] = {
49174917
BPF_EXIT_INSN(),
49184918
},
49194919
.fixup_map1 = { 4 },
4920-
.errstr = "R4 pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL",
4920+
.errstr = "R4 pointer arithmetic on map_value_or_null",
49214921
.result = REJECT,
49224922
.prog_type = BPF_PROG_TYPE_SCHED_CLS
49234923
},
@@ -4938,7 +4938,7 @@ static struct bpf_test tests[] = {
49384938
BPF_EXIT_INSN(),
49394939
},
49404940
.fixup_map1 = { 4 },
4941-
.errstr = "R4 pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL",
4941+
.errstr = "R4 pointer arithmetic on map_value_or_null",
49424942
.result = REJECT,
49434943
.prog_type = BPF_PROG_TYPE_SCHED_CLS
49444944
},
@@ -7253,7 +7253,7 @@ static struct bpf_test tests[] = {
72537253
BPF_EXIT_INSN(),
72547254
},
72557255
.fixup_map_in_map = { 3 },
7256-
.errstr = "R1 pointer arithmetic on CONST_PTR_TO_MAP prohibited",
7256+
.errstr = "R1 pointer arithmetic on map_ptr prohibited",
72577257
.result = REJECT,
72587258
},
72597259
{
@@ -8927,7 +8927,7 @@ static struct bpf_test tests[] = {
89278927
BPF_MOV64_IMM(BPF_REG_0, 0),
89288928
BPF_EXIT_INSN(),
89298929
},
8930-
.errstr = "R3 pointer arithmetic on PTR_TO_PACKET_END",
8930+
.errstr = "R3 pointer arithmetic on pkt_end",
89318931
.result = REJECT,
89328932
.prog_type = BPF_PROG_TYPE_XDP,
89338933
},
@@ -8946,7 +8946,7 @@ static struct bpf_test tests[] = {
89468946
BPF_MOV64_IMM(BPF_REG_0, 0),
89478947
BPF_EXIT_INSN(),
89488948
},
8949-
.errstr = "R3 pointer arithmetic on PTR_TO_PACKET_END",
8949+
.errstr = "R3 pointer arithmetic on pkt_end",
89508950
.result = REJECT,
89518951
.prog_type = BPF_PROG_TYPE_XDP,
89528952
},

0 commit comments

Comments
 (0)