sch_sfb: fix a crash in sfb_destroy()
Subsystems (3)
PATCH 1/1 sch_sfb: fix a crash in sfb_destroy()
When tcf_block_get() fails in sfb_init(), q->qdisc is still a NULL pointer which leads to a crash in sfb_destroy().
Linus suggested three solutions for this problem, the simplest fix is just moving the noop_qdisc assignment before tcf_block_get() so that qdisc_put() would become a nop.
--- net/sched/sch_sfb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/sched/sch_sfb.c +2 -2
@@ -552,11 +552,11 @@ static int sfb_init(struct Qdisc *sch, struct nlattr *opt,struct sfb_sched_data *q =qdisc_priv (sch);int err; + q->qdisc = &noop_qdisc; + err =tcf_block_get (&q->block, &q->filter_list, sch, extack);if (err)return err; - - q->qdisc = &noop_qdisc;return sfb_change (sch, opt, extack); }
Signature · 1 line
2.21.0
Quoted 26 lines · Cong Wang
When
tcf_block_get()fails insfb_init(),q->qdiscis still a NULL pointer which leads to a crash insfb_destroy().Linus suggested three solutions for this problem, the simplest fix is just moving the
noop_qdiscassignment beforetcf_block_get()so thatqdisc_put()would become a nop.Fixes 6529eaba33f0 ("net: sched: introduce tcf block infractructure")Reported-by [email protected]Suggested-by Linus Torvalds <[email protected]>Cc Jamal Hadi Salim <[email protected]>Cc Jiri Pirko <[email protected]>Signed-off-by Cong Wang <[email protected]>--- net/sched/sch_sfb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)net/sched/sch_sfb.c +0
-0@@ -552,11 +552,11 @@ static int sfb_init(struct Qdisc *sch, struct nlattr *opt,struct sfb_sched_data *q =qdisc_priv (sch);int err;
net/sched/sch_sfb.c static int sfb_init(struct Qdisc *sch, struct nlattr *opt, · line 562
Quoted 10 lines
net/sched/sch_sfb.c +2
-2+ q->qdisc = &noop_qdisc; + err =tcf_block_get (&q->block, &q->filter_list, sch, extack);if (err)return err; - - q->qdisc = &noop_qdisc;return sfb_change (sch, opt, extack); }
It seems a similar fix would be needed in net/sched/sch_dsmark.c ?
Quoted 3 lines · Eric Dumazet
It seems a similar fix would be needed in
net/sched/sch_dsmark.c?
Yeah, or just add a NULL check in dsmark_destroy().
Anyway, I will send a separate patch for it.
Thanks.
Quoted 7 lines · Cong Wang
Quoted 3 lines · Eric Dumazet
It seems a similar fix would be needed in
net/sched/sch_dsmark.c?Yeah, or just add a NULL check in
dsmark_destroy().
Well, this was why one of my suggestions was to just make "qdisc_put()" be happy with a NULL pointer (or even an ERR_PTR()).
That would have fixed not just sfb, but also dsmark with a single patch.
We tend to have that kind of pattern in a lot of places, where we can free unallocated structures (end ERR_PTR() pointers) withour errors, so that
destroy_fn(alloc_fn());
is defined to always work, even when alloc_fn() returns NULL or an error. That, and allowing the "it was never allocated at all" case (as long as it's initialized to NULL, of course) tends to make various error cases simpler.
The obvious one is kfree(kmalloc()), of course, but we've done it in other places too. So you find things like
void i2c_unregister_device(struct i2c_client *client)
{
if (IS_ERR_OR_NULL(client))
return;in various subsystems and drivers. So one of my suggestions was to just do that to qdisc_put().
It depends on what you want to do, of course. Do you want to make sure each user is being very careful? Or do you want to make the interfaces easy to use without _having_ to be careful? There are arguments both ways, but we've tended to move more towards a "easy to use" model than the "be careful" one.
Linus
David Miller
maintainerFrom: Linus Torvalds <[email protected]>
Date: Thu, 12 Sep 2019 11:31:06 +0100
Quoted 5 lines
It depends on what you want to do, of course. Do you want to make sure each user is being very careful? Or do you want to make the interfaces easy to use without _having_ to be careful? There are arguments both ways, but we've tended to move more towards a "easy to use" model than the "be careful" one.
Yes, I think allowing NULL or error pointers on free/destroy makes sense.
Quoted 8 lines · Linus Torvalds
Quoted 6 lines · Cong Wang
Quoted 3 lines · Eric Dumazet
It seems a similar fix would be needed in
net/sched/sch_dsmark.c?
Quoted 6 lines
Quoted 1 line
Yeah, or just add a NULL check in
dsmark_destroy().Well, this was why one of my suggestions was to just make "
qdisc_put()" be happy with a NULL pointer (or even anERR_PTR()).That would have fixed not just sfb, but also dsmark with a single patch.
Sure, I don't have any preference here, just want to find a minimum fix for -stable.
I will send v2.
Thanks.