Skip to content

Commit 55b66aa

Browse files
jsitnickiNobody
authored andcommitted
bpf: Treat bpf_sk_lookup remote_port as a 2-byte field
In commit 9a69e2b ("bpf: Make remote_port field in struct bpf_sk_lookup 16-bit wide") the remote_port field has been split up and re-declared from u32 to be16. However, the accompanying changes to the context access converter have not been well thought through when it comes big-endian platforms. Today 2-byte wide loads from offsetof(struct bpf_sk_lookup, remote_port) are handled as narrow loads from a 4-byte wide field. This by itself is not enough to create a problem, but when we combine 1. 32-bit wide access to ->remote_port backed by a 16-wide wide load, with 2. inherent difference between litte- and big-endian in how narrow loads need have to be handled (see bpf_ctx_narrow_access_offset), we get inconsistent results for a 2-byte loads from &ctx->remote_port on LE and BE architectures. This in turn makes BPF C code for the common case of 2-byte load from ctx->remote_port not portable. To rectify it, inform the context access converter that remote_port is 2-byte wide field, and only 1-byte loads need to be treated as narrow loads. At the same time, we special-case the 4-byte load from &ctx->remote_port to continue handling it the same way as do today, in order to keep the existing BPF programs working. Fixes: 9a69e2b ("bpf: Make remote_port field in struct bpf_sk_lookup 16-bit wide") Acked-by: Martin KaFai Lau <[email protected]> Signed-off-by: Jakub Sitnicki <[email protected]>
1 parent 1b691d7 commit 55b66aa

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

net/core/filter.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10989,13 +10989,24 @@ static bool sk_lookup_is_valid_access(int off, int size,
1098910989
case bpf_ctx_range(struct bpf_sk_lookup, local_ip4):
1099010990
case bpf_ctx_range_till(struct bpf_sk_lookup, remote_ip6[0], remote_ip6[3]):
1099110991
case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]):
10992-
case offsetof(struct bpf_sk_lookup, remote_port) ...
10993-
offsetof(struct bpf_sk_lookup, local_ip4) - 1:
1099410992
case bpf_ctx_range(struct bpf_sk_lookup, local_port):
1099510993
case bpf_ctx_range(struct bpf_sk_lookup, ingress_ifindex):
1099610994
bpf_ctx_record_field_size(info, sizeof(__u32));
1099710995
return bpf_ctx_narrow_access_ok(off, size, sizeof(__u32));
1099810996

10997+
case bpf_ctx_range(struct bpf_sk_lookup, remote_port):
10998+
/* Allow 4-byte access to 2-byte field for backward compatibility */
10999+
if (size == sizeof(__u32))
11000+
return true;
11001+
bpf_ctx_record_field_size(info, sizeof(__be16));
11002+
return bpf_ctx_narrow_access_ok(off, size, sizeof(__be16));
11003+
11004+
case offsetofend(struct bpf_sk_lookup, remote_port) ...
11005+
offsetof(struct bpf_sk_lookup, local_ip4) - 1:
11006+
/* Allow access to zero padding for backward compatibility */
11007+
bpf_ctx_record_field_size(info, sizeof(__u16));
11008+
return bpf_ctx_narrow_access_ok(off, size, sizeof(__u16));
11009+
1099911010
default:
1100011011
return false;
1100111012
}
@@ -11077,6 +11088,11 @@ static u32 sk_lookup_convert_ctx_access(enum bpf_access_type type,
1107711088
sport, 2, target_size));
1107811089
break;
1107911090

11091+
case offsetofend(struct bpf_sk_lookup, remote_port):
11092+
*target_size = 2;
11093+
*insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11094+
break;
11095+
1108011096
case offsetof(struct bpf_sk_lookup, local_port):
1108111097
*insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
1108211098
bpf_target_off(struct bpf_sk_lookup_kern,

0 commit comments

Comments
 (0)