Skip to content

Commit 86e6b15

Browse files
committed
x86: fix user address masking non-canonical speculation issue
It turns out that AMD has a "Meltdown Lite(tm)" issue with non-canonical accesses in kernel space. And so using just the high bit to decide whether an access is in user space or kernel space ends up with the good old "leak speculative data" if you have the right gadget using the result: CVE-2020-12965 “Transient Execution of Non-Canonical Accesses“ Now, the kernel surrounds the access with a STAC/CLAC pair, and those instructions end up serializing execution on older Zen architectures, which closes the speculation window. But that was true only up until Zen 5, which renames the AC bit [1]. That improves performance of STAC/CLAC a lot, but also means that the speculation window is now open. Note that this affects not just the new address masking, but also the regular valid_user_address() check used by access_ok(), and the asm version of the sign bit check in the get_user() helpers. It does not affect put_user() or clear_user() variants, since there's no speculative result to be used in a gadget for those operations. Reported-by: Andrew Cooper <[email protected]> Link: https://lore.kernel.org/all/[email protected]/ Link: https://lore.kernel.org/all/20241023094448.GAZxjFkEOOF_DM83TQ@fat_crate.local/ [1] Link: https://www.amd.com/en/resources/product-security/bulletin/amd-sb-1010.html Link: https://arxiv.org/pdf/2108.10771 Cc: Josh Poimboeuf <[email protected]> Cc: Borislav Petkov <[email protected]> Tested-by: Maciej Wieczor-Retman <[email protected]> # LAM case Fixes: 2865baf ("x86: support user address masking instead of non-speculative conditional") Fixes: 6014bc2 ("x86-64: make access_ok() independent of LAM") Fixes: b19b74b ("x86/mm: Rework address range check in get_user() and put_user()") Signed-off-by: Linus Torvalds <[email protected]>
1 parent ae90f6a commit 86e6b15

File tree

4 files changed

+42
-21
lines changed

4 files changed

+42
-21
lines changed

arch/x86/include/asm/uaccess_64.h

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
#include <asm/cpufeatures.h>
1313
#include <asm/page.h>
1414
#include <asm/percpu.h>
15+
#include <asm/runtime-const.h>
16+
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+
*/
21+
extern unsigned long USER_PTR_MAX;
1522

1623
#ifdef CONFIG_ADDRESS_MASKING
1724
/*
@@ -46,19 +53,24 @@ static inline unsigned long __untagged_addr_remote(struct mm_struct *mm,
4653

4754
#endif
4855

49-
/*
50-
* The virtual address space space is logically divided into a kernel
51-
* half and a user half. When cast to a signed type, user pointers
52-
* are positive and kernel pointers are negative.
53-
*/
54-
#define valid_user_address(x) ((__force long)(x) >= 0)
56+
#define valid_user_address(x) \
57+
((__force unsigned long)(x) <= runtime_const_ptr(USER_PTR_MAX))
5558

5659
/*
5760
* Masking the user address is an alternative to a conditional
5861
* user_access_begin that can avoid the fencing. This only works
5962
* for dense accesses starting at the address.
6063
*/
61-
#define mask_user_address(x) ((typeof(x))((long)(x)|((long)(x)>>63)))
64+
static inline void __user *mask_user_address(const void __user *ptr)
65+
{
66+
unsigned long mask;
67+
asm("cmp %1,%0\n\t"
68+
"sbb %0,%0"
69+
:"=r" (mask)
70+
:"r" (ptr),
71+
"0" (runtime_const_ptr(USER_PTR_MAX)));
72+
return (__force void __user *)(mask | (__force unsigned long)ptr);
73+
}
6274
#define masked_user_access_begin(x) ({ \
6375
__auto_type __masked_ptr = (x); \
6476
__masked_ptr = mask_user_address(__masked_ptr); \
@@ -69,23 +81,16 @@ static inline unsigned long __untagged_addr_remote(struct mm_struct *mm,
6981
* arbitrary values in those bits rather then masking them off.
7082
*
7183
* Enforce two rules:
72-
* 1. 'ptr' must be in the user half of the address space
84+
* 1. 'ptr' must be in the user part of the address space
7385
* 2. 'ptr+size' must not overflow into kernel addresses
7486
*
75-
* Note that addresses around the sign change are not valid addresses,
76-
* and will GP-fault even with LAM enabled if the sign bit is set (see
77-
* "CR3.LAM_SUP" that can narrow the canonicality check if we ever
78-
* enable it, but not remove it entirely).
79-
*
80-
* So the "overflow into kernel addresses" does not imply some sudden
81-
* exact boundary at the sign bit, and we can allow a lot of slop on the
82-
* size check.
87+
* Note that we always have at least one guard page between the
88+
* max user address and the non-canonical gap, allowing us to
89+
* ignore small sizes entirely.
8390
*
8491
* In fact, we could probably remove the size check entirely, since
8592
* any kernel accesses will be in increasing address order starting
86-
* at 'ptr', and even if the end might be in kernel space, we'll
87-
* hit the GP faults for non-canonical accesses before we ever get
88-
* there.
93+
* at 'ptr'.
8994
*
9095
* That's a separate optimization, for now just handle the small
9196
* constant case.

arch/x86/kernel/cpu/common.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
#include <asm/sev.h>
7070
#include <asm/tdx.h>
7171
#include <asm/posted_intr.h>
72+
#include <asm/runtime-const.h>
7273

7374
#include "cpu.h"
7475

@@ -2389,6 +2390,15 @@ void __init arch_cpu_finalize_init(void)
23892390
alternative_instructions();
23902391

23912392
if (IS_ENABLED(CONFIG_X86_64)) {
2393+
unsigned long USER_PTR_MAX = TASK_SIZE_MAX-1;
2394+
2395+
/*
2396+
* Enable this when LAM is gated on LASS support
2397+
if (cpu_feature_enabled(X86_FEATURE_LAM))
2398+
USER_PTR_MAX = (1ul << 63) - PAGE_SIZE - 1;
2399+
*/
2400+
runtime_const_init(ptr, USER_PTR_MAX);
2401+
23922402
/*
23932403
* Make sure the first 2MB area is not mapped by huge pages
23942404
* There are typically fixed size MTRRs in there and overlapping

arch/x86/kernel/vmlinux.lds.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ SECTIONS
358358
#endif
359359

360360
RUNTIME_CONST_VARIABLES
361+
RUNTIME_CONST(ptr, USER_PTR_MAX)
361362

362363
. = ALIGN(PAGE_SIZE);
363364

arch/x86/lib/getuser.S

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,13 @@
3939

4040
.macro check_range size:req
4141
.if IS_ENABLED(CONFIG_X86_64)
42-
mov %rax, %rdx
43-
sar $63, %rdx
42+
movq $0x0123456789abcdef,%rdx
43+
1:
44+
.pushsection runtime_ptr_USER_PTR_MAX,"a"
45+
.long 1b - 8 - .
46+
.popsection
47+
cmp %rax, %rdx
48+
sbb %rdx, %rdx
4449
or %rdx, %rax
4550
.else
4651
cmp $TASK_SIZE_MAX-\size+1, %eax

0 commit comments

Comments
 (0)