Skip to content

Commit dbf47a2

Browse files
w1ldptrdavem330
authored andcommitted
net: sched: act_sample: fix psample group handling on overwrite
Action sample doesn't properly handle psample_group pointer in overwrite case. Following issues need to be fixed: - In tcf_sample_init() function RCU_INIT_POINTER() is used to set s->psample_group, even though we neither setting the pointer to NULL, nor preventing concurrent readers from accessing the pointer in some way. Use rcu_swap_protected() instead to safely reset the pointer. - Old value of s->psample_group is not released or deallocated in any way, which results resource leak. Use psample_group_put() on non-NULL value obtained with rcu_swap_protected(). - The function psample_group_put() that released reference to struct psample_group pointed by rcu-pointer s->psample_group doesn't respect rcu grace period when deallocating it. Extend struct psample_group with rcu head and use kfree_rcu when freeing it. Fixes: 5c5670f ("net/sched: Introduce sample tc action") Signed-off-by: Vlad Buslov <vladbu@mellanox.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 36f1031 commit dbf47a2

3 files changed

Lines changed: 7 additions & 2 deletions

File tree

‎include/net/psample.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct psample_group {
1111
u32 group_num;
1212
u32 refcount;
1313
u32 seq;
14+
struct rcu_head rcu;
1415
};
1516

1617
struct psample_group *psample_group_get(struct net *net, u32 group_num);

‎net/psample/psample.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static void psample_group_destroy(struct psample_group *group)
154154
{
155155
psample_group_notify(group, PSAMPLE_CMD_DEL_GROUP);
156156
list_del(&group->list);
157-
kfree(group);
157+
kfree_rcu(group, rcu);
158158
}
159159

160160
static struct psample_group *

‎net/sched/act_sample.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,17 @@ static int tcf_sample_init(struct net *net, struct nlattr *nla,
102102
goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
103103
s->rate = rate;
104104
s->psample_group_num = psample_group_num;
105-
RCU_INIT_POINTER(s->psample_group, psample_group);
105+
rcu_swap_protected(s->psample_group, psample_group,
106+
lockdep_is_held(&s->tcf_lock));
106107

107108
if (tb[TCA_SAMPLE_TRUNC_SIZE]) {
108109
s->truncate = true;
109110
s->trunc_size = nla_get_u32(tb[TCA_SAMPLE_TRUNC_SIZE]);
110111
}
111112
spin_unlock_bh(&s->tcf_lock);
113+
114+
if (psample_group)
115+
psample_group_put(psample_group);
112116
if (goto_ch)
113117
tcf_chain_put_by_act(goto_ch);
114118

0 commit comments

Comments
 (0)