Skip to contentSkip to conversation

libbpf: add support for privileged/unprivileged control separation

PATCH Superseded [email protected] 3 participants 5 messages Nov 12, 2020 – Nov 13, 2020

Subsystems (5)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 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 - prepares bpf program if given and loads it on a selected network interface or loads the built in XDP program, if no XDP program is supplied. 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, struct bpf_prog_cfg *cfg,
int *xsks_map_fd)

if cfg == NULL, then the default program is loaded.

Instead of NULL user can pass pointer to struct
bpf_prog_cfg and provide own bpf program.

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 09a3dac7b579 ("bpf: Fix NULL dereference in bpf_task_storage")

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 | 184 ++++++++++++++++++++++++++++++++
 samples/bpf/xdpsock_user.c      | 146 +++++++++++++++++++++++--
 tools/lib/bpf/libbpf.map        |   2 +
 tools/lib/bpf/xsk.c             | 160 ++++++++++++++++++++++-----
 tools/lib/bpf/xsk.h             |  15 +++
 7 files changed, 483 insertions(+), 36 deletions(-)
 create mode 100644 samples/bpf/xdpsock_ctrl_proc.c
Signature · 1 line
2.20.1
#1 lore raw

PATCH v2 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 - prepares bpf program if given and loads it on a selected network interface or loads the built in XDP program, if no XDP program is supplied. 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      | 160 ++++++++++++++++++++++++++++++++-------
 tools/lib/bpf/xsk.h      |  15 ++++
 3 files changed, 151 insertions(+), 26 deletions(-)
