From: Quentin Monnet <quentin.monnet@netronome.com>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>
Cc: netdev@vger.kernel.org, oss-drivers@netronome.com,
Quentin Monnet <quentin.monnet@netronome.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Jesper Dangaard Brouer <brouer@redhat.com>,
Stanislav Fomichev <sdf@google.com>
Subject: [RFC PATCH bpf-next v2 2/9] tools: bpftool: add probes for /proc/ eBPF parameters
Date: Thu, 20 Dec 2018 12:24:25 +0000 [thread overview]
Message-ID: <20181220122432.27511-3-quentin.monnet@netronome.com> (raw)
In-Reply-To: <20181220122432.27511-1-quentin.monnet@netronome.com>
Add a set of probes to dump the eBPF-related parameters available from
/proc/: availability of bpf() syscall for unprivileged users,
JIT compiler status and hardening status, kallsyms exports status.
Sample output:
# bpftool feature probe kernel
Scanning system configuration...
bpf() syscall for unprivileged users is enabled
JIT compiler is disabled
JIT compiler hardening is disabled
JIT compiler kallsyms exports are disabled
...
# bpftool --json --pretty feature probe kernel
{
"system_config": {
"unprivileged_bpf_disabled": 0,
"bpf_jit_enable": 0,
"bpf_jit_harden": 0,
"bpf_jit_kallsyms": 0
},
...
}
These probes are skipped if procfs is not mounted.
v2:
- Remove C-style macros output from this patch.
Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
---
tools/bpf/bpftool/feature.c | 168 ++++++++++++++++++++++++++++++++++++
1 file changed, 168 insertions(+)
diff --git a/tools/bpf/bpftool/feature.c b/tools/bpf/bpftool/feature.c
index 9bf8e786572e..410d35857cf4 100644
--- a/tools/bpf/bpftool/feature.c
+++ b/tools/bpf/bpftool/feature.c
@@ -5,6 +5,7 @@
#include <string.h>
#include <unistd.h>
#include <sys/utsname.h>
+#include <sys/vfs.h>
#include <linux/filter.h>
#include <linux/limits.h>
@@ -13,11 +14,29 @@
#include "main.h"
+#ifndef PROC_SUPER_MAGIC
+# define PROC_SUPER_MAGIC 0x9fa0
+#endif
+
enum probe_component {
COMPONENT_UNSPEC,
COMPONENT_KERNEL,
};
+/* Miscellaneous utility functions */
+
+static bool check_procfs(void)
+{
+ struct statfs st_fs;
+
+ if (statfs("/proc", &st_fs) < 0)
+ return false;
+ if ((unsigned long)st_fs.f_type != PROC_SUPER_MAGIC)
+ return false;
+
+ return true;
+}
+
/* Printing utility functions */
static void
@@ -42,6 +61,135 @@ print_start_section(const char *json_title, const char *plain_title)
/* Probing functions */
+static int read_procfs(const char *path)
+{
+ char *endptr, *line = NULL;
+ size_t len = 0;
+ FILE *fd;
+ int res;
+
+ fd = fopen(path, "r");
+ if (!fd)
+ return -1;
+
+ res = getline(&line, &len, fd);
+ fclose(fd);
+ if (res < 0)
+ return -1;
+
+ errno = 0;
+ res = strtol(line, &endptr, 10);
+ if (errno || *line == '\0' || *endptr != '\n')
+ res = -1;
+ free(line);
+
+ return res;
+}
+
+static void probe_unprivileged_disabled(void)
+{
+ int res;
+
+ res = read_procfs("/proc/sys/kernel/unprivileged_bpf_disabled");
+ if (json_output) {
+ jsonw_int_field(json_wtr, "unprivileged_bpf_disabled", res);
+ } else {
+ switch (res) {
+ case 0:
+ printf("bpf() syscall for unprivileged users is enabled\n");
+ break;
+ case 1:
+ printf("bpf() syscall restricted to privileged users\n");
+ break;
+ case -1:
+ printf("Unable to retrieve required privileges for bpf() syscall\n");
+ break;
+ default:
+ printf("bpf() syscall restriction has unknown value %d\n", res);
+ }
+ }
+}
+
+static void probe_jit_enable(void)
+{
+ int res;
+
+ res = read_procfs("/proc/sys/net/core/bpf_jit_enable");
+ if (json_output) {
+ jsonw_int_field(json_wtr, "bpf_jit_enable", res);
+ } else {
+ switch (res) {
+ case 0:
+ printf("JIT compiler is disabled\n");
+ break;
+ case 1:
+ printf("JIT compiler is enabled\n");
+ break;
+ case 2:
+ printf("JIT compiler is enabled with debugging traces in kernel logs\n");
+ break;
+ case -1:
+ printf("Unable to retrieve JIT-compiler status\n");
+ break;
+ default:
+ printf("JIT-compiler status has unknown value %d\n",
+ res);
+ }
+ }
+}
+
+static void probe_jit_harden(void)
+{
+ int res;
+
+ res = read_procfs("/proc/sys/net/core/bpf_jit_harden");
+ if (json_output) {
+ jsonw_int_field(json_wtr, "bpf_jit_harden", res);
+ } else {
+ switch (res) {
+ case 0:
+ printf("JIT compiler hardening is disabled\n");
+ break;
+ case 1:
+ printf("JIT compiler hardening is enabled for unprivileged users\n");
+ break;
+ case 2:
+ printf("JIT compiler hardening is enabled for all users\n");
+ break;
+ case -1:
+ printf("Unable to retrieve JIT hardening status\n");
+ break;
+ default:
+ printf("JIT hardening status has unknown value %d\n",
+ res);
+ }
+ }
+}
+
+static void probe_jit_kallsyms(void)
+{
+ int res;
+
+ res = read_procfs("/proc/sys/net/core/bpf_jit_kallsyms");
+ if (json_output) {
+ jsonw_int_field(json_wtr, "bpf_jit_kallsyms", res);
+ } else {
+ switch (res) {
+ case 0:
+ printf("JIT compiler kallsyms exports are disabled\n");
+ break;
+ case 1:
+ printf("JIT compiler kallsyms exports are enabled for root\n");
+ break;
+ case -1:
+ printf("Unable to retrieve JIT kallsyms export status\n");
+ break;
+ default:
+ printf("JIT kallsyms exports status has unknown value %d\n", res);
+ }
+ }
+}
+
static int probe_kernel_version(void)
{
int version, subversion, patchlevel, code = 0;
@@ -109,6 +257,26 @@ static int do_probe(int argc, char **argv)
if (json_output)
jsonw_start_object(json_wtr);
+ switch (target) {
+ case COMPONENT_KERNEL:
+ case COMPONENT_UNSPEC:
+ print_start_section("system_config",
+ "Scanning system configuration...");
+ if (check_procfs()) {
+ probe_unprivileged_disabled();
+ probe_jit_enable();
+ probe_jit_harden();
+ probe_jit_kallsyms();
+ } else {
+ p_info("/* procfs not mounted, skipping related probes */");
+ }
+ if (json_output)
+ jsonw_end_object(json_wtr);
+ else
+ printf("\n");
+ break;
+ }
+
print_start_section("syscall_config",
"Scanning system call and kernel version...");
--
2.17.1
next prev parent reply other threads:[~2018-12-20 12:24 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-20 12:24 [RFC PATCH bpf-next v2 0/9] tools: bpftool: add probes for system and device Quentin Monnet
2018-12-20 12:24 ` [RFC PATCH bpf-next v2 1/9] tools: bpftool: add basic probe capability, probe syscall and kversion Quentin Monnet
2018-12-20 12:24 ` Quentin Monnet [this message]
2018-12-20 12:24 ` [RFC PATCH bpf-next v2 3/9] tools: bpftool: add probes for kernel configuration options Quentin Monnet
2018-12-20 17:40 ` Stanislav Fomichev
2018-12-20 18:02 ` Quentin Monnet
2018-12-20 18:14 ` Stanislav Fomichev
2018-12-20 12:24 ` [RFC PATCH bpf-next v2 4/9] tools: bpftool: add probes for eBPF program types Quentin Monnet
2018-12-20 17:45 ` Stanislav Fomichev
2018-12-20 18:06 ` [oss-drivers] " Quentin Monnet
2018-12-20 18:17 ` Stanislav Fomichev
2018-12-20 18:21 ` Quentin Monnet
2018-12-20 18:29 ` Stanislav Fomichev
2018-12-20 12:24 ` [RFC PATCH bpf-next v2 5/9] tools: bpftool: add probes for eBPF map types Quentin Monnet
2018-12-20 17:47 ` Stanislav Fomichev
2018-12-20 18:10 ` Quentin Monnet
2018-12-20 18:18 ` Stanislav Fomichev
2018-12-20 18:27 ` Quentin Monnet
2018-12-20 18:42 ` Stanislav Fomichev
2018-12-20 18:45 ` Quentin Monnet
2018-12-20 12:24 ` [RFC PATCH bpf-next v2 6/9] tools: bpftool: add probes for eBPF helper functions Quentin Monnet
2018-12-20 17:53 ` Stanislav Fomichev
2018-12-20 18:14 ` Quentin Monnet
2018-12-20 12:24 ` [RFC PATCH bpf-next v2 7/9] tools: bpftool: add C-style "#define" output for probes Quentin Monnet
2018-12-20 17:25 ` Daniel Borkmann
2018-12-20 20:05 ` Quentin Monnet
2018-12-20 22:29 ` Daniel Borkmann
2018-12-20 12:24 ` [RFC PATCH bpf-next v2 8/9] tools: bpftool: add probes for a network device Quentin Monnet
2018-12-20 12:24 ` [RFC PATCH bpf-next v2 9/9] tools: bpftool: add bash completion for bpftool probes Quentin Monnet
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=20181220122432.27511-3-quentin.monnet@netronome.com \
--to=quentin.monnet@netronome.com \
--cc=acme@kernel.org \
--cc=ast@kernel.org \
--cc=brouer@redhat.com \
--cc=daniel@iogearbox.net \
--cc=netdev@vger.kernel.org \
--cc=oss-drivers@netronome.com \
--cc=sdf@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).