Skip to content

Commit 4421a58

Browse files
jsitnickiAlexei Starovoitov
authored andcommitted
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 0x00 32-, 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. Reported-by: Menglong Dong <imagedong@tencent.com> Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com> Link: https://lore.kernel.org/r/20220130115518.213259-2-jakub@cloudflare.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent b3dddab commit 4421a58

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

include/uapi/linux/bpf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5574,7 +5574,8 @@ struct bpf_sock {
55745574
__u32 src_ip4;
55755575
__u32 src_ip6[4];
55765576
__u32 src_port; /* host byte order */
5577-
__u32 dst_port; /* network byte order */
5577+
__be16 dst_port; /* network byte order */
5578+
__u16 :16; /* zero padding */
55785579
__u32 dst_ip4;
55795580
__u32 dst_ip6[4];
55805581
__u32 state;

net/core/filter.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8265,6 +8265,7 @@ bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
82658265
struct bpf_insn_access_aux *info)
82668266
{
82678267
const int size_default = sizeof(__u32);
8268+
int field_size;
82688269

82698270
if (off < 0 || off >= sizeof(struct bpf_sock))
82708271
return false;
@@ -8276,7 +8277,6 @@ bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
82768277
case offsetof(struct bpf_sock, family):
82778278
case offsetof(struct bpf_sock, type):
82788279
case offsetof(struct bpf_sock, protocol):
8279-
case offsetof(struct bpf_sock, dst_port):
82808280
case offsetof(struct bpf_sock, src_port):
82818281
case offsetof(struct bpf_sock, rx_queue_mapping):
82828282
case bpf_ctx_range(struct bpf_sock, src_ip4):
@@ -8285,6 +8285,14 @@ bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
82858285
case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
82868286
bpf_ctx_record_field_size(info, size_default);
82878287
return bpf_ctx_narrow_access_ok(off, size, size_default);
8288+
case bpf_ctx_range(struct bpf_sock, dst_port):
8289+
field_size = size == size_default ?
8290+
size_default : sizeof_field(struct bpf_sock, dst_port);
8291+
bpf_ctx_record_field_size(info, field_size);
8292+
return bpf_ctx_narrow_access_ok(off, size, field_size);
8293+
case offsetofend(struct bpf_sock, dst_port) ...
8294+
offsetof(struct bpf_sock, dst_ip4) - 1:
8295+
return false;
82888296
}
82898297

82908298
return size == size_default;

0 commit comments

Comments
 (0)