From patchwork Mon Feb 7 13:14:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Sitnicki X-Patchwork-Id: 12737260 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A5F2C433F5 for ; Mon, 7 Feb 2022 13:49:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1387268AbiBGNqU (ORCPT ); Mon, 7 Feb 2022 08:46:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1449006AbiBGNPF (ORCPT ); Mon, 7 Feb 2022 08:15:05 -0500 Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BEF63C043189 for ; Mon, 7 Feb 2022 05:15:03 -0800 (PST) Received: by mail-lf1-x12f.google.com with SMTP id n8so26770746lfq.4 for ; Mon, 07 Feb 2022 05:15:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=K4b+1gU4xGtJcTniQUAVI+p3xLbAehO6nJB884Sw+pM=; b=OEMaG4vroO351YvW0nCd1W5AmcyT0nM9fnpK+x/NnX6/YakJjkfQUF6sOLFAaCv57B qb9Lc8MR8aFHd7q3HT34/PPW8MPdro8A+Q1bBEwuGuYozbLvKMDIHtiwAeTQ5U47iTbz QVZrT5u3W5HeqIgD8G1ifo6N63IdpYQE6YoGs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=K4b+1gU4xGtJcTniQUAVI+p3xLbAehO6nJB884Sw+pM=; b=3VuWZ0m+RSQJ9O+H3bMxOKAuE0VUU7e0ODrBmIhZJrf6DLyRBpSAfsNazsh2hCtWe6 Pl8IZV94rmtMLiFlx/0MCehYrQZcA3h9VpCq9XSnk8c5i4H2pRp8LfISj0YzhMAjuXco dAohRpMUrds0dB1DjgJnOC4YQIB/Dqkv7bcJxda4+IXmdmEjM/231xr/LzGNkiAWcX48 nmGPNfJoS3oHrHTUARlVFqGn/F3Xdbr9e7xHQWA6qC+rMc3biVEvMHKjXs/qVN+9gmEX nHSGbX9l3TudaD9uKtLPD9qfyibWVgpIQp8giGxmn40CdyRNAGCsFtjMNkW6lCZAjVqn a8rw== X-Gm-Message-State: AOAM531bCs8teqnsxzPuKQUuXeA6SJWdZ3jaZY4y8ca0xK9niKZlgFz6 1SJmnqvcmdYKkNz7g+i55bDv6bZ3Ax2ZHg== X-Google-Smtp-Source: ABdhPJxb3CNNE0QJRWRGKkMTSDXDbSFmE4gGPN6+SiMtK5FhREgkDxR0qqZIEi8oIcVkqLLYwvX6DA== X-Received: by 2002:a19:4301:: with SMTP id q1mr8151788lfa.170.1644239702106; Mon, 07 Feb 2022 05:15:02 -0800 (PST) Received: from cloudflare.com (user-5-173-137-68.play-internet.pl. [5.173.137.68]) by smtp.gmail.com with ESMTPSA id f10sm1588765ljf.116.2022.02.07.05.15.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 05:15:01 -0800 (PST) From: Jakub Sitnicki To: bpf@vger.kernel.org Cc: netdev@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , kernel-team@cloudflare.com Subject: [PATCH bpf-next 1/2] bpf: Make remote_port field in struct bpf_sk_lookup 16-bit wide Date: Mon, 7 Feb 2022 14:14:58 +0100 Message-Id: <20220207131459.504292-2-jakub@cloudflare.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220207131459.504292-1-jakub@cloudflare.com> References: <20220207131459.504292-1-jakub@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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 Signed-off-by: Jakub Sitnicki Acked-by: Yonghong Song Reported-by: kernel test robot --- include/uapi/linux/bpf.h | 3 ++- net/core/filter.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index a7f0ddedac1f..afe3d0d7f5f2 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -6453,7 +6453,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 */ diff --git a/net/core/filter.c b/net/core/filter.c index 99a05199a806..83f06d3b2c52 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -10843,7 +10843,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): case bpf_ctx_range(struct bpf_sk_lookup, ingress_ifindex): bpf_ctx_record_field_size(info, sizeof(__u32)); From patchwork Mon Feb 7 13:14:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Sitnicki X-Patchwork-Id: 12737261 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C4AFC433FE for ; Mon, 7 Feb 2022 13:49:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1388027AbiBGNqX (ORCPT ); Mon, 7 Feb 2022 08:46:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1449008AbiBGNPF (ORCPT ); Mon, 7 Feb 2022 08:15:05 -0500 Received: from mail-lf1-x132.google.com (mail-lf1-x132.google.com [IPv6:2a00:1450:4864:20::132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 023B5C0401C1 for ; Mon, 7 Feb 2022 05:15:05 -0800 (PST) Received: by mail-lf1-x132.google.com with SMTP id z4so11974098lfg.5 for ; Mon, 07 Feb 2022 05:15:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=GUprroLdbtzVOCQhX6EufnSsfv67k+YQ0AYLj0nUTaU=; b=BXsyvCntwia15OtieSs5kCjCAnxe8GnsTppEPvZR5/UhigjPiwqJh4Fy6ZI2XOq6Vg +yogT5D6SEupuAq+y6AXQBOaTEN1C/0WfI6bBVTtyo6kDFgjQ3oR9yi4O5aCQXZfOpC5 PCd376q251xcmcAm3AoUzfuhevfooGBGpkx5A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=GUprroLdbtzVOCQhX6EufnSsfv67k+YQ0AYLj0nUTaU=; b=ehf+p7K9uBBM4IM2fWpTmnnuwsSWfLXQ1OkK7tMpN2eYTxyrmW30pz7wf+W61I02QC VWdXU3/yMLpJYVtzCTqZ3Z36H10wiyYZC+3EqTNOtZIYlc7XlhusLEsz3MZ7wgB4/ZMe kzCG219uWU+dlYOBQL30gamK4wX0BoEXDi+gUq8u/YcgrVt+QqyoAzHS0Ka8JJHIdKOz 3mk+7W59LcKoASrLTNkjhdEAmDyetIZJyi6lL8ZxNPWwXmxy9cIzlogvug4nPkFHpskb bobmlMVJuY8OlfokK2uCBJ0sjEXXW/FfktGZgtO/cUAP6tlBS1BFHG8b0v8Xt957yRh/ IZBw== X-Gm-Message-State: AOAM533cpgHmAmjEOEwQtmjm4Y48BXWtFWmXMIQ0Xvg9txhN5jeTm/yi HoFfgR7tn6DCW7vO5VuW6fIA6w== X-Google-Smtp-Source: ABdhPJzPHrRpl4oFrtus3QjBVVhCwoMfUqDR7tcEjdh7+nWrFXzFqQJl4puEwen0NEL0WHBwqW7GSA== X-Received: by 2002:a05:6512:c04:: with SMTP id z4mr8545235lfu.229.1644239703353; Mon, 07 Feb 2022 05:15:03 -0800 (PST) Received: from cloudflare.com (user-5-173-137-68.play-internet.pl. [5.173.137.68]) by smtp.gmail.com with ESMTPSA id k16sm1607355ljg.111.2022.02.07.05.15.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 05:15:03 -0800 (PST) From: Jakub Sitnicki To: bpf@vger.kernel.org Cc: netdev@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , kernel-team@cloudflare.com Subject: [PATCH bpf-next 2/2] selftests/bpf: Cover 4-byte load from remote_port in bpf_sk_lookup Date: Mon, 7 Feb 2022 14:14:59 +0100 Message-Id: <20220207131459.504292-3-jakub@cloudflare.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220207131459.504292-1-jakub@cloudflare.com> References: <20220207131459.504292-1-jakub@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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 Acked-by: Yonghong Song --- tools/include/uapi/linux/bpf.h | 3 ++- tools/testing/selftests/bpf/progs/test_sk_lookup.c | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index a7f0ddedac1f..afe3d0d7f5f2 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -6453,7 +6453,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 */ diff --git a/tools/testing/selftests/bpf/progs/test_sk_lookup.c b/tools/testing/selftests/bpf/progs/test_sk_lookup.c index 83b0aaa52ef7..bf5b7caefdd0 100644 --- a/tools/testing/selftests/bpf/progs/test_sk_lookup.c +++ b/tools/testing/selftests/bpf/progs/test_sk_lookup.c @@ -392,6 +392,7 @@ int ctx_narrow_access(struct bpf_sk_lookup *ctx) { struct bpf_sock *sk; int err, family; + __u32 val_u32; bool v4; v4 = (ctx->family == AF_INET); @@ -418,6 +419,11 @@ int ctx_narrow_access(struct bpf_sk_lookup *ctx) if (LSW(ctx->remote_port, 0) != SRC_PORT) return SK_DROP; + /* Load from remote_port field with zero padding (backward compatibility) */ + val_u32 = *(__u32 *)&ctx->remote_port; + if (val_u32 != bpf_htonl(bpf_ntohs(SRC_PORT) << 16)) + return SK_DROP; + /* Narrow loads from local_port field. Expect DST_PORT. */ if (LSB(ctx->local_port, 0) != ((DST_PORT >> 0) & 0xff) || LSB(ctx->local_port, 1) != ((DST_PORT >> 8) & 0xff) ||