Skip to content

Commit 5b92a28

Browse files
Alexei Starovoitovborkmann
authored andcommitted
bpf: Support attaching tracing BPF program to other BPF programs
Allow FENTRY/FEXIT BPF programs to attach to other BPF programs of any type including their subprograms. This feature allows snooping on input and output packets in XDP, TC programs including their return values. In order to do that the verifier needs to track types not only of vmlinux, but types of other BPF programs as well. The verifier also needs to translate uapi/linux/bpf.h types used by networking programs into kernel internal BTF types used by FENTRY/FEXIT BPF programs. In some cases LLVM optimizations can remove arguments from BPF subprograms without adjusting BTF info that LLVM backend knows. When BTF info disagrees with actual types that the verifiers sees the BPF trampoline has to fallback to conservative and treat all arguments as u64. The FENTRY/FEXIT program can still attach to such subprograms, but it won't be able to recognize pointer types like 'struct sk_buff *' and it won't be able to pass them to bpf_skb_output() for dumping packets to user space. The FENTRY/FEXIT program would need to use bpf_probe_read_kernel() instead. The BPF_PROG_LOAD command is extended with attach_prog_fd field. When it's set to zero the attach_btf_id is one vmlinux BTF type ids. When attach_prog_fd points to previously loaded BPF program the attach_btf_id is BTF type id of main function or one of its subprograms. Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Song Liu <songliubraving@fb.com> Link: https://lore.kernel.org/bpf/20191114185720.1641606-18-ast@kernel.org
1 parent 8c1b6e6 commit 5b92a28

8 files changed

Lines changed: 152 additions & 28 deletions

File tree

‎arch/x86/net/bpf_jit_comp.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,8 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
504504
u8 *prog;
505505
int ret;
506506

507-
if (!is_kernel_text((long)ip))
507+
if (!is_kernel_text((long)ip) &&
508+
!is_bpf_text_address((long)ip))
508509
/* BPF trampoline in modules is not supported */
509510
return -EINVAL;
510511

‎include/linux/bpf.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ struct bpf_prog_aux {
495495
u32 func_cnt; /* used by non-func prog as the number of func progs */
496496
u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
497497
u32 attach_btf_id; /* in-kernel BTF type id to attach to */
498+
struct bpf_prog *linked_prog;
498499
bool verifier_zext; /* Zero extensions has been inserted by verifier. */
499500
bool offload_requested;
500501
bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */

‎include/linux/btf.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ static inline bool btf_type_is_func_proto(const struct btf_type *t)
8888
const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
8989
const char *btf_name_by_offset(const struct btf *btf, u32 offset);
9090
struct btf *btf_parse_vmlinux(void);
91+
struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
9192
#else
9293
static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
9394
u32 type_id)

‎include/uapi/linux/bpf.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ union bpf_attr {
425425
__aligned_u64 line_info; /* line info */
426426
__u32 line_info_cnt; /* number of bpf_line_info records */
427427
__u32 attach_btf_id; /* in-kernel BTF type id to attach to */
428+
__u32 attach_prog_fd; /* 0 to attach to vmlinux */
428429
};
429430

430431
struct { /* anonymous struct used by BPF_OBJ_* commands */

‎kernel/bpf/btf.c‎

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3530,6 +3530,20 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf,
35303530
return ctx_type;
35313531
}
35323532

3533+
static int btf_translate_to_vmlinux(struct bpf_verifier_log *log,
3534+
struct btf *btf,
3535+
const struct btf_type *t,
3536+
enum bpf_prog_type prog_type)
3537+
{
3538+
const struct btf_member *prog_ctx_type, *kern_ctx_type;
3539+
3540+
prog_ctx_type = btf_get_prog_ctx_type(log, btf, t, prog_type);
3541+
if (!prog_ctx_type)
3542+
return -ENOENT;
3543+
kern_ctx_type = prog_ctx_type + 1;
3544+
return kern_ctx_type->type;
3545+
}
3546+
35333547
struct btf *btf_parse_vmlinux(void)
35343548
{
35353549
struct btf_verifier_env *env = NULL;
@@ -3602,15 +3616,29 @@ struct btf *btf_parse_vmlinux(void)
36023616
return ERR_PTR(err);
36033617
}
36043618

