Skip to contentSkip to conversation

scs: Fix a wrong parameter in __scs_magic

PATCH Applied Zhichi Lin 4 participants 5 messages Oct 11, 2025 – Oct 30, 2025

lore
Share & tools
lore

Zhichi Lin

author
#0 lore raw

PATCH 1/1 scs: Fix a wrong parameter in __scs_magic

__scs_magic() needs a 'void *' variable, but a 'struct task_struct *' is given. 'task_scs(tsk)' is the starting address of the task's shadow call stack, and '__scs_magic(task_scs(tsk))' is the end address of the task's shadow call stack.
Here should be '__scs_magic(task_scs(tsk))'.

Fixes 5bbaf9d1fcb9 ("scs: Add support for stack usage debugging")
---
 kernel/scs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/scs.c +1 -1
index d7809affe..772488afd 100644
@@ -135,7 +135,7 @@ static void scs_check_usage(struct task_struct *tsk)
 	if (!IS_ENABLED(CONFIG_DEBUG_STACK_USAGE))
 		return;
 
-	for (p = task_scs(tsk); p < __scs_magic(tsk); ++p) {
+	for (p = task_scs(tsk); p < __scs_magic(task_scs(tsk)); ++p) {
 		if (!READ_ONCE_NOCHECK(*p))
 			break;
 		used += sizeof(*p);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 5 lines · On Sat, 11 Oct 2025 16:22:22 +0800 Zhichi Lin <[email protected]> wrote:

__scs_magic() needs a 'void *' variable, but a 'struct task_struct *' is given. 'task_scs(tsk)' is the starting address of the task's shadow call stack, and '__scs_magic(task_scs(tsk))' is the end address of the task's shadow call stack.
Here should be '__scs_magic(task_scs(tsk))'.

What are the userspace-visible runtime effects of this bug? Please always describe this when fixing something.

Quoted 1 line
Fixes 5bbaf9d1fcb9 ("scs: Add support for stack usage debugging")

This might need backporting into -stable kernels, that depends on the answer to the above question.

kernel/scs.c static void scs_check_usage(struct task_struct *tsk) · line 141

Quoted 11 lines
kernel/scs.c +1 -1
@@ -135,7 +135,7 @@ static void scs_check_usage(struct task_struct *tsk)
 	if (!IS_ENABLED(CONFIG_DEBUG_STACK_USAGE))
 		return;
 
-	for (p = task_scs(tsk); p < __scs_magic(tsk); ++p) {
+	for (p = task_scs(tsk); p < __scs_magic(task_scs(tsk)); ++p) {
 		if (!READ_ONCE_NOCHECK(*p))
 			break;
 		used += sizeof(*p);

Thanks, I'll grab the patch for now, maybe Will would prefer to take it?

author
#2 lore raw
Quoted 11 lines · Andrew Morton
Quoted 5 lines · On Sat, 11 Oct 2025 16:22:22 +0800 Zhichi Lin <[email protected]> wrote:

__scs_magic() needs a 'void *' variable, but a 'struct task_struct *' is given. 'task_scs(tsk)' is the starting address of the task's shadow call stack, and '__scs_magic(task_scs(tsk))' is the end address of the task's shadow call stack.
Here should be '__scs_magic(task_scs(tsk))'.

What are the userspace-visible runtime effects of this bug? Please always describe this when fixing something.

The user-visible effect of this bug is that when CONFIG_DEBUG_STACK_USAGE is enabled, the shadow call stack usage checking function (scs_check_usage) would scan an incorrect memory range. This could lead to:

1. **Inaccurate stack usage reporting**: The function would calculate wrong usage statistics for the shadow call stack, potentially showing incorrect value in kmsg.

2. **Potential kernel crash**: If the value of __scs_magic(tsk)is greater than that of __scs_magic(task_scs(tsk)), the for loop may access unmapped memory, potentially causing a kernel panic. However, this scenario is unlikely because task_struct is allocated via the slab allocator (which typically returns lower addresses), while the shadow call stack returned by task_scs(tsk) is allocated via vmalloc(which typically returns higher addresses).

However, since this is purely a debugging feature (CONFIG_DEBUG_STACK_USAGE), normal production systems should be not unaffected. The bug only impacts developers and testers who are actively debugging stack usage with this configuration enabled.

Additionally, this issue was discovered during code reading - our project does not actually use this feature. For a more accurate assessment of real-world impact and backporting necessity, I think Will and Sami would be better positioned to evaluate, as they have deeper knowledge of how this debugging feature is actually used.

Quoted 18 lines
Quoted 1 line
Fixes 5bbaf9d1fcb9 ("scs: Add support for stack usage debugging")

This might need backporting into -stable kernels, that depends on the answer to the above question.

kernel/scs.c static void scs_check_usage(struct task_struct *tsk) · line 141

Quoted 11 lines
kernel/scs.c +1 -1
@@ -135,7 +135,7 @@ static void scs_check_usage(struct task_struct *tsk)
  	if (!IS_ENABLED(CONFIG_DEBUG_STACK_USAGE))
  		return;
 
-	for (p = task_scs(tsk); p < __scs_magic(tsk); ++p) {
+	for (p = task_scs(tsk); p < __scs_magic(task_scs(tsk)); ++p) {
  		if (!READ_ONCE_NOCHECK(*p))
  			break;
  		used += sizeof(*p);

Thanks, I'll grab the patch for now, maybe Will would prefer to take it?

Acked-by
#3 lore raw
In reply to Andrew Morton
Quoted 22 lines · Andrew Morton
Quoted 5 lines · On Sat, 11 Oct 2025 16:22:22 +0800 Zhichi Lin <[email protected]> wrote:

__scs_magic() needs a 'void *' variable, but a 'struct task_struct *' is given. 'task_scs(tsk)' is the starting address of the task's shadow call stack, and '__scs_magic(task_scs(tsk))' is the end address of the task's shadow call stack.
Here should be '__scs_magic(task_scs(tsk))'.

What are the userspace-visible runtime effects of this bug? Please always describe this when fixing something.

Quoted 1 line
Fixes 5bbaf9d1fcb9 ("scs: Add support for stack usage debugging")

This might need backporting into -stable kernels, that depends on the answer to the above question.

Quoted 6 lines
kernel/scs.c +0 -0
@@ -135,7 +135,7 @@ static void scs_check_usage(struct task_struct *tsk)
 	if (!IS_ENABLED(CONFIG_DEBUG_STACK_USAGE))
 		return;
 
Quoted 7 lines

kernel/scs.c static void scs_check_usage(struct task_struct *tsk) · line 141

Quoted 5 lines
kernel/scs.c +1 -1
-	for (p = task_scs(tsk); p < __scs_magic(tsk); ++p) {
+	for (p = task_scs(tsk); p < __scs_magic(task_scs(tsk)); ++p) {
 		if (!READ_ONCE_NOCHECK(*p))
 			break;
 		used += sizeof(*p);

Thanks, I'll grab the patch for now, maybe Will would prefer to take it?

Apologies for the slow reply, I was away for a couple of weeks.

It looks like you've already put this in -next (thank you!), so please go ahead and send it to Linus as a fix.

Cheers,

Will

Reviewed-by on v1 1/1
#4 lore raw
In reply to Zhichi Lin
Quoted 19 lines · Zhichi Lin

__scs_magic() needs a 'void *' variable, but a 'struct task_struct *' is given. 'task_scs(tsk)' is the starting address of the task's shadow call stack, and '__scs_magic(task_scs(tsk))' is the end address of the task's shadow call stack.
Here should be '__scs_magic(task_scs(tsk))'.

Fixes 5bbaf9d1fcb9 ("scs: Add support for stack usage debugging")
---
 kernel/scs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/scs.c +0 -0
index d7809affe..772488afd 100644

kernel/scs.c static void scs_check_usage(struct task_struct *tsk) · line 141

Quoted 8 lines
kernel/scs.c +1 -1
@@ -135,7 +135,7 @@ static void scs_check_usage(struct task_struct *tsk)
        if (!IS_ENABLED(CONFIG_DEBUG_STACK_USAGE))
                return;
 
-       for (p = task_scs(tsk); p < __scs_magic(tsk); ++p) {
+       for (p = task_scs(tsk); p < __scs_magic(task_scs(tsk)); ++p) {
                if (!READ_ONCE_NOCHECK(*p))
                        break;
                used += sizeof(*p);

Looks correct to me. Thanks for the fix!

Sami