@@ -9149,10 +9149,11 @@ static int check_reg_const_str(struct bpf_verifier_env *env,
9149
9149
return 0;
9150
9150
}
9151
9151
9152
- /* Returns constant key value if possible, else negative error */
9153
- static s64 get_constant_map_key(struct bpf_verifier_env *env,
9152
+ /* Returns constant key value in `value` if possible, else negative error */
9153
+ static int get_constant_map_key(struct bpf_verifier_env *env,
9154
9154
struct bpf_reg_state *key,
9155
- u32 key_size)
9155
+ u32 key_size,
9156
+ s64 *value)
9156
9157
{
9157
9158
struct bpf_func_state *state = func(env, key);
9158
9159
struct bpf_reg_state *reg;
@@ -9179,8 +9180,10 @@ static s64 get_constant_map_key(struct bpf_verifier_env *env,
9179
9180
/* First handle precisely tracked STACK_ZERO */
9180
9181
for (i = off; i >= 0 && stype[i] == STACK_ZERO; i--)
9181
9182
zero_size++;
9182
- if (zero_size >= key_size)
9183
+ if (zero_size >= key_size) {
9184
+ *value = 0;
9183
9185
return 0;
9186
+ }
9184
9187
9185
9188
/* Check that stack contains a scalar spill of expected size */
9186
9189
if (!is_spilled_scalar_reg(&state->stack[spi]))
@@ -9203,9 +9206,12 @@ static s64 get_constant_map_key(struct bpf_verifier_env *env,
9203
9206
if (err < 0)
9204
9207
return err;
9205
9208
9206
- return reg->var_off.value;
9209
+ *value = reg->var_off.value;
9210
+ return 0;
9207
9211
}
9208
9212
9213
+ static bool can_elide_value_nullness(enum bpf_map_type type);
9214
+
9209
9215
static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
9210
9216
struct bpf_call_arg_meta *meta,
9211
9217
const struct bpf_func_proto *fn,
@@ -9354,9 +9360,16 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
9354
9360
err = check_helper_mem_access(env, regno, key_size, BPF_READ, false, NULL);
9355
9361
if (err)
9356
9362
return err;
9357
- meta->const_map_key = get_constant_map_key(env, reg, key_size);
9358
- if (meta->const_map_key < 0 && meta->const_map_key != -EOPNOTSUPP)
9359
- return meta->const_map_key;
9363
+ if (can_elide_value_nullness(meta->map_ptr->map_type)) {
9364
+ err = get_constant_map_key(env, reg, key_size, &meta->const_map_key);
9365
+ if (err < 0) {
9366
+ meta->const_map_key = -1;
9367
+ if (err == -EOPNOTSUPP)
9368
+ err = 0;
9369
+ else
9370
+ return err;
9371
+ }
9372
+ }
9360
9373
break;
9361
9374
case ARG_PTR_TO_MAP_VALUE:
9362
9375
if (type_may_be_null(arg_type) && register_is_null(reg))
0 commit comments