Netdev Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
To: Kris Van Hees <kris.van.hees@oracle.com>
Cc: "David S. Miller" <davem@davemloft.net>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Network Development <netdev@vger.kernel.org>,
	bpf <bpf@vger.kernel.org>, Kernel Team <kernel-team@fb.com>
Subject: Re: [PATCH v7 bpf-next 08/11] bpf: Implement verifier support for validation of async callbacks.
Date: Tue, 4 Jan 2022 10:19:25 -0800	[thread overview]
Message-ID: <CAADnVQ+MAWVmXoDYx6XOaqbnit2kSE9wx5ejEAW0ZTjrcsF=9A@mail.gmail.com> (raw)
In-Reply-To: <20220104171557.GB1559@oracle.com>

On Tue, Jan 4, 2022 at 9:16 AM Kris Van Hees <kris.van.hees@oracle.com> wrote:
>
> I ran into a problem due to this patch.  Specifically, the test in the
> __check_func_call() function is flaweed because it can actually mis-interpret
> a regular BPF-to-BPF pseudo-call as a callback call.
>
> Consider the conditional in the code:
>
>         if (insn->code == (BPF_JMP | BPF_CALL) &&
>             insn->imm == BPF_FUNC_timer_set_callback) {
>
> The BPF_FUNC_timer_set_callback has value 170.  This means that if you have
> a BPF program that contains a pseudo-call with an instruction delta of 170,
> this conditional will be found to be true by the verifier, and it will
> interpret the pseudo-call as a callback.  This leads to a mess with the
> verification of the program because it makes the wrong assumptions about the
> nature of this call.
>
> As far as I can see, the solution is simple.  Include an explicit check to
> ensure that src_reg is not a pseudo-call.  I.e. make the conditional:
>
>         if (insn->code == (BPF_JMP | BPF_CALL) &&
>             insn->src_reg != BPF_PSEUDO_CALL &&
>             insn->imm == BPF_FUNC_timer_set_callback) {
>
> It is of course a pretty rare case that this would go wrong, but since my
> code makes extensive use of BPF-to-BPF pseudo-calls, it was only a matter of
> time before I would run into a call with instruction delta 170.

Great catch. All makes sense.
Could you please submit an official patch ?
Checking for insn->src_reg == 0 is probably better,
since src_reg can be BPF_PSEUDO_KFUNC_CALL as well
though __check_func_call is not called for it.

  reply	other threads:[~2022-01-04 18:19 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-15  0:54 [PATCH v7 bpf-next 00/11] bpf: Introduce BPF timers Alexei Starovoitov
2021-07-15  0:54 ` [PATCH v7 bpf-next 01/11] bpf: Prepare bpf_prog_put() to be called from irq context Alexei Starovoitov
2021-07-15  0:54 ` [PATCH v7 bpf-next 02/11] bpf: Factor out bpf_spin_lock into helpers Alexei Starovoitov
2021-07-15  0:54 ` [PATCH v7 bpf-next 03/11] bpf: Introduce bpf timers Alexei Starovoitov
2021-07-15  0:54 ` [PATCH v7 bpf-next 04/11] bpf: Add map side support for " Alexei Starovoitov
2021-07-15  0:54 ` [PATCH v7 bpf-next 05/11] bpf: Prevent pointer mismatch in bpf_timer_init Alexei Starovoitov
2021-07-15  0:54 ` [PATCH v7 bpf-next 06/11] bpf: Remember BTF of inner maps Alexei Starovoitov
2021-07-15  0:54 ` [PATCH v7 bpf-next 07/11] bpf: Relax verifier recursion check Alexei Starovoitov
2021-07-15  0:54 ` [PATCH v7 bpf-next 08/11] bpf: Implement verifier support for validation of async callbacks Alexei Starovoitov
2022-01-04 17:15   ` Kris Van Hees
2022-01-04 18:19     ` Alexei Starovoitov [this message]
2021-07-15  0:54 ` [PATCH v7 bpf-next 09/11] bpf: Teach stack depth check about " Alexei Starovoitov
2021-07-15  0:54 ` [PATCH v7 bpf-next 10/11] selftests/bpf: Add bpf_timer test Alexei Starovoitov
2021-07-15  0:54 ` [PATCH v7 bpf-next 11/11] selftests/bpf: Add a test with bpf_timer in inner map Alexei Starovoitov
2021-07-15 20:40 ` [PATCH v7 bpf-next 00/11] bpf: Introduce BPF timers patchwork-bot+netdevbpf

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='CAADnVQ+MAWVmXoDYx6XOaqbnit2kSE9wx5ejEAW0ZTjrcsF=9A@mail.gmail.com' \
    --to=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=kernel-team@fb.com \
    --cc=kris.van.hees@oracle.com \
    --cc=netdev@vger.kernel.org \
    --subject='Re: [PATCH v7 bpf-next 08/11] bpf: Implement verifier support for validation of async callbacks.' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).