Skip to content

Commit 2ed0dc5

Browse files
jsitnickiAlexei Starovoitov
authored andcommitted
selftests/bpf: Cover 4-byte load from remote_port in bpf_sk_lookup
Extend the context access tests for sk_lookup prog to cover the surprising case of a 4-byte load from the remote_port field, where the expected value is actually shifted by 16 bits. Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/20220209184333.654927-3-jakub@cloudflare.com
1 parent 9a69e2b commit 2ed0dc5

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

tools/include/uapi/linux/bpf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6453,7 +6453,8 @@ struct bpf_sk_lookup {
64536453
__u32 protocol; /* IP protocol (IPPROTO_TCP, IPPROTO_UDP) */
64546454
__u32 remote_ip4; /* Network byte order */
64556455
__u32 remote_ip6[4]; /* Network byte order */
6456-
__u32 remote_port; /* Network byte order */
6456+
__be16 remote_port; /* Network byte order */
6457+
__u16 :16; /* Zero padding */
64576458
__u32 local_ip4; /* Network byte order */
64586459
__u32 local_ip6[4]; /* Network byte order */
64596460
__u32 local_port; /* Host byte order */

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ int ctx_narrow_access(struct bpf_sk_lookup *ctx)
392392
{
393393
struct bpf_sock *sk;
394394
int err, family;
395+
__u32 val_u32;
395396
bool v4;
396397

397398
v4 = (ctx->family == AF_INET);
@@ -418,6 +419,11 @@ int ctx_narrow_access(struct bpf_sk_lookup *ctx)
418419
if (LSW(ctx->remote_port, 0) != SRC_PORT)
419420
return SK_DROP;
420421

422+
/* Load from remote_port field with zero padding (backward compatibility) */
423+
val_u32 = *(__u32 *)&ctx->remote_port;
424+
if (val_u32 != bpf_htonl(bpf_ntohs(SRC_PORT) << 16))
425+
return SK_DROP;
426+
421427
/* Narrow loads from local_port field. Expect DST_PORT. */
422428
if (LSB(ctx->local_port, 0) != ((DST_PORT >> 0) & 0xff) ||
423429
LSB(ctx->local_port, 1) != ((DST_PORT >> 8) & 0xff) ||

0 commit comments

Comments
 (0)