Received: by 2002:ac0:b08d:0:0:0:0:0 with SMTP id l13csp4921474imc; Mon, 25 Feb 2019 13:41:14 -0800 (PST) X-Google-Smtp-Source: AHgI3IZ0nhgq4iX6cBdfsSfvbaOP1ii3ogGek/Ux8t166FOXCuEFwc4E6A1VFaCW4ocvHDYTLApp X-Received: by 2002:a17:902:d83:: with SMTP id 3mr22207106plv.43.1551130874406; Mon, 25 Feb 2019 13:41:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551130874; cv=none; d=google.com; s=arc-20160816; b=lliRM270ApWDBs7Si2eCGJjFMDkaifSTY229KcWIG2tK58JB2OecgpMkApUKS0jwlU 4iVW3ec2+TWJ6Yd0X0LHaOe2u+Gd+/+U8yQIMXLjja3puDeUQXHJPFvW+03dWUWPR8YF nz1QX+JCr/VZax7BwkVmRQOgwgXfulMP7jHgtdLF+xJpMBS3OxLjund1OWunsyDT0ol4 ISYlVYnitEVWGtTQ6Z3DInbz6E8NOn8aEWdnC3Ardj+Ibi4W/pY0Lv+UUP+t7RmUGMD6 LtWW0FzRvcX+gDGGGVH6t/D8XRcpyp6oF8zgPtFsvTmVTRTWt/9szfWiefUwNRfYbN2E hc4g== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=yhAR0dt2qW9qt/JESDO8o05yT3NEwx8Jiuhqeur/gRk=; b=dA1fMvVNUctb4DWOBwwKfY1EqCYchTnedkA/OollvmeR0U5yc4zsE4/J/kNv7H8hNs UcMKUN+nICifVH105Vh5lE1h2OLZmBHXoJHV2IDbCo/H3bYwaHvSA21R1ika6llhKvUd icF4RJryIuBsRuhyJT+OaaZ7ESfhKrj6Zn0kvT706rNTQXMuHD4MN2T7+7y4sFN+BhIa PLMTiIAmZG+B8xagyS3CEykeOZPAU4WXVf1p/OKciYQd64hYXtEojZ49icEAJA8TmlrB 7iablEfywlEDUW96lo2b/16F7ebE5g8KTfp+B8VU8Er3ARovOxSoSS+b0LO21b6QBqwB sY/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="n/FN5Qed"; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b9si10146332pfi.288.2019.02.25.13.40.59; Mon, 25 Feb 2019 13:41:14 -0800 (PST) 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="n/FN5Qed"; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732969AbfBYVfX (ORCPT + 99 others); Mon, 25 Feb 2019 16:35:23 -0500 Received: from mail.kernel.org ([198.145.29.99]:41498 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732346AbfBYVfW (ORCPT ); Mon, 25 Feb 2019 16:35:22 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (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 A3A262184E; Mon, 25 Feb 2019 21:35:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1551130521; bh=A6UrP24CN1HNEWbB9dDJYNKtSFQDAlWHzc68RTR+LBM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n/FN5QedW8JDHowyMzx5GP0QAWpkRbniriSrcjD0mEZJqcMpktSWactj8G05PEohN qDsFTdELsa5HOVMgzKP9d2o+stH4egIVPcEY4oKQGAncIEa/trVzAPhHsdX+yigx3f UtJCW6h4Air2shQhsCiP7TUbnwwVE4ULukhjNvjY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Adrian , Ben Hutchings , Jamal Hadi Salim , Jiri Pirko , Cong Wang , "David S. Miller" Subject: [PATCH 4.20 140/183] net_sched: fix a race condition in tcindex_destroy() Date: Mon, 25 Feb 2019 22:11:53 +0100 Message-Id: <20190225195118.468848266@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190225195054.748060397@linuxfoundation.org> References: <20190225195054.748060397@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.20-stable review patch. If anyone has any objections, please let me know. ------------------ From: Cong Wang [ Upstream commit 8015d93ebd27484418d4952284fd02172fa4b0b2 ] tcindex_destroy() invokes tcindex_destroy_element() via a walker to delete each filter result in its perfect hash table, and tcindex_destroy_element() calls tcindex_delete() which schedules tcf RCU works to do the final deletion work. Unfortunately this races with the RCU callback __tcindex_destroy(), which could lead to use-after-free as reported by Adrian. Fix this by migrating this RCU callback to tcf RCU work too, as that workqueue is ordered, we will not have use-after-free. Note, we don't need to hold netns refcnt because we don't call tcf_exts_destroy() here. Fixes: 27ce4f05e2ab ("net_sched: use tcf_queue_work() in tcindex filter") Reported-by: Adrian Cc: Ben Hutchings Cc: Jamal Hadi Salim Cc: Jiri Pirko Signed-off-by: Cong Wang Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/sched/cls_tcindex.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) --- a/net/sched/cls_tcindex.c +++ b/net/sched/cls_tcindex.c @@ -48,7 +48,7 @@ struct tcindex_data { u32 hash; /* hash table size; 0 if undefined */ u32 alloc_hash; /* allocated size */ u32 fall_through; /* 0: only classify if explicit match */ - struct rcu_head rcu; + struct rcu_work rwork; }; static inline int tcindex_filter_is_set(struct tcindex_filter_result *r) @@ -229,9 +229,11 @@ static int tcindex_destroy_element(struc return tcindex_delete(tp, arg, &last, NULL); } -static void __tcindex_destroy(struct rcu_head *head) +static void tcindex_destroy_work(struct work_struct *work) { - struct tcindex_data *p = container_of(head, struct tcindex_data, rcu); + struct tcindex_data *p = container_of(to_rcu_work(work), + struct tcindex_data, + rwork); kfree(p->perfect); kfree(p->h); @@ -258,9 +260,11 @@ static int tcindex_filter_result_init(st return tcf_exts_init(&r->exts, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE); } -static void __tcindex_partial_destroy(struct rcu_head *head) +static void tcindex_partial_destroy_work(struct work_struct *work) { - struct tcindex_data *p = container_of(head, struct tcindex_data, rcu); + struct tcindex_data *p = container_of(to_rcu_work(work), + struct tcindex_data, + rwork); kfree(p->perfect); kfree(p); @@ -478,7 +482,7 @@ tcindex_set_parms(struct net *net, struc } if (oldp) - call_rcu(&oldp->rcu, __tcindex_partial_destroy); + tcf_queue_work(&oldp->rwork, tcindex_partial_destroy_work); return 0; errout_alloc: @@ -570,7 +574,7 @@ static void tcindex_destroy(struct tcf_p walker.fn = tcindex_destroy_element; tcindex_walk(tp, &walker); - call_rcu(&p->rcu, __tcindex_destroy); + tcf_queue_work(&p->rwork, tcindex_destroy_work); }