bpf: Some fixes for nullness elision
Subsystems (4)
cover bpf: Some fixes for nullness elision
Two fixes for nullness elision. See commits for more details.
Daniel Xu (3):
bpf: verifier: Do not extract constant map keys for irrelevant maps
bpf: selftests: Test constant key extraction on irrelevant maps
bpf: verifier: Disambiguate get_constant_map_key() errors
kernel/bpf/verifier.c | 29 ++++++++++++++----- .../bpf/progs/verifier_array_access.c | 15 ++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-)
Signature · 1 line
2.47.1
PATCH 1/3 bpf: verifier: Do not extract constant map keys for irrelevant maps
Previously, we were trying to extract constant map keys for all bpf_map_lookup_elem(), regardless of map type. This is an issue if the map has a u64 key and the value is very high, as it can be interpreted as a negative signed value. This in turn is treated as an error value by check_func_arg() which causes a valid program to be incorrectly rejected.
Fix by only extracting constant map keys for relevant maps. See next commit for an example via selftest.
--- kernel/bpf/verifier.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +7 -3
@@ -9206,6 +9206,8 @@ static s64 get_constant_map_key(struct bpf_verifier_env *env,return reg->var_off.value; } +static bool can_elide_value_nullness (enum bpf_map_type type); + static int check_func_arg(struct bpf_verifier_env *env, u32 arg, struct bpf_call_arg_meta *meta, const struct bpf_func_proto *fn,
@@ -9354,9 +9356,11 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, err =check_helper_mem_access (env, regno, key_size,BPF_READ , false,NULL );if (err)return err; - meta->const_map_key =get_constant_map_key (env, reg, key_size); -if (meta->const_map_key <0 && meta->const_map_key != -EOPNOTSUPP ) -return meta->const_map_key; +if (can_elide_value_nullness (meta->map_ptr->map_type)) { + meta->const_map_key =get_constant_map_key (env, reg, key_size); +if (meta->const_map_key <0 && meta->const_map_key != -EOPNOTSUPP ) +return meta->const_map_key; + }break ;case ARG_PTR_TO_MAP_VALUE :if (type_may_be_null (arg_type) &®ister_is_null (reg))
Signature · 1 line
2.47.1
Re: [PATCH bpf-next 1/3] bpf: verifier: Do not extract constant map keys for irrelevant maps
Quoted 7 lines · Daniel Xu
Previously, we were trying to extract constant map keys for all
bpf_map_lookup_elem(), regardless of map type. This is an issue if the map has a u64 key and the value is very high, as it can be interpreted as a negative signed value. This in turn is treated as an error value bycheck_func_arg()which causes a valid program to be incorrectly rejected.
Quoted 7 lines
Fix by only extracting constant map keys for relevant maps. See next commit for an example via selftest.
Reported-by Marc Hartmayer <[email protected]>Reported-by Ilya Leoshkevich <[email protected]>Tested-by Marc Hartmayer <[email protected]>Signed-off-by Daniel Xu <[email protected]>
Nit:
would be good if commit message said something along the lines:
... the fix works because nullness elision is only allowed for
{PERCPU_}ARRAY maps, and keys for these are within u32 range ...
Quoted 32 lines
--- kernel/bpf/verifier.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)kernel/bpf/verifier.c +7
-3@@ -9206,6 +9206,8 @@ static s64 get_constant_map_key(struct bpf_verifier_env *env,return reg->var_off.value; } +static bool can_elide_value_nullness (enum bpf_map_type type); + static int check_func_arg(struct bpf_verifier_env *env, u32 arg, struct bpf_call_arg_meta *meta, const struct bpf_func_proto *fn,@@ -9354,9 +9356,11 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, err =check_helper_mem_access (env, regno, key_size,BPF_READ , false,NULL );if (err)return err; - meta->const_map_key =get_constant_map_key (env, reg, key_size); -if (meta->const_map_key <0 && meta->const_map_key != -EOPNOTSUPP ) -return meta->const_map_key; +if (can_elide_value_nullness (meta->map_ptr->map_type)) { + meta->const_map_key =get_constant_map_key (env, reg, key_size); +if (meta->const_map_key <0 && meta->const_map_key != -EOPNOTSUPP ) +return meta->const_map_key; + }break ;case ARG_PTR_TO_MAP_VALUE :if (type_may_be_null (arg_type) &®ister_is_null (reg))
Re: [PATCH bpf-next 1/3] bpf: verifier: Do not extract constant map keys for irrelevant maps
Quoted 16 lines · Eduard Zingerman
Quoted 7 lines · Daniel Xu
Previously, we were trying to extract constant map keys for all
bpf_map_lookup_elem(), regardless of map type. This is an issue if the map has a u64 key and the value is very high, as it can be interpreted as a negative signed value. This in turn is treated as an error value bycheck_func_arg()which causes a valid program to be incorrectly rejected.Quoted 7 lines
Fix by only extracting constant map keys for relevant maps. See next commit for an example via selftest.
Reported-by Marc Hartmayer <[email protected]>Reported-by Ilya Leoshkevich <[email protected]>Tested-by Marc Hartmayer <[email protected]>Signed-off-by Daniel Xu <[email protected]>
Quoted 6 lines
Acked-by Eduard Zingerman <[email protected]>Nit:
would be good if commit message said something along the lines:
... the fix works because nullness elision is only allowed for
{PERCPU_}ARRAY maps, and keys for these are within u32 range ...
Ack. I can respin if necessary. Otherwise, here's the edited commit msg:
bpf: verifier: Do not extract constant map keys for irrelevant maps
Previously, we were trying to extract constant map keys for all bpf_map_lookup_elem(), regardless of map type. This is an issue if the map has a u64 key and the value is very high, as it can be interpreted as a negative signed value. This in turn is treated as an error value by check_func_arg() which causes a valid program to be incorrectly rejected.
Fix by only extracting constant map keys for relevant maps. This fix works because nullness elision is only allowed for {PERCPU_}ARRAY maps, and keys for these are within u32 range. See next commit for an example via selftest.
PATCH 2/3 bpf: selftests: Test constant key extraction on irrelevant maps
In reply to Daniel XuTest that very high constant map keys are not interpreted as an error value by the verifier. This would previously fail.
--- .../selftests/bpf/progs/verifier_array_access.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
tools/testing/selftests/bpf/progs/verifier_array_access.c +15 -0
@@ -713,4 +713,19 @@ unsigned int non_stack_key_lookup(void)return val->index; } +SEC("socket") +__description("doesn't reject UINT64_MAX as s64 for irrelevant maps") +__success __retval(42) +unsigned int doesnt_reject_irrelevant_maps(void) +{ +__u64 key =0xFFFFFFFFFFFFFFFF ; +struct test_val *val; + + val =bpf_map_lookup_elem (&map_hash_48b, &key); +if (val) +return val->index; + +return 42 ; +} + char _license[] SEC("license") = "GPL";
Signature · 1 line
2.47.1
Re: [PATCH bpf-next 2/3] bpf: selftests: Test constant key extraction on irrelevant maps
Quoted 5 lines · Daniel Xu
Test that very high constant map keys are not interpreted as an error value by the verifier. This would previously fail.
Signed-off-by Daniel Xu <[email protected]>---
[...]
PATCH 3/3 bpf: verifier: Disambiguate get_constant_map_key() errors
In reply to Daniel XuRefactor get_constant_map_key() to disambiguate the constant key value from potential error values. In the case that the key is negative, it could be confused for an error.
It's not currently an issue, as the verifier seems to track s32 spills as u32. So even if the program wrongly uses a negative value for an arraymap key, the verifier just thinks it's an impossibly high value which gets correctly discarded.
Refactor anyways to make things cleaner and prevent potential future issues.
--- kernel/bpf/verifier.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-)
kernel/bpf/verifier.c +17 -8
@@ -9149,10 +9149,11 @@ static int check_reg_const_str(struct bpf_verifier_env *env,return 0 ; } -/* Returns constant key value if possible, else negative error */ -static s64 get_constant_map_key (struct bpf_verifier_env *env, +/* Returns constant key value in `value` if possible, else negative error */ +static int get_constant_map_key (struct bpf_verifier_env *env,struct bpf_reg_state *key, -u32 key_size) +u32 key_size, +s64 *value) {struct bpf_func_state *state =func (env, key);struct bpf_reg_state *reg;
@@ -9179,8 +9180,10 @@ static s64 get_constant_map_key(struct bpf_verifier_env *env,/* First handle precisely tracked STACK_ZERO */ for (i = off; i >=0 && stype[i] ==STACK_ZERO ; i--) zero_size++; -if (zero_size >= key_size) +if (zero_size >= key_size) { + *value =0 ;return 0 ; + }/* Check that stack contains a scalar spill of expected size */ if (!is_spilled_scalar_reg (&state->stack[spi]))
@@ -9203,7 +9206,8 @@ static s64 get_constant_map_key(struct bpf_verifier_env *env,if (err <0 )return err; -return reg->var_off.value; + *value = reg->var_off.value; +return 0 ; }static bool can_elide_value_nullness (enum bpf_map_type type);
@@ -9357,9 +9361,14 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,if (err)return err;if (can_elide_value_nullness (meta->map_ptr->map_type)) { - meta->const_map_key =get_constant_map_key (env, reg, key_size); -if (meta->const_map_key <0 && meta->const_map_key != -EOPNOTSUPP ) -return meta->const_map_key; + err =get_constant_map_key (env, reg, key_size, &meta->const_map_key); +if (err <0 ) { + meta->const_map_key =-1 ; +if (err == -EOPNOTSUPP ) + err =0 ; +else +return err; + } }break ;case ARG_PTR_TO_MAP_VALUE :
Signature · 1 line
2.47.1
Re: [PATCH bpf-next 3/3] bpf: verifier: Disambiguate get_constant_map_key() errors
Quoted 9 lines · Daniel Xu
Refactor
get_constant_map_key()to disambiguate the constant key value from potential error values. In the case that the key is negative, it could be confused for an error.It's not currently an issue, as the verifier seems to track s32 spills as u32. So even if the program wrongly uses a negative value for an arraymap key, the verifier just thinks it's an impossibly high value which gets correctly discarded.
Quoted 5 lines
Refactor anyways to make things cleaner and prevent potential future issues.
Signed-off-by Daniel Xu <[email protected]>---
[...]