Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp869063pxb; Tue, 1 Feb 2022 12:06:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJxZBAlAxm5+DrosQLisLa6w5aDLxbr+wGOhPCTE+lypQyeAjkh3rJtXf3p2/kjXo6ZpVRNY X-Received: by 2002:aa7:999b:: with SMTP id k27mr26827877pfh.38.1643745971508; Tue, 01 Feb 2022 12:06:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643745971; cv=none; d=google.com; s=arc-20160816; b=hgyGO6aOsegigFqk0ZykiMZj+2pQAJ6V2ue9rwA16q/A84fB7HKERtcj3d7m4A0RZe Uwk4CseCzUAxKjndln4rLvaQ9MGJ6g8d4FPKuDgxlc6Y89aEEoeYnBcWpqPrBnM+D7bV j40jOIq6a9Sv8uG0DxUThUHI3aOyQyMUMb15lr3eDtQTUM/OnZDQzmCjnokBN1+ULagM evm28URvCehEutK+M48CG+VUClUhax7vW2esZeawRChhxQsmbMQWjGdZlQ+RNLNAUy2i gEGjPlJgtJF0HtopZOav5GMq2iV+tniUAlQbdTwZYCvRO0BFYJez60cFUhO7vMTs9AWM xTEw== 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=a2771leGS8zdpB+qLgJsrVPVzpHKReHSicf5VWCt5So=; b=waLb+uizVjiq2BK4wve2KobEQ/XBdoe75xoh4bofPjIl+HXIuveHSCPQjzxDHmoeIG cnVtduPkZ7M19+xoVPakeNRCBL0T79nLbnwwLZtHMswucwbeeSa0Fg81y4ymVlIVx31T PMcg3QL8pzZZTHXXP7X3CTUHHMLtpcqP4QmUlqaeXdnBRD4yhajskJff81Z2CqhIh/sS XFDnO44VKhwZp+hIjwviI9oHn2ALxiDShq5DaMiyMEjuZcF3+ARWKVTvzuY40pRYarlE TAIVVaWpWxr7CBuXU9m4I0bSM1aP6UP/7ZgIFoQEJ24pt+S5RUyDpkqNbkJAIhBZPuR3 EL8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="MlR/jLqV"; 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 b6si2863887pjw.154.2022.02.01.12.05.59; Tue, 01 Feb 2022 12:06:11 -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="MlR/jLqV"; 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 S1377586AbiAaLYK (ORCPT + 99 others); Mon, 31 Jan 2022 06:24:10 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:44196 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359746AbiAaLPE (ORCPT ); Mon, 31 Jan 2022 06:15:04 -0500 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 dfw.source.kernel.org (Postfix) with ESMTPS id 50DAD60B98; Mon, 31 Jan 2022 11:15:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2F6CAC340E8; Mon, 31 Jan 2022 11:15:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643627703; bh=wOrCoH8cZQ5RYeR+TboB30xbv1JXHCedW2gBQ1rg2yk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MlR/jLqVXNHJWq5Z+EQhQ3pk+bfX9yKQ0pE4SG+df06WcydH0k7pl0DDfa4QSufDf Skl66UJQxFc5Fj7wa23rpzexlLpVidBp+0ZwVU9JOUGX/sGT21UFDCmEFSq5POt8vL z/FwhE6vQoE/R1oTf2iF0erXEcXy2Kgr5M0u+B7I= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, caixf , Jakub Kicinski , "David S. Miller" , Sasha Levin Subject: [PATCH 5.15 137/171] ipv4: fix ip option filtering for locally generated fragments Date: Mon, 31 Jan 2022 11:56:42 +0100 Message-Id: <20220131105234.642643745@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220131105229.959216821@linuxfoundation.org> References: <20220131105229.959216821@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: Jakub Kicinski [ Upstream commit 27a8caa59babb96c5890569e131bc0eb6d45daee ] During IP fragmentation we sanitize IP options. This means overwriting options which should not be copied with NOPs. Only the first fragment has the original, full options. ip_fraglist_prepare() copies the IP header and options from previous fragment to the next one. Commit 19c3401a917b ("net: ipv4: place control buffer handling away from fragmentation iterators") moved sanitizing options before ip_fraglist_prepare() which means options are sanitized and then overwritten again with the old values. Fixing this is not enough, however, nor did the sanitization work prior to aforementioned commit. ip_options_fragment() (which does the sanitization) uses ipcb->opt.optlen for the length of the options. ipcb->opt of fragments is not populated (it's 0), only the head skb has the state properly built. So even when called at the right time ip_options_fragment() does nothing. This seems to date back all the way to v2.5.44 when the fast path for pre-fragmented skbs had been introduced. Prior to that ip_options_build() would have been called for every fragment (in fact ever since v2.5.44 the fragmentation handing in ip_options_build() has been dead code, I'll clean it up in -next). In the original patch (see Link) caixf mentions fixing the handling for fragments other than the second one, but I'm not sure how _any_ fragment could have had their options sanitized with the code as it stood. Tested with python (MTU on lo lowered to 1000 to force fragmentation): import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.setsockopt(socket.IPPROTO_IP, socket.IP_OPTIONS, bytearray([7,4,5,192, 20|0x80,4,1,0])) s.sendto(b'1'*2000, ('127.0.0.1', 1234)) Before: IP (tos 0x0, ttl 64, id 1053, offset 0, flags [+], proto UDP (17), length 996, options (RR [bad length 4] [bad ptr 5] 192.148.4.1,,RA value 256)) localhost.36500 > localhost.search-agent: UDP, length 2000 IP (tos 0x0, ttl 64, id 1053, offset 968, flags [+], proto UDP (17), length 996, options (RR [bad length 4] [bad ptr 5] 192.148.4.1,,RA value 256)) localhost > localhost: udp IP (tos 0x0, ttl 64, id 1053, offset 1936, flags [none], proto UDP (17), length 100, options (RR [bad length 4] [bad ptr 5] 192.148.4.1,,RA value 256)) localhost > localhost: udp After: IP (tos 0x0, ttl 96, id 42549, offset 0, flags [+], proto UDP (17), length 996, options (RR [bad length 4] [bad ptr 5] 192.148.4.1,,RA value 256)) localhost.51607 > localhost.search-agent: UDP, bad length 2000 > 960 IP (tos 0x0, ttl 96, id 42549, offset 968, flags [+], proto UDP (17), length 996, options (NOP,NOP,NOP,NOP,RA value 256)) localhost > localhost: udp IP (tos 0x0, ttl 96, id 42549, offset 1936, flags [none], proto UDP (17), length 100, options (NOP,NOP,NOP,NOP,RA value 256)) localhost > localhost: udp RA (20 | 0x80) is now copied as expected, RR (7) is "NOPed out". Link: https://lore.kernel.org/netdev/20220107080559.122713-1-ooppublic@163.com/ Fixes: 19c3401a917b ("net: ipv4: place control buffer handling away from fragmentation iterators") Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: caixf Signed-off-by: Jakub Kicinski Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/ipv4/ip_output.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c index 9bca57ef8b838..ff38b46bd4b0f 100644 --- a/net/ipv4/ip_output.c +++ b/net/ipv4/ip_output.c @@ -826,15 +826,24 @@ int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, /* Everything is OK. Generate! */ ip_fraglist_init(skb, iph, hlen, &iter); - if (iter.frag) - ip_options_fragment(iter.frag); - for (;;) { /* Prepare header of the next frame, * before previous one went down. */ if (iter.frag) { + bool first_frag = (iter.offset == 0); + IPCB(iter.frag)->flags = IPCB(skb)->flags; ip_fraglist_prepare(skb, &iter); + if (first_frag && IPCB(skb)->opt.optlen) { + /* ipcb->opt is not populated for frags + * coming from __ip_make_skb(), + * ip_options_fragment() needs optlen + */ + IPCB(iter.frag)->opt.optlen = + IPCB(skb)->opt.optlen; + ip_options_fragment(iter.frag); + ip_send_check(iter.iph); + } } skb->tstamp = tstamp; -- 2.34.1