bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: KP Singh <kpsingh@chromium.org>
Cc: open list <linux-kernel@vger.kernel.org>,
	bpf <bpf@vger.kernel.org>,
	linux-security-module@vger.kernel.org,
	Brendan Jackman <jackmanb@google.com>,
	Florent Revest <revest@google.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	James Morris <jmorris@namei.org>,
	Kees Cook <keescook@chromium.org>, Paul Turner <pjt@google.com>,
	Jann Horn <jannh@google.com>,
	Florent Revest <revest@chromium.org>,
	Brendan Jackman <jackmanb@chromium.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: Re: [PATCH bpf-next v5 3/7] bpf: lsm: provide attachment points for BPF LSM programs
Date: Tue, 24 Mar 2020 14:26:55 -0700	[thread overview]
Message-ID: <CAEf4BzZZLBf3xRsV4khGCFdTxDFV61KbFfV1mHwM5yiCr4P37w@mail.gmail.com> (raw)
In-Reply-To: <20200324161211.GA11227@chromium.org>

On Tue, Mar 24, 2020 at 9:12 AM KP Singh <kpsingh@chromium.org> wrote:
>
> On 24-Mär 11:39, KP Singh wrote:
> > On 23-Mär 12:59, Andrii Nakryiko wrote:
> > > On Mon, Mar 23, 2020 at 9:45 AM KP Singh <kpsingh@chromium.org> wrote:
> > > >
> > > > From: KP Singh <kpsingh@google.com>
> > > >
> > > > When CONFIG_BPF_LSM is enabled, nops functions, bpf_lsm_<hook_name>, are
> > > > generated for each LSM hook. These nops are initialized as LSM hooks in
> > > > a subsequent patch.
> > > >
> > > > Signed-off-by: KP Singh <kpsingh@google.com>
> > > > Reviewed-by: Brendan Jackman <jackmanb@google.com>
> > > > Reviewed-by: Florent Revest <revest@google.com>
> > > > ---
> > > >  include/linux/bpf_lsm.h | 21 +++++++++++++++++++++
> > > >  kernel/bpf/bpf_lsm.c    | 19 +++++++++++++++++++
> > > >  2 files changed, 40 insertions(+)
> > > >  create mode 100644 include/linux/bpf_lsm.h
> > > >
> > > > diff --git a/include/linux/bpf_lsm.h b/include/linux/bpf_lsm.h
> > > > new file mode 100644
> > > > index 000000000000..c6423a140220
> > > > --- /dev/null
> > > > +++ b/include/linux/bpf_lsm.h
> > > > @@ -0,0 +1,21 @@
> > > > +/* SPDX-License-Identifier: GPL-2.0 */
> > > > +
> > > > +/*
> > > > + * Copyright (C) 2020 Google LLC.
> > > > + */
> > > > +
> > > > +#ifndef _LINUX_BPF_LSM_H
> > > > +#define _LINUX_BPF_LSM_H
> > > > +
> > > > +#include <linux/bpf.h>
> > > > +#include <linux/lsm_hooks.h>
> > > > +
> > > > +#ifdef CONFIG_BPF_LSM
> > > > +
> > > > +#define LSM_HOOK(RET, NAME, ...) RET bpf_lsm_##NAME(__VA_ARGS__);
> > > > +#include <linux/lsm_hook_names.h>
> > > > +#undef LSM_HOOK
> > > > +
> > > > +#endif /* CONFIG_BPF_LSM */
> > > > +
> > > > +#endif /* _LINUX_BPF_LSM_H */
> > > > diff --git a/kernel/bpf/bpf_lsm.c b/kernel/bpf/bpf_lsm.c
> > > > index 82875039ca90..530d137f7a84 100644
> > > > --- a/kernel/bpf/bpf_lsm.c
> > > > +++ b/kernel/bpf/bpf_lsm.c
> > > > @@ -7,6 +7,25 @@
> > > >  #include <linux/filter.h>
> > > >  #include <linux/bpf.h>
> > > >  #include <linux/btf.h>
> > > > +#include <linux/lsm_hooks.h>
> > > > +#include <linux/bpf_lsm.h>
> > > > +
> > > > +/* For every LSM hook  that allows attachment of BPF programs, declare a NOP
> > > > + * function where a BPF program can be attached as an fexit trampoline.
> > > > + */
> > > > +#define LSM_HOOK(RET, NAME, ...) LSM_HOOK_##RET(NAME, __VA_ARGS__)
> > > > +
> > > > +#define LSM_HOOK_int(NAME, ...)                        \
> > > > +noinline __weak int bpf_lsm_##NAME(__VA_ARGS__)        \
> > > > +{                                              \
> > > > +       return 0;                               \
> > > > +}
> > > > +
> > > > +#define LSM_HOOK_void(NAME, ...) \
> > > > +noinline __weak void bpf_lsm_##NAME(__VA_ARGS__) {}
> > > > +
> > >
> > > Could unify with:
> > >
> > > #define LSM_HOOK(RET, NAME, ...) noinline __weak RET bpf_lsm_##NAME(__VA_ARGS__)
> > > {
> > >     return (RET)0;
> > > }
> > >
> > > then you don't need LSM_HOOK_int and LSM_HOOK_void.
> >
> > Nice.
> >
> > But, given that we are adding default values and that
> > they are only needed for int hooks, we will need to keep the macros
> > separate for int and void. Or, Am I missing a trick here?
> >
> > - KP
>
> Actually, was able to get it work. not setting a default for void
> hooks makes the macros messier. So i just set it void. For example:
>
>   LSM_HOOK(void, void, bprm_committing_creds, struct linux_binprm *bprm)

