tools: bpftool: probe features for unprivileged users
Subsystems (4)
v3 cover tools: bpftool: probe features for unprivileged users
This set allows unprivileged users to probe available features with bpftool. On Daniel's suggestion, the "unprivileged" keyword must be passed on the command line to avoid accidentally dumping a subset of the features supported by the system. When used by root, this keyword makes bpftool drop the CAP_SYS_ADMIN capability and print the features available to unprivileged users only.
The first patch makes a variable global in feature.c to avoid piping too many booleans through the different functions. The second patch introduces the unprivileged probing, adding a dependency to libcap. Then the third patch makes this dependency optional, by restoring the initial behaviour (root only can probe features) if the library is not available.
v3: Update help message for bpftool feature probe ("unprivileged").
v2: Add "unprivileged" keyword, libcap check (patches 1 and 3 are new).
Quentin Monnet (3):
tools: bpftool: for "feature probe" define "full_mode" bool as global
tools: bpftool: allow unprivileged users to probe features
tools: bpftool: make libcap dependency optional
.../bpftool/Documentation/bpftool-feature.rst | 12 +- tools/bpf/bpftool/Makefile | 13 +- tools/bpf/bpftool/bash-completion/bpftool | 2 +- tools/bpf/bpftool/feature.c | 143 +++++++++++++++--- 4 files changed, 143 insertions(+), 27 deletions(-)
Signature · 1 line
2.20.1
PATCH v3 1/3 tools: bpftool: for "feature probe" define "full_mode" bool as global
The "full_mode" variable used for switching between full or partial feature probing (i.e. with or without probing helpers that will log warnings in kernel logs) was piped from the main do_probe() function down to probe_helpers_for_progtype(), where it is needed.
Define it as a global variable: the calls will be more readable, and if other similar flags were to be used in the future, we could use global variables as well instead of extending again the list of arguments with new flags.
--- tools/bpf/bpftool/feature.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
tools/bpf/bpftool/feature.c +7 -8
@@ -35,6 +35,8 @@ static const char * const helper_name[] = {#undef BPF_HELPER_MAKE_ENTRY +static bool full_mode; +/* Miscellaneous utility functions */ static bool check_procfs (void )
@@ -540,8 +542,7 @@ probe_helper_for_progtype(enum bpf_prog_type prog_type, bool supported_type,static void probe_helpers_for_progtype (enum bpf_prog_type prog_type,bool supported_type, -const char *define_prefix,bool full_mode, -__u32 ifindex) +const char *define_prefix,__u32 ifindex) {const char *ptype_name = prog_type_name[prog_type];char feat_name[128 ];
@@ -678,8 +679,7 @@ static void section_map_types(const char *define_prefix, __u32 ifindex) }static void -section_helpers (bool *supported_types,const char *define_prefix, -bool full_mode,__u32 ifindex) +section_helpers (bool *supported_types,const char *define_prefix,__u32 ifindex) {unsigned int i;
@@ -704,8 +704,8 @@ section_helpers(bool *supported_types, const char *define_prefix, define_prefix, define_prefix, define_prefix, define_prefix);for (i =BPF_PROG_TYPE_UNSPEC +1 ; i <ARRAY_SIZE (prog_type_name); i++) -probe_helpers_for_progtype (i, supported_types[i], - define_prefix, full_mode, ifindex); +probe_helpers_for_progtype (i, supported_types[i], define_prefix, + ifindex);print_end_section (); }
@@ -725,7 +725,6 @@ static int do_probe(int argc, char **argv)enum probe_component target =COMPONENT_UNSPEC ;const char *define_prefix =NULL ;bool supported_types[128 ] = {}; -bool full_mode = false;__u32 ifindex =0 ;char *ifname;
@@ -803,7 +802,7 @@ static int do_probe(int argc, char **argv) goto exit_close_json;section_program_types (supported_types, define_prefix, ifindex);section_map_types (define_prefix, ifindex); -section_helpers (supported_types, define_prefix, full_mode, ifindex); +section_helpers (supported_types, define_prefix, ifindex);section_misc (define_prefix, ifindex); exit_close_json:
Signature · 1 line
2.20.1
PATCH v3 2/3 tools: bpftool: allow unprivileged users to probe features
In reply to Quentin MonnetThere is demand for a way to identify what BPF helper functions are available to unprivileged users. To do so, allow unprivileged users to run "bpftool feature probe" to list BPF-related features. This will only show features accessible to those users, and may not reflect the full list of features available (to administrators) on the system.
To avoid the case where bpftool is inadvertently run as non-root and would list only a subset of the features supported by the system when it would be expected to list all of them, running as unprivileged is gated behind the "unprivileged" keyword passed to the command line. When used by a privileged user, this keyword allows to drop the CAP_SYS_ADMIN and to list the features available to unprivileged users. Note that this addsd a dependency on libpcap for compiling bpftool.
Note that there is no particular reason why the probes were restricted to root, other than the fact I did not need them for unprivileged and did not bother with the additional checks at the time probes were added.
--- .../bpftool/Documentation/bpftool-feature.rst | 10 +- tools/bpf/bpftool/Makefile | 2 +- tools/bpf/bpftool/bash-completion/bpftool | 2 +- tools/bpf/bpftool/feature.c | 102 +++++++++++++++--- 4 files changed, 100 insertions(+), 16 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-feature.rst +9 -1
@@ -19,7 +19,7 @@ SYNOPSIS FEATURE COMMANDS ================ -| **bpftool** **feature probe** [*COMPONENT*] [**full**] [**macros** [**prefix** *PREFIX*]] +| **bpftool** **feature probe** [*COMPONENT*] [**full**] [**unprivileged**] [**macros** [**prefix** *PREFIX*]] | **bpftool** **feature help** | | *COMPONENT* := { **kernel** | **dev** *NAME* }
@@ -49,6 +49,14 @@ DESCRIPTION Keyword **kernel** can be omitted. If no probe target is specified, probing the kernel is the default behaviour. + When the **unprivileged** keyword is used, bpftool will dump + only the features available to a user who does not have the + **CAP_SYS_ADMIN** capability set. The features available in + that case usually represent a small subset of the parameters + supported by the system. Unprivileged users MUST use the + **unprivileged** keyword: This is to avoid misdetection if + bpftool is inadvertently run as non-root, for example. + **bpftool feature probe dev** *NAME* [**full**] [**macros** [**prefix** *PREFIX*]] Probe network device for supported eBPF features and dump results to the console.
tools/bpf/bpftool/Makefile +1 -1
@@ -55,7 +55,7 @@ ifneq ($(EXTRA_LDFLAGS),) LDFLAGS += $(EXTRA_LDFLAGS) endif -LIBS = $(LIBBPF) -lelf -lz +LIBS = $(LIBBPF) -lelf -lz -lcap INSTALL ?= install RM ?= rm -f
tools/bpf/bpftool/bash-completion/bpftool +1 -1
@@ -1079,7 +1079,7 @@ _bpftool() COMPREPLY+=( $( compgen -W 'macros' -- "$cur" ) ) fi _bpftool_one_of_list 'kernel dev' - _bpftool_once_attr 'full' + _bpftool_once_attr 'full unprivileged' return 0 ;; *)
tools/bpf/bpftool/feature.c +89 -13
164 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v3 3/3 tools: bpftool: make libcap dependency optional
In reply to Quentin MonnetThe new libcap dependency is not used for an essential feature of bpftool, and we could imagine building the tool without checks on CAP_SYS_ADMIN by disabling probing features as an unprivileged users.
Make it so, in order to avoid a hard dependency on libcap, and to ease packaging/embedding of bpftool.
--- .../bpftool/Documentation/bpftool-feature.rst | 4 ++- tools/bpf/bpftool/Makefile | 13 +++++++--- tools/bpf/bpftool/feature.c | 26 +++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-feature.rst +3 -1
@@ -55,7 +55,9 @@ DESCRIPTION that case usually represent a small subset of the parameters supported by the system. Unprivileged users MUST use the **unprivileged** keyword: This is to avoid misdetection if - bpftool is inadvertently run as non-root, for example. + bpftool is inadvertently run as non-root, for example. This + keyword is unavailable if bpftool was compiled without + libcap. **bpftool feature probe dev** *NAME* [**full**] [**macros** [**prefix** *PREFIX*]] Probe network device for supported eBPF features and dump
tools/bpf/bpftool/Makefile +9 -4
@@ -55,16 +55,15 @@ ifneq ($(EXTRA_LDFLAGS),) LDFLAGS += $(EXTRA_LDFLAGS) endif -LIBS = $(LIBBPF) -lelf -lz -lcap - INSTALL ?= install RM ?= rm -f CLANG ?= clang FEATURE_USER = .bpftool -FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib \ +FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib libcap \ + clang-bpf-global-var +FEATURE_DISPLAY = libbfd disassembler-four-args zlib libcap \ clang-bpf-global-var -FEATURE_DISPLAY = libbfd disassembler-four-args zlib clang-bpf-global-var check_feat := 1 NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall
@@ -90,6 +89,12 @@ ifeq ($(feature-reallocarray), 0) CFLAGS += -DCOMPAT_NEED_REALLOCARRAY endif +LIBS = $(LIBBPF) -lelf -lz +ifeq ($(feature-libcap), 1) +CFLAGS += -DUSE_LIBCAP +LIBS += -lcap +endif + include $(wildcard $(OUTPUT)*.d) all: $(OUTPUT)bpftool
tools/bpf/bpftool/feature.c +26 -0
@@ -6,7 +6,9 @@#include <string.h> #include <unistd.h> #include <net/if.h> +#ifdef USE_LIBCAP #include <sys/capability.h> +#endif #include <sys/utsname.h> #include <sys/vfs.h>
@@ -37,7 +39,9 @@ static const char * const helper_name[] = {#undef BPF_HELPER_MAKE_ENTRYstatic bool full_mode; +#ifdef USE_LIBCAP static bool run_as_unprivileged; +#endif /* Miscellaneous utility functions */
@@ -475,11 +479,13 @@ probe_prog_type(enum bpf_prog_type prog_type, bool *supported_types, } res =bpf_probe_prog_type (prog_type, ifindex); +#ifdef USE_LIBCAP /* Probe may succeed even if program load fails, for unprivileged users * check that we did not fail because of insufficient permissions */ if (run_as_unprivileged && errno ==EPERM ) res = false; +#endif supported_types[prog_type] |= res;
@@ -535,12 +541,14 @@ probe_helper_for_progtype(enum bpf_prog_type prog_type, bool supported_type,if (supported_type) { res =bpf_probe_helper (id, prog_type, ifindex); +#ifdef USE_LIBCAP /* Probe may succeed even if program load fails, for * unprivileged users check that we did not fail because of * insufficient permissions */ if (run_as_unprivileged && errno ==EPERM ) res = false; +#endif }if (json_output) {
@@ -738,6 +746,7 @@ static void section_misc(const char *define_prefix, __u32 ifindex)static int handle_perms (void ) { +#ifdef USE_LIBCAP cap_value_t cap_list[1 ] = {CAP_SYS_ADMIN };bool has_sys_admin_cap = false;cap_flag_value_t val;
@@ -793,6 +802,18 @@ static int handle_perms(void) }return res; +#else +/* Detection assumes user has sufficient privileges (CAP_SYS_ADMIN). +* We do not use libpcap so let's approximate, and restrict usage to +* root user only. +*/ +if (geteuid ()) { +p_err ("full feature probing requires root privileges" ); +return -1 ; + } + +return 0 ; +#endif /* USE_LIBCAP */ }static int do_probe (int argc,char **argv)
@@ -852,8 +873,13 @@ static int do_probe(int argc, char **argv)return -1 ; define_prefix =GET_ARG (); }else if (is_prefix (*argv,"unprivileged" )) { +#ifdef USE_LIBCAP run_as_unprivileged = true;NEXT_ARG (); +#else +p_err ("unprivileged run not supported, recompile bpftool with libcap" ); +return -1 ; +#endif }else {p_err ("expected no more arguments, 'kernel', 'dev', 'macros' or 'prefix', got: '%s'?" , *argv);
Signature · 1 line
2.20.1
Quoted 25 lines · Quentin Monnet
This set allows unprivileged users to probe available features with bpftool. On Daniel's suggestion, the "unprivileged" keyword must be passed on the command line to avoid accidentally dumping a subset of the features supported by the system. When used by root, this keyword makes bpftool drop the
CAP_SYS_ADMINcapability and print the features available to unprivileged users only.The first patch makes a variable global in
feature.cto avoid piping too many booleans through the different functions. The second patch introduces the unprivileged probing, adding a dependency to libcap. Then the third patch makes this dependency optional, by restoring the initial behaviour (root only can probe features) if the library is not available.Cc Richard Palethorpe <[email protected]>Cc Michael Kerrisk <[email protected]>v3: Update help message for bpftool feature probe ("unprivileged").
v2: Add "unprivileged" keyword, libcap check (patches 1 and 3 are new).
Quentin Monnet (3):
tools: bpftool: for "feature probe" define "full_mode" bool as global
tools: bpftool: allow unprivileged users to probe features
tools: bpftool: make libcap dependency optional
Quoted 9 lines
.../bpftool/Documentation/bpftool-feature.rst | 12 +- tools/bpf/bpftool/Makefile | 13 +- tools/bpf/bpftool/bash-completion/bpftool | 2 +- tools/bpf/bpftool/feature.c | 143 +++++++++++++++--- 4 files changed, 143 insertions(+), 27 deletions(-)Signature · 1 line
2.20.1
For the series,
Quoted 25 lines · Quentin Monnet
This set allows unprivileged users to probe available features with bpftool. On Daniel's suggestion, the "unprivileged" keyword must be passed on the command line to avoid accidentally dumping a subset of the features supported by the system. When used by root, this keyword makes bpftool drop the
CAP_SYS_ADMINcapability and print the features available to unprivileged users only.The first patch makes a variable global in
feature.cto avoid piping too many booleans through the different functions. The second patch introduces the unprivileged probing, adding a dependency to libcap. Then the third patch makes this dependency optional, by restoring the initial behaviour (root only can probe features) if the library is not available.Cc Richard Palethorpe <[email protected]>Cc Michael Kerrisk <[email protected]>v3: Update help message for bpftool feature probe ("unprivileged").
v2: Add "unprivileged" keyword, libcap check (patches 1 and 3 are new).
Quentin Monnet (3):
tools: bpftool: for "feature probe" define "full_mode" bool as global
tools: bpftool: allow unprivileged users to probe features
tools: bpftool: make libcap dependency optional
Quoted 6 lines
.../bpftool/Documentation/bpftool-feature.rst | 12 +- tools/bpf/bpftool/Makefile | 13 +- tools/bpf/bpftool/bash-completion/bpftool | 2 +- tools/bpf/bpftool/feature.c | 143 +++++++++++++++--- 4 files changed, 143 insertions(+), 27 deletions(-)
Applied, thanks!