Skip to content

Commit faaf4a7

Browse files
olsajiriAlexei Starovoitov
authored andcommitted
bpf: Add btf_struct_ids_match function
Adding btf_struct_ids_match function to check if given address provided by BTF object + offset is also address of another nested BTF object. This allows to pass an argument to helper, which is defined via parent BTF object + offset, like for bpf_d_path (added in following changes): SEC("fentry/filp_close") int BPF_PROG(prog_close, struct file *file, void *id) { ... ret = bpf_d_path(&file->f_path, ... The first bpf_d_path argument is hold by verifier as BTF file object plus offset of f_path member. The btf_struct_ids_match function will walk the struct file object and check if there's nested struct path object on the given offset. Signed-off-by: Jiri Olsa <jolsa@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/20200825192124.710397-9-jolsa@kernel.org
1 parent 1c6d28a commit faaf4a7

3 files changed

Lines changed: 44 additions & 6 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,8 @@ int btf_struct_access(struct bpf_verifier_log *log,
13581358
const struct btf_type *t, int off, int size,
13591359
enum bpf_access_type atype,
13601360
u32 *next_btf_id);
1361+
bool btf_struct_ids_match(struct bpf_verifier_log *log,
1362+
int off, u32 id, u32 need_type_id);
13611363
int btf_resolve_helper_id(struct bpf_verifier_log *log,
13621364
const struct bpf_func_proto *fn, int);
13631365

‎kernel/bpf/btf.c‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4160,6 +4160,37 @@ int btf_struct_access(struct bpf_verifier_log *log,
41604160
return -EINVAL;
41614161
}
41624162

4163+
bool btf_struct_ids_match(struct bpf_verifier_log *log,
4164+
int off, u32 id, u32 need_type_id)
4165+
{
4166+
const struct btf_type *type;
4167+
int err;
4168+
4169+
/* Are we already done? */
4170+
if (need_type_id == id && off == 0)
4171+
return true;
4172+
4173+
again:
4174+
type = btf_type_by_id(btf_vmlinux, id);
4175+
if (!type)
4176+
return false;
4177+
err = btf_struct_walk(log, type, off, 1, &id);
4178+
if (err != WALK_STRUCT)
4179+
return false;
4180+
4181+
/* We found nested struct object. If it matches
4182+
* the requested ID, we're done. Otherwise let's
4183+
* continue the search with offset 0 in the new
4184+
* type.
4185+
*/
4186+
if (need_type_id != id) {
4187+
off = 0;
4188+
goto again;
4189+
}
4190+
4191+
return true;
4192+
}
4193+
41634194
int btf_resolve_helper_id(struct bpf_verifier_log *log,
41644195
const struct bpf_func_proto *fn, int arg)
41654196
{

‎kernel/bpf/verifier.c‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3995,24 +3995,29 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
39953995
goto err_type;
39963996
}
39973997
} else if (arg_type == ARG_PTR_TO_BTF_ID) {
3998+
bool ids_match = false;
3999+
39984000
expected_type = PTR_TO_BTF_ID;
39994001
if (type != expected_type)
40004002
goto err_type;
40014003
if (!fn->check_btf_id) {
40024004
if (reg->btf_id != meta->btf_id) {
4003-
verbose(env, "Helper has type %s got %s in R%d\n",
4004-
kernel_type_name(meta->btf_id),
4005-
kernel_type_name(reg->btf_id), regno);
4006-
4007-
return -EACCES;
4005+
ids_match = btf_struct_ids_match(&env->log, reg->off, reg->btf_id,
4006+
meta->btf_id);
4007+
if (!ids_match) {
4008+
verbose(env, "Helper has type %s got %s in R%d\n",
4009+
kernel_type_name(meta->btf_id),
4010+
kernel_type_name(reg->btf_id), regno);
4011+
return -EACCES;
4012+
}
40084013
}
40094014
} else if (!fn->check_btf_id(reg->btf_id, arg)) {
40104015
verbose(env, "Helper does not support %s in R%d\n",
40114016
kernel_type_name(reg->btf_id), regno);
40124017

40134018
return -EACCES;
40144019
}
4015-
if (!tnum_is_const(reg->var_off) || reg->var_off.value || reg->off) {
4020+
if ((reg->off && !ids_match) || !tnum_is_const(reg->var_off) || reg->var_off.value) {
40164021
verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n",
40174022
regno);
40184023
return -EACCES;

0 commit comments

Comments
 (0)