bpf: allow wide (u64) aligned stores for some fields of bpf_sock_addr
Since commitcd17d77705
("bpf/tools: sync bpf.h") clang decided that it can do a single u64 store into user_ip6[2] instead of two separate u32 ones: # 17: (18) r2 = 0x100000000000000 # ; ctx->user_ip6[2] = bpf_htonl(DST_REWRITE_IP6_2); # 19: (7b) *(u64 *)(r1 +16) = r2 # invalid bpf_context access off=16 size=8 >From the compiler point of view it does look like a correct thing to do, so let's support it on the kernel side. Credit to Andrii Nakryiko for a proper implementation of bpf_ctx_wide_store_ok. Cc: Andrii Nakryiko <andriin@fb.com> Cc: Yonghong Song <yhs@fb.com> Fixes:cd17d77705
("bpf/tools: sync bpf.h") Reported-by: kernel test robot <rong.a.chen@intel.com> Acked-by: Yonghong Song <yhs@fb.com> Acked-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
This commit is contained in:
parent
d2850ce0bd
commit
600c70bad6
|
@ -747,6 +747,12 @@ bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default)
|
||||||
return size <= size_default && (size & (size - 1)) == 0;
|
return size <= size_default && (size & (size - 1)) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define bpf_ctx_wide_store_ok(off, size, type, field) \
|
||||||
|
(size == sizeof(__u64) && \
|
||||||
|
off >= offsetof(type, field) && \
|
||||||
|
off + sizeof(__u64) <= offsetofend(type, field) && \
|
||||||
|
off % sizeof(__u64) == 0)
|
||||||
|
|
||||||
#define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
|
#define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
|
||||||
|
|
||||||
static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
|
static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
|
||||||
|
|
|
@ -3247,7 +3247,7 @@ struct bpf_sock_addr {
|
||||||
__u32 user_ip4; /* Allows 1,2,4-byte read and 4-byte write.
|
__u32 user_ip4; /* Allows 1,2,4-byte read and 4-byte write.
|
||||||
* Stored in network byte order.
|
* Stored in network byte order.
|
||||||
*/
|
*/
|
||||||
__u32 user_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
|
__u32 user_ip6[4]; /* Allows 1,2,4-byte read and 4,8-byte write.
|
||||||
* Stored in network byte order.
|
* Stored in network byte order.
|
||||||
*/
|
*/
|
||||||
__u32 user_port; /* Allows 4-byte read and write.
|
__u32 user_port; /* Allows 4-byte read and write.
|
||||||
|
@ -3256,10 +3256,10 @@ struct bpf_sock_addr {
|
||||||
__u32 family; /* Allows 4-byte read, but no write */
|
__u32 family; /* Allows 4-byte read, but no write */
|
||||||
__u32 type; /* Allows 4-byte read, but no write */
|
__u32 type; /* Allows 4-byte read, but no write */
|
||||||
__u32 protocol; /* Allows 4-byte read, but no write */
|
__u32 protocol; /* Allows 4-byte read, but no write */
|
||||||
__u32 msg_src_ip4; /* Allows 1,2,4-byte read an 4-byte write.
|
__u32 msg_src_ip4; /* Allows 1,2,4-byte read and 4-byte write.
|
||||||
* Stored in network byte order.
|
* Stored in network byte order.
|
||||||
*/
|
*/
|
||||||
__u32 msg_src_ip6[4]; /* Allows 1,2,4-byte read an 4-byte write.
|
__u32 msg_src_ip6[4]; /* Allows 1,2,4-byte read and 4,8-byte write.
|
||||||
* Stored in network byte order.
|
* Stored in network byte order.
|
||||||
*/
|
*/
|
||||||
__bpf_md_ptr(struct bpf_sock *, sk);
|
__bpf_md_ptr(struct bpf_sock *, sk);
|
||||||
|
|
|
@ -6890,6 +6890,16 @@ static bool sock_addr_is_valid_access(int off, int size,
|
||||||
if (!bpf_ctx_narrow_access_ok(off, size, size_default))
|
if (!bpf_ctx_narrow_access_ok(off, size, size_default))
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
|
if (bpf_ctx_wide_store_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))
|
||||||
|
return true;
|
||||||
|
|
||||||
if (size != size_default)
|
if (size != size_default)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -7730,9 +7740,6 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
|
||||||
/* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
|
/* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
|
||||||
* SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
|
* SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
|
||||||
*
|
*
|
||||||
* It doesn't support SIZE argument though since narrow stores are not
|
|
||||||
* supported for now.
|
|
||||||
*
|
|
||||||
* In addition it uses Temporary Field TF (member of struct S) as the 3rd
|
* In addition it uses Temporary Field TF (member of struct S) as the 3rd
|
||||||
* "register" since two registers available in convert_ctx_access are not
|
* "register" since two registers available in convert_ctx_access are not
|
||||||
* enough: we can't override neither SRC, since it contains value to store, nor
|
* enough: we can't override neither SRC, since it contains value to store, nor
|
||||||
|
@ -7740,7 +7747,7 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
|
||||||
* instructions. But we need a temporary place to save pointer to nested
|
* instructions. But we need a temporary place to save pointer to nested
|
||||||
* structure whose field we want to store to.
|
* structure whose field we want to store to.
|
||||||
*/
|
*/
|
||||||
#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \
|
#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF) \
|
||||||
do { \
|
do { \
|
||||||
int tmp_reg = BPF_REG_9; \
|
int tmp_reg = BPF_REG_9; \
|
||||||
if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
|
if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
|
||||||
|
@ -7751,8 +7758,7 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
|
||||||
offsetof(S, TF)); \
|
offsetof(S, TF)); \
|
||||||
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
|
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
|
||||||
si->dst_reg, offsetof(S, F)); \
|
si->dst_reg, offsetof(S, F)); \
|
||||||
*insn++ = BPF_STX_MEM( \
|
*insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg, \
|
||||||
BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \
|
|
||||||
bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
|
bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
|
||||||
target_size) \
|
target_size) \
|
||||||
+ OFF); \
|
+ OFF); \
|
||||||
|
@ -7764,8 +7770,8 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type,
|
||||||
TF) \
|
TF) \
|
||||||
do { \
|
do { \
|
||||||
if (type == BPF_WRITE) { \
|
if (type == BPF_WRITE) { \
|
||||||
SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \
|
SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, \
|
||||||
TF); \
|
OFF, TF); \
|
||||||
} else { \
|
} else { \
|
||||||
SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
|
SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
|
||||||
S, NS, F, NF, SIZE, OFF); \
|
S, NS, F, NF, SIZE, OFF); \
|
||||||
|
|
Loading…
Reference in New Issue