Skip to contentSkip to conversation

arm64/mm: Fix ttbr0 values stored in struct thread_info for software-pan

PATCH Applied Anshuman Khandual 3 participants 4 messages Jun 15, 2021 arm64

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64/mm: Fix ttbr0 values stored in struct thread_info for software-pan

When using CONFIG_ARM64_SW_TTBR0_PAN, a task's thread_info::ttbr0 must be the TTBR0_EL1 value used to run userspace. With 52-bit PAs, the PA must be packed into the TTBR using phys_to_ttbr(), but we forget to do this in some of the SW PAN code. Thus, if the value is installed into TTBR0_EL1 (as may happen in the uaccess routines), this could result in UNPREDICTABLE behaviour.

Since hardware with 52-bit PA support almost certainly has HW PAN, which will be used in preference, this shouldn't be a practical issue, but let's fix this for consistency.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Mark Rutland <[email protected]>
Cc James Morse <[email protected]>
Fixes 529c4b05a3cb ("arm64: handle 52-bit addresses in TTBR")

---
Just found via code inspection. Applies on v5.13-rc6.

 arch/arm64/include/asm/mmu_context.h | 4 ++--
 arch/arm64/kernel/setup.c            | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
arch/arm64/include/asm/mmu_context.h +2 -2
index d3cef9133539..eeb210997149 100644
@@ -177,9 +177,9 @@ static inline void update_saved_ttbr0(struct task_struct *tsk,
 		return;
 
 	if (mm == &init_mm)
-		ttbr = __pa_symbol(reserved_pg_dir);
+		ttbr = phys_to_ttbr(__pa_symbol(reserved_pg_dir));
 	else
-		ttbr = virt_to_phys(mm->pgd) | ASID(mm) << 48;
+		ttbr = phys_to_ttbr(virt_to_phys(mm->pgd)) | ASID(mm) << 48;
 
 	WRITE_ONCE(task_thread_info(tsk)->ttbr0, ttbr);
 }
arch/arm64/kernel/setup.c +1 -1
index 61845c0821d9..68b30e8c22db 100644
@@ -381,7 +381,7 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p)
 	 * faults in case uaccess_enable() is inadvertently called by the init
 	 * thread.
 	 */
-	init_task.thread_info.ttbr0 = __pa_symbol(reserved_pg_dir);
+	init_task.thread_info.ttbr0 = phys_to_ttbr(__pa_symbol(reserved_pg_dir));
 #endif
 
 	if (boot_args[1] || boot_args[2] || boot_args[3]) {
Signature · 1 line
2.20.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Anshuman Khandual

When using CONFIG_ARM64_SW_TTBR0_PAN, a task's thread_info::ttbr0 must be the TTBR0_EL1 value used to run userspace. With 52-bit PAs, the PA must be packed into the TTBR using phys_to_ttbr(), but we forget to do this in some of the SW PAN code. Thus, if the value is installed into TTBR0_EL1 (as may happen in the uaccess routines), this could result in UNPREDICTABLE behaviour.

Since hardware with 52-bit PA support almost certainly has HW PAN, which will be used in preference, this shouldn't be a practical issue, but let's fix this for consistency.

I'm ok with fixing this for consistency. We should never hit those paths unless someone built hardware with 52-bit PA (8.2) but without PAN (8.1) and it would not be architecture compliant.

I'll leave it with Will for 5.14, it's no a fix that needs urgent queuing.

maintainer
#2 lore raw
Quoted 12 lines · Catalin Marinas
Quoted 10 lines · Anshuman Khandual

When using CONFIG_ARM64_SW_TTBR0_PAN, a task's thread_info::ttbr0 must be the TTBR0_EL1 value used to run userspace. With 52-bit PAs, the PA must be packed into the TTBR using phys_to_ttbr(), but we forget to do this in some of the SW PAN code. Thus, if the value is installed into TTBR0_EL1 (as may happen in the uaccess routines), this could result in UNPREDICTABLE behaviour.

Since hardware with 52-bit PA support almost certainly has HW PAN, which will be used in preference, this shouldn't be a practical issue, but let's fix this for consistency.

Quoted 8 lines

I'm ok with fixing this for consistency. We should never hit those paths unless someone built hardware with 52-bit PA (8.2) but without PAN (8.1) and it would not be architecture compliant.

I'll leave it with Will for 5.14, it's no a fix that needs urgent queuing.

Cheers, I'll pick it up.

Will

maintainer Applied
#3 lore raw
In reply to Anshuman Khandual
Quoted 8 lines · Anshuman Khandual

When using CONFIG_ARM64_SW_TTBR0_PAN, a task's thread_info::ttbr0 must be the TTBR0_EL1 value used to run userspace. With 52-bit PAs, the PA must be packed into the TTBR using phys_to_ttbr(), but we forget to do this in some of the SW PAN code. Thus, if the value is installed into TTBR0_EL1 (as may happen in the uaccess routines), this could result in UNPREDICTABLE behaviour.

[...]

Applied to arm64 (for-next/mm), thanks!

[1/1] arm64/mm: Fix ttbr0 values stored in struct thread_info for software-pan

      https://git.kernel.org/arm64/c/9163f0113030

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev