Skip to content

Commit 18cdb37

Browse files
jrfastabdavem330
authored andcommitted
net: sched: do not use tcf_proto 'tp' argument from call_rcu
Using the tcf_proto pointer 'tp' from inside the classifiers callback is not valid because it may have been cleaned up by another call_rcu occuring on another CPU. 'tp' is currently being used by tcf_unbind_filter() in this patch we move instances of tcf_unbind_filter outside of the call_rcu() context. This is safe to do because any running schedulers will either read the valid class field or it will be zeroed. And all schedulers today when the class is 0 do a lookup using the same call used by the tcf_exts_bind(). So even if we have a running classifier hit the null class pointer it will do a lookup and get to the same result. This is particularly fragile at the moment because the only way to verify this is to audit the schedulers call sites. Reported-by: Cong Wang <[email protected]> Signed-off-by: John Fastabend <[email protected]> Acked-by: Cong Wang <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 13990f8 commit 18cdb37

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

net/sched/cls_basic.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ static int basic_init(struct tcf_proto *tp)
9191
static void basic_delete_filter(struct rcu_head *head)
9292
{
9393
struct basic_filter *f = container_of(head, struct basic_filter, rcu);
94-
struct tcf_proto *tp = f->tp;
9594

96-
tcf_unbind_filter(tp, &f->res);
9795
tcf_exts_destroy(&f->exts);
9896
tcf_em_tree_destroy(&f->ematches);
9997
kfree(f);
@@ -106,6 +104,7 @@ static void basic_destroy(struct tcf_proto *tp)
106104

107105
list_for_each_entry_safe(f, n, &head->flist, link) {
108106
list_del_rcu(&f->link);
107+
tcf_unbind_filter(tp, &f->res);
109108
call_rcu(&f->rcu, basic_delete_filter);
110109
}
111110
RCU_INIT_POINTER(tp->root, NULL);
@@ -120,6 +119,7 @@ static int basic_delete(struct tcf_proto *tp, unsigned long arg)
120119
list_for_each_entry(t, &head->flist, link)
121120
if (t == f) {
122121
list_del_rcu(&t->link);
122+
tcf_unbind_filter(tp, &t->res);
123123
call_rcu(&t->rcu, basic_delete_filter);
124124
return 0;
125125
}
@@ -222,6 +222,7 @@ static int basic_change(struct net *net, struct sk_buff *in_skb,
222222

223223
if (fold) {
224224
list_replace_rcu(&fold->link, &fnew->link);
225+
tcf_unbind_filter(tp, &fold->res);
225226
call_rcu(&fold->rcu, basic_delete_filter);
226227
} else {
227228
list_add_rcu(&fnew->link, &head->flist);

net/sched/cls_bpf.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ static int cls_bpf_init(struct tcf_proto *tp)
9292

9393
static void cls_bpf_delete_prog(struct tcf_proto *tp, struct cls_bpf_prog *prog)
9494
{
95-
tcf_unbind_filter(tp, &prog->res);
9695
tcf_exts_destroy(&prog->exts);
9796

9897
bpf_prog_destroy(prog->filter);
@@ -116,6 +115,7 @@ static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg)
116115
list_for_each_entry(prog, &head->plist, link) {
117116
if (prog == todel) {
118117
list_del_rcu(&prog->link);
118+
tcf_unbind_filter(tp, &prog->res);
119119
call_rcu(&prog->rcu, __cls_bpf_delete_prog);
120120
return 0;
121121
}
@@ -131,6 +131,7 @@ static void cls_bpf_destroy(struct tcf_proto *tp)
131131

132132
list_for_each_entry_safe(prog, tmp, &head->plist, link) {
133133
list_del_rcu(&prog->link);
134+
tcf_unbind_filter(tp, &prog->res);
134135
call_rcu(&prog->rcu, __cls_bpf_delete_prog);
135136
}
136137

@@ -282,6 +283,7 @@ static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
282283

283284
if (oldprog) {
284285
list_replace_rcu(&prog->link, &oldprog->link);
286+
tcf_unbind_filter(tp, &oldprog->res);
285287
call_rcu(&oldprog->rcu, __cls_bpf_delete_prog);
286288
} else {
287289
list_add_rcu(&prog->link, &head->plist);

net/sched/cls_fw.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ static int fw_init(struct tcf_proto *tp)
123123
static void fw_delete_filter(struct rcu_head *head)
124124
{
125125
struct fw_filter *f = container_of(head, struct fw_filter, rcu);
126-
struct tcf_proto *tp = f->tp;
127126

128-
tcf_unbind_filter(tp, &f->res);
129127
tcf_exts_destroy(&f->exts);
130128
kfree(f);
131129
}
@@ -143,6 +141,7 @@ static void fw_destroy(struct tcf_proto *tp)
143141
while ((f = rtnl_dereference(head->ht[h])) != NULL) {
144142
RCU_INIT_POINTER(head->ht[h],
145143
rtnl_dereference(f->next));
144+
tcf_unbind_filter(tp, &f->res);
146145
call_rcu(&f->rcu, fw_delete_filter);
147146
}
148147
}
@@ -166,6 +165,7 @@ static int fw_delete(struct tcf_proto *tp, unsigned long arg)
166165
fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
167166
if (pfp == f) {
168167
RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
168+
tcf_unbind_filter(tp, &f->res);
169169
call_rcu(&f->rcu, fw_delete_filter);
170170
return 0;
171171
}
@@ -280,6 +280,7 @@ static int fw_change(struct net *net, struct sk_buff *in_skb,
280280

281281
RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next));
282282
rcu_assign_pointer(*fp, fnew);
283+
tcf_unbind_filter(tp, &f->res);
283284
call_rcu(&f->rcu, fw_delete_filter);
284285

285286
*arg = (unsigned long)fnew;

net/sched/cls_route.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,7 @@ static void
269269
route4_delete_filter(struct rcu_head *head)
270270
{
271271
struct route4_filter *f = container_of(head, struct route4_filter, rcu);
272-
struct tcf_proto *tp = f->tp;
273272

274-
tcf_unbind_filter(tp, &f->res);
275273
tcf_exts_destroy(&f->exts);
276274
kfree(f);
277275
}
@@ -297,6 +295,7 @@ static void route4_destroy(struct tcf_proto *tp)
297295

298296
next = rtnl_dereference(f->next);
299297
RCU_INIT_POINTER(b->ht[h2], next);
298+
tcf_unbind_filter(tp, &f->res);
300299
call_rcu(&f->rcu, route4_delete_filter);
301300
}
302301
}
@@ -338,6 +337,7 @@ static int route4_delete(struct tcf_proto *tp, unsigned long arg)
338337
route4_reset_fastmap(head);
339338

340339
/* Delete it */
340+
tcf_unbind_filter(tp, &f->res);
341341
call_rcu(&f->rcu, route4_delete_filter);
342342

343343
/* Strip RTNL protected tree */
@@ -545,8 +545,10 @@ static int route4_change(struct net *net, struct sk_buff *in_skb,
545545

546546
route4_reset_fastmap(head);
547547
*arg = (unsigned long)f;
548-
if (fold)
548+
if (fold) {
549+
tcf_unbind_filter(tp, &fold->res);
549550
call_rcu(&fold->rcu, route4_delete_filter);
551+
}
550552
return 0;
551553

552554
errout:

0 commit comments

Comments
 (0)