Commit 95f2f26f authored by Benjamin Tissoires's avatar Benjamin Tissoires Committed by Alexei Starovoitov

bpf: split btf_check_subprog_arg_match in two

btf_check_subprog_arg_match() was used twice in verifier.c:
- when checking for the type mismatches between a (sub)prog declaration
  and BTF
- when checking the call of a subprog to see if the provided arguments
  are correct and valid

This is problematic when we check if the first argument of a program
(pointer to ctx) is correctly accessed:
To be able to ensure we access a valid memory in the ctx, the verifier
assumes the pointer to context is not null.
This has the side effect of marking the program accessing the entire
context, even if the context is never dereferenced.

For example, by checking the context access with the current code, the
following eBPF program would fail with -EINVAL if the ctx is set to null
from the userspace:

```
SEC("syscall")
int prog(struct my_ctx *args) {
  return 0;
}
```

In that particular case, we do not want to actually check that the memory
is correct while checking for the BTF validity, but we just want to
ensure that the (sub)prog definition matches the BTF we have.

So split btf_check_subprog_arg_match() in two so we can actually check
for the memory used when in a call, and ignore that part when not.

Note that a further patch is in preparation to disentangled
btf_check_func_arg_match() from these two purposes, and so right now we
just add a new hack around that by adding a boolean to this function.
Signed-off-by: default avatarBenjamin Tissoires <benjamin.tissoires@redhat.com>
Acked-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20220906151303.2780789-3-benjamin.tissoires@redhat.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 012ba115
...@@ -1947,6 +1947,8 @@ int btf_distill_func_proto(struct bpf_verifier_log *log, ...@@ -1947,6 +1947,8 @@ int btf_distill_func_proto(struct bpf_verifier_log *log,
struct bpf_reg_state; struct bpf_reg_state;
int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog, int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
struct bpf_reg_state *regs); struct bpf_reg_state *regs);
int btf_check_subprog_call(struct bpf_verifier_env *env, int subprog,
struct bpf_reg_state *regs);
int btf_check_kfunc_arg_match(struct bpf_verifier_env *env, int btf_check_kfunc_arg_match(struct bpf_verifier_env *env,
const struct btf *btf, u32 func_id, const struct btf *btf, u32 func_id,
struct bpf_reg_state *regs, struct bpf_reg_state *regs,
......
...@@ -6203,7 +6203,8 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, ...@@ -6203,7 +6203,8 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
const struct btf *btf, u32 func_id, const struct btf *btf, u32 func_id,
struct bpf_reg_state *regs, struct bpf_reg_state *regs,
bool ptr_to_mem_ok, bool ptr_to_mem_ok,
u32 kfunc_flags) u32 kfunc_flags,
bool processing_call)
{ {
enum bpf_prog_type prog_type = resolve_prog_type(env->prog); enum bpf_prog_type prog_type = resolve_prog_type(env->prog);
bool rel = false, kptr_get = false, trusted_arg = false; bool rel = false, kptr_get = false, trusted_arg = false;
...@@ -6389,7 +6390,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, ...@@ -6389,7 +6390,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
reg_ref_tname); reg_ref_tname);
return -EINVAL; return -EINVAL;
} }
} else if (ptr_to_mem_ok) { } else if (ptr_to_mem_ok && processing_call) {
const struct btf_type *resolve_ret; const struct btf_type *resolve_ret;
u32 type_size; u32 type_size;
...@@ -6464,7 +6465,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, ...@@ -6464,7 +6465,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
return rel ? ref_regno : 0; return rel ? ref_regno : 0;
} }
/* Compare BTF of a function with given bpf_reg_state. /* Compare BTF of a function declaration with given bpf_reg_state.
* Returns: * Returns:
* EFAULT - there is a verifier bug. Abort verification. * EFAULT - there is a verifier bug. Abort verification.
* EINVAL - there is a type mismatch or BTF is not available. * EINVAL - there is a type mismatch or BTF is not available.
...@@ -6491,7 +6492,50 @@ int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog, ...@@ -6491,7 +6492,50 @@ int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
return -EINVAL; return -EINVAL;
is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL;
err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global, 0); err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global, 0, false);
/* Compiler optimizations can remove arguments from static functions
* or mismatched type can be passed into a global function.
* In such cases mark the function as unreliable from BTF point of view.
*/
if (err)
prog->aux->func_info_aux[subprog].unreliable = true;
return err;
}
/* Compare BTF of a function call with given bpf_reg_state.
* Returns:
* EFAULT - there is a verifier bug. Abort verification.
* EINVAL - there is a type mismatch or BTF is not available.
* 0 - BTF matches with what bpf_reg_state expects.
* Only PTR_TO_CTX and SCALAR_VALUE states are recognized.
*
* NOTE: the code is duplicated from btf_check_subprog_arg_match()
* because btf_check_func_arg_match() is still doing both. Once that
* function is split in 2, we can call from here btf_check_subprog_arg_match()
* first, and then treat the calling part in a new code path.
*/
int btf_check_subprog_call(struct bpf_verifier_env *env, int subprog,
struct bpf_reg_state *regs)
{
struct bpf_prog *prog = env->prog;
struct btf *btf = prog->aux->btf;
bool is_global;
u32 btf_id;
int err;
if (!prog->aux->func_info)
return -EINVAL;
btf_id = prog->aux->func_info[subprog].type_id;
if (!btf_id)
return -EFAULT;
if (prog->aux->func_info_aux[subprog].unreliable)
return -EINVAL;
is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL;
err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global, 0, true);
/* Compiler optimizations can remove arguments from static functions /* Compiler optimizations can remove arguments from static functions
* or mismatched type can be passed into a global function. * or mismatched type can be passed into a global function.
...@@ -6507,7 +6551,7 @@ int btf_check_kfunc_arg_match(struct bpf_verifier_env *env, ...@@ -6507,7 +6551,7 @@ int btf_check_kfunc_arg_match(struct bpf_verifier_env *env,
struct bpf_reg_state *regs, struct bpf_reg_state *regs,
u32 kfunc_flags) u32 kfunc_flags)
{ {
return btf_check_func_arg_match(env, btf, func_id, regs, true, kfunc_flags); return btf_check_func_arg_match(env, btf, func_id, regs, true, kfunc_flags, true);
} }
/* Convert BTF of a function into bpf_reg_state if possible /* Convert BTF of a function into bpf_reg_state if possible
......
...@@ -6629,7 +6629,7 @@ static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn ...@@ -6629,7 +6629,7 @@ static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn
func_info_aux = env->prog->aux->func_info_aux; func_info_aux = env->prog->aux->func_info_aux;
if (func_info_aux) if (func_info_aux)
is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL;
err = btf_check_subprog_arg_match(env, subprog, caller->regs); err = btf_check_subprog_call(env, subprog, caller->regs);
if (err == -EFAULT) if (err == -EFAULT)
return err; return err;
if (is_global) { if (is_global) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment