Skip to content

Commit f2d2667

Browse files
torvaldsSasha Levin
authored andcommitted
x86: uaccess: don't use runtime-const rewriting in modules
[ Upstream commit 284922f4c563aa3a8558a00f2a05722133237fe8 ] The runtime-const infrastructure was never designed to handle the modular case, because the constant fixup is only done at boot time for core kernel code. But by the time I used it for the x86-64 user space limit handling in commit 86e6b15 ("x86: fix user address masking non-canonical speculation issue"), I had completely repressed that fact. And it all happens to work because the only code that currently actually gets inlined by modules is for the access_ok() limit check, where the default constant value works even when not fixed up. Because at least I had intentionally made it be something that is in the non-canonical address space region. But it's technically very wrong, and it does mean that at least in theory, the use of 'access_ok()' + '__get_user()' can trigger the same speculation issue with non-canonical addresses that the original commit was all about. The pattern is unusual enough that this probably doesn't matter in practice, but very wrong is still very wrong. Also, let's fix it before the nice optimized scoped user accessor helpers that Thomas Gleixner is working on cause this pseudo-constant to then be more widely used. This all came up due to an unrelated discussion with Mateusz Guzik about using the runtime const infrastructure for names_cachep accesses too. There the modular case was much more obviously broken, and Mateusz noted it in his 'v2' of the patch series. That then made me notice how broken 'access_ok()' had been in modules all along. Mea culpa, mea maxima culpa. Fix it by simply not using the runtime-const code in modules, and just using the USER_PTR_MAX variable value instead. This is not performance-critical like the core user accessor functions (get_user() and friends) are. Also make sure this doesn't get forgotten the next time somebody wants to do runtime constant optimizations by having the x86 runtime-const.h header file error out if included by modules. Fixes: 86e6b15 ("x86: fix user address masking non-canonical speculation issue") Acked-by: Borislav Petkov <bp@alien8.de> Acked-by: Sean Christopherson <seanjc@google.com> Cc: Thomas Gleixner <tglx@linutronix.de> Triggered-by: Mateusz Guzik <mjguzik@gmail.com> Link: https://lore.kernel.org/all/20251030105242.801528-1-mjguzik@gmail.com/ Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 139c55f commit f2d2667

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

arch/x86/include/asm/runtime-const.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#ifndef _ASM_RUNTIME_CONST_H
33
#define _ASM_RUNTIME_CONST_H
44

5+
#ifdef MODULE
6+
#error "Cannot use runtime-const infrastructure from modules"
7+
#endif
8+
59
#ifdef __ASSEMBLY__
610

711
.macro RUNTIME_CONST_PTR sym reg

arch/x86/include/asm/uaccess_64.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
#include <asm/cpufeatures.h>
1313
#include <asm/page.h>
1414
#include <asm/percpu.h>
15-
#include <asm/runtime-const.h>
1615

17-
/*
18-
* Virtual variable: there's no actual backing store for this,
19-
* it can purely be used as 'runtime_const_ptr(USER_PTR_MAX)'
20-
*/
16+
#ifdef MODULE
17+
#define runtime_const_ptr(sym) (sym)
18+
#else
19+
#include <asm/runtime-const.h>
20+
#endif
2121
extern unsigned long USER_PTR_MAX;
2222

2323
#ifdef CONFIG_ADDRESS_MASKING

arch/x86/kernel/cpu/common.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@
7878
DEFINE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
7979
EXPORT_PER_CPU_SYMBOL(cpu_info);
8080

81+
/* Used for modules: built-in code uses runtime constants */
82+
unsigned long USER_PTR_MAX;
83+
EXPORT_SYMBOL(USER_PTR_MAX);
84+
8185
u32 elf_hwcap2 __read_mostly;
8286

8387
/* Number of siblings per CPU package */
@@ -2578,7 +2582,7 @@ void __init arch_cpu_finalize_init(void)
25782582
alternative_instructions();
25792583

25802584
if (IS_ENABLED(CONFIG_X86_64)) {
2581-
unsigned long USER_PTR_MAX = TASK_SIZE_MAX;
2585+
USER_PTR_MAX = TASK_SIZE_MAX;
25822586

25832587
/*
25842588
* Enable this when LAM is gated on LASS support

0 commit comments

Comments
 (0)