Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp3245850imc; Wed, 13 Mar 2019 12:36:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqwtAuagMqW1UAdlQWhb8Ghk/1SYKaqdz585omK1Ab8/Rs+3YVYy1dFFfXY332Nliti723Z5 X-Received: by 2002:a17:902:24:: with SMTP id 33mr42264938pla.259.1552505814222; Wed, 13 Mar 2019 12:36:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552505814; cv=none; d=google.com; s=arc-20160816; b=X5VElvrV8UCbE8+9Oe8cwPP8VqhsffYN01d/gw2YCz//3A5LapdCJu/TzxBG9swR02 ld7kTcnYN8YmJn7A2pwzz+tsyS2RFOBksmJg4EMduvokhURBggYpU0xIFeE5wvUIAtj/ 6apFZGgzyAzkETINVylMTcrUwHlhPSXNl1r7/Yap8wsaZZP4hxWSmwWEgyGAaOd31Hho WstrWTQxvw6wDmeiaBgz4oiCoWVWlRnJsxQz8U+Df8IfysdwgWOlNcJ24SjaBkA4Yi+c rCLGGculqDgJwy0mVgu6LA8W/+BQnc2gqmgMRKo2Hke2rP3NC/glMG59K3whXIz4m/z4 9xuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=t5PWCrPyWxW0qT753eea94JmCbwWONVejS69qlQeN7E=; b=eU6isVL6Haliu7YVtPK9H1jrx8PH1BKymIBy972tx/33jbBY8rnVGQl2pNAN6k9Lmu j05zNJ9j6jAf2DX/oUUW1lVUyFOjHPcunRprGVcy4m1Bjs3lZRCp2ir3wiH13jY4NqX+ wqmwySgwtEQV28T4QTstAD+r9dI7cKBmtD5tI4u7Hg32q/RtN/ikXDpsiZ+0q5NFsERv pJO88mdYDIa8qkg2eCwe4bI6DPmgzIn/Gzd4AvshfaTvKKVQT/W0vTF2D75NzX1rQyIc Odw2x3tzWis9mVn0ko92Q8CjyqWE1Pboafj3DGQkhDBS17XUCA7jYy0tbASDhVYGT3Sj sZGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=FN2to5G8; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i185si11433224pfe.4.2019.03.13.12.36.37; Wed, 13 Mar 2019 12:36:54 -0700 (PDT) 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; dkim=pass header.i=@kernel.org header.s=default header.b=FN2to5G8; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727071AbfCMTKd (ORCPT + 99 others); Wed, 13 Mar 2019 15:10:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:41094 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726937AbfCMTKb (ORCPT ); Wed, 13 Mar 2019 15:10:31 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C868A2075C; Wed, 13 Mar 2019 19:10:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1552504230; bh=UK7dnFAL3VgAJLtFy0BBzLzHWgADj1ep/nc/+/bfCZE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FN2to5G8rwpC0gpTsxmkNIx9mcpeyboiPgJeIo34mN2yQkFD+Bg2ZxOkKksmeW0sj u0JSJW31dL5YyX+iGoCMKhwvF+cE0R5QG6n4BZTL12zG0wHcWiA7W2c9q5xObysV0d IAsvdnGEiFPZwI0dKcZVN4sE+qUPtKrZMkCWCQME= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Martin Willi , Steffen Klassert , Sasha Levin Subject: [PATCH AUTOSEL 4.20 04/60] esp: Skip TX bytes accounting when sending from a request socket Date: Wed, 13 Mar 2019 15:09:25 -0400 Message-Id: <20190313191021.158171-4-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190313191021.158171-1-sashal@kernel.org> References: <20190313191021.158171-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Martin Willi [ Upstream commit 09db51241118aeb06e1c8cd393b45879ce099b36 ] On ESP output, sk_wmem_alloc is incremented for the added padding if a socket is associated to the skb. When replying with TCP SYNACKs over IPsec, the associated sk is a casted request socket, only. Increasing sk_wmem_alloc on a request socket results in a write at an arbitrary struct offset. In the best case, this produces the following WARNING: WARNING: CPU: 1 PID: 0 at lib/refcount.c:102 esp_output_head+0x2e4/0x308 [esp4] refcount_t: addition on 0; use-after-free. CPU: 1 PID: 0 Comm: swapper/1 Not tainted 5.0.0-rc3 #2 Hardware name: Marvell Armada 380/385 (Device Tree) [...] [] (esp_output_head [esp4]) from [] (esp_output+0xb8/0x180 [esp4]) [] (esp_output [esp4]) from [] (xfrm_output_resume+0x558/0x664) [] (xfrm_output_resume) from [] (xfrm4_output+0x44/0xc4) [] (xfrm4_output) from [] (tcp_v4_send_synack+0xa8/0xe8) [] (tcp_v4_send_synack) from [] (tcp_conn_request+0x7f4/0x948) [] (tcp_conn_request) from [] (tcp_rcv_state_process+0x2a0/0xe64) [] (tcp_rcv_state_process) from [] (tcp_v4_do_rcv+0xf0/0x1f4) [] (tcp_v4_do_rcv) from [] (tcp_v4_rcv+0xdb8/0xe20) [] (tcp_v4_rcv) from [] (ip_protocol_deliver_rcu+0x2c/0x2dc) [] (ip_protocol_deliver_rcu) from [] (ip_local_deliver_finish+0x48/0x54) [] (ip_local_deliver_finish) from [] (ip_local_deliver+0x54/0xec) [] (ip_local_deliver) from [] (ip_rcv+0x48/0xb8) [] (ip_rcv) from [] (__netif_receive_skb_one_core+0x50/0x6c) [...] The issue triggers only when not using TCP syncookies, as for syncookies no socket is associated. Fixes: cac2661c53f3 ("esp4: Avoid skb_cow_data whenever possible") Fixes: 03e2a30f6a27 ("esp6: Avoid skb_cow_data whenever possible") Signed-off-by: Martin Willi Signed-off-by: Steffen Klassert Signed-off-by: Sasha Levin --- net/ipv4/esp4.c | 2 +- net/ipv6/esp6.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/net/ipv4/esp4.c b/net/ipv4/esp4.c index 9e1c840596c5..11c66510949d 100644 --- a/net/ipv4/esp4.c +++ b/net/ipv4/esp4.c @@ -325,7 +325,7 @@ int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info * skb->len += tailen; skb->data_len += tailen; skb->truesize += tailen; - if (sk) + if (sk && sk_fullsock(sk)) refcount_add(tailen, &sk->sk_wmem_alloc); goto out; diff --git a/net/ipv6/esp6.c b/net/ipv6/esp6.c index 63b2b66f9dfa..c053890a1d33 100644 --- a/net/ipv6/esp6.c +++ b/net/ipv6/esp6.c @@ -293,7 +293,7 @@ int esp6_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info skb->len += tailen; skb->data_len += tailen; skb->truesize += tailen; - if (sk) + if (sk && sk_fullsock(sk)) refcount_add(tailen, &sk->sk_wmem_alloc); goto out; -- 2.19.1