Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp767883pxb; Tue, 12 Apr 2022 12:57:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzkvIagVzIywng6RmbqRN6iG94Md6NgSObBe6FgFyvYfpPbj7YaRioRHJXFe4B8aegoF6nb X-Received: by 2002:a05:6a00:10c7:b0:4fd:9ee6:4130 with SMTP id d7-20020a056a0010c700b004fd9ee64130mr40326906pfu.84.1649793456863; Tue, 12 Apr 2022 12:57:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649793456; cv=none; d=google.com; s=arc-20160816; b=vJQJpKD0vPybqJcByTssI6VMToNILo/2H9U8W6aL/v5j/RYE8kJeKlbaXteD4/ip0Y abknYWSwK2hQFsJCvp1rP9jPn2M+xZmDkcqbxX6VwAHiQyJe4/bDr4TV44o4YK/uCHF3 J7pLr2g7WAQM7MLDGOxWwVby9lXF9jo4labLhd110DluWlXtrAurvUx37OZ46YwltuNG HfC/O+qwxn5levPjLMVdN+sPf1INLbksLDJQ9ach0WVO4MtftwTCqZHI07QrCAsrS/JG SnAS00skmgBoEMPpcAE4O5bmYMD8ZCxPDb5kxizka2CL/fz9nF9DVFlt5z+gh6IZVDQs h93A== 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=oZHB6NO7Nm0PUXMMoxaAwknmaIFVDw/6BIhQgh9FLBg=; b=kH6MVYZhdXNWWQ9Htr/CiI1j8xjwVcGan+nbpkSERPeiu5b+4uSADCEG2bgw7fLB0P QAAIF8L46qqUn53BtFGk2BD4Hy/VpgsJ9OWjTrbojrV38Vh4MZTd16gTpvx6wF9+Z38i AU7a/Qt0Rxj7QKaED85Cw1u8fSpsp3iw2lX2Nm8KtLtjsvWqfiX0OO68gXnmd9vO3Amv tncgaekJ+Le/ja/VQ3L5eFKNvYRNo3c6lEJqlekOWldbzkbmf2v6yN2HuJBF0ChT/9/K U4Tx3jgAAo4bOZNT4Yo4roeN8w70jFW/cJhCU9//1Iavj9b12ibIiI5wGsKcKQmb3odR 0afg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=phdtLMSD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id k8-20020a056a00134800b004fa3a8dff83si13379062pfu.58.2022.04.12.12.57.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 12:57:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=phdtLMSD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 2797671A30; Tue, 12 Apr 2022 12:50:57 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350850AbiDLGvB (ORCPT + 99 others); Tue, 12 Apr 2022 02:51:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351595AbiDLGpX (ORCPT ); Tue, 12 Apr 2022 02:45:23 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 466801E3ED; Mon, 11 Apr 2022 23:38:42 -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 DD394B81B1F; Tue, 12 Apr 2022 06:38:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 31F0CC385A1; Tue, 12 Apr 2022 06:38:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649745519; bh=y6oaYYIx9Y7lwbQ9BhHzvHnnJOBFGVQfpoF3q6Ik1q4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=phdtLMSDnIFuv5teR/I2h+EgA48k0OmSms+6gDAyBstWw+d+V0tOI9+aOb2WUOFjE PQI2iEX7tSR42uGcNXw07yn5xSCn/eXJBfrPrRt8dOWIZUCUF9Noti5+v4gNNGPPl8 WrGjsBp34UiVmcJLL1bUEjG/HmqEW7XGGNpm1jJo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Maxim Mikityanskiy , Alexei Starovoitov , Tariq Toukan , Arthur Fabre , Sasha Levin Subject: [PATCH 5.10 124/171] bpf: Support dual-stack sockets in bpf_tcp_check_syncookie Date: Tue, 12 Apr 2022 08:30:15 +0200 Message-Id: <20220412062931.472728326@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220412062927.870347203@linuxfoundation.org> References: <20220412062927.870347203@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: Maxim Mikityanskiy [ Upstream commit 2e8702cc0cfa1080f29fd64003c00a3e24ac38de ] bpf_tcp_gen_syncookie looks at the IP version in the IP header and validates the address family of the socket. It supports IPv4 packets in AF_INET6 dual-stack sockets. On the other hand, bpf_tcp_check_syncookie looks only at the address family of the socket, ignoring the real IP version in headers, and validates only the packet size. This implementation has some drawbacks: 1. Packets are not validated properly, allowing a BPF program to trick bpf_tcp_check_syncookie into handling an IPv6 packet on an IPv4 socket. 2. Dual-stack sockets fail the checks on IPv4 packets. IPv4 clients end up receiving a SYNACK with the cookie, but the following ACK gets dropped. This patch fixes these issues by changing the checks in bpf_tcp_check_syncookie to match the ones in bpf_tcp_gen_syncookie. IP version from the header is taken into account, and it is validated properly with address family. Fixes: 399040847084 ("bpf: add helper to check for a valid SYN cookie") Signed-off-by: Maxim Mikityanskiy Signed-off-by: Alexei Starovoitov Reviewed-by: Tariq Toukan Acked-by: Arthur Fabre Link: https://lore.kernel.org/bpf/20220406124113.2795730-1-maximmi@nvidia.com Signed-off-by: Sasha Levin --- net/core/filter.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/net/core/filter.c b/net/core/filter.c index fe5e0ec5cd3e..ddf9792c0cb2 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -6492,24 +6492,33 @@ BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len if (!th->ack || th->rst || th->syn) return -ENOENT; + if (unlikely(iph_len < sizeof(struct iphdr))) + return -EINVAL; + if (tcp_synq_no_recent_overflow(sk)) return -ENOENT; cookie = ntohl(th->ack_seq) - 1; - switch (sk->sk_family) { - case AF_INET: - if (unlikely(iph_len < sizeof(struct iphdr))) + /* Both struct iphdr and struct ipv6hdr have the version field at the + * same offset so we can cast to the shorter header (struct iphdr). + */ + switch (((struct iphdr *)iph)->version) { + case 4: + if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk)) return -EINVAL; ret = __cookie_v4_check((struct iphdr *)iph, th, cookie); break; #if IS_BUILTIN(CONFIG_IPV6) - case AF_INET6: + case 6: if (unlikely(iph_len < sizeof(struct ipv6hdr))) return -EINVAL; + if (sk->sk_family != AF_INET6) + return -EINVAL; + ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie); break; #endif /* CONFIG_IPV6 */ -- 2.35.1