From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B12A8C282D7 for ; Mon, 11 Feb 2019 21:06:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7B4D32081B for ; Mon, 11 Feb 2019 21:06:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PWefQByy" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727036AbfBKVG1 (ORCPT ); Mon, 11 Feb 2019 16:06:27 -0500 Received: from mail-pl1-f195.google.com ([209.85.214.195]:46283 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726930AbfBKVG1 (ORCPT ); Mon, 11 Feb 2019 16:06:27 -0500 Received: by mail-pl1-f195.google.com with SMTP id o6so124788pls.13 for ; Mon, 11 Feb 2019 13:06:26 -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=A0Qm27QBWLhaXMVGevvybHsFPsC0OSlUKY17W5xHZkk=; b=PWefQByyQVkXGNGA0mTYgixpbd5e2QoeI/F9/dS+QbTj83BTdu8o+M0xfua2qFA/M0 KwRwT/FmB/XyNnUF5c1RXsysMnBlzIJKkuPf0ouM4bTwJVN68S9ARz2XpyZ5dbgG64BF 4ujSiDjmNlSOWBSXU83rtoqtzUspe9CPLytimAJjXfg0vundgHkJIW5GUKrfPua15Foo er1Oh+5+VMk8ofkbhKBMHJ+Xfuykt3YJi2BCE3l8jG5dMqso+HW5cUSfd1FEiJChQy/8 bUYrTXfdYzkG+qLVMTkcPi+c7+weKqxP90b4DC08V/Fedo15aSO4jyLbTGa3FR0Yrkrb 5I6g== 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=A0Qm27QBWLhaXMVGevvybHsFPsC0OSlUKY17W5xHZkk=; b=n8xfvq15BGzcbtMN7BcdxfwWzwBwXoQq29+nNyDPmtB0Ok81pA7nDSYtIm3I89E8UP e75gAYzQ+KkG5Hw87K74err+UwMXLUAwYmhk/M/Cn8okoLlV14jJzgYwfBlutrqnafQf ZkfwyiLvUm1H5ZPuLfDxihmNmFekV96YPonYJSeRhpuEJCr5pfZo0tskYRxvgv1inFGl KpPFxJj/ob/WrED4VZKvBqMb3Er62p4kAGk3ZMnGPXU9piHyTuPf/YlOjoPG0Xrn3ZwW 0weHIyofdURm7LKFceUFMvL0QMxBGAgACs1OYabz4l3pfRibUgFrOuZx96AoFxYBNYEs WxmQ== X-Gm-Message-State: AHQUAuZXjnBPgKdRvz+Tx0sYeoMMvA7woYyqF4csfnf+JMfysuGD+cPd kU7ThMyUFHB/S+kNJaXXRy4BWSf1 X-Google-Smtp-Source: AHgI3IZCCtO/+M4p5cj5GYkM7bQmY8R/+Mz2jbtVKfxJxfktGax2VHMKRLsD98B1+WBnK+LJROGqbA== X-Received: by 2002:a17:902:449:: with SMTP id 67mr231650ple.310.1549919186343; Mon, 11 Feb 2019 13:06:26 -0800 (PST) Received: from tw-172-25-17-123.office.twttr.net ([8.25.197.25]) by smtp.gmail.com with ESMTPSA id q127sm14873872pgq.39.2019.02.11.13.06.25 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 11 Feb 2019 13:06:25 -0800 (PST) From: Cong Wang To: netdev@vger.kernel.org Cc: Cong Wang , Adrian , Ben Hutchings , Jamal Hadi Salim , Jiri Pirko Subject: [Patch net v2 1/3] net_sched: fix a race condition in tcindex_destroy() Date: Mon, 11 Feb 2019 13:06:14 -0800 Message-Id: <20190211210616.9592-2-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190211210616.9592-1-xiyou.wangcong@gmail.com> References: <20190211210616.9592-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. 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 --- net/sched/cls_tcindex.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/net/sched/cls_tcindex.c b/net/sched/cls_tcindex.c index 9ccc93f257db..79b52a637dda 100644 --- 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(struct tcf_proto *tp, 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(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_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, struct tcf_proto *tp, unsigned long base, } 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_proto *tp, walker.fn = tcindex_destroy_element; tcindex_walk(tp, &walker); - call_rcu(&p->rcu, __tcindex_destroy); + tcf_queue_work(&p->rwork, tcindex_destroy_work); } -- 2.20.1