surprised this works, was going to propose to specify `(void)0` as
default value :)

>
> This also allows me to use the cleanup you suggested and not having
> to split every usage into int and void.
>

Nice, one of the reasons for proposing this.

> - KP
>
> >
> > >
> > > > +#include <linux/lsm_hook_names.h>
> > > > +#undef LSM_HOOK
> > > >
> > > >  const struct bpf_prog_ops lsm_prog_ops = {
> > > >  };
> > > > --
> > > > 2.20.1
> > > >

  reply	other threads:[~2020-03-24 21:27 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-23 16:44 [PATCH bpf-next v5 0/8] MAC and Audit policy using eBPF (KRSI) KP Singh
2020-03-23 16:44 ` [PATCH bpf-next v5 1/7] bpf: Introduce BPF_PROG_TYPE_LSM KP Singh
2020-03-23 19:02   ` Yonghong Song
2020-03-23 16:44 ` [PATCH bpf-next v5 2/7] security: Refactor declaration of LSM hooks KP Singh
2020-03-23 19:33   ` Kees Cook
2020-03-23 19:56   ` Andrii Nakryiko
2020-03-24 16:06     ` KP Singh
2020-03-23 16:44 ` [PATCH bpf-next v5 3/7] bpf: lsm: provide attachment points for BPF LSM programs KP Singh
2020-03-23 19:04   ` Yonghong Song
2020-03-23 19:33   ` Kees Cook
2020-03-23 19:59   ` Andrii Nakryiko
2020-03-24 10:39     ` KP Singh
2020-03-24 16:12       ` KP Singh
2020-03-24 21:26         ` Andrii Nakryiko [this message]
2020-03-24 22:39           ` KP Singh
2020-03-23 16:44 ` [PATCH bpf-next v5 4/7] bpf: lsm: Implement attach, detach and execution KP Singh
2020-03-23 19:16   ` Yonghong Song
2020-03-23 19:44     ` KP Singh
2020-03-23 20:18   ` Andrii Nakryiko
2020-03-24 19:00     ` KP Singh
2020-03-24 14:35   ` Stephen Smalley
2020-03-24 14:50     ` KP Singh
2020-03-24 14:58       ` Stephen Smalley
2020-03-24 16:25         ` Casey Schaufler
2020-03-24 17:49           ` Stephen Smalley
2020-03-24 18:01             ` Kees Cook
2020-03-24 18:06               ` KP Singh
2020-03-24 18:21                 ` Stephen Smalley
2020-03-24 18:27                   ` KP Singh
2020-03-24 18:31                     ` KP Singh
2020-03-24 18:34                       ` Kees Cook
2020-03-24 18:33                   ` Kees Cook
2020-03-23 16:44 ` [PATCH bpf-next v5 5/7] bpf: lsm: Initialize the BPF LSM hooks KP Singh
2020-03-23 19:44   ` Kees Cook
2020-03-23 19:47     ` KP Singh
2020-03-23 20:21       ` Andrii Nakryiko
2020-03-23 20:47     ` Casey Schaufler
2020-03-23 21:44       ` Kees Cook
2020-03-24  1:13   ` Casey Schaufler
2020-03-24  1:52     ` KP Singh
2020-03-24 14:37       ` Stephen Smalley
2020-03-24 14:42         ` KP Singh
2020-03-24 14:51           ` Stephen Smalley
2020-03-24 14:51             ` KP Singh
2020-03-24 17:57               ` Kees Cook
2020-03-23 16:44 ` [PATCH bpf-next v5 6/7] tools/libbpf: Add support for BPF_PROG_TYPE_LSM KP Singh
2020-03-23 19:21   ` Yonghong Song
2020-03-23 20:25   ` Andrii Nakryiko
2020-03-24  1:57     ` KP Singh
2020-03-23 16:44 ` [PATCH bpf-next v5 7/7] bpf: lsm: Add selftests " KP Singh
2020-03-23 20:04   ` Yonghong Song
2020-03-24 20:04     ` KP Singh
2020-03-24 23:54   ` Andrii Nakryiko
2020-03-25  0:36     ` 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=CAEf4BzZZLBf3xRsV4khGCFdTxDFV61KbFfV1mHwM5yiCr4P37w@mail.gmail.com \
    --to=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=jackmanb@chromium.org \
    --cc=jackmanb@google.com \
    --cc=jannh@google.com \
    --cc=jmorris@namei.org \
    --cc=keescook@chromium.org \
    --cc=kpsingh@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=pjt@google.com \
    --cc=revest@chromium.org \
    --cc=revest@google.com \
    /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).