From patchwork Sat Feb 16 18:58:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 1043474 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="rHOU6SHq"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 441ztp5fJ4z9s7h for ; Sun, 17 Feb 2019 05:58:58 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732407AbfBPS6j (ORCPT ); Sat, 16 Feb 2019 13:58:39 -0500 Received: from mail-pg1-f196.google.com ([209.85.215.196]:36821 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725778AbfBPS6j (ORCPT ); Sat, 16 Feb 2019 13:58:39 -0500 Received: by mail-pg1-f196.google.com with SMTP id r124so6397281pgr.3 for ; Sat, 16 Feb 2019 10:58:38 -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:mime-version :content-transfer-encoding; bh=MmezaLd6/OtknimdXgwRGLDotJEmFWte9ycbmeeJEVU=; b=rHOU6SHqsSsiS3yFuwSIOBiGumscD8MMRGfkk1vvhj4fAKgHpBgf2Xw3/+fHpxOWbA KOE8tC5Yp53iKQmrkibvKtzXEtKatkSuRmR3x5AWrixX6kH0CahoD0CoW1owC+OO4HFM JH7st50VJEmCXH+BBXz4/tmZUSd+nJb0XsAPsMQkVmtxEz25C9NL055ODDvwahd9iL+v w6m0wfEES62GZTWMtFxDyWm+2umJKpkJ/RCtlVwuTMp4z6pVECbTBRTyFbaHVVfDq0Bq G7AK3eccB8P/3cKxruN1gN+UMLv6GfRSi6YSG6P/yswD0V44Zt9FhH/4BGxIArWSaOk/ qbCA== 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:mime-version :content-transfer-encoding; bh=MmezaLd6/OtknimdXgwRGLDotJEmFWte9ycbmeeJEVU=; b=cQ8dJZJCyVhn14SeydwFMqZ4DGMGrMe6phDxj/Vmb2N09XrBgleGuKp0bEhU42ypRm DLcrMgzGgeSfgYVCYFlWRooNNQSQPy+KFYYFXra8leRNWafgLBwRtPN8/dD6/WGVvV6I 44dm/7Myeaa7SLwdmxAvlvQj/caaUOTKBkWVkONujjRJS2qKcDqdxSIsek3QgrGe5RA0 u8TDo8ELpUvz0XOWgOkLFmCkHqQJ0Bl5vbJbC5bwdu6XZLNvTTGApdhDqOyAsmjohtdL iR+QJJsjv4eR3RqqjIvYz6QEhyQopBfX2sE5KC9B0iJNq8ZNWZDwe/L1WdtXXWQ217xP 6McQ== X-Gm-Message-State: AHQUAuaiV2/teCcJ9alaOB35YXcOOGi1cr44HvqWLRRWK0iQAF/Dweil aUdxBGE07zZdE4NjXS98e6GQV97C X-Google-Smtp-Source: AHgI3IYFsg9NCbyMmdnp57W6ls65ZUeGMmm69rAaUu66YtrP6aqaYSgS27ojj9EWOSlucvCvj2W3Cw== X-Received: by 2002:a62:5003:: with SMTP id e3mr16393135pfb.23.1550343517717; Sat, 16 Feb 2019 10:58:37 -0800 (PST) Received: from tw-172-25-17-123.office.twttr.net ([8.25.197.27]) by smtp.gmail.com with ESMTPSA id f20sm10725703pfn.177.2019.02.16.10.58.36 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 16 Feb 2019 10:58:36 -0800 (PST) From: Cong Wang To: netdev@vger.kernel.org Cc: Cong Wang , Adrian , Ben Hutchings , Jamal Hadi Salim , Jiri Pirko Subject: [Patch net-next 1/2] net_sched: fix a race condition in tcindex_destroy() Date: Sat, 16 Feb 2019 10:58:26 -0800 Message-Id: <20190216185827.21535-1-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org (cherry picked from 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 --- 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 e1981628047b..81a433ae31b3 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, false, 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); @@ -480,7 +484,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: @@ -572,7 +576,7 @@ static void tcindex_destroy(struct tcf_proto *tp, bool rtnl_held, walker.fn = tcindex_destroy_element; tcindex_walk(tp, &walker, true); - call_rcu(&p->rcu, __tcindex_destroy); + tcf_queue_work(&p->rwork, tcindex_destroy_work); }