From patchwork Mon Feb 11 01:47:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 1039549 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="gRRj8kPm"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 43yTFM1P3Yz9s1l for ; Mon, 11 Feb 2019 12:47:51 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726860AbfBKBrt (ORCPT ); Sun, 10 Feb 2019 20:47:49 -0500 Received: from mail-pl1-f194.google.com ([209.85.214.194]:46151 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726102AbfBKBrs (ORCPT ); Sun, 10 Feb 2019 20:47:48 -0500 Received: by mail-pl1-f194.google.com with SMTP id o6so4525945pls.13 for ; Sun, 10 Feb 2019 17:47:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=eQkc/FAzW3XahBoxnXxaFR5/ZL+vi9d9obbrAhKY5Ww=; b=gRRj8kPmRbbjGGlGMBQC8KrJ6IGKo5n6hnMIGocKRxcbLL8eaaaDgRj5JVz9e4otWK fdGGcwcvVAwjqv1Hjy+ZQCiv328n+wVMxmzhV0PRgkA2Dg+BVC/bGZnxCl8OJ1y2ftO+ pq5DtMjTtbZZtnPZWFV/K2lt94OvXp9EaPZJRnTPQtbSPUZbxX/hhbS2Xh6WvG6AX1xI xuJf8LCSwZhqiQzuazbix4KYFtTMcl2qHxE9Pw1ozWVQm4YbP5JuYBPZbLUpoQGh+MgH OFsgfbWQZTXAZj2sb6ehq5BJfhweVtx407s2HeYhNMywZx3FDL6WrY2nMWwS8f/tqX4M 3NvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=eQkc/FAzW3XahBoxnXxaFR5/ZL+vi9d9obbrAhKY5Ww=; b=LrH2z+cSVGYxMPaw8Su71JUpwIAlVx8Lo1lxahJ6RjCBDcWuyETBN3ImpEY/rUcAIv bu1ltq1CkbpBd0hr/wxuMJAmvF6si3CXf3sF6MirCnmQEJX2Uo19CeUN78a46IO7JISE uOVtOpcRB3uCthuzo0R5fMMM2nu1oP0Eq8fokhEjrEosRlKa4k5Mj5wLoxdI1aaGTmvP 7cbfWl3nbyFW73bplv4Vmk4h8SyhDblORjfoNeAgxEK6mkJONK3fCNly9IGbBDT3soVC Ul9JDEgZrNQqMjD3s8HLIBbos7Ap5OBMyfnhGNBCsqUtwJD20k1Bufyf/m7DMHVHZQFe LzPw== X-Gm-Message-State: AHQUAuY0Y7awghi7EGPeZHAxy6Nhd0djYxX75JD8RAgy10HaYmz028cu zkKYUpVSjrUK8rHevazRenyPEczS X-Google-Smtp-Source: AHgI3IbzDOtR8Q9TLljoYiGYON2GJHuGCE4yld99JEhCCW+ecehuyLzggIXkEWjW09J71i3Wq3TtJA== X-Received: by 2002:a17:902:834b:: with SMTP id z11mr36141698pln.151.1549849666995; Sun, 10 Feb 2019 17:47:46 -0800 (PST) Received: from tw-172-25-17-123.office.twttr.net ([8.25.197.25]) by smtp.gmail.com with ESMTPSA id p67sm11406703pfg.44.2019.02.10.17.47.45 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 10 Feb 2019 17:47:45 -0800 (PST) From: Cong Wang To: netdev@vger.kernel.org Cc: Cong Wang , Adrian , Ben Hutchings , Jamal Hadi Salim , Jiri Pirko Subject: [Patch net 1/3] net_sched: fix a race condition in tcindex_destroy() Date: Sun, 10 Feb 2019 17:47:29 -0800 Message-Id: <20190211014731.23932-2-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190211014731.23932-1-xiyou.wangcong@gmail.com> References: <20190211014731.23932-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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. This change requires us to store a net pointer inside struct tcindex_data, to avoid the known race with tc_action_net_exit(). 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 --- net/sched/cls_tcindex.c | 46 ++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/net/sched/cls_tcindex.c b/net/sched/cls_tcindex.c index 9ccc93f257db..14e6d80dd58e 100644 --- a/net/sched/cls_tcindex.c +++ b/net/sched/cls_tcindex.c @@ -48,7 +48,8 @@ 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 net *net; + struct rcu_work rwork; }; static inline int tcindex_filter_is_set(struct tcindex_filter_result *r) @@ -229,15 +230,23 @@ static int tcindex_destroy_element(struct tcf_proto *tp, return tcindex_delete(tp, arg, &last, NULL); } -static void __tcindex_destroy(struct rcu_head *head) +static void __tcindex_destroy(struct tcindex_data *p) { - struct tcindex_data *p = container_of(head, struct tcindex_data, rcu); - kfree(p->perfect); kfree(p->h); kfree(p); } +static void tcindex_destroy_work(struct work_struct *work) +{ + struct tcindex_data *p = container_of(to_rcu_work(work), + struct tcindex_data, + rwork); + + put_net(p->net); + __tcindex_destroy(p); +} + static inline int valid_perfect_hash(struct tcindex_data *p) { @@ -258,14 +267,22 @@ static int tcindex_filter_result_init(struct tcindex_filter_result *r) 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(struct tcindex_data *p) { - struct tcindex_data *p = container_of(head, struct tcindex_data, rcu); - kfree(p->perfect); kfree(p); } +static void tcindex_partial_destroy_work(struct work_struct *work) +{ + struct tcindex_data *p = container_of(to_rcu_work(work), + struct tcindex_data, + rwork); + + put_net(p->net); + __tcindex_partial_destroy(p); +} + static void tcindex_free_perfect_hash(struct tcindex_data *cp) { int i; @@ -333,6 +350,7 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base, cp->alloc_hash = p->alloc_hash; cp->fall_through = p->fall_through; cp->tp = tp; + cp->net = net; if (p->perfect) { int i; @@ -477,8 +495,13 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base, rcu_assign_pointer(*fp, f); } - if (oldp) - call_rcu(&oldp->rcu, __tcindex_partial_destroy); + if (oldp) { + if (oldp->net && maybe_get_net(oldp->net)) + tcf_queue_work(&oldp->rwork, + tcindex_partial_destroy_work); + else + __tcindex_partial_destroy(oldp); + } return 0; errout_alloc: @@ -570,7 +593,10 @@ static void tcindex_destroy(struct tcf_proto *tp, walker.fn = tcindex_destroy_element; tcindex_walk(tp, &walker); - call_rcu(&p->rcu, __tcindex_destroy); + if (maybe_get_net(p->net)) + tcf_queue_work(&p->rwork, tcindex_destroy_work); + else + __tcindex_destroy(p); }