Skip to content

Commit 87b60cf

Browse files
Eric Dumazetdavem330
Eric Dumazet
authored andcommitted
net_sched: fix error recovery at qdisc creation
Dmitry reported uses after free in qdisc code [1] The problem here is that ops->init() can return an error. qdisc_create_dflt() then call ops->destroy(), while qdisc_create() does _not_ call it. Four qdisc chose to call their own ops->destroy(), assuming their caller would not. This patch makes sure qdisc_create() calls ops->destroy() and fixes the four qdisc to avoid double free. [1] BUG: KASAN: use-after-free in mq_destroy+0x242/0x290 net/sched/sch_mq.c:33 at addr ffff8801d415d440 Read of size 8 by task syz-executor2/5030 CPU: 0 PID: 5030 Comm: syz-executor2 Not tainted 4.3.5-smp-DEV hardkernel#119 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 0000000000000046 ffff8801b435b870 ffffffff81bbbed4 ffff8801db000400 ffff8801d415d440 ffff8801d415dc40 ffff8801c4988510 ffff8801b435b898 ffffffff816682b1 ffff8801b435b928 ffff8801d415d440 ffff8801c49880c0 Call Trace: [<ffffffff81bbbed4>] __dump_stack lib/dump_stack.c:15 [inline] [<ffffffff81bbbed4>] dump_stack+0x6c/0x98 lib/dump_stack.c:51 [<ffffffff816682b1>] kasan_object_err+0x21/0x70 mm/kasan/report.c:158 [<ffffffff81668524>] print_address_description mm/kasan/report.c:196 [inline] [<ffffffff81668524>] kasan_report_error+0x1b4/0x4b0 mm/kasan/report.c:285 [<ffffffff81668953>] kasan_report mm/kasan/report.c:305 [inline] [<ffffffff81668953>] __asan_report_load8_noabort+0x43/0x50 mm/kasan/report.c:326 [<ffffffff82527b02>] mq_destroy+0x242/0x290 net/sched/sch_mq.c:33 [<ffffffff82524bdd>] qdisc_destroy+0x12d/0x290 net/sched/sch_generic.c:953 [<ffffffff82524e30>] qdisc_create_dflt+0xf0/0x120 net/sched/sch_generic.c:848 [<ffffffff8252550d>] attach_default_qdiscs net/sched/sch_generic.c:1029 [inline] [<ffffffff8252550d>] dev_activate+0x6ad/0x880 net/sched/sch_generic.c:1064 [<ffffffff824b1db1>] __dev_open+0x221/0x320 net/core/dev.c:1403 [<ffffffff824b24ce>] __dev_change_flags+0x15e/0x3e0 net/core/dev.c:6858 [<ffffffff824b27de>] dev_change_flags+0x8e/0x140 net/core/dev.c:6926 [<ffffffff824f5bf6>] dev_ifsioc+0x446/0x890 net/core/dev_ioctl.c:260 [<ffffffff824f61fa>] dev_ioctl+0x1ba/0xb80 net/core/dev_ioctl.c:546 [<ffffffff82430509>] sock_do_ioctl+0x99/0xb0 net/socket.c:879 [<ffffffff82430d30>] sock_ioctl+0x2a0/0x390 net/socket.c:958 [<ffffffff816f3b68>] vfs_ioctl fs/ioctl.c:44 [inline] [<ffffffff816f3b68>] do_vfs_ioctl+0x8a8/0xe50 fs/ioctl.c:611 [<ffffffff816f41a4>] SYSC_ioctl fs/ioctl.c:626 [inline] [<ffffffff816f41a4>] SyS_ioctl+0x94/0xc0 fs/ioctl.c:617 [<ffffffff8123e357>] entry_SYSCALL_64_fastpath+0x12/0x17 Signed-off-by: Eric Dumazet <edumazet@google.com> Reported-by: Dmitry Vyukov <dvyukov@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 1bf9605 commit 87b60cf

File tree

5 files changed

+19
-23
lines changed

5 files changed

+19
-23
lines changed

Diff for: net/sched/sch_api.c

+2
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,8 @@ static struct Qdisc *qdisc_create(struct net_device *dev,
10191019

10201020
return sch;
10211021
}
1022+
/* ops->init() failed, we call ->destroy() like qdisc_create_dflt() */
1023+
ops->destroy(sch);
10221024
err_out3:
10231025
dev_put(dev);
10241026
kfree((char *) sch - sch->padded);

