Message ID | 1552315868-25681-1-git-send-email-magnus.karlsson@intel.com |
---|---|
State | Changes Requested |
Delegated to: | BPF Maintainers |
Headers | show |
Series | [bpf] libbpf: fix to reject unknown flags in xsk_socket__create() | expand |
On Mon, Mar 11, 2019 at 7:52 AM Magnus Karlsson <magnus.karlsson@intel.com> wrote: > > In xsk_socket__create(), the libbpf_flags field was not checked for > setting currently unused/unknown flags. This patch fixes that by returning > -EINVAL if the user has set any flag that is not in use at this point > in time. > > Fixes: 1cad07884239 ("libbpf: add support for using AF_XDP sockets") > Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com> > --- > tools/lib/bpf/xsk.c | 15 +++++++++++---- > 1 file changed, 11 insertions(+), 4 deletions(-) > > diff --git a/tools/lib/bpf/xsk.c b/tools/lib/bpf/xsk.c > index f98ac82..912f942 100644 > --- a/tools/lib/bpf/xsk.c > +++ b/tools/lib/bpf/xsk.c > @@ -126,8 +126,8 @@ static void xsk_set_umem_config(struct xsk_umem_config *cfg, > cfg->frame_headroom = usr_cfg->frame_headroom; > } > > -static void xsk_set_xdp_socket_config(struct xsk_socket_config *cfg, > - const struct xsk_socket_config *usr_cfg) > +static int xsk_set_xdp_socket_config(struct xsk_socket_config *cfg, > + const struct xsk_socket_config *usr_cfg) > { > if (!usr_cfg) { > cfg->rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS; > @@ -135,14 +135,19 @@ static void xsk_set_xdp_socket_config(struct xsk_socket_config *cfg, > cfg->libbpf_flags = 0; > cfg->xdp_flags = 0; > cfg->bind_flags = 0; > - return; > + return 0; > } > > + if (cfg->libbpf_flags & ~XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD) > + return -EINVAL; > + Shouldn't this be checking usr_cfg->libbpf_flags instead? Also, what about bind_flags and xdp_flags? For those we are deferring to underlying systems to check extra flags? > cfg->rx_size = usr_cfg->rx_size; > cfg->tx_size = usr_cfg->tx_size; > cfg->libbpf_flags = usr_cfg->libbpf_flags; > cfg->xdp_flags = usr_cfg->xdp_flags; > cfg->bind_flags = usr_cfg->bind_flags; > + > + return 0; > } > > int xsk_umem__create(struct xsk_umem **umem_ptr, void *umem_area, __u64 size, > @@ -557,7 +562,9 @@ int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname, > } > strncpy(xsk->ifname, ifname, IFNAMSIZ); > > - xsk_set_xdp_socket_config(&xsk->config, usr_config); > + err = xsk_set_xdp_socket_config(&xsk->config, usr_config); > + if (err) > + goto out_socket; > > if (rx) { > err = setsockopt(xsk->fd, SOL_XDP, XDP_RX_RING, > -- > 2.7.4 >
On Mon, Mar 11, 2019 at 5:54 PM Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote: > > On Mon, Mar 11, 2019 at 7:52 AM Magnus Karlsson > <magnus.karlsson@intel.com> wrote: > > > > In xsk_socket__create(), the libbpf_flags field was not checked for > > setting currently unused/unknown flags. This patch fixes that by returning > > -EINVAL if the user has set any flag that is not in use at this point > > in time. > > > > Fixes: 1cad07884239 ("libbpf: add support for using AF_XDP sockets") > > Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com> > > --- > > tools/lib/bpf/xsk.c | 15 +++++++++++---- > > 1 file changed, 11 insertions(+), 4 deletions(-) > > > > diff --git a/tools/lib/bpf/xsk.c b/tools/lib/bpf/xsk.c > > index f98ac82..912f942 100644 > > --- a/tools/lib/bpf/xsk.c > > +++ b/tools/lib/bpf/xsk.c > > @@ -126,8 +126,8 @@ static void xsk_set_umem_config(struct xsk_umem_config *cfg, > > cfg->frame_headroom = usr_cfg->frame_headroom; > > } > > > > -static void xsk_set_xdp_socket_config(struct xsk_socket_config *cfg, > > - const struct xsk_socket_config *usr_cfg) > > +static int xsk_set_xdp_socket_config(struct xsk_socket_config *cfg, > > + const struct xsk_socket_config *usr_cfg) > > { > > if (!usr_cfg) { > > cfg->rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS; > > @@ -135,14 +135,19 @@ static void xsk_set_xdp_socket_config(struct xsk_socket_config *cfg, > > cfg->libbpf_flags = 0; > > cfg->xdp_flags = 0; > > cfg->bind_flags = 0; > > - return; > > + return 0; > > } > > > > + if (cfg->libbpf_flags & ~XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD) > > + return -EINVAL; > > + > > Shouldn't this be checking usr_cfg->libbpf_flags instead? Arrghh! Yes you are of course right. Thanks for catching this. Will spin a v2. > Also, what about bind_flags and xdp_flags? For those we are deferring > to underlying systems to check extra flags? I defer that to the underlying calls so I do not have to keep this code up to date with changes to those flags. Thanks: Magnus > > > cfg->rx_size = usr_cfg->rx_size; > > cfg->tx_size = usr_cfg->tx_size; > > cfg->libbpf_flags = usr_cfg->libbpf_flags; > > cfg->xdp_flags = usr_cfg->xdp_flags; > > cfg->bind_flags = usr_cfg->bind_flags; > > + > > + return 0; > > } > > > > int xsk_umem__create(struct xsk_umem **umem_ptr, void *umem_area, __u64 size, > > @@ -557,7 +562,9 @@ int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname, > > } > > strncpy(xsk->ifname, ifname, IFNAMSIZ); > > > > - xsk_set_xdp_socket_config(&xsk->config, usr_config); > > + err = xsk_set_xdp_socket_config(&xsk->config, usr_config); > > + if (err) > > + goto out_socket; > > > > if (rx) { > > err = setsockopt(xsk->fd, SOL_XDP, XDP_RX_RING, > > -- > > 2.7.4 > >
diff --git a/tools/lib/bpf/xsk.c b/tools/lib/bpf/xsk.c index f98ac82..912f942 100644 --- a/tools/lib/bpf/xsk.c +++ b/tools/lib/bpf/xsk.c @@ -126,8 +126,8 @@ static void xsk_set_umem_config(struct xsk_umem_config *cfg, cfg->frame_headroom = usr_cfg->frame_headroom; } -static void xsk_set_xdp_socket_config(struct xsk_socket_config *cfg, - const struct xsk_socket_config *usr_cfg) +static int xsk_set_xdp_socket_config(struct xsk_socket_config *cfg, + const struct xsk_socket_config *usr_cfg) { if (!usr_cfg) { cfg->rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS; @@ -135,14 +135,19 @@ static void xsk_set_xdp_socket_config(struct xsk_socket_config *cfg, cfg->libbpf_flags = 0; cfg->xdp_flags = 0; cfg->bind_flags = 0; - return; + return 0; } + if (cfg->libbpf_flags & ~XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD) + return -EINVAL; + cfg->rx_size = usr_cfg->rx_size; cfg->tx_size = usr_cfg->tx_size; cfg->libbpf_flags = usr_cfg->libbpf_flags; cfg->xdp_flags = usr_cfg->xdp_flags; cfg->bind_flags = usr_cfg->bind_flags; + + return 0; } int xsk_umem__create(struct xsk_umem **umem_ptr, void *umem_area, __u64 size, @@ -557,7 +562,9 @@ int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname, } strncpy(xsk->ifname, ifname, IFNAMSIZ); - xsk_set_xdp_socket_config(&xsk->config, usr_config); + err = xsk_set_xdp_socket_config(&xsk->config, usr_config); + if (err) + goto out_socket; if (rx) { err = setsockopt(xsk->fd, SOL_XDP, XDP_RX_RING,
In xsk_socket__create(), the libbpf_flags field was not checked for setting currently unused/unknown flags. This patch fixes that by returning -EINVAL if the user has set any flag that is not in use at this point in time. Fixes: 1cad07884239 ("libbpf: add support for using AF_XDP sockets") Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com> --- tools/lib/bpf/xsk.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)