Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp3315057pxp; Mon, 14 Mar 2022 16:23:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwq0Bgllh2T+hQZw8IaBicmw6x/sgUYBe4S15glrsdj7b/5klgPoas2JK1oKOrqD8lbAtR9 X-Received: by 2002:a05:6402:2812:b0:418:73df:ee4e with SMTP id h18-20020a056402281200b0041873dfee4emr9575673ede.162.1647300199242; Mon, 14 Mar 2022 16:23:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647300199; cv=none; d=google.com; s=arc-20160816; b=Jjq19oSWsjOC64T3WxCHlNAPDnfh9FlVSzhes76OAdz4rso/jrVDmfsLHuxF85vU1Y jTM56lA9HlN6oWUwSmWn+CjjI1LaVUryHJyJ1JisWD5qg2VAtiBVXCMYNRXVWilW5p6L 6+uzV/cOB5tJA/DWJ2KCAxbkNxsy8bz/n+JorTuesWjJp5hAJhZkUKpTj5vYL2wGRUN2 Ke53UqQfqtOGI8IGOSfmVycRMJE03RydsmaEFPhugLEBAZalNp20flA08mJ29V98sUGk 5KKEi8AUjYbLIlQninR6l8CjKs6o/XyvHzmti2GsmZ+YDObexuxhJlsV9IRBTLdyDU8T B4gw== 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=oBJk9Fk5UkV90Ud6FKrQIS8BOfBJoJ/wDjzj/FZhpOs=; b=bkf6BscVKoFqyc2loGluyyKBg7yJYmms9xPLOngpLJJLFHdJ/Ici2jrkznWVQn790x 5SW9c7QX9ry1P8JbUxS7FBK7ICURxsagT+xfwk4ulrhjmOEG1l/8eaGL39Uy8++DcX4v ni/OG3gxCQ/dvxt2ae3iDTYd/Hb1GZvSar5MeDQ5KOIQcxME7Oep4eEeiK+a1VUPazW4 43URxfZWYuiOtMmLjVW7QbUzujX7dezDuv9Eg52DptCvSv2K6C0MiN0jMMlgXrMX65fo X+kwc0Glt1/+GLbtcSmYGbNGjkhL7ZmzSPfo945nDY449B6Krby9X9isn/P9Uewxb0QD n9sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=kjcEMa4z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j18-20020a170906411200b006cfffe0553asi9972609ejk.133.2022.03.14.16.22.30; Mon, 14 Mar 2022 16:23:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=kjcEMa4z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S243867AbiCNMbb (ORCPT + 99 others); Mon, 14 Mar 2022 08:31:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243004AbiCNMUA (ORCPT ); Mon, 14 Mar 2022 08:20:00 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 382F652E18; Mon, 14 Mar 2022 05:15:12 -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 C67D1B80D24; Mon, 14 Mar 2022 12:15:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 17D6AC340E9; Mon, 14 Mar 2022 12:15:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1647260110; bh=RsFZTWnfjmpT6i3ems5I0UMcIWdeQ2t770YuUzve0og=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kjcEMa4z9bSWgpdmAiAKGRHch06w4mVvg7VvQIK8inRiDanMUSZr77v/BMeF1DtaR 2x7u2JPDRNsxlFrT2t96moIUMCQ33C1bm6BMMXi822ybdavaE/JeduVFVUskN1LGF6 MQikQBBDERkw5A4+3gDfgUmVsIZmSa9VbFxuBr+Y= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, valis , Steffen Klassert , Sasha Levin Subject: [PATCH 5.16 023/121] esp: Fix possible buffer overflow in ESP transformation Date: Mon, 14 Mar 2022 12:53:26 +0100 Message-Id: <20220314112744.776782376@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220314112744.120491875@linuxfoundation.org> References: <20220314112744.120491875@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=-8.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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: Steffen Klassert [ Upstream commit ebe48d368e97d007bfeb76fcb065d6cfc4c96645 ] The maximum message size that can be send is bigger than the maximum site that skb_page_frag_refill can allocate. So it is possible to write beyond the allocated buffer. Fix this by doing a fallback to COW in that case. v2: Avoid get get_order() costs as suggested by Linus Torvalds. Fixes: cac2661c53f3 ("esp4: Avoid skb_cow_data whenever possible") Fixes: 03e2a30f6a27 ("esp6: Avoid skb_cow_data whenever possible") Reported-by: valis Signed-off-by: Steffen Klassert Signed-off-by: Sasha Levin --- include/net/esp.h | 2 ++ net/ipv4/esp4.c | 5 +++++ net/ipv6/esp6.c | 5 +++++ 3 files changed, 12 insertions(+) diff --git a/include/net/esp.h b/include/net/esp.h index 9c5637d41d95..90cd02ff77ef 100644 --- a/include/net/esp.h +++ b/include/net/esp.h @@ -4,6 +4,8 @@ #include +#define ESP_SKB_FRAG_MAXSIZE (PAGE_SIZE << SKB_FRAG_PAGE_ORDER) + struct ip_esp_hdr; static inline struct ip_esp_hdr *ip_esp_hdr(const struct sk_buff *skb) diff --git a/net/ipv4/esp4.c b/net/ipv4/esp4.c index e1b1d080e908..70e6c87fbe3d 100644 --- a/net/ipv4/esp4.c +++ b/net/ipv4/esp4.c @@ -446,6 +446,7 @@ int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info * struct page *page; struct sk_buff *trailer; int tailen = esp->tailen; + unsigned int allocsz; /* this is non-NULL only with TCP/UDP Encapsulation */ if (x->encap) { @@ -455,6 +456,10 @@ int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info * return err; } + allocsz = ALIGN(skb->data_len + tailen, L1_CACHE_BYTES); + if (allocsz > ESP_SKB_FRAG_MAXSIZE) + goto cow; + if (!skb_cloned(skb)) { if (tailen <= skb_tailroom(skb)) { nfrags = 1; diff --git a/net/ipv6/esp6.c b/net/ipv6/esp6.c index 883b53fd7846..b7b573085bd5 100644 --- a/net/ipv6/esp6.c +++ b/net/ipv6/esp6.c @@ -483,6 +483,7 @@ int esp6_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info struct page *page; struct sk_buff *trailer; int tailen = esp->tailen; + unsigned int allocsz; if (x->encap) { int err = esp6_output_encap(x, skb, esp); @@ -491,6 +492,10 @@ int esp6_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info return err; } + allocsz = ALIGN(skb->data_len + tailen, L1_CACHE_BYTES); + if (allocsz > ESP_SKB_FRAG_MAXSIZE) + goto cow; + if (!skb_cloned(skb)) { if (tailen <= skb_tailroom(skb)) { nfrags = 1; -- 2.34.1