]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/commit
bpf: Make dst_port field in struct bpf_sock 16-bit wide
authorJakub Sitnicki <jakub@cloudflare.com>
Sun, 30 Jan 2022 11:55:17 +0000 (12:55 +0100)
committerStefan Bader <stefan.bader@canonical.com>
Fri, 20 May 2022 12:40:36 +0000 (14:40 +0200)
commit88b20f4db5be197f19c314f095d6c3583fa46d54
tree93d79899cb24d7c7666ca33f6b99b950f18356b8
parent90abbf7a4668fb60c26e359b976fbbe94f1446b4
bpf: Make dst_port field in struct bpf_sock 16-bit wide

BugLink: https://bugs.launchpad.net/bugs/1969107
[ Upstream commit 4421a582718ab81608d8486734c18083b822390d ]

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>
Signed-off-by: Sasha Levin <sashal@kernel.org>
(cherry picked from commit 1c6ffdf4cc45525e5736a1cb3e46cd232c121837)
Signed-off-by: Paolo Pisati <paolo.pisati@canonical.com>
include/uapi/linux/bpf.h
net/core/filter.c