Received: by 2002:a05:6512:3d0e:0:0:0:0 with SMTP id d14csp49685lfv; Tue, 12 Apr 2022 16:51:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxz9IhB12R/T6IcmUsV4BrkGyg3/eoiN/0eSJYVVPtwiZCjT/na2XtrFuS45DNQSrkT/8JN X-Received: by 2002:a17:903:11cc:b0:151:71e4:dadc with SMTP id q12-20020a17090311cc00b0015171e4dadcmr39203425plh.78.1649807475911; Tue, 12 Apr 2022 16:51:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649807475; cv=none; d=google.com; s=arc-20160816; b=puJRTUUtzc01T+tcqFIZ4oRYHp05n4rDpcv7ZUxAuritC0C+h52pVVGLdJ8+n4Ght+ xtaEkjBgDH9m4lLQ1aZaCE3gHAXedXl/vmdU/Jaiw0xdrYViXoykIdvtVhc+F9AxuTEe Fxqi1MGZSlDOZa0Pj7Eg+BdXiakbkwMjNJV/6coT+D6xMNPfb5bORFN44bYhslZPyce8 q3HFwvXC/e1wmANrU3rdO1vC+0xbGYpCDnHHS6ATcG3HqBV0DRFx1f+qwqro88gJC14F k8MssWzWz0/cCwUuGakmkNNGR3ymHJQuAq4hR68VfmNP/0nFK/2D3cQrXREsgaK16feA 469g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=MM0NNjZz2KXPYzxPlXMduCrHG1ueQXkj0iLxxB1CHgg=; b=C6ZEaZ6W/XAAy9FVOxrLpGKn/g6jVVblXMkJdYgjKYwFz+0KCIj8AT2XeqR4zkt6Rz KgoyEFIPNUhagkDO+OpRFrN73lQxs1n0Kv0mgj4PopIcN1qQCZ7LS2qMM5aH/rCcBo31 HGsYVb+KdoNEp/tmV7AfI4JDo4lpLtEAe9V9pSYzgMc4lx0Bw2uJBjAbpPevi0ZDny7t 7YZyWP5W34bHncBhk/DtX1y0ZQ/ZAe4sB8SimEWsOY5NeLwNmCrYkWD+aMy0FH6UH5ob HY98qkIoJhOi7Q4KjAsvp84pxawApwO8OUXE3G+sNUp7AhCx43VJc007FICuIY2EBZDY VTmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=urwrGadV; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id n18-20020a170902e55200b00153b2d164e0si14017168plf.232.2022.04.12.16.51.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 16:51:15 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=urwrGadV; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 93C501E5305; Tue, 12 Apr 2022 14:44:38 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351868AbiDLHEW (ORCPT + 99 others); Tue, 12 Apr 2022 03:04:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48494 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351548AbiDLGx5 (ORCPT ); Tue, 12 Apr 2022 02:53:57 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE2EDDFC0; Mon, 11 Apr 2022 23:42:29 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 85DF7B81B40; Tue, 12 Apr 2022 06:42:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E219BC385A6; Tue, 12 Apr 2022 06:42:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649745747; bh=Thuk7Bai6Yo1zdNeRAk2VU15/LhdKGE0MK7Vm2sewe4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=urwrGadVm/S+rYB087bDy6YxRHI/BS1AcgxIoaSyJO8TTsjg28G3W95BZQTU8yy62 EdgTTQxk4gllbIVihx3yGUidZ/123yNPCgvlXJWKDIyr/CK3i/XlPUYoNoH2IzqGHZ oivFxCaLAEPnYQwmi0ObLlqHGzT1+Rd1G7QMjrhs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Menglong Dong , Jakub Sitnicki , Alexei Starovoitov , Sasha Levin Subject: [PATCH 5.15 033/277] bpf: Make dst_port field in struct bpf_sock 16-bit wide Date: Tue, 12 Apr 2022 08:27:16 +0200 Message-Id: <20220412062943.002757383@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220412062942.022903016@linuxfoundation.org> References: <20220412062942.022903016@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jakub Sitnicki [ Upstream commit 4421a582718ab81608d8486734c18083b822390d ] Menglong Dong reports that the documentation for the dst_port field in struct bpf_sock is inaccurate and confusing. From the BPF program PoV, the field is a zero-padded 16-bit integer in network byte order. The value appears to the BPF user as if laid out in memory as so: offsetof(struct bpf_sock, dst_port) + 0 + 8 +16 0x00 +24 0x00 32-, 16-, and 8-bit wide loads from the field are all allowed, but only if the offset into the field is 0. 32-bit wide loads from dst_port are especially confusing. The loaded value, after converting to host byte order with bpf_ntohl(dst_port), contains the port number in the upper 16-bits. Remove the confusion by splitting the field into two 16-bit fields. For backward compatibility, allow 32-bit wide loads from offsetof(struct bpf_sock, dst_port). While at it, allow loads 8-bit loads at offset [0] and [1] from dst_port. Reported-by: Menglong Dong Signed-off-by: Jakub Sitnicki Link: https://lore.kernel.org/r/20220130115518.213259-2-jakub@cloudflare.com Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- include/uapi/linux/bpf.h | 3 ++- net/core/filter.c | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index e3fb5e520511..2136e45656ab 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -5347,7 +5347,8 @@ struct bpf_sock { __u32 src_ip4; __u32 src_ip6[4]; __u32 src_port; /* host byte order */ - __u32 dst_port; /* network byte order */ + __be16 dst_port; /* network byte order */ + __u16 :16; /* zero padding */ __u32 dst_ip4; __u32 dst_ip6[4]; __u32 state; diff --git a/net/core/filter.c b/net/core/filter.c index 76e406965b6f..a65de7ac60aa 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -7966,6 +7966,7 @@ bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type, struct bpf_insn_access_aux *info) { const int size_default = sizeof(__u32); + int field_size; if (off < 0 || off >= sizeof(struct bpf_sock)) return false; @@ -7977,7 +7978,6 @@ bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type, case offsetof(struct bpf_sock, family): case offsetof(struct bpf_sock, type): case offsetof(struct bpf_sock, protocol): - case offsetof(struct bpf_sock, dst_port): case offsetof(struct bpf_sock, src_port): case offsetof(struct bpf_sock, rx_queue_mapping): case bpf_ctx_range(struct bpf_sock, src_ip4): @@ -7986,6 +7986,14 @@ bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type, case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]): bpf_ctx_record_field_size(info, size_default); return bpf_ctx_narrow_access_ok(off, size, size_default); + case bpf_ctx_range(struct bpf_sock, dst_port): + field_size = size == size_default ? + size_default : sizeof_field(struct bpf_sock, dst_port); + bpf_ctx_record_field_size(info, field_size); + return bpf_ctx_narrow_access_ok(off, size, field_size); + case offsetofend(struct bpf_sock, dst_port) ... + offsetof(struct bpf_sock, dst_ip4) - 1: + return false; } return size == size_default; -- 2.35.1