Skip to content

Commit d93080c

Browse files
borkmanngregkh
authored andcommitted
bpf: fix partial copy of map_ptr when dst is scalar
commit 0962590 upstream. ALU operations on pointers such as scalar_reg += map_value_ptr are handled in adjust_ptr_min_max_vals(). Problem is however that map_ptr and range in the register state share a union, so transferring state through dst_reg->range = ptr_reg->range is just buggy as any new map_ptr in the dst_reg is then truncated (or null) for subsequent checks. Fix this by adding a raw member and use it for copying state over to dst_reg. Fixes: f1174f7 ("bpf/verifier: rework value tracking") Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Cc: Edward Cree <ecree@solarflare.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Edward Cree <ecree@solarflare.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 07a03b9 commit d93080c

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

‎include/linux/bpf_verifier.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ struct bpf_reg_state {
5050
* PTR_TO_MAP_VALUE_OR_NULL
5151
*/
5252
struct bpf_map *map_ptr;
53+
54+
/* Max size from any of the above. */
55+
unsigned long raw;
5356
};
5457
/* Fixed part of pointer offset, pointer types only */
5558
s32 off;

‎kernel/bpf/verifier.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2762,7 +2762,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
27622762
dst_reg->umax_value = umax_ptr;
27632763
dst_reg->var_off = ptr_reg->var_off;
27642764
dst_reg->off = ptr_reg->off + smin_val;
2765-
dst_reg->range = ptr_reg->range;
2765+
dst_reg->raw = ptr_reg->raw;
27662766
break;
27672767
}
27682768
/* A new variable offset is created. Note that off_reg->off
@@ -2792,10 +2792,11 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
27922792
}
27932793
dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
27942794
dst_reg->off = ptr_reg->off;
2795+
dst_reg->raw = ptr_reg->raw;
27952796
if (reg_is_pkt_pointer(ptr_reg)) {
27962797
dst_reg->id = ++env->id_gen;
27972798
/* something was added to pkt_ptr, set range to zero */
2798-
dst_reg->range = 0;
2799+
dst_reg->raw = 0;
27992800
}
28002801
break;
28012802
case BPF_SUB:
@@ -2824,7 +2825,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
28242825
dst_reg->var_off = ptr_reg->var_off;
28252826
dst_reg->id = ptr_reg->id;
28262827
dst_reg->off = ptr_reg->off - smin_val;
2827-
dst_reg->range = ptr_reg->range;
2828+
dst_reg->raw = ptr_reg->raw;
28282829
break;
28292830
}
28302831
/* A new variable offset is created. If the subtrahend is known
@@ -2850,11 +2851,12 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
28502851
}
28512852
dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
28522853
dst_reg->off = ptr_reg->off;
2854+
dst_reg->raw = ptr_reg->raw;
28532855
if (reg_is_pkt_pointer(ptr_reg)) {
28542856
dst_reg->id = ++env->id_gen;
28552857
/* something was added to pkt_ptr, set range to zero */
28562858
if (smin_val < 0)
2857-
dst_reg->range = 0;
2859+
dst_reg->raw = 0;
28582860
}
28592861
break;
28602862
case BPF_AND:

0 commit comments

Comments
 (0)