Skip to content

Commit 2865baf

Browse files
committed
x86: support user address masking instead of non-speculative conditional
The Spectre-v1 mitigations made "access_ok()" much more expensive, since it has to serialize execution with the test for a valid user address. All the normal user copy routines avoid this by just masking the user address with a data-dependent mask instead, but the fast "unsafe_user_read()" kind of patterms that were supposed to be a fast case got slowed down. This introduces a notion of using src = masked_user_access_begin(src); to do the user address sanity using a data-dependent mask instead of the more traditional conditional if (user_read_access_begin(src, len)) { model. This model only works for dense accesses that start at 'src' and on architectures that have a guard region that is guaranteed to fault in between the user space and the kernel space area. With this, the user access doesn't need to be manually checked, because a bad address is guaranteed to fault (by some architecture masking trick: on x86-64 this involves just turning an invalid user address into all ones, since we don't map the top of address space). This only converts a couple of examples for now. Example x86-64 code generation for loading two words from user space: stac mov %rax,%rcx sar $0x3f,%rcx or %rax,%rcx mov (%rcx),%r13 mov 0x8(%rcx),%r14 clac where all the error handling and -EFAULT is now purely handled out of line by the exception path. Of course, if the micro-architecture does badly at 'clac' and 'stac', the above is still pitifully slow. But at least we did as well as we could. Signed-off-by: Linus Torvalds <[email protected]>
1 parent 0c38364 commit 2865baf

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

arch/x86/include/asm/uaccess_64.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ static inline unsigned long __untagged_addr_remote(struct mm_struct *mm,
5353
*/
5454
#define valid_user_address(x) ((__force long)(x) >= 0)
5555

56+
/*
57+
* Masking the user address is an alternative to a conditional
58+
* user_access_begin that can avoid the fencing. This only works
59+
* for dense accesses starting at the address.
60+
*/
61+
#define mask_user_address(x) ((typeof(x))((long)(x)|((long)(x)>>63)))
62+
#define masked_user_access_begin(x) ({ __uaccess_begin(); mask_user_address(x); })
63+
5664
/*
5765
* User pointers can have tag bits on x86-64. This scheme tolerates
5866
* arbitrary values in those bits rather then masking them off.

fs/select.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,9 @@ static inline int get_sigset_argpack(struct sigset_argpack *to,
780780
{
781781
// the path is hot enough for overhead of copy_from_user() to matter
782782
if (from) {
783-
if (!user_read_access_begin(from, sizeof(*from)))
783+
if (can_do_masked_user_access())
784+
from = masked_user_access_begin(from);
785+
else if (!user_read_access_begin(from, sizeof(*from)))
784786
return -EFAULT;
785787
unsafe_get_user(to->p, &from->p, Efault);
786788
unsafe_get_user(to->size, &from->size, Efault);

include/linux/uaccess.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
})
3333
#endif
3434

35+
#ifdef masked_user_access_begin
36+
#define can_do_masked_user_access() 1
37+
#else
38+
#define can_do_masked_user_access() 0
39+
#define masked_user_access_begin(src) NULL
40+
#endif
41+
3542
/*
3643
* Architectures should provide two primitives (raw_copy_{to,from}_user())
3744
* and get rid of their private instances of copy_{to,from}_user() and

lib/strncpy_from_user.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ long strncpy_from_user(char *dst, const char __user *src, long count)
120120
if (unlikely(count <= 0))
121121
return 0;
122122

123+
if (can_do_masked_user_access()) {
124+
long retval;
125+
126+
src = masked_user_access_begin(src);
127+
retval = do_strncpy_from_user(dst, src, count, count);
128+
user_read_access_end();
129+
return retval;
130+
}
131+
123132
max_addr = TASK_SIZE_MAX;
124133
src_addr = (unsigned long)untagged_addr(src);
125134
if (likely(src_addr < max_addr)) {

lib/strnlen_user.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ long strnlen_user(const char __user *str, long count)
9696
if (unlikely(count <= 0))
9797
return 0;
9898

99+
if (can_do_masked_user_access()) {
100+
long retval;
101+
102+
str = masked_user_access_begin(str);
103+
retval = do_strnlen_user(str, count, count);
104+
user_read_access_end();
105+
return retval;
106+
}
107+
99108
max_addr = TASK_SIZE_MAX;
100109
src_addr = (unsigned long)untagged_addr(str);
101110
if (likely(src_addr < max_addr)) {

0 commit comments

Comments
 (0)