bpf: allow wide (u64) aligned loads for some fields of bpf_sock_addr
Subsystems (7)
cover bpf: allow wide (u64) aligned loads for some fields of bpf_sock_addr
When fixing selftests by adding support for wide stores, Yonghong reported that he had seen some examples where clang generates single u64 loads for two adjacent u32s as well:
http://lore.kernel.org/netdev/[email protected]
Let's support aligned u64 reads for some bpf_sock_addr fields as well.
(This can probably wait for bpf-next, I'll defer to Younhong and the maintainers.)
Stanislav Fomichev (5):
bpf: rename bpf_ctx_wide_store_ok to bpf_ctx_wide_access_ok
bpf: allow wide aligned loads for bpf_sock_addr user_ip6 and
msg_src_ip6
selftests/bpf: rename verifier/wide_store.c to verifier/wide_access.c selftests/bpf: add selftests for wide loads
bpf: sync bpf.h to tools/
include/linux/filter.h | 2 +- include/uapi/linux/bpf.h | 4 +- net/core/filter.c | 24 ++++-- tools/include/uapi/linux/bpf.h | 4 +- .../selftests/bpf/verifier/wide_access.c | 73 +++++++++++++++++++ .../selftests/bpf/verifier/wide_store.c | 36 --------- 6 files changed, 95 insertions(+), 48 deletions(-) create mode 100644 tools/testing/selftests/bpf/verifier/wide_access.c delete mode 100644 tools/testing/selftests/bpf/verifier/wide_store.c
Signature · 1 line
2.22.0.510.g264f2c817a-goog
PATCH 1/5 bpf: rename bpf_ctx_wide_store_ok to bpf_ctx_wide_access_ok
Rename bpf_ctx_wide_store_ok to bpf_ctx_wide_access_ok to indicate that it can be used for both loads and stores.
--- include/linux/filter.h | 2 +- net/core/filter.c | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-)
include/linux/filter.h +1 -1
@@ -747,7 +747,7 @@ bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)return size <= size_default && (size & (size -1 )) ==0 ; } -#define bpf_ctx_wide_store_ok (off, size, type, field) \ +#define bpf_ctx_wide_access_ok (off, size, type, field) \ (size == sizeof(__u64) && \ off >= offsetof(type, field) && \ off + sizeof(__u64) <= offsetofend(type, field) && \
net/core/filter.c +6 -6
@@ -6890,14 +6890,14 @@ static bool sock_addr_is_valid_access(int off, int size,if (!bpf_ctx_narrow_access_ok (off, size, size_default))return false; }else { -if (bpf_ctx_wide_store_ok (off, size, - struct bpf_sock_addr, - user_ip6)) +if (bpf_ctx_wide_access_ok (off, size, + struct bpf_sock_addr, + user_ip6))return true; -if (bpf_ctx_wide_store_ok (off, size, - struct bpf_sock_addr, - msg_src_ip6)) +if (bpf_ctx_wide_access_ok (off, size, + struct bpf_sock_addr, + msg_src_ip6))return true;if (size != size_default)
Signature · 1 line
2.22.0.510.g264f2c817a-goog
PATCH 2/5 bpf: allow wide aligned loads for bpf_sock_addr user_ip6 and msg_src_ip6
In reply to Stanislav FomichevAdd explicit check for u64 loads of user_ip6 and msg_src_ip6 and update the comment.
--- include/uapi/linux/bpf.h | 4 ++-- net/core/filter.c | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-)
include/uapi/linux/bpf.h +2 -2
@@ -3248,7 +3248,7 @@ struct bpf_sock_addr {__u32 user_ip4;/* Allows 1,2,4-byte read and 4-byte write. * Stored in network byte order. */ -__u32 user_ip6[4 ];/* Allows 1,2,4-byte read and 4,8-byte write. +__u32 user_ip6[4 ];/* Allows 1,2,4,8-byte read and 4,8-byte write. * Stored in network byte order. */ __u32 user_port;/* Allows 4-byte read and write.
@@ -3260,7 +3260,7 @@ struct bpf_sock_addr {__u32 msg_src_ip4;/* Allows 1,2,4-byte read and 4-byte write. * Stored in network byte order. */ -__u32 msg_src_ip6[4 ];/* Allows 1,2,4-byte read and 4,8-byte write. +__u32 msg_src_ip6[4 ];/* Allows 1,2,4,8-byte read and 4,8-byte write. * Stored in network byte order. */ __bpf_md_ptr(struct bpf_sock *, sk);
net/core/filter.c +11 -1
@@ -6884,9 +6884,19 @@ static bool sock_addr_is_valid_access(int off, int size, case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4): case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0], msg_src_ip6[3 ]): -/* Only narrow read access allowed for now. */ if (type ==BPF_READ ) {bpf_ctx_record_field_size (info, size_default); + +if (bpf_ctx_wide_access_ok (off, size, + struct bpf_sock_addr, + user_ip6)) +return true; + +if (bpf_ctx_wide_access_ok (off, size, + struct bpf_sock_addr, + msg_src_ip6)) +return true; +if (!bpf_ctx_narrow_access_ok (off, size, size_default))return false; }else {
Signature · 1 line
2.22.0.510.g264f2c817a-goog
PATCH 3/5 selftests/bpf: rename verifier/wide_store.c to verifier/wide_access.c
In reply to Stanislav FomichevMove the file and rename internal BPF_SOCK_ADDR define to BPF_SOCK_ADDR_STORE. This selftest will be extended in the next commit with the wide loads.
--- .../selftests/bpf/verifier/wide_access.c | 36 +++++++++++++++++++ .../selftests/bpf/verifier/wide_store.c | 36 ------------------- 2 files changed, 36 insertions(+), 36 deletions(-) create mode 100644 tools/testing/selftests/bpf/verifier/wide_access.c delete mode 100644 tools/testing/selftests/bpf/verifier/wide_store.c
tools/testing/selftests/bpf/verifier/wide_access.c new file +36 -0
@@ -0,0 +1,36 @@ +#define BPF_SOCK_ADDR_STORE (field, off, res, err) \ +{ \ + "wide store to bpf_sock_addr." #field "[" #off "]", \ + .insns = { \ + BPF_MOV64_IMM(BPF_REG_0, 1), \ + BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, \ + offsetof(struct bpf_sock_addr, field[off])), \ + BPF_EXIT_INSN(), \ + }, \ + .result = res, \ + .prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR, \ + .expected_attach_type = BPF_CGROUP_UDP6_SENDMSG, \ + .errstr = err, \ +} + +/* user_ip6[0] is u64 aligned */ +BPF_SOCK_ADDR_STORE (user_ip6,0 ,ACCEPT , +NULL ), +BPF_SOCK_ADDR_STORE (user_ip6,1 ,REJECT , +"invalid bpf_context access off=12 size=8" ), +BPF_SOCK_ADDR_STORE (user_ip6,2 ,ACCEPT , +NULL ), +BPF_SOCK_ADDR_STORE (user_ip6,3 ,REJECT , +"invalid bpf_context access off=20 size=8" ), + +/* msg_src_ip6[0] is _not_ u64 aligned */ +BPF_SOCK_ADDR_STORE (msg_src_ip6,0 ,REJECT , +"invalid bpf_context access off=44 size=8" ), +BPF_SOCK_ADDR_STORE (msg_src_ip6,1 ,ACCEPT , +NULL ), +BPF_SOCK_ADDR_STORE (msg_src_ip6,2 ,REJECT , +"invalid bpf_context access off=52 size=8" ), +BPF_SOCK_ADDR_STORE (msg_src_ip6,3 ,REJECT , +"invalid bpf_context access off=56 size=8" ), + +#undef BPF_SOCK_ADDR_STORE
tools/testing/selftests/bpf/verifier/wide_store.c deleted +0 -36
@@ -1,36 +0,0 @@ -#define BPF_SOCK_ADDR (field, off, res, err) \ -{ \ - "wide store to bpf_sock_addr." #field "[" #off "]", \ - .insns = { \ - BPF_MOV64_IMM(BPF_REG_0, 1), \ - BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, \ - offsetof(struct bpf_sock_addr, field[off])), \ - BPF_EXIT_INSN(), \ - }, \ - .result = res, \ - .prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR, \ - .expected_attach_type = BPF_CGROUP_UDP6_SENDMSG, \ - .errstr = err, \ -} - -/* user_ip6[0] is u64 aligned */ -BPF_SOCK_ADDR (user_ip6,0 ,ACCEPT , -NULL ), -BPF_SOCK_ADDR (user_ip6,1 ,REJECT , -"invalid bpf_context access off=12 size=8" ), -BPF_SOCK_ADDR (user_ip6,2 ,ACCEPT , -NULL ), -BPF_SOCK_ADDR (user_ip6,3 ,REJECT , -"invalid bpf_context access off=20 size=8" ), - -/* msg_src_ip6[0] is _not_ u64 aligned */ -BPF_SOCK_ADDR (msg_src_ip6,0 ,REJECT , -"invalid bpf_context access off=44 size=8" ), -BPF_SOCK_ADDR (msg_src_ip6,1 ,ACCEPT , -NULL ), -BPF_SOCK_ADDR (msg_src_ip6,2 ,REJECT , -"invalid bpf_context access off=52 size=8" ), -BPF_SOCK_ADDR (msg_src_ip6,3 ,REJECT , -"invalid bpf_context access off=56 size=8" ), - -#undef BPF_SOCK_ADDR
Signature · 1 line
2.22.0.510.g264f2c817a-goog
PATCH 4/5 selftests/bpf: add selftests for wide loads
In reply to Stanislav FomichevMirror existing wide store tests with wide loads. The only significant difference is expected error string.
--- .../selftests/bpf/verifier/wide_access.c | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+)
tools/testing/selftests/bpf/verifier/wide_access.c +37 -0
@@ -34,3 +34,40 @@ BPF_SOCK_ADDR_STORE(msg_src_ip6, 3, REJECT, "invalid bpf_context access off=56 size=8"),#undef BPF_SOCK_ADDR_STORE + +#define BPF_SOCK_ADDR_LOAD (field, off, res, err) \ +{ \ + "wide load from bpf_sock_addr." #field "[" #off "]", \ + .insns = { \ + BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, \ + offsetof(struct bpf_sock_addr, field[off])), \ + BPF_MOV64_IMM(BPF_REG_0, 1), \ + BPF_EXIT_INSN(), \ + }, \ + .result = res, \ + .prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR, \ + .expected_attach_type = BPF_CGROUP_UDP6_SENDMSG, \ + .errstr = err, \ +} + +/* user_ip6[0] is u64 aligned */ +BPF_SOCK_ADDR_LOAD (user_ip6,0 ,ACCEPT , +NULL ), +BPF_SOCK_ADDR_LOAD (user_ip6,1 ,REJECT , +"invalid bpf_context access off=12 size=8" ), +BPF_SOCK_ADDR_LOAD (user_ip6,2 ,ACCEPT , +NULL ), +BPF_SOCK_ADDR_LOAD (user_ip6,3 ,REJECT , +"invalid bpf_context access off=20 size=8" ), + +/* msg_src_ip6[0] is _not_ u64 aligned */ +BPF_SOCK_ADDR_LOAD (msg_src_ip6,0 ,REJECT , +"invalid bpf_context access off=44 size=8" ), +BPF_SOCK_ADDR_LOAD (msg_src_ip6,1 ,ACCEPT , +NULL ), +BPF_SOCK_ADDR_LOAD (msg_src_ip6,2 ,REJECT , +"invalid bpf_context access off=52 size=8" ), +BPF_SOCK_ADDR_LOAD (msg_src_ip6,3 ,REJECT , +"invalid bpf_context access off=56 size=8" ), + +#undef BPF_SOCK_ADDR_LOAD
Signature · 1 line
2.22.0.510.g264f2c817a-goog
PATCH 5/5 bpf: sync bpf.h to tools/
In reply to Stanislav FomichevUpdate bpf_sock_addr comments to indicate support for 8-byte reads from user_ip6 and msg_src_ip6.
--- tools/include/uapi/linux/bpf.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/include/uapi/linux/bpf.h +2 -2
@@ -3245,7 +3245,7 @@ struct bpf_sock_addr {__u32 user_ip4;/* Allows 1,2,4-byte read and 4-byte write. * Stored in network byte order. */ -__u32 user_ip6[4 ];/* Allows 1,2,4-byte read and 4,8-byte write. +__u32 user_ip6[4 ];/* Allows 1,2,4,8-byte read and 4,8-byte write. * Stored in network byte order. */ __u32 user_port;/* Allows 4-byte read and write.
@@ -3257,7 +3257,7 @@ struct bpf_sock_addr {__u32 msg_src_ip4;/* Allows 1,2,4-byte read and 4-byte write. * Stored in network byte order. */ -__u32 msg_src_ip6[4 ];/* Allows 1,2,4-byte read and 4,8-byte write. +__u32 msg_src_ip6[4 ];/* Allows 1,2,4,8-byte read and 4,8-byte write. * Stored in network byte order. */ __bpf_md_ptr(struct bpf_sock *, sk);
Signature · 1 line
2.22.0.510.g264f2c817a-goog
Quoted 14 lines · Stanislav Fomichev
When fixing selftests by adding support for wide stores, Yonghong reported that he had seen some examples where clang generates single u64 loads for two adjacent u32s as well:
http://lore.kernel.org/netdev/[email protected]Let's support aligned u64 reads for some
bpf_sock_addrfields as well.(This can probably wait for bpf-next, I'll defer to Younhong and the maintainers.)
Cc Yonghong Song <[email protected]>
Quoted 8 lines
Stanislav Fomichev (5):
bpf: renamebpf_ctx_wide_store_oktobpf_ctx_wide_access_ok
bpf: allow wide aligned loads forbpf_sock_addruser_ip6andmsg_src_ip6selftests/bpf: rename
verifier/wide_store.ctoverifier/wide_access.cselftests/bpf: add selftests for wide loads
bpf: syncbpf.htotools/
LGTM!
For the series:
Quoted 12 lines
include/linux/filter.h | 2 +- include/uapi/linux/bpf.h | 4 +- net/core/filter.c | 24 ++++-- tools/include/uapi/linux/bpf.h | 4 +- .../selftests/bpf/verifier/wide_access.c | 73 +++++++++++++++++++ .../selftests/bpf/verifier/wide_store.c | 36 --------- 6 files changed, 95 insertions(+), 48 deletions(-) create mode 100644 tools/testing/selftests/bpf/verifier/wide_access.c delete mode 100644 tools/testing/selftests/bpf/verifier/wide_store.c--
2.22.0.510.g264f2c817a-goog
Quoted 13 lines · Stanislav Fomichev
When fixing selftests by adding support for wide stores, Yonghong reported that he had seen some examples where clang generates single u64 loads for two adjacent u32s as well:
http://lore.kernel.org/netdev/[email protected]Let's support aligned u64 reads for some
bpf_sock_addrfields as well.(This can probably wait for bpf-next, I'll defer to Younhong and the maintainers.)
Cc Yonghong Song <[email protected]>
Quoted 7 lines
Stanislav Fomichev (5):
bpf: renamebpf_ctx_wide_store_oktobpf_ctx_wide_access_ok
bpf: allow wide aligned loads forbpf_sock_addruser_ip6andmsg_src_ip6selftests/bpf: rename
verifier/wide_store.ctoverifier/wide_access.cselftests/bpf: add selftests for wide loads
bpf: syncbpf.htotools/
Thanks for fixing. Maybe getting into bpf is better as this indeed a potential issue? I do not have strong feeling either as the issue can be easily workarounded with "volatile" tricks.
Quoted 11 lines
include/linux/filter.h | 2 +- include/uapi/linux/bpf.h | 4 +- net/core/filter.c | 24 ++++-- tools/include/uapi/linux/bpf.h | 4 +- .../selftests/bpf/verifier/wide_access.c | 73 +++++++++++++++++++ .../selftests/bpf/verifier/wide_store.c | 36 --------- 6 files changed, 95 insertions(+), 48 deletions(-)create mode 100644
tools/testing/selftests/bpf/verifier/wide_access.cdelete mode 100644tools/testing/selftests/bpf/verifier/wide_store.c
Quoted 21 lines · Stanislav Fomichev
When fixing selftests by adding support for wide stores, Yonghong reported that he had seen some examples where clang generates single u64 loads for two adjacent u32s as well:
http://lore.kernel.org/netdev/[email protected]Let's support aligned u64 reads for some
bpf_sock_addrfields as well.(This can probably wait for bpf-next, I'll defer to Younhong and the maintainers.)
Cc Yonghong Song <[email protected]>Stanislav Fomichev (5):
bpf: renamebpf_ctx_wide_store_oktobpf_ctx_wide_access_ok
bpf: allow wide aligned loads forbpf_sock_addruser_ip6andmsg_src_ip6selftests/bpf: rename
verifier/wide_store.ctoverifier/wide_access.cselftests/bpf: add selftests for wide loads
bpf: syncbpf.htotools/
Quoted 10 lines
include/linux/filter.h | 2 +- include/uapi/linux/bpf.h | 4 +- net/core/filter.c | 24 ++++-- tools/include/uapi/linux/bpf.h | 4 +- .../selftests/bpf/verifier/wide_access.c | 73 +++++++++++++++++++ .../selftests/bpf/verifier/wide_store.c | 36 --------- 6 files changed, 95 insertions(+), 48 deletions(-) create mode 100644 tools/testing/selftests/bpf/verifier/wide_access.c delete mode 100644 tools/testing/selftests/bpf/verifier/wide_store.c
Applied, thanks!