Skip to contentSkip to conversation

profile: Fix use after free in profile_tick()

PATCH In review Xingrui Yi 3 participants 3 messages Feb 6, 2025 – Feb 7, 2025 Stable branch

lore
Share & tools
lore

Xingrui Yi

author
#0 lore raw

PATCH 1/1 profile: Fix use after free in profile_tick()

[free]
profiling_store()
--> profile_init()

    --> free_cpumask_var(prof_cpu_mask)                           <-- freed

[use]
tick_sched_timer()
--> profile_tick()

    --> cpumask_available(prof_cpu_mask)                          <-- prof_cpu_mask is not NULL
                                                                      if cpumask offstack
      --> cpumask_test_cpu(smp_processor_id(), prof_cpu_mask)     <-- use after free

When profile_init() failed if prof_buffer is not allocated, prof_cpu_mask will be kfreed by free_cpumask_var() but not set to NULL when CONFIG_CPUMASK_OFFSTACK=y, thus profile_tick() will use prof_cpu_mask after free.

---
 kernel/profile.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/profile.c +3 -0
index 0db1122855c0..b5e85193cb02 100644
@@ -137,6 +137,9 @@ int __ref profile_init(void)
 		return 0;
 
 	free_cpumask_var(prof_cpu_mask);
+#ifdef CONFIG_CPUMASK_OFFSTACK
+	prof_cpu_mask = NULL;
+#endif
 	return -ENOMEM;
 }
 
Signature · 1 line
2.43.5
maintainer
#1 lore raw
Quoted 40 lines · Xingrui Yi

[free]
profiling_store()
--> profile_init()

    --> free_cpumask_var(prof_cpu_mask)                           <-- freed

[use]
tick_sched_timer()
--> profile_tick()

    --> cpumask_available(prof_cpu_mask)                          <-- prof_cpu_mask is not NULL
                                                                      if cpumask offstack
      --> cpumask_test_cpu(smp_processor_id(), prof_cpu_mask)     <-- use after free

When profile_init() failed if prof_buffer is not allocated, prof_cpu_mask will be kfreed by free_cpumask_var() but not set to NULL when CONFIG_CPUMASK_OFFSTACK=y, thus profile_tick() will use prof_cpu_mask after free.

---
 kernel/profile.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/profile.c +3 -0
index 0db1122855c0..b5e85193cb02 100644
@@ -137,6 +137,9 @@ int __ref profile_init(void)
 		return 0;
 
 	free_cpumask_var(prof_cpu_mask);
+#ifdef CONFIG_CPUMASK_OFFSTACK
+	prof_cpu_mask = NULL;
+#endif
 	return -ENOMEM;
 }
 
Signature · 1 line
2.43.5

<formletter>

This is not the correct way to submit patches for inclusion in the stable kernel tree. Please read:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.

</formletter>

maintainer
#2 lore raw
In reply to Xingrui Yi

[ Sasha's backport helper bot ]

Hi,

No upstream commit was identified. Using temporary commit for testing.

Results of testing on various branches:

| Branch                    | Patch Apply | Build Test |
|---------------------------|-------------|------------|
| stable/linux-5.10.y       |  Success    |  Failed    |

Build Errors:
Build error for stable/linux-5.10.y:

    ssh: connect to host 192.168.1.58 port 22: No route to host