libbpf: add support for privileged/unprivileged control separation
Subsystems (5)
v6 cover libbpf: add support for privileged/unprivileged control separation
From: Mariusz Dudek <[email protected]>
This patch series adds support for separation of eBPF program load and xsk socket creation. In for example a Kubernetes environment you can have an AF_XDP CNI or daemonset that is responsible for launching pods that execute an application using AF_XDP sockets. It is desirable that the pod runs with as low privileges as possible, CAP_NET_RAW in this case, and that all operations that require privileges are contained in the CNI or daemonset.
In this case, you have to be able separate ePBF program load from xsk socket creation.
Currently, this will not work with the xsk_socket__create APIs because you need to have CAP_NET_ADMIN privileges to load eBPF program and CAP_SYS_ADMIN privileges to create update xsk_bpf_maps. To be exact xsk_set_bpf_maps does not need those privileges but it takes the prog_fd and xsks_map_fd and those are known only to process that was loading eBPF program. The api bpf_prog_get_fd_by_id that looks up the fd of the prog using an prog_id and bpf_map_get_fd_by_id that looks for xsks_map_fd usinb map_id both requires CAP_SYS_ADMIN.
With this patch, the pod can be run with CAP_NET_RAW capability only. In case your umem is larger or equal process limit for MEMLOCK you need either increase the limit or CAP_IPC_LOCK capability. Without this patch in case of insufficient rights ENOPERM is returned by xsk_socket__create.
To resolve this privileges issue two new APIs are introduced:
- xsk_setup_xdp_prog - loads the built in XDP program. It can also return xsks_map_fd which is needed by unprivileged process to update xsks_map with AF_XDP socket "fd"
- xsk_sokcet__update_xskmap - inserts an AF_XDP socket into an xskmap for a particular xsk_socket
Usage example:
int xsk_setup_xdp_prog(int ifindex, int *xsks_map_fd)
int xsk_socket__update_xskmap(struct xsk_socket *xsk, int xsks_map_fd);
Inserts AF_XDP socket "fd" into the xskmap.
The first patch introduces the new APIs. The second patch provides a new sample applications working as control and modification to existing xdpsock application to work with less privileges.
This patch set is based on bpf-next commit ba0581749fec (net, xdp, xsk: fix __sk_mark_napi_id_once napi_id error)
Since v5
- fixed sample/bpf/xdpsock_user.c to resolve merge conflicts
Since v4
- sample/bpf/Makefile issues fixed
Since v3:
- force_set_map flag removed
- leaking of xsk struct fixed
- unified function error returning policy implemented
Since v2:
- new APIs moved itto LIBBPF_0.3.0 section
- struct bpf_prog_cfg_opts removed
- loading own eBPF program via xsk_setup_xdp_prog functionality removed
Since v1:
- struct bpf_prog_cfg improved for backward/forward compatibility
- API xsk_update_xskmap renamed to xsk_socket__update_xskmap
- commit message formatting fixed
Mariusz Dudek (2):
libbpf: separate XDP program load with xsk socket creation samples/bpf: sample application for eBPF load and socket creation
split
samples/bpf/Makefile | 4 +- samples/bpf/xdpsock.h | 8 ++ samples/bpf/xdpsock_ctrl_proc.c | 187 ++++++++++++++++++++++++++++++++ samples/bpf/xdpsock_user.c | 146 +++++++++++++++++++++++-- tools/lib/bpf/libbpf.map | 2 + tools/lib/bpf/xsk.c | 92 ++++++++++++++-- tools/lib/bpf/xsk.h | 5 + 7 files changed, 425 insertions(+), 19 deletions(-) create mode 100644 samples/bpf/xdpsock_ctrl_proc.c
Signature · 1 line
2.20.1
PATCH v6 1/2 libbpf: separate XDP program load with xsk socket creation
From: Mariusz Dudek <[email protected]>
Add support for separation of eBPF program load and xsk socket creation.
This is needed for use-case when you want to privide as little privileges as possible to the data plane application that will handle xsk socket creation and incoming traffic.
With this patch the data entity container can be run with only CAP_NET_RAW capability to fulfill its purpose of creating xsk socket and handling packages. In case your umem is larger or equal process limit for MEMLOCK you need either increase the limit or CAP_IPC_LOCK capability.
To resolve privileges issue two APIs are introduced:
- xsk_setup_xdp_prog - loads the built in XDP program. It can also return xsks_map_fd which is needed by unprivileged process to update xsks_map with AF_XDP socket "fd"
- xsk_socket__update_xskmap - inserts an AF_XDP socket into an xskmap for a particular xsk_socket
--- tools/lib/bpf/libbpf.map | 2 + tools/lib/bpf/xsk.c | 92 ++++++++++++++++++++++++++++++++++++---- tools/lib/bpf/xsk.h | 5 +++ 3 files changed, 90 insertions(+), 9 deletions(-)
tools/lib/bpf/libbpf.map +2 -0
@@ -345,4 +345,6 @@ LIBBPF_0.3.0 { btf__parse_split; btf__new_empty_split; btf__new_split; + xsk_setup_xdp_prog; + xsk_socket__update_xskmap; } LIBBPF_0.2.0;
tools/lib/bpf/xsk.c +83 -9
127 lines not on this page · raw message
tools/lib/bpf/xsk.h +5 -0
@@ -204,6 +204,11 @@ struct xsk_umem_config {__u32 flags; }; +LIBBPF_API int xsk_setup_xdp_prog(int ifindex, +int *xsks_map_fd); +LIBBPF_API int xsk_socket__update_xskmap(struct xsk_socket *xsk, +int xsks_map_fd); +/* Flags for the libbpf_flags field. */ #define XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD (1 << 0)
Signature · 1 line
2.20.1
PATCH v6 2/2 samples/bpf: sample application for eBPF load and socket creation split
In reply to [email protected]From: Mariusz Dudek <[email protected]>
Introduce a sample program to demonstrate the control and data plane split. For the control plane part a new program called xdpsock_ctrl_proc is introduced. For the data plane part, some code was added to xdpsock_user.c to act as the data plane entity.
Application xdpsock_ctrl_proc works as control entity with sudo privileges (CAP_SYS_ADMIN and CAP_NET_ADMIN are sufficient) and the extended xdpsock as data plane entity with CAP_NET_RAW capability only.
Usage example:
sudo ./samples/bpf/xdpsock_ctrl_proc -i <interface>
sudo ./samples/bpf/xdpsock -i <interface> -q <queue_id>
-n <interval> -N -l -R
--- samples/bpf/Makefile | 4 +- samples/bpf/xdpsock.h | 8 ++ samples/bpf/xdpsock_ctrl_proc.c | 187 ++++++++++++++++++++++++++++++++ samples/bpf/xdpsock_user.c | 146 +++++++++++++++++++++++-- 4 files changed, 335 insertions(+), 10 deletions(-) create mode 100644 samples/bpf/xdpsock_ctrl_proc.c
samples/bpf/Makefile +3 -1
@@ -48,6 +48,7 @@ tprogs-y += syscall_tp tprogs-y += cpustat tprogs-y += xdp_adjust_tail tprogs-y += xdpsock +tprogs-y += xdpsock_ctrl_proc tprogs-y += xsk_fwd tprogs-y += xdp_fwd tprogs-y += task_fd_query
@@ -105,6 +106,7 @@ syscall_tp-objs := syscall_tp_user.o cpustat-objs := cpustat_user.o xdp_adjust_tail-objs := xdp_adjust_tail_user.o xdpsock-objs := xdpsock_user.o +xdpsock_ctrl_proc-objs := xdpsock_ctrl_proc.o xsk_fwd-objs := xsk_fwd.o xdp_fwd-objs := xdp_fwd_user.o task_fd_query-objs := task_fd_query_user.o $(TRACE_HELPERS)
@@ -202,7 +204,7 @@ TPROGLDLIBS_tracex4 += -lrt TPROGLDLIBS_trace_output += -lrt TPROGLDLIBS_map_perf_test += -lrt TPROGLDLIBS_test_overhead += -lrt -TPROGLDLIBS_xdpsock += -pthread +TPROGLDLIBS_xdpsock += -pthread -lcap TPROGLDLIBS_xsk_fwd += -pthread # Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
samples/bpf/xdpsock.h +8 -0
@@ -8,4 +8,12 @@#define MAX_SOCKS 4 +#define SOCKET_NAME "sock_cal_bpf_fd" +#define MAX_NUM_OF_CLIENTS 10 + +#define CLOSE_CONN 1 + +typedef __u64 u64 ; +typedef __u32 u32 ; +#endif /* XDPSOCK_H */
samples/bpf/xdpsock_ctrl_proc.c new file +187 -0
187 lines not on this page · raw message
samples/bpf/xdpsock_user.c +137 -9
248 lines not on this page · raw message
Signature · 1 line
2.20.1
Re: [PATCH v6 bpf-next 2/2] samples/bpf: sample application for eBPF load and socket creation split
Quoted 33 lines · On Wed, Dec 2, 2020 at 2:39 AM <[email protected]> wrote:
int
main(int argc, char **argv)
{samples/bpf/xdpsock_user.c +17
-4+struct __user_cap_header_struct hdr = { _LINUX_CAPABILITY_VERSION_3,0 }; +struct __user_cap_data_struct data[2 ] = { {0 } };struct rlimit r = {RLIM_INFINITY ,RLIM_INFINITY };bool rx = false, tx = false;struct xsk_umem_info *umem;struct bpf_object *obj; +int xsks_map_fd =0 ;pthread_t pt;int i, ret;void *bufs;parse_command_line (argc, argv); -if (setrlimit (RLIMIT_MEMLOCK , &r)) { -fprintf (stderr,"ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n" , -strerror (errno)); -exit (EXIT_FAILURE ); +if (opt_reduced_cap) { +if (capget (&hdr, data) <0 ) +fprintf (stderr,"Error getting capabilities\n" ); + + data->effective &=CAP_TO_MASK (CAP_NET_RAW ); + data->permitted &=CAP_TO_MASK (CAP_NET_RAW ); + +if (capset (&hdr, data) <0 ) +fprintf (stderr,"Setting capabilities failed\n" ); + +if (capget (&hdr, data) <0 ) { +fprintf (stderr,"Error getting capabilities\n" ); + }else { + fprintf(stderr, "Capabilities EFF %x Caps INH %x Caps Per %x\n",
samples/bpf/xdpsock_user.c static void apply_setsockopt(struct xsk_socket_info *xsk) · line 1617
Quoted 10 lines
samples/bpf/xdpsock_user.c +10
-0+ data[0].effective, data[0].inheritable, data[0].permitted); +fprintf (stderr,"Capabilities EFF %x Caps INH %x Caps Per %x\n" , + data[1 ].effective, data[1 ].inheritable, data[1 ].permitted); + } + }else { +if (setrlimit (RLIMIT_MEMLOCK , &r)) { +fprintf (stderr,"ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n" , +strerror (errno)); +exit (EXIT_FAILURE ); + }
Due to this hunk the patch had an unpleasant conflict with Roman's set and I had to drop this set from bpf-next.
Please rebase and resend.
But it made me look into this change...why did you make rlimit conditional here? That doesn't look right.
Re: [PATCH v6 bpf-next 2/2] samples/bpf: sample application for eBPF load and socket creation split
Quoted 50 lines · Alexei Starovoitov
Quoted 33 lines · On Wed, Dec 2, 2020 at 2:39 AM <[email protected]> wrote:
int
main(int argc, char **argv)
{samples/bpf/xdpsock_user.c +17
-4+struct __user_cap_header_struct hdr = { _LINUX_CAPABILITY_VERSION_3,0 }; +struct __user_cap_data_struct data[2 ] = { {0 } };struct rlimit r = {RLIM_INFINITY ,RLIM_INFINITY };bool rx = false, tx = false;struct xsk_umem_info *umem;struct bpf_object *obj; +int xsks_map_fd =0 ;pthread_t pt;int i, ret;void *bufs;parse_command_line (argc, argv); -if (setrlimit (RLIMIT_MEMLOCK , &r)) { -fprintf (stderr,"ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n" , -strerror (errno)); -exit (EXIT_FAILURE ); +if (opt_reduced_cap) { +if (capget (&hdr, data) <0 ) +fprintf (stderr,"Error getting capabilities\n" ); + + data->effective &=CAP_TO_MASK (CAP_NET_RAW ); + data->permitted &=CAP_TO_MASK (CAP_NET_RAW ); + +if (capset (&hdr, data) <0 ) +fprintf (stderr,"Setting capabilities failed\n" ); + +if (capget (&hdr, data) <0 ) { +fprintf (stderr,"Error getting capabilities\n" ); + }else { + fprintf(stderr, "Capabilities EFF %x Caps INH %x Caps Per %x\n",samples/bpf/xdpsock_user.c static void apply_setsockopt(struct xsk_socket_info *xsk) · line 1617
Quoted 10 lines
samples/bpf/xdpsock_user.c +10
-0+ data[0].effective, data[0].inheritable, data[0].permitted); +fprintf (stderr,"Capabilities EFF %x Caps INH %x Caps Per %x\n" , + data[1 ].effective, data[1 ].inheritable, data[1 ].permitted); + } + }else { +if (setrlimit (RLIMIT_MEMLOCK , &r)) { +fprintf (stderr,"ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n" , +strerror (errno)); +exit (EXIT_FAILURE ); + }Due to this hunk the patch had an unpleasant conflict with Roman's set and I had to drop this set from bpf-next.
Please rebase and resend.
Quoted 2 lines
But it made me look into this change...why did you make rlimit conditional here? That doesn't look right.
RLIMIT_MEMLOCK was conditioned before, so I didn't change it. It is not in my branch "if (opt_reduced_cap)" because RLIMIT_MEMLOCK requires additional CAP_IPC_LOCK and my main task was to made it possible to use xsk_socket creation and this example with only CAP_NET_RAW.
As described in the cover letter "In case your umem is larger or equal process limit for MEMLOCK you need either increase the limit or CAP_IPC_LOCK capability."
Andrii Nakryiko
AppliedQuoted 88 lines · On Wed, Dec 2, 2020 at 2:39 AM <[email protected]> wrote:
From: Mariusz Dudek <[email protected]>
This patch series adds support for separation of eBPF program load and xsk socket creation. In for example a Kubernetes environment you can have an
AF_XDPCNI or daemonset that is responsible for launching pods that execute an application usingAF_XDPsockets. It is desirable that the pod runs with as low privileges as possible,CAP_NET_RAWin this case, and that all operations that require privileges are contained in the CNI or daemonset.In this case, you have to be able separate ePBF program load from xsk socket creation.
Currently, this will not work with the
xsk_socket__createAPIs because you need to haveCAP_NET_ADMINprivileges to load eBPF program andCAP_SYS_ADMINprivileges to create updatexsk_bpf_maps. To be exactxsk_set_bpf_mapsdoes not need those privileges but it takes theprog_fdandxsks_map_fdand those are known only to process that was loading eBPF program. The apibpf_prog_get_fd_by_idthat looks up the fd of the prog using anprog_idandbpf_map_get_fd_by_idthat looks forxsks_map_fdusinbmap_idboth requiresCAP_SYS_ADMIN.With this patch, the pod can be run with
CAP_NET_RAWcapability only. In case your umem is larger or equal process limit for MEMLOCK you need either increase the limit orCAP_IPC_LOCKcapability. Without this patch in case of insufficient rights ENOPERM is returned byxsk_socket__create.To resolve this privileges issue two new APIs are introduced:
-xsk_setup_xdp_prog- loads the built in XDP program. It can also returnxsks_map_fdwhich is needed by unprivileged process to updatexsks_mapwithAF_XDPsocket "fd"
-xsk_sokcet__update_xskmap- inserts anAF_XDPsocket into an xskmap for a particularxsk_socketUsage example:
intxsk_setup_xdp_prog(int ifindex, int *xsks_map_fd)int
xsk_socket__update_xskmap(struct xsk_socket *xsk, int xsks_map_fd);Inserts
AF_XDPsocket "fd" into the xskmap.The first patch introduces the new APIs. The second patch provides a new sample applications working as control and modification to existing xdpsock application to work with less privileges.
This patch set is based on bpf-next commit
ba0581749fec(net, xdp, xsk: fix__sk_mark_napi_id_oncenapi_iderror)Since v5
- fixedsample/bpf/xdpsock_user.cto resolve merge conflictsSince v4
- sample/bpf/Makefile issues fixedSince v3:
-force_set_mapflag removed
- leaking of xsk struct fixed
- unified function error returning policy implementedSince v2:
- new APIs moved itto LIBBPF_0.3.0 section
- structbpf_prog_cfg_optsremoved
- loading own eBPF program viaxsk_setup_xdp_progfunctionality removedSince v1:
- structbpf_prog_cfgimproved for backward/forward compatibility
- APIxsk_update_xskmaprenamed toxsk_socket__update_xskmap
- commit message formatting fixedMariusz Dudek (2):
libbpf: separate XDP program load with xsk socket creationsamples/bpf: sample application for eBPF load and socket creation
splitsamples/bpf/Makefile | 4 +- samples/bpf/xdpsock.h | 8 ++ samples/bpf/xdpsock_ctrl_proc.c | 187 ++++++++++++++++++++++++++++++++ samples/bpf/xdpsock_user.c | 146 +++++++++++++++++++++++-- tools/lib/bpf/libbpf.map | 2 + tools/lib/bpf/xsk.c | 92 ++++++++++++++-- tools/lib/bpf/xsk.h | 5 + 7 files changed, 425 insertions(+), 19 deletions(-) create mode 100644 samples/bpf/xdpsock_ctrl_proc.c
Quoted 3 lines
--
2.20.1
Applied to bpf-next. For the future, please carry over Acked-by you got, thanks. I've added Magnus's ones back.
This series was applied to bpf/bpf-next.git (refs/heads/master):
Hello:
This series was applied to bpf/bpf-next.git (refs/heads/master):
Quoted 11 lines · On Wed, 2 Dec 2020 11:39:21 +0100 you wrote:
From: Mariusz Dudek <[email protected]>
This patch series adds support for separation of eBPF program load and xsk socket creation. In for example a Kubernetes environment you can have an
AF_XDPCNI or daemonset that is responsible for launching pods that execute an application usingAF_XDPsockets. It is desirable that the pod runs with as low privileges as possible,CAP_NET_RAWin this case, and that all operations that require privileges are contained in the CNI or daemonset.
Quoted 1 line
[...]
Here is the summary with links:
- [v6,bpf-next,1/2] libbpf: separate XDP program load with xsk socket creation
https://git.kernel.org/bpf/bpf-next/c/56ab028af72b - [v6,bpf-next,2/2] samples/bpf: sample application for eBPF load and socket creation split https://git.kernel.org/bpf/bpf-next/c/d6482b4367ac
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html