Skip to contentSkip to conversation

net/sched: taprio: always validate TCA_TAPRIO_ATTR_PRIOMAP

PATCH Applied Eric Dumazet 4 participants 5 messages Jun 4, 2024 – Jun 5, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/sched: taprio: always validate TCA_TAPRIO_ATTR_PRIOMAP

If one TCA_TAPRIO_ATTR_PRIOMAP attribute has been provided, taprio_parse_mqprio_opt() must validate it, or userspace can inject arbitrary data to the kernel, the second time taprio_change() is called.

First call (with valid attributes) sets dev->num_tc to a non zero value.

Second call (with arbitrary mqprio attributes)
returns early from taprio_parse_mqprio_opt()
and bad things can happen.

Fixes a3d43c0d56f1 ("taprio: Add support adding an admin schedule")
Cc Vinicius Costa Gomes <[email protected]>
Cc Vladimir Oltean <[email protected]>
---
 net/sched/sch_taprio.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
net/sched/sch_taprio.c +6 -9
index 937a0c513c17..b284a06b5a75 100644
@@ -1176,16 +1176,13 @@ static int taprio_parse_mqprio_opt(struct net_device *dev,
 {
 	bool allow_overlapping_txqs = TXTIME_ASSIST_IS_ENABLED(taprio_flags);
 
-	if (!qopt && !dev->num_tc) {
-		NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
-		return -EINVAL;
-	}
-
-	/* If num_tc is already set, it means that the user already
-	 * configured the mqprio part
-	 */
-	if (dev->num_tc)
+	if (!qopt) {
+		if (!dev->num_tc) {
+			NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
+			return -EINVAL;
+		}
 		return 0;
+	}
 
 	/* taprio imposes that traffic classes map 1:n to tx queues */
 	if (qopt->num_tc > dev->num_tx_queues) {
Signature · 1 line
2.45.2.505.gda0bf45e8d-goog
#1 lore raw
Quoted 51 lines · Eric Dumazet

If one TCA_TAPRIO_ATTR_PRIOMAP attribute has been provided, taprio_parse_mqprio_opt() must validate it, or userspace can inject arbitrary data to the kernel, the second time taprio_change() is called.

First call (with valid attributes) sets dev->num_tc to a non zero value.

Second call (with arbitrary mqprio attributes)
returns early from taprio_parse_mqprio_opt()
and bad things can happen.

Fixes a3d43c0d56f1 ("taprio: Add support adding an admin schedule")
Cc Vinicius Costa Gomes <[email protected]>
Cc Vladimir Oltean <[email protected]>
---
 net/sched/sch_taprio.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
net/sched/sch_taprio.c +6 -9
index 937a0c513c17..b284a06b5a75 100644
@@ -1176,16 +1176,13 @@ static int taprio_parse_mqprio_opt(struct net_device *dev,
 {
 	bool allow_overlapping_txqs = TXTIME_ASSIST_IS_ENABLED(taprio_flags);
 
-	if (!qopt && !dev->num_tc) {
-		NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
-		return -EINVAL;
-	}
-
-	/* If num_tc is already set, it means that the user already
-	 * configured the mqprio part
-	 */
-	if (dev->num_tc)
+	if (!qopt) {
+		if (!dev->num_tc) {
+			NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
+			return -EINVAL;
+		}
 		return 0;
+	}
 
 	/* taprio imposes that traffic classes map 1:n to tx queues */
 	if (qopt->num_tc > dev->num_tx_queues) {
Signature · 1 line
2.45.2.505.gda0bf45e8d-goog

Correct, the mqprio qopt structure should be validated whenever passed, not just if dev->num_tc == 0.

But... what bad things can happen?

Whenever I try to trick the kernel into doing something with the second mqprio, I get:

"Changing the traffic mapping of a running schedule is not supported."

maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Eric Dumazet
Quoted 35 lines · Eric Dumazet

If one TCA_TAPRIO_ATTR_PRIOMAP attribute has been provided, taprio_parse_mqprio_opt() must validate it, or userspace can inject arbitrary data to the kernel, the second time taprio_change() is called.

First call (with valid attributes) sets dev->num_tc to a non zero value.

Second call (with arbitrary mqprio attributes)
returns early from taprio_parse_mqprio_opt()
and bad things can happen.

Fixes a3d43c0d56f1 ("taprio: Add support adding an admin schedule")
Cc Vinicius Costa Gomes <[email protected]>
Cc Vladimir Oltean <[email protected]>
---
 net/sched/sch_taprio.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
net/sched/sch_taprio.c +0 -6
index 937a0c513c17..b284a06b5a75 100644
@@ -1176,16 +1176,13 @@ static int taprio_parse_mqprio_opt(struct net_device *dev,
 {
 	bool allow_overlapping_txqs = TXTIME_ASSIST_IS_ENABLED(taprio_flags);
 
-	if (!qopt && !dev->num_tc) {
-		NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
-		return -EINVAL;
-	}
-
-	/* If num_tc is already set, it means that the user already

net/sched/sch_taprio.c static int taprio_parse_mqprio_opt(struct net_device *dev, · line 1185

Quoted 10 lines
net/sched/sch_taprio.c +6 -3
-	 * configured the mqprio part
-	 */
-	if (dev->num_tc)
+	if (!qopt) {
+		if (!dev->num_tc) {
+			NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
+			return -EINVAL;
+		}
 		return 0;
+	}

Nice one. I think I have an idea of what's going on here.

Validating the priomap even if we are not going to install it is good:

Cheers,

Signature · 1 line
Vinicius
Reviewed-by on v1 1/1
#3 lore raw
In reply to Eric Dumazet
Quoted 12 lines · Eric Dumazet

If one TCA_TAPRIO_ATTR_PRIOMAP attribute has been provided, taprio_parse_mqprio_opt() must validate it, or userspace can inject arbitrary data to the kernel, the second time taprio_change() is called.

First call (with valid attributes) sets dev->num_tc to a non zero value.

Second call (with arbitrary mqprio attributes)
returns early from taprio_parse_mqprio_opt()
and bad things can happen.

Quoted 6 lines
Fixes a3d43c0d56f1 ("taprio: Add support adding an admin schedule")
Cc Vinicius Costa Gomes <[email protected]>
Cc Vladimir Oltean <[email protected]>

---

#4 lore raw
In reply to Eric Dumazet
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Tue, 4 Jun 2024 18:15:11 +0000 you wrote:

If one TCA_TAPRIO_ATTR_PRIOMAP attribute has been provided, taprio_parse_mqprio_opt() must validate it, or userspace can inject arbitrary data to the kernel, the second time taprio_change() is called.

First call (with valid attributes) sets dev->num_tc to a non zero value.

[...]

Here is the summary with links:
- [net] net/sched: taprio: always validate TCA_TAPRIO_ATTR_PRIOMAP

    https://git.kernel.org/netdev/net/c/f921a58ae208

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html