Received: by 10.223.185.116 with SMTP id b49csp824891wrg; Sat, 10 Feb 2018 21:16:40 -0800 (PST) X-Google-Smtp-Source: AH8x227BI5MCjwsyVIEImjq78NIb3vEgi3U7PMiomgkUS0y04zaSXmzrGnKeQ9zAQ5Y4H5ogdreZ X-Received: by 2002:a17:902:d24:: with SMTP id 33-v6mr7218075plu.40.1518326200548; Sat, 10 Feb 2018 21:16:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518326200; cv=none; d=google.com; s=arc-20160816; b=TiItVm8Bd0KEikDViWwukQaDgwMYvB4LjQyBiNw6Yafy+h2aSJ/rJTKNvWsb2hmQPu 0uZC8mGZ0vbQmMKwDNhZokFDIYAu+fG9X/htjBRIDcyMfPAJee4yIpzQV/0GP88M+ycv q1gxkz/O0J0yKhN2Ms50+b6DEv9tkIeUJtJesokjPgekbLA4Nku3k6YTOB/UnwSo5szz XGImST8VQztd2OGntDQr83I6y0Fh5wFPKKyFB/oac7QgmXX9Dblwm5+K5KCj8ZvNUAvB leWxDPN0qnm1OEj6sH/moaGxnRpnMgPhzddNoLWnd5txqTwHqBASmyXPaNvptwJoEjk+ dBnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=mPdJjvMsgYsTSCGTnoM8zjQGK2gMoJipW3sHDtJyXO8=; b=Lh+aVCX3Xcn078OLO9uooEPuAboEF1lu2F6QZTje1/bhcTZ5Ea7stMw2Bh425fCdCH 3ZF/Bj/YnKSRA+awuP/waRHzUnu9v47qh64iwJOImJfuFrTfjAPuQY59fmDqcjPuNknc I59HzOl9lAeQl4627Q/xVwJsjLbG3gVY75OEFuGV7b4hw217lJZpig8rxzGMkg41+/KQ BSxMLU55od6gITBQORRb3MngEPJU88SRpdeEg62U3hFLy4YFWiFRvYBzTdNc5b4ih3DT m5I92Wpo3TSLGbeHgNAweHVlZIMDCdF/OqAawDk8h92NlCV78t1RoR4O6eqqguDTJ+3A z/TQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r17si2677278pfh.44.2018.02.10.21.16.27; Sat, 10 Feb 2018 21:16:40 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754059AbeBKFO1 (ORCPT + 99 others); Sun, 11 Feb 2018 00:14:27 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:41376 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752593AbeBKEdl (ORCPT ); Sat, 10 Feb 2018 23:33:41 -0500 Received: from [2a02:8011:400e:2:6f00:88c8:c921:d332] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ekjKd-0002hS-Ic; Sun, 11 Feb 2018 04:33:39 +0000 Received: from ben by deadeye with local (Exim 4.90) (envelope-from ) id 1ekjKY-0004Wa-Mx; Sun, 11 Feb 2018 04:33:34 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Julian Anastasov" , "Pablo Neira Ayuso" Date: Sun, 11 Feb 2018 04:20:06 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.2 64/79] netfilter: xt_TCPMSS: fix handling of malformed TCP header and options In-Reply-To: X-SA-Exim-Connect-IP: 2a02:8011:400e:2:6f00:88c8:c921:d332 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.2.99-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Pablo Neira Ayuso commit 71ffe9c77dd7a2b62207953091efa8dafec958dd upstream. Make sure the packet has enough room for the TCP header and that it is not malformed. While at it, store tcph->doff*4 in a variable, as it is used several times. This patch also fixes a possible off by one in case of malformed TCP options. Reported-by: Julian Anastasov Signed-off-by: Pablo Neira Ayuso Signed-off-by: Ben Hutchings --- net/netfilter/xt_TCPMSS.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) --- a/net/netfilter/xt_TCPMSS.c +++ b/net/netfilter/xt_TCPMSS.c @@ -51,7 +51,8 @@ tcpmss_mangle_packet(struct sk_buff *skb { const struct xt_tcpmss_info *info = par->targinfo; struct tcphdr *tcph; - unsigned int tcplen, i; + int len, tcp_hdrlen; + unsigned int i; __be16 oldval; u16 newmss; u8 *opt; @@ -63,11 +64,14 @@ tcpmss_mangle_packet(struct sk_buff *skb if (!skb_make_writable(skb, skb->len)) return -1; - tcplen = skb->len - tcphoff; + len = skb->len - tcphoff; + if (len < (int)sizeof(struct tcphdr)) + return -1; + tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff); + tcp_hdrlen = tcph->doff * 4; - /* Header cannot be larger than the packet */ - if (tcplen < tcph->doff*4) + if (len < tcp_hdrlen) return -1; if (info->mss == XT_TCPMSS_CLAMP_PMTU) { @@ -88,9 +92,8 @@ tcpmss_mangle_packet(struct sk_buff *skb newmss = info->mss; opt = (u_int8_t *)tcph; - for (i = sizeof(struct tcphdr); i < tcph->doff*4; i += optlen(opt, i)) { - if (opt[i] == TCPOPT_MSS && tcph->doff*4 - i >= TCPOLEN_MSS && - opt[i+1] == TCPOLEN_MSS) { + for (i = sizeof(struct tcphdr); i <= tcp_hdrlen - TCPOLEN_MSS; i += optlen(opt, i)) { + if (opt[i] == TCPOPT_MSS && opt[i+1] == TCPOLEN_MSS) { u_int16_t oldmss; oldmss = (opt[i+2] << 8) | opt[i+3]; @@ -113,9 +116,10 @@ tcpmss_mangle_packet(struct sk_buff *skb } /* There is data after the header so the option can't be added - without moving it, and doing so may make the SYN packet - itself too large. Accept the packet unmodified instead. */ - if (tcplen > tcph->doff*4) + * without moving it, and doing so may make the SYN packet + * itself too large. Accept the packet unmodified instead. + */ + if (len > tcp_hdrlen) return 0; /* @@ -132,10 +136,10 @@ tcpmss_mangle_packet(struct sk_buff *skb skb_put(skb, TCPOLEN_MSS); opt = (u_int8_t *)tcph + sizeof(struct tcphdr); - memmove(opt + TCPOLEN_MSS, opt, tcplen - sizeof(struct tcphdr)); + memmove(opt + TCPOLEN_MSS, opt, len - sizeof(struct tcphdr)); inet_proto_csum_replace2(&tcph->check, skb, - htons(tcplen), htons(tcplen + TCPOLEN_MSS), 1); + htons(len), htons(len + TCPOLEN_MSS), 1); opt[0] = TCPOPT_MSS; opt[1] = TCPOLEN_MSS; opt[2] = (newmss & 0xff00) >> 8;