Skip to contentSkip to conversation

x86: avoid copying dynamic FP state from init_task

PATCH Applied Benjamin Berg 2 participants 2 messages Feb 26, 2025 x86

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86: avoid copying dynamic FP state from init_task

From: Benjamin Berg <[email protected]>

The init_task instance of struct task_struct is statically allocated and may not contain the full FP state for userspace. As such, limit the copy to the valid area of both init_task and dst and ensure all memory is initialized.

Note that the FP state is only needed for userspace, and as such it is entirely reasonable for init_task to not contain parts of it.

Fixes 5aaeb5c01c5b ("x86/fpu, sched: Introduce CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT and use it on x86")

----

v2:
- Fix code if arch_task_struct_size < sizeof(init_task) by using memcpy_and_pad.

---
 arch/x86/kernel/process.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/x86/kernel/process.c +7 -1
index 6da6769d7254..a8f65c17df10 100644
@@ -93,7 +93,13 @@ EXPORT_PER_CPU_SYMBOL_GPL(__tss_limit_invalid);
  */
 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
 {
-	memcpy(dst, src, arch_task_struct_size);
+	/* init_task is not dynamically sized (incomplete FPU state) */
+	if (unlikely(src == &init_task))
+		memcpy_and_pad(dst, arch_task_struct_size,
+			       src, sizeof(init_task), 0);
+	else
+		memcpy(dst, src, arch_task_struct_size);
+
 #ifdef CONFIG_VM86
 	dst->thread.vm86 = NULL;
 #endif
Signature · 1 line
2.48.1
merge bot Applied
#1 lore raw

[tip: x86/fpu] x86/fpu: Avoid copying dynamic FP state from init_task in arch_dup_task_struct()

The following commit has been merged into the x86/fpu branch of tip:

The following commit has been merged into the x86/fpu branch of tip:

Commit-ID:     5d3b81d4d8520efe888536b6906dc10fd1a228a8
Gitweb:        https://git.kernel.org/tip/5d3b81d4d8520efe888536b6906dc10fd1a228a8
Author:        Benjamin Berg <[email protected]>
AuthorDate:    Wed, 26 Feb 2025 14:31:36 +01:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 26 Feb 2025 15:32:34 +01:00

x86/fpu: Avoid copying dynamic FP state from init_task in arch_dup_task_struct()

The init_task instance of struct task_struct is statically allocated and may not contain the full FP state for userspace. As such, limit the copy to the valid area of both init_task and 'dst' and ensure all memory is initialized.

Note that the FP state is only needed for userspace, and as such it is entirely reasonable for init_task to not contain parts of it.

Fixes 5aaeb5c01c5b ("x86/fpu, sched: Introduce CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT and use it on x86")
Cc Andy Lutomirski <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Oleg Nesterov <[email protected]>

----

v2:
- Fix code if arch_task_struct_size < sizeof(init_task) by using memcpy_and_pad.

---
 arch/x86/kernel/process.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/kernel/process.c +6 -1
index 6da6769..7cb52f8 100644
@@ -93,7 +93,12 @@ EXPORT_PER_CPU_SYMBOL_GPL(__tss_limit_invalid);
  */
 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
 {
-	memcpy(dst, src, arch_task_struct_size);
+	/* init_task is not dynamically sized (incomplete FPU state) */
+	if (unlikely(src == &init_task))
+		memcpy_and_pad(dst, arch_task_struct_size, src, sizeof(init_task), 0);
+	else
+		memcpy(dst, src, arch_task_struct_size);
+
 #ifdef CONFIG_VM86
 	dst->thread.vm86 = NULL;
 #endif