Split bpf_sock dst_port field
Subsystems (6)
cover Split bpf_sock dst_port field
This is a follow-up to discussion around the idea of making dst_port in struct bpf_sock a 16-bit field that happened in [1]. I have fleshed it out further:
v1:
- keep dst_field offset unchanged to prevent existing BPF program breakage (Martin)
- allow 8-bit loads from dst_port[0] and [1]
- add test coverage for the verifier and the context access converter
[1] https://lore.kernel.org/bpf/[email protected]/
Jakub Sitnicki (2):
bpf: Make dst_port field in struct bpf_sock 16-bit wide selftests/bpf: Extend verifier and bpf_sock tests for dst_port loads
include/uapi/linux/bpf.h | 3 +- net/core/filter.c | 9 ++- tools/include/uapi/linux/bpf.h | 3 +- .../selftests/bpf/prog_tests/sock_fields.c | 58 +++++++++---- .../selftests/bpf/progs/test_sock_fields.c | 41 ++++++++++ tools/testing/selftests/bpf/verifier/sock.c | 81 ++++++++++++++++++- 6 files changed, 172 insertions(+), 23 deletions(-)
Signature · 1 line
2.31.1
PATCH 1/2 bpf: Make dst_port field in struct bpf_sock 16-bit wide
Menglong Dong reports that the documentation for the dst_port field in struct bpf_sock is inaccurate and confusing. From the BPF program PoV, the field is a zero-padded 16-bit integer in network byte order. The value appears to the BPF user as if laid out in memory as so:
offsetof(struct bpf_sock, dst_port) + 0 <port MSB>
+ 8 <port LSB>
+16 0x00
+24 0x0032-, 16-, and 8-bit wide loads from the field are all allowed, but only if the offset into the field is 0.
32-bit wide loads from dst_port are especially confusing. The loaded value, after converting to host byte order with bpf_ntohl(dst_port), contains the port number in the upper 16-bits.
Remove the confusion by splitting the field into two 16-bit fields. For backward compatibility, allow 32-bit wide loads from offsetof(struct bpf_sock, dst_port).
While at it, allow loads 8-bit loads at offset [0] and [1] from dst_port.
--- include/uapi/linux/bpf.h | 3 ++- net/core/filter.c | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-)
include/uapi/linux/bpf.h +2 -1
@@ -5574,7 +5574,8 @@ struct bpf_sock {__u32 src_ip4;__u32 src_ip6[4 ];__u32 src_port;/* host byte order */ -__u32 dst_port;/* network byte order */ +__be16 dst_port;/* network byte order */ +__u16 zero_padding;__u32 dst_ip4;__u32 dst_ip6[4 ];__u32 state;
net/core/filter.c +8 -1
@@ -8265,6 +8265,7 @@ bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type, struct bpf_insn_access_aux *info) {const int size_default =sizeof (__u32); +int field_size;if (off <0 || off >=sizeof (struct bpf_sock ))return false;
@@ -8276,7 +8277,6 @@ bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,case offsetof(struct bpf_sock , family):case offsetof(struct bpf_sock , type):case offsetof(struct bpf_sock , protocol): -case offsetof(struct bpf_sock , dst_port):case offsetof(struct bpf_sock , src_port):case offsetof(struct bpf_sock , rx_queue_mapping): case bpf_ctx_range(struct bpf_sock, src_ip4):
@@ -8285,6 +8285,13 @@ bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type, case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):bpf_ctx_record_field_size (info, size_default);return bpf_ctx_narrow_access_ok (off, size, size_default); + case bpf_ctx_range(struct bpf_sock, dst_port): + field_size = size == size_default ? + size_default : sizeof_field(struct bpf_sock, dst_port); +bpf_ctx_record_field_size (info, field_size); +return bpf_ctx_narrow_access_ok (off, size, field_size); + case bpf_ctx_range(struct bpf_sock, zero_padding): +return false; }return size == size_default;
Signature · 1 line
2.31.1
Re: [PATCH bpf-next 1/2] bpf: Make dst_port field in struct bpf_sock 16-bit wide
Quoted 36 lines · Jakub Sitnicki
Menglong Dong reports that the documentation for the
dst_portfield in structbpf_sockis inaccurate and confusing. From the BPF program PoV, the field is a zero-padded 16-bit integer in network byte order. The value appears to the BPF user as if laid out in memory as so:offsetof(struct bpf_sock, dst_port) + 0 <port MSB> + 8 <port LSB> +16 0x00 +24 0x0032-, 16-, and 8-bit wide loads from the field are all allowed, but only if the offset into the field is 0.
32-bit wide loads from
dst_portare especially confusing. The loaded value, after converting to host byte order withbpf_ntohl(dst_port), contains the port number in the upper 16-bits.Remove the confusion by splitting the field into two 16-bit fields. For backward compatibility, allow 32-bit wide loads from
offsetof(struct bpf_sock, dst_port).While at it, allow loads 8-bit loads at offset [0] and [1] from
dst_port.Reported-by Menglong Dong <[email protected]>Signed-off-by Jakub Sitnicki <[email protected]>--- include/uapi/linux/bpf.h | 3 ++- net/core/filter.c | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-)include/uapi/linux/bpf.h +0
-0
include/uapi/linux/bpf.h struct bpf_sock { · line 5578
Quoted 6 lines
include/uapi/linux/bpf.h +2
-1@@ -5574,7 +5574,8 @@ struct bpf_sock {__u32 src_ip4;__u32 src_ip6[4 ];__u32 src_port;/* host byte order */ -__u32 dst_port;/* network byte order */ +__be16 dst_port;/* network byte order */ +__u16 zero_padding;
I was wondering can we do '__u16 :16' here ?
Should we do the same for bpf_sk_lookup->remote_port as well for consistency?
Thanks for the idea and the patches!
Re: [PATCH bpf-next 1/2] bpf: Make dst_port field in struct bpf_sock 16-bit wide
Quoted 32 lines · Alexei Starovoitov
Quoted 31 lines · Jakub Sitnicki
Menglong Dong reports that the documentation for the
dst_portfield in structbpf_sockis inaccurate and confusing. From the BPF program PoV, the field is a zero-padded 16-bit integer in network byte order. The value appears to the BPF user as if laid out in memory as so:offsetof(struct bpf_sock, dst_port) + 0 <port MSB> + 8 <port LSB> +16 0x00 +24 0x0032-, 16-, and 8-bit wide loads from the field are all allowed, but only if the offset into the field is 0.
32-bit wide loads from
dst_portare especially confusing. The loaded value, after converting to host byte order withbpf_ntohl(dst_port), contains the port number in the upper 16-bits.Remove the confusion by splitting the field into two 16-bit fields. For backward compatibility, allow 32-bit wide loads from
offsetof(struct bpf_sock, dst_port).While at it, allow loads 8-bit loads at offset [0] and [1] from
dst_port.Reported-by Menglong Dong <[email protected]>Signed-off-by Jakub Sitnicki <[email protected]>--- include/uapi/linux/bpf.h | 3 ++- net/core/filter.c | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-)
Quoted 13 lines
include/uapi/linux/bpf.h struct bpf_sock { · line 5578
Quoted 11 lines
include/uapi/linux/bpf.h +2
-1@@ -5574,7 +5574,8 @@ struct bpf_sock {__u32 src_ip4;__u32 src_ip6[4 ];__u32 src_port;/* host byte order */ -__u32 dst_port;/* network byte order */ +__be16 dst_port;/* network byte order */ +__u16 zero_padding;I was wondering can we do '
__u16:16' here ?
Great idea. Now done in v2:
https://lore.kernel.org/bpf/[email protected]/
Quoted 2 lines
Should we do the same for
bpf_sk_lookup->remote_portas well for consistency?
I can tend to that this upcoming week.
PATCH 2/2 selftests/bpf: Extend verifier and bpf_sock tests for dst_port loads
In reply to Jakub SitnickiAdd coverage to the verifier tests and tests for reading bpf_sock fields to ensure that 32-bit, 16-bit, and 8-bit loads from dst_port field are allowed only at intended offsets and produce expected values.
While 16-bit and 8-bit access to dst_port field is straight-forward, 32-bit wide loads need be allowed and produce a zero-padded 16-bit value for backward compatibility.
--- tools/include/uapi/linux/bpf.h | 3 +- .../selftests/bpf/prog_tests/sock_fields.c | 58 +++++++++---- .../selftests/bpf/progs/test_sock_fields.c | 41 ++++++++++ tools/testing/selftests/bpf/verifier/sock.c | 81 ++++++++++++++++++- 4 files changed, 162 insertions(+), 21 deletions(-)
tools/include/uapi/linux/bpf.h +2 -1
@@ -5574,7 +5574,8 @@ struct bpf_sock {__u32 src_ip4;__u32 src_ip6[4 ];__u32 src_port;/* host byte order */ -__u32 dst_port;/* network byte order */ +__be16 dst_port;/* network byte order */ +__u16 zero_padding;__u32 dst_ip4;__u32 dst_ip6[4 ];__u32 state;
tools/testing/selftests/bpf/prog_tests/sock_fields.c +41 -17
121 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_sock_fields.c +41 -0
51 lines not on this page · raw message
tools/testing/selftests/bpf/verifier/sock.c +78 -3
99 lines not on this page · raw message
Signature · 1 line
2.31.1
Quoted 8 lines · Jakub Sitnicki
This is a follow-up to discussion around the idea of making
dst_portin structbpf_socka 16-bit field that happened in [1]. I have fleshed it out further:v1:
- keepdst_fieldoffset unchanged to prevent existing BPF program breakage (Martin)
- allow 8-bit loads from dst_port[0] and [1]
- add test coverage for the verifier and the context access converter
lgtm. Thanks for the patches.