Diff for: net/sched/sch_hhf.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,9 @@ static int hhf_init(struct Qdisc *sch, struct nlattr *opt)
627627
q->hhf_arrays[i] = hhf_zalloc(HHF_ARRAYS_LEN *
628628
sizeof(u32));
629629
if (!q->hhf_arrays[i]) {
630-
hhf_destroy(sch);
630+
/* Note: hhf_destroy() will be called
631+
* by our caller.
632+
*/
631633
return -ENOMEM;
632634
}
633635
}
@@ -638,7 +640,9 @@ static int hhf_init(struct Qdisc *sch, struct nlattr *opt)
638640
q->hhf_valid_bits[i] = hhf_zalloc(HHF_ARRAYS_LEN /
639641
BITS_PER_BYTE);
640642
if (!q->hhf_valid_bits[i]) {
641-
hhf_destroy(sch);
643+
/* Note: hhf_destroy() will be called
644+
* by our caller.
645+
*/
642646
return -ENOMEM;
643647
}
644648
}

Diff for: net/sched/sch_mq.c

+3-7
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,22 @@ static int mq_init(struct Qdisc *sch, struct nlattr *opt)
5252
/* pre-allocate qdiscs, attachment can't fail */
5353
priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
5454
GFP_KERNEL);
55-
if (priv->qdiscs == NULL)
55+
if (!priv->qdiscs)
5656
return -ENOMEM;
5757

5858
for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
5959
dev_queue = netdev_get_tx_queue(dev, ntx);
6060
qdisc = qdisc_create_dflt(dev_queue, get_default_qdisc_ops(dev, ntx),
6161
TC_H_MAKE(TC_H_MAJ(sch->handle),
6262
TC_H_MIN(ntx + 1)));
63-
if (qdisc == NULL)
64-
goto err;
63+
if (!qdisc)
64+
return -ENOMEM;
6565
priv->qdiscs[ntx] = qdisc;
6666
qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
6767
}
6868

6969
sch->flags |= TCQ_F_MQROOT;
7070
return 0;
71-
72-
err:
73-
mq_destroy(sch);
74-
return -ENOMEM;
7571
}
7672

7773
static void mq_attach(struct Qdisc *sch)

Diff for: net/sched/sch_mqprio.c

+6-13
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,18 @@ static int mqprio_init(struct Qdisc *sch, struct nlattr *opt)
118118
/* pre-allocate qdisc, attachment can't fail */
119119
priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
120120
GFP_KERNEL);
121-
if (priv->qdiscs == NULL) {
122-
err = -ENOMEM;
123-
goto err;
124-
}
121+
if (!priv->qdiscs)
122+
return -ENOMEM;
125123

126124
for (i = 0; i < dev->num_tx_queues; i++) {
127125
dev_queue = netdev_get_tx_queue(dev, i);
128126
qdisc = qdisc_create_dflt(dev_queue,
129127
get_default_qdisc_ops(dev, i),
130128
TC_H_MAKE(TC_H_MAJ(sch->handle),
131129
TC_H_MIN(i + 1)));
132-
if (qdisc == NULL) {
133-
err = -ENOMEM;
134-
goto err;
135-
}
130+
if (!qdisc)
131+
return -ENOMEM;
132+
136133
priv->qdiscs[i] = qdisc;
137134
qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
138135
}
@@ -148,7 +145,7 @@ static int mqprio_init(struct Qdisc *sch, struct nlattr *opt)
148145
priv->hw_owned = 1;
149146
err = dev->netdev_ops->ndo_setup_tc(dev, sch->handle, 0, &tc);
150147
if (err)
151-
goto err;
148+
return err;
152149
} else {
153150
netdev_set_num_tc(dev, qopt->num_tc);
154151
for (i = 0; i < qopt->num_tc; i++)
@@ -162,10 +159,6 @@ static int mqprio_init(struct Qdisc *sch, struct nlattr *opt)
162159

163160
sch->flags |= TCQ_F_MQROOT;
164161
return 0;
165-
166-
err:
167-
mqprio_destroy(sch);
168-
return err;
169162
}
170163

171164
static void mqprio_attach(struct Qdisc *sch)

Diff for: net/sched/sch_sfq.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -743,9 +743,10 @@ static int sfq_init(struct Qdisc *sch, struct nlattr *opt)
743743
q->ht = sfq_alloc(sizeof(q->ht[0]) * q->divisor);
744744
q->slots = sfq_alloc(sizeof(q->slots[0]) * q->maxflows);
745745
if (!q->ht || !q->slots) {
746-
sfq_destroy(sch);
746+
/* Note: sfq_destroy() will be called by our caller */
747747
return -ENOMEM;
748748
}
749+
749750
for (i = 0; i < q->divisor; i++)
750751
q->ht[i] = SFQ_EMPTY_SLOT;
751752

0 commit comments

Comments
 (0)