Skip to contentSkip to conversation

net_sched: sch_fq: Fix out of range band computation

PATCH In review Jamal Hadi Salim 2 participants 6 messages Dec 13, 2023

Subsystems (3)
lore
Share & tools
lore

Jamal Hadi Salim

author maintainer
#0 lore raw

PATCH 1/1 net_sched: sch_fq: Fix out of range band computation

It is possible to compute a band of 3. Doing so will overrun array q->band_pkt_count[0-2] boundaries.

Fixes 29f834aa326e ("net_sched: sch_fq: add 3 bands and WRR scheduling")
---
 net/sched/sch_fq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/sch_fq.c +1 -1
index 3a31c47fea9b..217c430343df 100644
@@ -159,7 +159,7 @@ struct fq_sched_data {
 /* return the i-th 2-bit value ("crumb") */
 static u8 fq_prio2band(const u8 *prio2band, unsigned int prio)
 {
-	return (prio2band[prio / 4] >> (2 * (prio & 0x3))) & 0x3;
+	return (prio2band[prio / 4] >> (2 * (prio & 0x3))) % 0x3;
 }
 
 /*
Signature · 1 line
2.34.1
#1 lore raw
Quoted 16 lines · Jamal Hadi Salim

It is possible to compute a band of 3. Doing so will overrun array q->band_pkt_count[0-2] boundaries.

Fixes 29f834aa326e ("net_sched: sch_fq: add 3 bands and WRR scheduling")
---
 net/sched/sch_fq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/sch_fq.c +0 -0
index 3a31c47fea9b..217c430343df 100644

net/sched/sch_fq.c struct fq_sched_data { · line 164

Quoted 7 lines
net/sched/sch_fq.c +1 -1
@@ -159,7 +159,7 @@ struct fq_sched_data {
 /* return the i-th 2-bit value ("crumb") */
 static u8 fq_prio2band(const u8 *prio2band, unsigned int prio)
 {
-       return (prio2band[prio / 4] >> (2 * (prio & 0x3))) & 0x3;
+       return (prio2band[prio / 4] >> (2 * (prio & 0x3))) % 0x3;
 }
 

Are you sure this is needed ?

fq_load_priomap() makes sure this can not happen...

author maintainer
#2 lore raw
Quoted 13 lines · Eric Dumazet
Quoted 11 lines · Jamal Hadi Salim

It is possible to compute a band of 3. Doing so will overrun array q->band_pkt_count[0-2] boundaries.

Fixes 29f834aa326e ("net_sched: sch_fq: add 3 bands and WRR scheduling")
---
 net/sched/sch_fq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

net/sched/sch_fq.c struct fq_sched_data { · line 164

Quoted 12 lines
net/sched/sch_fq.c +1 -1
index 3a31c47fea9b..217c430343df 100644
@@ -159,7 +159,7 @@ struct fq_sched_data {
 /* return the i-th 2-bit value ("crumb") */
 static u8 fq_prio2band(const u8 *prio2band, unsigned int prio)
 {
-       return (prio2band[prio / 4] >> (2 * (prio & 0x3))) & 0x3;
+       return (prio2band[prio / 4] >> (2 * (prio & 0x3))) % 0x3;
 }
 

Are you sure this is needed ?

According to coverity static analysis.

Quoted 1 line

fq_load_priomap() makes sure this can not happen...

True. Sounds like a false positive because coverity sees the masking with &0x3 and assumes we could get a result of 3.

cheers,
jamal

#3 lore raw
In reply to Eric Dumazet
Quoted 28 lines · Eric Dumazet
Quoted 16 lines · Jamal Hadi Salim

It is possible to compute a band of 3. Doing so will overrun array q->band_pkt_count[0-2] boundaries.

Fixes 29f834aa326e ("net_sched: sch_fq: add 3 bands and WRR scheduling")
---
 net/sched/sch_fq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/sch_fq.c +0 -0
index 3a31c47fea9b..217c430343df 100644

net/sched/sch_fq.c struct fq_sched_data { · line 164

Quoted 7 lines
net/sched/sch_fq.c +1 -1
@@ -159,7 +159,7 @@ struct fq_sched_data {
 /* return the i-th 2-bit value ("crumb") */
 static u8 fq_prio2band(const u8 *prio2band, unsigned int prio)
 {
-       return (prio2band[prio / 4] >> (2 * (prio & 0x3))) & 0x3;
+       return (prio2band[prio / 4] >> (2 * (prio & 0x3))) % 0x3;
 }
 

Are you sure this is needed ?

Quoted 1 line

fq_load_priomap() makes sure this can not happen...

Yeah, I am pretty sure this patch is incorrect, we need to mask to get only two bits.

author maintainer
#4 lore raw
Quoted 30 lines · Eric Dumazet
Quoted 28 lines · Eric Dumazet
Quoted 16 lines · Jamal Hadi Salim

It is possible to compute a band of 3. Doing so will overrun array q->band_pkt_count[0-2] boundaries.

Fixes 29f834aa326e ("net_sched: sch_fq: add 3 bands and WRR scheduling")
---
 net/sched/sch_fq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/sch_fq.c +0 -0
index 3a31c47fea9b..217c430343df 100644

net/sched/sch_fq.c struct fq_sched_data { · line 164

Quoted 7 lines
net/sched/sch_fq.c +1 -1
@@ -159,7 +159,7 @@ struct fq_sched_data {
 /* return the i-th 2-bit value ("crumb") */
 static u8 fq_prio2band(const u8 *prio2band, unsigned int prio)
 {
-       return (prio2band[prio / 4] >> (2 * (prio & 0x3))) & 0x3;
+       return (prio2band[prio / 4] >> (2 * (prio & 0x3))) % 0x3;
 }
 

Are you sure this is needed ?

Quoted 4 lines
Quoted 1 line

fq_load_priomap() makes sure this can not happen...

Yeah, I am pretty sure this patch is incorrect, we need to mask to get only two bits.

The check in fq_load_priomap() is what makes it moot. Masking with b'11 could result in b'11. Definitely the modulo will guarantee whatever results can only be in the range 0..2. But it is not needed.

cheers,
jamal

#5 lore raw
Quoted 32 lines · Jamal Hadi Salim
Quoted 30 lines · Eric Dumazet
Quoted 28 lines · Eric Dumazet
Quoted 16 lines · Jamal Hadi Salim

It is possible to compute a band of 3. Doing so will overrun array q->band_pkt_count[0-2] boundaries.

Fixes 29f834aa326e ("net_sched: sch_fq: add 3 bands and WRR scheduling")
---
 net/sched/sch_fq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/sch_fq.c +0 -0
index 3a31c47fea9b..217c430343df 100644

net/sched/sch_fq.c struct fq_sched_data { · line 164

Quoted 7 lines
net/sched/sch_fq.c +1 -1
@@ -159,7 +159,7 @@ struct fq_sched_data {
 /* return the i-th 2-bit value ("crumb") */
 static u8 fq_prio2band(const u8 *prio2band, unsigned int prio)
 {
-       return (prio2band[prio / 4] >> (2 * (prio & 0x3))) & 0x3;
+       return (prio2band[prio / 4] >> (2 * (prio & 0x3))) % 0x3;
 }
 

Are you sure this is needed ?

Quoted 10 lines
Quoted 4 lines
Quoted 1 line

fq_load_priomap() makes sure this can not happen...

Yeah, I am pretty sure this patch is incorrect, we need to mask to get only two bits.

The check in fq_load_priomap() is what makes it moot. Masking with b'11 could result in b'11. Definitely the modulo will guarantee whatever results can only be in the range 0..2. But it is not needed.

Modulo would be incorrect, since it would use high order bits.

(0x22 % 3) is different than (0x22 & 3)

Had you written:

return ((prio2band[prio / 4] >> (2 * (prio & 0x3))) & 0x3) % 3)

Then yes, the last % 3 would be "not needed"