Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3215572pxb; Mon, 9 Nov 2020 05:48:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJxqAdkSjSZo/8TBhw0eDKlLM84lm5b4OiAY04D8CvTa22SiR2Pc+ntEErCx59bVkhe93zLp X-Received: by 2002:a05:6402:104d:: with SMTP id e13mr14996725edu.322.1604929724257; Mon, 09 Nov 2020 05:48:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604929724; cv=none; d=google.com; s=arc-20160816; b=rupJnHEzcoGZPq5LAnvLp946xmgn6y5CMcB/r8q09a0yGS/oT7MeFuPunjxXFp4g24 oKEqEmmVdNx2Drfee6QI78IU+YX2IdnFxT8wb+NhkLSnuqrXlD0UUGybtWCCkQF3PcB8 Zi5S/SqN1gWPp7BdfHUnyJXQHxCC4+0nrCGNcV6J+mY5R+kcYeg+bNvBNMEYd8x64GyA hSBJejPjTSYE5WKKIpY4gbm2iXwEal5uTnBlCs6LX8yWpCmBASpXuiKFDiwuIFPAfETa TCnWx1v64ePrZJN2YmNtgRC9NV7gtXStVlIGSAmQNLqLaY12xXYP2iRgyMRQPuorplp1 zWuA== 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=YBqTJz+ZqPZ95yl7vh/uw+jSoPtjo+s8OKpBXIXEC5o=; b=OCh75NSdkfVqhKrB3cpUA7c86stjlAFooNH6YZzfP6wcWvTwRj59VhHYiG5lnUnpbo KrXf0paMeQ8oPcaA316YY2JIdyUPKxz8VENJr1JO96DTs68/fYa60KQ53XkOFxKdpWUb ZjSIUIwpIoBxNaX3CNhOynV6GtZSPEBwSztN+yUUi/qmQ2ZBNn5Z80BbEayQMNARmiIS Op06R6n25UM+LNNFkefqhdV4lN2ColajfaZisAXsneF17COAY4iIKX+PdohicDynqWxZ eTK5k6+bb4xHQn0OzUuThe2NaNxaRpXc1gU8Q6shdbzc82jQl4quv1A0yi28AYeE9YHW tCjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0Rt2mgqU; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y99si6868916ede.294.2020.11.09.05.48.20; Mon, 09 Nov 2020 05:48:44 -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=@kernel.org header.s=default header.b=0Rt2mgqU; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731756AbgKINpE (ORCPT + 99 others); Mon, 9 Nov 2020 08:45:04 -0500 Received: from mail.kernel.org ([198.145.29.99]:54366 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730411AbgKINAP (ORCPT ); Mon, 9 Nov 2020 08:00:15 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D02D1216C4; Mon, 9 Nov 2020 13:00:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604926814; bh=JLcEgLdL/kL4nRVPKC9wWdDIZP3QmaLJDeSXO5Gt35Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0Rt2mgqUr3CLe2BOK/uip1GEt2/Epr0+Yxwd02K6Nfe7X9NFhUjJUxyhLwAG9861M hKKfHodZDKGZnNPOzpdVObopMgo32hJCOrExtomfi4AIQnnOnElHNuMuTDrkT3eqjN 8at4/dKEIzDJbRkXbv5LVC/sV/r/zB0QFE4Emhew= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jon Maloy , Thang Hoang Ngo , Tung Nguyen , Xin Long , Cong Wang , Jakub Kicinski Subject: [PATCH 4.9 006/117] tipc: fix memory leak caused by tipc_buf_append() Date: Mon, 9 Nov 2020 13:53:52 +0100 Message-Id: <20201109125025.942325540@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201109125025.630721781@linuxfoundation.org> References: <20201109125025.630721781@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tung Nguyen [ Upstream commit ceb1eb2fb609c88363e06618b8d4bbf7815a4e03 ] Commit ed42989eab57 ("tipc: fix the skb_unshare() in tipc_buf_append()") replaced skb_unshare() with skb_copy() to not reduce the data reference counter of the original skb intentionally. This is not the correct way to handle the cloned skb because it causes memory leak in 2 following cases: 1/ Sending multicast messages via broadcast link The original skb list is cloned to the local skb list for local destination. After that, the data reference counter of each skb in the original list has the value of 2. This causes each skb not to be freed after receiving ACK: tipc_link_advance_transmq() { ... /* release skb */ __skb_unlink(skb, &l->transmq); kfree_skb(skb); <-- memory exists after being freed } 2/ Sending multicast messages via replicast link Similar to the above case, each skb cannot be freed after purging the skb list: tipc_mcast_xmit() { ... __skb_queue_purge(pkts); <-- memory exists after being freed } This commit fixes this issue by using skb_unshare() instead. Besides, to avoid use-after-free error reported by KASAN, the pointer to the fragment is set to NULL before calling skb_unshare() to make sure that the original skb is not freed after freeing the fragment 2 times in case skb_unshare() returns NULL. Fixes: ed42989eab57 ("tipc: fix the skb_unshare() in tipc_buf_append()") Acked-by: Jon Maloy Reported-by: Thang Hoang Ngo Signed-off-by: Tung Nguyen Reviewed-by: Xin Long Acked-by: Cong Wang Link: https://lore.kernel.org/r/20201027032403.1823-1-tung.q.nguyen@dektech.com.au Signed-off-by: Jakub Kicinski Signed-off-by: Greg Kroah-Hartman --- net/tipc/msg.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) --- a/net/tipc/msg.c +++ b/net/tipc/msg.c @@ -140,12 +140,11 @@ int tipc_buf_append(struct sk_buff **hea if (fragid == FIRST_FRAGMENT) { if (unlikely(head)) goto err; - if (skb_cloned(frag)) - frag = skb_copy(frag, GFP_ATOMIC); + *buf = NULL; + frag = skb_unshare(frag, GFP_ATOMIC); if (unlikely(!frag)) goto err; head = *headbuf = frag; - *buf = NULL; TIPC_SKB_CB(head)->tail = NULL; if (skb_is_nonlinear(head)) { skb_walk_frags(head, tail) {