Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3936835pxj; Mon, 21 Jun 2021 09:44:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzfyNqOLF+LLnbOVhG70UPQIL5HCpvqSPPa8sl0WcK7lFDniRp37x+liYlkE/crAWck6g9z X-Received: by 2002:a17:906:1792:: with SMTP id t18mr25315964eje.38.1624293898072; Mon, 21 Jun 2021 09:44:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624293898; cv=none; d=google.com; s=arc-20160816; b=GHBQlnWd8HE+e8X0Df29hYuhCFMexL7T+OGgh4a98RbCDkOx6SeCehZE2DaLwoQXeq 3Ia/Qd30jquPnqhvg8C2g6D66gwI1vuMT50a85GOpf9xnSdw7450V45zedBPWt7JaQaC f1AvvQTWU9iYyVxaeSokuU0ZuomEGT5+M8vDDAfh6BBM5lbe9a/wXq4Sk9cCBu4Gf5Kn YbYHWpqZI6h1nSgXpODmjNfZVSmNh0NR2oqVzAZJAEKAN0OCmxaQcdd/4GwiRBiBwn77 9qgRuDiSKF7EnDBT0ocB/qfz22loUo5gsWHjsWHoOQ0xcxGV5SiXKBNaEa3mTkQulNS2 VxCQ== 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=lxilz17vboL6gyxtxUL+5ys9+Uv+9Vu6QmUaevI+Ct0=; b=0S+xkealkIzsBgLo52kqKesGPrMr/RXIosLIR5wNJEMSj1xgelw+calTV7uj+vZ3UR +mIOHhqWZSJ+vuVwIZj/gadfiMBsbQFSlryLRXWxeIWmJIJ4J75tJBBeO/H/1pcgDBRW O0vIu+bcmeUC9dTIVPsYdnSTY/1z6ksctrL7+aAGS/CyySNbtxY/YAZBc8IBcZ8wGIyw p6lEdVWz4xX8GzSlCFaztAo8rThV509UlBKElwpAUCf760/8qS7M9wFxYbAOm/cEyzln 7Mrc3ACizW+NchybDKQwUKusPRaTf79JO+b3kW3yQgakHbF0HnFj9GwwXgdH3y0NKVPJ F0NA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YLSt5MWJ; 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 m20si8175392eda.264.2021.06.21.09.44.35; Mon, 21 Jun 2021 09:44:58 -0700 (PDT) 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=YLSt5MWJ; 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 S232283AbhFUQmY (ORCPT + 99 others); Mon, 21 Jun 2021 12:42:24 -0400 Received: from mail.kernel.org ([198.145.29.99]:56040 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232137AbhFUQhn (ORCPT ); Mon, 21 Jun 2021 12:37:43 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 82F8D6108E; Mon, 21 Jun 2021 16:29:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1624292942; bh=qv1stP+xCY8eceU+/jZqPRtnb1Bt/Wv3iFl93CVvVY8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YLSt5MWJR1V8/BzO1Z9tPN6kpLPa/adTM/yafshdzmvX+JAsGfdg+7JUThCgFNEOM vVgZPo9WTNRt57dvUUy7pH6TnsKKU7DKA+8QK0M32G0CeD0FB/MM1Efvz2oEWg1gL0 DsB0l3OCZM5Q8g6Zr8SOEo8LlPTW8Y8fnCjiIqWY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Young Xiao <92siuyang@gmail.com>, Maxim Mikityanskiy , Florian Westphal , "David S. Miller" , Sasha Levin Subject: [PATCH 5.12 041/178] netfilter: synproxy: Fix out of bounds when parsing TCP options Date: Mon, 21 Jun 2021 18:14:15 +0200 Message-Id: <20210621154923.536748996@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210621154921.212599475@linuxfoundation.org> References: <20210621154921.212599475@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: Maxim Mikityanskiy [ Upstream commit 5fc177ab759418c9537433e63301096e733fb915 ] The TCP option parser in synproxy (synproxy_parse_options) could read one byte out of bounds. When the length is 1, the execution flow gets into the loop, reads one byte of the opcode, and if the opcode is neither TCPOPT_EOL nor TCPOPT_NOP, it reads one more byte, which exceeds the length of 1. This fix is inspired by commit 9609dad263f8 ("ipv4: tcp_input: fix stack out of bounds when parsing TCP options."). v2 changes: Added an early return when length < 0 to avoid calling skb_header_pointer with negative length. Cc: Young Xiao <92siuyang@gmail.com> Fixes: 48b1de4c110a ("netfilter: add SYNPROXY core/target") Signed-off-by: Maxim Mikityanskiy Reviewed-by: Florian Westphal Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/netfilter/nf_synproxy_core.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/net/netfilter/nf_synproxy_core.c b/net/netfilter/nf_synproxy_core.c index b100c04a0e43..3d6d49420db8 100644 --- a/net/netfilter/nf_synproxy_core.c +++ b/net/netfilter/nf_synproxy_core.c @@ -31,6 +31,9 @@ synproxy_parse_options(const struct sk_buff *skb, unsigned int doff, int length = (th->doff * 4) - sizeof(*th); u8 buf[40], *ptr; + if (unlikely(length < 0)) + return false; + ptr = skb_header_pointer(skb, doff + sizeof(*th), length, buf); if (ptr == NULL) return false; @@ -47,6 +50,8 @@ synproxy_parse_options(const struct sk_buff *skb, unsigned int doff, length--; continue; default: + if (length < 2) + return true; opsize = *ptr++; if (opsize < 2) return true; -- 2.30.2