Commit ce523680 authored by Jakub Sitnicki's avatar Jakub Sitnicki Committed by Alexei Starovoitov

selftests/bpf: Fix test for 4-byte load from remote_port on big-endian

The context access converter rewrites the 4-byte load from
bpf_sk_lookup->remote_port to a 2-byte load from bpf_sk_lookup_kern
structure.

It means that we cannot treat the destination register contents as a 32-bit
value, or the code will not be portable across big- and little-endian
architectures.

This is exactly the same case as with 4-byte loads from bpf_sock->dst_port
so follow the approach outlined in [1] and treat the register contents as a
16-bit value in the test.

[1]: https://lore.kernel.org/bpf/20220317113920.1068535-5-jakub@cloudflare.com/

Fixes: 2ed0dc59 ("selftests/bpf: Cover 4-byte load from remote_port in bpf_sk_lookup")
Signed-off-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20220319183356.233666-4-jakub@cloudflare.com
parent 3c69611b
...@@ -418,9 +418,15 @@ int ctx_narrow_access(struct bpf_sk_lookup *ctx) ...@@ -418,9 +418,15 @@ int ctx_narrow_access(struct bpf_sk_lookup *ctx)
if (LSW(ctx->remote_port, 0) != SRC_PORT) if (LSW(ctx->remote_port, 0) != SRC_PORT)
return SK_DROP; return SK_DROP;
/* Load from remote_port field with zero padding (backward compatibility) */ /*
* NOTE: 4-byte load from bpf_sk_lookup at remote_port offset
* is quirky. It gets rewritten by the access converter to a
* 2-byte load for backward compatibility. Treating the load
* result as a be16 value makes the code portable across
* little- and big-endian platforms.
*/
val_u32 = *(__u32 *)&ctx->remote_port; val_u32 = *(__u32 *)&ctx->remote_port;
if (val_u32 != bpf_htonl(bpf_ntohs(SRC_PORT) << 16)) if (val_u32 != SRC_PORT)
return SK_DROP; return SK_DROP;
/* Narrow loads from local_port field. Expect DST_PORT. */ /* Narrow loads from local_port field. Expect DST_PORT. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment