* [PATCH bpf-next v4 0/3] destructive bpf_kfuncs
@ 2022-08-09 10:53 Artem Savkov
2022-08-09 10:53 ` [PATCH bpf-next v4 1/3] bpf: add destructive kfunc flag Artem Savkov
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Artem Savkov @ 2022-08-09 10:53 UTC (permalink / raw
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, bpf, netdev
Cc: linux-kernel, Andrea Arcangeli, Daniel Vacek, Jiri Olsa, Song Liu,
Daniel Xu, Kumar Kartikeya Dwivedi, Artem Savkov
eBPF is often used for kernel debugging, and one of the widely used and
powerful debugging techniques is post-mortem debugging with a full memory dump.
Triggering a panic at exactly the right moment allows the user to get such a
dump and thus a better view at the system's state. Right now the only way to
do this in BPF is to signal userspace to trigger kexec/panic. This is
suboptimal as going through userspace requires context changes and adds
significant delays taking system further away from "the right moment". On a
single-cpu system the situation is even worse because BPF program won't even be
able to block the thread of interest.
This patchset tries to solve this problem by allowing properly marked tracing
bpf programs to call crash_kexec() kernel function. The only requirement for
now to run programs calling crash_kexec() or other destructive kfuncs is
CAP_SYS_BOOT capability. When signature checking for bpf programs is available
it is possible that stricter rules will be applied to programs utilizing
destructive kfuncs.
Changes in v4:
- Added description for KF_DESTRUCTIVE flag to documentation
Changes in v3:
- moved kfunc set registration to kernel/bpf/helpers.c
Changes in v2:
- BPF_PROG_LOAD flag dropped as it doesn't fully achieve it's aim of
preventing accidental execution of destructive bpf programs
- selftest moved to the end of patchset
- switched to kfunc destructive flag instead of a separate set
Changes from RFC:
- sysctl knob dropped
- using crash_kexec() instead of panic()
- using kfuncs instead of adding a new helper
Artem Savkov (3):
bpf: add destructive kfunc flag
bpf: export crash_kexec() as destructive kfunc
selftests/bpf: add destructive kfunc test
Documentation/bpf/kfuncs.rst | 9 +++++
include/linux/btf.h | 1 +
kernel/bpf/helpers.c | 21 +++++++++++
kernel/bpf/verifier.c | 5 +++
net/bpf/test_run.c | 5 +++
.../selftests/bpf/prog_tests/kfunc_call.c | 36 +++++++++++++++++++
.../bpf/progs/kfunc_call_destructive.c | 14 ++++++++
7 files changed, 91 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/kfunc_call_destructive.c
--
2.37.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH bpf-next v4 1/3] bpf: add destructive kfunc flag
2022-08-09 10:53 [PATCH bpf-next v4 0/3] destructive bpf_kfuncs Artem Savkov
@ 2022-08-09 10:53 ` Artem Savkov
2022-08-09 12:40 ` Kumar Kartikeya Dwivedi
2022-08-09 10:53 ` [PATCH bpf-next v4 2/3] bpf: export crash_kexec() as destructive kfunc Artem Savkov
2022-08-09 10:53 ` [PATCH bpf-next v4 3/3] selftests/bpf: add destructive kfunc test Artem Savkov
2 siblings, 1 reply; 6+ messages in thread
From: Artem Savkov @ 2022-08-09 10:53 UTC (permalink / raw
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, bpf, netdev
Cc: linux-kernel, Andrea Arcangeli, Daniel Vacek, Jiri Olsa, Song Liu,
Daniel Xu, Kumar Kartikeya Dwivedi, Artem Savkov
Add KF_DESTRUCTIVE flag for destructive functions. Functions with this
flag set will require CAP_SYS_BOOT capabilities.
Signed-off-by: Artem Savkov <asavkov@redhat.com>
---
Documentation/bpf/kfuncs.rst | 9 +++++++++
include/linux/btf.h | 1 +
kernel/bpf/verifier.c | 5 +++++
3 files changed, 15 insertions(+)
diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst
index c0b7dae6dbf5..2e97e08be7de 100644
--- a/Documentation/bpf/kfuncs.rst
+++ b/Documentation/bpf/kfuncs.rst
@@ -146,6 +146,15 @@ that operate (change some property, perform some operation) on an object that
was obtained using an acquire kfunc. Such kfuncs need an unchanged pointer to
ensure the integrity of the operation being performed on the expected object.
+2.4.5 KF_DESTRUCTIVE flag
+--------------------------
+
+The KF_DESTRUCTIVE flag is used to indicate functions calling which is
+destructive to the system. For example such a call can result in system
+rebooting or panicking. Due to this additional restrictions apply to these
+calls. At the moment they only require CAP_SYS_BOOT capability, but more can be
+added later.
+
2.5 Registering the kfuncs
--------------------------
diff --git a/include/linux/btf.h b/include/linux/btf.h
index cdb376d53238..51a0961c84e3 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -49,6 +49,7 @@
* for this case.
*/
#define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */
+#define KF_DESTRUCTIVE (1 << 5) /* kfunc performs destructive actions */
struct btf;
struct btf_member;
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 843a966cd02b..163cc0a2dc5a 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -7598,6 +7598,11 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
func_name);
return -EACCES;
}
+ if (*kfunc_flags & KF_DESTRUCTIVE && !capable(CAP_SYS_BOOT)) {
+ verbose(env, "destructive kfunc calls require CAP_SYS_BOOT capabilities\n");
+ return -EACCES;
+ }
+
acq = *kfunc_flags & KF_ACQUIRE;
/* Check the arguments */
--
2.37.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH bpf-next v4 2/3] bpf: export crash_kexec() as destructive kfunc
2022-08-09 10:53 [PATCH bpf-next v4 0/3] destructive bpf_kfuncs Artem Savkov
2022-08-09 10:53 ` [PATCH bpf-next v4 1/3] bpf: add destructive kfunc flag Artem Savkov
@ 2022-08-09 10:53 ` Artem Savkov
2022-08-09 17:48 ` Alexei Starovoitov
2022-08-09 10:53 ` [PATCH bpf-next v4 3/3] selftests/bpf: add destructive kfunc test Artem Savkov
2 siblings, 1 reply; 6+ messages in thread
From: Artem Savkov @ 2022-08-09 10:53 UTC (permalink / raw
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, bpf, netdev
Cc: linux-kernel, Andrea Arcangeli, Daniel Vacek, Jiri Olsa, Song Liu,
Daniel Xu, Kumar Kartikeya Dwivedi, Artem Savkov
Allow properly marked bpf programs to call crash_kexec().
Signed-off-by: Artem Savkov <asavkov@redhat.com>
---
kernel/bpf/helpers.c | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 1f961f9982d2..103dbddff41f 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -1711,3 +1711,24 @@ bpf_base_func_proto(enum bpf_func_id func_id)
return NULL;
}
}
+
+BTF_SET8_START(tracing_btf_ids)
+#ifdef CONFIG_KEXEC_CORE
+BTF_ID_FLAGS(func, crash_kexec, KF_DESTRUCTIVE)
+#endif
+BTF_SET8_END(tracing_btf_ids)
+
+static const struct btf_kfunc_id_set tracing_kfunc_set = {
+ .owner = THIS_MODULE,
+ .set = &tracing_btf_ids,
+};
+
+static int __init kfunc_init(void)
+{
+ if (register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &tracing_kfunc_set))
+ pr_warn("failed to register kfunc id set for BPF_PROG_TYPE_TRACING\n");
+
+ return 0;
+}
+
+late_initcall(kfunc_init);
--
2.37.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH bpf-next v4 3/3] selftests/bpf: add destructive kfunc test
2022-08-09 10:53 [PATCH bpf-next v4 0/3] destructive bpf_kfuncs Artem Savkov
2022-08-09 10:53 ` [PATCH bpf-next v4 1/3] bpf: add destructive kfunc flag Artem Savkov
2022-08-09 10:53 ` [PATCH bpf-next v4 2/3] bpf: export crash_kexec() as destructive kfunc Artem Savkov
@ 2022-08-09 10:53 ` Artem Savkov
2 siblings, 0 replies; 6+ messages in thread
From: Artem Savkov @ 2022-08-09 10:53 UTC (permalink / raw
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, bpf, netdev
Cc: linux-kernel, Andrea Arcangeli, Daniel Vacek, Jiri Olsa, Song Liu,
Daniel Xu, Kumar Kartikeya Dwivedi, Artem Savkov
Add a test checking that programs calling destructive kfuncs can only do
so if they have CAP_SYS_BOOT capabilities.
Signed-off-by: Artem Savkov <asavkov@redhat.com>
---
net/bpf/test_run.c | 5 +++
.../selftests/bpf/prog_tests/kfunc_call.c | 36 +++++++++++++++++++
.../bpf/progs/kfunc_call_destructive.c | 14 ++++++++
3 files changed, 55 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/kfunc_call_destructive.c
diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
index cbc9cd5058cb..afa7125252f6 100644
--- a/net/bpf/test_run.c
+++ b/net/bpf/test_run.c
@@ -695,6 +695,10 @@ noinline void bpf_kfunc_call_test_ref(struct prog_test_ref_kfunc *p)
{
}
+noinline void bpf_kfunc_call_test_destructive(void)
+{
+}
+
__diag_pop();
ALLOW_ERROR_INJECTION(bpf_modify_return_test, ERRNO);
@@ -719,6 +723,7 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_pass1)
BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail1)
BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail2)
BTF_ID_FLAGS(func, bpf_kfunc_call_test_ref, KF_TRUSTED_ARGS)
+BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE)
BTF_SET8_END(test_sk_check_kfunc_ids)
static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size,
diff --git a/tools/testing/selftests/bpf/prog_tests/kfunc_call.c b/tools/testing/selftests/bpf/prog_tests/kfunc_call.c
index c00eb974eb85..351fafa006fb 100644
--- a/tools/testing/selftests/bpf/prog_tests/kfunc_call.c
+++ b/tools/testing/selftests/bpf/prog_tests/kfunc_call.c
@@ -5,6 +5,9 @@
#include "kfunc_call_test.lskel.h"
#include "kfunc_call_test_subprog.skel.h"
#include "kfunc_call_test_subprog.lskel.h"
+#include "kfunc_call_destructive.skel.h"
+
+#include "cap_helpers.h"
static void test_main(void)
{
@@ -86,6 +89,36 @@ static void test_subprog_lskel(void)
kfunc_call_test_subprog_lskel__destroy(skel);
}
+static int test_destructive_open_and_load(void)
+{
+ struct kfunc_call_destructive *skel;
+ int err;
+
+ skel = kfunc_call_destructive__open();
+ if (!ASSERT_OK_PTR(skel, "prog_open"))
+ return -1;
+
+ err = kfunc_call_destructive__load(skel);
+
+ kfunc_call_destructive__destroy(skel);
+
+ return err;
+}
+
+static void test_destructive(void)
+{
+ __u64 save_caps = 0;
+
+ ASSERT_OK(test_destructive_open_and_load(), "succesful_load");
+
+ if (!ASSERT_OK(cap_disable_effective(1ULL << CAP_SYS_BOOT, &save_caps), "drop_caps"))
+ return;
+
+ ASSERT_EQ(test_destructive_open_and_load(), -13, "no_caps_failure");
+
+ cap_enable_effective(save_caps, NULL);
+}
+
void test_kfunc_call(void)
{
if (test__start_subtest("main"))
@@ -96,4 +129,7 @@ void test_kfunc_call(void)
if (test__start_subtest("subprog_lskel"))
test_subprog_lskel();
+
+ if (test__start_subtest("destructive"))
+ test_destructive();
}
diff --git a/tools/testing/selftests/bpf/progs/kfunc_call_destructive.c b/tools/testing/selftests/bpf/progs/kfunc_call_destructive.c
new file mode 100644
index 000000000000..767472bc5a97
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/kfunc_call_destructive.c
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+
+extern void bpf_kfunc_call_test_destructive(void) __ksym;
+
+SEC("tc")
+int kfunc_destructive_test(void)
+{
+ bpf_kfunc_call_test_destructive();
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.37.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH bpf-next v4 1/3] bpf: add destructive kfunc flag
2022-08-09 10:53 ` [PATCH bpf-next v4 1/3] bpf: add destructive kfunc flag Artem Savkov
@ 2022-08-09 12:40 ` Kumar Kartikeya Dwivedi
0 siblings, 0 replies; 6+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2022-08-09 12:40 UTC (permalink / raw
To: Artem Savkov
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, bpf, netdev,
linux-kernel, Andrea Arcangeli, Daniel Vacek, Jiri Olsa, Song Liu,
Daniel Xu
On Tue, 9 Aug 2022 at 12:53, Artem Savkov <asavkov@redhat.com> wrote:
>
> Add KF_DESTRUCTIVE flag for destructive functions. Functions with this
> flag set will require CAP_SYS_BOOT capabilities.
>
> Signed-off-by: Artem Savkov <asavkov@redhat.com>
> ---
> Documentation/bpf/kfuncs.rst | 9 +++++++++
> include/linux/btf.h | 1 +
> kernel/bpf/verifier.c | 5 +++++
> 3 files changed, 15 insertions(+)
>
> diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst
> index c0b7dae6dbf5..2e97e08be7de 100644
> --- a/Documentation/bpf/kfuncs.rst
> +++ b/Documentation/bpf/kfuncs.rst
> @@ -146,6 +146,15 @@ that operate (change some property, perform some operation) on an object that
> was obtained using an acquire kfunc. Such kfuncs need an unchanged pointer to
> ensure the integrity of the operation being performed on the expected object.
>
> +2.4.5 KF_DESTRUCTIVE flag
This should be 2.4.6.
> +--------------------------
> +
> +The KF_DESTRUCTIVE flag is used to indicate functions calling which is
> +destructive to the system. For example such a call can result in system
> +rebooting or panicking. Due to this additional restrictions apply to these
> +calls. At the moment they only require CAP_SYS_BOOT capability, but more can be
> +added later.
> +
> 2.5 Registering the kfuncs
> --------------------------
>
> diff --git a/include/linux/btf.h b/include/linux/btf.h
> index cdb376d53238..51a0961c84e3 100644
> --- a/include/linux/btf.h
> +++ b/include/linux/btf.h
> @@ -49,6 +49,7 @@
> * for this case.
> */
> #define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */
> +#define KF_DESTRUCTIVE (1 << 5) /* kfunc performs destructive actions */
>
> struct btf;
> struct btf_member;
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 843a966cd02b..163cc0a2dc5a 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -7598,6 +7598,11 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
> func_name);
> return -EACCES;
> }
> + if (*kfunc_flags & KF_DESTRUCTIVE && !capable(CAP_SYS_BOOT)) {
> + verbose(env, "destructive kfunc calls require CAP_SYS_BOOT capabilities\n");
> + return -EACCES;
> + }
> +
> acq = *kfunc_flags & KF_ACQUIRE;
>
> /* Check the arguments */
> --
> 2.37.1
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH bpf-next v4 2/3] bpf: export crash_kexec() as destructive kfunc
2022-08-09 10:53 ` [PATCH bpf-next v4 2/3] bpf: export crash_kexec() as destructive kfunc Artem Savkov
@ 2022-08-09 17:48 ` Alexei Starovoitov
0 siblings, 0 replies; 6+ messages in thread
From: Alexei Starovoitov @ 2022-08-09 17:48 UTC (permalink / raw
To: Artem Savkov
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, bpf,
Network Development, LKML, Andrea Arcangeli, Daniel Vacek,
Jiri Olsa, Song Liu, Daniel Xu, Kumar Kartikeya Dwivedi
On Tue, Aug 9, 2022 at 3:53 AM Artem Savkov <asavkov@redhat.com> wrote:
>
> Allow properly marked bpf programs to call crash_kexec().
>
> Signed-off-by: Artem Savkov <asavkov@redhat.com>
> ---
> kernel/bpf/helpers.c | 21 +++++++++++++++++++++
> 1 file changed, 21 insertions(+)
>
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 1f961f9982d2..103dbddff41f 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -1711,3 +1711,24 @@ bpf_base_func_proto(enum bpf_func_id func_id)
> return NULL;
> }
> }
> +
> +BTF_SET8_START(tracing_btf_ids)
> +#ifdef CONFIG_KEXEC_CORE
> +BTF_ID_FLAGS(func, crash_kexec, KF_DESTRUCTIVE)
> +#endif
> +BTF_SET8_END(tracing_btf_ids)
> +
> +static const struct btf_kfunc_id_set tracing_kfunc_set = {
> + .owner = THIS_MODULE,
> + .set = &tracing_btf_ids,
> +};
> +
> +static int __init kfunc_init(void)
> +{
> + if (register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &tracing_kfunc_set))
> + pr_warn("failed to register kfunc id set for BPF_PROG_TYPE_TRACING\n");
Please drop pr_warn. We don't have it in all other
places where we do registration.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2022-08-09 17:49 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-08-09 10:53 [PATCH bpf-next v4 0/3] destructive bpf_kfuncs Artem Savkov
2022-08-09 10:53 ` [PATCH bpf-next v4 1/3] bpf: add destructive kfunc flag Artem Savkov
2022-08-09 12:40 ` Kumar Kartikeya Dwivedi
2022-08-09 10:53 ` [PATCH bpf-next v4 2/3] bpf: export crash_kexec() as destructive kfunc Artem Savkov
2022-08-09 17:48 ` Alexei Starovoitov
2022-08-09 10:53 ` [PATCH bpf-next v4 3/3] selftests/bpf: add destructive kfunc test Artem Savkov
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.