From patchwork Thu Mar 24 11:14:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thadeu Lima de Souza Cascardo X-Patchwork-Id: 1608978 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=uUJt5nRo; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KPN2p1lfhz9s1l for ; Thu, 24 Mar 2022 22:16:34 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1nXLS8-0006sJ-3B; Thu, 24 Mar 2022 11:16:28 +0000 Received: from smtp-relay-canonical-0.internal ([10.131.114.83] helo=smtp-relay-canonical-0.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1nXLS7-0006s2-3n for kernel-team@lists.ubuntu.com; Thu, 24 Mar 2022 11:16:27 +0000 Received: from localhost.localdomain (1.general.cascardo.us.vpn [10.172.70.58]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-canonical-0.canonical.com (Postfix) with ESMTPSA id 233BD3F63E for ; Thu, 24 Mar 2022 11:16:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1648120586; bh=p1rg+vwz9CbMy38Oh0WRrb6RQcLTh7Es+YWAcmUZH6I=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uUJt5nRocQ7aLhlc0C1tIouQPQGAD/+HK94iIjM2d+nrtdZNJRvAbKtZ62hRKH+D/ 7ULTMTMyq+fCGUyERiMPNgJ3Ft3lM/ZPsiCFikpG1gfPdxl1OCHdY2R/ZXmZ2nc/kE XZkr6RsfxlyOCqZunfINci9lphCM4jVNWBWra3dipq9WjaqVH6nIQfh8mX5RGnKw+i fOzE/QnwcHx+xR2KkE5sjjYvLiGfzoNc9rxRyQU8JNcgw90Hot0Wgh+nu41qy8pqdv 1YfUtS/ou2E+zoIIdaNPKdjoQOaYaDKI5orPoSNOgpyh0m+VF5F14KzhbwOQ+jdZMX 0/f8tSicy5+Mw== From: Thadeu Lima de Souza Cascardo To: kernel-team@lists.ubuntu.com Subject: [SRU Bionic] esp: Fix possible buffer overflow in ESP transformation Date: Thu, 24 Mar 2022 08:14:51 -0300 Message-Id: <20220324111455.389344-2-cascardo@canonical.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220324111455.389344-1-cascardo@canonical.com> References: <20220324111455.389344-1-cascardo@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Steffen Klassert 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 (backported from commit ebe48d368e97d007bfeb76fcb065d6cfc4c96645) [cascardo: there was not x->encap test on esp6_output_head because of missing commit 0146dca70b877b73c5fd9c67912b8a0ca8a7bac7] [cascardo: add SKB_FRAG_PAGE_ORDER to include/net/sock.h] CVE-2022-27666 Signed-off-by: Thadeu Lima de Souza Cascardo --- include/net/esp.h | 2 ++ include/net/sock.h | 2 ++ net/ipv4/esp4.c | 5 +++++ net/ipv6/esp6.c | 5 +++++ 4 files changed, 14 insertions(+) diff --git a/include/net/esp.h b/include/net/esp.h index 117652eb6ea3..465e38890ee9 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/include/net/sock.h b/include/net/sock.h index 79f9b7ee4e45..8dc208d54232 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -2452,6 +2452,8 @@ extern int sysctl_optmem_max; extern __u32 sysctl_wmem_default; extern __u32 sysctl_rmem_default; +#define SKB_FRAG_PAGE_ORDER get_order(32768) + static inline int sk_get_wmem0(const struct sock *sk, const struct proto *proto) { /* Does this proto have per netns sysctl_wmem ? */ diff --git a/net/ipv4/esp4.c b/net/ipv4/esp4.c index d8151c72205f..2dc2887a0e84 100644 --- a/net/ipv4/esp4.c +++ b/net/ipv4/esp4.c @@ -257,6 +257,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 UDP Encapsulation */ if (x->encap) { @@ -266,6 +267,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 3eb167ad1ce6..cbc663756841 100644 --- a/net/ipv6/esp6.c +++ b/net/ipv6/esp6.c @@ -223,6 +223,11 @@ 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; + + 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)) {