3619+
struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog)
3620+
{
3621+
struct bpf_prog *tgt_prog = prog->aux->linked_prog;
3622+
3623+
if (tgt_prog) {
3624+
return tgt_prog->aux->btf;
3625+
} else {
3626+
return btf_vmlinux;
3627+
}
3628+
}
3629+
36053630
bool btf_ctx_access(int off, int size, enum bpf_access_type type,
36063631
const struct bpf_prog *prog,
36073632
struct bpf_insn_access_aux *info)
36083633
{
36093634
const struct btf_type *t = prog->aux->attach_func_proto;
3635+
struct bpf_prog *tgt_prog = prog->aux->linked_prog;
3636+
struct btf *btf = bpf_prog_get_target_btf(prog);
36103637
const char *tname = prog->aux->attach_func_name;
36113638
struct bpf_verifier_log *log = info->log;
36123639
const struct btf_param *args;
36133640
u32 nr_args, arg;
3641+
int ret;
36143642

36153643
if (off % 8) {
36163644
bpf_log(log, "func '%s' offset %d is not multiple of 8\n",
@@ -3619,7 +3647,8 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
36193647
}
36203648
arg = off / 8;
36213649
args = (const struct btf_param *)(t + 1);
3622-
nr_args = btf_type_vlen(t);
3650+
/* if (t == NULL) Fall back to default BPF prog with 5 u64 arguments */
3651+
nr_args = t ? btf_type_vlen(t) : 5;
36233652
if (prog->aux->attach_btf_trace) {
36243653
/* skip first 'void *__data' argument in btf_trace_##name typedef */
36253654
args++;
@@ -3628,26 +3657,32 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
36283657

36293658
if (prog->expected_attach_type == BPF_TRACE_FEXIT &&
36303659
arg == nr_args) {
3660+
if (!t)
3661+
/* Default prog with 5 args. 6th arg is retval. */
3662+
return true;
36313663
/* function return type */
3632-
t = btf_type_by_id(btf_vmlinux, t->type);
3664+
t = btf_type_by_id(btf, t->type);
36333665
} else if (arg >= nr_args) {
36343666
bpf_log(log, "func '%s' doesn't have %d-th argument\n",
36353667
tname, arg + 1);
36363668
return false;
36373669
} else {
3638-
t = btf_type_by_id(btf_vmlinux, args[arg].type);
3670+
if (!t)
3671+
/* Default prog with 5 args */
3672+
return true;
3673+
t = btf_type_by_id(btf, args[arg].type);
36393674
}
36403675
/* skip modifiers */
36413676
while (btf_type_is_modifier(t))
3642-
t = btf_type_by_id(btf_vmlinux, t->type);
3677+
t = btf_type_by_id(btf, t->type);
36433678
if (btf_type_is_int(t))
36443679
/* accessing a scalar */
36453680
return true;
36463681
if (!btf_type_is_ptr(t)) {
36473682
bpf_log(log,
36483683
"func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n",
36493684
tname, arg,
3650-
__btf_name_by_offset(btf_vmlinux, t->name_off),
3685+
__btf_name_by_offset(btf, t->name_off),
36513686
btf_kind_str[BTF_INFO_KIND(t->info)]);
36523687
return false;
36533688
}
@@ -3662,10 +3697,19 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
36623697
info->reg_type = PTR_TO_BTF_ID;
36633698
info->btf_id = t->type;
36643699

3665-
t = btf_type_by_id(btf_vmlinux, t->type);
3700+
if (tgt_prog) {
3701+
ret = btf_translate_to_vmlinux(log, btf, t, tgt_prog->type);
3702+
if (ret > 0) {
3703+
info->btf_id = ret;
3704+
return true;
3705+
} else {
3706+
return false;
3707+
}
3708+
}
3709+
t = btf_type_by_id(btf, t->type);
36663710
/* skip modifiers */
36673711
while (btf_type_is_modifier(t))
3668-
t = btf_type_by_id(btf_vmlinux, t->type);
3712+
t = btf_type_by_id(btf, t->type);
36693713
if (!btf_type_is_struct(t)) {
36703714
bpf_log(log,
36713715
"func '%s' arg%d type %s is not a struct\n",
@@ -3674,7 +3718,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
36743718
}
36753719
bpf_log(log, "func '%s' arg%d has btf_id %d type %s '%s'\n",
36763720
tname, arg, info->btf_id, btf_kind_str[BTF_INFO_KIND(t->info)],
3677-
__btf_name_by_offset(btf_vmlinux, t->name_off));
3721+
__btf_name_by_offset(btf, t->name_off));
36783722
return true;
36793723
}
36803724

@@ -3954,6 +3998,16 @@ int btf_distill_func_proto(struct bpf_verifier_log *log,
39543998
u32 i, nargs;
39553999
int ret;
39564000

4001+
if (!func) {
4002+
/* BTF function prototype doesn't match the verifier types.
4003+
* Fall back to 5 u64 args.
4004+
*/
4005+
for (i = 0; i < 5; i++)
4006+
m->arg_size[i] = 8;
4007+
m->ret_size = 8;
4008+
m->nr_args = 5;
4009+
return 0;
4010+
}
39574011
args = (const struct btf_param *)(func + 1);
39584012
nargs = btf_type_vlen(func);
39594013
if (nargs >= MAX_BPF_FUNC_ARGS) {

‎kernel/bpf/core.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,6 +2031,8 @@ void bpf_prog_free(struct bpf_prog *fp)
20312031
{
20322032
struct bpf_prog_aux *aux = fp->aux;
20332033

2034+
if (aux->linked_prog)
2035+
bpf_prog_put(aux->linked_prog);
20342036
INIT_WORK(&aux->work, bpf_prog_free_deferred);
20352037
schedule_work(&aux->work);
20362038
}

‎kernel/bpf/syscall.c‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,15 +1577,15 @@ static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
15771577
static int
15781578
bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
15791579
enum bpf_attach_type expected_attach_type,
1580-
u32 btf_id)
1580+
u32 btf_id, u32 prog_fd)
15811581
{
15821582
switch (prog_type) {
15831583
case BPF_PROG_TYPE_TRACING:
15841584
if (btf_id > BTF_MAX_TYPE)
15851585
return -EINVAL;
15861586
break;
15871587
default:
1588-
if (btf_id)
1588+
if (btf_id || prog_fd)
15891589
return -EINVAL;
15901590
break;
15911591
}
@@ -1636,7 +1636,7 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
16361636
}
16371637

16381638
/* last field in 'union bpf_attr' used by this command */
1639-
#define BPF_PROG_LOAD_LAST_FIELD attach_btf_id
1639+
#define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
16401640

16411641
static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
16421642
{
@@ -1679,7 +1679,8 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
16791679

16801680
bpf_prog_load_fixup_attach_type(attr);
16811681
if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
1682-
attr->attach_btf_id))
1682+
attr->attach_btf_id,
1683+
attr->attach_prog_fd))
16831684
return -EINVAL;
16841685

