Skip to content

Commit 7aebfa1

Browse files
rdnaAlexei Starovoitov
authored andcommitted
bpf: Support narrow loads from bpf_sock_addr.user_port
bpf_sock_addr.user_port supports only 4-byte load and it leads to ugly code in BPF programs, like: volatile __u32 user_port = ctx->user_port; __u16 port = bpf_ntohs(user_port); Since otherwise clang may optimize the load to be 2-byte and it's rejected by verifier. Add support for 1- and 2-byte loads same way as it's supported for other fields in bpf_sock_addr like user_ip4, msg_src_ip4, etc. Signed-off-by: Andrey Ignatov <rdna@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/c1e983f4c17573032601d0b2b1f9d1274f24bc16.1589420814.git.rdna@fb.com
1 parent 6a09815 commit 7aebfa1

3 files changed

Lines changed: 9 additions & 10 deletions

File tree

‎include/uapi/linux/bpf.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3728,7 +3728,7 @@ struct bpf_sock_addr {
37283728
__u32 user_ip6[4]; /* Allows 1,2,4,8-byte read and 4,8-byte write.
37293729
* Stored in network byte order.
37303730
*/
3731-
__u32 user_port; /* Allows 4-byte read and write.
3731+
__u32 user_port; /* Allows 1,2,4-byte read and 4-byte write.
37323732
* Stored in network byte order
37333733
*/
37343734
__u32 family; /* Allows 4-byte read, but no write */

‎net/core/filter.c‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7029,6 +7029,7 @@ static bool sock_addr_is_valid_access(int off, int size,
70297029
case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
70307030
case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
70317031
msg_src_ip6[3]):
7032+
case bpf_ctx_range(struct bpf_sock_addr, user_port):
70327033
if (type == BPF_READ) {
70337034
bpf_ctx_record_field_size(info, size_default);
70347035

@@ -7059,10 +7060,6 @@ static bool sock_addr_is_valid_access(int off, int size,
70597060
return false;
70607061
}
70617062
break;
7062-
case bpf_ctx_range(struct bpf_sock_addr, user_port):
7063-
if (size != size_default)
7064-
return false;
7065-
break;
70667063
case offsetof(struct bpf_sock_addr, sk):
70677064
if (type != BPF_READ)
70687065
return false;
@@ -7958,8 +7955,8 @@ static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
79587955
struct bpf_insn *insn_buf,
79597956
struct bpf_prog *prog, u32 *target_size)
79607957
{
7958+
int off, port_size = sizeof_field(struct sockaddr_in6, sin6_port);
79617959
struct bpf_insn *insn = insn_buf;
7962-
int off;
79637960

79647961
switch (si->off) {
79657962
case offsetof(struct bpf_sock_addr, user_family):
@@ -7994,9 +7991,11 @@ static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
79947991
offsetof(struct sockaddr_in6, sin6_port));
79957992
BUILD_BUG_ON(sizeof_field(struct sockaddr_in, sin_port) !=
79967993
sizeof_field(struct sockaddr_in6, sin6_port));
7997-
SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,
7998-
struct sockaddr_in6, uaddr,
7999-
sin6_port, tmp_reg);
7994+
/* Account for sin6_port being smaller than user_port. */
7995+
port_size = min(port_size, BPF_LDST_BYTES(si));
7996+
SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7997+
struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
7998+
sin6_port, bytes_to_bpf_size(port_size), 0, tmp_reg);
80007999
break;
80018000

80028001
case offsetof(struct bpf_sock_addr, family):

‎tools/include/uapi/linux/bpf.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3728,7 +3728,7 @@ struct bpf_sock_addr {
37283728
__u32 user_ip6[4]; /* Allows 1,2,4,8-byte read and 4,8-byte write.
37293729
* Stored in network byte order.
37303730
*/
3731-
__u32 user_port; /* Allows 4-byte read and write.
3731+
__u32 user_port; /* Allows 1,2,4-byte read and 4-byte write.
37323732
* Stored in network byte order
37333733
*/
37343734
__u32 family; /* Allows 4-byte read, but no write */

0 commit comments

Comments
 (0)