Skip to contentSkip to conversation

net:sched fix array-index-out-of-bounds in taprio_change

PATCH Superseded [email protected] 4 participants 8 messages Aug 11, 2021 – Aug 30, 2021

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net:sched fix array-index-out-of-bounds in taprio_change

From: Haimin Zhang <[email protected]>

syzbot report an array-index-out-of-bounds in taprio_change index 16 is out of range for type '__u16 [16]'
that's because mqprio->num_tc is lager than TC_MAX_QUEUE,so we check the return value of netdev_set_num_tc.

---
 net/sched/sch_taprio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/sched/sch_taprio.c +3 -1
index 9c79374..1ab2fc9 100644
@@ -1513,7 +1513,9 @@ static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
 	taprio_set_picos_per_byte(dev, q);
 
 	if (mqprio) {
-		netdev_set_num_tc(dev, mqprio->num_tc);
+		err = netdev_set_num_tc(dev, mqprio->num_tc);
+		if (err)
+			goto free_sched;
 		for (i = 0; i < mqprio->num_tc; i++)
 			netdev_set_tc_queue(dev, i,
 					    mqprio->count[i],
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 19 lines · On 8/11/21 7:10 AM, [email protected] wrote:

From: Haimin Zhang <[email protected]>

syzbot report an array-index-out-of-bounds in taprio_change index 16 is out of range for type '__u16 [16]'
that's because mqprio->num_tc is lager than TC_MAX_QUEUE,so we check the return value of netdev_set_num_tc.

---
 net/sched/sch_taprio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/sched/sch_taprio.c +0 -0
index 9c79374..1ab2fc9 100644

net/sched/sch_taprio.c static int taprio_change(struct Qdisc *sch, struct nlattr *opt, · line 1521

Quoted 10 lines
net/sched/sch_taprio.c +3 -1
@@ -1513,7 +1513,9 @@ static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
 	taprio_set_picos_per_byte(dev, q);
 
 	if (mqprio) {
-		netdev_set_num_tc(dev, mqprio->num_tc);
+		err = netdev_set_num_tc(dev, mqprio->num_tc);
+		if (err)
+			goto free_sched;
 		for (i = 0; i < mqprio->num_tc; i++)
 			netdev_set_tc_queue(dev, i,
 					    mqprio->count[i],

When was the bug added ?

Hint: Please provide a Fixes: tag

taprio_parse_mqprio_opt() already checks :

/* Verify num_tc is not out of max range */

if (qopt->num_tc > TC_MAX_QUEUE) {
    NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
    return -EINVAL;
}

So what is happening exactly ?

#2 lore raw
Quoted 39 lines · Eric Dumazet
Quoted 19 lines · On 8/11/21 7:10 AM, [email protected] wrote:

From: Haimin Zhang <[email protected]>

syzbot report an array-index-out-of-bounds in taprio_change index 16 is out of range for type '__u16 [16]'
that's because mqprio->num_tc is lager than TC_MAX_QUEUE,so we check the return value of netdev_set_num_tc.

---
 net/sched/sch_taprio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/sched/sch_taprio.c +0 -0
index 9c79374..1ab2fc9 100644

net/sched/sch_taprio.c static int taprio_change(struct Qdisc *sch, struct nlattr *opt, · line 1521

Quoted 10 lines
net/sched/sch_taprio.c +3 -1
@@ -1513,7 +1513,9 @@ static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
 	taprio_set_picos_per_byte(dev, q);
 
 	if (mqprio) {
-		netdev_set_num_tc(dev, mqprio->num_tc);
+		err = netdev_set_num_tc(dev, mqprio->num_tc);
+		if (err)
+			goto free_sched;
 		for (i = 0; i < mqprio->num_tc; i++)
 			netdev_set_tc_queue(dev, i,
 					    mqprio->count[i],

When was the bug added ?

Hint: Please provide a Fixes: tag

taprio_parse_mqprio_opt() already checks :

Quoted 9 lines

/* Verify num_tc is not out of max range */

if (qopt->num_tc > TC_MAX_QUEUE) {
     NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
     return -EINVAL;
}

So what is happening exactly ?

Hi, Eric!

I've looked into this bug, but I decided to write a reproducer for it first. Unfortunately, I didn't finish it yesterday, but I have an idea about what happened:

taprio_parse_mqprio_opt() may return 0 before qopt->num_tc check:

	/* If num_tc is already set, it means that the user already
	 * configured the mqprio part
	 */
	if (dev->num_tc)
		return 0;

Then taprio_mqprio_cmp() fails here:

	if (!mqprio || mqprio->num_tc != dev->num_tc)
		return -1;

That's why we won't get shift-out-of-bound in taprio_mqprio_cmp().

And finally taprio_change() gets to buggy for with wrong mqprio->num_tc. I don't know how to reproduce it, but I'll try to finish my reproducer this evening.

Does above makes any sense?

With regards,
Pavel Skripkin

#3 lore raw
Quoted 64 lines · Pavel Skripkin
Quoted 39 lines · Eric Dumazet
Quoted 19 lines · On 8/11/21 7:10 AM, [email protected] wrote:

From: Haimin Zhang <[email protected]>

syzbot report an array-index-out-of-bounds in taprio_change index 16 is out of range for type '__u16 [16]'
that's because mqprio->num_tc is lager than TC_MAX_QUEUE,so we check the return value of netdev_set_num_tc.

---
 net/sched/sch_taprio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/sched/sch_taprio.c +0 -0
index 9c79374..1ab2fc9 100644

net/sched/sch_taprio.c static int taprio_change(struct Qdisc *sch, struct nlattr *opt, · line 1521

Quoted 10 lines
net/sched/sch_taprio.c +3 -1
@@ -1513,7 +1513,9 @@ static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
 	taprio_set_picos_per_byte(dev, q);
 
 	if (mqprio) {
-		netdev_set_num_tc(dev, mqprio->num_tc);
+		err = netdev_set_num_tc(dev, mqprio->num_tc);
+		if (err)
+			goto free_sched;
 		for (i = 0; i < mqprio->num_tc; i++)
 			netdev_set_tc_queue(dev, i,
 					    mqprio->count[i],

When was the bug added ?

Hint: Please provide a Fixes: tag

taprio_parse_mqprio_opt() already checks :

Quoted 9 lines

/* Verify num_tc is not out of max range */

if (qopt->num_tc > TC_MAX_QUEUE) {
     NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
     return -EINVAL;
}

So what is happening exactly ?

Hi, Eric!

I've looked into this bug, but I decided to write a reproducer for it first. Unfortunately, I didn't finish it yesterday, but I have an idea about what happened:

taprio_parse_mqprio_opt() may return 0 before qopt->num_tc check:

	/* If num_tc is already set, it means that the user already
	 * configured the mqprio part
	 */
	if (dev->num_tc)
		return 0;
Quoted 6 lines

Then taprio_mqprio_cmp() fails here:

	if (!mqprio || mqprio->num_tc != dev->num_tc)
		return -1;

That's why we won't get shift-out-of-bound in taprio_mqprio_cmp().

			  ^^^^^^^^^^^^^^^^^

			array-index-out-of-bounds

Sorry for confusion

With regards,
Pavel Skripkin

#4 lore raw
In reply to Eric Dumazet
Quoted 39 lines · Eric Dumazet
Quoted 19 lines · On 8/11/21 7:10 AM, [email protected] wrote:

From: Haimin Zhang <[email protected]>

syzbot report an array-index-out-of-bounds in taprio_change index 16 is out of range for type '__u16 [16]'
that's because mqprio->num_tc is lager than TC_MAX_QUEUE,so we check the return value of netdev_set_num_tc.

---
  net/sched/sch_taprio.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
net/sched/sch_taprio.c +0 -0
index 9c79374..1ab2fc9 100644

net/sched/sch_taprio.c static int taprio_change(struct Qdisc *sch, struct nlattr *opt, · line 1521

Quoted 10 lines
net/sched/sch_taprio.c +3 -1
@@ -1513,7 +1513,9 @@ static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
  	taprio_set_picos_per_byte(dev, q);
 
  	if (mqprio) {
-		netdev_set_num_tc(dev, mqprio->num_tc);
+		err = netdev_set_num_tc(dev, mqprio->num_tc);
+		if (err)
+			goto free_sched;
  		for (i = 0; i < mqprio->num_tc; i++)
  			netdev_set_tc_queue(dev, i,
  					    mqprio->count[i],

When was the bug added ?

Hint: Please provide a Fixes: tag

taprio_parse_mqprio_opt() already checks :

Quoted 11 lines

/* Verify num_tc is not out of max range */

if (qopt->num_tc > TC_MAX_QUEUE) {
     NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
     return -EINVAL;
}

So what is happening exactly ?

syzkaller reported this problem,the log shows mqprio->count[16] is accessed. here is the log
https://syzkaller.appspot.com/bug?id=3a3677d4e7539ec5e671a81e32882ad40b5f7b64

the added check logic is hurtlessness,and netdev_set_num_tc does have a return value.

#5 lore raw

hi,i wonder to know what‘s going on with this patch ? are you plannig to merge it ?

Quoted 59 lines · Haimin Zhang
Quoted 39 lines · Eric Dumazet
Quoted 19 lines · On 8/11/21 7:10 AM, [email protected] wrote:

From: Haimin Zhang <[email protected]>

syzbot report an array-index-out-of-bounds in taprio_change index 16 is out of range for type '__u16 [16]'
that's because mqprio->num_tc is lager than TC_MAX_QUEUE,so we check the return value of netdev_set_num_tc.

---
  net/sched/sch_taprio.c | 4 +++-

  1 file changed, 3 insertions(+), 1 deletion(-)
net/sched/sch_taprio.c +0 -0
index 9c79374..1ab2fc9 100644

net/sched/sch_taprio.c static int taprio_change(struct Qdisc *sch, · line 1521

Quoted 10 lines
net/sched/sch_taprio.c +3 -1
@@ -1513,7 +1513,9 @@ static int taprio_change(struct Qdisc *sch, 
 struct nlattr *opt,
       taprio_set_picos_per_byte(dev, q);
       if (mqprio) {
-        netdev_set_num_tc(dev, mqprio->num_tc);
+        err = netdev_set_num_tc(dev, mqprio->num_tc);
+        if (err)
+            goto free_sched;
           for (i = 0; i < mqprio->num_tc; i++)
               netdev_set_tc_queue(dev, i,
                           mqprio->count[i],

When was the bug added ?

Hint: Please provide a Fixes: tag

taprio_parse_mqprio_opt() already checks :

Quoted 11 lines

/* Verify num_tc is not out of max range */
if (qopt->num_tc > TC_MAX_QUEUE) {
     NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range"); return -EINVAL;
}

So what is happening exactly ?

syzkaller reported this problem,the log shows mqprio->count[16] is accessed. here is the log
https://syzkaller.appspot.com/bug?id=3a3677d4e7539ec5e671a81e32882ad40b5f7b64

the added check logic is hurtlessness,and netdev_set_num_tc does have a return value.

maintainer
#6 lore raw
Quoted 2 lines · On Mon, 30 Aug 2021 10:09:38 +0800 Haimin Zhang wrote:

hi,i wonder to know what‘s going on with this patch ? are you plannig to merge it ?

Please don't top post. As I told you I think that fix belongs in taprio_parse_mqprio_opt() which is validating inputs, not in the middle of the code applying the changes.

maintainer
#7 lore raw
In reply to [email protected]
Quoted 18 lines · On Wed, 11 Aug 2021 13:10:09 +0800 [email protected] wrote:

From: Haimin Zhang <[email protected]>

syzbot report an array-index-out-of-bounds in taprio_change index 16 is out of range for type '__u16 [16]'
that's because mqprio->num_tc is lager than TC_MAX_QUEUE,so we check the return value of netdev_set_num_tc.

---
 net/sched/sch_taprio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/sched/sch_taprio.c +0 -0
index 9c79374..1ab2fc9 100644

net/sched/sch_taprio.c static int taprio_change(struct Qdisc *sch, struct nlattr *opt, · line 1518

Quoted 7 lines
net/sched/sch_taprio.c +3 -1
@@ -1513,7 +1513,9 @@ static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
 	taprio_set_picos_per_byte(dev, q);
 
 	if (mqprio) {
-		netdev_set_num_tc(dev, mqprio->num_tc);
+		err = netdev_set_num_tc(dev, mqprio->num_tc);
+		if (err)
+			goto free_sched;

taprio_set_picos_per_byte() already got called and applied some of the changes. It seems like the early return from taprio_parse_mqprio_opt() if dev->num_tc is non-zero is incorrect. That function is supposed to validate that mqprio_opt() is correct AFAIU. That would mean:

Fixes a3d43c0d56f1 ("taprio: Add support adding an admin schedule")

Vinicius - WDYT?

Quoted 3 lines
net/sched/sch_taprio.c +0 -0
 		for (i = 0; i < mqprio->num_tc; i++)
 			netdev_set_tc_queue(dev, i,
 					    mqprio->count[i],