16851686
/* plain bpf_prog allocation */
@@ -1689,6 +1690,16 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
16891690

16901691
prog->expected_attach_type = attr->expected_attach_type;
16911692
prog->aux->attach_btf_id = attr->attach_btf_id;
1693+
if (attr->attach_prog_fd) {
1694+
struct bpf_prog *tgt_prog;
1695+
1696+
tgt_prog = bpf_prog_get(attr->attach_prog_fd);
1697+
if (IS_ERR(tgt_prog)) {
1698+
err = PTR_ERR(tgt_prog);
1699+
goto free_prog_nouncharge;
1700+
}
1701+
prog->aux->linked_prog = tgt_prog;
1702+
}
16921703

16931704
prog->aux->offload_requested = !!attr->prog_ifindex;
16941705

‎kernel/bpf/verifier.c‎

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9390,13 +9390,17 @@ static void print_verification_stats(struct bpf_verifier_env *env)
93909390
static int check_attach_btf_id(struct bpf_verifier_env *env)
93919391
{
93929392
struct bpf_prog *prog = env->prog;
9393+
struct bpf_prog *tgt_prog = prog->aux->linked_prog;
93939394
u32 btf_id = prog->aux->attach_btf_id;
93949395
const char prefix[] = "btf_trace_";
9396+
int ret = 0, subprog = -1, i;
93959397
struct bpf_trampoline *tr;
93969398
const struct btf_type *t;
9399+
bool conservative = true;
93979400
const char *tname;
9398-
int ret = 0;
9401+
struct btf *btf;
93999402
long addr;
9403+
u64 key;
94009404

94019405
if (prog->type != BPF_PROG_TYPE_TRACING)
94029406
return 0;
@@ -9405,19 +9409,47 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
94059409
verbose(env, "Tracing programs must provide btf_id\n");
94069410
return -EINVAL;
94079411
}
9408-
t = btf_type_by_id(btf_vmlinux, btf_id);
9412+
btf = bpf_prog_get_target_btf(prog);
9413+
if (!btf) {
9414+
verbose(env,
9415+
"FENTRY/FEXIT program can only be attached to another program annotated with BTF\n");
9416+
return -EINVAL;
9417+
}
9418+
t = btf_type_by_id(btf, btf_id);
94099419
if (!t) {
94109420
verbose(env, "attach_btf_id %u is invalid\n", btf_id);
94119421
return -EINVAL;
94129422
}
9413-
tname = btf_name_by_offset(btf_vmlinux, t->name_off);
9423+
tname = btf_name_by_offset(btf, t->name_off);
94149424
if (!tname) {
94159425
verbose(env, "attach_btf_id %u doesn't have a name\n", btf_id);
94169426
return -EINVAL;
94179427
}
9428+
if (tgt_prog) {
9429+
struct bpf_prog_aux *aux = tgt_prog->aux;
9430+
9431+
for (i = 0; i < aux->func_info_cnt; i++)
9432+
if (aux->func_info[i].type_id == btf_id) {
9433+
subprog = i;
9434+
break;
9435+
}
9436+
if (subprog == -1) {
9437+
verbose(env, "Subprog %s doesn't exist\n", tname);
9438+
return -EINVAL;
9439+
}
9440+
conservative = aux->func_info_aux[subprog].unreliable;
9441+
key = ((u64)aux->id) << 32 | btf_id;
9442+
} else {
9443+
key = btf_id;
9444+
}
94189445