tools/lib/bpf/libbpf.map +2 -0
index 29ff4807b909..73aa12388055 100644
@@ -336,6 +336,8 @@ LIBBPF_0.2.0 {
 		perf_buffer__epoll_fd;
 		perf_buffer__consume_buffer;
 		xsk_socket__create_shared;
+		xsk_setup_xdp_prog;
+		xsk_socket__update_xskmap;
 } LIBBPF_0.1.0;
 
 LIBBPF_0.3.0 {
tools/lib/bpf/xsk.c +134 -26

235 lines not on this page · raw message

tools/lib/bpf/xsk.h +15 -0
index 1069c46364ff..c852ec742437 100644
@@ -201,6 +201,21 @@ struct xsk_umem_config {
 	__u32 flags;
 };
 
+struct bpf_prog_cfg_opts {
+	size_t sz; /* size of this struct for forward/backward compatibility */
+	struct bpf_insn *prog;
+	const char *license;
+	size_t insns_cnt;
+	int xsks_map_fd;
+};
+#define bpf_prog_cfg_opts__last_field xsks_map_fd
+
+LIBBPF_API int xsk_setup_xdp_prog(int ifindex,
+				  struct bpf_prog_cfg_opts *opts,
+				  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
#2 lore raw

Re: [PATCH v2 bpf-next 1/2] libbpf: separate XDP program load with xsk socket creation

Quoted 39 lines · On Thu, Nov 12, 2020 at 12:58 AM <[email protected]> wrote:

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 - prepares bpf program if given and loads it on a selected network interface or loads the built in XDP program, if no XDP program is supplied. 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      | 160 ++++++++++++++++++++++++++++++++-------
 tools/lib/bpf/xsk.h      |  15 ++++
 3 files changed, 151 insertions(+), 26 deletions(-)
tools/lib/bpf/libbpf.map +0 -0
index 29ff4807b909..73aa12388055 100644

tools/lib/bpf/libbpf.map LIBBPF_0.2.0 { · line 341

Quoted 6 lines
tools/lib/bpf/libbpf.map +2 -0
@@ -336,6 +336,8 @@ LIBBPF_0.2.0 {
                perf_buffer__epoll_fd;
                perf_buffer__consume_buffer;
                xsk_socket__create_shared;
+               xsk_setup_xdp_prog;
+               xsk_socket__update_xskmap;
 } LIBBPF_0.1.0;

New APIs have to go into LIBBPF_0.3.0 section now.

Quoted 2 lines
tools/lib/bpf/libbpf.map +0 -0
 LIBBPF_0.3.0 {

[...]

Quoted 10 lines
tools/lib/bpf/libbpf.map +5 -0
+static int __xsk_setup_xdp_prog(struct xsk_socket *_xdp,
+                               struct bpf_prog_cfg_opts *cfg,
+                               bool force_set_map,
+                               int *xsks_map_fd)
 {
+       struct xsk_socket *xsk = _xdp;
        struct xsk_ctx *ctx = xsk->ctx;
        __u32 prog_id = 0;
        int err;

tools/lib/bpf/libbpf.map static int xsk_setup_xdp_prog(struct xsk_socket *xsk) · line 636

Quoted 7 lines
tools/lib/bpf/libbpf.map +1 -3
@@ -578,14 +633,17 @@ static int xsk_setup_xdp_prog(struct xsk_socket *xsk)
                return err;
 
        if (!prog_id) {
-               err = xsk_create_bpf_maps(xsk);
-               if (err)
-                       return err;
+               if (!cfg || !cfg->insns_cnt) {

you can't do this, use OPTS_GET() macro to access fields of opts struct.

tools/lib/bpf/libbpf.map static int xsk_setup_xdp_prog(struct xsk_socket *xsk) · line 641

Quoted 5 lines
tools/lib/bpf/libbpf.map +5 -0
+                       err = xsk_create_bpf_maps(xsk);
+                       if (err)
+                               return err;
+               } else {
+                       ctx->xsks_map_fd = cfg->xsks_map_fd;

same

Quoted 11 lines
tools/lib/bpf/libbpf.map +3 -3
+               }
 
-               err = xsk_load_xdp_prog(xsk);
+               err = xsk_load_xdp_prog(xsk, cfg);
                if (err) {
-                       xsk_delete_bpf_maps(xsk);
-                       return err;
+                       goto err_load_xdp_prog;
                }
        } else {
                ctx->prog_fd = bpf_prog_get_fd_by_id(prog_id);

[...]

Quoted 17 lines

int xsk_socket__create_shared(struct xsk_socket **xsk_ptr,
const char *ifname,
__u32 queue_id, struct xsk_umem *umem,

tools/lib/bpf/libbpf.map +1 -1
@@ -838,7 +946,7 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr,
        ctx->prog_fd = -1;
 
        if (!(xsk->config.libbpf_flags & XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD)) {
-               err = xsk_setup_xdp_prog(xsk);
+               err = __xsk_setup_xdp_prog(xsk, NULL, false, NULL);
                if (err)
                        goto out_mmap_tx;
        }
tools/lib/bpf/xsk.h +0 -0
index 1069c46364ff..c852ec742437 100644

tools/lib/bpf/xsk.h struct xsk_umem_config { · line 204

Quoted 4 lines
tools/lib/bpf/xsk.h +1 -0
@@ -201,6 +201,21 @@ struct xsk_umem_config {
        __u32 flags;
 };
 
+struct bpf_prog_cfg_opts {

The name of this struct doesn't really match an API it's used in, it looks to be related to struct bpf_program, which is extremely misleading. Why didn't you go with something like xdp_setup_xdp_prog_opts?

Also, so far we've been following the convention that non-optional parameters are passed directly as function arguments, with all the optional things put into opts. Looking at this struct, prog and insns_cnt look non-optional. Not sure about the license. Also, it seems strange to have xsks_map_fd in opts struct and as an output parameter... If that's really in/out param, you can do OPTS_SET() and pass it back through the same opts struct.

But in general, I'm also surprised that this API is using the bpf_insn array as an input. Do people really construct such a low-level set of instructions manually? Seems like a rather painful API.

Björn, Magnus, please take a look as well and chime in on API design (I have too little context on XSK use cases).

Quoted 20 lines
tools/lib/bpf/xsk.h +14 -1
+       size_t sz; /* size of this struct for forward/backward compatibility */
+       struct bpf_insn *prog;
+       const char *license;
+       size_t insns_cnt;
+       int xsks_map_fd;
+};
+#define bpf_prog_cfg_opts__last_field xsks_map_fd
+
+LIBBPF_API int xsk_setup_xdp_prog(int ifindex,
+                                 struct bpf_prog_cfg_opts *opts,
+                                 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)
 
--

2.20.1

#3 lore raw

Re: [PATCH v2 bpf-next 1/2] libbpf: separate XDP program load with xsk socket creation

Quoted 133 lines · Andrii Nakryiko
Quoted 39 lines · On Thu, Nov 12, 2020 at 12:58 AM <[email protected]> wrote:

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 - prepares bpf program if given and loads it on a selected network interface or loads the built in XDP program, if no XDP program is supplied. 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      | 160 ++++++++++++++++++++++++++++++++-------
 tools/lib/bpf/xsk.h      |  15 ++++
 3 files changed, 151 insertions(+), 26 deletions(-)
tools/lib/bpf/libbpf.map +0 -0
index 29ff4807b909..73aa12388055 100644

tools/lib/bpf/libbpf.map LIBBPF_0.2.0 { · line 341

Quoted 6 lines
tools/lib/bpf/libbpf.map +2 -0
@@ -336,6 +336,8 @@ LIBBPF_0.2.0 {
                perf_buffer__epoll_fd;
                perf_buffer__consume_buffer;
                xsk_socket__create_shared;
+               xsk_setup_xdp_prog;
+               xsk_socket__update_xskmap;
 } LIBBPF_0.1.0;

New APIs have to go into LIBBPF_0.3.0 section now.

Quoted 2 lines
tools/lib/bpf/libbpf.map +0 -0
 LIBBPF_0.3.0 {

[...]

Quoted 10 lines
tools/lib/bpf/libbpf.map +5 -0
+static int __xsk_setup_xdp_prog(struct xsk_socket *_xdp,
+                               struct bpf_prog_cfg_opts *cfg,
+                               bool force_set_map,
+                               int *xsks_map_fd)
 {
+       struct xsk_socket *xsk = _xdp;
        struct xsk_ctx *ctx = xsk->ctx;
        __u32 prog_id = 0;
        int err;

tools/lib/bpf/libbpf.map static int xsk_setup_xdp_prog(struct xsk_socket *xsk) · line 636

Quoted 7 lines
tools/lib/bpf/libbpf.map +1 -3
@@ -578,14 +633,17 @@ static int xsk_setup_xdp_prog(struct xsk_socket *xsk)
                return err;
 
        if (!prog_id) {
-               err = xsk_create_bpf_maps(xsk);
-               if (err)
-                       return err;
+               if (!cfg || !cfg->insns_cnt) {

you can't do this, use OPTS_GET() macro to access fields of opts struct.

tools/lib/bpf/libbpf.map static int xsk_setup_xdp_prog(struct xsk_socket *xsk) · line 641

Quoted 5 lines
tools/lib/bpf/libbpf.map +5 -0
+                       err = xsk_create_bpf_maps(xsk);
+                       if (err)
+                               return err;
+               } else {
+                       ctx->xsks_map_fd = cfg->xsks_map_fd;

same

Quoted 11 lines
tools/lib/bpf/libbpf.map +3 -3
+               }
 
-               err = xsk_load_xdp_prog(xsk);
+               err = xsk_load_xdp_prog(xsk, cfg);
                if (err) {
-                       xsk_delete_bpf_maps(xsk);
-                       return err;
+                       goto err_load_xdp_prog;
                }
        } else {
                ctx->prog_fd = bpf_prog_get_fd_by_id(prog_id);

[...]

Quoted 17 lines

int xsk_socket__create_shared(struct xsk_socket **xsk_ptr,
const char *ifname,
__u32 queue_id, struct xsk_umem *umem,

tools/lib/bpf/libbpf.map +1 -1
@@ -838,7 +946,7 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr,
        ctx->prog_fd = -1;
 
        if (!(xsk->config.libbpf_flags & XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD)) {
-               err = xsk_setup_xdp_prog(xsk);
+               err = __xsk_setup_xdp_prog(xsk, NULL, false, NULL);
                if (err)
                        goto out_mmap_tx;
        }
tools/lib/bpf/xsk.h +0 -0
index 1069c46364ff..c852ec742437 100644

tools/lib/bpf/xsk.h struct xsk_umem_config { · line 204

Quoted 4 lines
tools/lib/bpf/xsk.h +1 -0
@@ -201,6 +201,21 @@ struct xsk_umem_config {
        __u32 flags;
 };
 
+struct bpf_prog_cfg_opts {

The name of this struct doesn't really match an API it's used in, it looks to be related to struct bpf_program, which is extremely misleading. Why didn't you go with something like xdp_setup_xdp_prog_opts?

Also, so far we've been following the convention that non-optional parameters are passed directly as function arguments, with all the optional things put into opts. Looking at this struct, prog and insns_cnt look non-optional. Not sure about the license. Also, it seems strange to have xsks_map_fd in opts struct and as an output parameter... If that's really in/out param, you can do OPTS_SET() and pass it back through the same opts struct.

Quoted 6 lines

But in general, I'm also surprised that this API is using the bpf_insn array as an input. Do people really construct such a low-level set of instructions manually? Seems like a rather painful API.

Björn, Magnus, please take a look as well and chime in on API design (I have too little context on XSK use cases).

Yes, it is much too macho to write an eBPF program directly using assembly :-). My suggestion is to get rid of this whole struct bpf_prog_cfg_opts completely and the parameter in the call. There is no reason to provide functionality to load your own program in this API as it is already so easy using the existing libbpf APIs e.g., with bpf_prog_load_xattr() followed by bpf_set_link_xdp_fd(). So I suggest the following:

LIBBPF_API int xsk_setup_default_prog(int ifindex, int *xsks_map_fd);

It is strictly a helper function for setting up the default XDP program from a process that has the privilege to do this.

Thanks Andrii for taking a look at this and thanks Mariusz for taking a stab at this.

Quoted 21 lines
Quoted 21 lines
tools/lib/bpf/xsk.h +14 -1
+       size_t sz; /* size of this struct for forward/backward compatibility */
+       struct bpf_insn *prog;
+       const char *license;
+       size_t insns_cnt;
+       int xsks_map_fd;
+};
+#define bpf_prog_cfg_opts__last_field xsks_map_fd
+
+LIBBPF_API int xsk_setup_xdp_prog(int ifindex,
+                                 struct bpf_prog_cfg_opts *opts,
+                                 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)
 
--

2.20.1

#4 lore raw

PATCH v2 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 | 184 ++++++++++++++++++++++++++++++++
 samples/bpf/xdpsock_user.c      | 146 +++++++++++++++++++++++--
 4 files changed, 332 insertions(+), 10 deletions(-)
 create mode 100644 samples/bpf/xdpsock_ctrl_proc.c
samples/bpf/Makefile +3 -1
index aeebf5d12f32..8fb8be1c0144 100644
@@ -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 := bpf_load.o task_fd_query_user.o $(TRACE_HELPERS)
@@ -204,7 +206,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
index b7eca15c78cc..fd70cce60712 100644
@@ -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 +184 -0

184 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