linux/tools/bpf/bpftool
Quentin Monnet 73a4f0407e tools, bpftool: Make capability check account for new BPF caps
Following the introduction of CAP_BPF, and the switch from CAP_SYS_ADMIN
to other capabilities for various BPF features, update the capability
checks (and potentially, drops) in bpftool for feature probes. Because
bpftool and/or the system might not know of CAP_BPF yet, some caution is
necessary:

- If compiled and run on a system with CAP_BPF, check CAP_BPF,
  CAP_SYS_ADMIN, CAP_PERFMON, CAP_NET_ADMIN.

- Guard against CAP_BPF being undefined, to allow compiling bpftool from
  latest sources on older systems. If the system where feature probes
  are run does not know of CAP_BPF, stop checking after CAP_SYS_ADMIN,
  as this should be the only capability required for all the BPF
  probing.

- If compiled from latest sources on a system without CAP_BPF, but later
  executed on a newer system with CAP_BPF knowledge, then we only test
  CAP_SYS_ADMIN. Some probes may fail if the bpftool process has
  CAP_SYS_ADMIN but misses the other capabilities. The alternative would
  be to redefine the value for CAP_BPF in bpftool, but this does not
  look clean, and the case sounds relatively rare anyway.

Note that libcap offers a cap_to_name() function to retrieve the name of
a given capability (e.g. "cap_sys_admin"). We do not use it because
deriving the names from the macros looks simpler than using
cap_to_name() (doing a strdup() on the string) + cap_free() + handling
the case of failed allocations, when we just want to use the name of the
capability in an error message.

The checks when compiling without libcap (i.e. root versus non-root) are
unchanged.

v2:
- Do not allocate cap_list dynamically.
- Drop BPF-related capabilities when running with "unprivileged", even
  if we didn't have the full set in the first place (in v1, we would
  skip dropping them in that case).
- Keep track of what capabilities we have, print the names of the
  missing ones for privileged probing.
- Attempt to drop only the capabilities we actually have.
- Rename a couple variables.

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20200523010247.20654-1-quentin@isovalent.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2020-06-01 14:38:19 -07:00
..
bash-completion bpf, bpftool: Enable get{peer, sock}name attach types 2020-05-19 11:32:04 -07:00
Documentation bpf, bpftool: Enable get{peer, sock}name attach types 2020-05-19 11:32:04 -07:00
skeleton bpftool: Use linux/types.h from source tree for profiler build 2020-03-12 16:22:41 +01:00
.gitignore SPDX patches for 5.7-rc1. 2020-04-03 13:12:26 -07:00
btf_dumper.c tools, bpftool: Poison and replace kernel integer typedefs 2020-05-11 21:20:46 +02:00
btf.c tools, bpftool: Clean subcommand help messages 2020-06-01 14:38:18 -07:00
cfg.c tools, bpftool: Poison and replace kernel integer typedefs 2020-05-11 21:20:46 +02:00
cfg.h tools: bpftool: replace Netronome boilerplate with SPDX license headers 2018-12-13 12:08:44 +01:00
cgroup.c tools, bpftool: Clean subcommand help messages 2020-06-01 14:38:18 -07:00
common.c bpftool: Add bpf_link show and pin support 2020-04-28 17:27:08 -07:00
feature.c tools, bpftool: Make capability check account for new BPF caps 2020-06-01 14:38:19 -07:00
gen.c tools, bpftool: Clean subcommand help messages 2020-06-01 14:38:18 -07:00
iter.c tools, bpftool: Clean subcommand help messages 2020-06-01 14:38:18 -07:00
jit_disasm.c tools/bpf/bpftool: Remove duplicate headers 2020-04-26 08:40:01 -07:00
json_writer.c tools: bpftool: move "__printf()" attributes to header file 2019-08-15 22:06:46 -07:00
json_writer.h tools: bpftool: move "__printf()" attributes to header file 2019-08-15 22:06:46 -07:00
link.c tools, bpftool: Clean subcommand help messages 2020-06-01 14:38:18 -07:00
main.c tools/bpftool: Add bpf_iter support for bptool 2020-05-09 17:05:27 -07:00
main.h bpf, bpftool: Enable get{peer, sock}name attach types 2020-05-19 11:32:04 -07:00
Makefile tools: bpftool: Make libcap dependency optional 2020-04-29 23:25:11 +02:00
map_perf_ring.c tools, bpftool: Poison and replace kernel integer typedefs 2020-05-11 21:20:46 +02:00
map.c tools, bpftool: Clean subcommand help messages 2020-06-01 14:38:18 -07:00
net.c tools, bpftool: Clean subcommand help messages 2020-06-01 14:38:18 -07:00
netlink_dumper.c bpftool: Use consistent include paths for libbpf 2020-01-20 16:37:45 -08:00
netlink_dumper.h tools: bpftool: dual license all files 2018-12-13 12:08:44 +01:00
perf.c tools, bpftool: Clean subcommand help messages 2020-06-01 14:38:18 -07:00
prog.c tools, bpftool: Clean subcommand help messages 2020-06-01 14:38:18 -07:00
struct_ops.c tools, bpftool: Clean subcommand help messages 2020-06-01 14:38:18 -07:00
tracelog.c tools: bpftool: add an option to prevent auto-mount of bpffs, tracefs 2018-12-18 14:47:17 +01:00
xlated_dumper.c bpftool: Use consistent include paths for libbpf 2020-01-20 16:37:45 -08:00
xlated_dumper.h tools: bpftool: replace Netronome boilerplate with SPDX license headers 2018-12-13 12:08:44 +01:00