Skip to content

Commit 726d061

Browse files
w1ldptrdavem330
authored andcommitted
net: sched: prevent insertion of new classifiers during chain flush
Extend tcf_chain with 'flushing' flag. Use the flag to prevent insertion of new classifier instances when chain flushing is in progress in order to prevent resource leak when tcf_proto is created by unlocked users concurrently. Return EAGAIN error from tcf_chain_tp_insert_unique() to restart tc_new_tfilter() and lookup the chain/proto again. Signed-off-by: Vlad Buslov <vladbu@mellanox.com> Acked-by: Jiri Pirko <jiri@mellanox.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 8b64678 commit 726d061

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

‎include/net/sch_generic.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ struct tcf_chain {
356356
unsigned int refcnt;
357357
unsigned int action_refcnt;
358358
bool explicitly_created;
359+
bool flushing;
359360
const struct tcf_proto_ops *tmplt_ops;
360361
void *tmplt_priv;
361362
};

‎net/sched/cls_api.c‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,12 @@ static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
477477
mutex_unlock(&block->lock);
478478

479479
/* The last dropped non-action reference will trigger notification. */
480-
if (is_last && !by_act)
480+
if (is_last && !by_act) {
481481
tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain_index,
482482
block, NULL, 0, 0, false);
483+
/* Last reference to chain, no need to lock. */
484+
chain->flushing = false;
485+
}
483486

484487
if (refcnt == 0) {
485488
tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
@@ -511,6 +514,7 @@ static void tcf_chain_flush(struct tcf_chain *chain)
511514
tp = tcf_chain_dereference(chain->filter_chain, chain);
512515
RCU_INIT_POINTER(chain->filter_chain, NULL);
513516
tcf_chain0_head_change(chain, NULL);
517+
chain->flushing = true;
514518
mutex_unlock(&chain->filter_chain_lock);
515519

516520
while (tp) {
@@ -1610,15 +1614,20 @@ static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
16101614
return tcf_chain_dereference(*chain_info->pprev, chain);
16111615
}
16121616

1613-
static void tcf_chain_tp_insert(struct tcf_chain *chain,
1614-
struct tcf_chain_info *chain_info,
1615-
struct tcf_proto *tp)
1617+
static int tcf_chain_tp_insert(struct tcf_chain *chain,
1618+
struct tcf_chain_info *chain_info,
1619+
struct tcf_proto *tp)
16161620
{
1621+
if (chain->flushing)
1622+
return -EAGAIN;
1623+
16171624
if (*chain_info->pprev == chain->filter_chain)
16181625
tcf_chain0_head_change(chain, tp);
16191626
tcf_proto_get(tp);
16201627
RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
16211628
rcu_assign_pointer(*chain_info->pprev, tp);
1629+
1630+
return 0;
16221631
}
16231632

16241633
static void tcf_chain_tp_remove(struct tcf_chain *chain,
@@ -1649,18 +1658,22 @@ static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
16491658
{
16501659
struct tcf_chain_info chain_info;
16511660
struct tcf_proto *tp;
1661+
int err = 0;
16521662

16531663
mutex_lock(&chain->filter_chain_lock);
16541664

16551665
tp = tcf_chain_tp_find(chain, &chain_info,
16561666
protocol, prio, false);
16571667
if (!tp)
1658-
tcf_chain_tp_insert(chain, &chain_info, tp_new);
1668+
err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
16591669
mutex_unlock(&chain->filter_chain_lock);
16601670

16611671
if (tp) {
16621672
tcf_proto_destroy(tp_new, NULL);
16631673
tp_new = tp;
1674+
} else if (err) {
1675+
tcf_proto_destroy(tp_new, NULL);
1676+
tp_new = ERR_PTR(err);
16641677
}
16651678

16661679
return tp_new;
@@ -1943,6 +1956,11 @@ static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
19431956
if (tp == NULL) {
19441957
struct tcf_proto *tp_new = NULL;
19451958

1959+
if (chain->flushing) {
1960+
err = -EAGAIN;
1961+
goto errout_locked;
1962+
}
1963+
19461964
/* Proto-tcf does not exist, create new one */
19471965

19481966
if (tca[TCA_KIND] == NULL || !protocol) {
@@ -1966,11 +1984,15 @@ static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
19661984
protocol, prio, chain, extack);
19671985
if (IS_ERR(tp_new)) {
19681986
err = PTR_ERR(tp_new);
1969-
goto errout;
1987+
goto errout_tp;
19701988
}
19711989

19721990
tp_created = 1;
19731991
tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio);
1992+
if (IS_ERR(tp)) {
1993+
err = PTR_ERR(tp);
1994+
goto errout_tp;
1995+
}
19741996
} else {
19751997
mutex_unlock(&chain->filter_chain_lock);
19761998
}
@@ -2011,6 +2033,7 @@ static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
20112033
errout:
20122034
if (err && tp_created)
20132035
tcf_chain_tp_delete_empty(chain, tp, NULL);
2036+
errout_tp:
20142037
if (chain) {
20152038
if (tp && !IS_ERR(tp))
20162039
tcf_proto_put(tp, NULL);

0 commit comments

Comments
 (0)