]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/commitdiff
bpf: Make remote_port field in struct bpf_sk_lookup 16-bit wide
authorJakub Sitnicki <jakub@cloudflare.com>
Wed, 9 Feb 2022 18:43:32 +0000 (19:43 +0100)
committerStefan Bader <stefan.bader@canonical.com>
Fri, 20 May 2022 12:41:23 +0000 (14:41 +0200)
BugLink: https://bugs.launchpad.net/bugs/1969107
commit 9a69e2b385f443f244a7e8b8bcafe5ccfb0866b4 upstream.

remote_port is another case of a BPF context field documented as a 32-bit
value in network byte order for which the BPF context access converter
generates a load of a zero-padded 16-bit integer in network byte order.

First such case was dst_port in bpf_sock which got addressed in commit
4421a582718a ("bpf: Make dst_port field in struct bpf_sock 16-bit wide").

Loading 4-bytes from the remote_port offset and converting the value with
bpf_ntohl() leads to surprising results, as the expected value is shifted
by 16 bits.

Reduce the confusion by splitting the field in two - a 16-bit field holding
a big-endian integer, and a 16-bit zero-padding anonymous field that
follows it.

Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20220209184333.654927-2-jakub@cloudflare.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
(cherry picked from commit 0c64645e6373761f1f8f292dc122742827f1240e)
Signed-off-by: Paolo Pisati <paolo.pisati@canonical.com>
include/uapi/linux/bpf.h
net/bpf/test_run.c
net/core/filter.c

index 2136e45656ab2b7fcf1406c6d3834b477d6d5912..a887e582f0e78b2ca306b921ee3818a800f31d4d 100644 (file)
@@ -6223,7 +6223,8 @@ struct bpf_sk_lookup {
        __u32 protocol;         /* IP protocol (IPPROTO_TCP, IPPROTO_UDP) */
        __u32 remote_ip4;       /* Network byte order */
        __u32 remote_ip6[4];    /* Network byte order */
-       __u32 remote_port;      /* Network byte order */
+       __be16 remote_port;     /* Network byte order */
+       __u16 :16;              /* Zero padding */
        __u32 local_ip4;        /* Network byte order */
        __u32 local_ip6[4];     /* Network byte order */
        __u32 local_port;       /* Host byte order */
index b5f4ef35357c808a52f3dfc70dad00b8513984e9..655ee0e2de86d251e07bd9f4b0b7f5ca93aafa97 100644 (file)
@@ -954,7 +954,7 @@ int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kat
        if (!range_is_zero(user_ctx, offsetofend(typeof(*user_ctx), local_port), sizeof(*user_ctx)))
                goto out;
 
-       if (user_ctx->local_port > U16_MAX || user_ctx->remote_port > U16_MAX) {
+       if (user_ctx->local_port > U16_MAX) {
                ret = -ERANGE;
                goto out;
        }
@@ -962,7 +962,7 @@ int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kat
        ctx.family = (u16)user_ctx->family;
        ctx.protocol = (u16)user_ctx->protocol;
        ctx.dport = (u16)user_ctx->local_port;
-       ctx.sport = (__force __be16)user_ctx->remote_port;
+       ctx.sport = user_ctx->remote_port;
 
        switch (ctx.family) {
        case AF_INET:
index fbde862e3e8246c8419239b7a184b3ef121ecc09..cdd7e92db3030f881c487dd7f33196088b7583d3 100644 (file)
@@ -10540,7 +10540,8 @@ static bool sk_lookup_is_valid_access(int off, int size,
        case bpf_ctx_range(struct bpf_sk_lookup, local_ip4):
        case bpf_ctx_range_till(struct bpf_sk_lookup, remote_ip6[0], remote_ip6[3]):
        case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]):
-       case bpf_ctx_range(struct bpf_sk_lookup, remote_port):
+       case offsetof(struct bpf_sk_lookup, remote_port) ...
+            offsetof(struct bpf_sk_lookup, local_ip4) - 1:
        case bpf_ctx_range(struct bpf_sk_lookup, local_port):
                bpf_ctx_record_field_size(info, sizeof(__u32));
                return bpf_ctx_narrow_access_ok(off, size, sizeof(__u32));