Skip to contentSkip to conversation

net_sched: properly check for empty skb array on error path

PATCH Applied Cong Wang 3 participants 7 messages Dec 18, 2017 – Dec 19, 2017

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 net_sched: properly check for empty skb array on error path

First, the check of &q->ring.queue against NULL is wrong, it is always false. We should check the value rather than the address.

Secondly, we need the same check in pfifo_fast_reset() too, as both ->reset() and ->destroy() are called in qdisc_destroy().

Fixes c5ad119fb6c0 ("net: sched: pfifo_fast use skb_array")
Cc John Fastabend <[email protected]>
---
 net/sched/sch_generic.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
net/sched/sch_generic.c +7 -1
index 981c08fe810b..876fab2604b8 100644
@@ -659,6 +659,12 @@ static void pfifo_fast_reset(struct Qdisc *qdisc)
 		struct skb_array *q = band2list(priv, band);
 		struct sk_buff *skb;
 
+		/* NULL ring is possible if destroy path is due to a failed
+		 * skb_array_init() in pfifo_fast_init() case.
+		 */
+		if (!q->ring.queue)
+			continue;
+
 		while ((skb = skb_array_consume_bh(q)) != NULL)
 			kfree_skb(skb);
 	}
@@ -719,7 +725,7 @@ static void pfifo_fast_destroy(struct Qdisc *sch)
 		/* NULL ring is possible if destroy path is due to a failed
 		 * skb_array_init() in pfifo_fast_init() case.
 		 */
-		if (!&q->ring.queue)
+		if (!q->ring.queue)
 			continue;
 		/* Destroy ring but no need to kfree_skb because a call to
 		 * pfifo_fast_reset() has already done that work.
Signature · 1 line
2.13.0
#1 lore raw
Quoted 3 lines · Cong Wang

First, the check of &q->ring.queue against NULL is wrong, it is always false. We should check the value rather than the address.

Thanks.

Quoted 3 lines

Secondly, we need the same check in pfifo_fast_reset() too, as both ->reset() and ->destroy() are called in qdisc_destroy().

not that it hurts to have the check here, but if init fails in qdisc_create it seems only ->destroy() is called without a ->reset().

Is there another path for init() to fail that I'm missing.

Quoted 8 lines
Fixes c5ad119fb6c0 ("net: sched: pfifo_fast use skb_array")
Cc John Fastabend <[email protected]>
---
 net/sched/sch_generic.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
author
#2 lore raw
Quoted 11 lines · John Fastabend
Quoted 3 lines · Cong Wang

First, the check of &q->ring.queue against NULL is wrong, it is always false. We should check the value rather than the address.

Thanks.

Quoted 3 lines

Secondly, we need the same check in pfifo_fast_reset() too, as both ->reset() and ->destroy() are called in qdisc_destroy().

Quoted 5 lines

not that it hurts to have the check here, but if init fails in qdisc_create it seems only ->destroy() is called without a ->reset().

Is there another path for init() to fail that I'm missing.

Pretty sure ->reset() is called in qdisc_destroy() and also before ->destroy():

void qdisc_destroy(struct Qdisc *qdisc)

{
        const struct Qdisc_ops  *ops = qdisc->ops;
        struct sk_buff *skb, *tmp;

        if (qdisc->flags & TCQ_F_BUILTIN ||
            !refcount_dec_and_test(&qdisc->refcnt))
                return;
#ifdef CONFIG_NET_SCHED
        qdisc_hash_del(qdisc);

        qdisc_put_stab(rtnl_dereference(qdisc->stab));
#endif
        gen_kill_estimator(&qdisc->rate_est);
        if (ops->reset)
                ops->reset(qdisc);
        if (ops->destroy)
                ops->destroy(qdisc);
#3 lore raw
Quoted 13 lines · Cong Wang
Quoted 11 lines · John Fastabend
Quoted 3 lines · Cong Wang

First, the check of &q->ring.queue against NULL is wrong, it is always false. We should check the value rather than the address.

Thanks.

Quoted 3 lines

Secondly, we need the same check in pfifo_fast_reset() too, as both ->reset() and ->destroy() are called in qdisc_destroy().

Quoted 9 lines
Quoted 5 lines

not that it hurts to have the check here, but if init fails in qdisc_create it seems only ->destroy() is called without a ->reset().

Is there another path for init() to fail that I'm missing.

Pretty sure ->reset() is called in qdisc_destroy() and also before ->destroy():

Except, the failed init path does not call qdisc_destroy.

static struct Qdisc *qdisc_create(struct net_device *dev, [...]

        if (ops->init) {
                err = ops->init(sch, tca[TCA_OPTIONS]);
                if (err != 0)
                        goto err_out5;
        }

[...]

err_out5:

        /* ops->init() failed, we call ->destroy() like qdisc_create_dflt() */
        if (ops->destroy)
                ops->destroy(sch);
