Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6804d42

Browse files
danobiKernel Patches Daemon
authored and
Kernel Patches Daemon
committedFeb 4, 2025·
bpf: verifier: Disambiguate get_constant_map_key() errors
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. Refactor anyways to make things cleaner and prevent potential future issues. Acked-by: Eduard Zingerman <[email protected]> Signed-off-by: Daniel Xu <[email protected]>
1 parent 75b752a commit 6804d42

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed
 

‎kernel/bpf/verifier.c

+17-8
Original file line numberDiff line numberDiff line change
@@ -9149,10 +9149,11 @@ static int check_reg_const_str(struct bpf_verifier_env *env,
91499149
return 0;
91509150
}
91519151

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,
91549154
struct bpf_reg_state *key,
9155-
u32 key_size)
9155+
u32 key_size,
9156+
s64 *value)
91569157
{
91579158
struct bpf_func_state *state = func(env, key);
91589159
struct bpf_reg_state *reg;
@@ -9179,8 +9180,10 @@ static s64 get_constant_map_key(struct bpf_verifier_env *env,
91799180
/* First handle precisely tracked STACK_ZERO */
91809181
for (i = off; i >= 0 && stype[i] == STACK_ZERO; i--)
91819182
zero_size++;
9182-
if (zero_size >= key_size)
9183+
if (zero_size >= key_size) {
9184+
*value = 0;
91839185
return 0;
9186+
}
91849187

91859188
/* Check that stack contains a scalar spill of expected size */
91869189
if (!is_spilled_scalar_reg(&state->stack[spi]))
@@ -9203,7 +9206,8 @@ static s64 get_constant_map_key(struct bpf_verifier_env *env,
92039206
if (err < 0)
92049207
return err;
92059208

9206-
return reg->var_off.value;
9209+
*value = reg->var_off.value;
9210+
return 0;
92079211
}
92089212

92099213
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,
93579361
if (err)
93589362
return err;
93599363
if (can_elide_value_nullness(meta->map_ptr->map_type)) {
9360-
meta->const_map_key = get_constant_map_key(env, reg, key_size);
9361-
if (meta->const_map_key < 0 && meta->const_map_key != -EOPNOTSUPP)
9362-
return meta->const_map_key;
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+
}
93639372
}
93649373
break;
93659374
case ARG_PTR_TO_MAP_VALUE:

0 commit comments

Comments
 (0)
Please sign in to comment.