From: Eduard Zingerman <eddyz87@gmail.com>
To: Leon Hwang <leon.hwang@linux.dev>, bpf@vger.kernel.org
Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
toke@redhat.com, martin.lau@kernel.org, yonghong.song@linux.dev,
puranjay@kernel.org, xukuohai@huaweicloud.com,
iii@linux.ibm.com, kernel-patches-bot@fb.com
Subject: Re: [PATCH bpf-next v3 1/4] bpf: Prevent updating extended prog to prog_array map
Date: Fri, 27 Sep 2024 05:23:28 -0700 [thread overview]
Message-ID: <1b718d0893bc51ddfa4982aa45894b78cee8858b.camel@gmail.com> (raw)
In-Reply-To: <aac4921a-3f09-4c62-af92-df9f8412dcf6@linux.dev>
On Thu, 2024-09-26 at 15:16 +0800, Leon Hwang wrote:
[...]
> There's no protection against concurrent update.
>
> > Sequence of actions in bpf_tracing_prog_attach():
> > a. call bpf_trampoline_link_prog(&link->link, tr)
> > this returns success if `tr->extension_prog` is NULL,
> > meaning trampoline is "free";
> > b. update tgt_prog->aux->is_extended = true.
> >
> > Sequence of actions in bpf_tracing_link_release():
> > c. call bpf_trampoline_unlink_prog(&tr_link->link, tr_link->trampoline)
> > this sets `tr->extension_prog` to NULL;
> > d. update tr_link->tgt_prog->aux->is_extended = false.
> >
> > In a concurrent environment, is it possible to have actions ordered as:
> > - thread #1: does bpf_tracing_link_release(link pointing to tgt_prog)
> > - thread #2: does bpf_tracing_prog_attach(some_prog, tgt_prog)
> > - thread #1: (c) tr->extension_prog is set to NULL
> > - thread #2: (a) tr->extension_prog is set to some_prog
> > - thread #2: (b) tgt_prog->aux->is_extended = true
> > - thread #1: (d) tgt_prog->aux->is_extended = false
> >
> > Thus, loosing the is_extended mark?
>
> Yes, you are correct.
>
> >
> > (As far as I understand bpf_trampoline_compute_key() call in
> > bpf_tracing_prog_attach() it is possible for threads #1 and #2 to
> > operate on a same trampoline object).
> >
>
> In order to avoid the above case:
>
> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index 6988e432fc3d..1f19b754623c 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -3281,6 +3281,9 @@ static void bpf_tracing_link_release(struct
> bpf_link *link)
> struct bpf_tracing_link *tr_link =
> container_of(link, struct bpf_tracing_link, link.link);
>
> + if (link->prog->type == BPF_PROG_TYPE_EXT)
> + tr_link->tgt_prog->aux->is_extended = false;
> +
Isn't this too early to reset 'is_extended'?
E.g. consider scenario:
- thread #1 enters bpf_tracing_link_release() and sets is_extended == false
- thread #2 enters prog_fd_array_get_ptr(), is_extended is false,
and it proceeds putting tgt_prog to an array;
- execution of tgt_prog is triggered and freplace patch is still in effect,
because thread #1 had not finished bpf_tracing_link_release() yet.
Here same bug we are trying to protect against (circular tailcall)
is still potentially visible, isn't it?
> WARN_ON_ONCE(bpf_trampoline_unlink_prog(&tr_link->link,
> tr_link->trampoline));
>
> @@ -3518,6 +3521,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog
> *prog,
> if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
> /* we allocated a new trampoline, so free the old one */
> bpf_trampoline_put(prog->aux->dst_trampoline);
> + if (prog->type == BPF_PROG_TYPE_EXT)
> + tgt_prog->aux->is_extended = true;
>
> prog->aux->dst_prog = NULL;
> prog->aux->dst_trampoline = NULL;
>
> In bpf_tracing_link_release():
> 1. update tr_link->tgt_prog->aux->is_extended = false.
> 2. bpf_trampoline_unlink_prog().
>
> In bpf_tracing_prog_attach():
> 1. bpf_trampoline_link_prog().
> 2. update tgt_prog->aux->is_extended = true.
>
> Then, it is able to avoid losing the is_extended mark.
>
> Thanks,
> Leon
>
next prev parent reply other threads:[~2024-09-27 12:23 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-23 13:40 [PATCH bpf-next v3 0/4] bpf: Fix tailcall infinite loop caused by freplace Leon Hwang
2024-09-23 13:40 ` [PATCH bpf-next v3 1/4] bpf: Prevent updating extended prog to prog_array map Leon Hwang
2024-09-25 1:24 ` Eduard Zingerman
2024-09-26 7:16 ` Leon Hwang
2024-09-27 12:23 ` Eduard Zingerman [this message]
2024-09-23 13:40 ` [PATCH bpf-next v3 2/4] bpf: Prevent extending tail callee prog with freplace Leon Hwang
2024-09-25 5:32 ` Eduard Zingerman
2024-09-26 7:19 ` Leon Hwang
2024-09-27 10:58 ` Eduard Zingerman
2024-09-23 13:40 ` [PATCH bpf-next v3 3/4] selftests/bpf: Add a test case to confirm a tailcall infinite loop issue has been prevented Leon Hwang
2024-09-23 13:40 ` [PATCH bpf-next v3 4/4] selftests/bpf: Add cases to test tailcall in freplace Leon Hwang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1b718d0893bc51ddfa4982aa45894b78cee8858b.camel@gmail.com \
--to=eddyz87@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=iii@linux.ibm.com \
--cc=kernel-patches-bot@fb.com \
--cc=leon.hwang@linux.dev \
--cc=martin.lau@kernel.org \
--cc=puranjay@kernel.org \
--cc=toke@redhat.com \
--cc=xukuohai@huaweicloud.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox