Skip to contentSkip to conversation

sched: address a potential NULL pointer dereference in the GRED scheduler.

PATCH In review kwqcheii 3 participants 7 messages Feb 27, 2025 – Mar 7, 2025

Subsystems (3)
lore
Share & tools
lore

kwqcheii

author
#0 lore raw

PATCH 1/1 sched: address a potential NULL pointer dereference in the GRED scheduler.

If kzalloc in gred_init returns a NULL pointer, the code follows the error handling path, invoking gred_destroy. This, in turn, calls gred_offload, where memset could receive a NULL pointer as input, potentially leading to a kernel crash.

---
 net/sched/sch_gred.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
net/sched/sch_gred.c +6 -4
index ab6234b4fcd5..fa643e5709bd 100644
@@ -317,10 +317,12 @@ static void gred_offload(struct Qdisc *sch, enum tc_gred_command command)
 	if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
 		return;
 
-	memset(opt, 0, sizeof(*opt));
-	opt->command = command;
-	opt->handle = sch->handle;
-	opt->parent = sch->parent;
+	if (opt) {
+		memset(opt, 0, sizeof(*opt));
+		opt->command = command;
+		opt->handle = sch->handle;
+		opt->parent = sch->parent;
+	}
 
 	if (command == TC_GRED_REPLACE) {
 		unsigned int i;
Signature · 1 line
2.48.1
#1 lore raw
Quoted 1 line · kwqcheii

If kzalloc in gred_init returns a NULL pointer, the code follows the error handling path, invoking gred_destroy. This, in turn, calls gred_offload, where memset could receive a NULL pointer as input, potentially leading to a kernel crash.

Thanks for your patch.

Please add your Signed-off-by for your patch, which is a minimum requirement here. You can check Linux kernel development process for more details: https://docs.kernel.org/process/5.Posting.html#before-creating-patches

Also, ./scripts/checkpatch.pl could help you catch issues like this one, it would save you and others a lot of time.

Lastly, if you saw a real crash, please include the kernel stack trace in your patch description. There is a significant difference between a real crash and a theoretical one.

Regards,
Cong

author
#2 lore raw

PATCH 1/1 sched: address a potential NULL pointer dereference in the GRED scheduler.

In reply to kwqcheii

If kzalloc in gred_init returns a NULL pointer, the code follows the error handling path, invoking gred_destroy. This, in turn, calls gred_offload, where memset could receive a NULL pointer as input, potentially leading to a kernel crash.

---
 net/sched/sch_gred.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
net/sched/sch_gred.c +6 -4
index ab6234b4fcd5..fa643e5709bd 100644
@@ -317,10 +317,12 @@ static void gred_offload(struct Qdisc *sch, enum tc_gred_command command)
 	if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
 		return;
 
-	memset(opt, 0, sizeof(*opt));
-	opt->command = command;
-	opt->handle = sch->handle;
-	opt->parent = sch->parent;
+	if (opt) {
+		memset(opt, 0, sizeof(*opt));
+		opt->command = command;
+		opt->handle = sch->handle;
+		opt->parent = sch->parent;
+	}
 
 	if (command == TC_GRED_REPLACE) {
 		unsigned int i;
Signature · 1 line
2.48.1
#3 lore raw
Quoted 5 lines · kwqcheii

If kzalloc in gred_init returns a NULL pointer, the code follows the error handling path, invoking gred_destroy. This, in turn, calls gred_offload, where memset could receive a NULL pointer as input, potentially leading to a kernel crash.

Please use your real name for Signed-off-by.

Quoted 12 lines
---
 net/sched/sch_gred.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
net/sched/sch_gred.c +0 -0
index ab6234b4fcd5..fa643e5709bd 100644
@@ -317,10 +317,12 @@ static void gred_offload(struct Qdisc *sch, enum tc_gred_command command)
 	if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
 		return;
 

net/sched/sch_gred.c static void gred_offload(struct Qdisc *sch, enum tc_gred_command command) · line 325

Quoted 10 lines
net/sched/sch_gred.c +6 -4
-	memset(opt, 0, sizeof(*opt));
-	opt->command = command;
-	opt->handle = sch->handle;
-	opt->parent = sch->parent;
+	if (opt) {
+		memset(opt, 0, sizeof(*opt));
+		opt->command = command;
+		opt->handle = sch->handle;
+		opt->parent = sch->parent;
+	}

I think the whole gred_offload() should be skipped when table->opt == NULL, espeically the last call of ->ndo_setup_tc(). Something like:

net/sched/sch_gred.c +2 -1
index ab6234b4fcd5..532fde548b88 100644
@@ -913,7 +913,8 @@ static void gred_destroy(struct Qdisc *sch)
        for (i = 0; i < table->DPs; i++)
                gred_destroy_vq(table->tab[i]);
 
-       gred_offload(sch, TC_GRED_DESTROY);
+       if (table->opt)
+               gred_offload(sch, TC_GRED_DESTROY);
        kfree(table->opt);
 }
 

What do you think?

Thanks.

author
#4 lore raw

PATCH 1/1 sched: address a potential NULL pointer dereference in the GRED scheduler.

If kzalloc in gred_init returns a NULL pointer, the code follows the error handling path, invoking gred_destroy. This, in turn, calls gred_offload, where memset could receive a NULL pointer as input, potentially leading to a kernel crash.

---
 net/sched/sch_gred.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/sched/sch_gred.c +2 -1
index ab6234b4fcd5..532fde548b88 100644
@@ -913,7 +913,8 @@ static void gred_destroy(struct Qdisc *sch)
 	for (i = 0; i < table->DPs; i++)
 		gred_destroy_vq(table->tab[i]);
 
-	gred_offload(sch, TC_GRED_DESTROY);
+	if (table->opt)
+		gred_offload(sch, TC_GRED_DESTROY);
 	kfree(table->opt);
 }
 
Signature · 1 line
2.48.1
Reviewed-by on v1 1/1
#5 lore raw
Quoted 6 lines · Jun Yang

If kzalloc in gred_init returns a NULL pointer, the code follows the error handling path, invoking gred_destroy. This, in turn, calls gred_offload, where memset could receive a NULL pointer as input, potentially leading to a kernel crash.

When table->opt is NULL in gred_init(), gred_change_table_def() is not called yet, so it is not necessary to call ->ndo_setup_tc() in gred_offload().

Fixes f25c0515c521 ("net: sched: gred: dynamically allocate tc_gred_qopt_offload")

Thanks!

#6 lore raw
In reply to Jun Yang
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 8 lines · On Wed, 5 Mar 2025 23:44:10 +0800 you wrote:

If kzalloc in gred_init returns a NULL pointer, the code follows the error handling path, invoking gred_destroy. This, in turn, calls gred_offload, where memset could receive a NULL pointer as input, potentially leading to a kernel crash.

[...]

Here is the summary with links:
- sched: address a potential NULL pointer dereference in the GRED scheduler.

    https://git.kernel.org/netdev/net/c/115ef44a9822

You are awesome, thank you!

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