Skip to contentSkip to conversation

sch_sfb: fix a crash in sfb_destroy()

PATCH In review Cong Wang 4 participants 6 messages Sep 11, 2019 – Sep 12, 2019

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

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.

Fixes 6529eaba33f0 ("net: sched: introduce tcf block infractructure")
Reported-by [email protected]
Cc Jamal Hadi Salim <[email protected]>
Cc Jiri Pirko <[email protected]>
---
 net/sched/sch_sfb.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/sched/sch_sfb.c +2 -2
index 1dff8506a715..db1c8eb521a2 100644
@@ -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
#1 lore raw
Quoted 26 lines · Cong Wang

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.

Fixes 6529eaba33f0 ("net: sched: introduce tcf block infractructure")
Reported-by [email protected]
Cc Jamal Hadi Salim <[email protected]>
Cc Jiri Pirko <[email protected]>
---
 net/sched/sch_sfb.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/sched/sch_sfb.c +0 -0
index 1dff8506a715..db1c8eb521a2 100644
@@ -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 ?

author
#2 lore raw
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.

#3 lore raw
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

maintainer
#4 lore raw

From: 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.

author
#5 lore raw
In reply to Linus Torvalds
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 an ERR_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.