err_out3:
        dev_put(dev);
        kfree((char *) sch - sch->padded);
err_out2:
        module_put(ops->owner);
err_out:
        *errp = err;
        return NULL;

[...]

Quoted 21 lines

void qdisc_destroy(struct Qdisc *qdisc)

{
        const struct Qdisc_ops  *ops = qdisc->ops;
        struct sk_buff *skb, *tmp;

        if (qdisc->flags & TCQ_F_BUILTIN ||
            !refcount_dec_and_test(&qdisc->refcnt))
                return;
#ifdef CONFIG_NET_SCHED
        qdisc_hash_del(qdisc);

        qdisc_put_stab(rtnl_dereference(qdisc->stab));
#endif
        gen_kill_estimator(&qdisc->rate_est);
        if (ops->reset)
                ops->reset(qdisc);
        if (ops->destroy)
                ops->destroy(qdisc);
author
#4 lore raw
Quoted 34 lines · John Fastabend
Quoted 13 lines · Cong Wang
Quoted 11 lines · John Fastabend
Quoted 3 lines · Cong Wang

First, the check of &q->ring.queue against NULL is wrong, it is always false. We should check the value rather than the address.

Thanks.

Quoted 3 lines

Secondly, we need the same check in pfifo_fast_reset() too, as both ->reset() and ->destroy() are called in qdisc_destroy().

Quoted 9 lines
Quoted 5 lines

not that it hurts to have the check here, but if init fails in qdisc_create it seems only ->destroy() is called without a ->reset().

Is there another path for init() to fail that I'm missing.

Pretty sure ->reset() is called in qdisc_destroy() and also before ->destroy():

Except, the failed init path does not call qdisc_destroy.

static struct Qdisc *qdisc_create(struct net_device *dev, [...]

        if (ops->init) {
                err = ops->init(sch, tca[TCA_OPTIONS]);
                if (err != 0)
                        goto err_out5;
        }
Quoted 6 lines

[...]

err_out5:

        /* ops->init() failed, we call ->destroy() like qdisc_create_dflt() */
        if (ops->destroy)
                ops->destroy(sch);

Didn't I say qdisc_destroy() rather than ->destroy()? :-)

struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
const struct Qdisc_ops *ops, unsigned int parentid)

{
        struct Qdisc *sch;

        if (!try_module_get(ops->owner))
                return NULL;

        sch = qdisc_alloc(dev_queue, ops);
        if (IS_ERR(sch)) {
                module_put(ops->owner);
                return NULL;
        }
        sch->parent = parentid;

        if (!ops->init || ops->init(sch, NULL) == 0)
                return sch;

        qdisc_destroy(sch);
        return NULL;
}
#5 lore raw
Quoted 38 lines · Cong Wang
Quoted 36 lines · John Fastabend
Quoted 13 lines · Cong Wang
Quoted 11 lines · John Fastabend
Quoted 3 lines · Cong Wang

First, the check of &q->ring.queue against NULL is wrong, it is always false. We should check the value rather than the address.

Thanks.

Quoted 3 lines

Secondly, we need the same check in pfifo_fast_reset() too, as both ->reset() and ->destroy() are called in qdisc_destroy().

Quoted 9 lines
Quoted 5 lines

not that it hurts to have the check here, but if init fails in qdisc_create it seems only ->destroy() is called without a ->reset().

Is there another path for init() to fail that I'm missing.

Pretty sure ->reset() is called in qdisc_destroy() and also before ->destroy():

Except, the failed init path does not call qdisc_destroy.

static struct Qdisc *qdisc_create(struct net_device *dev, [...]

        if (ops->init) {
                err = ops->init(sch, tca[TCA_OPTIONS]);
                if (err != 0)
                        goto err_out5;
        }

[...]

Quoted 7 lines
Quoted 4 lines

err_out5:

        /* ops->init() failed, we call ->destroy() like qdisc_create_dflt() */
        if (ops->destroy)
                ops->destroy(sch);

Didn't I say qdisc_destroy() rather than ->destroy()? :-)

Yep, thanks for the fix.

maintainer Applied
#6 lore raw
In reply to Cong Wang

From: Cong Wang <[email protected]>
Date: Mon, 18 Dec 2017 14:34:26 -0800

Quoted 10 lines

First, the check of &q->ring.queue against NULL is wrong, it is always false. We should check the value rather than the address.

Secondly, we need the same check in pfifo_fast_reset() too, as both ->reset() and ->destroy() are called in qdisc_destroy().

Fixes c5ad119fb6c0 ("net: sched: pfifo_fast use skb_array")
Cc John Fastabend <[email protected]>

Applied, thanks Cong.