Re: [PATCH bpf-next v3 1/2] bpf: verifier: Support eliding map lookup nullness

From: Alexei Starovoitov
Date: Wed Sep 25 2024 - 04:24:27 EST


On Tue, Sep 24, 2024 at 12:40 PM Daniel Xu <dxu@xxxxxxxxx> wrote:
>
> +
> +/* Returns constant key value if possible, else -1 */
> +static long get_constant_map_key(struct bpf_verifier_env *env,
> + struct bpf_reg_state *key)
> +{
> + struct bpf_func_state *state = func(env, key);
> + struct bpf_reg_state *reg;
> + int stack_off;
> + int slot;
> + int spi;
> +
> + if (key->type != PTR_TO_STACK)
> + return -1;
> + if (!tnum_is_const(key->var_off))
> + return -1;
> +
> + stack_off = key->off + key->var_off.value;
> + slot = -stack_off - 1;
> + if (slot < 0)
> + /* Stack grew upwards */

The comment is misleading.
The verifier is supposed to catch this.
It's just this helper was called before the stack bounds
were checked?
Maybe the call can be done later?

> + return -1;
> + else if (slot >= state->allocated_stack)
> + /* Stack uninitialized */
> + return -1;
> +
> + spi = slot / BPF_REG_SIZE;
> + reg = &state->stack[spi].spilled_ptr;
> + if (!tnum_is_const(reg->var_off))
> + /* Stack value not statically known */
> + return -1;
> +
> + return reg->var_off.value;
> +}

Looks like the code is more subtle than it looks.

I think it's better to guard it all with CAP_BPF.

pw-bot: cr