Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp487904pxu; Tue, 5 Jan 2021 17:32:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJzAnR/ifnxXke0O5RyUTznplkkoY33WZqbXX8HeD1nAakH7tWDkteFlOIdQOkFg7K+wcq29 X-Received: by 2002:aa7:da01:: with SMTP id r1mr2368267eds.45.1609896734781; Tue, 05 Jan 2021 17:32:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609896734; cv=none; d=google.com; s=arc-20160816; b=cfQc7c6yZXXczmucFwg/L85NgjpaEUSzndosPd8gCKfUw+ITVh9UzkcfepPk9LCgje O1YTOnN/ZY6Kwx0DiMTF9zlW2thqDvAr4N3rwVrVrdwepoVnZins2M9irc/aPjHf9DhI hmaXlB+cn1+BhQm5CiueewpAIkumoFML5TWG+7vBHx1rP23e1yV+V9YsTFuBCOdxQwam uf6TiKp+REpHtsJsQJ6yOTeMXsSNpM5k7oCzWNHB1IDoEATiCLCwzw0xCW1//MWnuIpd Fvxl6Xy6MvpJwJWRO16Au4PiRKvOLMr7TzJe46vbhmY5br+gW8lxndJRsXgtVnmtbJjJ 9+HQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:dlp-filter:cms-type:content-language :thread-index:content-transfer-encoding:mime-version:message-id:date :subject:in-reply-to:cc:to:from:dkim-signature:dkim-filter; bh=3QXRuG8IcfTM8EsdS2v4RlosyPk5o5uoH+4fZbr0kKw=; b=HHH28U9sDOFpwu5IBTP7OyD64QQuwbe+Spc0IR2rWUgP0GNd6qY3UWX3dTkUIlAeaf FGHP2eGODCO0Ap0zvo45vFzK1w5UUGH1/Mr9nPnH4MlD2+ISXVHbkzlHxt/UH/bT9xSn 3WCbl7i1V3yMey6nsDsydXdG5+2q3SD042A7iQFPrUe5Y/P7xZ+P/pzQcy/Invqq99tY rUVwhYcmjjCM2a8TGALwJSVkpCElmT5vZd3BgcKju0nRWFo21tQ4kAAoR3l9V6OkQDFe maz5GnhW0R+Z/G1LI3fnQ3vPu1vuGwUNg4l9b+jJHE17tVH4u1hPq60laqiZAnflSrCU SESg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=VCIIp71Z; 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=samsung.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m10si333635eji.137.2021.01.05.17.31.51; Tue, 05 Jan 2021 17:32:14 -0800 (PST) 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=@samsung.com header.s=mail20170921 header.b=VCIIp71Z; 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=samsung.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725817AbhAFBac (ORCPT + 99 others); Tue, 5 Jan 2021 20:30:32 -0500 Received: from mailout4.samsung.com ([203.254.224.34]:25574 "EHLO mailout4.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725824AbhAFBab (ORCPT ); Tue, 5 Jan 2021 20:30:31 -0500 Received: from epcas2p1.samsung.com (unknown [182.195.41.53]) by mailout4.samsung.com (KnoxPortal) with ESMTP id 20210106012947epoutp047df8a5bda276ed94b2cf24d490a6de31~XgGqld9-O2638326383epoutp04L for ; Wed, 6 Jan 2021 01:29:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout4.samsung.com 20210106012947epoutp047df8a5bda276ed94b2cf24d490a6de31~XgGqld9-O2638326383epoutp04L DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1609896587; bh=3QXRuG8IcfTM8EsdS2v4RlosyPk5o5uoH+4fZbr0kKw=; h=From:To:Cc:In-Reply-To:Subject:Date:References:From; b=VCIIp71ZMLgAcSEC+LJk2+709fBAwsY4+ruktnonYXM2kPwxs1jFlnjnF0ZBavZhx hkIPnmb/8aUsgdaKkUn3/yCznpAZNgeYBbhm77uDCa463oGjcgbyYlx0LvtG69cSsP 7tDTASoiagwtrHynRU6TKmJH+ds1hj9AVNfwr7jc= Received: from epsnrtp4.localdomain (unknown [182.195.42.165]) by epcas2p3.samsung.com (KnoxPortal) with ESMTP id 20210106012946epcas2p3f6da8347d9dcb5be13c24e321b8f4d03~XgGqG5rUc0338803388epcas2p3j; Wed, 6 Jan 2021 01:29:46 +0000 (GMT) Received: from epsmges2p2.samsung.com (unknown [182.195.40.181]) by epsnrtp4.localdomain (Postfix) with ESMTP id 4D9Wxh70YczMqYkt; Wed, 6 Jan 2021 01:29:44 +0000 (GMT) Received: from epcas2p3.samsung.com ( [182.195.41.55]) by epsmges2p2.samsung.com (Symantec Messaging Gateway) with SMTP id FD.D6.56312.88215FF5; Wed, 6 Jan 2021 10:29:44 +0900 (KST) Received: from epsmtrp1.samsung.com (unknown [182.195.40.13]) by epcas2p2.samsung.com (KnoxPortal) with ESMTPA id 20210106012944epcas2p28eb422cf1d17aaf679df3872f7cf29bd~XgGn16aHp3110831108epcas2p2Y; Wed, 6 Jan 2021 01:29:44 +0000 (GMT) Received: from epsmgms1p1new.samsung.com (unknown [182.195.42.41]) by epsmtrp1.samsung.com (KnoxPortal) with ESMTP id 20210106012944epsmtrp13f5b53ee179260afdfdfbe78165491ef~XgGn0pixb0228702287epsmtrp1S; Wed, 6 Jan 2021 01:29:44 +0000 (GMT) X-AuditID: b6c32a46-1d9ff7000000dbf8-e6-5ff51288e464 Received: from epsmtip1.samsung.com ( [182.195.34.30]) by epsmgms1p1new.samsung.com (Symantec Messaging Gateway) with SMTP id D0.D5.13470.88215FF5; Wed, 6 Jan 2021 10:29:44 +0900 (KST) Received: from KORDO035731 (unknown [12.36.185.47]) by epsmtip1.samsung.com (KnoxPortal) with ESMTPA id 20210106012944epsmtip1c69bd113585033a1655b98e8a28ed143~XgGnjHjvx2292122921epsmtip1M; Wed, 6 Jan 2021 01:29:44 +0000 (GMT) From: "Dongseok Yi" To: "'Willem de Bruijn'" Cc: "'David S. Miller'" , "'Jakub Kicinski'" , "'Miaohe Lin'" , "'Willem de Bruijn'" , "'Paolo Abeni'" , "'Florian Westphal'" , "'Al Viro'" , "'Guillaume Nault'" , "'Yunsheng Lin'" , "'Steffen Klassert'" , "'Yadu Kishore'" , "'Marco Elver'" , "'Network Development'" , "'LKML'" , In-Reply-To: Subject: RE: [PATCH net] net: fix use-after-free when UDP GRO with shared fraglist Date: Wed, 6 Jan 2021 10:29:43 +0900 Message-ID: <017f01d6e3cb$698246a0$3c86d3e0$@samsung.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQGzSLyD0IId2D3HDIYy/7dNsmGIiACENc+NAWJEhNGqUcp8MA== Content-Language: ko X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrDJsWRmVeSWpSXmKPExsWy7bCmuW6H0Nd4g9uvTSzmnG9hsWg7s53V YlvvakaLGZ82sltc2NbHarFu2yJGi87vS1ksLu+aw2bR8JbLouFOM5vFsQViFt9Ov2G02N35 g93i/N/jrBbvthxht1j8cwOTg4DHlpU3mTx2zrrL7rFgU6lHy5G3rB6bVnWyebzfd5XNo2/L KkaPTa1LWD0OfV/A6vF5k5zHpidvmQK4o3JsMlITU1KLFFLzkvNTMvPSbZW8g+Od403NDAx1 DS0tzJUU8hJzU22VXHwCdN0yc4DeUVIoS8wpBQoFJBYXK+nb2RTll5akKmTkF5fYKqUWpOQU GBoW6BUn5haX5qXrJefnWhkaGBiZAlUm5GRMXxJ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775vl+nu/zTR6KL3qAhVJbVFtZ jUqeLCEEmLNSMmryQdFnWWTlWxpZPOkYynhYjiNn5nmAcj+UkeiRMwtHF515ABl6CzFUX2Eh kN4nQPoX+whUbR2GemrbAXIZvpDI8+MejjocVSTK7yvlzacZh83LY66ZX5KMldMx6VU+nOHs BoLpvNlAMFkOO2C4/QU44+614sxHbhTDNft4KwbGCeYo2OQt21jNlLnrBZtzCmLUjimp6VfK cD1olBhBEAVpKWy+dZQwAgElol0AZj95TxoB9ceAsOLykgATAl+nV+EBphXApy+7Cb9B0OGw 3XwA92sxPQv+Onmf9EN8Og2HLR7Hv9QXAJqO92F+KoheCYtPm4Bfh9Ax8NbzUsJ/DaPHwnqb 1D8W0jPh52OdZEAPgTUnmv+u8ulJMPPNfhDQo2G5z8IPtBsD+1qK/pVYCMtKsskAI4YnDRn8 IyDE3C/K3C/K3C/K3G/FCjA7GMGqtcoEpTZKHaVit0do5UqtTpUQsTFFyYG/7w6feBWU27si 3IBHATeAFF8iFqLqTzKRUCHfsZPVpMg0umRW6wYjKUwyXPjIWCMT0QnyrWwSy6pZzX+XRwWF 6nlRq9en7p6orwjWXYwP44ZHLLWnbbKNS6kzRfKlTd5B85Ynej2nSvKHZni6MxcMTnyX1nD2 UncQHrfLlqrePdj29e60M6H192i3zza94bCsRUsdEZ9xr1DfTgpevOfVs7CUV8Wr2zx50Xc2 LmKuHzUqPlkjvYXRuriWAYbYbdy6rrAvdV42eQN5CP1ss/EuI90qU/xCE2Nd23jhxrU1APSE iOIfnzvIm1q3qOedathY19Rz0kHKUx11t/N/NVSZI8fFLQv9plAIpJuafiRMz3mzV1htKZod 2zlQQHIlpW0ucW9Ozvfaxu37WnHUznytPDRjQpYzVzBeXHBTqjBZ0DwJpt0sjwrna7Ty3zIW d6pdAwAA X-CMS-MailID: 20210106012944epcas2p28eb422cf1d17aaf679df3872f7cf29bd X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-Sendblock-Type: AUTO_CONFIDENTIAL CMS-TYPE: 102P DLP-Filter: Pass X-CFilter-Loop: Reflected X-CMS-RootMailID: 20210104085750epcas2p1a5b22559d87df61ef3c8215ae0b470b5 References: <1609750005-115609-1-git-send-email-dseok.yi@samsung.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021-01-05 06:03, Willem de Bruijn wrote: > > On Mon, Jan 4, 2021 at 4:00 AM Dongseok Yi wrote: > > > > skbs in frag_list could be shared by pskb_expand_head() from BPF. > > Can you elaborate on the BPF connection? With the following registered ptypes, /proc/net # cat ptype Type Device Function ALL tpacket_rcv 0800 ip_rcv.cfi_jt 0011 llc_rcv.cfi_jt 0004 llc_rcv.cfi_jt 0806 arp_rcv 86dd ipv6_rcv.cfi_jt BPF checks skb_ensure_writable between tpacket_rcv and ip_rcv (or ipv6_rcv). And it calls pskb_expand_head. [ 132.051228] pskb_expand_head+0x360/0x378 [ 132.051237] skb_ensure_writable+0xa0/0xc4 [ 132.051249] bpf_skb_pull_data+0x28/0x60 [ 132.051262] bpf_prog_331d69c77ea5e964_schedcls_ingres+0x5f4/0x1000 [ 132.051273] cls_bpf_classify+0x254/0x348 [ 132.051284] tcf_classify+0xa4/0x180 [ 132.051294] __netif_receive_skb_core+0x590/0xd28 [ 132.051303] __netif_receive_skb+0x50/0x17c [ 132.051312] process_backlog+0x15c/0x1b8 > > > While tcpdump, sk_receive_queue of PF_PACKET has the original frag_list. > > But the same frag_list is queued to PF_INET (or PF_INET6) as the fraglist > > chain made by skb_segment_list(). > > > > If the new skb (not frag_list) is queued to one of the sk_receive_queue, > > multiple ptypes can see this. The skb could be released by ptypes and > > it causes use-after-free. > > If I understand correctly, a udp-gro-list skb makes it up the receive > path with one or more active packet sockets. > > The packet socket will call skb_clone after accepting the filter. This > replaces the head_skb, but shares the skb_shinfo and thus frag_list. > > udp_rcv_segment later converts the udp-gro-list skb to a list of > regular packets to pass these one-by-one to udp_queue_rcv_one_skb. > Now all the frags are fully fledged packets, with headers pushed > before the payload. This does not change their refcount anymore than > the skb_clone in pf_packet did. This should be 1. > > Eventually udp_recvmsg will call skb_consume_udp on each packet. > > The packet socket eventually also frees its cloned head_skb, which triggers > > kfree_skb_list(shinfo->frag_list) > kfree_skb > skb_unref > refcount_dec_and_test(&skb->users) Every your understanding is right, but > > > > > [ 4443.426215] ------------[ cut here ]------------ > > [ 4443.426222] refcount_t: underflow; use-after-free. > > [ 4443.426291] WARNING: CPU: 7 PID: 28161 at lib/refcount.c:190 > > refcount_dec_and_test_checked+0xa4/0xc8 > > [ 4443.426726] pstate: 60400005 (nZCv daif +PAN -UAO) > > [ 4443.426732] pc : refcount_dec_and_test_checked+0xa4/0xc8 > > [ 4443.426737] lr : refcount_dec_and_test_checked+0xa0/0xc8 > > [ 4443.426808] Call trace: > > [ 4443.426813] refcount_dec_and_test_checked+0xa4/0xc8 > > [ 4443.426823] skb_release_data+0x144/0x264 > > [ 4443.426828] kfree_skb+0x58/0xc4 > > [ 4443.426832] skb_queue_purge+0x64/0x9c > > [ 4443.426844] packet_set_ring+0x5f0/0x820 > > [ 4443.426849] packet_setsockopt+0x5a4/0xcd0 > > [ 4443.426853] __sys_setsockopt+0x188/0x278 > > [ 4443.426858] __arm64_sys_setsockopt+0x28/0x38 > > [ 4443.426869] el0_svc_common+0xf0/0x1d0 > > [ 4443.426873] el0_svc_handler+0x74/0x98 > > [ 4443.426880] el0_svc+0x8/0xc > > > > Fixes: 3a1296a38d0c (net: Support GRO/GSO fraglist chaining.) > > Signed-off-by: Dongseok Yi > > --- > > net/core/skbuff.c | 20 +++++++++++++++++++- > > 1 file changed, 19 insertions(+), 1 deletion(-) > > > > diff --git a/net/core/skbuff.c b/net/core/skbuff.c > > index f62cae3..1dcbda8 100644 > > --- a/net/core/skbuff.c > > +++ b/net/core/skbuff.c > > @@ -3655,7 +3655,8 @@ struct sk_buff *skb_segment_list(struct sk_buff *skb, > > unsigned int delta_truesize = 0; > > unsigned int delta_len = 0; > > struct sk_buff *tail = NULL; > > - struct sk_buff *nskb; > > + struct sk_buff *nskb, *tmp; > > + int err; > > > > skb_push(skb, -skb_network_offset(skb) + offset); > > > > @@ -3665,11 +3666,28 @@ struct sk_buff *skb_segment_list(struct sk_buff *skb, > > nskb = list_skb; > > list_skb = list_skb->next; > > > > + err = 0; > > + if (skb_shared(nskb)) { > > I must be missing something still. This does not square with my > understanding that the two sockets are operating on clones, with each > frag_list skb having skb->users == 1. > > Unless the packet socket patch previously also triggered an > skb_unclone/pskb_expand_head, as that call skb_clone_fraglist, which > calls skb_get on each frag_list skb. A cloned skb after tpacket_rcv cannot go through skb_ensure_writable with the original shinfo. pskb_expand_head reallocates the shinfo of the skb and call skb_clone_fraglist. skb_release_data in pskb_expand_head could not reduce skb->users of the each frag_list skb if skb_shinfo(skb)->dataref == 2. After the reallocation, skb_shinfo(skb)->dataref == 1 but each frag_list skb could have skb->users == 2. > > > > + tmp = skb_clone(nskb, GFP_ATOMIC); > > + if (tmp) { > > + kfree_skb(nskb); > > + nskb = tmp; > > + err = skb_unclone(nskb, GFP_ATOMIC); > > + } else { > > + err = -ENOMEM; > > + } > > + } > > + > > if (!tail) > > skb->next = nskb; > > else > > tail->next = nskb; > > > > + if (unlikely(err)) { > > + nskb->next = list_skb; > > + goto err_linearize; > > + } > > + > > tail = nskb; > > > > delta_len += nskb->len; > > -- > > 2.7.4 > >