94199446
switch (prog->expected_attach_type) {
94209447
case BPF_TRACE_RAW_TP:
9448+
if (tgt_prog) {
9449+
verbose(env,
9450+
"Only FENTRY/FEXIT progs are attachable to another BPF prog\n");
9451+
return -EINVAL;
9452+
}
94219453
if (!btf_type_is_typedef(t)) {
94229454
verbose(env, "attach_btf_id %u is not a typedef\n",
94239455
btf_id);
@@ -9429,11 +9461,11 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
94299461
return -EINVAL;
94309462
}
94319463
tname += sizeof(prefix) - 1;
9432-
t = btf_type_by_id(btf_vmlinux, t->type);
9464+
t = btf_type_by_id(btf, t->type);
94339465
if (!btf_type_is_ptr(t))
94349466
/* should never happen in valid vmlinux build */
94359467
return -EINVAL;
9436-
t = btf_type_by_id(btf_vmlinux, t->type);
9468+
t = btf_type_by_id(btf, t->type);
94379469
if (!btf_type_is_func_proto(t))
94389470
/* should never happen in valid vmlinux build */
94399471
return -EINVAL;
@@ -9452,30 +9484,51 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
94529484
btf_id);
94539485
return -EINVAL;
94549486
}
9455-
t = btf_type_by_id(btf_vmlinux, t->type);
9487+
t = btf_type_by_id(btf, t->type);
94569488
if (!btf_type_is_func_proto(t))
94579489
return -EINVAL;
9458-
tr = bpf_trampoline_lookup(btf_id);
9490+
tr = bpf_trampoline_lookup(key);
94599491
if (!tr)
94609492
return -ENOMEM;
94619493
prog->aux->attach_func_name = tname;
9494+
/* t is either vmlinux type or another program's type */
94629495
prog->aux->attach_func_proto = t;
94639496
mutex_lock(&tr->mutex);
94649497
if (tr->func.addr) {
94659498
prog->aux->trampoline = tr;
94669499
goto out;
94679500
}
9468-
ret = btf_distill_func_proto(&env->log, btf_vmlinux, t,
9501+
if (tgt_prog && conservative) {
9502+
prog->aux->attach_func_proto = NULL;
9503+
t = NULL;
9504+
}
9505+
ret = btf_distill_func_proto(&env->log, btf, t,
94699506
tname, &tr->func.model);
94709507
if (ret < 0)
94719508
goto out;
9472-
addr = kallsyms_lookup_name(tname);
9473-
if (!addr) {
9474-
verbose(env,
9475-
"The address of function %s cannot be found\n",
9476-
tname);
9477-
ret = -ENOENT;
9478-
goto out;
9509+
if (tgt_prog) {
9510+
if (!tgt_prog->jited) {
9511+
/* for now */
9512+
verbose(env, "Can trace only JITed BPF progs\n");
9513+
ret = -EINVAL;
9514+
goto out;
9515+
}
9516+
if (tgt_prog->type == BPF_PROG_TYPE_TRACING) {
9517+
/* prevent cycles */
9518+
verbose(env, "Cannot recursively attach\n");
9519+
ret = -EINVAL;
9520+
goto out;
9521+
}
9522+
addr = (long) tgt_prog->aux->func[subprog]->bpf_func;
9523+
} else {
9524+
addr = kallsyms_lookup_name(tname);
9525+
if (!addr) {
9526+
verbose(env,
9527+
"The address of function %s cannot be found\n",
9528+
tname);
9529+
ret = -ENOENT;
9530+
goto out;
9531+
}
94799532
}
94809533
tr->func.addr = (void *)addr;
94819534
prog->aux->trampoline = tr;

0 commit comments

Comments
 (0)