Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp860108pxb; Tue, 1 Feb 2022 11:57:13 -0800 (PST) X-Google-Smtp-Source: ABdhPJw2iIHwfi8IKp3TjDJwyVX7W8K7KjqCNXNqvtCk8LWZuTvMBKbvgeM9WTII08KKF7KVSRac X-Received: by 2002:a50:ec12:: with SMTP id g18mr27153237edr.208.1643745432851; Tue, 01 Feb 2022 11:57:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643745432; cv=none; d=google.com; s=arc-20160816; b=YmsW7I0wrhLGIv38c9RxBL7nqsUzlIdOFgKnKSTyB0jnauUQ7bLTKPVBgl+keF4j/+ 4871G0OKISyYo776YRffM+gEWBWsmNWUhar2Ae+mYB/6J/kacfO4azj5w2ubWUF5kqn7 Va05ggKWuroSc31SjOJlVJf3EQM1ZMoIyQHRAgX1dM0biDrQDqvcEh7XE3Fdk4gNCAh8 dKAXTPNzdMmVtJcqpdL0xAOhz3Z/UKYUiDuoz6C4hIywipk5xryRPIRzW/E9il8JfBIw aMc/+9SAKIZRWU5/2oAxXdncStjQOoALh2WoelmfQVo7xO7mqSuwmSW7W4ADm+vdn6rO TaHg== 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=T+WCldhBp0RWWDZVhIdyKvWePAEJpw4zLUTqpISWYXc=; b=sMWhdLl4IHYkWxBr2iPflJwYVykhORDjQLUu0TF0gF0xsT3S85U/Jjy9vzu519KDn6 3/TAulT4BpZkbjgsj+uuijTQ7cZdMt23XnFcVsFbZZpcd+uQs4nTpEbVcqrmrgOgi8N2 BrrNAyuMwqvrSvLpQefP3+yKONq9N/GY9lxTkCC+jCF1WR2VahDOJjoaTcgGkUztJZ9A P1IwirmPUu6VYF0ppvWHnP0S+OfyuAAD3Z5eJpqiwRv3japGLEOzbx6VmItrTBzO2DaC kun1JCwfCOCbe6TbZq+EaUTOWkvUaG6Un51Xx/dkFe04RcIunTO8fFW577mRS4KPc1pK 3bfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=eh5gvIoV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m25si9641293ejn.613.2022.02.01.11.56.47; Tue, 01 Feb 2022 11:57:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=eh5gvIoV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.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: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358857AbiAaLEu (ORCPT + 99 others); Mon, 31 Jan 2022 06:04:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1358796AbiAaLCf (ORCPT ); Mon, 31 Jan 2022 06:02:35 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A1A5C06136C; Mon, 31 Jan 2022 03:00:54 -0800 (PST) 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 44601B82A69; Mon, 31 Jan 2022 11:00:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BC15BC340EE; Mon, 31 Jan 2022 11:00:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643626852; bh=krk3JK/6W2Uck8W8Qsdm4XLEPCdioCNoGKl/TvaDwy4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eh5gvIoVVs6T2Ifa+buAzEnu1dN2qI+ql6u7wxKim7nBsXvOmXef9XvIF2UcjmbfW BWoKXu78dUe+OL3cd2f/UHop/5aoMb58ciuIEhRF+mosfVjeon67XULhByjQa8CVad 1I55LrYfF0b85fGAKtyILnjDU3qI4zZdhv796cYM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Dumazet , Ray Che , David Ahern , Geoff Alexander , Willy Tarreau , Jakub Kicinski , Sasha Levin Subject: [PATCH 5.4 60/64] ipv4: tcp: send zero IPID in SYNACK messages Date: Mon, 31 Jan 2022 11:56:45 +0100 Message-Id: <20220131105217.695729664@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220131105215.644174521@linuxfoundation.org> References: <20220131105215.644174521@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Dumazet [ Upstream commit 970a5a3ea86da637471d3cd04d513a0755aba4bf ] In commit 431280eebed9 ("ipv4: tcp: send zero IPID for RST and ACK sent in SYN-RECV and TIME-WAIT state") we took care of some ctl packets sent by TCP. It turns out we need to use a similar strategy for SYNACK packets. By default, they carry IP_DF and IPID==0, but there are ways to ask them to use the hashed IP ident generator and thus be used to build off-path attacks. (Ref: Off-Path TCP Exploits of the Mixed IPID Assignment) One of this way is to force (before listener is started) echo 1 >/proc/sys/net/ipv4/ip_no_pmtu_disc Another way is using forged ICMP ICMP_FRAG_NEEDED with a very small MTU (like 68) to force a false return from ip_dont_fragment() In this patch, ip_build_and_send_pkt() uses the following heuristics. 1) Most SYNACK packets are smaller than IPV4_MIN_MTU and therefore can use IP_DF regardless of the listener or route pmtu setting. 2) In case the SYNACK packet is bigger than IPV4_MIN_MTU, we use prandom_u32() generator instead of the IPv4 hashed ident one. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Eric Dumazet Reported-by: Ray Che Reviewed-by: David Ahern Cc: Geoff Alexander Cc: Willy Tarreau Signed-off-by: Jakub Kicinski Signed-off-by: Sasha Levin --- net/ipv4/ip_output.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c index 0ec529d77a56e..418e939878004 100644 --- a/net/ipv4/ip_output.c +++ b/net/ipv4/ip_output.c @@ -161,12 +161,19 @@ int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk, iph->daddr = (opt && opt->opt.srr ? opt->opt.faddr : daddr); iph->saddr = saddr; iph->protocol = sk->sk_protocol; - if (ip_dont_fragment(sk, &rt->dst)) { + /* Do not bother generating IPID for small packets (eg SYNACK) */ + if (skb->len <= IPV4_MIN_MTU || ip_dont_fragment(sk, &rt->dst)) { iph->frag_off = htons(IP_DF); iph->id = 0; } else { iph->frag_off = 0; - __ip_select_ident(net, iph, 1); + /* TCP packets here are SYNACK with fat IPv4/TCP options. + * Avoid using the hashed IP ident generator. + */ + if (sk->sk_protocol == IPPROTO_TCP) + iph->id = (__force __be16)prandom_u32(); + else + __ip_select_ident(net, iph, 1); } if (opt && opt->opt.optlen) { -- 2.34.1