Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp890242pxp; Wed, 16 Mar 2022 20:19:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxBmgCtHOItuOXqdtrJD2Y4l2NzWcHGHLfLzbQtpijB9r+6gN7uAfbCtacO8pgK084mXumQ X-Received: by 2002:aa7:9ad8:0:b0:4f7:78d4:de8c with SMTP id x24-20020aa79ad8000000b004f778d4de8cmr2420682pfp.38.1647487182034; Wed, 16 Mar 2022 20:19:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647487182; cv=none; d=google.com; s=arc-20160816; b=G45gemNFTV2Iep2TV5reuZ56tDCOC8J0OugsBVPISrtoK+sLatzjqoabi1/dt9txUO tom6WHHoCENzBi9aWQs242TDgMvmGlCe7PFn75GvuNZ26K/5U+J+y7rOvCAY8J83LZsY 3kZ34IGPyp/30HJgUuxl9Yh8DitmMKJShI+OUPNx50/kuaCa0hOGDmJYBKeCF+UMVV69 6LhqXVM5GSEuozsj1B9EYkfKyL4LJrrZAqxMz3y54TE8W8fTkOIj2klxtBBPiHXfVM2C RMiVpx3LbAoT3K2HbAplaOr4RImqz1emV+fCS5MrakkoByqi9cwlPK2ZH1JmNpvmUGsF UYAg== 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=fbTL++vN1C/Wb83pIWRlupC+b1C7zURehvO19BBvwlj5b3Qy8uwIOeXpik60wsTRxG ElBozpcKQV0ftfJoFlGY/u7dO5Dn02+ALPfgaY+cebCBcGS1tcLd5igwHqPOGA+WEWAP st7KZ/rXAqfhYVAUeem/zzGacrKEez4TojxGzlwk0omzcxhJrUZGHASy2MgJBbKWHk3D H31h5ItG3zdGEUYtFknaswiuaC/YXTra2XHZ3B3b3FDx8RDhnTcXFt/bwzvtDKPbWqtl kqAMGMQ+o/TxO9BKkKDB/+bk8PL8TniQIyYg2RbmbgvmJAYOW5Ujh7iYv7LOod2webpM mcNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=AI2ClrVz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id y1-20020a1709029b8100b00151ef5032ebsi3360867plp.99.2022.03.16.20.19.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Mar 2022 20:19:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=AI2ClrVz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 078E922B09; Wed, 16 Mar 2022 20:19:39 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240867AbiCNMMa (ORCPT + 99 others); Mon, 14 Mar 2022 08:12:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241351AbiCNMIi (ORCPT ); Mon, 14 Mar 2022 08:08:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82BCC4C439; Mon, 14 Mar 2022 05:05:01 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 04E8B612FF; Mon, 14 Mar 2022 12:05:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 36201C36AE2; Mon, 14 Mar 2022 12:04:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1647259500; bh=RsFZTWnfjmpT6i3ems5I0UMcIWdeQ2t770YuUzve0og=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AI2ClrVzUm7PoJ+nbbAVOgtfO77fOOnlikKtpcLhjfe9IVnFppojHJ6t2BDWZvZlw f1gWAjI0mWudXl3p17SVPW+CWi93xcuikiXsm9iXI1cODZIhbTb0f9YDI6sLVosxm8 9yNEfNX82P0w0vV7WS2Jbqa8Vu79/f9BLN43UmKo= 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.15 020/110] esp: Fix possible buffer overflow in ESP transformation Date: Mon, 14 Mar 2022 12:53:22 +0100 Message-Id: <20220314112743.599549873@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220314112743.029192918@linuxfoundation.org> References: <20220314112743.029192918@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=-3.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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