xsk: fix non-existing flag/option behavior
Subsystems (4)
cover xsk: fix non-existing flag/option behavior
These two patches addresses the behavior of xsk_bind and Tx descriptor validation, when non-existing flags are set.
Thanks,
Björn
Björn Töpel (2):
xsk: fix to reject invalid flags in xsk_bind
xsk: fix to reject invalid options in Tx descriptor
net/xdp/xsk.c | 5 ++++- net/xdp/xsk_queue.h | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-)
Signature · 1 line
2.19.1
PATCH 1/2 xsk: fix to reject invalid flags in xsk_bind
From: Björn Töpel <[email protected]>
Passing a non-existing flag in the sxdp_flags member of struct sockaddr_xdp was, incorrectly, silently ignored. This patch addresses that behavior, and rejects any non-existing flags.
We have examined existing user space code, and to our best knowledge, no one is relying on the current incorrect behavior. AF_XDP is still in its infancy, so from our perspective, the risk of breakage is very low, and addressing this problem now is important.
--- net/xdp/xsk.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/xdp/xsk.c +4 -1
@@ -407,6 +407,10 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)if (sxdp->sxdp_family !=AF_XDP )return -EINVAL ; + flags = sxdp->sxdp_flags; +if (flags & ~(XDP_SHARED_UMEM |XDP_COPY |XDP_ZEROCOPY )) +return -EINVAL ; +mutex_lock (&xs->mutex);if (xs->dev) { err = -EBUSY ;
@@ -425,7 +429,6 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) } qid = sxdp->sxdp_queue_id; - flags = sxdp->sxdp_flags;if (flags &XDP_SHARED_UMEM ) {struct xdp_sock *umem_xs;
Signature · 1 line
2.19.1
Re: [PATCH bpf 1/2] xsk: fix to reject invalid flags in xsk_bind
Quoted 22 lines · On Fri, 8 Mar 2019 08:57:26 +0100 Björn Töpel <[email protected]> wrote:
From: Björn Töpel <[email protected]>
Passing a non-existing flag in the
sxdp_flagsmember of structsockaddr_xdpwas, incorrectly, silently ignored. This patch addresses that behavior, and rejects any non-existing flags.We have examined existing user space code, and to our best knowledge, no one is relying on the current incorrect behavior.
AF_XDPis still in its infancy, so from our perspective, the risk of breakage is very low, and addressing this problem now is important.Fixes 965a99098443 ("xsk: add support for bind for Rx")Signed-off-by Björn Töpel <[email protected]>--- net/xdp/xsk.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/xdp/xsk.c +0
-0
net/xdp/xsk.c static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) · line 413
Quoted 7 lines
net/xdp/xsk.c +4
-0@@ -407,6 +407,10 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)if (sxdp->sxdp_family !=AF_XDP )return -EINVAL ; + flags = sxdp->sxdp_flags; +if (flags & ~(XDP_SHARED_UMEM |XDP_COPY |XDP_ZEROCOPY )) +return -EINVAL ; +
What about setting more than one flag at a time? Is it allowed/make any sense? After a quick look it seems that they exclude each other, e.g. you can't force a zero copy and copy mode at the same time. And for XDP_SHARED_UMEM two remaining flags can't be set.
So maybe check here also that only one particular flag is set by doing:
if (hweight32(flags & (XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY)) > 1)
return -EINVAL;
just like we do it for IFLA_XDP_FLAGS in net/core/rtnetlink.c?
Quoted 11 lines
net/xdp/xsk.c +0
-1mutex_lock (&xs->mutex);if (xs->dev) { err = -EBUSY ;@@ -425,7 +429,6 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) } qid = sxdp->sxdp_queue_id; - flags = sxdp->sxdp_flags;if (flags &XDP_SHARED_UMEM ) {struct xdp_sock *umem_xs;
Re: [PATCH bpf 1/2] xsk: fix to reject invalid flags in xsk_bind
Quoted 39 lines · Maciej Fijalkowski
Quoted 22 lines · On Fri, 8 Mar 2019 08:57:26 +0100 Björn Töpel <[email protected]> wrote:
From: Björn Töpel <[email protected]>
Passing a non-existing flag in the
sxdp_flagsmember of structsockaddr_xdpwas, incorrectly, silently ignored. This patch addresses that behavior, and rejects any non-existing flags.We have examined existing user space code, and to our best knowledge, no one is relying on the current incorrect behavior.
AF_XDPis still in its infancy, so from our perspective, the risk of breakage is very low, and addressing this problem now is important.Fixes 965a99098443 ("xsk: add support for bind for Rx")Signed-off-by Björn Töpel <[email protected]>--- net/xdp/xsk.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/xdp/xsk.c +0
-0net/xdp/xsk.c static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) · line 413
Quoted 7 lines
net/xdp/xsk.c +4
-0@@ -407,6 +407,10 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)if (sxdp->sxdp_family !=AF_XDP )return -EINVAL ; + flags = sxdp->sxdp_flags; +if (flags & ~(XDP_SHARED_UMEM |XDP_COPY |XDP_ZEROCOPY )) +return -EINVAL ; +What about setting more than one flag at a time? Is it allowed/make any sense? After a quick look it seems that they exclude each other, e.g. you can't force a zero copy and copy mode at the same time. And for
XDP_SHARED_UMEMtwo remaining flags can't be set.
Quoted 7 lines
So maybe check here also that only one particular flag is set by doing:
if (
hweight32(flags & (XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY))> 1)return -EINVAL;just like we do it for
IFLA_XDP_FLAGSinnet/core/rtnetlink.c?
We have flag semantic checks further down, and my rational was to *only* check unknown flags first. IMO the current patch is easier to understand, than your suggested one.
Thanks for taking a look!
Cheers,
Björn
Quoted 12 lines
Quoted 11 lines
net/xdp/xsk.c +0
-1mutex_lock (&xs->mutex);if (xs->dev) { err = -EBUSY ;@@ -425,7 +429,6 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) } qid = sxdp->sxdp_queue_id; - flags = sxdp->sxdp_flags;if (flags &XDP_SHARED_UMEM ) {struct xdp_sock *umem_xs;
Re: [PATCH bpf 1/2] xsk: fix to reject invalid flags in xsk_bind
Quoted 46 lines · On Fri, 8 Mar 2019 11:11:05 +0100 Björn Töpel <[email protected]> wrote:
Quoted 44 lines · Maciej Fijalkowski
Quoted 22 lines · On Fri, 8 Mar 2019 08:57:26 +0100 Björn Töpel <[email protected]> wrote:
From: Björn Töpel <[email protected]>
Passing a non-existing flag in the
sxdp_flagsmember of structsockaddr_xdpwas, incorrectly, silently ignored. This patch addresses that behavior, and rejects any non-existing flags.We have examined existing user space code, and to our best knowledge, no one is relying on the current incorrect behavior.
AF_XDPis still in its infancy, so from our perspective, the risk of breakage is very low, and addressing this problem now is important.Fixes 965a99098443 ("xsk: add support for bind for Rx")Signed-off-by Björn Töpel <[email protected]>--- net/xdp/xsk.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/xdp/xsk.c +0
-0net/xdp/xsk.c static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) · line 413
Quoted 7 lines
net/xdp/xsk.c +4
-0@@ -407,6 +407,10 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)if (sxdp->sxdp_family !=AF_XDP )return -EINVAL ; + flags = sxdp->sxdp_flags; +if (flags & ~(XDP_SHARED_UMEM |XDP_COPY |XDP_ZEROCOPY )) +return -EINVAL ; +What about setting more than one flag at a time? Is it allowed/make any sense? After a quick look it seems that they exclude each other, e.g. you can't force a zero copy and copy mode at the same time. And for
XDP_SHARED_UMEMtwo remaining flags can't be set.So maybe check here also that only one particular flag is set by doing:
if (
hweight32(flags & (XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY))> 1)return -EINVAL;
Quoted 7 lines
Quoted 2 lines
just like we do it for
IFLA_XDP_FLAGSinnet/core/rtnetlink.c?We have flag semantic checks further down, and my rational was to *only* check unknown flags first. IMO the current patch is easier to understand, than your suggested one.
Hmm thought that bailing out earlier would be better and we could drop the actual copy flags checks for shared umem. For xdp_umem_assign_dev() instead of passing flags you could just pass a boolean whether you're doing zero copy or not. And that brings up the question whether we really need a XDP_COPY flag?
Quoted 17 lines
Thanks for taking a look!
Cheers,
BjörnQuoted 12 lines
Quoted 11 lines
net/xdp/xsk.c +0
-1mutex_lock (&xs->mutex);if (xs->dev) { err = -EBUSY ;@@ -425,7 +429,6 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) } qid = sxdp->sxdp_queue_id; - flags = sxdp->sxdp_flags;if (flags &XDP_SHARED_UMEM ) {struct xdp_sock *umem_xs;
Re: [PATCH bpf 1/2] xsk: fix to reject invalid flags in xsk_bind
Quoted 1 line · Maciej Fijalkowski
[...]
Quoted 23 lines
Quoted 15 lines
Quoted 15 lines
net/xdp/xsk.c static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) · line 413
Quoted 4 lines
net/xdp/xsk.c +4
-0+ flags = sxdp->sxdp_flags; +if (flags & ~(XDP_SHARED_UMEM |XDP_COPY |XDP_ZEROCOPY )) +return -EINVAL ; +What about setting more than one flag at a time? Is it allowed/make any sense? After a quick look it seems that they exclude each other, e.g. you can't force a zero copy and copy mode at the same time. And for
XDP_SHARED_UMEMtwo remaining flags can't be set.So maybe check here also that only one particular flag is set by doing:
if (
hweight32(flags & (XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY))> 1)return -EINVAL;Quoted 7 lines
Quoted 2 lines
just like we do it for
IFLA_XDP_FLAGSinnet/core/rtnetlink.c?We have flag semantic checks further down, and my rational was to *only* check unknown flags first. IMO the current patch is easier to understand, than your suggested one.
Quoted 5 lines
Hmm thought that bailing out earlier would be better and we could drop the actual copy flags checks for shared umem. For
xdp_umem_assign_dev()instead of passing flags you could just pass a boolean whether you're doing zero copy or not. And that brings up the question whether we really need aXDP_COPYflag?
I'd prefer doing that as a follow-up patch.
XDP_COPY is needed to explicitly enable copy-mode. No flags is "select the best option", and COPY/ZEROCOPY is to explicitly select a mode.
Daniel Borkmann
maintainerRe: [PATCH bpf 1/2] xsk: fix to reject invalid flags in xsk_bind
Quoted 2 lines · Björn Töpel
On Fri, 8 Mar 2019 at 11:46, Maciej Fijalkowski
<[email protected]> wrote:
[...]
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
So maybe check here also that only one particular flag is set by doing:
if (
hweight32(flags & (XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY))> 1)return -EINVAL;just like we do it for
IFLA_XDP_FLAGSinnet/core/rtnetlink.c?We have flag semantic checks further down, and my rational was to *only* check unknown flags first. IMO the current patch is easier to understand, than your suggested one.
Quoted 6 lines
Quoted 4 lines
Hmm thought that bailing out earlier would be better and we could drop the actual copy flags checks for shared umem. For
xdp_umem_assign_dev()instead of passing flags you could just pass a boolean whether you're doing zero copy or not. And that brings up the question whether we really need aXDP_COPYflag?I'd prefer doing that as a follow-up patch.
Ok, follow-up for Maciej's suggestion is fine, imho, it would definitely make it more obvious resp. future proof such that rejecting some invalid combination doesn't get missed under some branch. Given it's currently not the case aside from the fix, rolling this into your next patches for bpf-next might be best. In any case, fixes look good, so applied, thanks!
Quoted 2 lines
XDP_COPYis needed to explicitly enable copy-mode. No flags is "select the best option", and COPY/ZEROCOPY is to explicitly select a mode.
PATCH 2/2 xsk: fix to reject invalid options in Tx descriptor
In reply to Björn TöpelFrom: Björn Töpel <[email protected]>
Passing a non-existing option in the options member of struct xdp_desc, incorrectly, silently ignored. This patch addresses that behavior, and drops any Tx descriptor with non-existing options.
We have examined existing user space code, and to our best knowledge, no one is relying on the current incorrect behavior. AF_XDP is still in its infancy, so from our perspective, the risk of breakage is very low, and addressing this problem now is important.
--- net/xdp/xsk_queue.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/xdp/xsk_queue.h +2 -2
@@ -174,8 +174,8 @@ static inline bool xskq_is_valid_desc(struct xsk_queue *q, struct xdp_desc *d)if (!xskq_is_valid_addr (q, d->addr))return false; -if (((d->addr + d->len) & q->chunk_mask) != - (d->addr & q->chunk_mask)) { +if (((d->addr + d->len) & q->chunk_mask) != (d->addr & q->chunk_mask) || + d->options) { q->invalid_descs++;return false; }
Signature · 1 line
2.19.1