Skip to content

Commit 9a69e2b

Browse files
jsitnickiAlexei Starovoitov
authored andcommitted
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 4421a58 ("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. Suggested-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20220209184333.654927-2-jakub@cloudflare.com
1 parent dc37dc6 commit 9a69e2b

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

include/uapi/linux/bpf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6453,7 +6453,8 @@ struct bpf_sk_lookup {
64536453
__u32 protocol; /* IP protocol (IPPROTO_TCP, IPPROTO_UDP) */
64546454
__u32 remote_ip4; /* Network byte order */
64556455
__u32 remote_ip6[4]; /* Network byte order */
6456-
__u32 remote_port; /* Network byte order */
6456+
__be16 remote_port; /* Network byte order */
6457+
__u16 :16; /* Zero padding */
64576458
__u32 local_ip4; /* Network byte order */
64586459
__u32 local_ip6[4]; /* Network byte order */
64596460
__u32 local_port; /* Host byte order */

net/bpf/test_run.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,15 +1147,15 @@ int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kat
11471147
if (!range_is_zero(user_ctx, offsetofend(typeof(*user_ctx), local_port), sizeof(*user_ctx)))
11481148
goto out;
11491149

1150-
if (user_ctx->local_port > U16_MAX || user_ctx->remote_port > U16_MAX) {
1150+
if (user_ctx->local_port > U16_MAX) {
11511151
ret = -ERANGE;
11521152
goto out;
11531153
}
11541154

11551155
ctx.family = (u16)user_ctx->family;
11561156
ctx.protocol = (u16)user_ctx->protocol;
11571157
ctx.dport = (u16)user_ctx->local_port;
1158-
ctx.sport = (__force __be16)user_ctx->remote_port;
1158+
ctx.sport = user_ctx->remote_port;
11591159

11601160
switch (ctx.family) {
11611161
case AF_INET:

net/core/filter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10843,7 +10843,8 @@ static bool sk_lookup_is_valid_access(int off, int size,
1084310843
case bpf_ctx_range(struct bpf_sk_lookup, local_ip4):
1084410844
case bpf_ctx_range_till(struct bpf_sk_lookup, remote_ip6[0], remote_ip6[3]):
1084510845
case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]):
10846-
case bpf_ctx_range(struct bpf_sk_lookup, remote_port):
10846+
case offsetof(struct bpf_sk_lookup, remote_port) ...
10847+
offsetof(struct bpf_sk_lookup, local_ip4) - 1:
1084710848
case bpf_ctx_range(struct bpf_sk_lookup, local_port):
1084810849
case bpf_ctx_range(struct bpf_sk_lookup, ingress_ifindex):
1084910850
bpf_ctx_record_field_size(info, sizeof(__u32));

0 commit comments

Comments
 (0)