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 v6 4/8] bpf: lsm: Implement attach, detach and execution
Date: Wed, 25 Mar 2020 18:49:09 -0700 [thread overview]
Message-ID: <CAEf4BzbZ0Y+BXezgbdzN2T1cH9osREJUNQQoQJ5rJ0EYyD-Udg@mail.gmail.com> (raw)
In-Reply-To: <20200325152629.6904-5-kpsingh@chromium.org>
On Wed, Mar 25, 2020 at 8:27 AM KP Singh <kpsingh@chromium.org> wrote:
>
> From: KP Singh <kpsingh@google.com>
>
> JITed BPF programs are dynamically attached to the LSM hooks
> using BPF trampolines. The trampoline prologue generates code to handle
> conversion of the signature of the hook to the appropriate BPF context.
>
> The allocated trampoline programs are attached to the nop functions
> initialized as LSM hooks.
>
> BPF_PROG_TYPE_LSM programs must have a GPL compatible license and
> and need CAP_SYS_ADMIN (required for loading eBPF programs).
>
> Upon attachment:
>
> * A BPF fexit trampoline is used for LSM hooks with a void return type.
> * A BPF fmod_ret trampoline is used for LSM hooks which return an
> int. The attached programs can override the return value of the
> bpf LSM hook to indicate a MAC Policy decision.
>
> 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 | 11 ++++++++
> kernel/bpf/bpf_lsm.c | 28 +++++++++++++++++++++
> kernel/bpf/btf.c | 9 ++++++-
> kernel/bpf/syscall.c | 56 ++++++++++++++++++++++++++++-------------
> kernel/bpf/trampoline.c | 17 ++++++++++---
> kernel/bpf/verifier.c | 19 +++++++++++---
> 6 files changed, 113 insertions(+), 27 deletions(-)
>
[...]
> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index 85567a6ea5f9..3ba30fd6101e 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -25,6 +25,7 @@
> #include <linux/nospec.h>
> #include <linux/audit.h>
> #include <uapi/linux/btf.h>
> +#include <linux/bpf_lsm.h>
>
> #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
> (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
> @@ -1935,6 +1936,7 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
>
> switch (prog_type) {
> case BPF_PROG_TYPE_TRACING:
> + case BPF_PROG_TYPE_LSM:
> case BPF_PROG_TYPE_STRUCT_OPS:
> case BPF_PROG_TYPE_EXT:
> break;
> @@ -2367,10 +2369,28 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog)
> struct file *link_file;
> int link_fd, err;
>
> - if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
> - prog->expected_attach_type != BPF_TRACE_FEXIT &&
> - prog->expected_attach_type != BPF_MODIFY_RETURN &&
> - prog->type != BPF_PROG_TYPE_EXT) {
> + switch (prog->type) {
> + case BPF_PROG_TYPE_TRACING:
> + if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
> + prog->expected_attach_type != BPF_TRACE_FEXIT &&
> + prog->expected_attach_type != BPF_MODIFY_RETURN) {
> + err = -EINVAL;
> + goto out_put_prog;
> + }
> + break;
> + case BPF_PROG_TYPE_EXT:
> + if (prog->expected_attach_type != 0) {
> + err = -EINVAL;
> + goto out_put_prog;
> + }
> + break;
> + case BPF_PROG_TYPE_LSM:
> + if (prog->expected_attach_type != BPF_LSM_MAC) {
> + err = -EINVAL;
> + goto out_put_prog;
> + }
> + break;
> + default:
thanks, this is much more "scalable" in terms of maintenance!
> err = -EINVAL;
> goto out_put_prog;
> }
> @@ -2449,16 +2469,10 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
> if (IS_ERR(prog))
> return PTR_ERR(prog);
>
> - if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
> - prog->type != BPF_PROG_TYPE_TRACING &&
> - prog->type != BPF_PROG_TYPE_EXT &&
> - prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
> - err = -EINVAL;
> - goto out_put_prog;
> - }
> -
> - if (prog->type == BPF_PROG_TYPE_TRACING ||
> - prog->type == BPF_PROG_TYPE_EXT) {
> + switch (prog->type) {
> + case BPF_PROG_TYPE_TRACING:
> + case BPF_PROG_TYPE_EXT:
> + case BPF_PROG_TYPE_LSM:
> if (attr->raw_tracepoint.name) {
> /* The attach point for this category of programs
> * should be specified via btf_id during program load.
> @@ -2466,11 +2480,13 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
> err = -EINVAL;
> goto out_put_prog;
> }
> - if (prog->expected_attach_type == BPF_TRACE_RAW_TP)
> + if (prog->expected_attach_type == BPF_TRACE_RAW_TP) {
this should probably also ensure prog->type == BPF_PROG_TYPE_TRACING ?
Otherwise you can trick kernel with BPF_PROG_TYPE_LSM and
expected_attach_type == BPF_TRACE_RAW_TP, no?
> tp_name = prog->aux->attach_func_name;
> - else
> - return bpf_tracing_prog_attach(prog);
> - } else {
> + break;
> + }
> + return bpf_tracing_prog_attach(prog);
> + case BPF_PROG_TYPE_RAW_TRACEPOINT:
> + case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
> if (strncpy_from_user(buf,
> u64_to_user_ptr(attr->raw_tracepoint.name),
> sizeof(buf) - 1) < 0) {
> @@ -2479,6 +2495,10 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
> }
[...]
next prev parent reply other threads:[~2020-03-26 1:49 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-25 15:26 [PATCH bpf-next v6 0/8] MAC and Audit policy using eBPF (KRSI) KP Singh
2020-03-25 15:26 ` [PATCH bpf-next v6 1/8] bpf: Introduce BPF_PROG_TYPE_LSM KP Singh
2020-03-26 1:51 ` Andrii Nakryiko
2020-03-26 13:11 ` KP Singh
2020-03-25 15:26 ` [PATCH bpf-next v6 2/8] security: Refactor declaration of LSM hooks KP Singh
2020-03-25 22:25 ` Casey Schaufler
2020-03-25 23:46 ` KP Singh
2020-03-25 15:26 ` [PATCH bpf-next v6 3/8] bpf: lsm: provide attachment points for BPF LSM programs KP Singh
2020-03-25 19:28 ` Kees Cook
2020-03-25 19:39 ` KP Singh
2020-03-25 20:07 ` Kees Cook
2020-03-25 20:14 ` KP Singh
2020-03-25 15:26 ` [PATCH bpf-next v6 4/8] bpf: lsm: Implement attach, detach and execution KP Singh
2020-03-26 1:49 ` Andrii Nakryiko [this message]
2020-03-26 13:35 ` KP Singh
2020-03-25 15:26 ` [PATCH bpf-next v6 5/8] bpf: lsm: Initialize the BPF LSM hooks KP Singh
2020-03-25 19:30 ` Kees Cook
2020-03-25 15:26 ` [PATCH bpf-next v6 6/8] tools/libbpf: Add support for BPF_PROG_TYPE_LSM KP Singh
2020-03-26 1:56 ` Andrii Nakryiko
2020-03-25 15:26 ` [PATCH bpf-next v6 7/8] bpf: lsm: Add selftests " KP Singh
2020-03-26 2:01 ` Andrii Nakryiko
2020-03-26 13:36 ` KP Singh
2020-03-25 15:26 ` [PATCH bpf-next v6 8/8] bpf: lsm: Add Documentation KP Singh
2020-03-25 19:24 ` [PATCH bpf-next v6 0/8] MAC and Audit policy using eBPF (KRSI) Kees Cook
2020-03-25 19:42 ` 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=CAEf4BzbZ0Y+BXezgbdzN2T1cH9osREJUNQQoQJ5rJ0EYyD-Udg@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).