Skip to content

Commit 2913136

Browse files
torvaldsgregkh
authored andcommitted
x86: fix user address masking non-canonical speculation issue
commit 86e6b15 upstream. 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 <andrew.cooper3@citrix.com> Link: https://lore.kernel.org/all/80d94591-1297-4afb-b510-c665efd37f10@citrix.com/ 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 <jpoimboe@kernel.org> Cc: Borislav Petkov <bp@alien8.de> Tested-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com> # 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 <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ee6f1a6 commit 2913136

4 files changed

Lines changed: 42 additions & 21 deletions

File tree

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,43 +53,41 @@ 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) ({ __uaccess_begin(); mask_user_address(x); })
6375

6476
/*
6577
* User pointers can have tag bits on x86-64. This scheme tolerates
6678
* arbitrary values in those bits rather then masking them off.
6779
*
6880
* Enforce two rules:
69-
* 1. 'ptr' must be in the user half of the address space
81+
* 1. 'ptr' must be in the user part of the address space
7082
* 2. 'ptr+size' must not overflow into kernel addresses
7183
*
72-
* Note that addresses around the sign change are not valid addresses,
73-
* and will GP-fault even with LAM enabled if the sign bit is set (see
74-
* "CR3.LAM_SUP" that can narrow the canonicality check if we ever
75-
* enable it, but not remove it entirely).
76-
*
77-
* So the "overflow into kernel addresses" does not imply some sudden
78-
* exact boundary at the sign bit, and we can allow a lot of slop on the
79-
* size check.
84+
* Note that we always have at least one guard page between the
85+
* max user address and the non-canonical gap, allowing us to
86+
* ignore small sizes entirely.
8087
*
8188
* In fact, we could probably remove the size check entirely, since
8289
* any kernel accesses will be in increasing address order starting
83-
* at 'ptr', and even if the end might be in kernel space, we'll
84-
* hit the GP faults for non-canonical accesses before we ever get
85-
* there.
90+
* at 'ptr'.
8691
*
8792
* That's a separate optimization, for now just handle the small
8893
* 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

@@ -2371,6 +2372,15 @@ void __init arch_cpu_finalize_init(void)
23712372
alternative_instructions();
23722373

23732374
if (IS_ENABLED(CONFIG_X86_64)) {
2375+
unsigned long USER_PTR_MAX = TASK_SIZE_MAX-1;
2376+
2377+
/*
2378+
* Enable this when LAM is gated on LASS support
2379+
if (cpu_feature_enabled(X86_FEATURE_LAM))
2380+
USER_PTR_MAX = (1ul << 63) - PAGE_SIZE - 1;
2381+
*/
2382+
runtime_const_init(ptr, USER_PTR_MAX);
2383+
23742384
/*
23752385
* Make sure the first 2MB area is not mapped by huge pages
23762386
* 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
@@ -359,6 +359,7 @@ SECTIONS
359359

360360
RUNTIME_CONST(shift, d_hash_shift)
361361
RUNTIME_CONST(ptr, dentry_hashtable)
362+
RUNTIME_CONST(ptr, USER_PTR_MAX)
362363

363364
. = ALIGN(PAGE_SIZE);
364365

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)