bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: KP Singh <kpsingh@chromium.org>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: open list <linux-kernel@vger.kernel.org>,
	bpf <bpf@vger.kernel.org>, Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Paul Turner <pjt@google.com>,
	Florent Revest <revest@chromium.org>,
	Brendan Jackman <jackmanb@chromium.org>
Subject: Re: [PATCH bpf-next 3/7] bpf: Introduce BPF_MODIFY_RETURN
Date: Tue, 3 Mar 2020 23:51:24 +0100	[thread overview]
Message-ID: <20200303225124.GA12968@chromium.org> (raw)
In-Reply-To: <CAEf4BzZVV12WoHDnQSfOKpndr3qVLEAz8itMcdqnQq8Q4njc0w@mail.gmail.com>

On 03-Mär 14:37, Andrii Nakryiko wrote:
> On Tue, Mar 3, 2020 at 6:12 AM KP Singh <kpsingh@chromium.org> wrote:
> >
> > From: KP Singh <kpsingh@google.com>
> >
> > When multiple programs are attached, each program receives the return
> > value from the previous program on the stack and the last program
> > provides the return value to the attached function.
> >
> > The fmod_ret bpf programs are run after the fentry programs and before
> > the fexit programs. The original function is only called if all the
> > fmod_ret programs return 0 to avoid any unintended side-effects. The
> > success value, i.e. 0 is not currently configurable but can be made so
> > where user-space can specify it at load time.
> >
> > For example:
> >
> > int func_to_be_attached(int a, int b)
> > {  <--- do_fentry
> >
> > do_fmod_ret:
> >    <update ret by calling fmod_ret>
> >    if (ret != 0)
> >         goto do_fexit;
> >
> > original_function:
> >
> >     <side_effects_happen_here>
> >
> > }  <--- do_fexit
> >
> > The fmod_ret program attached to this function can be defined as:
> >
> > SEC("fmod_ret/func_to_be_attached")
> > BPF_PROG(func_name, int a, int b, int ret)
> 
> same as on cover letter, return type is missing

Fixed. Thanks!

> 
> > {
> >         // This will skip the original function logic.
> >         return 1;
> > }
> >
> > The first fmod_ret program is passed 0 in its return argument.
> >
> > Signed-off-by: KP Singh <kpsingh@google.com>
> > ---
> >  arch/x86/net/bpf_jit_comp.c    | 96 ++++++++++++++++++++++++++++++++--
> >  include/linux/bpf.h            |  1 +
> >  include/uapi/linux/bpf.h       |  1 +
> >  kernel/bpf/btf.c               |  3 +-
> >  kernel/bpf/syscall.c           |  1 +
> >  kernel/bpf/trampoline.c        |  5 +-
> >  kernel/bpf/verifier.c          |  1 +
> >  tools/include/uapi/linux/bpf.h |  1 +
> >  8 files changed, 103 insertions(+), 6 deletions(-)
> >
> 
> [...]
> 
> >
> > +       if (fmod_ret->nr_progs) {
> > +               branches = kcalloc(fmod_ret->nr_progs, sizeof(u8 *),
> > +                                  GFP_KERNEL);
> > +               if (!branches)
> > +                       return -ENOMEM;
> > +               if (invoke_bpf_mod_ret(m, &prog, fmod_ret, stack_size,
> > +                                      branches))
> 
> branches leaks here

Good catch, sloppy work here by me.

> 
> > +                       return -EINVAL;
> > +       }
> > +
> >         if (flags & BPF_TRAMP_F_CALL_ORIG) {
> > -               if (fentry->nr_progs)
> > +               if (fentry->nr_progs || fmod_ret->nr_progs)
> >                         restore_regs(m, &prog, nr_args, stack_size);
> >
> >                 /* call original function */
> > @@ -1573,6 +1649,14 @@ int arch_prepare_bpf_trampoline(void *image, void *image_end,
> 
> there is early return one line above here, you need to free branches
> in that case to not leak memory
> 
> So I guess it's better to do goto cleanup approach at this point?

yeah, agreed, updated to doing a cleanup at the end.

- KP

> 
> >                 emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8);
> >         }
> >
> > +       if (fmod_ret->nr_progs) {
> > +               align16_branch_target(&prog);
> > +               for (i = 0; i < fmod_ret->nr_progs; i++)
> > +                       emit_cond_near_jump(&branches[i], prog, branches[i],
> > +                                           X86_JNE);
> > +               kfree(branches);
> > +       }
> > +
> 
> [...]

  reply	other threads:[~2020-03-03 22:51 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-03 14:09 [PATCH bpf-next 0/7] Introduce BPF_MODIFY_RET tracing progs KP Singh
2020-03-03 14:09 ` [PATCH bpf-next 1/7] bpf: Refactor trampoline update code KP Singh
2020-03-03 22:12   ` Andrii Nakryiko
2020-03-03 22:24     ` KP Singh
2020-03-03 23:03       ` Andrii Nakryiko
2020-03-03 23:08         ` KP Singh
2020-03-03 14:09 ` [PATCH bpf-next 2/7] bpf: JIT helpers for fmod_ret progs KP Singh
2020-03-03 22:26   ` Andrii Nakryiko
2020-03-03 22:28     ` KP Singh
2020-03-03 23:56       ` Alexei Starovoitov
2020-03-04  1:26         ` KP Singh
2020-03-03 14:09 ` [PATCH bpf-next 3/7] bpf: Introduce BPF_MODIFY_RETURN KP Singh
2020-03-03 22:37   ` Andrii Nakryiko
2020-03-03 22:51     ` KP Singh [this message]
2020-03-03 14:09 ` [PATCH bpf-next 4/7] bpf: Attachment verification for BPF_MODIFY_RETURN KP Singh
2020-03-03 22:44   ` Andrii Nakryiko
2020-03-03 23:21     ` KP Singh
2020-03-04  0:03       ` Alexei Starovoitov
2020-03-04  1:06         ` KP Singh
2020-03-03 14:09 ` [PATCH bpf-next 5/7] tools/libbpf: Add support " KP Singh
2020-03-03 22:45   ` Andrii Nakryiko
2020-03-03 14:09 ` [PATCH bpf-next 6/7] bpf: Add test ops for BPF_PROG_TYPE_TRACING KP Singh
2020-03-03 22:51   ` Andrii Nakryiko
2020-03-03 22:57     ` KP Singh
2020-03-03 14:09 ` [PATCH bpf-next 7/7] bpf: Add selftests for BPF_MODIFY_RETURN KP Singh
2020-03-03 22:58   ` Andrii Nakryiko
2020-03-03 22:12 ` [PATCH bpf-next 0/7] Introduce BPF_MODIFY_RET tracing progs Andrii Nakryiko
2020-03-03 22:25   ` KP Singh

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=20200303225124.GA12968@chromium.org \
    --to=kpsingh@chromium.org \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=jackmanb@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pjt@google.com \
    --cc=revest@chromium.org \
    /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;
as well as URLs for NNTP newsgroup(s).