bpf: Relax tracing prog recursive attach rules
RFC PATCH 1/1 bpf: Relax tracing prog recursive attach rules
Currently, it's not allowed to attach an fentry/fexit prog to another one of the same type. At the same time it's not uncommon to see a tracing program with lots of logic in use, and the attachment limitation prevents usage of fentry/fexit for performance analysis (e.g. with "bpftool prog profile" command) in this case. An example could be falcosecurity libs project that uses tp_btf tracing programs.
Following the corresponding discussion [1], the reason for that is to avoid tracing progs call cycles without introducing more complex solutions. Relax "no same type" requirement to "no progs that are already an attach target themselves" for the tracing type. In this way only a standalone tracing program (without any other progs attached to it) could be attached to another one, and no cycle could be formed. To implement, add a new field into bpf_prog_aux to track the fact of attachment in the target prog.
As a side effect of this change alone, one could create an unbounded chain of tracing progs attached to each other. Similar issues between fentry/fexit and extend progs are addressed via forbidding certain combinations that could lead to similar chains. Introduce an attach_depth field to limit the attachment chain, and display it in bpftool.
[1]: https://lore.kernel.org/bpf/[email protected]/
--- include/linux/bpf.h | 2 ++ include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 8 +++++++- kernel/bpf/verifier.c | 19 ++++++++++++++++--- tools/bpf/bpftool/prog.c | 2 ++ tools/include/uapi/linux/bpf.h | 1 + 6 files changed, 29 insertions(+), 4 deletions(-)
include/linux/bpf.h +2 -0
@@ -1394,6 +1394,7 @@ struct bpf_prog_aux {u32 real_func_cnt;/* includes hidden progs, only used for JIT and freeing progs */ u32 func_idx;/* 0 for non-func prog, the index in func array for func prog */ u32 attach_btf_id;/* in-kernel BTF type id to attach to */ +u32 attach_depth;/* position of the prog in the attachment chain */ u32 ctx_arg_info_size;u32 max_rdonly_access;u32 max_rdwr_access;
@@ -1414,6 +1415,7 @@ struct bpf_prog_aux {bool xdp_has_frags;bool exception_cb;bool exception_boundary; +bool followed;/* Another program is attached to it. */ /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */ const struct btf_type *attach_func_proto;/* function name for valid attach_btf_id */
include/uapi/linux/bpf.h +1 -0
@@ -6465,6 +6465,7 @@ struct bpf_prog_info {__u32 verified_insns;__u32 attach_btf_obj_id;__u32 attach_btf_id; +__u32 attach_depth; } __attribute__((aligned(8)));struct bpf_map_info {
kernel/bpf/syscall.c +7 -1
@@ -3038,9 +3038,12 @@ static void bpf_tracing_link_release(struct bpf_link *link)bpf_trampoline_put (tr_link->trampoline); + link->prog->aux->attach_depth--;/* tgt_prog is NULL if target is a kernel function */ -if (tr_link->tgt_prog) +if (tr_link->tgt_prog) { + tr_link->tgt_prog->aux->followed = false;bpf_prog_put (tr_link->tgt_prog); + } }static void bpf_tracing_link_dealloc (struct bpf_link *link)
@@ -3235,6 +3238,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,if (err) goto out_unlock; + tgt_prog->aux->followed = true; + prog->aux->attach_depth = tgt_prog->aux->attach_depth +1 ; err =bpf_trampoline_link_prog (&link->link, tr);if (err) {bpf_link_cleanup (&link_primer);
@@ -4509,6 +4514,7 @@ static int bpf_prog_get_info_by_fd(struct file *file,if (prog->aux->btf) info.btf_id =btf_obj_id (prog->aux->btf); info.attach_btf_id = prog->aux->attach_btf_id; + info.attach_depth = prog->aux->attach_depth;if (attach_btf) info.attach_btf_obj_id =btf_obj_id (attach_btf);
kernel/bpf/verifier.c +16 -3
@@ -20329,6 +20329,12 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,if (tgt_prog) {struct bpf_prog_aux *aux = tgt_prog->aux; +if (aux->attach_depth >=32 ) { +bpf_log (log,"Target program attach depth is %d. Too large\n" , + aux->attach_depth); +return -EINVAL ; + } +if (bpf_prog_is_dev_bound (prog->aux) && !bpf_prog_dev_bound_match (prog, tgt_prog)) {bpf_log (log,"Target program bound device mismatch" );
@@ -20367,9 +20373,16 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,bpf_log (log,"Can attach to only JITed progs\n" );return -EINVAL ; } -if (tgt_prog->type == prog->type) { -/* Cannot fentry/fexit another fentry/fexit program. -* Cannot attach program extension to another extension. +if (tgt_prog->type == prog->type && + (prog_extension || prog->aux->followed)) { +/* +* To avoid potential call chain cycles, prevent attaching programs +* of the same type. The only exception is standalone fentry/fexit +* programs that themselves are not attachment targets. +* That means: +* - Cannot attach followed fentry/fexit to another +* fentry/fexit program. +* - Cannot attach program extension to another extension. * It's ok to attach fentry/fexit to extension program. */ bpf_log (log,"Cannot recursively attach\n" );
tools/bpf/bpftool/prog.c +2 -0
@@ -554,6 +554,8 @@ static void print_prog_plain(struct bpf_prog_info *info, int fd)printf (" memlock %sB" , memlock);free (memlock); +printf (" attach depth %d" , info->attach_depth); +if (info->nr_map_ids)show_prog_maps (fd, info->nr_map_ids);
tools/include/uapi/linux/bpf.h +1 -0
@@ -6465,6 +6465,7 @@ struct bpf_prog_info {__u32 verified_insns;__u32 attach_btf_obj_id;__u32 attach_btf_id; +__u32 attach_depth; } __attribute__((aligned(8)));struct bpf_map_info {
base-commit: 100888fb6d8a185866b1520031ee7e3182b173de
Signature · 1 line
2.41.0
Quoted 17 lines · Dmitrii Dolgov
Currently, it's not allowed to attach an fentry/fexit prog to another one of the same type. At the same time it's not uncommon to see a tracing program with lots of logic in use, and the attachment limitation prevents usage of fentry/fexit for performance analysis (e.g. with "bpftool prog profile" command) in this case. An example could be falcosecurity libs project that uses
tp_btftracing programs.Following the corresponding discussion [1], the reason for that is to avoid tracing progs call cycles without introducing more complex solutions. Relax "no same type" requirement to "no progs that are already an attach target themselves" for the tracing type. In this way only a standalone tracing program (without any other progs attached to it) could be attached to another one, and no cycle could be formed. To implement, add a new field into
bpf_prog_auxto track the fact of attachment in the target prog.
Quoted 6 lines
As a side effect of this change alone, one could create an unbounded chain of tracing progs attached to each other. Similar issues between fentry/fexit and extend progs are addressed via forbidding certain combinations that could lead to similar chains. Introduce an
attach_depthfield to limit the attachment chain, and display it in bpftool.
From what I see currently it's not possible to achieve such a call cycle with tracing programs (even without the verifier check addressed in this patch), so I had to test this change on a modified kernel with a couple of other checks disabled as well. But otherwise I would appreciate feedback about whether it's a reasonable thing to do.
Hi Dmitrii,
kernel test robot noticed the following build warnings:
url: https://github.com/intel-lab-lkp/linux/commits/Dmitrii-Dolgov/bpf-Relax-tracing-prog-recursive-attach-rules/20231114-164631 base: 100888fb6d8a185866b1520031ee7e3182b173de patch link: https://lore.kernel.org/r/20231114084118.11095-1-9erthalion6%40gmail.com patch subject: [RFC PATCH bpf-next] bpf: Relax tracing prog recursive attach rules config: i386-randconfig-141-20231119 (https://download.01.org/0day-ci/archive/20231119/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce: (https://download.01.org/0day-ci/archive/20231119/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]> | Reported-by: Dan Carpenter <[email protected]> | Closes: https://lore.kernel.org/r/[email protected]/
smatch warnings:kernel/bpf/syscall.c:3241 bpf_tracing_prog_attach() error: we previously assumed 'tgt_prog' could be null (see line 3186)
vim +/tgt_prog +3241 kernel/bpf/syscall.c
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3096 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3097 int tgt_prog_fd,
2fcc82411e74e5 Kui-Feng Lee 2022-05-10 3098 u32 btf_id,
2fcc82411e74e5 Kui-Feng Lee 2022-05-10 3099 u64 bpf_cookie)
fec56f5890d93f Alexei Starovoitov 2019-11-14 3100 {
a3b80e1078943d Andrii Nakryiko 2020-04-28 3101 struct bpf_link_primer link_primer;
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3102 struct bpf_prog *tgt_prog = NULL;
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3103 struct bpf_trampoline *tr = NULL;
70ed506c3bbcfa Andrii Nakryiko 2020-03-02 3104 struct bpf_tracing_link *link;
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3105 u64 key = 0;
a3b80e1078943d Andrii Nakryiko 2020-04-28 3106 int err;
fec56f5890d93f Alexei Starovoitov 2019-11-14 3107
9e4e01dfd3254c KP Singh 2020-03-29 3108 switch (prog->type) {
9e4e01dfd3254c KP Singh 2020-03-29 3109 case BPF_PROG_TYPE_TRACING:
fec56f5890d93f Alexei Starovoitov 2019-11-14 3110 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
be8704ff07d237 Alexei Starovoitov 2020-01-20 3111 prog->expected_attach_type != BPF_TRACE_FEXIT &&
9e4e01dfd3254c KP Singh 2020-03-29 3112 prog->expected_attach_type != BPF_MODIFY_RETURN) {
9e4e01dfd3254c KP Singh 2020-03-29 3113 err = -EINVAL;
9e4e01dfd3254c KP Singh 2020-03-29 3114 goto out_put_prog;
9e4e01dfd3254c KP Singh 2020-03-29 3115 }
9e4e01dfd3254c KP Singh 2020-03-29 3116 break;
9e4e01dfd3254c KP Singh 2020-03-29 3117 case BPF_PROG_TYPE_EXT:
9e4e01dfd3254c KP Singh 2020-03-29 3118 if (prog->expected_attach_type != 0) {
9e4e01dfd3254c KP Singh 2020-03-29 3119 err = -EINVAL;
9e4e01dfd3254c KP Singh 2020-03-29 3120 goto out_put_prog;
9e4e01dfd3254c KP Singh 2020-03-29 3121 }
9e4e01dfd3254c KP Singh 2020-03-29 3122 break;
9e4e01dfd3254c KP Singh 2020-03-29 3123 case BPF_PROG_TYPE_LSM:
9e4e01dfd3254c KP Singh 2020-03-29 3124 if (prog->expected_attach_type != BPF_LSM_MAC) {
9e4e01dfd3254c KP Singh 2020-03-29 3125 err = -EINVAL;
9e4e01dfd3254c KP Singh 2020-03-29 3126 goto out_put_prog;
9e4e01dfd3254c KP Singh 2020-03-29 3127 }
9e4e01dfd3254c KP Singh 2020-03-29 3128 break;
9e4e01dfd3254c KP Singh 2020-03-29 3129 default:
fec56f5890d93f Alexei Starovoitov 2019-11-14 3130 err = -EINVAL;
fec56f5890d93f Alexei Starovoitov 2019-11-14 3131 goto out_put_prog;
fec56f5890d93f Alexei Starovoitov 2019-11-14 3132 }
fec56f5890d93f Alexei Starovoitov 2019-11-14 3133
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3134 if (!!tgt_prog_fd != !!btf_id) {
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3135 err = -EINVAL;
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3136 goto out_put_prog;
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3137 }
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3138
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3139 if (tgt_prog_fd) {
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3140 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3141 if (prog->type != BPF_PROG_TYPE_EXT) {
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3142 err = -EINVAL;
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3143 goto out_put_prog;
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3144 }
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3145
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3146 tgt_prog = bpf_prog_get(tgt_prog_fd);
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3147 if (IS_ERR(tgt_prog)) {
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3148 err = PTR_ERR(tgt_prog);
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3149 tgt_prog = NULL;
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3150 goto out_put_prog;
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3151 }
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3152
22dc4a0f5ed11b Andrii Nakryiko 2020-12-03 3153 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3154 }
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3155
70ed506c3bbcfa Andrii Nakryiko 2020-03-02 3156 link = kzalloc(sizeof(*link), GFP_USER);
70ed506c3bbcfa Andrii Nakryiko 2020-03-02 3157 if (!link) {
70ed506c3bbcfa Andrii Nakryiko 2020-03-02 3158 err = -ENOMEM;
70ed506c3bbcfa Andrii Nakryiko 2020-03-02 3159 goto out_put_prog;
70ed506c3bbcfa Andrii Nakryiko 2020-03-02 3160 }
f7e0beaf39d386 Kui-Feng Lee 2022-05-10 3161 bpf_link_init(&link->link.link, BPF_LINK_TYPE_TRACING,
f2e10bff16a0fd Andrii Nakryiko 2020-04-28 3162 &bpf_tracing_link_lops, prog);
f2e10bff16a0fd Andrii Nakryiko 2020-04-28 3163 link->attach_type = prog->expected_attach_type;
2fcc82411e74e5 Kui-Feng Lee 2022-05-10 3164 link->link.cookie = bpf_cookie;
70ed506c3bbcfa Andrii Nakryiko 2020-03-02 3165
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3166 mutex_lock(&prog->aux->dst_mutex);
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3167
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3168 /* There are a few possible cases here:
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3169 *
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3170 * - if prog->aux->dst_trampoline is set, the program was just loaded
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3171 * and not yet attached to anything, so we can use the values stored
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3172 * in prog->aux
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3173 *
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3174 * - if prog->aux->dst_trampoline is NULL, the program has already been
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3175 * attached to a target and its initial target was cleared (below)
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3176 *
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3177 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3178 * target_btf_id using the link_create API.
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3179 *
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3180 * - if tgt_prog == NULL when this function was called using the old
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3181 * raw_tracepoint_open API, and we need a target from prog->aux
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3182 *
f3a95075549e0e Jiri Olsa 2021-04-14 3183 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
f3a95075549e0e Jiri Olsa 2021-04-14 3184 * was detached and is going for re-attachment.There is a giant comment here explaining how tgt_prog can be NULL. I read it but I lack some context.
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3185 */
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 @3186 if (!prog->aux->dst_trampoline && !tgt_prog) {
^^^^^^^^^f3a95075549e0e Jiri Olsa 2021-04-14 3187 /*
f3a95075549e0e Jiri Olsa 2021-04-14 3188 * Allow re-attach for TRACING and LSM programs. If it's
f3a95075549e0e Jiri Olsa 2021-04-14 3189 * currently linked, bpf_trampoline_link_prog will fail.
f3a95075549e0e Jiri Olsa 2021-04-14 3190 * EXT programs need to specify tgt_prog_fd, so they
f3a95075549e0e Jiri Olsa 2021-04-14 3191 * re-attach in separate code path.
f3a95075549e0e Jiri Olsa 2021-04-14 3192 */
f3a95075549e0e Jiri Olsa 2021-04-14 3193 if (prog->type != BPF_PROG_TYPE_TRACING &&
f3a95075549e0e Jiri Olsa 2021-04-14 3194 prog->type != BPF_PROG_TYPE_LSM) {
f3a95075549e0e Jiri Olsa 2021-04-14 3195 err = -EINVAL;
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3196 goto out_unlock;
babf3164095b06 Andrii Nakryiko 2020-03-09 3197 }
f3a95075549e0e Jiri Olsa 2021-04-14 3198 btf_id = prog->aux->attach_btf_id;
f3a95075549e0e Jiri Olsa 2021-04-14 3199 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
f3a95075549e0e Jiri Olsa 2021-04-14 3200 }
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3201
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3202 if (!prog->aux->dst_trampoline ||
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3203 (key && key != prog->aux->dst_trampoline->key)) {
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3204 /* If there is no saved target, or the specified target is
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3205 * different from the destination specified at load time, we
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3206 * need a new trampoline and a check for compatibility
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3207 */
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3208 struct bpf_attach_target_info tgt_info = {};
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3209
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3210 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3211 &tgt_info);
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3212 if (err)
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3213 goto out_unlock;
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3214
31bf1dbccfb0a9 Viktor Malik 2023-03-10 3215 if (tgt_info.tgt_mod) {
31bf1dbccfb0a9 Viktor Malik 2023-03-10 3216 module_put(prog->aux->mod);
31bf1dbccfb0a9 Viktor Malik 2023-03-10 3217 prog->aux->mod = tgt_info.tgt_mod;
31bf1dbccfb0a9 Viktor Malik 2023-03-10 3218 }
31bf1dbccfb0a9 Viktor Malik 2023-03-10 3219
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3220 tr = bpf_trampoline_get(key, &tgt_info);
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3221 if (!tr) {
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3222 err = -ENOMEM;
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3223 goto out_unlock;
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3224 }
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3225 } else {
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3226 /* The caller didn't specify a target, or the target was the
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3227 * same as the destination supplied during program load. This
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3228 * means we can reuse the trampoline and reference from program
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3229 * load time, and there is no need to allocate a new one. This
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3230 * can only happen once for any program, as the saved values in
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3231 * prog->aux are cleared below.
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3232 */
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3233 tr = prog->aux->dst_trampoline;
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3234 tgt_prog = prog->aux->dst_prog;tgt_prog is re-assigned here. Maybe the this covers all the NULL cases.
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3235 }3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3236f7e0beaf39d386 Kui-Feng Lee 2022-05-10 3237 err = bpf_link_prime(&link->link.link, &link_primer);
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3238 if (err)
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3239 goto out_unlock;
fec56f5890d93f Alexei Starovoitov 2019-11-14 3240
26a0833438cdde Dmitrii Dolgov 2023-11-14 @3241 tgt_prog->aux->followed = true;
^^^^^^^^^^^^^^^^^^^^^^^Unchecked dereference
26a0833438cdde Dmitrii Dolgov 2023-11-14 3242 prog->aux->attach_depth = tgt_prog->aux->attach_depth + 1;
Second unchecked dereference here
f7e0beaf39d386 Kui-Feng Lee 2022-05-10 3243 err = bpf_trampoline_link_prog(&link->link, tr);
babf3164095b06 Andrii Nakryiko 2020-03-09 3244 if (err) {
a3b80e1078943d Andrii Nakryiko 2020-04-28 3245 bpf_link_cleanup(&link_primer);
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3246 link = NULL;
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3247 goto out_unlock;
fec56f5890d93f Alexei Starovoitov 2019-11-14 3248 }
babf3164095b06 Andrii Nakryiko 2020-03-09 3249
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3250 link->tgt_prog = tgt_prog;
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3251 link->trampoline = tr;
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3252
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3253 /* Always clear the trampoline and target prog from prog->aux to make
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3254 * sure the original attach destination is not kept alive after a
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3255 * program is (re-)attached to another target.
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3256 */
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3257 if (prog->aux->dst_prog &&
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3258 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3259 /* got extra prog ref from syscall, or attaching to different prog */
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3260 bpf_prog_put(prog->aux->dst_prog);
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3261 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3262 /* we allocated a new trampoline, so free the old one */
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3263 bpf_trampoline_put(prog->aux->dst_trampoline);
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3264
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3265 prog->aux->dst_prog = NULL;
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3266 prog->aux->dst_trampoline = NULL;
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3267 mutex_unlock(&prog->aux->dst_mutex);
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3268
a3b80e1078943d Andrii Nakryiko 2020-04-28 3269 return bpf_link_settle(&link_primer);
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3270 out_unlock:
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3271 if (tr && tr != prog->aux->dst_trampoline)
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3272 bpf_trampoline_put(tr);
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3273 mutex_unlock(&prog->aux->dst_mutex);
3aac1ead5eb6b7 Toke Høiland-Jørgensen 2020-09-29 3274 kfree(link);
fec56f5890d93f Alexei Starovoitov 2019-11-14 3275 out_put_prog:
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3276 if (tgt_prog_fd && tgt_prog)
4a1e7c0c63e02d Toke Høiland-Jørgensen 2020-09-29 3277 bpf_prog_put(tgt_prog);
fec56f5890d93f Alexei Starovoitov 2019-11-14 3278 return err;
fec56f5890d93f Alexei Starovoitov 2019-11-14 3279 }
fec56f5890d93f Alexei Starovoitov 2019-11-14 3280 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Quoted 15 lines · Dan Carpenter
Hi Dmitrii,
kernel test robot noticed the following build warnings:
url: https://github.com/intel-lab-lkp/linux/commits/Dmitrii-Dolgov/bpf-Relax-tracing-prog-recursive-attach-rules/20231114-164631 base: 100888fb6d8a185866b1520031ee7e3182b173de patch link: https://lore.kernel.org/r/20231114084118.11095-1-9erthalion6%40gmail.com patch subject: [RFC PATCH bpf-next] bpf: Relax tracing prog recursive attach rules config: i386-randconfig-141-20231119 (https://download.01.org/0day-ci/archive/20231119/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce: (https://download.01.org/0day-ci/archive/20231119/[email protected]/reproduce)If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
Quoted 8 lines
| Reported-by: kernel test robot <[email protected]> | Reported-by: Dan Carpenter <[email protected]> | Closes: https://lore.kernel.org/r/[email protected]/smatch warnings:
kernel/bpf/syscall.c:3241bpf_tracing_prog_attach()error: we previously assumed 'tgt_prog' could be null (see line 3186)vim +/tgt_prog +3241
kernel/bpf/syscall.c
Right, my mistake, thanks for letting me know. Looks like the condition branch where tgt_prog is assigned covers only reattachment, and it will blow when using old raw_tracepoint API. I'm going to send a new version of the patch soon.