History

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.

Andrey Ignatov committed 6 years ago

Commit 7aebfa11 parent 6a098153 files · +9 −10Browse files

Unified · Split

3 files
  1. include/uapi/linux/bpf.h +1 −1
  2. net/core/filter.c +7 −8
  3. tools/include/uapi/linux/bpf.h +1 −1
include/uapi/linux/bpf.h+1−1
@@ -3728,7 +3728,7 @@ struct bpf_sock_addr { · 3727 unmodified lines
3728 __u32 user_ip6[4]; /* Allows 1,2,4,8-byte read and 4,8-byte write.3728 __u32 user_ip6[4]; /* Allows 1,2,4,8-byte read and 4,8-byte write.
3729 * Stored in network byte order.3729 * Stored in network byte order.
3730 */3730 */
3731 __u32 user_port; /* Allows 4-byte read and write.3731 __u32 user_port; /* Allows 1,2,4-byte read and 4-byte write.
3732 * Stored in network byte order3732 * Stored in network byte order
3733 */3733 */
3734 __u32 family; /* Allows 4-byte read, but no write */3734 __u32 family; /* Allows 4-byte read, but no write */
net/core/filter.c+7−8
@@ -7029,6 +7029,7 @@ static bool sock_addr_is_valid_access(int off, int size, · 7028 unmodified lines
7029 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):7029 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
7030 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],7030 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
7031 msg_src_ip6[3]):7031 msg_src_ip6[3]):
7032 case bpf_ctx_range(struct bpf_sock_addr, user_port):
7032 if (type == BPF_READ) {7033 if (type == BPF_READ) {
7033 bpf_ctx_record_field_size(info, size_default);7034 bpf_ctx_record_field_size(info, size_default);
7034 7035
@@ -7059,10 +7060,6 @@ static bool sock_addr_is_valid_access(int off, int size, · 24 unmodified lines
7059 return false;7060 return false;
7060 }7061 }
7061 break;7062 break;
7062 case bpf_ctx_range(struct bpf_sock_addr, user_port):
7063 if (size != size_default)
7064 return false;
7065 break;
7066 case offsetof(struct bpf_sock_addr, sk):7063 case offsetof(struct bpf_sock_addr, sk):
7067 if (type != BPF_READ)7064 if (type != BPF_READ)
7068 return false;7065 return false;
@@ -7958,8 +7955,8 @@ static u32 sock_addr_convert_ctx_access(enum bpf_access_type type, · 889 unmodified lines
7958 struct bpf_insn *insn_buf,7955 struct bpf_insn *insn_buf,
7959 struct bpf_prog *prog, u32 *target_size)7956 struct bpf_prog *prog, u32 *target_size)
7960{7957{
7958 int off, port_size = sizeof_field(struct sockaddr_in6, sin6_port);
7961 struct bpf_insn *insn = insn_buf;7959 struct bpf_insn *insn = insn_buf;
7962 int off;
7963 7960
7964 switch (si->off) {7961 switch (si->off) {
7965 case offsetof(struct bpf_sock_addr, user_family):7962 case offsetof(struct bpf_sock_addr, user_family):
@@ -7994,9 +7991,11 @@ static u32 sock_addr_convert_ctx_access(enum bpf_access_type type, · 28 unmodified lines
7994 offsetof(struct sockaddr_in6, sin6_port));7991 offsetof(struct sockaddr_in6, sin6_port));
7995 BUILD_BUG_ON(sizeof_field(struct sockaddr_in, sin_port) !=7992 BUILD_BUG_ON(sizeof_field(struct sockaddr_in, sin_port) !=
7996 sizeof_field(struct sockaddr_in6, sin6_port));7993 sizeof_field(struct sockaddr_in6, sin6_port));
7997 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,7994 /* Account for sin6_port being smaller than user_port. */
7998 struct sockaddr_in6, uaddr,7995 port_size = min(port_size, BPF_LDST_BYTES(si));
7999 sin6_port, tmp_reg);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);
8000 break;7999 break;
8001 8000
8002 case offsetof(struct bpf_sock_addr, family):8001 case offsetof(struct bpf_sock_addr, family):
tools/include/uapi/linux/bpf.h+1−1
@@ -3728,7 +3728,7 @@ struct bpf_sock_addr { · 3727 unmodified lines
3728 __u32 user_ip6[4]; /* Allows 1,2,4,8-byte read and 4,8-byte write.3728 __u32 user_ip6[4]; /* Allows 1,2,4,8-byte read and 4,8-byte write.
3729 * Stored in network byte order.3729 * Stored in network byte order.
3730 */3730 */
3731 __u32 user_port; /* Allows 4-byte read and write.3731 __u32 user_port; /* Allows 1,2,4-byte read and 4-byte write.
3732 * Stored in network byte order3732 * Stored in network byte order
3733 */3733 */
3734 __u32 family; /* Allows 4-byte read, but no write */3734 __u32 family; /* Allows 4-byte read, but no write */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t