Split bpf_sk_lookup remote_port field
Subsystems (6)
v2 cover Split bpf_sk_lookup remote_port field
Following the recent split-up of the bpf_sock dst_port field, apply the same to technique to the bpf_sk_lookup remote_port field to make uAPI more user friendly.
v1 -> v2:
- Remove remote_port range check and cast to be16 in TEST_RUN for sk_lookup (kernel test robot)
Jakub Sitnicki (2):
bpf: Make remote_port field in struct bpf_sk_lookup 16-bit wide selftests/bpf: Cover 4-byte load from remote_port in bpf_sk_lookup
include/uapi/linux/bpf.h | 3 ++- net/bpf/test_run.c | 4 ++-- net/core/filter.c | 3 ++- tools/include/uapi/linux/bpf.h | 3 ++- tools/testing/selftests/bpf/progs/test_sk_lookup.c | 6 ++++++ 5 files changed, 14 insertions(+), 5 deletions(-)
Signature · 1 line
2.31.1
PATCH v2 1/2 bpf: Make remote_port field in struct bpf_sk_lookup 16-bit wide
remote_port is another case of a BPF context field documented as a 32-bit value in network byte order for which the BPF context access converter generates a load of a zero-padded 16-bit integer in network byte order.
First such case was dst_port in bpf_sock which got addressed in commit 4421a582718a ("bpf: Make dst_port field in struct bpf_sock 16-bit wide").
Loading 4-bytes from the remote_port offset and converting the value with bpf_ntohl() leads to surprising results, as the expected value is shifted by 16 bits.
Reduce the confusion by splitting the field in two - a 16-bit field holding a big-endian integer, and a 16-bit zero-padding anonymous field that follows it.
--- include/uapi/linux/bpf.h | 3 ++- net/bpf/test_run.c | 4 ++-- net/core/filter.c | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-)
include/uapi/linux/bpf.h +2 -1
@@ -6453,7 +6453,8 @@ struct bpf_sk_lookup {__u32 protocol;/* IP protocol (IPPROTO_TCP, IPPROTO_UDP) */ __u32 remote_ip4;/* Network byte order */ __u32 remote_ip6[4 ];/* Network byte order */ -__u32 remote_port;/* Network byte order */ +__be16 remote_port;/* Network byte order */ + __u16 :16 ;/* Zero padding */ __u32 local_ip4;/* Network byte order */ __u32 local_ip6[4 ];/* Network byte order */ __u32 local_port;/* Host byte order */
net/bpf/test_run.c +2 -2
@@ -1146,7 +1146,7 @@ int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *katif (!range_is_zero (user_ctx,offsetofend (typeof (*user_ctx), local_port),sizeof (*user_ctx))) goto out; -if (user_ctx->local_port >U16_MAX || user_ctx->remote_port >U16_MAX ) { +if (user_ctx->local_port >U16_MAX ) { ret = -ERANGE ; goto out; }
@@ -1154,7 +1154,7 @@ int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kat ctx.family = (u16 )user_ctx->family; ctx.protocol = (u16 )user_ctx->protocol; ctx.dport = (u16 )user_ctx->local_port; - ctx.sport = (__force __be16 )user_ctx->remote_port; + ctx.sport = user_ctx->remote_port;switch (ctx.family) {case AF_INET :
net/core/filter.c +2 -1
@@ -10843,7 +10843,8 @@ static bool sk_lookup_is_valid_access(int off, int size, case bpf_ctx_range(struct bpf_sk_lookup, local_ip4): case bpf_ctx_range_till(struct bpf_sk_lookup, remote_ip6[0], remote_ip6[3]): case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]): - case bpf_ctx_range(struct bpf_sk_lookup, remote_port): + case offsetof(struct bpf_sk_lookup, remote_port) ... + offsetof(struct bpf_sk_lookup , local_ip4) -1 : case bpf_ctx_range(struct bpf_sk_lookup, local_port): case bpf_ctx_range(struct bpf_sk_lookup, ingress_ifindex):bpf_ctx_record_field_size (info,sizeof (__u32));
Signature · 1 line
2.31.1
PATCH v2 2/2 selftests/bpf: Cover 4-byte load from remote_port in bpf_sk_lookup
In reply to Jakub SitnickiExtend the context access tests for sk_lookup prog to cover the surprising case of a 4-byte load from the remote_port field, where the expected value is actually shifted by 16 bits.
--- tools/include/uapi/linux/bpf.h | 3 ++- tools/testing/selftests/bpf/progs/test_sk_lookup.c | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-)
tools/include/uapi/linux/bpf.h +2 -1
@@ -6453,7 +6453,8 @@ struct bpf_sk_lookup {__u32 protocol;/* IP protocol (IPPROTO_TCP, IPPROTO_UDP) */ __u32 remote_ip4;/* Network byte order */ __u32 remote_ip6[4 ];/* Network byte order */ -__u32 remote_port;/* Network byte order */ +__be16 remote_port;/* Network byte order */ + __u16 :16 ;/* Zero padding */ __u32 local_ip4;/* Network byte order */ __u32 local_ip6[4 ];/* Network byte order */ __u32 local_port;/* Host byte order */
tools/testing/selftests/bpf/progs/test_sk_lookup.c +6 -0
@@ -392,6 +392,7 @@ int ctx_narrow_access(struct bpf_sk_lookup *ctx) {struct bpf_sock *sk;int err, family; +__u32 val_u32;bool v4; v4 = (ctx->family ==AF_INET );
@@ -418,6 +419,11 @@ int ctx_narrow_access(struct bpf_sk_lookup *ctx)if (LSW (ctx->remote_port,0 ) !=SRC_PORT )return SK_DROP ; +/* Load from remote_port field with zero padding (backward compatibility) */ + val_u32 = *(__u32 *)&ctx->remote_port; +if (val_u32 !=bpf_htonl (bpf_ntohs (SRC_PORT ) <<16 )) +return SK_DROP ; +/* Narrow loads from local_port field. Expect DST_PORT. */ if (LSB(ctx->local_port, 0) != ((DST_PORT >> 0) & 0xff) || LSB(ctx->local_port, 1) != ((DST_PORT >> 8) & 0xff) ||
Signature · 1 line
2.31.1
Re: [PATCH bpf-next v2 2/2] selftests/bpf: Cover 4-byte load from remote_port in bpf_sk_lookup
Quoted 39 lines · Jakub Sitnicki
Extend the context access tests for
sk_lookupprog to cover the surprising case of a 4-byte load from theremote_portfield, where the expected value is actually shifted by 16 bits.Acked-by Yonghong Song <[email protected]>Signed-off-by Jakub Sitnicki <[email protected]>--- tools/include/uapi/linux/bpf.h | 3 ++- tools/testing/selftests/bpf/progs/test_sk_lookup.c | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-)tools/include/uapi/linux/bpf.h +2
-1@@ -6453,7 +6453,8 @@ struct bpf_sk_lookup {__u32 protocol;/* IP protocol (IPPROTO_TCP, IPPROTO_UDP) */ __u32 remote_ip4;/* Network byte order */ __u32 remote_ip6[4 ];/* Network byte order */ -__u32 remote_port;/* Network byte order */ +__be16 remote_port;/* Network byte order */ + __u16 :16 ;/* Zero padding */ __u32 local_ip4;/* Network byte order */ __u32 local_ip6[4 ];/* Network byte order */ __u32 local_port;/* Host byte order */ tools/testing/selftests/bpf/progs/test_sk_lookup.c +1
-0@@ -392,6 +392,7 @@ int ctx_narrow_access(struct bpf_sk_lookup *ctx) {struct bpf_sock *sk;int err, family; +__u32 val_u32;bool v4; v4 = (ctx->family ==AF_INET );
tools/testing/selftests/bpf/progs/test_sk_lookup.c int ctx_narrow_access(struct bpf_sk_lookup *ctx) · line 426
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_sk_lookup.c +5
-0@@ -418,6 +419,11 @@ int ctx_narrow_access(struct bpf_sk_lookup *ctx)if (LSW (ctx->remote_port,0 ) !=SRC_PORT )return SK_DROP ; +/* Load from remote_port field with zero padding (backward compatibility) */ + val_u32 = *(__u32 *)&ctx->remote_port; +if (val_u32 !=bpf_htonl (bpf_ntohs (SRC_PORT ) <<16 )) +return SK_DROP ; +
Jakub, can you please double check that your patch set doesn't break big-endian architectures? I've noticed that our s390x test runner is now failing in the sk_lookup selftest. See [0]. Also CC'ing Ilya.
[0] https://github.com/libbpf/libbpf/runs/5220996832?check_suite_focus=true
Quoted 6 lines
tools/testing/selftests/bpf/progs/test_sk_lookup.c +0
-1/* Narrow loads from local_port field. Expect DST_PORT. */ if (LSB(ctx->local_port, 0) != ((DST_PORT >> 0) & 0xff) || LSB(ctx->local_port, 1) != ((DST_PORT >> 8) & 0xff) || --2.31.1
Re: [PATCH bpf-next v2 2/2] selftests/bpf: Cover 4-byte load from remote_port in bpf_sk_lookup
Quoted 51 lines · Andrii Nakryiko
Quoted 49 lines · Jakub Sitnicki
Extend the context access tests for
sk_lookupprog to cover the surprising
case of a 4-byte load from theremote_portfield, where the expected value
is actually shifted by 16 bits.Acked-by Yonghong Song <[email protected]>Signed-off-by Jakub Sitnicki <[email protected]>---
tools/include/uapi/linux/bpf.h | 3 ++-tools/testing/selftests/bpf/progs/test_sk_lookup.c| 6 ++++++2 files changed, 8 insertions(+), 1 deletion(-)tools/include/uapi/linux/bpf.h +0
-0@@ -6453,7 +6453,8 @@ struct bpf_sk_lookup { __u32 protocol; /* IP protocol (IPPROTO_TCP,
IPPROTO_UDP) */
__u32remote_ip4; /* Network byte order */__u32remote_ip6[4]; /* Network byte order */ -__u32remote_port; /* Network byte order */ +__be16remote_port; /* Network byte order */ +__u16:16; /* Zero padding */__u32local_ip4; /* Network byte order */__u32local_ip6[4]; /* Network byte order */__u32local_port; /* Host byte order */tools/testing/selftests/bpf/progs/test_sk_lookup.c +1
-0@@ -392,6 +392,7 @@ int ctx_narrow_access(struct bpf_sk_lookup *ctx) { struct bpf_sock *sk; int err, family; + __u32 val_u32; bool v4;v4 = (
ctx->family==AF_INET);tools/testing/selftests/bpf/progs/test_sk_lookup.c +0
-0@@ -418,6 +419,11 @@ int ctx_narrow_access(struct bpf_sk_lookup *ctx) if (LSW(ctx->remote_port, 0) != SRC_PORT) return SK_DROP;
Quoted 10 lines
Quoted 6 lines
tools/testing/selftests/bpf/progs/test_sk_lookup.c +1
-0+ /* Load from remote_port field with zero padding (backwardcompatibility) */
+val_u32= *(__u32 *)&ctx->remote_port;
+ if (val_u32!=bpf_htonl(bpf_ntohs(SRC_PORT) << 16)) + returnSK_DROP;
+Jakub, can you please double check that your patch set doesn't break big-endian architectures? I've noticed that our s390x test runner is now failing in the
sk_lookupselftest. See [0]. Also CC'ing Ilya.
I agree that this looks like an endianness issue. The new check seems to make little sense on big-endian to me, so I would just #ifdef it out.
Quoted 10 lines
[0]
https://github.com/libbpf/libbpf/runs/5220996832?check_suite_focus=trueQuoted 6 lines
/* Narrow loads from
local_portfield. ExpectDST_PORT. */ if (LSB(ctx->local_port, 0)!= ((DST_PORT>> 0) &0xff) ||LSB(ctx->local_port, 1)!= ((DST_PORT>> 8) &0xff) || --
2.31.1
Re: [PATCH bpf-next v2 2/2] selftests/bpf: Cover 4-byte load from remote_port in bpf_sk_lookup
Quoted 3 lines · Ilya Leoshkevich
Quoted 2 lines · Andrii Nakryiko
On Wed, Feb 9, 2022 at 10:43 AM Jakub Sitnicki <[email protected]> wrote:
[...]
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
+ /* Load from
remote_portfield with zero padding (backward compatibility) */
+val_u32= *(__u32 *)&ctx->remote_port;
+ if (val_u32!=bpf_htonl(bpf_ntohs(SRC_PORT) << 16)) + returnSK_DROP;
+
Quoted 7 lines
Quoted 3 lines
Jakub, can you please double check that your patch set doesn't break big-endian architectures? I've noticed that our s390x test runner is now failing in the
sk_lookupselftest. See [0]. Also CC'ing Ilya.I agree that this looks like an endianness issue. The new check seems to make little sense on big-endian to me, so I would just #ifdef it out.
We have a very similar check for a load from context in progs/test_sock_fields.c, which is not causing problems:
static __noinline bool sk_dst_port__load_word(struct bpf_sock *sk)
{
__u32 *word = (__u32 *)&sk->dst_port;
return word[0] == bpf_htonl(0xcafe0000);
}So I think I just messed something up here. Will dig into it.
[...]
Re: [PATCH bpf-next v2 2/2] selftests/bpf: Cover 4-byte load from remote_port in bpf_sk_lookup
Quoted 25 lines · Jakub Sitnicki
Quoted 3 lines · Ilya Leoshkevich
Quoted 2 lines · Andrii Nakryiko
On Wed, Feb 9, 2022 at 10:43 AM Jakub Sitnicki <[email protected]> wrote:
[...]
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
+ /* Load from
remote_portfield with zero padding (backward compatibility) */
+val_u32= *(__u32 *)&ctx->remote_port;
+ if (val_u32!=bpf_htonl(bpf_ntohs(SRC_PORT) << 16)) + returnSK_DROP;
+Quoted 7 lines
Quoted 3 lines
Jakub, can you please double check that your patch set doesn't break big-endian architectures? I've noticed that our s390x test runner is now failing in the
sk_lookupselftest. See [0]. Also CC'ing Ilya.I agree that this looks like an endianness issue. The new check seems to make little sense on big-endian to me, so I would just #ifdef it out.
We have a very similar check for a load from context in
progs/test_sock_fields.c, which is not causing problems:
Quoted 7 lines
static
__noinlineboolsk_dst_port__load_word(struct bpf_sock *sk){ __u32 *word = (__u32 *)&sk->dst_port; return word[0] == bpf_htonl(0xcafe0000); }So I think I just messed something up here. Will dig into it.
Pretty sure the source of the problem here is undefined behaviour. Can't legally shift u16 by 16 bits like I did in the bpf_ntohs(SRC_PORT) << 16 expression. Will fix.
Re: [PATCH bpf-next v2 2/2] selftests/bpf: Cover 4-byte load from remote_port in bpf_sk_lookup
Quoted 32 lines · Jakub Sitnicki
Quoted 31 lines · Jakub Sitnicki
Quoted 3 lines · Ilya Leoshkevich
Quoted 2 lines · Andrii Nakryiko
On Wed, Feb 9, 2022 at 10:43 AM Jakub Sitnicki <[email protected]> wrote:
[...]
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
+ /* Load from
remote_portfield with zero padding (backward compatibility) */
+val_u32= *(__u32 *)&ctx->remote_port;
+ if (val_u32!=bpf_htonl(bpf_ntohs(SRC_PORT) << 16)) + returnSK_DROP;
+Quoted 7 lines
Quoted 3 lines
Jakub, can you please double check that your patch set doesn't break big-endian architectures? I've noticed that our s390x test runner is now failing in the
sk_lookupselftest. See [0]. Also CC'ing Ilya.I agree that this looks like an endianness issue. The new check seems to make little sense on big-endian to me, so I would just #ifdef it out.
We have a very similar check for a load from context in
progs/test_sock_fields.c, which is not causing problems:static
__noinlineboolsk_dst_port__load_word(struct bpf_sock *sk){ __u32 *word = (__u32 *)&sk->dst_port; return word[0] == bpf_htonl(0xcafe0000); }
Quoted 5 lines
Quoted 1 line
So I think I just messed something up here. Will dig into it.
Pretty sure the source of the problem here is undefined behaviour. Can't legally shift u16 by 16 bits like I did in the
bpf_ntohs(SRC_PORT) << 16expression. Will fix.
Proposed fix posted, but forgot to CC Ilya so linking here:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Wed, 9 Feb 2022 19:43:31 +0100 you wrote:
Following the recent split-up of the
bpf_sockdst_portfield, apply the same to technique to thebpf_sk_lookupremote_portfield to make uAPI more user friendly.v1 -> v2:
- Removeremote_portrange check and cast to be16 inTEST_RUNforsk_lookup(kernel test robot)[...]
Here is the summary with links:
- [bpf-next,v2,1/2] bpf: Make remote_port field in struct bpf_sk_lookup 16-bit wide
https://git.kernel.org/bpf/bpf-next/c/9a69e2b385f4 - [bpf-next,v2,2/2] selftests/bpf: Cover 4-byte load from remote_port in bpf_sk_lookup https://git.kernel.org/bpf/bpf-next/c/2ed0dc5937d3
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html