Skip to content

Commit f0dc865

Browse files
jsitnickiNobody
authored andcommitted
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/[email protected]/ Fixes: 2ed0dc5 ("selftests/bpf: Cover 4-byte load from remote_port in bpf_sk_lookup") Signed-off-by: Jakub Sitnicki <[email protected]>
1 parent 787ee2b commit f0dc865

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

tools/testing/selftests/bpf/progs/test_sk_lookup.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,15 @@ int ctx_narrow_access(struct bpf_sk_lookup *ctx)
418418
if (LSW(ctx->remote_port, 0) != SRC_PORT)
419419
return SK_DROP;
420420

421-
/* Load from remote_port field with zero padding (backward compatibility) */
421+
/*
422+
* NOTE: 4-byte load from bpf_sk_lookup at remote_port offset
423+
* is quirky. It gets rewritten by the access converter to a
424+
* 2-byte load for backward compatibility. Treating the load
425+
* result as a be16 value makes the code portable across
426+
* little- and big-endian platforms.
427+
*/
422428
val_u32 = *(__u32 *)&ctx->remote_port;
423-
if (val_u32 != bpf_htonl(bpf_ntohs(SRC_PORT) << 16))
429+
if (val_u32 != SRC_PORT)
424430
return SK_DROP;
425431

426432
/* Narrow loads from local_port field. Expect DST_PORT. */

0 commit comments

Comments
 (0)